worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Feature request: A post-switch blocking hook

Open IdoHadar opened this issue 8 months ago • 24 comments

I want to copy gitignored files every time I switch a worktree, but currently it's only possible when creating a new one. For example: I change something in my .env or .claude configs and want it to move over to the main worktree when switching back to it.

Currently I see no good way to do so because the post-switch hook is non-blocking and will interfere with claude starting with the old config and when using pre-switch the worktree may not be created yet.

IdoHadar avatar Mar 19 '26 11:03 IdoHadar

Thanks for the suggestion! I searched the codebase and confirmed there's no existing way to run blocking commands after a switch completes. Here's a summary of what exists today:

  • post-switch hooks exist but are intentionally non-blocking (spawned in the background), so wt step copy-ignored configured there will race with whatever runs next in the worktree.
  • pre-switch hooks are blocking, but as you noted, the destination worktree may not exist yet (for creates), and the switch hasn't happened so the working directory context is wrong.
  • post-create is blocking but only fires on worktree creation, not when switching to an existing one.

The closest workaround today would be wt switch <branch> --execute 'wt step copy-ignored', since --execute runs after the switch completes and blocks — but it's manual and doesn't integrate into the hook system.

I'll leave it for a maintainer to evaluate and prioritize.

worktrunk-bot avatar Mar 19 '26 11:03 worktrunk-bot

no strong reason to not offer this variant. any ideas for names?! either @IdoHadar or @worktrunk-bot ?

but also @IdoHadar if it's worth a moment to review if that specific thing is a good idea? do you really want to block on every switch? what files do you want to copy around? would you be better off symlinking them? (not something that wt step copy-ignored does, but maybe it's worth us thinking about this)

max-sixty avatar Mar 19 '26 18:03 max-sixty

A few naming ideas, roughly in order of preference:

1. post-switch becomes blocking; rename background variant to post-start-switch

This aligns with the create-path pattern where post-create (blocking) precedes post-start (background). The switch path would become:

  • pre-switch → blocking, fail-fast
  • switch happens
  • post-switch → blocking, warn (like post-create)
  • post-start-switch → background (like post-start)

This is a breaking change for anyone using post-switch today who relies on it being non-blocking, but it makes the naming consistent: post-{action} = blocking, post-start* = background. A deprecation cycle could help — keep running post-switch hooks in the background for one release with a warning, then flip to blocking.

2. New hook: post-enter

Fires on every switch (including creates), blocking with warn strategy. Avoids any breaking change. "Enter" captures the idea of landing in a worktree regardless of how you got there. Lifecycle:

  • switch/create happens
  • post-create (blocking, only on create)
  • post-enter (blocking, every switch)
  • post-switch / post-start (background, as today)

3. New hook: on-switch

Same as above but uses on- prefix to signal synchronous behavior. Feels a bit inconsistent with the existing pre-/post- convention though.

Of these, I lean toward option 1 — it's the cleanest long-term even though it requires a deprecation cycle. Users today who have post-switch = "some-command" likely want it to block and may not realize it doesn't.

worktrunk-bot avatar Mar 19 '26 18:03 worktrunk-bot

Currently I see no good way to do so because the post-switch hook is non-blocking and will interfere with claude starting with the old config and when using pre-switch the worktree may not be created yet.

actually, if you really want this, presumably it can work with a combination of pre-switch and post-create?

max-sixty avatar Mar 19 '26 20:03 max-sixty

It could work with pre-switch and post-create if it doesn't require blocking if I understand correctly, but I require blocking for claude. I would appreciate maybe some way to copy only specific files on every switch, (.claude directory, .env files) but I think your suggestion to symlink could also work, will this be supported if I create a symlink in main and then copy-ignored can presumably copy the symlink itself? maybe can have some directory for this stuff, I for example created a dedicated volumes directory for all my deployments so they will all use the same data regardless of worktree

IdoHadar avatar Mar 20 '26 16:03 IdoHadar

A few points that might help:

Symlinks are preserved by copy-ignored. If you create .env -> /shared/env in your primary worktree, copy-ignored will recreate the same symlink in the destination — it doesn't dereference. So all worktrees would point to the same shared file. That could work well for your use case.

You can already limit which files get copied using a .worktreeinclude file at the repo root. It uses gitignore-style patterns — only entries matching both .gitignore and .worktreeinclude are copied. For example:

# .worktreeinclude
.env
.claude/

This would copy only .env and .claude/ across worktrees, skipping other gitignored files like node_modules/ or build caches.

For the symlink approach specifically: you could create symlinks to a shared directory (like your volumes idea) and then use copy-ignored to replicate those symlinks across worktrees. The symlink targets are preserved exactly — absolute, relative, or otherwise. With .worktreeinclude you'd control which symlinks get copied.

