Repository navigation
fix(pr/merge): only clean up local branches in the pull request's own repository - #14544
wippa-studios wants to merge 1 commit into
Conversation
… repository `gh pr merge <PR-URL> --delete-branch` ran its local cleanup in the repository of the current working directory, matching the head branch by name only. Merging a pull request by URL from an unrelated checkout therefore removed that checkout's linked worktree and force-deleted a same-named branch, so unpushed commits were left reachable only from the reflog. `git branch -D` bypasses git's own merged-check, so nothing stopped it. Two independent guards now sit in front of the cleanup, and both fail closed. Scope: the cleanup runs only when a remote of the current directory resolves to the pull request's base repository, compared by owner and name rather than by URL. Only the base repository counts. Accepting the head repository as well would let a fork whose name collides with the base satisfy the check, which is the same name-in-the-wrong-scope mistake in a different place. Safety: a branch is deleted only when its tip is an ancestor of the pull request's head commit, so a local branch holding unpushed work is left alone and the user is told which commits would be lost and how to remove it by hand. The interactive prompt draws on the same decision, so it no longer offers to delete a branch that is not the pull request's. Fixes #14537 Co-Authored-By: Claude Opus 4.5 (Anthropic) <[email protected]>
|
Thanks for your pull request! Unfortunately, it doesn't meet the requirements for review:
Please update your PR to address the above. This PR will be automatically closed in 4 days if these requirements are not met. Full contribution requirements
|
|
Closing this — it doesn't meet the bar, and that's on me.
Worth keeping for whoever triages it: the local cleanup matches the head branch by name in the cwd's repo, and |
Fixes #14537.
The bug
gh pr merge <PR-URL> --delete-branchran its local cleanup in the repository of the current working directory, matching the head branch by name only. Merging a pull request by URL from an unrelated checkout therefore removed that checkout's linked worktree and force-deleted a same-named branch — so any unpushed commits were left reachable only from the reflog.git branch -Dbypasses git's own merged-check, so nothing further stopped it.Three sites, all confirmed at
9b031151a:merge.go:124—CanDeleteLocalBranch = !cmd.Flags().Changed("repo"). A URL selector does not set--repo, so local cleanup stays armed. Correct as written; it is the insufficient guard, not the bug.merge.go:594—localBranchExistsisHasLocalBranch(headRefName), which isgit rev-parse --verify refs/heads/<name>: name-only and scoped to the cwd's repo. Nothing ties it to the pull request's repository.merge.go:496/504—WorktreeRemovethenDeleteLocalBranch(git branch -D).The fix
Two independent guards, both in
(*mergeContext).localBranchAvailable, which replaces the oldmerge.go:594expression. That line was the single source of truth for "is there a local branch we may clean up" — it fed both the interactive prompt and the cleanup gate — so guarding there means the guards cannot be bypassed by any other path intoWorktreeRemove/DeleteLocalBranch, and the prompt stops offering to delete a branch that is not the pull request's.Scope. Cleanup runs only when a remote of the current directory resolves to the pull request's base repository, compared by owner and name rather than by URL string, since one repository reaches git through many URL spellings.
Safety. The branch is deleted only when its tip is an ancestor of
headRefOid(git merge-base --is-ancestor, which includes equality). A local branch holding unpushed work is left alone, and the user is told which commits would be lost and how to remove it by hand.Both fail closed: an error reading remotes, reading the local tip, or comparing the two skips the cleanup with a message rather than falling through to deleting.
Two decisions worth your review
Only the base repository counts. Accepting the head repository as well would fix the reported bug too, and I implemented it that way first. I removed it: a fork whose name collides with the base repository's would then satisfy the check, which is the same name-in-the-wrong-scope mistake in a different place. Base-only is strictly safer and keeps the GraphQL query unchanged. It means a user sitting in the fork of a cross-repository PR no longer gets local cleanup — tell me if that is a workflow you want to keep.
HeadRefOid == ""keeps the pre-guard behaviour.headRefOidis non-null in the GraphQL schema, so an empty value means the field was not populated rather than that there is nothing to compare against. Failing closed there would have disabled--delete-branchfor every existing fixture, sinceHeadRefOidappears nowhere in the currentmerge_test.go. If you would rather it fail closed, the fixtures need populating and I can do that instead — it is the one path where a not-knowing is treated as a knowing.Related:
pkg/cmd/repo/synchas its ownIsAncestorthat collapses "could not determine" into "no". I left it alone to avoid churn insync_test.go, but it is the same shape of ambiguity and probably worth its own change.Tests
New
pkg/cmd/pr/merge/merge_cleanup_test.go, 13 cases, in a new file —merge_test.gois 2,456 lines and the only edit to it is reverting the twoExpectFieldslines that no longer apply. The existing 43 merge tests and thegitpackage are untouched and pass.Against pristine
merge.go,git client.goandmerge_test.go, 8 of the 13 fail, and they fail on the data loss itself — the command recorder catches it directly:Covered: a same-named branch in another repository (with and without a linked worktree, and with a case-different owner); a local tip not on the pull request head (with and without a worktree); remotes that cannot be read; a directory with no matching remote; the head repository not counting; and — the regression guard proving the feature still works — the in-scope cases where the tip is on the head, which pass before and after.
Verification
gofmt -lclean ·go build ./...·go vet ./pkg/cmd/pr/merge/... ./git/·go test ./...0 FAIL packages ·golangci-lint0 issues.Test counts:
pkg/cmd/pr/merge43 → 55 (+12),git53 → 55 (+2) and +5 subtests. No test lost.Not run:
acceptance/testdata/pr/pr-merge-merge-strategy.txtar, which drives the happy path against live GitHub. Reading it, the cwd is a clone of the pull request's repository and the local tip equals the pushed head, so both guards pass and it should be unaffected — but I could not execute it.AI Assistance
Prepared with AI assistance (Claude Opus 4.5, driven through OpenCode). I have read the whole diff, I understand every change in it, and I take responsibility for it. The commit carries a
Co-Authored-By:trailer perAGENTS.md.