Skip to content

fix: refuse removing a worktree that contains another, and finish picker removals before exit - #4861

Merged
max-sixty merged 5 commits into
mainfrom
review-waiting
Nov 21, 2026
Merged

max-sixty merged 5 commits into
mainfrom
review-waiting

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Three removal-safety fixes. Together they replace #4458 and #4463, fixing the same failures in far less code.

wt remove and wt step prune deleted a checkout nested inside the target. wt remove, wt remove --force and wt step prune moved the target into trash along with any checkout nested inside it, deleting that checkout's uncommitted files at exit 0. An ignored nested checkout is invisible to the dirty gate, and --force waives only the target's own changes. Only wt merge refused this (#4360). Planning now refuses such a target on every removal path:

  • wt remove fails with a hint to remove the nested worktree first.
  • wt step prune prints Skipped <branch> (contains worktree @ <path>) and removes the rest.

The guard is Repository::worktree_nested_in. It canonicalizes each registered checkout once per inventory read and answers each target with one range lookup, so prune adds no per-target path resolution (#3954). A checkout nested during wt remove's approval prompt or pre-remove hook still isn't caught; only merge rechecks after them.

Leaving the picker right after alt-x abandoned the removal. The process exited with the removal thread still running: the row was gone but the worktree stayed, and nothing said so. A removal further along left its checkout in trash. wt switch now waits for requested removals once skim releases the terminal. It prints Finishing worktree removal... while one is still running, then reports any failure.

A failed promote gave rm -rf as the first step for the only copy of the ignored files. When a post-checkout hook fails git switch after promote has staged ignored files in .git/wt/staging/promote, the error now names that directory. The next run says what a/ and b/ hold, and gives rm -rf only as the step after recovering them.

Supersedes #4458 and #4463.

This was written by Claude Code on behalf of max-sixty

`wt remove`, `wt remove --force` and `wt step prune` moved the target into
trash with any checkout nested inside it, deleting that checkout's uncommitted
files at exit 0. An ignored nested checkout is invisible to the dirty gate, and
`--force` waives only the target's own changes. Only `wt merge` refused this.

Planning now refuses such a target on every removal path. `wt remove` fails
with a hint to remove the nested worktree first. `wt step prune` prints
`Skipped <branch> (contains worktree @ <path>)` in the preview and the run, and
removes the rest. Merge's recheck after hooks reports the same error.

The guard is `Repository::worktree_nested_in`. It resolves each registered
checkout once per inventory read, keeps the canonical paths sorted beside the
inventory in the repository cache, and answers each target with one range
lookup, so a prune of N worktrees resolves N paths rather than N per target
and gains no registry wait (#3954).

A checkout nested during `wt remove`'s approval prompt or `pre-remove` hook
still isn't caught; only merge rechecks after them.
Leaving the picker right after alt-x ended the process with the removal thread
still running. The row had gone, but the worktree stayed and nothing said so;
a removal further along left its checkout in trash.

`wt switch` now waits for requested removals once skim releases the terminal,
printing `Finishing worktree removal...` if one is still running, and then
reports any failure. A slow `pre-remove` hook delays exit by its remaining time.
A failing `post-checkout` hook fails `git switch` after promote has moved ignored
files into `.git/wt/staging/promote`. The error named only the failed step, and
the next run's first instruction for the only copy was `rm -rf`.

The failure now names the staging directory. The next run says what `a/` and
`b/` hold, and gives `rm -rf` as the step after recovering them.
@max-sixty
max-sixty merged commit 76f6952 into main Nov 21, 2026
38 checks passed
@max-sixty
max-sixty deleted the review-waiting branch November 21, 2026 00:55
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