Skip to content

tools/pr-land.py: restack a stacked branch by merges after the PR below squashes - #1055

Merged
GLinnik21 merged 10 commits into
mainfrom
tools/pr-land-restack
Oct 26, 2026
Merged

GLinnik21 merged 10 commits into
mainfrom
tools/pr-land-restack

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

Stacked on #1053 (base is that PR's branch, so the diff is only restack). After #1053 squashes, this restacks with the very procedure it implements.

What

tools/pr-land.py restack --worktree W --upper BRANCH --lower PR [--dry-run] [--push]

After the PR below a stacked PR squash-merges as commit S, the upper branch is brought current by merges, never a rebase:

  1. merge the lower PR's final head
  2. merge S^
  3. merge --no-ff -s ours S (marks S merged without changing the upper tree, which already has the lower PR's content)
  4. merge origin/<base>

Result: git diff origin/<base>..BRANCH is only the upper PR's own change.

Safety, all before any fetch or merge:

  • the lower PR must be MERGED with a merge commit; S must exist locally, be an ancestor of origin/<base> and have exactly one parent
  • W must be clean, on BRANCH, and have no merge, cherry-pick or revert in progress
  • --upper must be a plain branch name (not a flag or refspec) and must not be the base branch
  • --dry-run fetches nothing and changes nothing

On a conflict it stops at the failing step and leaves the merge in progress for the human; it never aborts, resets, rebases or forces. --push runs only after all four merges, is a plain push, follows the branch's upstream (origin/<name> only; any other remote is refused before the merges), and reports the git rejection reason.

Why

The manual procedure lives in AGENTS.md prose and was done by hand. Step 3 alone (-s ours on the squash) drops whatever main gained before it; the four-step order is the part that is easy to get wrong.

Verification

  • python3 tools/test_pr_land.py: 88 tests OK (hermetic: temp dirs, a local bare origin, a fake gh on PATH, no network)
  • python3 ci/test_source_bundle.py: 43 tests OK
  • Each new behaviour was written test-first and failed first for the stated reason; the core steps were mutation-checked (drop step 3, swap steps 1 and 2, a fetching dry run and a --force push each turn the suite red)
  • One independent review: no blocking findings; 5 of its 6 minor findings are fixed here. Not changed: --dry-run prints "It would run" before three read-only checks that can still refuse; its docstring already says the checks come first.

Not verified

  • Never run against real GitHub. refs/pull/N/head is fetched and exercised only against a local bare origin.
  • A cherry-pick or revert in progress shares the code path of the merge-in-progress refusal and has no dedicated test.
  • %(upstream:remotename) was exercised on one git version only (it exists since git 2.13).

No tracking issue (owner: none).

🤖 Generated with Claude Code

https://claude.ai/code/session_014bHMduhNfrC8w3qjNhEWdB

GLinnik21 and others added 7 commits October 26, 2026 04:46
…o not allow

gate is the read-only verdict: the PR open, not a draft, on main or release/v*,
at the asserted head, every required context of the base's rules reported and
passing, nothing pending, failed or cancelled, mergeStateStatus CLEAN (UNKNOWN
and an all-green BLOCKED are re-polled; BEHIND and DIRTY say to merge the base
into the branch). wait polls it with a 30-60 s backoff, never gh run list.
merge is wait, then gh pr merge --squash --match-head-commit (never --auto)
with a required body, retrying only the head-oid race, then a verify against
origin/<base>. The subject and body go through outbound-guard in-process before
any gh call. Exit codes: 0 green, 1 red, 2 usage, 3 refused, 4 timeout, 5 head
moved.

tools/test_pr_land.py runs it against a fake gh on PATH with an injected
sleeper, real gh JSON as fixtures, and a temporary git origin for the verify.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014bHMduhNfrC8w3qjNhEWdB
…s the merge went through

UNSTABLE is GitHub's "mergeable with a non-passing commit status": the required checks pass while
a non-required one (CodeQL) still runs or failed. It was refused by name as an unknown state, so a
merge started in that window exited 3 at once and never waited, and a failing non-required check
gave exit 3 instead of 1 naming the check. It now goes to the rows like BLOCKED: a failed row is
exit 1 naming it, a running row is exit 4, all rows passing is lag (exit 4 after the bounded
re-poll), and green still needs CLEAN.

The verify step after a successful merge: every VERIFY FAILED line starts "gh pr merge succeeded;"
(exit 1 also means a red check with nothing merged); a MERGED state with no merge commit is worded
on its own; and odd JSON (a check-run without a name, a non-object mergeCommit) is noted, never a
traceback. A required_status_checks entry without a context is refused instead of a KeyError. A
head race that never clears while the head stays at S is exit 3, not 5. The module docstring states
the order judge() actually applies.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014bHMduhNfrC8w3qjNhEWdB
…ow it squashes

`restack --worktree W --upper BRANCH --lower PR [--dry-run] [--push]` runs AGENTS.md's four merges
on a stacked branch once the pull request below it has squash-merged: the lower PR's final head
(fetched from refs/pull/PR/head, since the branch is deleted on merge), the squash's first parent,
`-s ours` on the squash, then origin/<base>. It refuses (3) unless the lower PR is MERGED with a
one-parent merge commit on origin/<base> and W is a clean checkout on BRANCH; stops at the first
failing step, names it and its command, and exits 1 with the tree as git left it (run it again after
resolving: the finished steps are no-ops); --dry-run prints the four commands and changes nothing,
not even by a fetch; --push pushes BRANCH only, plain, after all four succeeded. It never calls
`gh pr merge` and grants no merge authority.

tools/test_pr_land.py covers it on a disposable bare origin and clone with the fake gh: the upper
diff against origin/main equals its own change (the later main commit edits a file the lower PR
added, which only merges cleanly because step 3 made the squash the merge base), a conflict at
step 1, 2 and 4 is named and left in progress, a resolved conflict resumes, --dry-run leaves HEAD,
status and every ref alone, the three refusals, and --push moves refs/heads/BRANCH only and a
rejected push is never forced.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014bHMduhNfrC8w3qjNhEWdB
…the credential scan passes

ci/source_bundle.py refuses any file that holds the Plex token key next to a 20+ character
value. The squash-message shape-rule test deliberately writes such a value, so the literal
tripped the repository tree scan (PR #1053). The value is now a module constant formatted
into the body, as .claude/hooks/outbound-guard-test.py does; the runtime string is unchanged,
so the shape rule is still what refuses it.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014bHMduhNfrC8w3qjNhEWdB
…stream on --push, says why a push failed

Review round on the restack subcommand, five items:

* checked_out also refuses (3) when MERGE_HEAD, CHERRY_PICK_HEAD or REVERT_HEAD resolves. A merge a
  human resolved and staged but did not commit has an index equal to HEAD, so `status --porcelain`
  passed it, the fetch ran, step 1 died on "You have not concluded your merge", and stopped() named
  the wrong step.
* --push resolves the branch's upstream. One on origin is pushed as `upper:refs/heads/x` (a checkout
  made by `git checkout -B tmp origin/x` used to create a stray `tmp` on origin and leave the PR
  alone), none or a local one as before, and one on any other remote is refused (3) before the
  first merge. Never forced.
* PUSH FAILED prints the `!`, `error:`, `remote:` and `fatal:` lines instead of `To <url>`, the first
  line of a rejected push, which dropped the reason.
* Step 2's message names the lower PR's base instead of main; the usage docstring lists every exit-3
  refusal of restack.
* --upper equal to the lower PR's base branch is refused (3) before anything is fetched or merged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014bHMduhNfrC8w3qjNhEWdB
@GLinnik21
GLinnik21 merged commit 0bf9eee into main Oct 26, 2026
15 checks passed
@GLinnik21
GLinnik21 deleted the tools/pr-land-restack branch October 26, 2026 10:05
GLinnik21 added a commit that referenced this pull request Oct 26, 2026
GLinnik21 added a commit that referenced this pull request Oct 26, 2026
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.

1 participant