Repository navigation
fix: resolve every no-argument default through @, which outside a worktree names HEAD's branch - #4521
Conversation
Since #4496 (0c75655), `wt switch` resolves its argument through `Repository::resolve_worktree`. Its `@` arm expected `current_worktree().root()` to fail outside a worktree, but `WorkingTree::root()` falls back to the cwd itself. From a bare repository's directory, or from a normal repository's `.git` directory, `@` therefore resolved to that directory as a detached worktree: `wt switch @` reported "Already on worktree for detached worktree", ran pre-switch hooks with no branch, and cd'd into the directory. v0.80.0 switched to the worktree of the branch HEAD names. `@` now resolves to the registered worktree at the cwd's root, and, where none is registered, falls through to the branch HEAD names, which is how `expand_selector` already reads `@`. The `GitError::NotInWorktree` error the old arm mapped to was unreachable and is removed. `wt step copy-ignored` without `--to` used `root()` the same way, so from a bare repository's directory it copied ignored files into the bare repository itself. Its destination now defaults to `@`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
Run outside every worktree, wt remove @ and wt remove with no argument now give different answers. The no-argument form still fails with "Worktree not found at
@ falls through to the branch HEAD names and removes that branch's worktree and branch. The PR body names this change. I'm holding the approval because it widens a deletion: it adds a target that wt remove used to refuse, and that target isn't the worktree the user is in.
Reproduced on this branch with a proj/.git → .bare layout, worktrees main and feature, bare HEAD set to refs/heads/feature, and wt config state default-branch set main. From proj/:
$ wt remove @ --yes --foreground
◎ Removing feature worktree...
✓ Removed feature worktree & branch (no added changes on main, ⊂)
$ wt remove --yes
✗ Failed to remove worktree
Worktree not found at …/proj
If default-branch detection runs uncached, it infers feature from HEAD and refuses ("Cannot remove the default branch feature"). So the default-branch refusal only protects HEAD's branch when the configured default branch and HEAD agree.
The fall-through makes sense for switch and copy-ignored, since both just need a destination. For remove, "Prefer a clear failure" (AGENTS.md → Data Safety) argues for keeping @ an error when the cwd isn't a worktree, as the no-argument form already does. One way to do that is to keep resolve_worktree's @ arm path-only for removal, either through a flag or a remove-side check that @ matched a registered worktree, and let the other callers fall through. Whether wt remove @ from a bare directory should remove anything is a product call for you.
Outside a worktree `@` now resolves to the branch HEAD names, which suits switch and copy-ignored but would let `wt remove @` from a bare repo's directory remove that branch's worktree. Removal acts only on the worktree the user is in, so refuse as the no-argument form does. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
I don't understand these line breaks in the quotes? then: explain why |
|
Line breaks: the body has
The guard works, but it rebuilds the no-argument behavior next to it, with a different message ( |
|
we want to map @ to HEAD. but why not map to arg to HEAD too? think of ways of addressing this at a deeper, broader level, so we reduce the number of code paths, canonicalize, etc |
The maintainer has since asked whether no-argument removal should map to HEAD too, and for a deeper rework of the code paths. The approved head refuses that case, so the design is open again.
|
Agreed, no argument should mean A broader shape, in three parts:
The behavior change, so it's a deliberate choice: from a bare repo's directory or a Parts 1 and 2 would fit in this PR as one more commit, and part 3 is a natural follow-up. I can push either if you'd like. I haven't pushed anything because it's your branch. |
"The current worktree" had two definitions. `resolve_worktree("@")` reads
the registered worktree at the cwd, else the branch HEAD names, while the
no-argument forms read `current_worktree().root()` and `.branch()`
directly. Outside every worktree, `root()` falls back to the cwd, so the
two disagreed: `wt remove @` and `wt remove` took different paths and gave
different answers from a bare repository's directory.
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
`["@"]`. The single-target path and the `@` guard beside it are gone.
- `CommandEnv::for_action` and `for_action_loading_config` are deleted;
`wt merge`, `wt step commit`, `wt step squash` (including its preview),
`wt hook` and `wt hook show` use `for_selector("@")`.
- `wt step diff` resolves `@` like `--branch`.
- `wt config state` and `wt step push --no-ff` use
`require_selected_branch("@")`; `require_current_branch` is deleted.
`Repository::rooted_at` keeps the invoking repository when the selected
worktree is the one it was discovered in, so inside a worktree these
commands run exactly as before. `require_selected_branch` no longer names
a detached worktree the user is standing in.
From a bare repository's directory or a `.git` directory, these commands
now act on the worktree of the branch HEAD names, where they used to treat
the directory itself as a worktree. For `wt remove`, the default-branch
refusal guards that case.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ outside a worktree to the branch HEAD names@, which outside a worktree names HEAD's branch
worktrunk-bot
left a comment
There was a problem hiding this comment.
From a linked worktree that was moved without git worktree repair, wt remove with no argument now prunes that worktree's own registration. It then tries to delete the branch. The directory the user is standing in is left with a dangling .git file, so git status there fails with fatal: not a git repository. Uncommitted files stay on disk, but they're no longer in a checkout. Main refuses in the same spot with Worktree not found at <dir>.
The fall-through in resolve_worktree's @ arm (inline) causes this. It fires whenever the cwd's root isn't a registered worktree, not only when the cwd is outside a work tree. In a moved worktree, git rev-parse --show-toplevel succeeds, so the cwd is inside a work tree. @ falls through to HEAD's branch, and the inventory still has that branch at its old, missing path. wt remove then takes the stale-worktree path that wt remove <branch> uses: prune, then delete the branch if integrated. That's fine when the user names the branch, but here they meant the directory they're in.
Reproduced on this head against a main build:
$ git worktree add ../feat -b feat && mv ../feat ../moved && cd ../moved && echo dirty > u.txt
$ wt remove --yes --foreground # main
✗ Failed to remove worktree
Worktree not found at …/moved
$ wt remove --yes --foreground # this PR
✗ Failed to delete branch feat
fatal: not a git repository: (null)
$ git status
fatal: not a git repository: (null)
This widens what wt remove can delete, so I'm holding approval. Everything else in the refactor matches the PR description: the remove, hook, config-state and bare-repository integration tests pass locally (463 tests).
The no-argument `wt remove` now shares the validated path, whose WORKTRUNK_FIRST_OUTPUT early exit the time-to-first-output benchmark relies on but no test reached. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`@` fell through to HEAD's branch whenever the cwd's root wasn't a registered worktree, including a worktree moved without `git worktree repair`. There `wt remove` pruned the branch's stale registration and left the cwd's checkout with a dangling .git file. Fall through only outside every work tree; inside an unregistered one, `@` names this directory, which holds no worktree. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
The moved-worktree case is fixed. On this head, wt remove, wt remove @, wt merge and wt hook show all refuse with No worktree @ <dir> from an unrepaired moved worktree, and the checkout and its untracked file are left intact.
I'm still not approving, because wt remove from a bare repository's directory can now remove HEAD's branch's worktree and branch. That widens what wt remove can delete, and the repo's review guidance (.claude/skills/running-tend/references/review-pr.md → Data-Loss Surface: Hold for Human Review) leaves those merges to you. I found nothing else on this head.
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 theproj/holdingproj/.git→.bare) and from a normal repository's.gitdirectory. It printsAlready 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 switcharguments throughRepository::resolve_worktree. Its@arm expectedcurrent_worktree().root()to fail outside a worktree, butWorkingTree::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, asexpand_selectoralready reads@. The unreachableGitError::NotInWorktreeis 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 removeandwt remove @took different code paths and gave different answers. Every no-argument default is nowselector.unwrap_or("@"), so each command has one path with or without an argument:wt removewith no argument goes throughvalidate_remove_targetswith["@"]. Its separate single-target path is gone, and so is the@guard an earlier commit on this branch added beside it.CommandEnv::for_actionandfor_action_loading_configare deleted.wt merge,wt step commit,wt step squash(including--show-prompt/--dry-run),wt hookandwt hook showuseCommandEnv::for_selector("@").wt step diffresolves@the way it resolves--branch.wt step copy-ignoreddefaults--toto@. 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 stateandwt step push --no-ffuserequire_selected_branch("@");Repository::require_current_branchis deleted.Inside a worktree these commands behave as before.
Repository::rooted_atkeeps 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_branchno longer names the detached worktree the user is standing in:wt config state vars setthere saysCannot set variable: not on a branch (detached HEAD)with a plaingit switch <branch>hint.Behavior change outside a worktree
From a bare repository's directory or a
.gitdirectory,wt remove,wt merge,wt step commit,wt step squash,wt step diffandwt hooknow 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. Forwt remove, the default-branch refusal is what guards this case: with HEAD on the default branch,wt removeandwt remove @both refuse withCannot 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 withBranch <name> has no worktree, andwt removedeletes 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, sowt removethere refuses withNo 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 @fromproj/,proj/.bareandrepo/.gitagain 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.gitdirectory.test_bare_repo_at_from_bare_directory_names_head_branch:wt switch @andwt step copy-ignoredfrom a bare repository's directory.test_bare_repo_remove_from_bare_directory_targets_head_branch:wt removeandwt remove @give identical output there and refuse as default branch.test_bare_repo_previews_from_bare_directory_use_head_branch:wt hook showandwt step squash --show-promptread HEAD's branch's worktree, not the primary one.test_remove_no_argument_in_moved_worktree_refuses:wt removefrom a moved worktree refuses and leaves its checkout intact.test_vars_set_in_detached_worktree: the detached-HEAD message for the no-argument form.