Skip to content

feat(rm): wt rm --claude and wt merged --rm to clean up worktrees + sessions - #4

Open
stilliard wants to merge 1 commit into
test/split-per-commandfrom
feat/claude-rm
Open

feat(rm): wt rm --claude and wt merged --rm to clean up worktrees + sessions#4
stilliard wants to merge 1 commit into
test/split-per-commandfrom
feat/claude-rm

Conversation

@stilliard

Copy link
Copy Markdown
Owner

Summary

Stacked on #3 (targets that branch; retarget to main once it merges).

  • wt rm <name> --claude — removes the worktree, then deletes its Claude Code sessions via the official claude rm <id>. Sessions are matched using the same job-state-enriched attribution as the --claude tables, and only deleted after the worktree removal actually succeeded (a dirty worktree that git worktree remove refuses leaves the sessions untouched).
  • wt merged --rm [base] — bulk-removes everything wt merged lists, showing the list/table and asking for confirmation first; -y/--yes skips the prompt. Add --claude to delete each worktree's sessions too.
  • The main working tree is never removed, even when it's checked out on a merged branch — skipped with a warning.
  • Removals go through _wt_rm, so pre-rm/post-rm hooks fire per worktree as usual. _wt_rm now also propagates git worktree remove's exit code instead of swallowing it.

Test plan

  • bats -r test — 64/64 passing (8 new tests: confirmation accept/abort, unmerged kept, main-worktree skip, session deletion via stubbed claude rm, no-session case, failed-removal-keeps-sessions)
  • Manually verified the confirmation prompt + table + abort path against a real repo with ~20 worktrees and live Claude Code sessions (answered n; nothing removed)

🤖 Generated with Claude Code

…essions

- `wt rm <name> --claude` removes the worktree and then deletes its
  Claude Code sessions via the official `claude rm <id>` (sessions are
  matched with the same job-state-enriched attribution as the tables,
  and only deleted after the worktree removal actually succeeded).
- `wt merged --rm [base]` bulk-removes everything `wt merged` lists,
  showing the list and asking for confirmation first (-y/--yes to
  skip); with --claude it deletes each worktree's sessions too.
- The main working tree is never removed, even when it happens to be
  checked out on a merged branch - it's skipped with a warning.
- Each removal goes through _wt_rm, so pre-rm/post-rm hooks fire per
  worktree as usual; _wt_rm now also propagates git worktree remove's
  exit code instead of swallowing it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 6, 2026 18:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds safe cleanup workflows for merged Git worktrees, including optional deletion of related Claude Code sessions.

Changes:

  • Add wt merged --rm (with confirmation / -y) to remove merged worktrees while never deleting the main worktree
  • Add wt rm --claude and wt merged --rm --claude to delete Claude Code sessions tied to removed worktrees
  • Expand Bats test coverage and update README/usage text for the new flags

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
wt.sh Implements --rm for merged worktrees and adds session deletion via --claude during removal
test/merged.bats Adds tests for wt merged --rm behavior (confirmation, selective removal, main worktree skip)
test/claude/rm.bats Adds tests for wt rm --claude session deletion behavior and failure handling
test/claude/merged.bats Adds integration test for wt merged --rm --claude -y
README.md Documents new removal flags and Claude session cleanup behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wt.sh
Comment on lines +241 to 244
local rc=0
git worktree remove "$target" || rc=$?
_WT_HOOK_ROOT="$root" _wt_run_hook post-rm "$1" "$target"
_wt_run_adhoc_hook "$post_hook" "$1" "$target"
Comment thread wt.sh
Comment on lines +245 to +248
if [ "$claude" -eq 1 ] && [ "$rc" -eq 0 ]; then
_wt_claude_rm_sessions "$target"
fi
return "$rc"
Comment thread wt.sh
Comment on lines +334 to +340
if [ "$show_claude" -eq 1 ]; then
_wt_rm --claude "$branch"
else
_wt_rm "$branch"
fi
done <<< "$list"
return 0
Comment thread wt.sh
Comment on lines +132 to +134
printf '%s' "$_WT_CLAUDE_JSON" | "$_WT_JQ_BIN" -r --arg wt "$path" \
'.[] | select(.cwd == $wt) | .id' | while IFS= read -r id; do
[ -z "$id" ] && continue
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.

2 participants