Skip to content

fix(switch): return wt switch - to the worktree it left, not the first checkout of its branch - #4497

Merged
max-sixty merged 6 commits into
mainfrom
4493
Oct 18, 2026
Merged

max-sixty merged 6 commits into
mainfrom
4493

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #4496. wt switch - recorded only the branch it left (worktrunk.history) and found the worktree again by branch name. From the second checkout of a branch checked out twice it returned to the first checkout; from a detached worktree it had no branch to record, so it returned to an older branch or failed with "No previous branch".

A switch now also records the path of the worktree it leaves, in worktrunk.history-path, and clears worktrunk.history when that worktree is detached, so the two keys always describe one worktree. As a worktree argument, - resolves to the recorded path while a worktree is still registered there and still holds the recorded branch; otherwise it falls back to the branch, as before. Every worktree argument resolves - this way, so wt remove - now removes the worktree wt switch - would return to (its dirty and integration checks unchanged). As a ref (--base -, merge targets) - still reads the branch. wt list marks the previous worktree by the same rule, and wt config state set/clear keep the two keys in step.

Behaviour that changes: after leaving a detached worktree, wt config state shows no previous branch, --base - errors instead of using an older branch, and an older wt reading the key sees no history. Known limit: if an older wt later records the same branch without a path, the stale path still matches, which matters only for a branch checked out twice.

New integration tests return to a second checkout and to a detached worktree (both fail on main), and pin the fallbacks to the branch: when an older wt recorded another branch, and when the worktree left was removed. The full pre-merge gate passes locally; Codex reviewed it.

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

max-sixty and others added 5 commits October 17, 2026 15:38
…hrough

Several commands resolved a worktree argument, kept only the branch or only
the path, and looked the other half up again. Each re-lookup could disagree
with the first:

- `wt switch origin/feature` gave pre-switch hooks `branch=origin/feature`
  and no `target_worktree_path`, because the hooks ran their own copy of
  target resolution without the remote-prefix strip.
- `wt step promote` re-found the target worktree by branch name. For a branch
  checked out twice it picked the first copy, and the exchange failed partway
  through, leaving both worktrees detached. The same happened from the second
  copy with no argument, and when the default branch had a second checkout.
- `wt switch @` resolved `@` to a branch name and back, so it landed in the
  wrong copy of a duplicated branch and failed in a detached worktree.
- The bare-repo worktree-path warning showed the raw argument, and skipped a
  hand-kept list of shortcuts to avoid showing them.

`wt switch` now resolves its argument once, into a `SwitchTarget` that the
bare-repo warning, the pre-switch hooks, and the plan all read. Non-`pr:`
arguments go through `Repository::resolve_worktree`, so `@` resolves by path.
When a pre-switch hook ran, the pipeline locates the argument again on a
fresh `Repository`, because a hook that fetches can change what the argument
names. `--base` and `--create` validation stay after the hooks.

`require_worktree` returns the worktree's branch with its path. Promote
resolves every route to a worktree, and refuses up front when either branch
is checked out in more than one worktree.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	tests/integration_tests/bare_repository.rs
…rst checkout of its branch

`wt switch -` recorded only the branch it left (`worktrunk.history`) and found
the worktree again by branch name. From the second checkout of a branch
checked out twice it returned to the first; from a detached worktree it had no
branch to record and returned to an older one, or nowhere.

A switch now also records the worktree's path in `worktrunk.history-path`, and
clears `worktrunk.history` when the worktree it leaves is detached. `-` as a
worktree argument resolves to that path while the worktree there is still
registered and still holds the recorded branch; otherwise it falls back to the
branch, as before. `-` as a ref (`--base -`, merge targets) still reads the
branch. `wt list` marks the previous worktree by the same rule, and setting or
clearing the previous branch keeps the two keys consistent.

Co-Authored-By: Claude Opus 5.5 (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.

wt remove - resolves through resolve_worktree as well, so this PR also changes which worktree it deletes. For a branch checked out twice it now removes the checkout the user just left, where it used to remove the branch's first checkout. After leaving a detached worktree it now removes that detached worktree, where it used to remove an older branch's worktree or fail with "No previous branch". In both cases the new target is the worktree wt switch - would return to, and the existing dirty and integration checks still apply, so it looks like a correction rather than a hazard. Changes to what a removal can reach are held for a maintainer under Data-Loss Surface: Hold for Human Review in .claude/skills/running-tend/references/review-pr.md, so I'm leaving the approval to you rather than approving.

@max-sixty
max-sixty merged commit 55d302d into main Oct 18, 2026
37 checks passed
@max-sixty
max-sixty deleted the 4493 branch October 18, 2026 05:33
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