Skip to content

fix(#575): cleanup-worktree.sh stays on main worktree before destructive remove - #579

Merged
admarble merged 3 commits into
mainfrom
feature/575-fix-cleanup-worktree-aborts-mid-way-when-invok
May 6, 2026
Merged

admarble merged 3 commits into
mainfrom
feature/575-fix-cleanup-worktree-aborts-mid-way-when-invok

Conversation

@admarble

@admarble admarble commented May 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Capture main worktree path early via git worktree list --porcelain, then cd to it before git worktree remove --force. Fixes fix(cleanup-worktree): aborts mid-way when invoked from inside the worktree being deleted #575.
  • Without this, callers invoking the script from inside the worktree being deleted hit Unable to read current working directory and the local branch / remote branch / main update steps silently fail.
  • Single file, +9/-0 lines. Comments at the capture and cd sites explain why.

Test plan

  • AC-1: ran cd ../worktrees/test-cleanup-575 && cleanup-worktree.sh test-cleanup-575 → exit 0, all 5 steps printed and succeeded.
  • AC-2: post-run, git worktree list | grep -c test-cleanup-575 = 0, git branch --list test-cleanup-575 empty, git ls-remote origin test-cleanup-575 empty.
  • AC-3: ran cd <main-repo> && cleanup-worktree.sh test-cleanup-575b → exit 0, output identical to pre-patch happy path.
  • bash -n templates/scripts/cleanup-worktree.sh passes.

Fixes #575

…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
admarble added 2 commits May 6, 2026 13:34
…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
Resolves CHANGELOG.md conflict by keeping both Fixed entries (#575 and
#570) under the existing "newest-on-top" convention. All other files
auto-merged cleanly: pre-tool.sh hook copies and qa SKILL.md mirrors
from #570/#573 land unchanged.
@admarble
admarble merged commit 60dfd6c into main May 6, 2026
5 checks passed
@admarble
admarble deleted the feature/575-fix-cleanup-worktree-aborts-mid-way-when-invok branch May 6, 2026 18:47
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.

fix(cleanup-worktree): aborts mid-way when invoked from inside the worktree being deleted

1 participant