Skip to content

fix(scripts): gc-worktrees.sh uses git branch -D for name+SHA confirmed merges - #2469

Merged
carlos-alm merged 3 commits into
mainfrom
fix/issue-2310-gc-worktrees-branch-D
Aug 13, 2026
Merged

carlos-alm merged 3 commits into
mainfrom
fix/issue-2310-gc-worktrees-branch-D

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

  • --prune-branches used plain git branch -d for every branch classify() had already deemed "settled" via authoritative GitHub PR state. -d's own internal check is an ancestry test — under a squash/rebase-merge workflow (this repo's own, confirmed live — housekeep/fixer branch cleanup is broken: squash-merge false negative + no post-drain sweep #2309) a genuinely-merged branch's tip is never an ancestor of the squash commit on the default branch, so -d silently refused to delete it and the branch survived every future --prune-branches run indefinitely.
  • Switching to -D unconditionally for every "settled" branch would introduce a worse bug: classify() previously matched a branch to a settled PR by name only. A branch name can be reused after its earlier PR merged or closed — matching by name alone would hand a brand-new, never-merged branch's real commits to -D. classify() now requires an exact name-and-current-tip-SHA match against a MERGED PR before a branch is -D-eligible, mirroring housekeep's Phase 4a fix for the identical gap (Greptile review, PR fix(skills): housekeep branch pruning ignores squash merges; fixer never re-runs it after draining #2311). Every other "settled" case (closed-without-merging, a reused name, or the ancestry-test fallback) still only gets -d, preserving its refusal-if-unmerged safety net.
  • Also corrects a stale housekeep/SKILL.md comment that attributed this name+SHA gap to issue gc-worktrees.sh's --prune-branches uses git branch -d, which refuses genuinely squash-merged branches #2310 while describing it as unresolved — it's resolved here, in the same PR.

Closes #2310

Test plan

  • New unit tests tests/unit/gc-worktrees-classify-2310.test.ts — extracts the real classify() function from the script (not a reimplementation) and exercises it with controlled inputs: open PR, exact name+SHA merge match, name-reuse mismatch, closed-without-merging, no PR (orphan), and both ancestor-test-fallback branches. Confirmed to fail against the pre-fix script (4/8 cases wrongly returned "safe for -D") and pass against the fix.
  • tests/unit/docs-skills-mirror-sync.test.ts (housekeep skill doc) still passes.
  • bash -n syntax check: clean.
  • Full test suite (320 files / 5141 tests): passing.
  • npm run lint: clean.

…ed merges

--prune-branches used plain `git branch -d` for every branch classify() had
already deemed "settled" via authoritative GitHub PR state. -d's own
internal check is an ancestry test -- under a squash/rebase-merge workflow
(this repo's own, confirmed live -- issue #2309) a genuinely-merged
branch's tip is never an ancestor of the squash commit on the default
branch, so -d silently refused to delete it and the branch survived every
future --prune-branches run indefinitely.

Switching to -D unconditionally for every "settled" branch would introduce
a worse bug: classify() previously matched a branch to a settled PR by
name only. A branch name can be reused after its earlier PR merged or
closed -- matching by name alone would hand a brand-new, never-merged
branch's real commits to -D. classify() now requires an exact
name-and-current-tip-SHA match against a MERGED PR before a branch is
-D-eligible, mirroring housekeep's Phase 4a fix for the identical gap
(Greptile review, PR #2311). Every other "settled" case (closed-without-
merging, a reused name, or the ancestry-test fallback) still only gets -d,
preserving its refusal-if-unmerged safety net.

Also corrects a stale housekeep/SKILL.md comment that attributed this
name+SHA gap to issue #2310 while describing it as unresolved -- it's
resolved here, in the same PR.

Closes #2310
@greptile-apps

greptile-apps Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR safely prunes squash- or rebase-merged branches after requiring an exact branch-name and tip-SHA match.

  • Distinguishes GitHub-confirmed merges from cases that still require Git’s ancestry-safe deletion.
  • Uses atomic compare-and-delete semantics to prevent deleting a concurrently advanced or recreated branch.
  • Adds focused regression coverage and updates the housekeep documentation.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; atomic expected-SHA deletion addresses both previously reported branch-deletion races.

Important Files Changed

Filename Overview
.claude/scripts/gc-worktrees.sh Adds exact name-and-SHA merge classification and atomically deletes only a branch ref that still matches the classified SHA.
.claude/skills/housekeep/SKILL.md Updates the documented pruning behavior to reflect the resolved reused-branch-name safeguard.
tests/unit/gc-worktrees-classify-2310.test.ts Covers classification outcomes and verifies that changed refs are rejected by the expected-SHA deletion path.

Sequence Diagram

sequenceDiagram
  participant GC as gc-worktrees.sh
  participant GH as GitHub PR state
  participant Git as Git refs
  GC->>GH: Fetch merged branch name and head SHA
  GC->>GC: Classify worktree using branch and captured HEAD
  GC->>Git: Remove worktree
  GC->>Git: update-ref -d ref expected-SHA
  alt Ref still equals expected SHA
    Git-->>GC: Delete branch atomically
  else Ref changed or was recreated
    Git-->>GC: Refuse deletion
  end
Loading

Reviews (3): Last reviewed commit: "fix(scripts): use git update-ref -d for ..." | Re-trigger Greptile

Comment thread .claude/scripts/gc-worktrees.sh Outdated
# additionally guards against a reused branch name aliasing an old, unrelated merged
# PR (Greptile review, PR #2311) — a name-only match would hand -D a brand-new,
# never-merged branch's real commits just because an old PR once used the same name.
if git -C "$main_root" branch -D "$b" >/dev/null 2>&1; then echo " branch deleted $b"; fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Force deletion loses SHA guard

If another local process advances or recreates a classified branch before the deferred deletion loop runs, git branch -D deletes the new ref by name without verifying that it still points to the SHA confirmed merged, causing unmerged local commits to be lost.

Fix in Claude Code

…s.sh -D

Greptile flagged PR #2469's original fix (deferred batch deletion after the
full worktree walk) as a TOCTOU gap: a concurrent process could recreate or
advance a freed branch name during the walk, and the deferred loop would
then -D that new ref by name alone with no re-verification.

Moves deletion into a new prune_branch() helper, called immediately after
each branch's own worktree removal instead of batched at the end, and adds
an explicit `git rev-parse --verify` re-check against the SHA classify()
actually confirmed before the -D call specifically — refusing the delete if
the ref moved in the interim. prune_branch() is extracted and unit-tested
the same way classify() already is.

Addresses Greptile review, PR #2469.
@carlos-alm

Copy link
Copy Markdown
Contributor Author

@greptileai Addressed the TOCTOU finding: branch deletion now happens inline in a new prune_branch() helper called immediately after each branch's own worktree removal (not deferred to a batch loop after the full walk), and the -D path re-verifies the branch's current tip SHA against the one classify() confirmed immediately before deleting — refusing if it changed. prune_branch() is extracted and unit-tested the same way classify() already is. Please re-review.

Comment thread .claude/scripts/gc-worktrees.sh Outdated
Comment on lines +193 to +197
if [ "$(git -C "$main_root" rev-parse --quiet --verify "refs/heads/$branch" 2>/dev/null)" = "$head" ]; then
if git -C "$main_root" branch -D "$branch" >/dev/null 2>&1; then echo " branch deleted $branch"; fi
else
echo " skip (branch ref changed since classification) $branch"
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 SHA check remains non-atomic

If another local process advances or recreates the branch after rev-parse returns but before git branch -D executes, the deletion applies to the new ref by name, causing unmerged local commits to be deleted.

Suggested change
if [ "$(git -C "$main_root" rev-parse --quiet --verify "refs/heads/$branch" 2>/dev/null)" = "$head" ]; then
if git -C "$main_root" branch -D "$branch" >/dev/null 2>&1; then echo " branch deleted $branch"; fi
else
echo " skip (branch ref changed since classification) $branch"
fi
if git -C "$main_root" update-ref -d "refs/heads/$branch" "$head" 2>/dev/null; then
echo " branch deleted $branch"
else
echo " skip (branch ref changed since classification) $branch"
fi

Fix in Claude Code

…nch delete

Greptile's round-2 review on PR #2469 found that the SHA re-check added in
the previous commit still ran as a separate `git rev-parse` followed by a
separate `git branch -D` — two distinct git invocations, leaving a narrow
window for a concurrent process to mutate the ref in between.

Replaces both with a single `git update-ref -d refs/heads/<branch> <sha>`
call, which git executes as one atomic compare-and-delete: the ref is
deleted only if it still points at the given SHA, in the same operation
that checks it. Updates prune_branch()'s unit tests to match.

Addresses Greptile review, PR #2469 (round 2).
@carlos-alm

Copy link
Copy Markdown
Contributor Author

@greptileai Addressed: replaced the separate rev-parse check + git branch -D with a single atomic git update-ref -d refs/heads/<branch> <sha> call, so there's no longer a window between confirming the SHA and deleting the ref — git performs the compare-and-delete as one operation. Tests updated to match. Please re-review.

@carlos-alm
carlos-alm merged commit fd08dae into main Aug 13, 2026
23 checks passed
@carlos-alm
carlos-alm deleted the fix/issue-2310-gc-worktrees-branch-D branch August 13, 2026 00:08
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gc-worktrees.sh's --prune-branches uses git branch -d, which refuses genuinely squash-merged branches

1 participant