Repository navigation
docs(hook): correct pre-switch {{ branch }} resolution note - #3516
Conversation
| | Hook | Purpose | | ||
| |------|---------| | ||
| | `pre-switch` | Runs before branch resolution or worktree creation. `{{ branch }}` is the destination as typed (before resolution) | | ||
| | `pre-switch` | Runs before worktree creation. `{{ branch }}` is the resolved destination branch — symbolic args like `-`, `@`, `^` resolve to the concrete name | |
There was a problem hiding this comment.
I don't think we need this level of detail in the defn
please move / remove (including the old note)
There was a problem hiding this comment.
Done — dropped the {{ branch }} note (old and new) from the cell. It now just describes when and where the hook runs, matching the other rows; the Template variables section already documents {{ branch }}. Regenerated the doc mirrors in the same push.
Per review feedback, the pre-switch cell doesn't need the {{ branch }}
resolution detail — the Template variables section already covers it.
Describe when/where the hook runs, matching the other rows.
| | Hook | Purpose | | ||
| |------|---------| | ||
| | `pre-switch` | Runs before branch resolution or worktree creation. `{{ branch }}` is the destination as typed (before resolution) | | ||
| | `pre-switch` | Runs in the source worktree before switching — creating, switching to existing, or staying on current | |
There was a problem hiding this comment.
what does "staying on current" mean?
can you also check back to when we first made the initial change and consider why we made it? I worry there was a good reason which we're now obscuring (but also maybe the functionality changed?)
There was a problem hiding this comment.
Two answers:
"staying on current" — the no-op switch: wt switch <branch> when you're already on that branch's worktree. The resolve step lands on the current branch and nothing moves, but pre-switch still fires. It's the same three-case phrasing the post-switch row already uses ("creating, switching to existing, or staying on current"). This PR's own snapshot exercises it: wt switch @ from main prints Already on worktree for main, and the pre-switch hook still ran.
On the original note — you're right that it had a reason, and the reason went stale via an intentional behavior change (not this PR). The {{ branch }} / "before resolution" wording was correct when written, then a later fix changed the mechanism it described. Trimming the note removes a line that's now wrong; nothing deliberate is being obscured. Timeline in the details below.
History of the note
- feat(switch): expose destination branch in pre-switch hooks #1497
feat(switch): expose destination branch in pre-switch hooks(e47778d5b) — movedplan_switchresolution before the hook so{{ branch }}became the destination instead of the source. It rewrote the doc from "template variables reflect the current worktree (the source)" to "{{ branch }}is the destination branch argument as the user typed it (before resolution)". At that point the "as typed" clause was accurate for a symbolic arg —@/-/^were not yet resolved before the hook, so the var held the literal token. - fix(switch): resolve symbolic target (-, @, ^) before pre-switch hooks #2310
fix(switch): resolve symbolic target (-, @, ^) before pre-switch hooks(d97a6c4b1) — addedresolve_worktree_name(target_branch)into the hook context build, intentionally so{{ target }},{{ target_worktree_path }}, and the Active overrides point at the real destination. That's the behavior change: from fix(switch): resolve symbolic target (-, @, ^) before pre-switch hooks #2310 on, a symbolic arg is resolved to its concrete branch before the hook, so "as typed (before resolution)" became wrong for exactly the@/-/^cases. - The doc was never updated for fix(switch): resolve symbolic target (-, @, ^) before pre-switch hooks #2310, so the "as typed (before resolution)" note carried a stale claim until this PR.
Current code, src/commands/worktree/switch.rs:
let resolved_target = repo
.resolve_worktree_name(target_branch)
.unwrap_or_else(|_| target_branch.to_string());
let pre_ctx = CommandContext::new(repo, config, Some(&resolved_target), ¤t_path, yes);So {{ branch }} derives from resolved_target — the concrete destination — which is what the new test test_user_pre_switch_branch_var_resolves_symbolic pins (@ → main, not @).
Given that, removing the note (per your earlier "remove including the old note") is the right call — the Template variables section is the place to describe {{ branch }}, and if it's worth stating there that symbolic args are resolved for pre-switch, I'm happy to add a line to that section instead of the table. Let me know if you'd prefer that.
Problem
The
pre-switchhook documentation described{{ branch }}as the destination "as typed (before resolution)" — but the code resolves symbolic arguments before the hook runs, so the description was wrong forwt switch -/@/^.run_pre_switch_hooks(src/commands/worktree/switch.rs) resolves the argument first:{{ branch }}derives from that context (branch_or_head()), so for a symbolic argument it holds the resolved concrete branch, not the literal token. The old wording only happened to be right for plain branch names, where raw == resolved.Changes
src/cli/mod.rs, mirrored todocs/content/hook.mdand the skill references): thepre-switchrow no longer carries a{{ branch }}note at all. Per review feedback that level of detail doesn't belong in the table — the Template variables section already documents{{ branch }}. The row now describes when and where the hook runs, matching the other rows.run_pre_switch_hookscall site: corrected to state that symbolic args are resolved before the hook runs, so{{ branch }}/{{ target }}carry the concrete destination.Verification
Added
test_user_pre_switch_branch_var_resolves_symbolic: apre-switchhook writes{{ branch }}to a marker while switching with the symbolic arg@. The marker contains the resolved concrete name (main), not@— which fails against the old documented behavior and confirms the resolution mechanism. The snapshot shows the hook preview renderingecho 'main'.Doc mirrors regenerated via
test_docs_are_in_sync. No--helpsnapshot captures the Hook Types table, so none changed.Found during the nightly code survey.