Skip to content

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

feat: Improve display of git command errors

Maximilian Rooscdde20c3fc78
diff --git a/src/commands/commit.rs b/src/commands/commit.rs
index 1c0cfb086..e9deb0175 100644
--- a/src/commands/commit.rs
+++ b/src/commands/commit.rs
@@ -1,7 +1,7 @@
use anyhow::Context;
use worktrunk::config::CommitGenerationConfig;
use worktrunk::git::Repository;
-use worktrunk::styling::{AnstyleStyle, CYAN, ERROR, GREEN, HINT, format_with_gutter};
+use worktrunk::styling::{AnstyleStyle, CYAN, GREEN, HINT, format_with_gutter};
use super::command_executor::CommandContext;
use super::hooks::HookPipeline;
@@ -116,11 +116,8 @@ impl<'a> CommitGenerator<'a> {
let formatted_message = self.format_message_for_display(&commit_message);
crate::output::gutter(format_with_gutter(&formatted_message, "", None))?;
- if let Err(e) = repo.run_command(&["commit", "-m", &commit_message]) {
- crate::output::error(format!("{ERROR}Failed to commit{ERROR:#}"))?;
- crate::output::gutter(format_with_gutter(&e.to_string(), "", None))?;
- anyhow::bail!("");
- }
+ repo.run_command(&["commit", "-m", &commit_message])
+ .context("Failed to commit")?;
let commit_hash = repo
.run_command(&["rev-parse", "--short", "HEAD"])?
diff --git a/src/commands/standalone.rs b/src/commands/standalone.rs
index 638d453ab..82fd708c5 100644
--- a/src/commands/standalone.rs
+++ b/src/commands/standalone.rs
@@ -1,7 +1,7 @@
use anyhow::{Context, bail};
use worktrunk::HookType;
use worktrunk::git::Repository;
-use worktrunk::styling::{AnstyleStyle, CYAN, CYAN_BOLD, ERROR, GREEN_BOLD, format_with_gutter};
+use worktrunk::styling::{AnstyleStyle, CYAN, CYAN_BOLD, GREEN_BOLD, format_with_gutter};
use super::commit::{CommitGenerator, CommitOptions};
use super::context::CommandEnv;
@@ -250,11 +250,8 @@ pub fn handle_squash(
}
// Commit with the generated message
- if let Err(e) = repo.run_command(&["commit", "-m", &commit_message]) {
- crate::output::error(format!("{ERROR}Failed to create squash commit{ERROR:#}"))?;
- crate::output::gutter(format_with_gutter(&e.to_string(), "", None))?;
- bail!("");
- }
+ repo.run_command(&["commit", "-m", &commit_message])
+ .context("Failed to create squash commit")?;
// Get commit hash for display
let commit_hash = repo
diff --git a/src/main.rs b/src/main.rs
index 050b04957..1b24ad89c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,7 +5,7 @@ use std::process;
use worktrunk::config::{WorktrunkConfig, set_config_path};
use worktrunk::git::{Repository, exit_code, is_command_not_approved, set_base_path};
use worktrunk::path::format_path_for_display;
-use worktrunk::styling::println;
+use worktrunk::styling::{format_with_gutter, println};
mod cli;
mod commands;
@@ -757,13 +757,20 @@ fn main() {
let _ = output::terminate_output();
if let Err(e) = result {
- // Error messages are already formatted with emoji and colors
// Route through output system to respect mode:
// - Interactive mode: errors go to stdout
// - Directive mode: errors go to stderr
- // Skip empty errors (already displayed by the command)
+ //
+ // Multi-line errors from external commands (e.g., git output) get gutter formatting.
+ // We detect this by checking if context was added: e.to_string() != root_cause means
+ // the error was wrapped with .context(), so the root cause is raw external output.
let msg = e.to_string();
- if !msg.is_empty() {
+ let root_cause = e.root_cause().to_string();
+ let has_context = msg != root_cause;
+ if has_context && root_cause.contains('\n') {
+ let _ = output::error(msg);
+ let _ = output::gutter(format_with_gutter(&root_cause, "", None));
+ } else {
let _ = output::error(msg);
}
diff --git a/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap b/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap
index ef15003e0..9d5cc380f 100644
--- a/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap
+++ b/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap
@@ -28,7 +28,7 @@ exit_code: 1
πŸ”„ Committing changes with default message...
πŸ’‘ Using fallback commit message. Run 'wt config --help' for LLM setup guide
  WIP: Changes
-❌ Failed to commit
+❌ Failed to commit
  On branch main
  nothing to commit, working tree clean
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__invalid_toml.snap b/tests/snapshots/integration__integration_tests__post_start_commands__invalid_toml.snap
index d165de690..81df5e2e5 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__invalid_toml.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__invalid_toml.snap
@@ -14,14 +14,21 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_BIN: ""
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
---
success: false
exit_code: 1
----- stdout -----
❌ Failed to load project config
+  Failed to parse TOML: TOML parse error at line 1, column 31
+  |
+  1 | post-create = [invalid syntax
+  | ^
+  unclosed array, expected `]`
----- stderr -----