Skip to content

Prune stale worktrees and clean up session branches#97

Merged
mattmoran56 merged 3 commits into
mainfrom
session/fix/git-errors
May 8, 2026
Merged

Prune stale worktrees and clean up session branches#97
mattmoran56 merged 3 commits into
mainfrom
session/fix/git-errors

Conversation

@mattmoran56
Copy link
Copy Markdown
Owner

Summary

  • git worktree prune now 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.
  • removeWorktree now also deletes the attached session/* branch, so closing a session no longer leaves orphan branches behind.
  • Path comparison in removeWorktree is 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 with cannot 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.
  • Open a project in CodeCrucible whose .git/worktrees/ has a stale entry; confirm GitHub Desktop can delete the affected branch afterwards.

🤖 Generated with Claude Code

mattmoran56 and others added 3 commits May 7, 2026 19:22
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>
@mattmoran56 mattmoran56 merged commit a81105a into main May 8, 2026
3 checks passed
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.

1 participant