diff --git a/config/module/get_git.go b/config/module/get_git.go index b28122230..5ab27ba0b 100644 --- a/config/module/get_git.go +++ b/config/module/get_git.go @@ -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 { diff --git a/config/module/get_git_test.go b/config/module/get_git_test.go index 598444b6d..3885ff8e7 100644 --- a/config/module/get_git_test.go +++ b/config/module/get_git_test.go @@ -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 { diff --git a/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG b/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG index dd932c30f..d13fed6c9 100644 --- a/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG +++ b/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG @@ -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 # diff --git a/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD b/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD index 396932ba1..40709bc8e 100644 --- a/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD +++ b/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD @@ -1,3 +1,6 @@ 0000000000000000000000000000000000000000 497bc37401eb3c9b11865b1768725b64066eccee Mitchell Hashimoto 1410850637 -0700 commit (initial): A commit 497bc37401eb3c9b11865b1768725b64066eccee 243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 Mitchell Hashimoto 1410886526 -0700 commit: tag1 243f0fc5c4e586d1a3daa54c981b6f34e9ab1085 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto 1410886536 -0700 commit: remove tag1 +1f31e97f053caeb5d6b7bffa3faf82941c99efa2 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto 1410886909 -0700 checkout: moving from master to test-branch +1f31e97f053caeb5d6b7bffa3faf82941c99efa2 7b7614f8759ac8b5e4b02be65ad8e2667be6dd87 Mitchell Hashimoto 1410886913 -0700 commit: Branch +7b7614f8759ac8b5e4b02be65ad8e2667be6dd87 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto 1410886916 -0700 checkout: moving from test-branch to master diff --git a/config/module/test-fixtures/basic-git/DOTgit/logs/refs/heads/test-branch b/config/module/test-fixtures/basic-git/DOTgit/logs/refs/heads/test-branch new file mode 100644 index 000000000..937067a2a --- /dev/null +++ b/config/module/test-fixtures/basic-git/DOTgit/logs/refs/heads/test-branch @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 1f31e97f053caeb5d6b7bffa3faf82941c99efa2 Mitchell Hashimoto 1410886909 -0700 branch: Created from HEAD +1f31e97f053caeb5d6b7bffa3faf82941c99efa2 7b7614f8759ac8b5e4b02be65ad8e2667be6dd87 Mitchell Hashimoto 1410886913 -0700 commit: Branch diff --git a/config/module/test-fixtures/basic-git/DOTgit/objects/40/4618c9d96dfa0a5d365b518e0dfbb5a387c649 b/config/module/test-fixtures/basic-git/DOTgit/objects/40/4618c9d96dfa0a5d365b518e0dfbb5a387c649 new file mode 100644 index 000000000..434fcab20 Binary files /dev/null and b/config/module/test-fixtures/basic-git/DOTgit/objects/40/4618c9d96dfa0a5d365b518e0dfbb5a387c649 differ diff --git a/config/module/test-fixtures/basic-git/DOTgit/objects/7b/7614f8759ac8b5e4b02be65ad8e2667be6dd87 b/config/module/test-fixtures/basic-git/DOTgit/objects/7b/7614f8759ac8b5e4b02be65ad8e2667be6dd87 new file mode 100644 index 000000000..abe281a74 --- /dev/null +++ b/config/module/test-fixtures/basic-git/DOTgit/objects/7b/7614f8759ac8b5e4b02be65ad8e2667be6dd87 @@ -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„ \ No newline at end of file diff --git a/config/module/test-fixtures/basic-git/DOTgit/refs/heads/test-branch b/config/module/test-fixtures/basic-git/DOTgit/refs/heads/test-branch new file mode 100644 index 000000000..a5f298b83 --- /dev/null +++ b/config/module/test-fixtures/basic-git/DOTgit/refs/heads/test-branch @@ -0,0 +1 @@ +7b7614f8759ac8b5e4b02be65ad8e2667be6dd87