Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Show hook execution path when running in different directory (#333)
Maximilian Roos75e899b08806
diff --git a/src/commands/commit.rs b/src/commands/commit.rsindex abd7830df..d072bbf6c 100644--- a/src/commands/commit.rs+++ b/src/commands/commit.rs@@ -176,7 +176,8 @@ impl CommitOptions<'_> {HookType::PreCommit,&extra_vars,HookFailureStrategy::FailFast,- None,+ None, // name_filter: run all hooks+ None, // display_path: running in expected directory).map_err(worktrunk::git::add_hook_skip_hint)?;}diff --git a/src/commands/hooks.rs b/src/commands/hooks.rsindex 40581928e..7be21bfce 100644--- a/src/commands/hooks.rs+++ b/src/commands/hooks.rs@@ -1,7 +1,10 @@+use std::path::{Path, PathBuf};+use color_print::cformat;use worktrunk::HookType;use worktrunk::config::CommandConfig;use worktrunk::git::WorktrunkError;+use worktrunk::path::format_path_for_display;use worktrunk::styling::{format_bash_with_gutter, progress_message, warning_message};use super::command_executor::{CommandContext, PreparedCommand, prepare_commands};@@ -13,12 +16,16 @@ pub struct SourcedCommand {pub prepared: PreparedCommand,pub source: HookSource,pub hook_type: HookType,+ /// Path to display in announcement, if different from user's current directory.+ /// When `Some`, shows "@ path" suffix to clarify where the command runs.+ pub display_path: Option<PathBuf>,}impl SourcedCommand {/// Announce this command before execution.////// Format: "Running pre-merge user:foo:" for named, "Running post-create user hook:" for unnamed+ /// When display_path is set, appends "@ path" to show where the command runs.fn announce(&self) -> anyhow::Result<()> {// Named: "user:foo" matches filter syntax// Unnamed: "user hook" is clearer than just "user:"@@ -28,7 +35,14 @@ impl SourcedCommand {};let full_label =crate::commands::format_command_label(&self.hook_type.to_string(), Some(&display_name));- crate::output::print(progress_message(format!("{full_label}:")))?;+ let message = match &self.display_path {+ Some(path) => {+ let path_display = format_path_for_display(path);+ cformat!("{full_label} @ <bold>{path_display}</>:")+ }+ None => format!("{full_label}:"),+ };+ crate::output::print(progress_message(message))?;crate::output::gutter(format_bash_with_gutter(&self.prepared.expanded))?;Ok(())}@@ -100,6 +114,10 @@ impl<'a> ParsedFilter<'a> {/// Collects commands from user config first, then project config, applying the name filter./// The filter supports source prefixes: `user:foo` or `project:foo` to run only from one source./// Returns a flat list of commands with source information for execution.+///+/// `display_path`: When `Some`, the path is shown in hook announcements (e.g., "@ ~/repo").+/// Use this when commands run in a different directory than where the user invoked the command.+#[allow(clippy::too_many_arguments)]pub fn prepare_hook_commands(ctx: &CommandContext,user_config: Option<&CommandConfig>,@@ -107,10 +125,13 @@ pub fn prepare_hook_commands(hook_type: HookType,extra_vars: &[(&str, &str)],name_filter: Option<&str>,+ display_path: Option<&Path>,) -> anyhow::Result<Vec<SourcedCommand>> {let parsed_filter = name_filter.map(ParsedFilter::parse);let mut commands = Vec::new();+ let display_path = display_path.map(|p| p.to_path_buf());+if let Some(config) = user_config {// Skip user commands if filter specifies project sourceif parsed_filter@@ -123,6 +144,7 @@ pub fn prepare_hook_commands(prepared: p,source: HookSource::User,hook_type,+ display_path: display_path.clone(),}));}}@@ -139,6 +161,7 @@ pub fn prepare_hook_commands(prepared: p,source: HookSource::Project,hook_type,+ display_path: display_path.clone(),}));}}@@ -264,6 +287,10 @@ pub(crate) fn check_name_filter_matched(/// This is the canonical implementation for running hooks from both sources./// Runs user hooks first, then project hooks sequentially. Handles name filtering/// and returns an error if a name filter was provided but no matching command found.+///+/// `display_path`: When `Some`, shows the path in hook announcements. Use when hooks+/// run in a different directory than where the user invoked the command.+#[allow(clippy::too_many_arguments)]pub fn run_hook_with_filter(ctx: &CommandContext,user_config: Option<&CommandConfig>,@@ -272,6 +299,7 @@ pub fn run_hook_with_filter(extra_vars: &[(&str, &str)],failure_strategy: HookFailureStrategy,name_filter: Option<&str>,+ display_path: Option<&Path>,) -> anyhow::Result<()> {let commands = prepare_hook_commands(ctx,@@ -280,6 +308,7 @@ pub fn run_hook_with_filter(hook_type,extra_vars,name_filter,+ display_path,)?;check_name_filter_matched(name_filter, commands.len(), user_config, project_config)?;diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 60b0f054a..93495b93f 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -1,3 +1,5 @@+use std::path::Path;+use worktrunk::HookType;use worktrunk::config::ProjectConfig;use worktrunk::git::Repository;@@ -280,7 +282,15 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {&destination_repo_root,yes,);- execute_post_merge_commands(&ctx, &target_branch, None)?;+ // Show path when user's shell stays in a different directory than where hooks run.+ // This happens only with --no-remove on a feature branch (not in_main, not on_target).+ // When in_main or on_target, user is already in the destination directory.+ let display_path = if !remove_effective && !in_main && !on_target {+ Some(destination_path.as_path())+ } else {+ None+ };+ execute_post_merge_commands(&ctx, &target_branch, None, display_path)?;}Ok(())@@ -316,18 +326,23 @@ pub fn run_pre_merge_commands(&[("target", target_branch)],HookFailureStrategy::FailFast,name_filter,+ None, // No path display - running in expected directory).map_err(worktrunk::git::add_hook_skip_hint)}-/// Execute post-merge commands sequentially in the main worktree (blocking)+/// Execute post-merge commands sequentially in the target worktree (blocking)////// Runs user hooks first, then project hooks./// Approval is handled at the gate (command entry point).+///+/// `display_path`: When `Some`, shows the path in hook announcements. Pass this when+/// the worktree is preserved and the user's shell remains in a different directory.pub fn execute_post_merge_commands(ctx: &CommandContext,target_branch: &str,name_filter: Option<&str>,+ display_path: Option<&Path>,) -> anyhow::Result<()> {// Load project config from the main worktree pathlet project_config = ctx.repo.load_project_config()?;@@ -342,6 +357,7 @@ pub fn execute_post_merge_commands(&[("target", target_branch)],HookFailureStrategy::Warn,name_filter,+ display_path,).map_err(worktrunk::git::add_hook_skip_hint)}@@ -367,6 +383,7 @@ pub fn execute_pre_remove_commands(&[],HookFailureStrategy::FailFast,name_filter,+ None, // No path display - running in expected directory).map_err(worktrunk::git::add_hook_skip_hint)}diff --git a/src/commands/standalone.rs b/src/commands/standalone.rsindex e8f7a2e1d..5414a7b0f 100644--- a/src/commands/standalone.rs+++ b/src/commands/standalone.rs@@ -90,6 +90,7 @@ pub fn run_hook(hook_type: HookType, yes: bool, name_filter: Option<&str>) -> an&[],HookFailureStrategy::FailFast,name_filter,+ None,)}HookType::PostStart => {@@ -106,6 +107,7 @@ pub fn run_hook(hook_type: HookType, yes: bool, name_filter: Option<&str>) -> anhook_type,&[],name_filter,+ None,)?;check_name_filter_matched(name_filter, commands.len(), user_config, project_config)?;spawn_hook_commands_background(&ctx, commands, hook_type)@@ -124,6 +126,7 @@ pub fn run_hook(hook_type: HookType, yes: bool, name_filter: Option<&str>) -> anhook_type,&[],name_filter,+ None,)?;check_name_filter_matched(name_filter, commands.len(), user_config, project_config)?;spawn_hook_commands_background(&ctx, commands, hook_type)@@ -149,6 +152,7 @@ pub fn run_hook(hook_type: HookType, yes: bool, name_filter: Option<&str>) -> an&extra_vars,HookFailureStrategy::FailFast,name_filter,+ None,)}HookType::PreMerge => {@@ -160,7 +164,8 @@ pub fn run_hook(hook_type: HookType, yes: bool, name_filter: Option<&str>) -> an}HookType::PostMerge => {// Use current branch as target (matches approval prompt for wt hook)- execute_post_merge_commands(&ctx, ctx.branch_or_head(), name_filter)+ // No display_path - user is already in the directory where hooks run+ execute_post_merge_commands(&ctx, ctx.branch_or_head(), name_filter, None)}HookType::PreRemove => execute_pre_remove_commands(&ctx, name_filter),}@@ -292,6 +297,7 @@ pub fn handle_squash(&extra_vars,HookFailureStrategy::FailFast,None,+ None,).map_err(worktrunk::git::add_hook_skip_hint)?;}diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex be2063db1..ce2a00b43 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -827,6 +827,7 @@ impl<'a> CommandContext<'a> {&[],HookFailureStrategy::Warn,None,+ None, // No path display - running in expected directory)}@@ -843,6 +844,7 @@ impl<'a> CommandContext<'a> {HookType::PostStart,&[],None,+ None, // No path display - running in expected directory)?;spawn_hook_commands_background(self, commands, HookType::PostStart)@@ -863,6 +865,7 @@ impl<'a> CommandContext<'a> {HookType::PostSwitch,&[],None,+ None, // No path display - running in expected directory)?;spawn_hook_commands_background(self, commands, HookType::PostSwitch)diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_post_merge_executes.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_merge_executes.snapindex f79f39d9c..e3ca74a54 100644--- a/tests/snapshots/integration__integration_tests__user_hooks__user_post_merge_executes.snap+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_merge_executes.snap@@ -38,5 +38,5 @@ exit_code: 0[107m [0m 1 file changed, 1 insertion(+)[32m✓[39m [32mMerged to [1mmain[22m [90m(1 commit, 1 file, [32m+1[39m[39m[90m)[39m[39m[2m○[22m Worktree preserved (--no-remove)-[36m◎[39m [36mRunning post-merge [1muser:notify[22m:[39m+[36m◎[39m [36mRunning post-merge [1muser:notify[22m @ [1m_REPO_[22m:[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'USER_POST_MERGE_RAN'[0m[2m [0m[2m[36m>[0m[2m user_postmerge.txt