Skip to content

Commit dcb9004

Browse files
committed
config/module: git supports tags
1 parent 7e94f7d commit dcb9004

11 files changed

Lines changed: 68 additions & 8 deletions

File tree

‎config/module/get_git.go‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,38 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
1616
return fmt.Errorf("git must be available and on the PATH")
1717
}
1818

19+
// Extract some query parameters we use
20+
q := u.Query()
21+
tag := q.Get("tag")
22+
q.Del("tag")
23+
u.RawQuery = q.Encode()
24+
25+
// First: clone or update the repository
1926
_, err := os.Stat(dst)
2027
if err != nil && !os.IsNotExist(err) {
2128
return err
2229
}
2330
if err == nil {
24-
return g.update(dst, u)
31+
err = g.update(dst, u)
32+
} else {
33+
err = g.clone(dst, u)
34+
}
35+
if err != nil {
36+
return err
37+
}
38+
39+
// Next: check out the proper tag/branch if it is specified, and checkout
40+
if tag == "" {
41+
return nil
2542
}
2643

27-
return g.clone(dst, u)
44+
return g.checkout(dst, tag)
45+
}
46+
47+
func (g *GitGetter) checkout(dst string, ref string) error {
48+
cmd := exec.Command("git", "checkout", ref)
49+
cmd.Dir = dst
50+
return getRunCommand(cmd)
2851
}
2952

3053
func (g *GitGetter) clone(dst string, u *url.URL) error {

‎config/module/get_git_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,38 @@ func TestGitGetter(t *testing.T) {
4949
t.Fatalf("err: %s", err)
5050
}
5151
}
52+
53+
func TestGitGetter_tag(t *testing.T) {
54+
if !testHasGit {
55+
t.Log("git not found, skipping")
56+
t.Skip()
57+
}
58+
59+
g := new(GitGetter)
60+
dst := tempDir(t)
61+
62+
// Git doesn't allow nested ".git" directories so we do some hackiness
63+
// here to get around that...
64+
moduleDir := filepath.Join(fixtureDir, "basic-git")
65+
oldName := filepath.Join(moduleDir, "DOTgit")
66+
newName := filepath.Join(moduleDir, ".git")
67+
if err := os.Rename(oldName, newName); err != nil {
68+
t.Fatalf("err: %s", err)
69+
}
70+
defer os.Rename(newName, oldName)
71+
72+
url := testModuleURL("basic-git")
73+
q := url.Query()
74+
q.Add("tag", "v1.0")
75+
url.RawQuery = q.Encode()
76+
77+
if err := g.Get(dst, url); err != nil {
78+
t.Fatalf("err: %s", err)
79+
}
80+
81+
// Verify the main file exists
82+
mainPath := filepath.Join(dst, "main_tag1.tf")
83+
if _, err := os.Stat(mainPath); err != nil {
84+
t.Fatalf("err: %s", err)
85+
}
86+
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
A commit
1+
remove tag1
22
# Please enter the commit message for your changes. Lines starting
33
# with '#' will be ignored, and an empty message aborts the commit.
44
# On branch master
5-
#
6-
# Initial commit
7-
#
85
# Changes to be committed:
9-
# new file: main.tf
6+
# deleted: main_tag1.tf
107
#
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
0000000000000000000000000000000000000000 497bc37401eb3c9b11865b1768725b64066eccee Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410850637 -0700 commit (initial): A commit
2+
497bc37401eb3c9b11865b1768725b64066eccee 243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886526 -0700 commit: tag1
3+
243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886536 -0700 commit: remove tag1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
0000000000000000000000000000000000000000 497bc37401eb3c9b11865b1768725b64066eccee Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410850637 -0700 commit (initial): A commit
2+
497bc37401eb3c9b11865b1768725b64066eccee 243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886526 -0700 commit: tag1
3+
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.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
497bc37401eb3c9b11865b1768725b64066eccee
1+
1f31e97f053caeb5d6b7bffa3faf82941c99efa2

0 commit comments

Comments
 (0)