1 line
4.1 KiB
JSON
1 line
4.1 KiB
JSON
{"content": "---\nallowed-tools: Bash(git:*), Bash(rm:*), Bash(ls:*), Bash(pwd:*), Bash(grep:*)\nargument-hint: --all | --branch claude/name | --dry-run\ndescription: Clean up merged worktrees and their branches\n---\n\n# Worktree Cleanup\n\nRemove worktrees and branches that have been merged: $ARGUMENTS\n\n## Instructions\n\nYou are in the **main repository** (not a worktree). Clean up finished worktrees.\n\n### Branch Patterns\n\nThis project uses the following branch prefixes for worktrees:\n- `claude/*` — Claude Code auto-created worktrees\n- `claude-daniel/*` — User-created worktrees\n- `review/*` — Component review worktrees\n\nAll three prefixes must be checked in every step below.\n\n### Step 1: Validate Environment\n\n1. Verify this is the main working tree (first entry in `git worktree list`)\n2. If inside a worktree, warn: \"Run `/worktree-cleanup` from the main repo, not from a worktree.\"\n3. Fetch latest from origin: `git fetch origin --prune`\n4. Get the main branch name (main or master)\n\n### Step 2: Parse Arguments\n\nParse `$ARGUMENTS` for options:\n\n- `--all` — clean up ALL merged worktrees and branches\n- `--branch <prefix>/<name>` — clean up a specific worktree/branch\n- `--dry-run` — show what would be cleaned up without doing anything\n- `--force-all` — remove ALL worktrees regardless of merge status (asks confirmation per worktree)\n- No arguments — list worktrees and ask which to clean up\n\n### Step 3: Identify Worktrees\n\n1. List all worktrees: `git worktree list`\n2. List all matching branches:\n ```bash\n git branch --list 'claude/*' 'claude-daniel/*' 'review/*'\n ```\n3. For each matching branch, check if it's been merged into main:\n ```bash\n git branch --merged origin/<main-branch> | grep -E '^\\s+(claude/|claude-daniel/|review/)'\n ```\n4. Also check remote branches:\n ```bash\n git branch -r --merged origin/<main-branch> | grep -E 'origin/(claude/|claude-daniel/|review/)'\n ```\n5. For squash-merged branches (not detected by `--merged`), check if the branch diff is empty against main:\n ```bash\n # A branch is effectively merged if its changes already exist in main\n git diff origin/main...<branch> --stat\n ```\n If the diff is empty or very small (only whitespace), consider it merged.\n\n### Step 4: Display Status\n\nShow a table of all worktrees/branches:\n\n```\n| # | Worktree | Branch | Merged? | Dirty? | Action |\n|---|---------|--------|---------|--------|--------|\n| 1 | eager-mendeleev | claude/eager-mendeleev | Yes | Clean | Will remove |\n| 2 | agent-a7e312d0 | review/code-reviewer-2026-04-01 | No | Clean | Skipped |\n```\n\n### Step 5: Confirm and Execute\n\nIf `--dry-run` was specified, show the table and stop.\n\nOtherwise, use AskUserQuestion to confirm cleanup (unless `--all` was specified with only merged branches).\n\nFor each worktree/branch to clean up:\n\n1. Remove the worktree:\n ```bash\n git worktree remove <path>\n ```\n If that fails (dirty worktree), warn and skip — **never force-remove**.\n\n2. Delete the local branch:\n ```bash\n git branch -d <branch>\n ```\n Use `-d` (not `-D`) for merged branches. For `--force-all` unmerged branches, use `-D` only after explicit user confirmation.\n\n3. Delete the remote branch (if it exists):\n ```bash\n git push origin --delete <branch>\n ```\n If the remote branch doesn't exist, ignore the error silently.\n\n### Step 6: Prune\n\nAfter all removals:\n\n```bash\ngit worktree prune\n```\n\n### Step 7: Summary\n\nShow what was cleaned up:\n\n```\nCleanup Complete\n──────────────────────────────────\nRemoved: <N> worktree(s)\nDeleted: <N> local branch(es)\nDeleted: <N> remote branch(es)\nSkipped: <N> unmerged branch(es)\n──────────────────────────────────\n```\n\nIf any unmerged branches were skipped, list them and suggest:\n- Merge the PR first, then run cleanup again\n- Or use `git worktree remove <path>` and `git branch -D <branch>` manually if the work is truly abandoned\n"} |