config/module: support branches in git

This commit is contained in:
Mitchell Hashimoto 2014-09-16 10:02:11 -07:00
parent 13d892217c
commit ac19a488d2
8 changed files with 63 additions and 9 deletions

View File

@ -17,11 +17,11 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
}
// Extract some query parameters we use
var tag string
var ref string
q := u.Query()
if len(q) > 0 {
tag = q.Get("tag")
q.Del("tag")
ref = q.Get("ref")
q.Del("ref")
// Copy the URL
var newU url.URL = *u
@ -44,11 +44,11 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
}
// Next: check out the proper tag/branch if it is specified, and checkout
if tag == "" {
if ref == "" {
return nil
}
return g.checkout(dst, tag)
return g.checkout(dst, ref)
}
func (g *GitGetter) checkout(dst string, ref string) error {

View File

@ -50,6 +50,52 @@ func TestGitGetter(t *testing.T) {
}
}
func TestGitGetter_branch(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("ref", "test-branch")
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_branch.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
// Get again should work
if err := g.Get(dst, url); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath = filepath.Join(dst, "main_branch.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestGitGetter_tag(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
@ -71,7 +117,7 @@ func TestGitGetter_tag(t *testing.T) {
url := testModuleURL("basic-git")
q := url.Query()
q.Add("tag", "v1.0")
q.Add("ref", "v1.0")
url.RawQuery = q.Encode()
if err := g.Get(dst, url); err != nil {

View File

@ -1,7 +1,7 @@
remove tag1
Branch
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# On branch test-branch
# Changes to be committed:
# deleted: main_tag1.tf
# new file: main_branch.tf
#

View File

@ -1,3 +1,6 @@
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
1f31e97f053caeb5d6b7bffa3faf82941c99efa2 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886909 -0700 checkout: moving from master to test-branch
1f31e97f053caeb5d6b7bffa3faf82941c99efa2 7b7614f8759ac8b5e4b02be65ad8e2667be6dd87 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886913 -0700 commit: Branch
7b7614f8759ac8b5e4b02be65ad8e2667be6dd87 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886916 -0700 checkout: moving from test-branch to master

View File

@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886909 -0700 branch: Created from HEAD
1f31e97f053caeb5d6b7bffa3faf82941c99efa2 7b7614f8759ac8b5e4b02be65ad8e2667be6dd87 Mitchell Hashimoto <mitchell.hashimoto@gmail.com> 1410886913 -0700 commit: Branch

View File

@ -0,0 +1,2 @@
x­ÎK
Â0…aÇYE6`Iš7ˆˆ#'.â&¹×šFbÜ¿EpN?ø'µZ—Ág££#r-´•>…l&`²²&éQdŠÑ€ò.YØ:nƒKRƒ#aTŒ&Ûè"(òsÐ2…€3ƒ÷(­óû2RÁuå7x•¥¶Ñøi?ðµ©üìò¨°¬SjõÌ¥Â{¤âGá„`»îÅÀ¿Œ±k‡-öÛS„

View File

@ -0,0 +1 @@
7b7614f8759ac8b5e4b02be65ad8e2667be6dd87