Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: deduplicate hook config resolution (#1512)
Maximilian Roos15ffa05fdc0f
diff --git a/src/commands/commit.rs b/src/commands/commit.rsindex 1f28674df..53a99d177 100644--- a/src/commands/commit.rs+++ b/src/commands/commit.rs@@ -157,12 +157,12 @@ impl CommitOptions<'_> {pub fn commit(self) -> anyhow::Result<()> {let project_config = self.ctx.repo.load_project_config()?;let user_hooks = self.ctx.config.hooks(self.ctx.project_id().as_deref());- let user_hooks_exist = user_hooks.pre_commit.is_some();- let project_hooks_exist = project_config- .as_ref()- .map(|c| c.hooks.pre_commit.is_some())- .unwrap_or(false);- let any_hooks_exist = user_hooks_exist || project_hooks_exist;+ let (user_cfg, proj_cfg) = super::hooks::lookup_hook_configs(+ &user_hooks,+ project_config.as_ref(),+ HookType::PreCommit,+ );+ let any_hooks_exist = user_cfg.is_some() || proj_cfg.is_some();// Show skip messageif !self.verify && any_hooks_exist {@@ -183,10 +183,8 @@ impl CommitOptions<'_> {super::hooks::run_hook_with_filter(self.ctx,HookCommandSpec {- user_config: user_hooks.pre_commit.as_ref(),- project_config: project_config- .as_ref()- .and_then(|c| c.hooks.pre_commit.as_ref()),+ user_config: user_cfg,+ project_config: proj_cfg,hook_type: HookType::PreCommit,extra_vars: &extra_vars,name_filter: None,diff --git a/src/commands/handle_switch.rs b/src/commands/handle_switch.rsindex f3b3bb6fc..512f76d09 100644--- a/src/commands/handle_switch.rs+++ b/src/commands/handle_switch.rs@@ -63,6 +63,22 @@ pub(crate) fn run_pre_switch_hooks(Ok(())}+/// Hook types that apply after a switch operation.+///+/// Creates trigger post-create + post-start + post-switch hooks;+/// existing worktrees trigger only post-switch.+fn switch_post_hook_types(is_create: bool) -> &'static [HookType] {+ if is_create {+ &[+ HookType::PostCreate,+ HookType::PostStart,+ HookType::PostSwitch,+ ]+ } else {+ &[HookType::PostSwitch]+ }+}+/// Approve switch hooks upfront and show "Commands declined" if needed.////// Returns `true` if hooks are approved to run.@@ -79,18 +95,7 @@ pub(crate) fn approve_switch_hooks(}let ctx = CommandContext::new(repo, config, Some(plan.branch()), plan.worktree_path(), yes);- let approved = if plan.is_create() {- approve_hooks(- &ctx,- &[- HookType::PostCreate,- HookType::PostStart,- HookType::PostSwitch,- ],- )?- } else {- approve_hooks(&ctx, &[HookType::PostSwitch])?- };+ let approved = approve_hooks(&ctx, switch_post_hook_types(plan.is_create()))?;if !approved {eprintln!(@@ -377,25 +382,10 @@ fn validate_switch_templates(let project_config = repo.load_project_config()?;let user_hooks = config.hooks(repo.project_identifier().ok().as_deref());- let hook_types: &[HookType] = if plan.is_create() {- &[- HookType::PostCreate,- HookType::PostStart,- HookType::PostSwitch,- ]- } else {- &[HookType::PostSwitch]- };-- for &hook_type in hook_types {- let configs = [- ("user", user_hooks.get(hook_type)),- (- "project",- project_config.as_ref().and_then(|c| c.hooks.get(hook_type)),- ),- ];- for (source, cfg) in configs {+ for &hook_type in switch_post_hook_types(plan.is_create()) {+ let (user_cfg, proj_cfg) =+ super::hooks::lookup_hook_configs(&user_hooks, project_config.as_ref(), hook_type);+ for (source, cfg) in [("user", user_cfg), ("project", proj_cfg)] {if let Some(cfg) = cfg {for cmd in cfg.commands() {let name = match &cmd.name {diff --git a/src/commands/hooks.rs b/src/commands/hooks.rsindex b498eb3ca..4dca504b7 100644--- a/src/commands/hooks.rs+++ b/src/commands/hooks.rs@@ -409,7 +409,7 @@ pub fn run_hook_with_filter(}/// Look up user and project configs for a given hook type.-fn lookup_hook_configs<'a>(+pub(crate) fn lookup_hook_configs<'a>(user_hooks: &'a worktrunk::config::HooksConfig,project_config: Option<&'a worktrunk::config::ProjectConfig>,hook_type: HookType,diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rsindex 13fcc887f..89073d2de 100644--- a/src/commands/step_commands.rs+++ b/src/commands/step_commands.rs@@ -139,10 +139,12 @@ pub fn handle_squash(// Check if any pre-commit hooks exist (needed for skip message and approval)let project_config = repo.load_project_config()?;let user_hooks = ctx.config.hooks(ctx.project_id().as_deref());- let any_hooks_exist = user_hooks.pre_commit.is_some()- || project_config- .as_ref()- .is_some_and(|c| c.hooks.pre_commit.is_some());+ let (user_cfg, proj_cfg) = super::hooks::lookup_hook_configs(+ &user_hooks,+ project_config.as_ref(),+ HookType::PreCommit,+ );+ let any_hooks_exist = user_cfg.is_some() || proj_cfg.is_some();// "Approve at the Gate": approve pre-commit hooks upfront (unless --no-verify)// Shadow verify: if user declines approval, skip hooks but continue squash@@ -193,10 +195,8 @@ pub fn handle_squash(run_hook_with_filter(&ctx,HookCommandSpec {- user_config: user_hooks.pre_commit.as_ref(),- project_config: project_config- .as_ref()- .and_then(|c| c.hooks.pre_commit.as_ref()),+ user_config: user_cfg,+ project_config: proj_cfg,hook_type: HookType::PreCommit,extra_vars: &extra_vars,name_filter: None,