Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Preserve child process exit codes
Maximilian Roos0222c9499044
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 75eaea5bd..bcf10bd87 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -511,10 +511,16 @@ pub fn run_pre_merge_commands(crate::output::progress(format_bash_with_gutter(&prepared.expanded, ""))?;if let Err(e) = execute_command_in_worktree(worktree_path, &prepared.expanded) {+ // Extract exit code from ChildProcessExited to preserve signal information+ let exit_code = match &e {+ GitError::ChildProcessExited { code, .. } => Some(*code),+ _ => None,+ };return Err(GitError::HookCommandFailed {hook_type: HookType::PreMerge,command_name: prepared.name.clone(),error: e.to_string(),+ exit_code,});}@@ -637,10 +643,16 @@ pub fn run_pre_commit_commands(crate::output::progress(format_bash_with_gutter(&prepared.expanded, ""))?;if let Err(e) = execute_command_in_worktree(worktree_path, &prepared.expanded) {+ // Extract exit code from ChildProcessExited to preserve signal information+ let exit_code = match &e {+ GitError::ChildProcessExited { code, .. } => Some(*code),+ _ => None,+ };return Err(GitError::HookCommandFailed {hook_type: HookType::PreCommit,command_name: prepared.name.clone(),error: e.to_string(),+ exit_code,});}}diff --git a/src/git/error.rs b/src/git/error.rsindex 43b9260c3..38fb7e704 100644--- a/src/git/error.rs+++ b/src/git/error.rs@@ -19,6 +19,7 @@ pub enum GitError {hook_type: HookType,command_name: Option<String>,error: String,+ exit_code: Option<i32>,},/// Working tree has uncommitted changesUncommittedChanges,@@ -43,6 +44,8 @@ pub enum GitError {MergeCommitsFound,/// Command was not approved by userCommandNotApproved,+ /// Child process exited with non-zero code (preserves exit code for signals)+ ChildProcessExited { code: i32, message: String },/// Push operation failedPushFailed { error: String },/// Rebase resulted in a conflict or incomplete state@@ -87,6 +90,7 @@ impl std::fmt::Display for GitError {hook_type,command_name,error,+ exit_code: _,} => {let error_bold = ERROR.bold();match command_name {@@ -202,6 +206,12 @@ impl std::fmt::Display for GitError {Ok(()) // on_skip callback handles the printing}+ // Child process exited with non-zero code+ // Just display the message - main.rs will use the exit code+ GitError::ChildProcessExited { code: _, message } => {+ write!(f, "{}", message)+ }+// Push failedGitError::PushFailed { error } => {write!(f, "{ERROR_EMOJI} {ERROR}Push failed: {error}{ERROR:#}")@@ -242,8 +252,23 @@ impl std::error::Error for GitError {}// Automatic conversion from io::Error to GitError// This eliminates the need for manual .map_err() on output functions+// Parses exit codes from error messages to preserve signal information+//+// Protocol: execute_streaming() embeds exit codes in error messages as:+// "CHILD_EXIT_CODE:{code} {original_message}"+// This allows passing exit codes through io::Error (which doesn't carry codes)+// while preserving the full error context.impl From<std::io::Error> for GitError {fn from(e: std::io::Error) -> Self {- GitError::CommandFailed(e.to_string())+ let msg = e.to_string();+ // Parse exit code from error message (format: "CHILD_EXIT_CODE:130 Command failed...")+ if let Some(rest) = msg.strip_prefix("CHILD_EXIT_CODE:")+ && let Some(space_idx) = rest.find(' ')+ && let Ok(code) = rest[..space_idx].parse::<i32>()+ {+ let message = rest[space_idx + 1..].to_string();+ return GitError::ChildProcessExited { code, message };+ }+ GitError::CommandFailed(msg)}}diff --git a/src/main.rs b/src/main.rsindex 5914c1f22..4184d8f24 100644--- a/src/main.rs+++ b/src/main.rs@@ -720,6 +720,13 @@ fn main() {if let Err(e) = result {// Error messages are already formatted with emoji and colorseprintln!("{}", e);- process::exit(1);++ // Preserve exit code from child processes (especially for signals like SIGINT)+ let exit_code = match &e {+ GitError::ChildProcessExited { code, .. } => *code,+ GitError::HookCommandFailed { exit_code, .. } => exit_code.unwrap_or(1),+ _ => 1,+ };+ process::exit(exit_code);}}diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex 2043f65f1..9437110d3 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -208,9 +208,11 @@ pub(crate) fn execute_streaming(.map_err(|e| io::Error::other(format!("Failed to wait for command: {}", e)))?;if !status.success() {+ // Get the exit code if available (None means terminated by signal on some platforms)+ let code = status.code().unwrap_or(1);return Err(io::Error::other(format!(- "Command failed with exit code: {}",- status+ "CHILD_EXIT_CODE:{} Command failed with exit code: {}",+ code, status)));}@@ -257,9 +259,8 @@ pub fn execute_command_in_worktree(stderr().flush().ok(); // Ignore flush errors - reset is best-effort, command execution should proceed// Execute with stdout→stderr redirect for deterministic ordering- // Convert io::Error to GitError::CommandFailed to preserve error message formatting- execute_streaming(command, worktree_path, true)- .map_err(|e| GitError::CommandFailed(e.to_string()))?;+ // io::Error is automatically converted to GitError, parsing exit codes via From impl+ execute_streaming(command, worktree_path, true)?;// Flush to ensure all output appears before we continuesuper::flush()?;