Repository navigation
fix(switch): resolve a worktree path for pre-switch hook variables - #4493
Conversation
run_pre_switch_hooks looked the destination up by branch name only, so a worktree path argument — what the picker passes — left branch/target as the raw path and worktree_path, worktree_name, upstream, and commit describing the source. It now runs the same resolve_selector ladder as plan_switch, honoring --create's branch-only reading. A detached destination leaves branch/target unset and takes commit from its own HEAD. Closes #4492
|
@worktrunk-bot can you survey our handling of paths vs branches; evaluate:
|
|
Summary: the boundary already exists and most commands use it. Bugs1. A
2. After that, both the main worktree and Consistency across commands
Structural suggestions
Smaller observation: I can open fix PRs for bugs 1 and 2: switch resolving once, and promote resolving to a worktree and refusing duplicate checkouts. Say if you want them split or combined with the helper changes. |
|
OK will fix those on my end |
…hrough (#4496) 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_ Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
run_pre_switch_hooksfound the destination withworktree_for_branch, so it only matched a branch name. A worktree path, which is what the interactive picker passes, matched nothing.branchandtargetthen held the raw path, whileworktree_path,worktree_name,upstream, andcommitdescribed the source worktree. The hook now runs the sameresolve_selectorladder thatplan_switchuses: branch first, then the argument as a worktree path, and branch-only under--create. With that change,wt switch ../repo.featurerenders the same variables aswt switch feature. A detached worktree can only be named by path. For one,branchandtargetstay unset, matching #4009, andcommitcomes from the destination's HEAD instead of the source's.test_pre_switch_vars_with_path_argumentreproduces the report: on main it rendersbranch=<path> … wt_name=repo target_wt= commit=, and it passes with this change.test_pre_switch_vars_with_detached_path_argumentcovers the detached case. The existing switch, picker, and user-hook integration tests still pass (474), and clippy is clean.Closes #4492 — automated triage