Skip to content

Fix worktree-rm: detect squash-merged branches via GitHub PR status#123

Merged
igerber merged 3 commits intomainfrom
fix-worktree-rm-branch-check
Feb 1, 2026
Merged

Fix worktree-rm: detect squash-merged branches via GitHub PR status#123
igerber merged 3 commits intomainfrom
fix-worktree-rm-branch-check

Conversation

@igerber
Copy link
Copy Markdown
Owner

@igerber igerber commented Feb 1, 2026

Summary

  • Replace git branch -d in worktree-rm step 6 with a 3-step approach that queries GitHub first
  • Add git fetch origin (best-effort) to update tracking refs before branch deletion
  • Add gh pr view check to detect squash merges, rebase merges, and regular merges via GitHub API
  • Force-delete (-D) when GitHub confirms PR was merged; safe-delete (-d) otherwise
  • Degrades gracefully when gh is unavailable, offline, or no PR exists for the branch

Methodology references (required if estimator / math changes)

  • N/A — no methodology changes (slash command config only)

Validation

  • Tests added/updated: No test changes (slash command markdown, not executable code)
  • Manual testing: verify with merged-PR worktree, no-PR worktree, and gh unavailable

Security / privacy

  • Confirm no secrets/PII in this PR: Yes

Generated with Claude Code

…deleting

git branch -d fails to detect squash-merged branches because the commit
hashes differ. Replace the single git branch -d with a 3-step approach:
1. Fetch origin to update tracking refs (best-effort)
2. Query GitHub via gh pr view for PR merge status
3. Force-delete (-D) when GitHub confirms merge; safe-delete (-d) otherwise

This handles squash merges, rebase merges, and stale local refs while
degrading gracefully when gh is unavailable or offline.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 1, 2026

Overall assessment: ⚠️ Needs changes

Executive summary:

  • P1: gh pr view "$BRANCH" can interpret numeric branch names as PR numbers, leading to a false MERGED result and forced deletion of an unmerged local branch.
  • No estimator/weighting/variance/assumption changes; Methodology Registry cross‑check not applicable.
  • Doc‑only change; no tests expected.

Methodology

  • No findings (no estimator, weighting, variance/SE, identification assumptions, or default behavior changes).

Code Quality

  • Severity: P1
    Impact: gh pr view "$BRANCH" treats numeric branch names as PR numbers, so a local branch named like 1234 could be force‑deleted if PR #1234 is merged, even when the branch itself is not merged.
    Concrete fix: Replace the lookup with a head-branch query that cannot be confused with PR numbers, e.g. gh pr list --head "$BRANCH" --json state --jq '.[0].state' and treat empty output as unknown. Update the deletion logic accordingly.
    Location: .claude/commands/worktree-rm.md:L92-L114

Performance

  • No findings.

Maintainability

  • No findings.

Tech Debt

  • No findings.

Security

  • No findings.

Documentation/Tests

  • No findings.

…ame confusion

gh pr view "$BRANCH" interprets numeric branch names (e.g., 1234) as PR
numbers, which could cause force-deletion of an unrelated branch's work.
Switch to gh pr list --head which always queries by branch name. Also
handle the new empty-output case when no PR exists for the branch.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@igerber
Copy link
Copy Markdown
Owner Author

igerber commented Feb 1, 2026

/ai-review

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 1, 2026

🔁 AI review rerun (requested by @igerber)

Head SHA: 2628482c53fa9856697307dc31657e02e92e2bd0


Overall assessment: ⚠️ Needs changes

Executive summary

  • No estimator, math, weighting, variance/SE, or identification changes; methodology registry reviewed and not applicable for this doc-only PR.
  • P1: Force-deleting branches solely based on PR merged state can drop local-only commits (e.g., branch reused after merge or unpushed work).
  • Minor robustness: gh pr list --head uses the first PR only, which can misclassify when multiple PRs share the same head name (conservative but inconsistent).
  • No tests updated; acceptable for docs, but the new decision logic is behaviorally significant.

Methodology

  • P3 (Info) — No estimator/methodology changes in this PR; registry review not applicable. Impact: N/A. Fix: None.

Code Quality

  • P1 — Force-delete on PR_MERGED=true can erase local-only commits if the branch has diverged since the merged PR. Impact: potential data loss for work not pushed or not part of the merged PR. Location: .claude/commands/worktree-rm.md:L99-L118. Fix: Before git branch -D, check whether the branch is ahead of its upstream (or has no upstream). If ahead or upstream missing, fall back to git branch -d or prompt. Example logic: git rev-parse --abbrev-ref "$BRANCH@{u}" + git rev-list --count "$BRANCH@{u}..$BRANCH"; only force-delete when count is 0.
  • P3 — Using .[0].state can misclassify when multiple PRs exist for the same head name, yielding PR_MERGED=false/unknown despite a merged PR. Impact: branch may not be deleted when it could be, resulting in extra manual cleanup. Location: .claude/commands/worktree-rm.md:L99-L111. Fix: Prefer “any merged PR” or the most recently updated PR (e.g., include updatedAt in --json and select the latest, or check map(select(.state=="MERGED")) | length).

Performance

  • P3 (Info) — No performance-impacting changes. Impact: N/A. Fix: None.

Maintainability

  • P3 (Info) — No maintainability concerns identified. Impact: N/A. Fix: None.

Tech Debt

  • P3 (Info) — No new tech debt identified. Impact: N/A. Fix: None.

Security

  • P3 (Info) — No security issues identified; no secrets/PII introduced. Impact: N/A. Fix: None.

Documentation/Tests

  • P3 (Info) — No tests updated (doc-only change). Impact: N/A. Fix: Optional—if there is a harness for slash-command behavior, add a small test for merged PR vs. local-ahead branch handling.

Use --state merged for unambiguous multi-PR handling and add
upstream-ahead check to prevent force-deleting local-only commits.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@igerber
Copy link
Copy Markdown
Owner Author

igerber commented Feb 1, 2026

/ai-review

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 1, 2026

🔁 AI review rerun (requested by @igerber)

Head SHA: 2d6c556addac1cde485729edbb4d044064d4acb2


Overall assessment: ✅ Looks good

Executive summary

  • No estimator/math/SE/assumption changes; Methodology Registry not implicated.
  • Doc-only update to the worktree removal command; logic description is clear overall.
  • One minor documentation accuracy nit about what git fetch improves.

Methodology

  • No findings. No estimator or inference changes; Methodology Registry review is not applicable.

Code Quality

  • No findings.

Performance

  • No findings.

Maintainability

  • No findings.

Tech Debt

  • No findings.

Security

  • No findings.

Documentation/Tests

  • [P3] Fetch rationale is slightly inaccurate: git fetch origin does not improve git branch -d merge detection (which checks merge into current HEAD), though it can help upstream/ahead comparisons. Impact: could mislead users about why the fetch is useful. Fix: reword to clarify it updates upstream refs for the ahead/behind check, or note it doesn’t affect git branch -d merge detection. .claude/commands/worktree-rm.md:L86-L93

@igerber igerber merged commit 06c3cd7 into main Feb 1, 2026
@igerber igerber deleted the fix-worktree-rm-branch-check branch February 1, 2026 15:36
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