Repository navigation
fix: point pre-switch hook vars to target worktree - #1633
worktrunk-bot wants to merge 5 commits into
Conversation
`{{ worktree_path }}` in pre-switch hooks now resolves to the destination
worktree path (for existing worktrees), and `{{ source_worktree_path }}`
provides the current (source) path. This enables copying files between
worktrees on every switch.
For creates where the target doesn't exist yet, `{{ worktree_path }}`
falls back to the current worktree.
Closes #1617
Co-Authored-By: Claude Opus 4.6 <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
test_user_pre_switch_worktree_path_is_destination uses assert_eq! to compare {{ worktree_path }} (POSIX-converted via to_posix_path in build_hook_context) against feature_wt.to_string_lossy() (native path). On Linux/macOS this is fine (to_posix_path is a no-op), but on Windows to_posix_path converts C:\... to /c/... via cygpath, while to_string_lossy() keeps the native format — the assert_eq! would fail.
Compare with test_user_post_remove_template_vars_reference_removed_worktree which uses contains with just the directory name for cross-platform safety.
Same issue applies to the source_worktree_path assertion on line 1993.
The test compared POSIX-converted template paths (/c/Users/...) against native Windows paths (C:\Users\...) via assert_eq!, causing Windows CI failure. Switch to ends_with on directory names, matching the pattern used by test_user_post_remove_template_vars_reference_removed_worktree. Co-Authored-By: Claude <[email protected]>
Template variables use POSIX paths (via to_posix_path), so the test must compare with to_slash_lossy() rather than to_string_lossy() to handle Windows paths correctly. Co-Authored-By: Claude Opus 4.6 <[email protected]>
On Windows, template variables are cygpath-converted (/c/Users/...) while PathBuf uses native paths (C:\...). Compare directory names instead of full paths for cross-platform correctness. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
| ## pre-switch | ||
|
|
||
| Runs before every `wt switch` — before branch resolution or worktree creation. `{{ branch }}` is the destination branch argument as the user typed it (before resolution). Failure aborts the switch. | ||
| Runs before every `wt switch` — before branch resolution or worktree creation. Template variables point to the **destination** worktree: `{{ branch }}` is the target branch (as the user typed it, before resolution), and `{{ worktree_path }}` is the target worktree path. `{{ source_worktree_path }}` provides the current (source) worktree path. Failure aborts the switch. |
There was a problem hiding this comment.
why not have this as base_worktree_path? that would then be consistent with the others?
There was a problem hiding this comment.
They're actually different concepts — base_worktree_path is the worktree of the branch you're branching from (e.g., main's worktree when running wt switch --create feature --base main), while source_worktree_path is the worktree you're currently sitting in.
If you're in worktree A (feature-1) and run wt switch --create feature-2 --base main, then base_worktree_path = main's worktree, but source_worktree_path = feature-1's worktree. They can point to completely different places.
Happy to rename if you have a better name in mind though — current_worktree_path could also work?
Phase 1 of the template variable redesign discussed in #1633. Adds directional vars (`base`, `target`, `target_worktree_path`, `cwd`) to all two-worktree hooks — non-breaking additions only, no existing behavior changes. ## New variables - **`cwd`** — execution directory, always exists on disk (all hooks). Enables the Active model in Phase 2 where `worktree_path` becomes identity rather than execution context. - **`target_worktree_path`** — target worktree path, added alongside existing `target` for merge hooks (pre-merge, post-merge) and remove hooks (pre-remove, post-remove). - **`base` / `base_worktree_path`** — source branch/worktree, added to pre-switch hooks. For existing destinations, `target` / `target_worktree_path` are also provided. These follow the "Active" model where each two-worktree operation has a base (source/origin) and target (destination) side: - **Arriving ops** (switch, create): Active = destination. `base` = where you came from. - **Departing ops** (merge, remove): Active = source. `target` = where it's going. Mnemonic: *"Are you going there, or is it going away?"* Phase 2 (separate PR) will change bare var semantics so `worktree_path` consistently points to the Active worktree, with `cwd` as the execution-context escape hatch. Ref #1633 > _This was written by Claude Code on behalf of @max-sixty_ --------- Co-authored-by: Claude <[email protected]>
Phase 2 of the template variable redesign. **Breaking change**: `worktree_path` now points to the Active worktree (the subject of the operation) rather than always the execution directory. ## Changes **pre-switch (existing worktrees):** `worktree_path` now points to the destination worktree, not the source. Use `base_worktree_path` or `cwd` for the source. **pre-switch (creates):** `worktree_path` keeps its default (source worktree = cwd). The planned destination path isn't available until after pre-switch hooks complete. **post-merge:** `worktree_path` now points to the feature worktree (Active), not the merge target. Use `target_worktree_path` or `cwd` for where code landed. Feature identity (path, name, commit) is captured before removal so it's available even after the worktree is deleted. **Empty extra_vars removal mechanism removed** — it was added and then removed in the same PR as the pre-switch create approach changed. ## The model - **Arriving ops** (switch, create): Active = destination - **Departing ops** (merge, remove): Active = source Mnemonic: *"Are you going there, or is it going away?"* Ref #1633 > _This was written by Claude Code on behalf of @max-sixty_ --------- Co-authored-by: Claude <[email protected]>
Follow-up fixes and documentation for the template variable redesign (#1655, #1660). **Code fixes:** - `commit` derivation now uses `rev-parse <branch>` instead of `rev-parse HEAD`, so `{{ commit }}` follows the Active branch - `base` / `base_worktree_path` now available in post-switch for existing switches (previously only creates) - post-remove `{{ target }}` now correctly resolves to the destination branch, not the removed branch **Documentation:** - Restructured template variable table — Active vars grouped first, directional vars second, repo-level vars last - Added arrive/depart table explaining what bare vars, `base`, and `target` point to per operation - Documented when `cwd` differs from `worktree_path` (pre-switch existing, post-remove, post-merge with removal) **Tests:** 4 new integration tests for directional var semantics. Ref #1633 > _This was written by Claude Code on behalf of @max-sixty_ Co-authored-by: Claude <[email protected]>
|
Closing — superseded by the template variable redesign in #1655 and #1660. The
|
) Adds changelog entry for the template variable redesign (breaking + improved) and a migration section in the hook docs explaining what changed and how to update existing hooks. Ref #1633 > _This was written by Claude Code on behalf of @max-sixty_ Co-authored-by: Claude <[email protected]>
Summary
{{ worktree_path }}inpre-switchhooks now points to the destination worktree (for existing worktrees), not the source{{ source_worktree_path }}variable provides the current (source) worktree path{{ worktree_path }}falls back to the current worktreeThis enables the pattern @IdoHadar requested in #1617 — copying files between worktrees on every switch:
Test plan
{{ worktree_path }}= destination and{{ source_worktree_path }}= source🤖 Generated with Claude Code