Skip to content

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

Include executed git command in error messages

Maximilian Roos200eea431b7c
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index b2eff96e0..413d5c805 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -1405,7 +1405,9 @@ impl Repository {
};
let t0 = Instant::now();
- let output = cmd.output().context("Failed to execute git command")?;
+ let output = cmd
+ .output()
+ .with_context(|| format!("Failed to execute: git {}", args.join(" ")))?;
let duration = t0.elapsed();
// Performance tracing at debug level (enable with RUST_LOG=debug)
@@ -1475,7 +1477,9 @@ impl Repository {
log::debug!("$ git {} [{}]", args.join(" "), worktree);
}
- let output = cmd.output().context("Failed to execute git command")?;
+ let output = cmd
+ .output()
+ .with_context(|| format!("Failed to execute: git {}", args.join(" ")))?;
let success = output.status.success();
if !success {
diff --git a/tests/git_error_display.rs b/tests/git_error_display.rs
index 933dc0502..9baf48050 100644
--- a/tests/git_error_display.rs
+++ b/tests/git_error_display.rs
@@ -1,6 +1,6 @@
use insta::assert_snapshot;
use std::path::PathBuf;
-use worktrunk::git::GitError;
+use worktrunk::git::{GitError, HookType, WorktrunkError};
// ============================================================================
// Worktree errors
@@ -243,6 +243,16 @@ fn display_parse_error() {
assert_snapshot!("parse_error", err.to_string());
}
+#[test]
+fn display_remote_only_branch() {
+ let err = GitError::RemoteOnlyBranch {
+ branch: "feature".into(),
+ remote: "origin".into(),
+ };
+
+ assert_snapshot!("remote_only_branch", err.to_string());
+}
+
#[test]
fn display_other() {
let err = GitError::Other {
@@ -251,3 +261,61 @@ fn display_other() {
assert_snapshot!("other", err.to_string());
}
+
+// ============================================================================
+// WorktrunkError display tests
+// ============================================================================
+
+#[test]
+fn display_hook_command_failed_with_name() {
+ let err = WorktrunkError::HookCommandFailed {
+ hook_type: HookType::PreMerge,
+ command_name: Some("test".into()),
+ error: "exit code 1".into(),
+ exit_code: Some(1),
+ };
+
+ assert_snapshot!("hook_command_failed_with_name", err.to_string());
+}
+
+#[test]
+fn display_hook_command_failed_without_name() {
+ let err = WorktrunkError::HookCommandFailed {
+ hook_type: HookType::PostCreate,
+ command_name: None,
+ error: "command not found".into(),
+ exit_code: Some(127),
+ };
+
+ assert_snapshot!("hook_command_failed_without_name", err.to_string());
+}
+
+// ============================================================================
+// Integration test: verify error message includes command when git unavailable
+// ============================================================================
+
+/// Test that when git execution fails, the error message includes the command.
+/// This is an integration test because it requires running the actual binary.
+#[test]
+#[cfg(unix)]
+fn git_unavailable_error_includes_command() {
+ use insta_cmd::get_cargo_bin;
+ use std::process::Command;
+
+ let mut cmd = Command::new(get_cargo_bin("wt"));
+ cmd.arg("list")
+ // Set PATH to empty so git isn't found
+ .env("PATH", "/nonexistent")
+ // Prevent any fallback mechanisms
+ .env_remove("GIT_EXEC_PATH");
+
+ let output = cmd.output().expect("Failed to run wt");
+ let stderr = String::from_utf8_lossy(&output.stderr);
+
+ // The error should include the git command that failed
+ assert!(
+ stderr.contains("Failed to execute: git"),
+ "Error should include 'Failed to execute: git', got: {}",
+ stderr
+ );
+}
diff --git a/tests/snapshots/git_error_display__hook_command_failed_with_name.snap b/tests/snapshots/git_error_display__hook_command_failed_with_name.snap
new file mode 100644
index 000000000..3e07ec863
--- /dev/null
+++ b/tests/snapshots/git_error_display__hook_command_failed_with_name.snap
@@ -0,0 +1,7 @@
+---
+source: tests/git_error_display.rs
+expression: err.to_string()
+---
+❌ pre-merge command failed: test: exit code 1
+
+πŸ’‘ Use --no-verify to skip pre-merge commands
diff --git a/tests/snapshots/git_error_display__hook_command_failed_without_name.snap b/tests/snapshots/git_error_display__hook_command_failed_without_name.snap
new file mode 100644
index 000000000..56d3d7763
--- /dev/null
+++ b/tests/snapshots/git_error_display__hook_command_failed_without_name.snap
@@ -0,0 +1,7 @@
+---
+source: tests/git_error_display.rs
+expression: err.to_string()
+---
+❌ post-create command failed: command not found
+
+πŸ’‘ Use --no-verify to skip post-create commands
diff --git a/tests/snapshots/git_error_display__remote_only_branch.snap b/tests/snapshots/git_error_display__remote_only_branch.snap
new file mode 100644
index 000000000..19158791d
--- /dev/null
+++ b/tests/snapshots/git_error_display__remote_only_branch.snap
@@ -0,0 +1,7 @@
+---
+source: tests/git_error_display.rs
+expression: err.to_string()
+---
+❌ Branch feature exists only on remote (origin/feature)
+
+πŸ’‘ Use wt switch feature to create a local worktree