From dcb900470c989038d77637e051d2c412e53643c9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 16 Sep 2014 09:55:51 -0700 Subject: [PATCH] config/module: git supports tags --- config/module/get_git.go | 27 +++++++++++++- config/module/get_git_test.go | 35 ++++++++++++++++++ .../basic-git/DOTgit/COMMIT_EDITMSG | 7 +--- .../test-fixtures/basic-git/DOTgit/logs/HEAD | 2 + .../basic-git/DOTgit/logs/refs/heads/master | 2 + .../1f/31e97f053caeb5d6b7bffa3faf82941c99efa2 | Bin 0 -> 167 bytes .../24/3f0fc5c4e586d1a3daa54c981b6f34e9ab1085 | Bin 0 -> 164 bytes .../b7/757b6a3696ad036e9aa2f5b4856d09e7f17993 | Bin 0 -> 82 bytes .../e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 | Bin 0 -> 15 bytes .../basic-git/DOTgit/refs/heads/master | 2 +- .../basic-git/DOTgit/refs/tags/v1.0 | 1 + 11 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 config/module/test-fixtures/basic-git/DOTgit/objects/1f/31e97f053caeb5d6b7bffa3faf82941c99efa2 create mode 100644 config/module/test-fixtures/basic-git/DOTgit/objects/24/3f0fc5c4e586d1a3daa54c981b6f34e9ab1085 create mode 100644 config/module/test-fixtures/basic-git/DOTgit/objects/b7/757b6a3696ad036e9aa2f5b4856d09e7f17993 create mode 100644 config/module/test-fixtures/basic-git/DOTgit/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 create mode 100644 config/module/test-fixtures/basic-git/DOTgit/refs/tags/v1.0 diff --git a/config/module/get_git.go b/config/module/get_git.go index 132bd8e11..0507bf344 100644 --- a/config/module/get_git.go +++ b/config/module/get_git.go @@ -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") } + // 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) if err != nil && !os.IsNotExist(err) { return err } 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 { diff --git a/config/module/get_git_test.go b/config/module/get_git_test.go index 33a0a98a6..4c81bbbe1 100644 --- a/config/module/get_git_test.go +++ b/config/module/get_git_test.go @@ -49,3 +49,38 @@ func TestGitGetter(t *testing.T) { 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) + } +} diff --git a/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG b/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG index fe6bc2b3d..dd932c30f 100644 --- a/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG +++ b/config/module/test-fixtures/basic-git/DOTgit/COMMIT_EDITMSG @@ -1,10 +1,7 @@ -A commit +remove tag1 # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master -# -# Initial commit -# # Changes to be committed: -# new file: main.tf +# deleted: main_tag1.tf # diff --git a/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD b/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD index ad39b8a0f..396932ba1 100644 --- a/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD +++ b/config/module/test-fixtures/basic-git/DOTgit/logs/HEAD @@ -1 +1,3 @@ 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 diff --git a/config/module/test-fixtures/basic-git/DOTgit/logs/refs/heads/master b/config/module/test-fixtures/basic-git/DOTgit/logs/refs/heads/master index ad39b8a0f..396932ba1 100644 --- a/config/module/test-fixtures/basic-git/DOTgit/logs/refs/heads/master +++ b/config/module/test-fixtures/basic-git/DOTgit/logs/refs/heads/master @@ -1 +1,3 @@ 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 diff --git a/config/module/test-fixtures/basic-git/DOTgit/objects/1f/31e97f053caeb5d6b7bffa3faf82941c99efa2 b/config/module/test-fixtures/basic-git/DOTgit/objects/1f/31e97f053caeb5d6b7bffa3faf82941c99efa2 new file mode 100644 index 0000000000000000000000000000000000000000..5793a840b76235dafcb144ed66d1b8d274ed76bf GIT binary patch literal 167 zcmV;Y09gNc0jde)V4 V5S{)q#u}CSCf;!s>;oX7Q=N++Phyo>KhY@))4ka?%IV3*@oJ1_}!Me>)YD$BHN~~{94vhTh*t2;A_M3v&+5kx(&IOvjz`l>`{yQXvi4V SwJ)0dC8iqRL45!N?@<9%)ls?t literal 0 HcmV?d00001 diff --git a/config/module/test-fixtures/basic-git/DOTgit/objects/b7/757b6a3696ad036e9aa2f5b4856d09e7f17993 b/config/module/test-fixtures/basic-git/DOTgit/objects/b7/757b6a3696ad036e9aa2f5b4856d09e7f17993 new file mode 100644 index 0000000000000000000000000000000000000000..10192566594b6bfa1e9d04c558444006bb2f27fe GIT binary patch literal 82 zcmV-Y0ImOc0V^p=O;s>AWiT`_Ff%bx$W6@5(<@11urNq2jQC!%i0{sU{W8An8}_#! ou|-l6Uy_(^2vYZK?xWe8E?#r??$%sa9(Ci;lb+y-0LsiEdb9E;RsaA1 literal 0 HcmV?d00001 diff --git a/config/module/test-fixtures/basic-git/DOTgit/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 b/config/module/test-fixtures/basic-git/DOTgit/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 new file mode 100644 index 0000000000000000000000000000000000000000..711223894375fe1186ac5bfffdc48fb1fa1e65cc GIT binary patch literal 15 Wcmb