Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Add template variable support to --execute command (#581)

Maximilian Roos9a92f8170259
diff --git a/.claude-plugin/skills/worktrunk/reference/switch.md b/.claude-plugin/skills/worktrunk/reference/switch.md
index 47f2df4ea..cfb9e695f 100644
--- a/.claude-plugin/skills/worktrunk/reference/switch.md
+++ b/.claude-plugin/skills/worktrunk/reference/switch.md
@@ -73,7 +73,7 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
Additional arguments for --execute command (after --)
Arguments after <b>--</b> are appended to the execute command. Each argument
- is POSIX shell-escaped before appending.
+ is expanded for templates, then POSIX shell-escaped.
<b><span class=g>Options:</span></b>
<b><span class=c>-c</span></b>, <b><span class=c>--create</span></b>
@@ -91,6 +91,10 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
full terminal control. Useful for launching editors, AI agents, or
other interactive tools.
+ Supports ]8;;@/hook.md#template-variables\<u>hook template variables]8;;\</u> (<b>{{ branch }}</b>, <b>{{ worktree_path }}</b>,
+ etc.) and filters. <b>{{ base }}</b> and <b>{{ base_worktree_path }}</b> require
+ --create.
+
Especially useful with shell aliases:
<b>alias wsc=&#39;wt switch --create -x claude&#39;</b>
@@ -100,6 +104,10 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
Arguments after <b>--</b> are passed to the command, so <b>wsc feature -- &#39;Fix</b>
GH #322&#39; runs <b>claude &#39;Fix GH #322&#39;</b>, starting Claude with a prompt.
+ Template example: <b>-x &#39;code {{ worktree_path }}&#39;</b> opens VS Code at the
+ worktree, <b>-x &#39;tmux new -s {{ branch | sanitize }}&#39;</b> starts a tmux
+ session named after the branch.
+
<b><span class=c>-y</span></b>, <b><span class=c>--yes</span></b>
Skip approval prompts
diff --git a/docs/content/switch.md b/docs/content/switch.md
index db7f1ffa4..b8c26394b 100644
--- a/docs/content/switch.md
+++ b/docs/content/switch.md
@@ -96,7 +96,7 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
Additional arguments for --execute command (after --)
Arguments after <b>--</b> are appended to the execute command. Each argument
- is POSIX shell-escaped before appending.
+ is expanded for templates, then POSIX shell-escaped.
<b><span class=g>Options:</span></b>
<b><span class=c>-c</span></b>, <b><span class=c>--create</span></b>
@@ -114,6 +114,10 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
full terminal control. Useful for launching editors, AI agents, or
other interactive tools.
+ Supports ]8;;@/hook.md#template-variables\<u>hook template variables]8;;\</u> (<b>{{ branch }}</b>, <b>{{ worktree_path }}</b>,
+ etc.) and filters. <b>{{ base }}</b> and <b>{{ base_worktree_path }}</b> require
+ --create.
+
Especially useful with shell aliases:
<b>alias wsc=&#39;wt switch --create -x claude&#39;</b>
@@ -123,6 +127,10 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
Arguments after <b>--</b> are passed to the command, so <b>wsc feature -- &#39;Fix</b>
GH #322&#39; runs <b>claude &#39;Fix GH #322&#39;</b>, starting Claude with a prompt.
+ Template example: <b>-x &#39;code {{ worktree_path }}&#39;</b> opens VS Code at the
+ worktree, <b>-x &#39;tmux new -s {{ branch | sanitize }}&#39;</b> starts a tmux
+ session named after the branch.
+
<b><span class=c>-y</span></b>, <b><span class=c>--yes</span></b>
Skip approval prompts
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 35e7726ba..3be666465 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -333,6 +333,10 @@ To change which branch a worktree is on, use `git switch` inside that worktree.
/// it full terminal control. Useful for launching editors, AI agents,
/// or other interactive tools.
///
+ /// Supports [hook template variables](@/hook.md#template-variables)
+ /// (`{{ branch }}`, `{{ worktree_path }}`, etc.) and filters.
+ /// `{{ base }}` and `{{ base_worktree_path }}` require `--create`.
+ ///
/// Especially useful with shell aliases:
///
/// ```sh
@@ -344,13 +348,17 @@ To change which branch a worktree is on, use `git switch` inside that worktree.
/// Code. Arguments after `--` are passed to the command, so
/// `wsc feature -- 'Fix GH #322'` runs `claude 'Fix GH #322'`,
/// starting Claude with a prompt.
+ ///
+ /// Template example: `-x 'code {{ worktree_path }}'` opens VS Code
+ /// at the worktree, `-x 'tmux new -s {{ branch | sanitize }}'` starts
+ /// a tmux session named after the branch.
#[arg(short = 'x', long)]
execute: Option<String>,
/// Additional arguments for --execute command (after --)
///
/// Arguments after `--` are appended to the execute command.
- /// Each argument is POSIX shell-escaped before appending.
+ /// Each argument is expanded for templates, then POSIX shell-escaped.
#[arg(last = true, requires = "execute")]
execute_args: Vec<String>,
diff --git a/src/main.rs b/src/main.rs
index ef27726e9..bb234abbc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,7 +3,7 @@ use clap::FromArgMatches;
use color_print::cformat;
use std::path::PathBuf;
use std::process;
-use worktrunk::config::{WorktrunkConfig, set_config_path};
+use worktrunk::config::{WorktrunkConfig, expand_template, set_config_path};
use worktrunk::git::{Repository, exit_code, set_base_path};
use worktrunk::path::format_path_for_display;
use worktrunk::shell::extract_filename_from_path;
@@ -26,7 +26,7 @@ mod verbose_log;
pub use crate::cli::OutputFormat;
-use commands::command_executor::CommandContext;
+use commands::command_executor::{CommandContext, build_hook_context};
#[cfg(unix)]
use commands::handle_select;
use commands::worktree::{SwitchResult, handle_push};
@@ -1405,6 +1405,26 @@ fn main() {
output::prompt_shell_integration(&mut config, &binary_name(), skip_prompt);
}
+ // Build extra vars for base branch context (used by both hooks and --execute)
+ // "base" is the branch we branched from when creating a new worktree.
+ // For existing worktrees, there's no base concept.
+ let (base_branch, base_worktree_path): (Option<&str>, Option<&str>) = match &result
+ {
+ SwitchResult::Created {
+ base_branch,
+ base_worktree_path,
+ ..
+ } => (base_branch.as_deref(), base_worktree_path.as_deref()),
+ SwitchResult::Existing(_) | SwitchResult::AlreadyAt(_) => (None, None),
+ };
+ let extra_vars: Vec<(&str, &str)> = [
+ base_branch.map(|b| ("base", b)),
+ base_worktree_path.map(|p| ("base_worktree_path", p)),
+ ]
+ .into_iter()
+ .flatten()
+ .collect();
+
// Spawn background hooks after success message
// - post-switch: runs on ALL switches (shows "@ path" when shell won't be there)
// - post-start: runs only when creating a NEW worktree
@@ -1420,26 +1440,6 @@ fn main() {
yes,
);
- // Build extra vars for base branch context
- // "base" is the branch we branched from when creating a new worktree.
- // For existing worktrees, there's no base concept.
- let (base_branch, base_worktree_path): (Option<&str>, Option<&str>) =
- match &result {
- SwitchResult::Created {
- base_branch,
- base_worktree_path,
- ..
- } => (base_branch.as_deref(), base_worktree_path.as_deref()),
- SwitchResult::Existing(_) | SwitchResult::AlreadyAt(_) => (None, None),
- };
- let extra_vars: Vec<(&str, &str)> = [
- base_branch.map(|b| ("base", b)),
- base_worktree_path.map(|p| ("base_worktree_path", p)),
- ]
- .into_iter()
- .flatten()
- .collect();
-
// Post-switch runs first (immediate "I'm here" signal)
ctx.spawn_post_switch_commands(&extra_vars, hooks_display_path.as_deref())?;
@@ -1452,15 +1452,46 @@ fn main() {
// Execute user command after post-start hooks have been spawned
// Note: execute_args requires execute via clap's `requires` attribute
if let Some(cmd) = execute {
+ // Build template context for expansion (includes base vars when creating)
+ let repo = Repository::current()?;
+ let repo_root = repo.worktree_base().context("Failed to get repo root")?;
+ let ctx = CommandContext::new(
+ &repo,
+ &config,
+ Some(&branch_info.branch),
+ result.path(),
+ &repo_root,
+ yes,
+ );
+ let template_vars = build_hook_context(&ctx, &extra_vars);
+ let vars: std::collections::HashMap<&str, &str> = template_vars
+ .iter()
+ .map(|(k, v)| (k.as_str(), v.as_str()))
+ .collect();
+
+ // Expand template variables in command (shell_escape: true for safety)
+ let expanded_cmd = expand_template(&cmd, &vars, true).map_err(|e| {
+ anyhow::anyhow!("Failed to expand --execute template: {}", e)
+ })?;
+
// Append any trailing args (after --) to the execute command
+ // Each arg is also expanded, then shell-escaped
let full_cmd = if execute_args.is_empty() {
- cmd
+ expanded_cmd
} else {
- let escaped_args: Vec<_> = execute_args
+ let expanded_args: Result<Vec<_>, _> = execute_args
+ .iter()
+ .map(|arg| {
+ expand_template(arg, &vars, false).map_err(|e| {
+ anyhow::anyhow!("Failed to expand argument template: {}", e)
+ })
+ })
+ .collect();
+ let escaped_args: Vec<_> = expanded_args?
.iter()
.map(|arg| shlex::try_quote(arg).unwrap_or(arg.into()).into_owned())
.collect();
- format!("{} {}", cmd, escaped_args.join(" "))
+ format!("{} {}", expanded_cmd, escaped_args.join(" "))
};
execute_user_command(&full_cmd)?;
}
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index c49d3b772..4fee1adc0 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -306,6 +306,110 @@ fn test_switch_execute_multiline(repo: TestRepo) {
&["--create", "multiline-test", "--execute", multiline_cmd],
);
}
+
+// Execute template expansion tests
+#[rstest]
+fn test_switch_execute_template_branch(repo: TestRepo) {
+ // Test that {{ branch }} is expanded in --execute command
+ snapshot_switch(
+ "switch_execute_template_branch",
+ &repo,
+ &[
+ "--create",
+ "template-test",
+ "--execute",
+ "echo 'branch={{ branch }}'",
+ ],
+ );
+}
+
+#[rstest]
+fn test_switch_execute_template_base(repo: TestRepo) {
+ // Test that {{ base }} is available when creating with --create
+ snapshot_switch(
+ "switch_execute_template_base",
+ &repo,
+ &[
+ "--create",
+ "from-main",
+ "--base",
+ "main",
+ "--execute",
+ "echo 'base={{ base }}'",
+ ],
+ );
+}
+
+#[rstest]
+fn test_switch_execute_template_base_without_create(mut repo: TestRepo) {
+ // Test that {{ base }} is empty when switching to existing worktree (no --create)
+ repo.add_worktree("existing");
+ snapshot_switch(
+ "switch_execute_template_base_without_create",
+ &repo,
+ &["existing", "--execute", "echo 'base={{ base }}'"],
+ );
+}
+
+#[rstest]
+fn test_switch_execute_template_with_filter(repo: TestRepo) {
+ // Test that filters work ({{ branch | sanitize }})
+ snapshot_switch(
+ "switch_execute_template_with_filter",
+ &repo,
+ &[
+ "--create",
+ "feature/with-slash",
+ "--execute",
+ "echo 'sanitized={{ branch | sanitize }}'",
+ ],
+ );
+}
+
+#[rstest]
+fn test_switch_execute_template_shell_escape(repo: TestRepo) {
+ // Test that shell metacharacters in branch names are escaped
+ // Without escaping, this would execute `id` as a separate command
+ snapshot_switch(
+ "switch_execute_template_shell_escape",
+ &repo,
+ &["--create", "feat;id", "--execute", "echo {{ branch }}"],
+ );
+}
+
+#[rstest]
+fn test_switch_execute_template_worktree_path(repo: TestRepo) {
+ // Test that {{ worktree_path }} is expanded
+ snapshot_switch(
+ "switch_execute_template_worktree_path",
+ &repo,
+ &[
+ "--create",
+ "path-test",
+ "--execute",
+ "echo 'path={{ worktree_path }}'",
+ ],
+ );
+}
+
+#[rstest]
+fn test_switch_execute_template_in_args(repo: TestRepo) {
+ // Test that templates are expanded in trailing args (after --)
+ snapshot_switch(
+ "switch_execute_template_in_args",
+ &repo,
+ &[
+ "--create",
+ "args-test",
+ "--execute",
+ "echo",
+ "--",
+ "branch={{ branch }}",
+ "repo={{ repo }}",
+ ],
+ );
+}
+
// --no-verify flag tests
#[rstest]
fn test_switch_no_config_commands_execute_still_runs(repo: TestRepo) {
diff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
index 1e934c20e..a5ca5798f 100644
--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
@@ -33,7 +33,7 @@ Usage: wt switch [OPTIONS] <BRANCH> [--
[EXECUTE_ARGS]...
Additional arguments for --execute command (after --)
- Arguments after -- are appended to the execute command. Each argument is POSIX shell-escaped before appending.
+ Arguments after -- are appended to the execute command. Each argument is expanded for templates, then POSIX shell-escaped.
Options:
-c, --create
@@ -49,12 +49,16 @@ Usage: wt switch [OPTIONS] <BRANCH> [--
Replaces the wt process with the command after switching, giving it full terminal control. Useful for launching editors, AI agents, or other interactive tools.
+ Supports ]8;;@/hook.md#template-variables\hook template variables]8;;\ ({{ branch }}, {{ worktree_path }}, etc.) and filters. {{ base }} and {{ base_worktree_path }} require --create.
+
Especially useful with shell aliases:
alias wsc='wt switch --create -x claude'
wsc feature-branch -- 'Fix GH #322'
Then wsc feature-branch creates the worktree and launches Claude Code. Arguments after -- are passed to the command, so wsc feature -- 'Fix GH #322' runs claude 'Fix GH #322', starting Claude with a prompt.
+
+ Template example: -x 'code {{ worktree_path }}' opens VS Code at the worktree, -x 'tmux new -s {{ branch | sanitize }}' starts a tmux session named after the branch.
-y, --yes
Skip approval prompts
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_template_base.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_base.snap
new file mode 100644
index 000000000..42c250c00
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_base.snap
@@ -0,0 +1,46 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - from-main
+ - "--base"
+ - main
+ - "--execute"
+ - "echo 'base={{ base }}'"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+base=main
+
+----- stderr -----
+✓ Created branch from-main and worktree from main @ _REPO_.from-main
+↳ Customize worktree locations: wt config create
+▲ Executing echo 'base={{ base }}' @ _REPO_.from-main, but shell directory unchanged — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
+◎ Executing (--execute):
+  echo 'base=main'
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_template_base_without_create.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_base_without_create.snap
new file mode 100644
index 000000000..d47b9dfe6
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_base_without_create.snap
@@ -0,0 +1,41 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - existing
+ - "--execute"
+ - "echo 'base={{ base }}'"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+base=
+
+----- stderr -----
+▲ Executing echo 'base={{ base }}' @ _REPO_.existing, but shell directory unchanged — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
+◎ Executing (--execute):
+  echo 'base='
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_template_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_branch.snap
new file mode 100644
index 000000000..375f3a3ac
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_branch.snap
@@ -0,0 +1,44 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - template-test
+ - "--execute"
+ - "echo 'branch={{ branch }}'"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+branch=template-test
+
+----- stderr -----
+✓ Created branch template-test and worktree from main @ _REPO_.template-test
+↳ Customize worktree locations: wt config create
+▲ Executing echo 'branch={{ branch }}' @ _REPO_.template-test, but shell directory unchanged — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
+◎ Executing (--execute):
+  echo 'branch=template-test'
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_template_in_args.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_in_args.snap
new file mode 100644
index 000000000..2097f3abe
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_in_args.snap
@@ -0,0 +1,47 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - args-test
+ - "--execute"
+ - echo
+ - "--"
+ - "branch={{ branch }}"
+ - "repo={{ repo }}"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+branch=args-test repo=repo
+
+----- stderr -----
+✓ Created branch args-test and worktree from main @ _REPO_.args-test
+↳ Customize worktree locations: wt config create
+▲ Executing echo @ _REPO_.args-test, but shell directory unchanged — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
+◎ Executing (--execute):
+  echo 'branch=args-test' 'repo=repo'
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_template_shell_escape.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_shell_escape.snap
new file mode 100644
index 000000000..c31d856ae
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_shell_escape.snap
@@ -0,0 +1,44 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - feat;id
+ - "--execute"
+ - "echo {{ branch }}"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+feat;id
+
+----- stderr -----
+✓ Created branch feat;id and worktree from main @ _REPO_.feat;id
+↳ Customize worktree locations: wt config create
+▲ Executing echo {{ branch }} @ _REPO_.feat;id, but shell directory unchanged — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
+◎ Executing (--execute):
+  echo 'feat;id'
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_template_with_filter.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_with_filter.snap
new file mode 100644
index 000000000..4384dc902
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_with_filter.snap
@@ -0,0 +1,44 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - feature/with-slash
+ - "--execute"
+ - "echo 'sanitized={{ branch | sanitize }}'"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+sanitized=feature-with-slash
+
+----- stderr -----
+✓ Created branch feature/with-slash and worktree from main @ _REPO_.feature-with-slash
+↳ Customize worktree locations: wt config create
+▲ Executing echo 'sanitized={{ branch | sanitize }}' @ _REPO_.feature-with-slash, but shell directory unchanged — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
+◎ Executing (--execute):
+  echo 'sanitized=feature-with-slash'
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_template_worktree_path.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_worktree_path.snap
new file mode 100644
index 000000000..9c5bb29ce
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_template_worktree_path.snap
@@ -0,0 +1,44 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - path-test
+ - "--execute"
+ - "echo 'path={{ worktree_path }}'"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+path=_REPO_.path-test
+
+----- stderr -----
+✓ Created branch path-test and worktree from main @ _REPO_.path-test
+↳ Customize worktree locations: wt config create
+▲ Executing echo 'path={{ worktree_path }}' @ _REPO_.path-test, but shell directory unchanged — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
+◎ Executing (--execute):
+  echo 'path=_REPO_.path-test'