Skip to content

fix(#750): hard-gate cleanup-worktree remote delete on merge state - #751

Merged
admarble merged 3 commits into
mainfrom
feature/750-fix-cleanup-worktree-deletes-remote-branch-before-
Jun 28, 2026
Merged

admarble merged 3 commits into
mainfrom
feature/750-fix-cleanup-worktree-deletes-remote-branch-before-

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

cleanup-worktree.sh deleted the remote branch unconditionally — the merged-PR check only gated a non-blocking prompt, so running the script (or piping echo y |) against a branch whose PR was still OPEN deleted the PR head branch and made GitHub close the PR unmerged, stranding the work.

This PR hard-gates the destructive step and makes the script automatable:

  • Remote delete is gated on PR_STATUS == MERGED or an explicit --delete-remote/--force flag. Otherwise it's skipped and the remote branch + open PR are left intact.
  • Local teardown (worktree + local branch) stays unconditional, so the branch lock is still freed for a subsequent gh pr merge --delete-branch.
  • Non-interactive support: --yes/-y (and --force) skip the confirmation prompt; without a confirm flag in a non-interactive context the script exits safely instead of stalling on read -p.
  • Mirror reconciled: removed the stale, gitignored, divergent .claude/scripts/cleanup-worktree.sh (carried the same bug; on no code path).

Flag semantics: --yes = non-interactive confirm only (never overrides the merge gate); --delete-remote = override the merge gate (still honors TTY confirm); --force = both.

Pre-PR AC Verification

AC Description Status Evidence
AC-1 OPEN PR + no flag → remote NOT deleted, PR stays open ✅ Stubbed run: REMOTE_DELETE=NO; gate at cleanup-worktree.sh remote-delete block
AC-2 Remote delete only when MERGED or --delete-remote/--force ✅ MERGED→delete, OPEN+--force→delete, OPEN+no flag→skip
AC-3 Non-interactive: --yes/--force runs to completion; without it, safe exit (no read -p stall) ✅ Run with </dev/null: exit 0, safe-exit message, no hang
AC-4 Pre-merge local removal still frees the branch lock ✅ Local worktree/branch teardown is unconditional; only remote delete is gated
AC-5 Fix in templates/scripts/ + .claude/scripts/ reconciled ✅ templates/scripts/ gated (scripts/ symlink inherits); stale gitignored .claude/scripts/ copy deleted
AC-6 Happy path (post-merge from root) unchanged ✅ MERGED short-circuits confirm gate, full local+remote teardown, no new prompt

Test plan

Behavior verified with a stubbed git/gh harness (no real branches touched):

  • OPEN PR, no flag, non-interactive → exit 0, no remote delete, safe-exit message
  • OPEN PR + --force → remote delete fires
  • OPEN PR + --yes (confirm only) → no remote delete, "skipped" message
  • OPEN PR + --delete-remote non-interactive (no --yes) → safe exit, no delete
  • OPEN PR + --yes --delete-remote → remote delete fires
  • MERGED PR, no flag → remote delete fires (happy path)
  • bash -n syntax clean on templates/ + scripts/ symlink; npm run build + npm run lint pass

Closes #750

Remote-branch deletion in cleanup-worktree.sh was unconditional: the
merged-PR check only gated a non-blocking prompt while `git push origin
--delete` always fired. Invoking the script (or piping `echo y |`) on a
branch whose PR was still OPEN deleted the PR head branch, making GitHub
close the PR unmerged and stranding the work.

- Hard-gate remote delete on `PR_STATUS == MERGED` or an explicit
  `--delete-remote`/`--force` flag (AC-1, AC-2); otherwise skip it and
  leave the remote branch + open PR intact.
- Local teardown (worktree + local branch) stays unconditional so the
  branch lock is freed for a later `gh pr merge --delete-branch` (AC-4).
- Add `--yes`/`-y` and `--force` for non-interactive confirm; in a
  non-interactive context without a confirm flag, exit safely instead of
  stalling on `read -p` (AC-3). MERGED happy path is unchanged (AC-6).
- Remove the stale, gitignored, divergent `.claude/scripts/` copy that
  carried the same unconditional-delete bug (AC-5).

Entire-Checkpoint: 35d3bdee7a20
@admarble
admarble merged commit d1cbea4 into main Jun 28, 2026
5 checks passed
@admarble
admarble deleted the feature/750-fix-cleanup-worktree-deletes-remote-branch-before- branch June 28, 2026 19:57
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.

fix(cleanup-worktree): deletes remote branch before PR is merged — closes the open PR unmerged

1 participant