Skip to content

fix: point pre-switch hook vars to target worktree - #1633

Closed
worktrunk-bot wants to merge 5 commits into
mainfrom
fix/pre-switch-target-vars
Closed

worktrunk-bot wants to merge 5 commits into
mainfrom
fix/pre-switch-target-vars

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Summary

  • {{ worktree_path }} in pre-switch hooks now points to the destination worktree (for existing worktrees), not the source
  • New {{ source_worktree_path }} variable provides the current (source) worktree path
  • For creates (target doesn't exist yet), {{ worktree_path }} falls back to the current worktree

This enables the pattern @IdoHadar requested in #1617 — copying files between worktrees on every switch:

[pre-switch]
copy-config = "cp -r {{ source_worktree_path }}/.claude {{ worktree_path }}/.claude 2>/dev/null || true"

Test plan

  • Existing pre-switch tests updated for new target-pointing behavior
  • New test verifies {{ worktree_path }} = destination and {{ source_worktree_path }} = source
  • All 1215 integration tests pass
  • Lints pass

🤖 Generated with Claude Code

`{{ 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 worktrunk-bot left a comment

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.

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.

Comment thread tests/integration_tests/user_hooks.rs Resolved
worktrunk-bot and others added 4 commits March 20, 2026 21:29
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]>
Comment thread docs/content/hook.md
## 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.

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.

why not have this as base_worktree_path? that would then be consistent with the others?

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.

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?

max-sixty added a commit that referenced this pull request Mar 22, 2026
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]>
max-sixty added a commit that referenced this pull request Mar 22, 2026
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]>
max-sixty added a commit that referenced this pull request Mar 22, 2026
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]>
@max-sixty

Copy link
Copy Markdown
Owner

Closing — superseded by the template variable redesign in #1655 and #1660.

The source_worktree_path concept from this PR is now covered by base_worktree_path (available in pre-switch and all creation hooks), as part of a broader model where bare vars consistently point to the Active branch and directional vars (base/target) give the other side.

This was written by Claude Code on behalf of @max-sixty

@max-sixty max-sixty closed this Mar 22, 2026
max-sixty added a commit that referenced this pull request Mar 22, 2026
)

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]>
@max-sixty
max-sixty deleted the fix/pre-switch-target-vars branch June 18, 2026 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants