Skip to content

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

fix: show hook path when shell integration inactive (#385)

Maximilian Roosfb064e82907f
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 93495b93f..adb13598d 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -282,13 +282,16 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {
&destination_repo_root,
yes,
);
- // 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 {
+ // Show path when user's shell won't be in the destination directory where hooks run.
+ let display_path = if in_main || on_target {
+ // User is already in the destination directory
+ None
+ } else if !remove_effective {
+ // Worktree preserved — user's shell stays in feature worktree
Some(destination_path.as_path())
} else {
- None
+ // Worktree removed — show path only if shell integration won't cd there
+ crate::output::hooks_display_path(&destination_path)
};
execute_post_merge_commands(&ctx, &target_branch, None, display_path)?;
}
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index fa823e92b..671aba2a9 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -832,7 +832,13 @@ impl<'a> CommandContext<'a> {
}
/// Spawn post-start commands in parallel as background processes (non-blocking)
- pub fn spawn_post_start_commands(&self) -> anyhow::Result<()> {
+ ///
+ /// `display_path`: When `Some`, shows the path in hook announcements. Pass this when
+ /// the user's shell won't be in the worktree (shell integration not active).
+ pub fn spawn_post_start_commands(
+ &self,
+ display_path: Option<&std::path::Path>,
+ ) -> anyhow::Result<()> {
let project_config = self.repo.load_project_config()?;
let commands = prepare_hook_commands(
@@ -844,7 +850,7 @@ impl<'a> CommandContext<'a> {
HookType::PostStart,
&[],
None,
- None, // No path display - running in expected directory
+ display_path,
)?;
spawn_hook_commands_background(self, commands, HookType::PostStart)
diff --git a/src/main.rs b/src/main.rs
index 0a56085fd..b8c8f71cb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1279,7 +1279,7 @@ fn main() {
// Post-start runs only on creation (setup tasks)
if matches!(&result, SwitchResult::Created { .. }) {
- ctx.spawn_post_start_commands()?;
+ ctx.spawn_post_start_commands(hooks_display_path.as_deref())?;
}
}
diff --git a/src/output/global.rs b/src/output/global.rs
index ce7924ced..63486bb28 100644
--- a/src/output/global.rs
+++ b/src/output/global.rs
@@ -276,6 +276,20 @@ pub fn is_shell_integration_active() -> bool {
has_directive_file()
}
+/// Returns `Some(path)` when shell integration isn't active, `None` otherwise.
+///
+/// Use this to decide whether hook announcements should show "@ path".
+/// When shell integration is active, the user's shell will cd to the path automatically,
+/// so no annotation is needed. When inactive, showing the path helps users understand
+/// where hooks are running.
+pub fn hooks_display_path(path: &std::path::Path) -> Option<&std::path::Path> {
+ if is_shell_integration_active() {
+ None
+ } else {
+ Some(path)
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 4c7b4cc51..f52245c94 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -912,8 +912,8 @@ fn spawn_post_switch_after_remove(
&repo_root,
false, // force=false for CommandContext
);
- // No display_path needed - changed_directory is true, so user's shell IS going there
- ctx.spawn_post_switch_commands(None)
+ // Show path when shell integration isn't active — the cd directive won't take effect
+ ctx.spawn_post_switch_commands(super::hooks_display_path(main_path))
}
/// Handle output for RemovedWorktree removal
diff --git a/src/output/mod.rs b/src/output/mod.rs
index 4349594da..72bf0d6ce 100644
--- a/src/output/mod.rs
+++ b/src/output/mod.rs
@@ -31,8 +31,8 @@ pub mod handlers;
// Re-export the public API
pub use global::{
- blank, change_directory, execute, flush, is_shell_integration_active, print, stdout,
- terminate_output,
+ blank, change_directory, execute, flush, hooks_display_path, is_shell_integration_active,
+ print, stdout, terminate_output,
};
// Re-export output handlers
pub use handlers::{
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_failure.snap b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_failure.snap
index 61e001c1f..c6b040609 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_failure.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_failure.snap
@@ -24,6 +24,7 @@ info:
SOURCE_DATE_EPOCH: "1735776000"
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -38,7 +39,7 @@ exit_code: 1
✓ Merged to main (1 commit, 1 file, +1)
◎ Removing feature worktree & branch in background (same commit as main, _)
○ Switched to worktree for main @ _REPO_
-◎ Running post-merge project hook:
+◎ Running post-merge project hook @ _REPO_:
  exit 1
▲ Command failed: exit status: 1
✗ post-merge command failed: exit status: 1
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_named.snap b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_named.snap
index 8fabefd59..597b72765 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_named.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_named.snap
@@ -24,6 +24,7 @@ info:
SOURCE_DATE_EPOCH: "1735776000"
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
@@ -38,7 +39,7 @@ exit_code: 0
✓ Merged to main (1 commit, 1 file, +1)
◎ Removing feature worktree & branch in background (same commit as main, _)
○ Switched to worktree for main @ _REPO_
-◎ Running post-merge project:notify:
+◎ Running post-merge project:notify @ _REPO_:
  echo 'Merge to main complete' > notify.txt
-◎ Running post-merge project:deploy:
+◎ Running post-merge project:deploy @ _REPO_:
  echo 'Deploying branch feature' > deploy.txt
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_success.snap b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_success.snap
index a8f6470ad..c298b2136 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_success.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_command_success.snap
@@ -24,6 +24,7 @@ info:
SOURCE_DATE_EPOCH: "1735776000"
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
@@ -38,5 +39,5 @@ exit_code: 0
✓ Merged to main (1 commit, 1 file, +1)
◎ Removing feature worktree & branch in background (same commit as main, _)
○ Switched to worktree for main @ _REPO_
-◎ Running post-merge project hook:
+◎ Running post-merge project hook @ _REPO_:
  echo 'merged feature to main' > post-merge-ran.txt
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_with_nothing_to_merge.snap b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_with_nothing_to_merge.snap
index 8545548e1..ff8b05a39 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_with_nothing_to_merge.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_post_merge_runs_with_nothing_to_merge.snap
@@ -24,6 +24,7 @@ info:
SOURCE_DATE_EPOCH: "1735776000"
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
@@ -34,5 +35,5 @@ exit_code: 0
○ Already up to date with main (no new commits, no rebase needed)
◎ Removing feature worktree & branch in background (same commit as main, _)
○ Switched to worktree for main @ _REPO_
-◎ Running post-merge project hook:
+◎ Running post-merge project hook @ _REPO_:
  echo 'post-merge ran' > post-merge-ran.txt
diff --git a/tests/snapshots/integration__integration_tests__merge__readme_example_complex.snap b/tests/snapshots/integration__integration_tests__merge__readme_example_complex.snap
index f34538830..cc917544b 100644
--- a/tests/snapshots/integration__integration_tests__merge__readme_example_complex.snap
+++ b/tests/snapshots/integration__integration_tests__merge__readme_example_complex.snap
@@ -24,6 +24,7 @@ info:
SOURCE_DATE_EPOCH: "1735776000"
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
@@ -68,7 +69,7 @@ test result: ok. 18 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; fin
✓ Merged to main (1 commit, 3 files, +33)
◎ Removing feature-auth worktree & branch in background (same commit as main, _)
○ Switched to worktree for main @ _REPO_
-◎ Running post-merge project:install:
+◎ Running post-merge project:install @ _REPO_:
  cargo install --path .
 Installing worktrunk v0.1.0
Compiling worktrunk v0.1.0
diff --git a/tests/snapshots/integration__integration_tests__merge__readme_example_hooks_post_create.snap b/tests/snapshots/integration__integration_tests__merge__readme_example_hooks_post_create.snap
index d5ae62430..7d250c7b4 100644
--- a/tests/snapshots/integration__integration_tests__merge__readme_example_hooks_post_create.snap
+++ b/tests/snapshots/integration__integration_tests__merge__readme_example_hooks_post_create.snap
@@ -41,5 +41,5 @@ exit_code: 0
✓ Created new worktree for feature-x from main @ _REPO_.feature-x
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project:dev:
+◎ Running post-start project:dev @ _REPO_.feature-x:
  uv run dev
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__both_create_and_start.snap b/tests/snapshots/integration__integration_tests__post_start_commands__both_create_and_start.snap
index c3a89cdc9..fb0ab5650 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__both_create_and_start.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__both_create_and_start.snap
@@ -37,5 +37,5 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project:server:
+◎ Running post-start project:server @ _REPO_.feature:
  sleep 0.05 && echo 'Server running' > server.txt
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__execute_with_post_start.snap b/tests/snapshots/integration__integration_tests__post_start_commands__execute_with_post_start.snap
index 1be1a9572..680f90014 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__execute_with_post_start.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__execute_with_post_start.snap
@@ -37,7 +37,7 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Executing echo 'Execute flag' > execute.txt @ _REPO_.feature, but shell directory unchanged — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  echo 'Background task' > background.txt
◎ Executing (--execute):
  echo 'Execute flag' > execute.txt
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_complex_shell.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_complex_shell.snap
index caf05bd94..464bb9e26 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_complex_shell.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_complex_shell.snap
@@ -35,7 +35,7 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  echo 'line1
  line2
  line3' | grep line2 > filtered.txt
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_create_with_command.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_create_with_command.snap
index 45592413b..85ee6e7d8 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_create_with_command.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_create_with_command.snap
@@ -35,5 +35,5 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  echo 'POST-START-RAN' > post_start_marker.txt
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_invalid_command.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_invalid_command.snap
index d55c600c8..7fafd468a 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_invalid_command.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_invalid_command.snap
@@ -35,5 +35,5 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  echo 'unclosed quote
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_log_captures_output.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_log_captures_output.snap
index 2814f01d4..a9592e693 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_log_captures_output.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_log_captures_output.snap
@@ -35,5 +35,5 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  echo 'stdout output' && echo 'stderr output' >&2
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiline_with_newlines.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiline_with_newlines.snap
index 436f1b6bb..aa8c31795 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiline_with_newlines.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiline_with_newlines.snap
@@ -35,7 +35,7 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  echo 'first line' > multiline.txt
  echo 'second line' >> multiline.txt
  echo 'third line' >> multiline.txt
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiple_background.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiple_background.snap
index f6b99e916..3a360345a 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiple_background.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_multiple_background.snap
@@ -35,7 +35,7 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project:task1:
+◎ Running post-start project:task1 @ _REPO_.feature:
  echo 'Task 1 running' > task1.txt
-◎ Running post-start project:task2:
+◎ Running post-start project:task2 @ _REPO_.feature:
  echo 'Task 2 running' > task2.txt
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_separate_logs.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_separate_logs.snap
index 7b1388b31..554891b14 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_separate_logs.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_separate_logs.snap
@@ -35,9 +35,9 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project:task1:
+◎ Running post-start project:task1 @ _REPO_.feature:
  echo 'TASK1_OUTPUT'
-◎ Running post-start project:task2:
+◎ Running post-start project:task2 @ _REPO_.feature:
  echo 'TASK2_OUTPUT'
-◎ Running post-start project:task3:
+◎ Running post-start project:task3 @ _REPO_.feature:
  echo 'TASK3_OUTPUT'
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_single_background.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_single_background.snap
index 21d9fb81c..f5ace790b 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_single_background.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_single_background.snap
@@ -35,5 +35,5 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  sleep 0.1 && echo 'Background task done' > background.txt
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_and_project_post_start.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_and_project_post_start.snap
index a556b50f6..afbd949ec 100644
--- a/tests/snapshots/integration__integration_tests__user_hooks__user_and_project_post_start.snap
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_and_project_post_start.snap
@@ -35,7 +35,7 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start user:bg:
+◎ Running post-start user:bg @ _REPO_.feature:
  echo 'USER_POST_START' > user_bg.txt
-◎ Running post-start project hook:
+◎ Running post-start project hook @ _REPO_.feature:
  echo 'PROJECT_POST_START' > project_bg.txt
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_executes.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_executes.snap
index 84055507e..7df259b4f 100644
--- a/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_executes.snap
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_executes.snap
@@ -35,5 +35,5 @@ exit_code: 0
✓ Created new worktree for feature from main @ _REPO_.feature
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-◎ Running post-start user:bg:
+◎ Running post-start user:bg @ _REPO_.feature:
  echo 'USER_POST_START_RAN' > user_bg_marker.txt