Skip to content

fix: picker alt-r removal for detached worktrees - #1717

Merged
max-sixty merged 2 commits into
mainfrom
switch-remove
Mar 25, 2026
Merged

max-sixty merged 2 commits into
mainfrom
switch-remove

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Resolve the removal target by path (via list_worktrees) instead of always using RemoveTarget::Branch. Detached worktrees have no branch name, so Branch("(detached)") failed silently — the item vanished from the picker but the worktree persisted. Now any worktree item resolves to RemoveTarget::Path, with Branch as fallback for branch-only items.

Adds test_do_removal_removes_detached_worktree unit test. Verified interactively via tmux-cli.

This was written by Claude Code on behalf of maximilian

Resolve the removal target by path (via list_worktrees) instead of
always using Branch. Detached worktrees have no branch name, so
RemoveTarget::Branch("(detached)") would fail silently. Now any
worktree item resolves to RemoveTarget::Path, with Branch as
fallback for branch-only items.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The find closure has a precedence bug: wt.branch.is_none() matches any detached worktree unconditionally, regardless of selected_output. If a detached worktree appears before the target in list_worktrees(), selecting a branched worktree for removal will incorrectly resolve to the detached worktree instead.

For example: user selects "feature-x" for removal, but the main worktree happens to be detached. find matches the main worktree (because branch.is_none()) before reaching the "feature-x" worktree.

Fix: only fall through to branch.is_none() when selected_output is "(detached)".

The test exercises do_removal (the git removal) but not the resolution logic in invoke where this bug lives. Consider adding a test that exercises the worktree lookup path.

Comment thread src/commands/picker/mod.rs Resolved
Match by branch first, then fall back to detached worktrees. The
previous `||` condition could match a detached worktree even when the
user selected a branched item.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The two-pass resolution approach fixes the precedence bug — branch match now takes priority over detached fallback. Previous feedback addressed.

codecov/patch is failing (75.55% vs 95.47% target): the worktree-path resolution block (lines 134–144) is new untested code. It runs inside skim's event callback, so it's not reachable from do_removal unit tests. Options:

  1. Extract the resolution logic into a standalone function (e.g., resolve_removal_target(&repo, &selected_output) -> RemoveTarget) that can be unit-tested independently.
  2. Accept the gap — this is interactive-only code exercisable only through the TUI picker.

I can push a fix commit to extract and test the resolution logic if you'd like.

@max-sixty
max-sixty merged commit 4513a6f into main Mar 25, 2026
21 of 23 checks passed
@max-sixty
max-sixty deleted the switch-remove branch March 25, 2026 07:24
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