Skip to content

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

refactor: improve formatting of hook and worktree errors

Maximilian Roos6cf79760d8f0
diff --git a/src/git/error.rs b/src/git/error.rs
index 4a8c5fc96..423e7c23d 100644
--- a/src/git/error.rs
+++ b/src/git/error.rs
@@ -122,16 +122,18 @@ impl std::fmt::Display for GitError {
command_name,
error,
exit_code: _,
- } => match command_name {
- Some(name) => write!(
- f,
- "{ERROR_EMOJI} {ERROR}{hook_type} command failed: {ERROR_BOLD}{name}{ERROR_BOLD:#}{ERROR:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-verify to skip {hook_type} commands{HINT:#}"
- ),
- None => write!(
+ } => {
+ // Build optional command name suffix
+ let name_suffix = command_name
+ .as_ref()
+ .map(|n| format!(": {ERROR_BOLD}{n}{ERROR_BOLD:#}"))
+ .unwrap_or_default();
+
+ write!(
f,
- "{ERROR_EMOJI} {ERROR}{hook_type} command failed{ERROR:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-verify to skip {hook_type} commands{HINT:#}"
- ),
- },
+ "{ERROR_EMOJI} {ERROR}{hook_type} command failed{name_suffix}{ERROR:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-verify to skip {hook_type} commands{HINT:#}"
+ )
+ }
// Uncommitted changes
GitError::UncommittedChanges => {
@@ -282,14 +284,15 @@ impl std::fmt::Display for GitError {
base_branch,
error,
} => {
- let header = match base_branch {
- Some(base) => format!(
- "{ERROR_EMOJI} {ERROR}Failed to create worktree for {ERROR_BOLD}{branch}{ERROR_BOLD:#}{ERROR} from base {ERROR_BOLD}{base}{ERROR_BOLD:#}{ERROR:#}"
- ),
- None => format!(
- "{ERROR_EMOJI} {ERROR}Failed to create worktree for {ERROR_BOLD}{branch}{ERROR_BOLD:#}{ERROR:#}"
- ),
- };
+ // Build optional base branch suffix (includes re-establishing ERROR color)
+ let base_suffix = base_branch
+ .as_ref()
+ .map(|base| format!("{ERROR} from base {ERROR_BOLD}{base}{ERROR_BOLD:#}"))
+ .unwrap_or_default();
+
+ let header = format!(
+ "{ERROR_EMOJI} {ERROR}Failed to create worktree for {ERROR_BOLD}{branch}{ERROR_BOLD:#}{base_suffix}{ERROR:#}"
+ );
write!(f, "{}", format_error_block(header, error))
}
diff --git a/tests/integration_tests/.directives.rs.pending-snap b/tests/integration_tests/.directives.rs.pending-snap
new file mode 100644
index 000000000..a0a6ccc48
--- /dev/null
+++ b/tests/integration_tests/.directives.rs.pending-snap
@@ -0,0 +1,10 @@
+{"run_id":"1762730396-93253000","line":26,"new":{"module_name":"integration__integration_tests__directives","snapshot_name":"switch_internal_directive","metadata":{"source":"tests/integration_tests/directives.rs","assertion_line":26,"info":{"program":"wt","args":["--internal","switch","my-feature"],"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":"/dev/null","GIT_CONFIG_SYSTEM":"/dev/null","GIT_EDITOR":"","LANG":"C","LC_ALL":"C","SOURCE_DATE_EPOCH":"1761609600","WORKTRUNK_CONFIG_PATH":"/var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp9TCHsY/test-config.toml"}}},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\n\n----- stderr -----\n❌ \u001b[31mFailed to create worktree for \u001b[1m\u001b[31mmy-feature\u001b[0m\u001b[31m\u001b[0m\n\n\u001b[40m \u001b[0m fatal: invalid reference: my-feature"},"old":{"module_name":"integration__integration_tests__directives","metadata":{},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\n\n----- stderr -----\n❌ \u001b[31mFailed to create worktree for \u001b[1m\u001b[31mmy-feature\u001b[0m\u001b[0m\n\n\u001b[40m \u001b[0m fatal: invalid reference: my-feature"}}
+{"run_id":"1762730396-93253000","line":55,"new":{"module_name":"integration__integration_tests__directives","snapshot_name":"switch_without_internal","metadata":{"source":"tests/integration_tests/directives.rs","assertion_line":55,"info":{"program":"wt","args":["switch","my-feature"],"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":"/dev/null","GIT_CONFIG_SYSTEM":"/dev/null","GIT_EDITOR":"","LANG":"C","LC_ALL":"C","SOURCE_DATE_EPOCH":"1761609600","WORKTRUNK_CONFIG_PATH":"/var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpTiun5b/test-config.toml"}}},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\n\n----- stderr -----\n❌ \u001b[31mFailed to create worktree for \u001b[1m\u001b[31mmy-feature\u001b[0m\u001b[31m\u001b[0m\n\n\u001b[40m \u001b[0m fatal: invalid reference: my-feature"},"old":{"module_name":"integration__integration_tests__directives","metadata":{},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\n\n----- stderr -----\n❌ \u001b[31mFailed to create worktree for \u001b[1m\u001b[31mmy-feature\u001b[0m\u001b[0m\n\n\u001b[40m \u001b[0m fatal: invalid reference: my-feature"}}
+{"run_id":"1762730396-93253000","line":87,"new":null,"old":null}
+{"run_id":"1762730396-93253000","line":105,"new":null,"old":null}
+{"run_id":"1762730411-618815000","line":26,"new":{"module_name":"integration__integration_tests__directives","snapshot_name":"switch_internal_directive","metadata":{"source":"tests/integration_tests/directives.rs","assertion_line":26,"info":{"program":"wt","args":["--internal","switch","my-feature"],"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":"/dev/null","GIT_CONFIG_SYSTEM":"/dev/null","GIT_EDITOR":"","LANG":"C","LC_ALL":"C","SOURCE_DATE_EPOCH":"1761609600","WORKTRUNK_CONFIG_PATH":"/var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpMT7lHf/test-config.toml"}}},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\n\n----- stderr -----\n❌ \u001b[31mFailed to create worktree for \u001b[1m\u001b[31mmy-feature\u001b[0m\u001b[31m\u001b[0m\n\n\u001b[40m \u001b[0m fatal: invalid reference: my-feature"},"old":{"module_name":"integration__integration_tests__directives","metadata":{},"snapshot":"success: false\nexit_code: 1\n----- stdout -----\n\n----- stderr -----\n❌ \u001b[31mFailed to create worktree for \u001b[1m\u001b[31mmy-feature\u001b[0m\u001b[0m\n\n\u001b[40m \u001b[0m fatal: invalid reference: my-feature"}}
+{"run_id":"1762730434-933262000","line":26,"new":null,"old":null}
+{"run_id":"1762730442-972420000","line":26,"new":null,"old":null}
+{"run_id":"1762730442-972420000","line":55,"new":null,"old":null}
+{"run_id":"1762730442-972420000","line":87,"new":null,"old":null}
+{"run_id":"1762730442-972420000","line":105,"new":null,"old":null}