Skip to content

fix: resolve worktree arguments once and pass the resolved worktree through - #4496

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

max-sixty merged 3 commits into
mainfrom
4493

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Follow-up to the survey on #4493. 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. This resolves the argument once and passes the resolved worktree through.

Case Before After
wt switch origin/feature (feature has a worktree) pre-switch hook got branch=origin/feature and no target_worktree_path hook gets branch=feature and the worktree path
wt step promote <second copy> of a branch checked out twice acted on the first copy, failed partway, left the main worktree and that copy detached refuses before touching anything, listing both checkouts
the same with no argument from the second copy, or with the default branch checked out twice same partial failure same refusal
wt switch @ from a second copy or a detached worktree landed in the first copy, or errored already_at the current worktree
bare-repo worktree-path warning for @ / - / pr:N skipped through a hand-kept list of shortcuts names the branch the shortcut resolves to; the list is gone

wt switch

SwitchTarget holds the resolved destination (an existing worktree, or a branch to create one for) plus the forge's answer for pr:/mr:. The bare-repo warning, the pre-switch hooks, and plan_switch all read it, so the hooks no longer run their own copy of resolution. Arguments other than pr:/mr: and --create names go through Repository::resolve_worktree, which is what makes @ resolve by path; the remote-prefix strip applies to a BranchOnly result.

A pre-switch hook can change what the argument names: the fetch-if-stale hook the docs suggest makes a remote-only branch appear. So when a hook actually ran (execute_hook now reports whether one did), the pipeline locates the argument again through the same SwitchTarget::locate, on a fresh Repository, and plans against that. The forge is not queried twice. --base and --create validation still run after the hooks.

Two behaviour changes follow. Naming errors (wt switch - with no history, a path holding no worktree) now fail before the pre-switch hooks run rather than after. And when a hook ran, the rest of the switch reads repository state as the hook left it, .config/wt.toml included.

Promote and the other --branch callers

Repository::require_worktree returns the branch along with the path, so CommandEnv::for_selector, copy-ignored, and promote use the resolved pair instead of looking one up from the other. Promote resolves every route (argument, @, ^) to a worktree and refuses when either branch is checked out more than once.

Not covered

wt switch - still records the previous location as a branch name, so it can't return to the second copy of a duplicated branch or to a detached worktree. Changing what the history records is a separate change.

Testing

New integration tests cover each row above, plus a pre-switch hook that fetches, with both late and origin/late. The hook-fetch tests also pass on main; they pin the behaviour that resolving before the hooks could break. Every other new test fails on main. The full pre-merge gate passes locally. Codex and a separate Claude agent reviewed it, the latter twice.

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

max-sixty and others added 3 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
@max-sixty
max-sixty merged commit 0c75655 into main Oct 18, 2026
38 checks passed
@max-sixty
max-sixty deleted the 4493 branch October 18, 2026 04:27
max-sixty added a commit that referenced this pull request Oct 20, 2026
…orktree names HEAD's branch (#4521)

Since v0.80.0, `wt switch @` run outside any worktree treats the
directory it runs in as a detached worktree. This happens from a bare
repository's directory (or the `proj/` holding `proj/.git` → `.bare`)
and from a normal repository's `.git` directory. It prints `Already on
worktree for detached worktree @ <dir>`, runs pre-switch hooks with no
branch, and cds into the directory. v0.80.0 switched to the worktree of
the branch HEAD names.

#4496 routed `wt switch` arguments through
`Repository::resolve_worktree`. Its `@` arm expected
`current_worktree().root()` to fail outside a worktree, but
`WorkingTree::root()` falls back to the cwd. `@` now resolves to the
registered worktree at the cwd's root and, where none is registered,
falls through to the branch HEAD names, as `expand_selector` already
reads `@`. The unreachable `GitError::NotInWorktree` is removed.

## One definition of the current worktree

The no-argument forms of other commands read `current_worktree().root()`
and `.branch()` directly, so "the current worktree" had a second
definition, and outside every worktree the two disagreed: `wt remove`
and `wt remove @` took different code paths and gave different answers.
Every no-argument default is now `selector.unwrap_or("@")`, so each
command has one path with or without an argument:

- `wt remove` with no argument goes through `validate_remove_targets`
with `["@"]`. Its separate single-target path is gone, and so is the `@`
guard an earlier commit on this branch added beside it.
- `CommandEnv::for_action` and `for_action_loading_config` are deleted.
`wt merge`, `wt step commit`, `wt step squash` (including
`--show-prompt` / `--dry-run`), `wt hook` and `wt hook show` use
`CommandEnv::for_selector("@")`.
- `wt step diff` resolves `@` the way it resolves `--branch`.
- `wt step copy-ignored` defaults `--to` to `@`. Run from a bare
repository's directory, it used to copy ignored files into the bare
repository itself (in v0.80.0 too).
- `wt config state` and `wt step push --no-ff` use
`require_selected_branch("@")`; `Repository::require_current_branch` is
deleted.

Inside a worktree these commands behave as before.
`Repository::rooted_at` keeps the invoking repository when the selected
worktree is the one it was discovered in, and `@` matches the worktree
by path, so detached worktrees and duplicate checkouts of one branch
resolve to themselves. `require_selected_branch` no longer names the
detached worktree the user is standing in: `wt config state vars set`
there says `Cannot set variable: not on a branch (detached HEAD)` with a
plain `git switch <branch>` hint.

## Behavior change outside a worktree

From a bare repository's directory or a `.git` directory, `wt remove`,
`wt merge`, `wt step commit`, `wt step squash`, `wt step diff` and `wt
hook` now act on the worktree of the branch HEAD names, where they used
to treat the directory itself as a worktree. `wt config state … --branch
@` and `--to @` act on HEAD's branch there too. For `wt remove`, the
default-branch refusal is what guards this case: with HEAD on the
default branch, `wt remove` and `wt remove @` both refuse with `Cannot
remove the default branch main`. It only protects HEAD's branch when the
cached default branch matches HEAD. Where HEAD's branch has no worktree,
the commands that need a worktree fail with `Branch <name> has no
worktree`, and `wt remove` deletes the branch alone if it is integrated.

The fall-through applies only outside every work tree. Inside a work
tree git doesn't have registered at that path, such as a worktree moved
without `git worktree repair`, `@` still names the directory, so `wt
remove` there refuses with `No worktree @ <dir>` rather than pruning the
branch's stale registration.

## Verification

The release review's scripts, rerun against main, v0.80.0 and this
branch, show `wt switch @` from `proj/`, `proj/.bare` and `repo/.git`
again landing in the HEAD branch's worktree, with pre-switch hooks
seeing that branch and path. New or changed integration tests, each of
which fails without its fix:

- `test_switch_at_outside_a_worktree_names_the_head_branch`: `wt switch
@` from a `.git` directory.
- `test_bare_repo_at_from_bare_directory_names_head_branch`: `wt switch
@` and `wt step copy-ignored` from a bare repository's directory.
- `test_bare_repo_remove_from_bare_directory_targets_head_branch`: `wt
remove` and `wt remove @` give identical output there and refuse as
default branch.
- `test_bare_repo_previews_from_bare_directory_use_head_branch`: `wt
hook show` and `wt step squash --show-prompt` read HEAD's branch's
worktree, not the primary one.
- `test_remove_no_argument_in_moved_worktree_refuses`: `wt remove` from
a moved worktree refuses and leaves its checkout intact.
- `test_vars_set_in_detached_worktree`: the detached-HEAD message for
the no-argument form.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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