Skip to content

docs(hook): correct pre-switch {{ branch }} resolution note - #3516

Merged
max-sixty merged 2 commits into
mainfrom
nightly/pre-switch-branch-doc-29677559340
Jul 20, 2026
Merged

max-sixty merged 2 commits into
mainfrom
nightly/pre-switch-branch-doc-29677559340

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The pre-switch hook 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 for wt switch - / @ / ^.

run_pre_switch_hooks (src/commands/worktree/switch.rs) resolves the argument first:

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), &current_path, yes);

{{ 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

  • Hook Types table (src/cli/mod.rs, mirrored to docs/content/hook.md and the skill references): the pre-switch row 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.
  • Inline comment at the run_pre_switch_hooks call 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: a pre-switch hook 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 rendering echo 'main'.

Doc mirrors regenerated via test_docs_are_in_sync. No --help snapshot captures the Hook Types table, so none changed.

Found during the nightly code survey.

@worktrunk-bot worktrunk-bot added the nightly-cleanup Issues found by nightly code quality sweep label Jul 19, 2026
Comment thread src/cli/mod.rs Outdated
| 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 |

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this level of detail in the defn

please move / remove (including the old note)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Comment thread docs/content/hook.md
| 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 |

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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), &current_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.

@max-sixty
max-sixty merged commit c6eb148 into main Jul 20, 2026
42 checks passed
@max-sixty
max-sixty deleted the nightly/pre-switch-branch-doc-29677559340 branch July 20, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nightly-cleanup Issues found by nightly code quality sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants