Skip to content

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

Defer --execute command until after hooks spawn

Maximilian Roos33b48d5ecca5
diff --git a/src/main.rs b/src/main.rs
index 090de8e30..890bb4387 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -17,7 +17,7 @@ use commands::{
handle_dev_commit, handle_dev_push, handle_dev_rebase, handle_dev_run_hook, handle_dev_squash,
handle_init, handle_list, handle_merge, handle_remove, handle_switch,
};
-use output::{handle_remove_output, handle_switch_output};
+use output::{execute_user_command, handle_remove_output, handle_switch_output};
#[derive(Debug, Clone, Copy, clap::ValueEnum)]
pub enum OutputFormat {
@@ -454,7 +454,7 @@ fn main() {
handle_switch(&branch, create, base.as_deref(), force, no_verify, &config)?;
// Show success message (temporal locality: immediately after worktree creation)
- handle_switch_output(&result, &branch, execute.as_deref())?;
+ handle_switch_output(&result, &branch, execute.is_some())?;
// Now spawn post-start hooks (background processes, after success message)
if !no_verify {
@@ -468,6 +468,11 @@ fn main() {
)?;
}
+ // Execute user command after post-start hooks have been spawned
+ if let Some(cmd) = execute {
+ execute_user_command(&cmd)?;
+ }
+
Ok(())
}),
Commands::Remove { worktrees } => {
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 286004d2d..e913c723a 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -66,26 +66,17 @@ fn shell_integration_hint() -> &'static str {
pub fn handle_switch_output(
result: &SwitchResult,
branch: &str,
- execute: Option<&str>,
+ has_execute_command: bool,
) -> Result<(), GitError> {
- use worktrunk::styling::{CYAN, format_bash_with_gutter};
-
// Set target directory for command execution
super::change_directory(result.path())?;
// Show success message (includes emoji and color)
super::success(format_switch_message(result, branch))?;
- // Execute command if provided
- if let Some(cmd) = execute {
- // Show what command is being executed (matches post-create/post-start format)
- super::progress(format!("πŸ”„ {CYAN}Executing (--execute):{CYAN:#}"))?;
- super::progress(format_bash_with_gutter(cmd, ""))?;
-
- super::execute(cmd)?;
- } else {
- // No execute command: show shell integration hint
- // (suppressed in directive mode since user already has integration)
+ // If no execute command provided: show shell integration hint
+ // (suppressed in directive mode since user already has integration)
+ if !has_execute_command {
super::hint(format!("\n{}", shell_integration_hint()))?;
}
@@ -95,6 +86,19 @@ pub fn handle_switch_output(
Ok(())
}
+/// Execute the --execute command after hooks have run
+pub fn execute_user_command(command: &str) -> Result<(), GitError> {
+ use worktrunk::styling::{CYAN, format_bash_with_gutter};
+
+ // Show what command is being executed (matches post-create/post-start format)
+ super::progress(format!("πŸ”„ {CYAN}Executing (--execute):{CYAN:#}"))?;
+ super::progress(format_bash_with_gutter(command, ""))?;
+
+ super::execute(command)?;
+
+ Ok(())
+}
+
/// Handle output for a remove operation
pub fn handle_remove_output(result: &RemoveResult, branch: Option<&str>) -> Result<(), GitError> {
// For removed worktree: emit cd directive BEFORE deletion so shell changes directory instantly
diff --git a/src/output/mod.rs b/src/output/mod.rs
index 876f1de58..597d4344f 100644
--- a/src/output/mod.rs
+++ b/src/output/mod.rs
@@ -68,7 +68,9 @@ pub use global::{
};
// Re-export output handlers
-pub use handlers::{execute_command_in_worktree, handle_remove_output, handle_switch_output};
+pub use handlers::{
+ execute_command_in_worktree, execute_user_command, handle_remove_output, handle_switch_output,
+};
use std::path::Path;
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 dc0797ef6..77e8b6bbd 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
@@ -19,17 +19,17 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpcYBzHx/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpN4uLuk/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
βœ… Created new worktree for feature at [REPO].feature
-πŸ”„ Executing (--execute):
-  echo 'Execute flag' > execute.txt
-
πŸ”„ Running post-start:
  echo 'Background task' > background.txt
+πŸ”„ Executing (--execute):
+  echo 'Execute flag' > execute.txt
+
----- stderr -----