Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix git url with ambiguous ref #382

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion get_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
60 changes: 60 additions & 0 deletions get_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down