Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(switch): resolve a worktree path for pre-switch hook variables (#4493)
Worktrunk Botd14f217c6615
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rsindex 74ef2c2ef..9a2a4727a 100644--- a/src/commands/worktree/switch.rs+++ b/src/commands/worktree/switch.rs@@ -1588,6 +1588,7 @@ fn run_pre_switch_hooks(config: &UserConfig,target_branch: &str,ref_target: Option<&ResolvedTarget>,+ create: bool,yes: bool,) -> anyhow::Result<()> {let current_wt = repo.current_worktree();@@ -1596,14 +1597,32 @@ fn run_pre_switch_hooks(// pre-switch hook receives has to name the same branch the switch goes on// to resolve, normalization included. A `pr:`/`mr:` argument is already// past that point — the forge answered with the branch name itself.- let resolved_target = match ref_target {- Some(target) => target.selector.token().to_string(),+ let selector = match ref_target {+ Some(target) => Some(target.selector.clone()),+ // `--create` names a branch that doesn't exist yet, never a path —+ // the same reading `resolve_switch_target` gives it.None => repo.expand_selector(target_branch)- .map(|s| s.token().to_string())- .unwrap_or_else(|_| target_branch.to_string()),+ .ok()+ .map(|s| if create { s.branch_only() } else { s }),+ };+ // The same worktree ladder `plan_switch` runs, so a worktree named by its+ // path — which is how the picker names one — describes the destination+ // just as its branch name would (issue #4492). A detached destination has+ // no branch, which leaves `branch` and `target` unset.+ let (dest_branch, dest_path) = match selector.as_ref().map(|s| repo.resolve_selector(s)) {+ Some(Ok(ResolvedWorktree::Worktree { path, branch, .. })) => (branch, Some(path)),+ _ => (+ Some(+ selector+ .as_ref()+ .map_or(target_branch, |s| s.token())+ .to_string(),+ ),+ None,+ ),};- let pre_ctx = CommandContext::new(repo, config, Some(&resolved_target), ¤t_path, yes);+ let pre_ctx = CommandContext::new(repo, config, dest_branch.as_deref(), ¤t_path, yes);let pre_switch_approved = approve_hooks(&pre_ctx, &[HookType::PreSwitch])?;if pre_switch_approved {@@ -1615,18 +1634,25 @@ fn run_pre_switch_hooks(// matching `branch` and what `wt hook pre-switch` already renders// there (issue #4009).let base_branch = current_wt.branch().ok().flatten();- let dest_path = repo.worktree_for_branch(&resolved_target).ok().flatten();let ref_identity = ref_target.and_then(|t| t.ref_identity.as_ref());let mut vars = TemplateVars::new().with_base(base_branch.as_deref(), ¤t_path)- .with_target(&resolved_target)+ .with_target_opt(dest_branch.as_deref()).with_pr(ref_identity.map(|id| id.number),ref_identity.map(|id| id.url.as_str()),);if let Some(p) = dest_path.as_deref() {vars = vars.with_target_worktree_path(p).with_active_worktree(p);+ // Without a branch, `commit` would otherwise read HEAD where the+ // hook runs — the source worktree.+ if dest_branch.is_none()+ && let Ok(Some(commit)) = repo.worktree_at(p).head_sha()+ && let Ok(short) = repo.short_sha(&commit)+ {+ vars = vars.with_active_commit(&commit, &short);+ }}let extra_vars = vars.as_extra_vars();@@ -1884,7 +1910,7 @@ impl SwitchPipeline<'_> {// against. `yes` is the single switch-wide flag, so the picker (no// `--yes`) and the argument path gate `pre-switch` hooks identically.if verify && !is_recovered {- run_pre_switch_hooks(repo, config, identifier, ref_target.as_ref(), yes)?;+ run_pre_switch_hooks(repo, config, identifier, ref_target.as_ref(), create, yes)?;}// Capture source (base) worktree identity BEFORE the switch, fordiff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rsindex b64371e76..fa11162b4 100644--- a/tests/integration_tests/user_hooks.rs+++ b/tests/integration_tests/user_hooks.rs@@ -4016,6 +4016,75 @@ capture = "echo 'target={{ target }} target_wt={{ target_worktree_path }} wt_pat);}+/// Regression test for #4492: a worktree path selects the same worktree as its+/// branch name, so the pre-switch vars must describe that destination too.+/// The picker passes paths, so every picker switch takes this route.+#[rstest]+fn test_pre_switch_vars_with_path_argument(mut repo: TestRepo) {+ let feature_path = repo.add_worktree("feature");+ let feature_name = feature_path.file_name().unwrap().to_string_lossy();++ repo.write_test_config(+ r#"[pre-switch]+capture = "echo 'branch={{ branch }} target={{ target }} wt_name={{ worktree_name }} target_wt={% if target_worktree_path %}set{% endif %} commit={% if commit %}set{% endif %}' > pre_switch_path.txt"+"#,+ );++ let output = repo+ .wt_command()+ .args(["switch", feature_path.to_str().unwrap(), "--yes"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "stderr: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ let content = fs::read_to_string(repo.root_path().join("pre_switch_path.txt")).unwrap();+ assert_eq!(+ content.trim(),+ format!("branch=feature target=feature wt_name={feature_name} target_wt=set commit=set"),+ );+}++/// A detached worktree is reachable only by path. Its pre-switch vars leave+/// `branch`/`target` unset and take `commit` from the destination's HEAD, not+/// the source's.+#[rstest]+fn test_pre_switch_vars_with_detached_path_argument(mut repo: TestRepo) {+ let detached_path = repo.add_worktree("detached");+ repo.commit_in_worktree(&detached_path, "file.txt", "content", "detached commit");+ repo.detach_head_in_worktree("detached");+ let detached_sha = repo.head_sha_in(&detached_path);++ repo.write_test_config(+ r#"[pre-switch]+capture = "echo 'branch={% if branch is defined %}{{ branch }}{% else %}unset{% endif %} target={% if target is defined %}{{ target }}{% else %}unset{% endif %} commit={{ commit }}' > pre_switch_detached_path.txt"+"#,+ );++ let output = repo+ .wt_command()+ .args(["switch", detached_path.to_str().unwrap(), "--yes"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "stderr: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ let content =+ fs::read_to_string(repo.root_path().join("pre_switch_detached_path.txt")).unwrap();+ assert_eq!(+ content.trim(),+ format!("branch=unset target=unset commit={detached_sha}"),+ );+}+/// Post-remove: target/target_worktree_path point to where user ends up.#[rstest]fn test_post_remove_has_target_vars(mut repo: TestRepo) {