Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(switch): resolve symbolic target (-, @, ^) before pre-switch hooks (#2310)
Worktrunk Botd97a6c4b186c
diff --git a/src/commands/handle_switch.rs b/src/commands/handle_switch.rsindex f2245c9ee..0a3aa5674 100644--- a/src/commands/handle_switch.rs+++ b/src/commands/handle_switch.rs@@ -94,8 +94,11 @@ pub struct SwitchOptions<'a> {/// Run pre-switch hooks before branch resolution or worktree creation.///-/// The hook context uses the **destination** branch argument as `{{ branch }}`,-/// so hooks receive the user's raw input before resolution.+/// Symbolic arguments (`-`, `@`, `^`) are resolved to concrete branch names+/// before building the hook context so `{{ target }}`, `{{ target_worktree_path }}`,+/// and the Active overrides point at the real destination. When resolution+/// fails (e.g., no previous branch for `-`), the raw argument is used — the+/// same error surfaces later from `plan_switch` with the canonical message.////// Directional vars:/// - `base` / `base_worktree_path`: current (source) branch and worktree@@ -108,7 +111,10 @@ pub(crate) fn run_pre_switch_hooks() -> anyhow::Result<()> {let current_wt = repo.current_worktree();let current_path = current_wt.path().to_path_buf();- let pre_ctx = CommandContext::new(repo, config, Some(target_branch), ¤t_path, yes);+ 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), ¤t_path, yes);let pre_switch_approved = approve_hooks(&pre_ctx, &[HookType::PreSwitch])?;if pre_switch_approved {@@ -124,7 +130,7 @@ pub(crate) fn run_pre_switch_hooks(// Target vars and Active overrides: destination worktree.// For existing worktrees: override bare vars (worktree_path, worktree_name,// worktree) to point to the destination (Active), not the source.- let dest_path = repo.worktree_for_branch(target_branch).ok().flatten();+ let dest_path = repo.worktree_for_branch(&resolved_target).ok().flatten();let dest_name = dest_path.as_ref().and_then(|p| p.file_name())@@ -132,7 +138,7 @@ pub(crate) fn run_pre_switch_hooks(.map(|s| s.to_string());let dest_path_str = dest_path.map(|p| worktrunk::path::to_posix_path(&p.to_string_lossy()));- extra_vars.push(("target", target_branch));+ extra_vars.push(("target", &resolved_target));if let Some(ref p) = dest_path_str {// Existing destination: override bare vars to Active (destination)extra_vars.push(("target_worktree_path", p));diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rsindex 09ccf85b9..2aa3b48f2 100644--- a/tests/integration_tests/user_hooks.rs+++ b/tests/integration_tests/user_hooks.rs@@ -2550,6 +2550,78 @@ capture = "echo 'wt_path={{ worktree_path }} base={{ base }} base_wt={{ base_wor);}+/// Regression test for #2309: `wt switch -` should resolve the symbolic+/// argument before setting up pre-switch hook template variables, so+/// `{{ target }}`, `{{ target_worktree_path }}`, and the Active bare vars+/// (`{{ worktree_path }}`, `{{ worktree_name }}`) reflect the actual destination+/// instead of the raw `-` argument or the source worktree.+#[rstest]+fn test_pre_switch_vars_with_dash_shortcut(mut repo: TestRepo) {+ let feature_path = repo.add_worktree("feature");++ // Establish switch history: main -> feature. This records main as previous,+ // so `wt switch -` from feature resolves back to main.+ repo.wt_command()+ .args(["switch", "feature", "--yes"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();++ // Install the pre-switch hook after the history-building switch so the+ // capture reflects only the `-` switch we care about.+ repo.write_test_config(+ r#"[pre-switch]+capture = "echo 'target={{ target }} target_wt={{ target_worktree_path }} wt_path={{ worktree_path }} wt_name={{ worktree_name }}' > pre_switch_dash.txt"+"#,+ );++ let switch_output = repo+ .wt_command()+ .args(["switch", "-", "--yes"])+ .current_dir(&feature_path)+ .output()+ .unwrap();++ assert!(+ switch_output.status.success(),+ "`wt switch -` should succeed with a pre-switch hook referencing target_worktree_path.\n\+ stdout: {}\nstderr: {}",+ String::from_utf8_lossy(&switch_output.stdout),+ String::from_utf8_lossy(&switch_output.stderr),+ );++ let vars_file = feature_path.join("pre_switch_dash.txt");+ let content = fs::read_to_string(&vars_file).unwrap();++ let main_name = repo+ .root_path()+ .file_name()+ .unwrap()+ .to_string_lossy()+ .to_string();++ assert!(+ content.contains("target=main"),+ "{{{{ target }}}} should resolve to 'main' when using `-`, got: {content}"+ );+ // Both `target_worktree_path` and `worktree_path` should end at the main+ // worktree directory — the layout is `... /<main_name> <next-field>=`.+ assert!(+ content.contains(&format!("/{main_name} wt_path="))+ || content.contains(&format!(r"\{main_name} wt_path=")),+ "{{{{ target_worktree_path }}}} should point to the main worktree, got: {content}"+ );+ assert!(+ content.contains(&format!("/{main_name} wt_name="))+ || content.contains(&format!(r"\{main_name} wt_name=")),+ "{{{{ worktree_path }}}} should point to the main worktree (Active), got: {content}"+ );+ assert!(+ content.contains(&format!("wt_name={main_name}")),+ "{{{{ worktree_name }}}} should be the main worktree name, got: {content}"+ );+}+/// Post-remove: target/target_worktree_path point to where user ends up.#[rstest]fn test_post_remove_has_target_vars(mut repo: TestRepo) {