On the blocking question — pre-switch is blocking and fires for existing worktrees, so pre-switch + post-create would cover both cases (switching to existing + creating new). The gap is that pre-switch runs in the source worktree context, not the destination. If you're copying files into the destination, you'd need the destination path — which pre-switch doesn't currently expose.

worktrunk-bot avatar Mar 20 '26 16:03 worktrunk-bot

which pre-switch doesn't currently expose.

please confirm this? that seems like one piece of the puzzle that would be required for @IdoHadar 's to work, and would be generally helpful

we should have a {{ branch }}-like variable available. Draft a PR if it isn't already (or maybe update docs if it is)

max-sixty avatar Mar 20 '26 18:03 max-sixty

Confirmed — pre-switch does expose {{ branch }} as the destination branch (the raw user input before resolution). However, {{ worktree_path }} in pre-switch points to the source (current) worktree, not the destination.

For existing worktrees, you can get the destination path using the template function:

{{ worktree_path_of_branch(branch) }}

So a working pre-switch hook for @IdoHadar's use case would look something like:

[pre-switch]
copy-configs = """
SRC="{{ worktree_path }}"
DEST="{{ worktree_path_of_branch(branch) }}"
if [ -n "$DEST" ]; then
  cp -r "$SRC/.claude" "$DEST/.claude" 2>/dev/null || true
  cp "$SRC/.env" "$DEST/.env" 2>/dev/null || true
fi
"""

The gap: worktree_path_of_branch returns empty if the destination doesn't exist yet (new worktree via --create). But for creates, post-create already handles that — it's blocking and has access to {{ base_worktree_path }}. So pre-switch + post-create should cover both cases today.

This is documented in the hook page but not prominently — the worktree_path_of_branch function is listed in the template reference section. Happy to send a docs PR making this pattern more visible if that would help.

worktrunk-bot avatar Mar 20 '26 18:03 worktrunk-bot

let's have pre-switch vars all point to the target, not the current; please open a PR with that change

max-sixty avatar Mar 20 '26 20:03 max-sixty

