Repository navigation
fix(scripts): gc-worktrees.sh uses git branch -D for name+SHA confirmed merges - #2469
Conversation
…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 SummaryThe PR safely prunes squash- or rebase-merged branches after requiring an exact branch-name and tip-SHA match.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains; atomic expected-SHA deletion addresses both previously reported branch-deletion races. Important Files Changed
Sequence DiagramsequenceDiagram
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
Reviews (3): Last reviewed commit: "fix(scripts): use git update-ref -d for ..." | Re-trigger Greptile |
| # 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 |
There was a problem hiding this comment.
…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.
|
@greptileai Addressed the TOCTOU finding: branch deletion now happens inline in a new |
| 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 |
There was a problem hiding this comment.
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.
| 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 |
…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).
|
@greptileai Addressed: replaced the separate |
Summary
--prune-branchesused plaingit branch -dfor every branchclassify()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-dsilently refused to delete it and the branch survived every future--prune-branchesrun indefinitely.-Dunconditionally 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.housekeep/SKILL.mdcomment 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
tests/unit/gc-worktrees-classify-2310.test.ts— extracts the realclassify()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 -nsyntax check: clean.npm run lint: clean.