config/module: git supports tags
This commit is contained in:
parent
7e94f7d4a9
commit
dcb900470c
|
@ -16,15 +16,38 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
|
||||||
return fmt.Errorf("git must be available and on the PATH")
|
return fmt.Errorf("git must be available and on the PATH")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Extract some query parameters we use
|
||||||
|
q := u.Query()
|
||||||
|
tag := q.Get("tag")
|
||||||
|
q.Del("tag")
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
// First: clone or update the repository
|
||||||
_, err := os.Stat(dst)
|
_, err := os.Stat(dst)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return g.update(dst, u)
|
err = g.update(dst, u)
|
||||||
|
} else {
|
||||||
|
err = g.clone(dst, u)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.clone(dst, u)
|
// Next: check out the proper tag/branch if it is specified, and checkout
|
||||||
|
if tag == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return g.checkout(dst, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GitGetter) checkout(dst string, ref string) error {
|
||||||
|
cmd := exec.Command("git", "checkout", ref)
|
||||||
|
cmd.Dir = dst
|
||||||
|
return getRunCommand(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitGetter) clone(dst string, u *url.URL) error {
|
func (g *GitGetter) clone(dst string, u *url.URL) error {
|
||||||
|
|
|
@ -49,3 +49,38 @@ func TestGitGetter(t *testing.T) {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGitGetter_tag(t *testing.T) {
|
||||||
|
if !testHasGit {
|
||||||
|
t.Log("git not found, skipping")
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
|
||||||
|
g := new(GitGetter)
|
||||||
|
dst := tempDir(t)
|
||||||
|
|
||||||
|
// Git doesn't allow nested ".git" directories so we do some hackiness
|
||||||
|
// here to get around that...
|
||||||
|
moduleDir := filepath.Join(fixtureDir, "basic-git")
|
||||||
|
oldName := filepath.Join(moduleDir, "DOTgit")
|
||||||
|
newName := filepath.Join(moduleDir, ".git")
|
||||||
|
if err := os.Rename(oldName, newName); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
defer os.Rename(newName, oldName)
|
||||||
|
|
||||||
|
url := testModuleURL("basic-git")
|
||||||
|
q := url.Query()
|
||||||
|
q.Add("tag", "v1.0")
|
||||||
|
url.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
if err := g.Get(dst, url); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the main file exists
|
||||||
|
mainPath := filepath.Join(dst, "main_tag1.tf")
|
||||||
|
if _, err := os.Stat(mainPath); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
A commit
|
remove tag1
|
||||||
# Please enter the commit message for your changes. Lines starting
|
# Please enter the commit message for your changes. Lines starting
|
||||||
# with '#' will be ignored, and an empty message aborts the commit.
|
# with '#' will be ignored, and an empty message aborts the commit.
|
||||||
# On branch master
|
# On branch master
|
||||||
#
|
|
||||||
# Initial commit
|
|
||||||
#
|
|
||||||
# Changes to be committed:
|
# Changes to be committed:
|
||||||
# new file: main.tf
|
# deleted: main_tag1.tf
|
||||||
#
|
#
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
0000000000000000000000000000000000000000 497bc37401eb3c9b11865b1768725b64066eccee Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410850637 -0700 commit (initial): A commit
|
0000000000000000000000000000000000000000 497bc37401eb3c9b11865b1768725b64066eccee Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410850637 -0700 commit (initial): A commit
|
||||||
|
497bc37401eb3c9b11865b1768725b64066eccee 243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886526 -0700 commit: tag1
|
||||||
|
243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886536 -0700 commit: remove tag1
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
0000000000000000000000000000000000000000 497bc37401eb3c9b11865b1768725b64066eccee Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410850637 -0700 commit (initial): A commit
|
0000000000000000000000000000000000000000 497bc37401eb3c9b11865b1768725b64066eccee Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410850637 -0700 commit (initial): A commit
|
||||||
|
497bc37401eb3c9b11865b1768725b64066eccee 243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886526 -0700 commit: tag1
|
||||||
|
243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886536 -0700 commit: remove tag1
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1 +1 @@
|
||||||
497bc37401eb3c9b11865b1768725b64066eccee
|
1f31e97f053caeb5d6b7bffa3faf82941c99efa2
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
243f0fc5c4e586d1a3daa54c981b6f34e9ab1085
|
Loading…
Reference in New Issue