fix: avoid --delete-branch to prevent worktree branch deletion error

gh pr merge --delete-branch fails when the current worktree is on the
branch being merged, because git refuses to delete a checked-out branch.
Instead, merge without --delete-branch and delete only the remote branch
afterward. Local branch cleanup is left to the worktree manager.
This commit is contained in:
Jinjing
2026-03-27 12:49:56 -07:00
parent 1ea340e561
commit 8c827e4128
+24 -1
View File
@@ -249,11 +249,34 @@ export async function mergePR(
): Promise<{ ok: true } | { ok: false; error: string }> {
await acquire()
try {
await execFileAsync('gh', ['pr', 'merge', String(prNumber), `--${method}`, '--delete-branch'], {
await execFileAsync('gh', ['pr', 'merge', String(prNumber), `--${method}`], {
cwd: repoPath,
encoding: 'utf-8',
env: { ...process.env, GH_PROMPT_DISABLED: '1' }
})
// Delete remote branch separately — local branch deletion fails when
// the user's worktree is checked out on that branch, so we only clean
// up the remote side. Local cleanup is left to the caller / worktree manager.
const headBranch = (
await execFileAsync(
'gh',
['pr', 'view', String(prNumber), '--json', 'headRefName', '-q', '.headRefName'],
{
cwd: repoPath,
encoding: 'utf-8'
}
)
).stdout.trim()
if (headBranch) {
try {
await execFileAsync('git', ['push', 'origin', '--delete', headBranch], {
cwd: repoPath,
encoding: 'utf-8'
})
} catch {
// Remote branch may already be deleted by GitHub — ignore
}
}
return { ok: true }
} catch (err) {
const message =