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: Repo.Diff shouldn't fail given ambiguous branch names #203

Merged
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
7 changes: 4 additions & 3 deletions cmd/av/stack_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ func getUpstreamStatus(repo *git.Repo, branch meta.Branch) (string, error) {
}

upstreamBranch := fmt.Sprintf("remotes/origin/%s", branch.Name)
upstreamDiff, err := repo.Diff(
&git.DiffOpts{Quiet: true, Specifiers: []string{branch.Name, upstreamBranch}},
)
upstreamDiff, err := repo.Diff(&git.DiffOpts{
Quiet: true,
Specifiers: []string{branch.Name, upstreamBranch},
})
if err != nil {
return "", err
}
Expand Down
24 changes: 24 additions & 0 deletions e2e_tests/stack_tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package e2e_tests

import (
"testing"

"github.com/aviator-co/av/internal/git/gittest"
)

func TestStackTree(t *testing.T) {
repo := gittest.NewTempRepo(t)
Chdir(t, repo.Dir())

RequireAv(t, "stack", "branch", "foo")
gittest.CommitFile(t, repo, "foo", []byte("foo"))

RequireAv(t, "stack", "branch", "bar")
gittest.CommitFile(t, repo, "bar", []byte("bar"))

gittest.CheckoutBranch(t, repo, "main")
RequireAv(t, "stack", "branch", "spam")
gittest.CommitFile(t, repo, "spam", []byte("spam"))

RequireAv(t, "stack", "tree")
}
11 changes: 7 additions & 4 deletions internal/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ func (r *Repo) Diff(d *DiffOpts) (*Diff, error) {
}

args = append(args, d.Specifiers...)

// This needs to be last because everything after the `--` is interpreted
// as a path, not a flag.
if len(d.Paths) > 0 {
args = append(args, "--")
args = append(args, d.Paths...)
}
// Note that we still append this `--` even if there are no paths because
// otherwise Git might interpret a specifier as ambiguous path and raise an
// error.
args = append(args, "--")
args = append(args, d.Paths...)

output, err := r.Run(&RunOpts{
Args: args,
})
Expand Down
28 changes: 28 additions & 0 deletions internal/git/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package git_test

import (
"testing"

"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/git/gittest"
"github.com/stretchr/testify/require"
)

func TestRepoDiffAmbiguousPathName(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case fails without the change to Repo.Diff in this pull request.

repo := gittest.NewTempRepo(t)

_, err := repo.CheckoutBranch(&git.CheckoutBranch{Name: "foo", NewBranch: true})
require.NoError(t, err, "repo.CheckoutBranch should not error given a valid branch name")

gittest.CommitFile(t, repo, "foo", []byte("foo"))
diff, err := repo.Diff(&git.DiffOpts{
Quiet: true,
Specifiers: []string{"main", "foo"},
})
require.NoError(t, err, "repo.Diff should not error given an ambiguous branch/path name")
require.False(
t,
diff.Empty,
"diff between branches with different trees should return non-empty",
)
}