Skip to content

Comments

fix: correct worktree handling bugs in clean_gone command#27605

Open
alexhraber wants to merge 2 commits intoanthropics:mainfrom
alexhraber:main
Open

fix: correct worktree handling bugs in clean_gone command#27605
alexhraber wants to merge 2 commits intoanthropics:mainfrom
alexhraber:main

Conversation

@alexhraber
Copy link

Summary

  • sed prefix stripping: sed 's/^[+* ]//' only removed 1 character of the 2-character git branch -v prefix (+ , * , or ), leaving a stray leading space. Fixed to sed 's/^.\{2\}//' which correctly strips both characters.
  • grep regex injection: grep "\\[$branch\\]" treated the branch name as a regex pattern — branch names containing ., *, [, ], etc. would silently match wrong entries or error out. Fixed to grep -F "[$branch]" for literal fixed-string matching.
  • Deprecated empty-check: Replaced [ ! -z "$worktree" ] with idiomatic [ -n "$worktree" ].

Changes

Line Bug Fix
29 sed 's/^[+* ]//' removed only 1 char of the 2-char git branch -v prefix sed 's/^.\{2\}//' removes exactly 2 characters
32 grep "\\[$branch\\]" treated branch name as regex — names with ., *, [ etc. would break or match incorrectly grep -F "[$branch]" does literal fixed-string matching
33 [ ! -z "$worktree" ] — negated deprecated test [ -n "$worktree" ] — idiomatic non-empty check

Impact

The most critical bug was grep regex injection (line 32). If a branch name contained regex metacharacters, the worktree lookup would silently match the wrong entry or error out, potentially removing the wrong worktree or skipping cleanup entirely. The sed bug (line 29) was masked by awk skipping leading whitespace downstream, but made the pipeline fragile and incorrect.

Test plan

  • Run /clean_gone on a repo with branches marked [gone] that have associated worktrees
  • Verify branches with special characters in names (e.g. fix/v1.0, feature[test]) are handled correctly
  • Confirm worktrees are correctly identified and removed before branch deletion

Fix three bugs in the branch cleanup script that caused incorrect
behavior when processing worktree-associated branches.
fix: correct worktree handling bugs in clean_gone command
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