From 5ce69c3ebb085a4c50de5ca6c2f29e5bb4eaacc0 Mon Sep 17 00:00:00 2001 From: Nima Saed-Samii Date: Wed, 5 Oct 2022 18:04:26 +0200 Subject: [PATCH 1/2] Make git checkout command unambiguous --- get_git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/get_git.go b/get_git.go index db89edef8..1b0cdd077 100644 --- a/get_git.go +++ b/get_git.go @@ -174,7 +174,7 @@ func (g *GitGetter) GetFile(dst string, u *url.URL) error { } func (g *GitGetter) checkout(ctx context.Context, dst string, ref string) error { - cmd := exec.CommandContext(ctx, "git", "checkout", ref) + cmd := exec.CommandContext(ctx, "git", "checkout", ref, "--") cmd.Dir = dst return getRunCommand(cmd) } From 24a23dc92e54870f6c3917fa1c668aa2049b47a8 Mon Sep 17 00:00:00 2001 From: Nima Saed-Samii Date: Wed, 5 Oct 2022 18:05:17 +0200 Subject: [PATCH 2/2] Add test for ambiguous git refs --- get_git_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/get_git_test.go b/get_git_test.go index df6ad0390..23454b82c 100644 --- a/get_git_test.go +++ b/get_git_test.go @@ -305,6 +305,50 @@ func TestGitGetter_shallowCloneWithCommitID(t *testing.T) { } } +func TestGitGetter_branchAmbiguous(t *testing.T) { + if !testHasGit { + t.Skip("git not found, skipping") + } + + g := new(GitGetter) + dst := tempDir(t) + + repo := testGitRepo(t, "branch") + repo.commitFileInDir("file.txt", "ambiguous", "branch-and-file-with-same-name") + commitID, err := repo.latestCommit() + if err != nil { + t.Fatal(err) + } + repo.git("checkout", "-b", "ambiguous") + repo.commitFile("branch.txt", "branch") + repo.git("checkout", commitID) + + q := repo.url.Query() + q.Add("ref", "ambiguous") + repo.url.RawQuery = q.Encode() + + if err := g.Get(dst, repo.url); err != nil { + t.Fatalf("err: %s", err) + } + + // Verify the main file exists + mainPath := filepath.Join(dst, "branch.txt") + if _, err := os.Stat(mainPath); err != nil { + t.Fatalf("err: %s", err) + } + + // Get again should work + if err := g.Get(dst, repo.url); err != nil { + t.Fatalf("err: %s", err) + } + + // Verify the main file exists + mainPath = filepath.Join(dst, "branch.txt") + if _, err := os.Stat(mainPath); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestGitGetter_branchUpdate(t *testing.T) { if !testHasGit { t.Skip("git not found, skipping") @@ -868,6 +912,22 @@ func (r *gitRepo) commitFile(file, content string) { r.git("commit", "-m", "Adding "+file) } +// commitFile writes and commits a text file to the repo. +func (r *gitRepo) commitFileInDir(file, dir, content string) { + dir_path := filepath.Join(r.dir, dir) + if err := os.Mkdir(dir_path, 0755); err != nil { + r.t.Fatal(err) + } + + rel_file_path := filepath.Join(dir, file) + file_path := filepath.Join(dir_path, file) + if err := ioutil.WriteFile(file_path, []byte(content), 0600); err != nil { + r.t.Fatal(err) + } + r.git("add", rel_file_path) + r.git("commit", "-m", "Adding "+rel_file_path) +} + // latestCommit returns the full commit id of the latest commit on the current // branch. func (r *gitRepo) latestCommit() (string, error) {