Repository navigation
fix(#575): cleanup-worktree.sh stays on main worktree before destructive remove - #579
Merged
admarble merged 3 commits intoMay 6, 2026
Conversation
…ive remove Capture MAIN_WORKTREE early via `git worktree list --porcelain` and `cd` to it before `git worktree remove --force`. Without this, callers invoking the script from inside the worktree being deleted hit `Unable to read current working directory` on every subsequent git command, leaving local/remote branches and main update in a half-cleaned state. Fixes #575
Closed
3 tasks
…space paths
Address the gaps surfaced in QA review of the prior commit:
1. Move `cd "$MAIN_WORKTREE"` from immediately-before the main `git worktree
remove` to immediately-after the y/n prompt block — i.e. before the
`.exec-agents/agent-*` cleanup loop. Without this, callers cd'd into a
sub-worktree path (e.g. `<worktree>/.exec-agents/agent-1`) hit the original
bug at the FIRST `git worktree remove` (the agent loop's call), and the
silent `2>/dev/null || true` guards would mask the cascading failures of
the subsequent `git branch --list` and `git branch -D` calls.
2. Switch `MAIN_WORKTREE` extraction from `awk '/^worktree / {print $2; exit}'`
to `sed -n 's/^worktree //p' | head -n 1`, so worktree paths containing
whitespace survive intact.
3. Switch `WORKTREE_PATH` resolution from
`git worktree list | grep "$BRANCH_NAME" | awk '{print $1}'` to an awk
state-machine over `git worktree list --porcelain` that pairs each
`worktree <path>` line with its following `branch refs/heads/<name>`
line. Same whitespace fix, plus matches against the branch ref rather
than free-form-grepping the entire line (which previously false-matched
whenever the branch name happened to also appear in the path string).
CHANGELOG entry added under Unreleased / Fixed.
Verified live with four repros against the patched script:
- AC-1: cwd inside `$WORKTREE_PATH` → exit 0, all 5 steps complete.
- AC-3: cwd at main repo root → exit 0, identical happy-path output.
- New: cwd inside `<worktree>/.exec-agents/agent-1` → exit 0, both the
agent worktree and the parent worktree cleaned, both branches deleted.
- New: worktree path `test 575 spaces` (whitespace) → exit 0, worktree
removed, branch deleted, directory gone.
Refs #575
admarble
deleted the
feature/575-fix-cleanup-worktree-aborts-mid-way-when-invok
branch
May 6, 2026 18:47
This was referenced May 6, 2026
Closed
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
git worktree list --porcelain, thencdto it beforegit worktree remove --force. Fixes fix(cleanup-worktree): aborts mid-way when invoked from inside the worktree being deleted #575.Unable to read current working directoryand the local branch / remote branch / main update steps silently fail.cdsites explain why.Test plan
cd ../worktrees/test-cleanup-575 && cleanup-worktree.sh test-cleanup-575→ exit 0, all 5 steps printed and succeeded.git worktree list | grep -c test-cleanup-575= 0,git branch --list test-cleanup-575empty,git ls-remote origin test-cleanup-575empty.cd <main-repo> && cleanup-worktree.sh test-cleanup-575b→ exit 0, output identical to pre-patch happy path.bash -n templates/scripts/cleanup-worktree.shpasses.Fixes #575