Skip to content

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

refactor(hooks): Unify hook types and generalize errors

Maximilian Roosd022a6908e29
diff --git a/Cargo.lock b/Cargo.lock
index 2b023fe4f..b950b5be6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1644,6 +1644,28 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
+[[package]]
+name = "strum"
+version = "0.26.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
+dependencies = [
+ "strum_macros",
+]
+
+[[package]]
+name = "strum_macros"
+version = "0.26.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "rustversion",
+ "syn",
+]
+
[[package]]
name = "syn"
version = "2.0.108"
@@ -2355,6 +2377,7 @@ dependencies = [
"serde_json",
"shell-escape",
"shellexpand",
+ "strum",
"synoptic",
"tempfile",
"terminal_size",
diff --git a/Cargo.toml b/Cargo.toml
index 7168f879c..8dde95474 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -34,6 +34,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
shell-escape = "0.1"
shellexpand = "3.1"
+strum = { version = "0.26", features = ["derive"] }
synoptic = "2"
terminal_size = "0.4"
toml = "0.9"
diff --git a/src/commands/completion.rs b/src/commands/completion.rs
index bee95728f..c1ace08c0 100644
--- a/src/commands/completion.rs
+++ b/src/commands/completion.rs
@@ -138,14 +138,8 @@ pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {
let context = parse_completion_context(&args);
match context {
- CompletionContext::SwitchBranch => {
- // Complete with all branches
- let branches = get_branches_for_completion(|| Repository::current().all_branches());
- for branch in branches {
- println!("{}", branch);
- }
- }
- CompletionContext::PushTarget
+ CompletionContext::SwitchBranch
+ | CompletionContext::PushTarget
| CompletionContext::MergeTarget
| CompletionContext::RemoveBranch
| CompletionContext::BaseFlag => {
diff --git a/src/commands/dev.rs b/src/commands/dev.rs
index 9da14b11a..77f748dd0 100644
--- a/src/commands/dev.rs
+++ b/src/commands/dev.rs
@@ -1,3 +1,4 @@
+use worktrunk::HookType;
use worktrunk::config::{ProjectConfig, WorktrunkConfig};
use worktrunk::git::{GitError, GitResultExt, Repository};
use worktrunk::styling::{HINT, HINT_EMOJI, eprintln};
@@ -8,9 +9,6 @@ use super::merge::{
};
use super::worktree::{execute_post_create_commands, execute_post_start_commands_sequential};
-// Re-export HookType from main
-pub use crate::HookType;
-
/// Handle `wt dev run-hook` command
pub fn handle_dev_run_hook(hook_type: HookType, force: bool) -> Result<(), GitError> {
// Derive context from current environment
@@ -116,12 +114,11 @@ fn load_project_config(repo: &Repository) -> Result<ProjectConfig, GitError> {
fn check_hook_configured<T>(hook: &Option<T>, hook_type: HookType) -> Result<(), GitError> {
if hook.is_none() {
- let hook_name = hook_type.as_str();
eprintln!(
- "{HINT_EMOJI} {HINT}No {hook_name} commands configured in project config{HINT:#}"
+ "{HINT_EMOJI} {HINT}No {hook_type} commands configured in project config{HINT:#}"
);
return Err(GitError::CommandFailed(format!(
- "No {hook_name} commands configured"
+ "No {hook_type} commands configured"
)));
}
Ok(())
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 89ea3db0b..af7a88b67 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -1,3 +1,4 @@
+use worktrunk::HookType;
use worktrunk::config::{ProjectConfig, WorktrunkConfig};
use worktrunk::git::{GitError, GitResultExt, Repository};
use worktrunk::styling::{
@@ -560,7 +561,8 @@ pub fn run_pre_merge_commands(
crate::output::progress(format_bash_with_gutter(&prepared.expanded, ""))?;
if let Err(e) = execute_command_in_worktree(worktree_path, &prepared.expanded) {
- return Err(GitError::PreMergeCommandFailed {
+ return Err(GitError::HookCommandFailed {
+ hook_type: HookType::PreMerge,
command_name: prepared.name.clone(),
error: e.to_string(),
});
@@ -689,7 +691,8 @@ pub fn run_pre_commit_commands(
crate::output::progress(format_bash_with_gutter(&prepared.expanded, ""))?;
if let Err(e) = execute_command_in_worktree(worktree_path, &prepared.expanded) {
- return Err(GitError::PreCommitCommandFailed {
+ return Err(GitError::HookCommandFailed {
+ hook_type: HookType::PreCommit,
command_name: prepared.name.clone(),
error: e.to_string(),
});
@@ -747,7 +750,8 @@ pub fn run_pre_squash_commands(
crate::output::progress(format_bash_with_gutter(&prepared.expanded, ""))?;
if let Err(e) = execute_command_in_worktree(worktree_path, &prepared.expanded) {
- return Err(GitError::PreSquashCommandFailed {
+ return Err(GitError::HookCommandFailed {
+ hook_type: HookType::PreSquash,
command_name: prepared.name.clone(),
error: e.to_string(),
});
diff --git a/src/git.rs b/src/git.rs
index 7ff750765..a3b5700ef 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -1,6 +1,17 @@
use std::path::PathBuf;
use std::process::Command;
+#[derive(Debug, Clone, Copy, clap::ValueEnum, strum::Display)]
+#[strum(serialize_all = "kebab-case")]
+pub enum HookType {
+ PostCreate,
+ PostStart,
+ PreCommit,
+ PreSquash,
+ PreMerge,
+ PostMerge,
+}
+
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct Worktree {
pub path: PathBuf,
@@ -22,18 +33,9 @@ pub enum GitError {
DetachedHead,
/// Working tree has untracked files
UntrackedFiles,
- /// Pre-commit command failed
- PreCommitCommandFailed {
- command_name: Option<String>,
- error: String,
- },
- /// Pre-squash command failed
- PreSquashCommandFailed {
- command_name: Option<String>,
- error: String,
- },
- /// Pre-merge command failed
- PreMergeCommandFailed {
+ /// Hook command failed
+ HookCommandFailed {
+ hook_type: HookType,
command_name: Option<String>,
error: String,
},
@@ -98,44 +100,9 @@ impl std::fmt::Display for GitError {
)
}
- // Pre-commit command failed
- GitError::PreCommitCommandFailed {
- command_name,
- error,
- } => {
- let error_bold = ERROR.bold();
- match command_name {
- Some(name) => write!(
- f,
- "{ERROR_EMOJI} {ERROR}Pre-commit command failed: {ERROR:#}{error_bold}{name}{error_bold:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip pre-commit commands{HINT:#}"
- ),
- None => write!(
- f,
- "{ERROR_EMOJI} {ERROR}Pre-commit command failed{ERROR:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip pre-commit commands{HINT:#}"
- ),
- }
- }
-
- // Pre-squash command failed
- GitError::PreSquashCommandFailed {
- command_name,
- error,
- } => {
- let error_bold = ERROR.bold();
- match command_name {
- Some(name) => write!(
- f,
- "{ERROR_EMOJI} {ERROR}Pre-squash command failed: {ERROR:#}{error_bold}{name}{error_bold:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip pre-squash commands{HINT:#}"
- ),
- None => write!(
- f,
- "{ERROR_EMOJI} {ERROR}Pre-squash command failed{ERROR:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip pre-squash commands{HINT:#}"
- ),
- }
- }
-
- // Pre-merge command failed
- GitError::PreMergeCommandFailed {
+ // Hook command failed
+ GitError::HookCommandFailed {
+ hook_type,
command_name,
error,
} => {
@@ -143,11 +110,11 @@ impl std::fmt::Display for GitError {
match command_name {
Some(name) => write!(
f,
- "{ERROR_EMOJI} {ERROR}Pre-merge command failed: {ERROR:#}{error_bold}{name}{error_bold:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip pre-merge commands{HINT:#}"
+ "{ERROR_EMOJI} {ERROR}{hook_type} command failed: {ERROR:#}{error_bold}{name}{error_bold:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip {hook_type} commands{HINT:#}"
),
None => write!(
f,
- "{ERROR_EMOJI} {ERROR}Pre-merge command failed{ERROR:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip pre-merge commands{HINT:#}"
+ "{ERROR_EMOJI} {ERROR}{hook_type} command failed{ERROR:#}\n\n{error}\n\n{HINT_EMOJI} {HINT}Use --no-hooks to skip {hook_type} commands{HINT:#}"
),
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 39b590427..dd2c72946 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,5 +3,8 @@ pub mod git;
pub mod shell;
pub mod styling;
+// Re-export HookType for convenience
+pub use git::HookType;
+
// Note: display, commands, and llm modules are used by main.rs but not exposed as public API
// Test comment
diff --git a/src/main.rs b/src/main.rs
index f0361cff8..d190cf750 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,7 @@
use anstyle::Style;
-use clap::{ArgAction, CommandFactory, Parser, Subcommand, ValueEnum};
+use clap::{ArgAction, CommandFactory, Parser, Subcommand};
use std::process;
+use worktrunk::HookType;
use worktrunk::config::WorktrunkConfig;
use worktrunk::git::{GitError, GitResultExt, Repository};
use worktrunk::styling::{SUCCESS_EMOJI, println};
@@ -66,30 +67,6 @@ enum ConfigCommand {
},
}
-#[derive(Debug, Clone, Copy, ValueEnum)]
-pub enum HookType {
- PostCreate,
- PostStart,
- PreCommit,
- PreSquash,
- PreMerge,
- PostMerge,
-}
-
-impl HookType {
- /// Returns the kebab-case name for display and error messages
- pub fn as_str(self) -> &'static str {
- match self {
- HookType::PostCreate => "post-create",
- HookType::PostStart => "post-start",
- HookType::PreCommit => "pre-commit",
- HookType::PreSquash => "pre-squash",
- HookType::PreMerge => "pre-merge",
- HookType::PostMerge => "post-merge",
- }
- }
-}
-
#[derive(Subcommand)]
enum DevCommand {
/// Run a project hook for testing
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
index 44bfdddd5..2853812ac 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
@@ -12,7 +12,7 @@ expression: output.normalized().as_ref()
  echo '✗ Test suite failed: 3 tests failing' && exit 1
✗ Test suite failed: 3 tests failing
-❌ Pre-merge command failed: test
+❌ pre-merge command failed: test
Command failed with exit code: exit status: 1
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
index 5ea0996b9..59d9b1f95 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
@@ -11,7 +11,7 @@ expression: output.normalized().as_ref()
  echo '✗ Test suite failed: 3 tests failing' && exit 1
✗ Test suite failed: 3 tests failing
-❌ Pre-merge command failed: test
+❌ pre-merge command failed: test
Command failed with exit code: exit status: 1
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
index d73cf5c86..ff97215d7 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
@@ -4,7 +4,7 @@ expression: output.normalized().as_ref()
---
✓ Code formatting check passed
✗ Test suite failed: 3 tests failing
-❌ Pre-merge command failed: test
+❌ pre-merge command failed: test
Command failed with exit code: exit status: 1
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_failure.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_failure.snap
index 5e42121ad..7e72770ff 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_failure.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_failure.snap
@@ -17,7 +17,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpz7ITKV/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpnUhwLD/test-config.toml
---
success: false
exit_code: 1
@@ -29,7 +29,7 @@ exit_code: 1
----- stderr -----
-❌ Pre-merge command failed
+❌ pre-merge command failed
Command failed with exit code: exit status: 1
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_squash_command_failure.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_squash_command_failure.snap
index c1fc53bf4..f73aa91f9 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_squash_command_failure.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_squash_command_failure.snap
@@ -17,7 +17,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp8p7RH0/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpX7kAU7/test-config.toml
---
success: false
exit_code: 1
@@ -27,7 +27,7 @@ exit_code: 1
----- stderr -----
-❌ Pre-squash command failed
+❌ pre-squash command failed
Command failed with exit code: exit status: 1