Skip to content
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
10 changes: 10 additions & 0 deletions internal/cli/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion internal/engine/mrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading