Prune stale worktrees and clean up session branches#97
Merged
Conversation
Normalize worktree paths via fs.realpath before comparing in removeWorktree, so the session-branch cleanup isn't bypassed on macOS (/var vs /private/var) or due to trailing-slash differences. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- Narrow removeWorktree's swallowed error to the already-gone case by re-checking the worktree list after prune; rethrow the original error if the worktree is still registered. - Gate session branch -D on actual removal success so unmerged work isn't deleted when removal failed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Stale `.git/worktrees/` admin entries (left behind when a worktree directory is deleted or its parent repo is moved outside of git) keep their branch "in use", so external tools like GitHub Desktop fail with `cannot delete branch '...' used by worktree at '...'`. We now prune before list/create/remove and delete the orphan `session/*` branch when a session's worktree is removed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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 prunenow runs before list/create/remove, so stale.git/worktrees/admin entries (left behind when a worktree dir is deleted or the parent repo is moved outside of git) get cleared.removeWorktreenow also deletes the attachedsession/*branch, so closing a session no longer leaves orphan branches behind.removeWorktreeis now robust to symlinked ancestors (e.g. macOS/var→/private/var) and to a missing target directory.Why
External tools (GitHub Desktop, plain
git branch -d) fail withcannot delete branch '...' used by worktree at '...'whenever a stale entry references a deleted/moved worktree path. The user was hitting this every time they opened GitHub Desktop on a repo previously managed by CodeCrucible.Test plan
npx vitest run tests/unit/main/worktree.service.test.ts— 5 new tests cover create, remove (incl. orphan-branch cleanup), prune-on-list, remove-when-dir-already-gone, and the "leave non-session/*branches alone" guarantee..git/worktrees/has a stale entry; confirm GitHub Desktop can delete the affected branch afterwards.🤖 Generated with Claude Code