Skip to content

feat(integration): detect rebase-merged branches by per-commit patch-id - #4824

Open
lahma wants to merge 2 commits into
max-sixty:mainfrom
lahma:feat/rebase-merge-detection
Open

lahma wants to merge 2 commits into
max-sixty:mainfrom
lahma:feat/rebase-merge-detection

Conversation

@lahma

@lahma lahma commented Nov 16, 2026

Copy link
Copy Markdown

Problem

With a rebase-merge workflow (GitHub "Rebase and merge", Bitbucket "Rebase, fast-forward"), a multi-commit branch lands on the default branch as new commits. Once the default branch edits the same lines again, none of the six integration checks match:

  • the branch is not an ancestor (the landed commits have new SHAs),
  • git merge-tree conflicts, so MergeAddsNothing can't apply,
  • no single target commit carries the branch's combined diff, so PatchIdMatch (squash detection) fails.

git cherry main <branch> prints only - lines, yet wt step prune and wt remove keep the branch, and wt list shows ✗ instead of ⊂. On a Bitbucket-hosted monorepo that rebase-merges every PR, worktrees of merged branches pile up: seven such branches there (2–6 commits each, 105–935 commits behind the default branch) are kept, although every one of their commits has a patch-id --verbatim twin on the default branch.

I found no existing issue or PR for rebase-merged multi-commit branches. #4437 reworks the squash scan past the cap and is orthogonal to this; whichever lands second rebases.

Change

A seventh check, commits_match (IntegrationReason::CommitsMatch), evaluated inside the existing patch-id fallback, so only branches whose merge-tree conflicts reach it. A branch counts as integrated when every commit in merge-base..branch has a distinct commit in merge-base..target with the same patch-id --verbatim.

Safety argument — this loosens the check that lets wt remove / wt step prune delete a branch without -D, so it needs a maintainer's call under the data-loss review policy:

  • Patch-id equality is the only evidence. The match is a multiset: each target commit vouches for one branch commit, so branch +X, −X, +X against target +X, −X is kept.
  • A branch commit without a patch-id fails the check: an empty commit, or a merge (diff-tree --stdin emits no patch for a merge, whose conflict resolution no patch-id covers).
  • The merge-tree gate is unchanged: a clean merge that would change the target still means "not integrated" (pinned by a revert test).
  • Both sides come from the same plumbing diff-tree | patch-id pipe, so diff.* config can't skew the ids (same guarantee as fix(integration): make squash-merge detection immune to diff.* git config #2821 / fix(git): run diffs through plumbing so user git config can't change what wt reads #4146).
  • PatchIdMatch keeps precedence, so a one-commit branch reports exactly what it reports today.

The tests pin each refusal: partial landing, a commit added after the merge, repeated patches, a merge on the branch, an empty commit, amended content with the same author identity, and a revert without later edits. I mutation-checked them: replacing the multiset with a set, accepting a commit without a patch-id, ranking CommitsMatch above PatchIdMatch, adding #[serde(default)] to the new cache field, treating author identity as evidence, and an off-by-one at the walk cap each fail a test.

Cost

The two commits can land separately.

1. feat(integration) — the per-commit check under the existing 500-commit cap. The branch's own commits ride the target-side diff-tree --stdin | patch-id pipe that already runs, so the only new process is one rev-list over the branch's own commits. No new full-range diff.

2. perf(integration) — past the cap, select candidates by author identity. Rebase, cherry-pick and forge rebase merges keep the author name, email and date. A header-only rev-list --format=%H %at %ae walk replaces the rev-list --count pre-flight, and past 500 commits only target commits sharing a branch commit's author identity are diffed (at most 500). A never-merged stale branch is rejected with no diffing at all. The walk stops at 10,000 commits (the range cap #4100 and #4437 chose). The identity only selects candidates; patch-ids still decide.

branch shape before after
clean merge-tree (most branches) merge-tree unchanged
conflicting, ≤ 500 behind count + squash pipe + target pipe header walk + branch walk; branch commits ride the existing pipe
conflicting, 500–10,000 behind, never merged count header walk + branch walk; no diffs
conflicting, 500–10,000 behind, rebase-merged count header walk + branch walk + one pipe over the few same-author commits
conflicting, > 10,000 behind count header walk, stopped at 10,001 commits

