Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Extract run_hook_with_filter to hooks module
Maximilian Roosf612edc96685
diff --git a/src/commands/hooks.rs b/src/commands/hooks.rsindex 249364f39..96f6ddac6 100644--- a/src/commands/hooks.rs+++ b/src/commands/hooks.rs@@ -262,3 +262,75 @@ impl<'a> HookPipeline<'a> {Ok(())}}++/// Run user and project hooks for a given hook type.+///+/// This is the canonical implementation for running hooks from both sources.+/// Runs user hooks first, then project hooks. Handles name filtering and+/// returns an error if a name filter was provided but no matching command found.+pub fn run_hook_with_filter(+ ctx: &CommandContext,+ user_config: Option<&CommandConfig>,+ project_config: Option<&CommandConfig>,+ hook_type: HookType,+ extra_vars: &[(&str, &str)],+ failure_strategy: HookFailureStrategy,+ name_filter: Option<&str>,+) -> anyhow::Result<()> {+ let pipeline = HookPipeline::new(*ctx);+ let mut total_commands_run = 0;++ // Run user hooks first (no approval required)+ if let Some(config) = user_config {+ total_commands_run += pipeline.run_sequential(+ config,+ hook_type,+ HookSource::User,+ extra_vars,+ HookFailureStrategy::FailFast, // User hooks always fail-fast+ name_filter,+ )?;+ }++ // Then run project hooks (approval handled at gate)+ if let Some(config) = project_config {+ total_commands_run += pipeline.run_sequential(+ config,+ hook_type,+ HookSource::Project,+ extra_vars,+ failure_strategy,+ name_filter,+ )?;+ }++ // If name filter was provided but no commands matched, error with available names+ if let Some(name) = name_filter+ && total_commands_run == 0+ {+ let mut available = Vec::new();+ if let Some(config) = user_config {+ available.extend(+ config+ .commands()+ .iter()+ .filter_map(|c| c.name.as_ref().map(|n| format!("user:{n}"))),+ );+ }+ if let Some(config) = project_config {+ available.extend(+ config+ .commands()+ .iter()+ .filter_map(|c| c.name.as_ref().map(|n| format!("project:{n}"))),+ );+ }+ return Err(worktrunk::git::GitError::HookCommandNotFound {+ name: name.to_string(),+ available,+ }+ .into());+ }++ Ok(())+}diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex ec919842f..d20c561f2 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -7,7 +7,7 @@ use super::command_approval::approve_command_batch;use super::command_executor::CommandContext;use super::commit::CommitOptions;use super::context::CommandEnv;-use super::hooks::{HookFailureStrategy, HookPipeline, HookSource};+use super::hooks::{HookFailureStrategy, run_hook_with_filter};use super::project_config::collect_commands_for_hooks;use super::repository_ext::RepositoryCliExt;use super::worktree::{MergeOperations, RemoveResult, handle_push};@@ -276,63 +276,15 @@ pub fn run_pre_merge_commands(target_branch: &str,name_filter: Option<&str>,) -> anyhow::Result<()> {- let pipeline = HookPipeline::new(*ctx);- let extra_vars = [("target", target_branch)];- let mut total_commands_run = 0;-- // Run user hooks first (no approval required)- if let Some(user_config) = &ctx.config.pre_merge {- total_commands_run += pipeline.run_sequential(- user_config,- HookType::PreMerge,- HookSource::User,- &extra_vars,- HookFailureStrategy::FailFast,- name_filter,- )?;- }-- // Then run project hooks (require approval)- if let Some(pre_merge_config) = &project_config.pre_merge {- total_commands_run += pipeline.run_sequential(- pre_merge_config,- HookType::PreMerge,- HookSource::Project,- &extra_vars,- HookFailureStrategy::FailFast,- name_filter,- )?;- }-- // If name filter was provided but no commands matched, error with available names- if let Some(name) = name_filter- && total_commands_run == 0- {- let mut available = Vec::new();- if let Some(config) = &ctx.config.pre_merge {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("user:{n}"))),- );- }- if let Some(config) = &project_config.pre_merge {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("project:{n}"))),- );- }- return Err(worktrunk::git::GitError::HookCommandNotFound {- name: name.to_string(),- available,- }- .into());- }-- Ok(())+ run_hook_with_filter(+ ctx,+ ctx.config.pre_merge.as_ref(),+ project_config.pre_merge.as_ref(),+ HookType::PreMerge,+ &[("target", target_branch)],+ HookFailureStrategy::FailFast,+ name_filter,+ )}/// Execute post-merge commands sequentially in the main worktree (blocking)@@ -344,66 +296,18 @@ pub fn execute_post_merge_commands(target_branch: &str,name_filter: Option<&str>,) -> anyhow::Result<()> {- let pipeline = HookPipeline::new(*ctx);- let extra_vars = [("target", target_branch)];- let mut total_commands_run = 0;-- // Run user hooks first (no approval required)- if let Some(user_config) = &ctx.config.post_merge {- total_commands_run += pipeline.run_sequential(- user_config,- HookType::PostMerge,- HookSource::User,- &extra_vars,- HookFailureStrategy::Warn,- name_filter,- )?;- }-- // Then run project hooks (require approval)- // Load project config from the main worktree path directly+ // Load project config from the main worktree pathlet project_config = ctx.repo.load_project_config()?;- if let Some(post_merge_config) = project_config.as_ref().and_then(|c| c.post_merge.as_ref()) {- total_commands_run += pipeline.run_sequential(- post_merge_config,- HookType::PostMerge,- HookSource::Project,- &extra_vars,- HookFailureStrategy::Warn,- name_filter,- )?;- }-- // If name filter was provided but no commands matched, error with available names- if let Some(name) = name_filter- && total_commands_run == 0- {- let mut available = Vec::new();- if let Some(config) = &ctx.config.post_merge {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("user:{n}"))),- );- }- if let Some(config) = project_config.as_ref().and_then(|c| c.post_merge.as_ref()) {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("project:{n}"))),- );- }- return Err(worktrunk::git::GitError::HookCommandNotFound {- name: name.to_string(),- available,- }- .into());- }-- Ok(())+ run_hook_with_filter(+ ctx,+ ctx.config.post_merge.as_ref(),+ project_config.as_ref().and_then(|c| c.post_merge.as_ref()),+ HookType::PostMerge,+ &[("target", target_branch)],+ HookFailureStrategy::Warn,+ name_filter,+ )}/// Execute pre-remove commands sequentially in the worktree (blocking)@@ -415,62 +319,15 @@ pub fn execute_pre_remove_commands(ctx: &CommandContext,name_filter: Option<&str>,) -> anyhow::Result<()> {- let pipeline = HookPipeline::new(*ctx);- let mut total_commands_run = 0;-- // Run user hooks first (no approval required)- if let Some(user_config) = &ctx.config.pre_remove {- total_commands_run += pipeline.run_sequential(- user_config,- HookType::PreRemove,- HookSource::User,- &[],- HookFailureStrategy::FailFast,- name_filter,- )?;- }-- // Then run project hooks (require approval)let project_config = ctx.repo.load_project_config()?;- if let Some(pre_remove_config) = project_config.as_ref().and_then(|c| c.pre_remove.as_ref()) {- total_commands_run += pipeline.run_sequential(- pre_remove_config,- HookType::PreRemove,- HookSource::Project,- &[],- HookFailureStrategy::FailFast,- name_filter,- )?;- }-- // If name filter was provided but no commands matched, error with available names- if let Some(name) = name_filter- && total_commands_run == 0- {- let mut available = Vec::new();- if let Some(config) = &ctx.config.pre_remove {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("user:{n}"))),- );- }- if let Some(config) = project_config.as_ref().and_then(|c| c.pre_remove.as_ref()) {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("project:{n}"))),- );- }- return Err(worktrunk::git::GitError::HookCommandNotFound {- name: name.to_string(),- available,- }- .into());- }-- Ok(())+ run_hook_with_filter(+ ctx,+ ctx.config.pre_remove.as_ref(),+ project_config.as_ref().and_then(|c| c.pre_remove.as_ref()),+ HookType::PreRemove,+ &[],+ HookFailureStrategy::FailFast,+ name_filter,+ )}diff --git a/src/commands/standalone.rs b/src/commands/standalone.rsindex 8975c18c9..ff5b1230e 100644--- a/src/commands/standalone.rs+++ b/src/commands/standalone.rs@@ -13,7 +13,7 @@ use worktrunk::styling::{use super::command_executor::CommandContext;use super::commit::{CommitGenerator, CommitOptions};use super::context::CommandEnv;-use super::hooks::HookPipeline;+use super::hooks::{HookFailureStrategy, HookPipeline, run_hook_with_filter};use super::merge::{execute_post_merge_commands, execute_pre_remove_commands, run_pre_merge_commands,};@@ -56,35 +56,55 @@ pub fn run_hook(hook_type: HookType, force: bool, name_filter: Option<&str>) ->};}+ /// Helper to require at least one hook is configured (for standalone `wt hook` command)+ fn require_hooks(+ user: Option<&CommandConfig>,+ project: Option<&CommandConfig>,+ hook_type: HookType,+ ) -> anyhow::Result<()> {+ if user.is_none() && project.is_none() {+ return Err(worktrunk::git::GitError::Other {+ message: format!("No {hook_type} hook configured (neither user nor project)"),+ }+ .into());+ }+ Ok(())+ }+// Execute the hook based on typematch hook_type {HookType::PostCreate => {let user_config = user_hook!(post_create);let project_config = project_config.as_ref().and_then(|c| c.post_create.as_ref());+ require_hooks(user_config, project_config, hook_type)?;run_hook_with_filter(&ctx,user_config,project_config,hook_type,&[],+ HookFailureStrategy::FailFast,name_filter,)}HookType::PostStart => {let user_config = user_hook!(post_start);let project_config = project_config.as_ref().and_then(|c| c.post_start.as_ref());+ require_hooks(user_config, project_config, hook_type)?;run_hook_with_filter(&ctx,user_config,project_config,hook_type,&[],+ HookFailureStrategy::FailFast,name_filter,)}HookType::PreCommit => {let user_config = user_hook!(pre_commit);let project_config = project_config.as_ref().and_then(|c| c.pre_commit.as_ref());+ require_hooks(user_config, project_config, hook_type)?;// Pre-commit hook can optionally use target branch contextlet target_branch = repo.default_branch().ok();let extra_vars: Vec<(&str, &str)> = target_branch@@ -98,6 +118,7 @@ pub fn run_hook(hook_type: HookType, force: bool, name_filter: Option<&str>) ->project_config,hook_type,&extra_vars,+ HookFailureStrategy::FailFast,name_filter,)}@@ -116,86 +137,6 @@ pub fn run_hook(hook_type: HookType, force: bool, name_filter: Option<&str>) ->}}-/// Run user and project hooks with optional name filter (used by standalone hook commands)-///-/// Runs user hooks first, then project hooks.-/// Returns an error if no hooks are configured for this hook type.-fn run_hook_with_filter(- ctx: &super::command_executor::CommandContext,- user_config: Option<&worktrunk::config::CommandConfig>,- project_config: Option<&worktrunk::config::CommandConfig>,- hook_type: HookType,- extra_vars: &[(&str, &str)],- name_filter: Option<&str>,-) -> anyhow::Result<()> {- use super::hooks::{HookFailureStrategy, HookPipeline, HookSource};-- // Check at least one hook is configured- if user_config.is_none() && project_config.is_none() {- return Err(worktrunk::git::GitError::Other {- message: format!("No {hook_type} hook configured (neither user nor project)"),- }- .into());- }-- let pipeline = HookPipeline::new(*ctx);- let mut total_commands_run = 0;-- // Run user hooks first (no approval required)- if let Some(config) = user_config {- total_commands_run += pipeline.run_sequential(- config,- hook_type,- HookSource::User,- extra_vars,- HookFailureStrategy::FailFast,- name_filter,- )?;- }-- // Then run project hooks (approval already happened at gate)- if let Some(config) = project_config {- total_commands_run += pipeline.run_sequential(- config,- hook_type,- HookSource::Project,- extra_vars,- HookFailureStrategy::FailFast,- name_filter,- )?;- }-- // If name filter was provided but no commands matched, error with available names- if let Some(name) = name_filter- && total_commands_run == 0- {- let mut available = Vec::new();- if let Some(config) = user_config {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("user:{n}"))),- );- }- if let Some(config) = project_config {- available.extend(- config- .commands()- .iter()- .filter_map(|c| c.name.as_ref().map(|n| format!("project:{n}"))),- );- }- return Err(worktrunk::git::GitError::HookCommandNotFound {- name: name.to_string(),- available,- }- .into());- }-- Ok(())-}-/// Handle `wt step commit` commandpub fn step_commit(force: bool,