fix: correct worktree handling bugs in clean_gone command#27605
Open
alexhraber wants to merge 2 commits intoanthropics:mainfrom
Open
fix: correct worktree handling bugs in clean_gone command#27605alexhraber wants to merge 2 commits intoanthropics:mainfrom
alexhraber wants to merge 2 commits intoanthropics:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sed 's/^[+* ]//'only removed 1 character of the 2-charactergit branch -vprefix (+,*, or), leaving a stray leading space. Fixed tosed 's/^.\{2\}//'which correctly strips both characters.grep "\\[$branch\\]"treated the branch name as a regex pattern — branch names containing.,*,[,], etc. would silently match wrong entries or error out. Fixed togrep -F "[$branch]"for literal fixed-string matching.[ ! -z "$worktree" ]with idiomatic[ -n "$worktree" ].Changes
sed 's/^[+* ]//'removed only 1 char of the 2-chargit branch -vprefixsed 's/^.\{2\}//'removes exactly 2 charactersgrep "\\[$branch\\]"treated branch name as regex — names with.,*,[etc. would break or match incorrectlygrep -F "[$branch]"does literal fixed-string matching[ ! -z "$worktree" ]— negated deprecated test[ -n "$worktree" ]— idiomatic non-empty checkImpact
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
awkskipping leading whitespace downstream, but made the pipeline fragile and incorrect.Test plan
/clean_goneon a repo with branches marked[gone]that have associated worktreesfix/v1.0,feature[test]) are handled correctly