Repository navigation
Conversation
…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
worktrunk-bot
approved these changes
Oct 18, 2026
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]>
1 task
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.
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.
wt switch origin/feature(featurehas a worktree)branch=origin/featureand notarget_worktree_pathbranch=featureand the worktree pathwt step promote <second copy>of a branch checked out twicewt switch @from a second copy or a detached worktreealready_atthe current worktree@/-/pr:Nwt switchSwitchTargetholds the resolved destination (an existing worktree, or a branch to create one for) plus the forge's answer forpr:/mr:. The bare-repo warning, the pre-switch hooks, andplan_switchall read it, so the hooks no longer run their own copy of resolution. Arguments other thanpr:/mr:and--createnames go throughRepository::resolve_worktree, which is what makes@resolve by path; the remote-prefix strip applies to aBranchOnlyresult.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_hooknow reports whether one did), the pipeline locates the argument again through the sameSwitchTarget::locate, on a freshRepository, and plans against that. The forge is not queried twice.--baseand--createvalidation 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.tomlincluded.Promote and the other
--branchcallersRepository::require_worktreereturns the branch along with the path, soCommandEnv::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
lateandorigin/late. The hook-fetch tests also pass onmain; they pin the behaviour that resolving before the hooks could break. Every other new test fails onmain. The full pre-merge gate passes locally. Codex and a separate Claude agent reviewed it, the latter twice.