diff --git a/internal/cli/submit_test.go b/internal/cli/submit_test.go index a2cf07c..7a39a95 100644 --- a/internal/cli/submit_test.go +++ b/internal/cli/submit_test.go @@ -35,6 +35,16 @@ func TestPush(t *testing.T) { if !strings.HasPrefix(h.remoteSHA("c"), h.sha("c")) { t.Fatal("rebased c not pushed") } + // The host restacked c itself (same changes, new commits): push takes over. + host := h.repo.Clone() + host.Git("checkout", "--quiet", "c") + host.Git("commit", "--quiet", "--amend", "-m", "c1 restacked by the host") + host.Git("push", "--quiet", "--force", "origin", "c") + h.repo.Git("fetch", "--quiet") + h.must("push") + if !strings.HasPrefix(h.remoteSHA("c"), h.sha("c")) { + t.Fatal("push refused a remote c that holds the same changes") + } // Someone else pushed to b: the lease must refuse to clobber it. h.repo.PushFromElsewhere("b", "theirs.txt", "x\n", "their commit") h.repo.Git("commit", "--quiet", "--amend", "-m", "a1 amended again") diff --git a/internal/engine/mrs.go b/internal/engine/mrs.go index 2196e4f..1c5aaa9 100644 --- a/internal/engine/mrs.go +++ b/internal/engine/mrs.go @@ -78,6 +78,8 @@ func belongs(g *git.Repo, branch string, mr *forge.CR) bool { // if that value was integrated locally (git.Repo.Includes): a commit someone // else pushed, such as an applied review suggestion, is refused rather than // overwritten, even right after a fetch has refreshed the tracking refs. +// Remote commits whose changes the branch already has (git.Repo.HasPatches), +// such as GitHub restacking a native stack after a merge, are overwritten. func Push(g *git.Repo, remote string, branches []string) error { if len(branches) == 0 { return nil @@ -87,7 +89,7 @@ func Push(g *git.Repo, remote string, branches []string) error { for _, b := range branches { lease := "" if sha, err := g.RevParse("refs/remotes/" + remote + "/" + b); err == nil { - if !g.Includes(b, sha) { + if !g.Includes(b, sha) && !g.HasPatches(b, sha) { foreign = append(foreign, b) continue } diff --git a/internal/git/git.go b/internal/git/git.go index eb6e398..e601efc 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -342,6 +342,18 @@ func (r *Repo) Includes(branch, sha string) bool { return false } +// HasPatches reports whether every commit sha has and the local branch lacks +// makes a change the branch already has, as when a host rebased the branch +// onto the same base itself. Merge commits never count. +func (r *Repo) HasPatches(branch, sha string) bool { + rng := "refs/heads/" + branch + "..." + sha + if out, err := r.Run("rev-list", "--right-only", "--merges", rng); err != nil || out != "" { + return false + } + out, err := r.Run("rev-list", "--right-only", "--cherry-pick", rng) + return err == nil && out == "" +} + // maxReflogChecks bounds the ancestry checks Includes makes against reflog entries. const maxReflogChecks = 30 diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 3ae713f..d08698d 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -137,6 +137,35 @@ func TestIncludes(t *testing.T) { } } +func TestHasPatches(t *testing.T) { + t.Parallel() + tr := testutil.NewRepo(t) + g := &git.Repo{Dir: tr.Dir} + tr.Git("checkout", "--quiet", "-b", "feat") + tr.Commit("a.txt", "a\n", "a") + // The host rebased the branch itself: the same change, a new commit. + tr.Git("branch", "rebased", "main") + tr.Git("checkout", "--quiet", "rebased") + tr.Git("cherry-pick", "feat") + tr.Git("commit", "--quiet", "--amend", "-m", "a, rebased by the host") + rebased := tr.Git("rev-parse", "HEAD") + if g.Includes("feat", rebased) { + t.Fatal("the host's rebase should not count as included") + } + if !g.HasPatches("feat", rebased) { + t.Fatal("a commit with a change the branch has is not recognised") + } + extra := tr.Commit("b.txt", "b\n", "someone else's") + if g.HasPatches("feat", extra) { + t.Fatal("a new change is seen as already in the branch") + } + tr.Git("reset", "--quiet", "--hard", rebased) + tr.Git("merge", "--quiet", "--no-ff", "-m", "merge feat", "feat") + if g.HasPatches("feat", tr.Git("rev-parse", "HEAD")) { + t.Fatal("a merge commit is seen as already in the branch") + } +} + func TestBranchNamesAndTags(t *testing.T) { t.Parallel() tr := testutil.NewRepo(t)