Skip to content

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

feat: log command responses and all spawned processes in verbose mode (#680)

Maximilian Roosc8ce65b47ab4
diff --git a/src/commands/for_each.rs b/src/commands/for_each.rs
index b35978447..8bbb9724b 100644
--- a/src/commands/for_each.rs
+++ b/src/commands/for_each.rs
@@ -170,6 +170,8 @@ fn run_command_streaming(
) -> Result<(), CommandError> {
let shell = ShellConfig::get();
+ log::debug!("$ {} (streaming)", command);
+
let stdin_mode = if stdin_content.is_some() {
Stdio::piped()
} else {
diff --git a/src/commands/process.rs b/src/commands/process.rs
index d16032aa4..b772984fc 100644
--- a/src/commands/process.rs
+++ b/src/commands/process.rs
@@ -66,14 +66,21 @@ pub fn spawn_detached(
)
})?;
+ log::debug!(
+ "$ {} (detached, logging to {}-{}.log)",
+ command,
+ safe_branch,
+ safe_name
+ );
+
#[cfg(unix)]
{
- spawn_detached_unix(worktree_path, command, log_file, context_json, name)?;
+ spawn_detached_unix(worktree_path, command, log_file, context_json)?;
}
#[cfg(windows)]
{
- spawn_detached_windows(worktree_path, command, log_file, context_json, name)?;
+ spawn_detached_windows(worktree_path, command, log_file, context_json)?;
}
Ok(log_path)
@@ -85,7 +92,6 @@ fn spawn_detached_unix(
command: &str,
log_file: fs::File,
context_json: Option<&str>,
- name: &str,
) -> anyhow::Result<()> {
use std::os::unix::process::CommandExt;
@@ -107,10 +113,6 @@ fn spawn_detached_unix(
let shell_cmd = format!("{} &", full_command);
- // Log only the operation identifier, not the full command (which may contain context_json
- // with user data that shouldn't appear in debug logs)
- log::debug!("spawn_detached: {} in {}", name, worktree_path.display());
-
// Detachment via process_group(0): puts the spawned shell in its own process group.
// When the controlling PTY closes, SIGHUP is sent to the foreground process group.
// Since our process is in a different group, it doesn't receive the signal.
@@ -145,15 +147,10 @@ fn spawn_detached_windows(
command: &str,
log_file: fs::File,
context_json: Option<&str>,
- name: &str,
) -> anyhow::Result<()> {
use std::os::windows::process::CommandExt;
use worktrunk::shell_exec::ShellConfig;
- // Log only the operation identifier, not the full command (which may contain context_json
- // with user data that shouldn't appear in debug logs)
- log::debug!("spawn_detached: {} in {}", name, worktree_path.display());
-
// CREATE_NEW_PROCESS_GROUP: Creates new process group (0x00000200)
// DETACHED_PROCESS: Creates process without console (0x00000008)
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index af96b9ee6..25c6ed264 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -460,10 +460,6 @@ impl Repository {
// Normalize carriage returns to newlines for consistent output
// Git uses \r for progress updates; in non-TTY contexts this causes snapshot instability
let stderr = stderr.replace('\r', "\n");
- // Log errors with ! prefix
- for line in stderr.trim().lines() {
- log::debug!(" ! {}", line);
- }
// Some git commands print errors to stdout (e.g., `commit` with nothing to commit)
let stdout = String::from_utf8_lossy(&output.stdout);
let error_msg = [stderr.trim(), stdout.trim()]
@@ -475,12 +471,6 @@ impl Repository {
}
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
- if !stdout.is_empty() {
- // Log output indented
- for line in stdout.trim().lines() {
- log::debug!(" {}", line);
- }
- }
Ok(stdout)
}
diff --git a/src/git/repository/working_tree.rs b/src/git/repository/working_tree.rs
index ec4eab179..16b75d47d 100644
--- a/src/git/repository/working_tree.rs
+++ b/src/git/repository/working_tree.rs
@@ -64,9 +64,6 @@ impl<'a> WorkingTree<'a> {
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stderr = stderr.replace('\r', "\n");
- for line in stderr.trim().lines() {
- log::debug!(" ! {}", line);
- }
let stdout = String::from_utf8_lossy(&output.stdout);
let error_msg = [stderr.trim(), stdout.trim()]
.into_iter()
@@ -77,11 +74,6 @@ impl<'a> WorkingTree<'a> {
}
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
- if !stdout.is_empty() {
- for line in stdout.trim().lines() {
- log::debug!(" {}", line);
- }
- }
Ok(stdout)
}
diff --git a/src/help_pager.rs b/src/help_pager.rs
index f1c9adbec..8ffbe0497 100644
--- a/src/help_pager.rs
+++ b/src/help_pager.rs
@@ -117,6 +117,7 @@ pub(crate) fn show_help_in_pager(help_text: &str, use_pager: bool) -> std::io::R
// Spawn pager with TTY access (interactive, unlike detached diff renderer)
// Falls back to direct output if pager unavailable (e.g., less not installed)
let shell = ShellConfig::get();
+ log::debug!("$ {} (pager)", pager_cmd);
let mut cmd = shell.command(&final_cmd);
// Prevent subprocesses from writing to the directive file
cmd.env_remove(worktrunk::shell_exec::DIRECTIVE_FILE_ENV_VAR);
diff --git a/src/shell.rs b/src/shell.rs
index b811f6db0..e80d99d9a 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -750,6 +750,8 @@ pub fn detect_zsh_compinit() -> Option<bool> {
let probe_cmd =
r#"(( $+functions[compdef] )) && echo __WT_COMPINIT_YES__ || echo __WT_COMPINIT_NO__"#;
+ log::debug!("$ zsh -ic '{}' (probe)", probe_cmd);
+
let mut child = Command::new("zsh")
.arg("-ic")
.arg(probe_cmd)
diff --git a/src/shell_exec.rs b/src/shell_exec.rs
index 3836b2792..d9258fb01 100644
--- a/src/shell_exec.rs
+++ b/src/shell_exec.rs
@@ -232,6 +232,19 @@ fn thread_id_number() -> u64 {
.unwrap_or(0)
}
+/// Log command output (stdout/stderr) for debugging.
+fn log_output(output: &std::process::Output) {
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let stderr = String::from_utf8_lossy(&output.stderr);
+
+ for line in stdout.lines() {
+ log::debug!(" {}", line);
+ }
+ for line in stderr.lines() {
+ log::debug!(" ! {}", line);
+ }
+}
+
/// Implementation of timeout-based command execution.
///
/// Spawns the process, captures stdout/stderr in background threads, and waits with timeout.
@@ -602,6 +615,7 @@ impl Cmd {
dur_us,
output.status.success()
);
+ log_output(output);
}
(Ok(output), None) => {
log::debug!(
@@ -612,6 +626,7 @@ impl Cmd {
dur_us,
output.status.success()
);
+ log_output(output);
}
(Err(e), Some(ctx)) => {
log::debug!(