Timings of those git commands on this repository's own history (Linux, median of 7 runs, no commit-graph):

commits behind rev-list --count (before) header walk (after) branch walk (new)
299 18 ms 18 ms 9 ms
1,999 50 ms 70 ms 11 ms
4,999 81 ms 120 ms 11 ms

Under the cap, appending a three-commit branch to the existing target pipe took it from 1547 ms to 1562 ms. The worst case, a never-merged conflicting branch 500–10,000 commits behind, goes from one --count to the header walk plus the branch walk: about 1.6×, +30–50 ms. Because candidates past the cap are chosen by author identity rather than by path, the stale lockfile-bump case discussed on #4437 diffs nothing unless one of its commits actually landed.

MergeProbeResult gains a required field, so cached probe entries written before it read as misses and old negatives are recomputed once (pinned by a cache test).

Docs

wt remove --help lists the seventh condition and the reworded cap sentence; wt list --help and the JSON schema gain commits_match. Mirrors and help snapshots are regenerated.

Out of scope: amended commits whose patch changed (matching on subject or author would delete unique content), and non-default targets (#4637).

A branch landed by Bitbucket's "Rebase, fast-forward" or GitHub's "Rebase
and merge" arrives on the default branch as new commits. Once the default
branch edits the same lines again, every existing check misses it: it is
not an ancestor, `merge-tree` conflicts, and no single target commit holds
the branch's combined diff, so `PatchIdMatch` fails too. `wt step prune`
and `wt remove` then keep such branches indefinitely, and `wt list` shows
them as `✗` rather than `⊂`, although `git cherry` reports every commit as
already upstream.

Add a seventh check, `CommitsMatch` (`commits_match` in JSON), evaluated
in the same patch-id fallback, after `PatchIdMatch`. A branch counts as
integrated when every commit in `merge_base..branch` has a distinct target
commit in `merge_base..target` with the same `patch-id --verbatim`:

- Patch-id equality is the only evidence, matched as a multiset, so each
  target commit vouches for one branch commit.
- A commit without a patch-id fails the check: an empty commit, or a
  merge, whose conflict resolution no patch-id covers.
- The merge-tree gate is unchanged: a clean merge that would change the
  target still means "not integrated".
- The branch's commits ride the target-side `diff-tree --stdin | patch-id`
  pipe that already runs, under the existing 500-commit cap; the extra
  cost is one `rev-list` over the branch's own commits.

`MergeProbeResult` gains a required field, so cached entries written
before it read as misses and old negatives are recomputed once.
… identity

The per-commit patch-id check only ran within the 500-commit cap, so a
rebase-merged branch whose merge point has since fallen further behind
the default branch stayed undetected.

A rebase, a cherry-pick and a forge's rebase merge all keep the author
name, email and time. So past the cap, diff only the target commits that
share an author identity with a branch commit, at most 500 of them:

- A header-only `rev-list --format=%H %at %ae` walk replaces the
  `rev-list --count` pre-flight. Its length is the count, and it stops
  listing at 10,000 commits, past which both checks give up.
- When some branch commit's identity is absent from the target, the
  branch is rejected without diffing anything: the common case for a
  stale branch that was never merged.
- The identity only selects candidates. Patch-ids still decide, so an
  amended commit that kept its author time is not a match.
- Within the cap nothing changes: every target commit is diffed, which
  the squash check needs and which also finds rebase merges that reset
  author times.

On this repository's history, the header walk costs 18 ms against
18 ms for `--count` 299 commits back, 70 against 50 ms 2,000 back, and
120 against 81 ms 5,000 back; the branch-side walk adds about 10 ms.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Holding this for a maintainer decision. CommitsMatch widens which branches wt remove and wt step prune delete without -D. The repo's review policy reserves that call for @max-sixty, so this is a comment, not an approval.

One trade-off to weigh: suppose main lands a branch's commits, reverts one of them, and later edits lines another one touched. Merge-tree then conflicts, every commit still has a twin, and the branch is deleted. The reverted content survives only in main's history. The squash check already accepts the same shape (squash, revert, re-edit), so this extends that trade-off to rebase merges rather than adding a new one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants