Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: Emit errors on git commit failures
Maximilian Roos4b5dbad83443
diff --git a/src/commands/commit.rs b/src/commands/commit.rsindex e9deb0175..1c0cfb086 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, GREEN, HINT, format_with_gutter};+use worktrunk::styling::{AnstyleStyle, CYAN, ERROR, GREEN, HINT, format_with_gutter};use super::command_executor::CommandContext;use super::hooks::HookPipeline;@@ -116,8 +116,11 @@ impl<'a> CommitGenerator<'a> {let formatted_message = self.format_message_for_display(&commit_message);crate::output::gutter(format_with_gutter(&formatted_message, "", None))?;- repo.run_command(&["commit", "-m", &commit_message])- .context("Failed to commit")?;+ 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!("");+ }let commit_hash = repo.run_command(&["rev-parse", "--short", "HEAD"])?diff --git a/src/commands/standalone.rs b/src/commands/standalone.rsindex 82fd708c5..638d453ab 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, GREEN_BOLD, format_with_gutter};+use worktrunk::styling::{AnstyleStyle, CYAN, CYAN_BOLD, ERROR, GREEN_BOLD, format_with_gutter};use super::commit::{CommitGenerator, CommitOptions};use super::context::CommandEnv;@@ -250,8 +250,11 @@ pub fn handle_squash(}// Commit with the generated message- repo.run_command(&["commit", "-m", &commit_message])- .context("Failed to create squash commit")?;+ 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!("");+ }// Get commit hash for displaylet commit_hash = repodiff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex 605bd247f..8ece037db 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -1177,7 +1177,14 @@ impl Repository {for line in stderr.trim().lines() {log::debug!(" ! {}", line);}- bail!("{}", stderr);+ // 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()]+ .into_iter()+ .filter(|s| !s.is_empty())+ .collect::<Vec<_>>()+ .join("\n");+ bail!("{}", error_msg);}let stdout = String::from_utf8_lossy(&output.stdout).into_owned();diff --git a/src/main.rs b/src/main.rsindex c780f16f9..050b04957 100644--- a/src/main.rs+++ b/src/main.rs@@ -761,7 +761,11 @@ fn main() {// Route through output system to respect mode:// - Interactive mode: errors go to stdout// - Directive mode: errors go to stderr- let _ = output::error(e.to_string());+ // Skip empty errors (already displayed by the command)+ let msg = e.to_string();+ if !msg.is_empty() {+ let _ = output::error(msg);+ }// Preserve exit code from child processes (especially for signals like SIGINT)let code = exit_code(&e).unwrap_or(1);diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex b0240f8bd..7e7c3444b 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -3320,3 +3320,24 @@ fn test_step_commit_with_both_flags() {],);}++#[test]+fn test_step_commit_nothing_to_commit() {+ let repo = TestRepo::new();+ repo.commit("Initial commit");++ // No changes made - commit should fail with "nothing to commit"+ snapshot_step_commit_with_env(+ "step_commit_nothing_to_commit",+ &repo,+ &["--stage=none"],+ None,+ &[+ ("WORKTRUNK_COMMIT_GENERATION__COMMAND", "echo"),+ (+ "WORKTRUNK_COMMIT_GENERATION__ARGS",+ "feat: this should fail",+ ),+ ],+ );+}diff --git a/tests/snapshots/integration__integration_tests__merge__merge_rebase_conflict.snap b/tests/snapshots/integration__integration_tests__merge__merge_rebase_conflict.snapindex bd8736159..e6ee121c4 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_rebase_conflict.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_rebase_conflict.snap@@ -16,6 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_BIN: ""WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"---success: false@@ -26,6 +27,8 @@ exit_code: 1[107m [0m Rebasing (1/1)[107m [0m error: could not apply [SHA]... Update shared.txt in feature[107m [0m Could not apply [SHA]... # Update shared.txt in feature+[107m [0m Auto-merging shared.txt+[107m [0m CONFLICT (content): Merge conflict in shared.txt----- stderr -----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.snapnew file mode 100644index 000000000..ef15003e0--- /dev/null+++ b/tests/snapshots/integration__integration_tests__merge__step_commit_nothing_to_commit.snap@@ -0,0 +1,35 @@+---+source: tests/integration_tests/merge.rs+info:+ program: wt+ args:+ - step+ - commit+ - "--stage=none"+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_BIN: ""+ WORKTRUNK_COMMIT_GENERATION__ARGS: "feat: this should fail"+ WORKTRUNK_COMMIT_GENERATION__COMMAND: echo+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+---+success: false+exit_code: 1+----- stdout -----+π [36mCommitting changes with default message...[0m+π‘ [2m[2mUsing fallback commit message. Run 'wt config --help' for LLM setup guide[0m[0m+[107m [0m [1mWIP: Changes[0m+β [31m[31mFailed to commit[0m[0m+[107m [0m On branch main+[107m [0m nothing to commit, working tree clean++----- stderr -----