Done — #1633 makes pre-switch template variables point to the target worktree:

  • {{ worktree_path }} → destination worktree path (for existing worktrees; falls back to current for creates where the target doesn't exist yet)
  • {{ source_worktree_path }} → new variable with the current (source) worktree path

So @IdoHadar's use case would look like:

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

worktrunk-bot avatar Mar 20 '26 21:03 worktrunk-bot

Done — #1633 makes pre-switch template variables point to the target worktree:

  • {{ worktree_path }} → destination worktree path (for existing worktrees; falls back to current for creates where the target doesn't exist yet)
  • {{ source_worktree_path }} → new variable with the current (source) worktree path

So @IdoHadar's use case would look like:

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

Will this be blocking?

IdoHadar avatar Mar 22 '26 09:03 IdoHadar

Yes — pre-switch is already blocking today (it runs synchronously before the switch happens, and a non-zero exit code aborts the switch). That's what makes it suitable here.

The PR (#1633) only changes which worktree the template variables point to — it doesn't change the blocking behavior.

worktrunk-bot avatar Mar 22 '26 09:03 worktrunk-bot

What if the worktree does not exist? Will pre-switch and post-create both copy the files?

Done — #1633 makes pre-switch template variables point to the target worktree:

  • {{ worktree_path }} → destination worktree path (for existing worktrees; falls back to current for creates where the target doesn't exist yet)
  • {{ source_worktree_path }} → new variable with the current (source) worktree path

So @IdoHadar's use case would look like:

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

IdoHadar avatar Mar 22 '26 09:03 IdoHadar

Good question. When creating a new worktree, both hooks fire but at different phases, so there's no double-copy problem:

pre-switch fires first, but since the target worktree doesn't exist yet, {{ worktree_path }} falls back to the current (source) worktree path. So the cp command would be copying from source to source — effectively a no-op. The || true means it won't error out.

post-create fires after the worktree is created, with {{ worktree_path }} pointing to the newly created worktree. This is where the actual copy happens for new worktrees. It exposes {{ base_worktree_path }} (the worktree you created from).

So the full setup would be:

# Copies when switching to an existing worktree
[pre-switch]
copy-config = "cp -r {{ source_worktree_path }}/.claude {{ worktree_path }}/.claude 2>/dev/null || true"

# Copies when creating a new worktree (pre-switch is a no-op in this case)
[post-create]
copy-config = "cp -r {{ base_worktree_path }}/.claude {{ worktree_path }}/.claude 2>/dev/null || true"

Both pre-switch and post-create are blocking, so your config will be in place before anything else runs.

Alternatively, if you're using copy-ignored with a .worktreeinclude file, post-create already runs copy-ignored automatically — you'd just need the pre-switch hook for subsequent switches to existing worktrees.

worktrunk-bot avatar Mar 22 '26 09:03 worktrunk-bot

Alternatively, if you're using copy-ignored with a .worktreeinclude file, post-create already runs copy-ignored automatically

is this accurate?

max-sixty avatar Mar 22 '26 15:03 max-sixty

No — that's not accurate, apologies for the confusion. copy-ignored is a standalone command (wt step copy-ignored) that must be run explicitly. It is not invoked automatically during post-create or any other hook.

To copy gitignored files on worktree creation, you'd need to add it as a post-create hook yourself:

[post-create]
copy-ignored = "wt step copy-ignored"

worktrunk-bot avatar Mar 22 '26 15:03 worktrunk-bot

ok, please give a cohesive answer to @IdoHadar

max-sixty avatar Mar 22 '26 15:03 max-sixty

@IdoHadar here's a cohesive summary of how to set this up, covering both switching to existing worktrees and creating new ones:

Setup

Add these two hooks to your wt.toml:

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

[post-create]
copy-config = "cp -r {{ base_worktree_path }}/.claude {{ worktree_path }}/.claude 2>/dev/null || true; cp {{ base_worktree_path }}/.env {{ worktree_path }}/.env 2>/dev/null || true"

How it works

  • Switching to an existing worktree → pre-switch fires. It's blocking (runs before the switch completes), so your config is in place before anything else starts. {{ source_worktree_path }} is where you're coming from, {{ worktree_path }} is where you're going.

  • Creating a new worktree → pre-switch fires but is effectively a no-op (the destination doesn't exist yet, so {{ worktree_path }} falls back to the source path and the copy goes nowhere). Then post-create fires after the worktree is created — also blocking — and copies from {{ base_worktree_path }} to the new {{ worktree_path }}.

Both hooks are blocking, so your .claude and .env files will be in place before Claude or anything else starts in the target worktree.

Requires PR #1633

The {{ source_worktree_path }} variable and the updated {{ worktree_path }} behavior in pre-switch come from #1633, which is open but not yet merged.

Alternative: .worktreeinclude + copy-ignored

If you'd rather use the built-in copy-ignored mechanism instead of raw cp commands, create a .worktreeinclude file at the repo root:

.env
.claude/

Then configure the hooks to use wt step copy-ignored:

[pre-switch]
copy-config = "wt step copy-ignored --from {{ source_worktree_path }}"

[post-create]
copy-config = "wt step copy-ignored"

copy-ignored only copies files matching both .gitignore and .worktreeinclude, so it won't pull in node_modules or build caches. It also preserves symlinks if you go the symlink route later.

worktrunk-bot avatar Mar 22 '26 15:03 worktrunk-bot

@max-sixty @worktrunk-bot I saw there were PRs that address my feature but did not understand how should I use the new mechanism, can you help?

IdoHadar avatar Mar 25 '26 09:03 IdoHadar

@IdoHadar Several PRs landed recently that redesigned hook template variables (#1655, #1660, #1679). Here's how it all works now:

What changed

  • {{ worktree_path }} in pre-switch now points to the destination worktree (previously it was the source)
  • {{ base_worktree_path }} is a new variable — the worktree you're switching from
  • pre-start is the new name for what was post-create — it's blocking and runs after the worktree is created

Setup

Add this to your .config/wt.toml:

# When switching to an existing worktree (blocking, runs before switch)
[pre-switch]
copy-config = "cp -r {{ base_worktree_path }}/.claude {{ worktree_path }}/.claude 2>/dev/null || true; cp {{ base_worktree_path }}/.env {{ worktree_path }}/.env 2>/dev/null || true"

# When creating a new worktree (blocking, runs after worktree is created)
[pre-start]
copy-config = "cp -r {{ base_worktree_path }}/.claude {{ worktree_path }}/.claude 2>/dev/null || true; cp {{ base_worktree_path }}/.env {{ worktree_path }}/.env 2>/dev/null || true"

Both hooks are blocking — your files will be in place before Claude or anything else starts.

How the two hooks cover both cases

Scenario Hook that fires {{ base_worktree_path }} {{ worktree_path }}
Switch to existing worktree pre-switch Source (where you are) Destination
Create new worktree pre-start Source (where you created from) New worktree

pre-switch also fires on create, but the destination worktree doesn't exist yet at that point, so {{ worktree_path }} falls back — the pre-start hook handles that case.

Alternative: copy-ignored with .worktreeinclude

If you'd rather use the built-in file-copying mechanism, create a .worktreeinclude file at the repo root to limit which gitignored files are copied:

.env
.claude/

Then use wt step copy-ignored --force in your hooks:

[pre-switch]
copy-config = "wt step copy-ignored --from {{ base }} --to {{ branch }} --force"

[pre-start]
copy-config = "wt step copy-ignored --force"

The --force flag overwrites existing files so you always get the latest version.

Symlink alternative

If you'd prefer all worktrees to share the same files (rather than copying), you can symlink them to a shared location and use copy-ignored to replicate the symlinks — it preserves symlinks without dereferencing.

You'll need the latest release that includes these changes.

worktrunk-bot avatar Mar 25 '26 09:03 worktrunk-bot