fix: correct rm --all worktree loop for bash (0-indexed arrays) - #28
Merged
Merged
Conversation
_cmux_rm_all built its dirs/branches arrays with += (0-indexed in bash) but iterated with for (( i=1; i<=${#dirs[@]}; i++ )). In bash this skips the first worktree and reads an empty element at i==count, producing a blank 'Failed:' line and '1 failed'. zsh masked it (1-indexed arrays). Use 0-indexed, strict-less-than loops; identical behavior in zsh and bash.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes array iteration in _cmux_rm_all so all worktrees/branches are correctly listed and removed without skipping the first entry or indexing past the end.
Changes:
- Update two
forloops to iterate over Bash arrays using 0-based indexing (i=0; i<${#dirs[@]}; i++). - Aligns printed and removal logic with correct
dirs[$i]/branches[$i]access.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
siennathesane
approved these changes
Jun 16, 2026
siennathesane
left a comment
Collaborator
There was a problem hiding this comment.
gotta love off-by-one errors! lgtm 🫶🏼
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.
cmux rm --all(_cmux_rm_all) silently leaves the first worktree behind and prints a spuriousFailed:line when run under bash.Cause
The
dirs/branchesarrays are built with+=, so they are 0-indexed in bash. But both loops in_cmux_rm_alliteratefor (( i = 1; i <= ${#dirs[@]}; i++ )):istarts at1, so element0(the first worktree) is never previewed or removed.iends atcount, which is one past the last valid index;${dirs[$count]}is empty, socd ""errors andgit worktree remove ""fails, producing a blankFailed:entry and1 failed.zsh hid this because its arrays are 1-indexed.
Fix
Change both loops to 0-indexed, strict-less-than bounds:
This is correct in bash and remains correct in zsh, where
${arr[0]}is empty and${arr[1..n]}hold the data — iterating0..n-1simply visits the first empty slot harmlessly while still covering every real element. (In practice cmux already runs the surrounding code in bash on Linux/WSL.)Verification
Repro driver: create a temp git repo, add 3 worktrees under the default nested layout, source
cmux.sh, and drive the non-interactive_cmux_rm_allpath.Before (unfixed): preview omits the first worktree, removal prints
cd: : No such file or directory, a blankFailed:, andDone. 2/3 removed (1 failed).— the first worktree survives.After (this change): preview lists all three, output is
All 3 worktrees removed., andgit worktree listshows zero cmux worktrees remaining.Run on
bash 5.2.21.