Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat!: point bare template vars to Active worktree identity (#1660)
Maximilian Roos3845969caf79
diff --git a/src/commands/handle_switch.rs b/src/commands/handle_switch.rsindex f38adc4b7..d3047ca21 100644--- a/src/commands/handle_switch.rs+++ b/src/commands/handle_switch.rs@@ -63,13 +63,30 @@ pub(crate) fn run_pre_switch_hooks(("base_worktree_path", &base_path_str),];- // Target vars: destination (if the worktree already exists)+ // 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_name = dest_path+ .as_ref()+ .and_then(|p| p.file_name())+ .and_then(|n| n.to_str())+ .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));if let Some(ref p) = dest_path_str {- extra_vars.push(("target", target_branch));+ // Existing destination: override bare vars to Active (destination)extra_vars.push(("target_worktree_path", p));+ extra_vars.push(("worktree_path", p));+ extra_vars.push(("worktree", p)); // deprecated alias+ if let Some(ref name) = dest_name {+ extra_vars.push(("worktree_name", name));+ }}+ // For creates (dest_path_str is None): worktree_path keeps its default+ // (the source worktree = cwd). The planned destination path is computed+ // later during plan_switch, after pre-switch hooks complete.execute_hook(&pre_ctx,diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 3878bf262..5906e3651 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -258,6 +258,26 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {None => repo.home_path()?,};+ // Capture feature worktree identity BEFORE removal for post-merge template vars.+ // After removal the feature worktree is gone, but post-merge hooks need to+ // reference it as the Active identity (branch, worktree_path, commit).+ let feature_path_str = worktrunk::path::to_posix_path(&env.worktree_path.to_string_lossy());+ let feature_name = env+ .worktree_path+ .file_name()+ .and_then(|n| n.to_str())+ .unwrap_or("unknown")+ .to_string();+ let feature_commit = repo+ .current_worktree()+ .run_command(&["rev-parse", "HEAD"])+ .ok()+ .map(|s| s.trim().to_string());+ let feature_short_commit = feature_commit+ .as_ref()+ .filter(|c| c.len() >= 7)+ .map(|c| c[..7].to_string());+// Finish worktree unless removal is disabled or blocked.// Guards are shared with `wt remove`: is_primary_worktree (Phase 2) and// check_not_default_branch (Phase 3) are the same helpers both paths use.@@ -288,10 +308,6 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {BranchDeletionMode::SafeDelete,);let expected_path = path_mismatch(repo, ¤t_branch, &worktree_root, config);- let removed_commit = current_wt- .run_command(&["rev-parse", "HEAD"])- .ok()- .map(|s| s.trim().to_string());let remove_result = RemoveResult::RemovedWorktree {main_path: destination_path.clone(),@@ -303,29 +319,39 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {integration_reason,force_worktree: false,expected_path,- removed_commit,+ removed_commit: feature_commit.clone(),};crate::output::handle_remove_output(&remove_result, false, verify, false)?;true};if verify {- // Execute post-merge commands in the destination worktree- // This runs after cleanup so the context is clear to the user+ // Post-merge hooks run in the destination worktree (target), but bare vars+ // point to the Active (feature branch) per the template variable model.+ // The destination worktree is the execution context (cwd).let ctx = CommandContext::new(repo, config, Some(¤t_branch), &destination_path, yes);- // Show path when user's shell won't be in the destination directory where hooks run.let display_path = if removed {- // Worktree removed, user will cd to destinationcrate::output::post_hook_display_path(&destination_path)} else {- // No cd happens — user stays at cwd (either already at destination,- // or worktree preserved so they stay in feature)crate::output::pre_hook_display_path(&destination_path)};++ // Override bare vars to Active (feature branch identity)let mut extra: Vec<(&str, &str)> = vec![("target", target_branch.as_str())];if let Some(ref p) = target_wt_path_str {extra.push(("target_worktree_path", p));}+ // Active = feature: override worktree_path and friends+ extra.push(("worktree_path", &feature_path_str));+ extra.push(("worktree", &feature_path_str)); // deprecated alias+ extra.push(("worktree_name", &feature_name));+ if let Some(ref c) = feature_commit {+ extra.push(("commit", c));+ }+ if let Some(ref sc) = feature_short_commit {+ extra.push(("short_commit", sc));+ }+execute_hook(&ctx,HookType::PostMerge,