Add prune subcommand - #56
Merged
Merged
Conversation
Surfaces `git worktree prune` as a user-facing command. When a worktree directory is deleted by hand (`rm -rf feature-x`) instead of through `git trees rm`, git's administrative entry lingers in `git worktree list` and keeps the branch locked against a fresh checkout. `prune` acts immediately with only a `--dry-run` preview, deliberately exempt from the `--apply` rule: it unlinks metadata only for directories already gone from disk, and leaves the branch untouched, so there is no work to lose. AGENTS.md is amended to scope that rule to operations that can lose work and to name prune as the exception. The report is captured before acting, from stderr (`git worktree prune --verbose` writes there, not stdout), and parsed with a whole-line regex so a name containing a space is not truncated by field splitting. `cmd_clean` is left unmodified — it already prunes internally. Closes #55 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This was referenced Aug 21, 2026
Merged
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.
Implements
git trees prune [--dry-run]. Closes #55.What it does
When a user deletes a worktree directory by hand (
rm -rf feature-x) instead of usinggit trees rm, git's administrative entry under$GIT_COMMON_DIR/worktrees/<name>/lingers. The stale entry keeps showing up ingit worktree listand holds the branch locked against a fresh checkout.pruneclears those entries.Stale worktree names go to stdout, one per line (machine-consumable); git's reason for each goes to stderr. Nothing to prune prints a notice on stderr, empty stdout, exit 0.
The
--applyexemption — read this partAGENTS.md carried this rule:
prunedeliberately does not follow it. It acts immediately, with--dry-runas the only flag. The reasoning:$GIT_COMMON_DIR/worktrees/<name>/— for directories that are already gone from disk.git show-ref --verify --quiet refs/heads/<name>still succeeds after the real run.There is no work to lose. Inverting this to
--applywould make the common case a no-op that prints a nag.Accordingly I amended AGENTS.md: the rule is now scoped to operations that can lose work ("Nothing that can lose work without
--apply."), withprunenamed as the deliberate exception. There is also an in-code comment oncmd_pruneexplaining the exemption, so a reviewer scanning AGENTS.md does not read it as a rule violation.cmd_cleandeliberately unmodifiedcmd_cleanalready callsgit worktree pruneinternally after each removal and again at the end. It needs no prune flag, and this PR does not touch it.Implementation notes
--dry-run.2>&1is required.git worktree prune --verbosewrites its report to stderr, not stdout (verified on git 2.54) —out=$(git worktree prune -n -v)captures nothing.$2. A field split would truncate a worktree name containing a space.--expireis not exposed, keeping the surface minimal.Also updated
Header docblock,
usage()heredoc,main()dispatch arm, and a README section with the acts-immediately callout.Verification
New
# --- prune ---test section, placed before# --- clean ---since clean must stay last (its fixtures mutate the shared$ORIGIN). The prune fixtures work entirely inside their ownnew_container prune-cand do not mutate$ORIGIN. Coverage: clean container (empty stdout, exit 0); a worktree whose directory isrm -rf'd behind git's back;--dry-runnaming it while leaving metadata intact; the real run removing metadata but leaving the branch; idempotence; a live worktree never pruned; unknown option, positional arg, and outside-a-repo all rejected.🤖 Generated with Claude Code
Release coordination (v1.0.3)
One of five PRs into
release-1.0.3(#56 prune, #58 sync, #60 completions, #59 curl install, #57 Homebrew). All five are green on CI (smoke on Linux + macOS).Suggested merge order: #56 → #58 → #60 → #59 → #57. Only one pair conflicts: #58 vs #56.
Merge first. #58 (sync) will then need a rebase: both PRs append to the same two spots in
tests/smoke.sh(thehelp lists …assertion block and the new-section insertion point before# --- clean ---). Two trivial adjacent-append conflicts;git-treesandREADME.mdmerge cleanly.Note: CodeRabbit skipped all five — "reviews are disabled for this base branch." These have not had automated review; that would come when
release-1.0.3merges tomain.The CHANGELOG is deliberately excluded from every PR — it needs the merged PR URLs, so it lands as one commit on
release-1.0.3before taggingv1.0.3.