Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Introduce `collect_commands_for_hooks` function
Maximilian Roosedc3a7a2f901
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex bedefdc2e..2cd3e65fd 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -8,7 +8,7 @@ use super::command_executor::CommandContext;use super::commit::{CommitOptions, commit_changes};use super::context::CommandEnv;use super::hooks::{HookFailureStrategy, HookPipeline};-use super::project_config::load_project_config;+use super::project_config::{collect_commands_for_hooks, load_project_config};use super::worktree::handle_push;/// Context for collecting merge commands@@ -39,25 +39,20 @@ impl<'a> MergeCommandCollector<'a> {// Collect pre-commit commands if we'll commit (direct or via squash)// These run before: (1) direct commit (line 179), or (2) squash commit (line 194 → handle_dev_squash)- if !self.no_commit- && self.repo.is_dirty()?- && let Some(pre_commit_config) = &project_config.pre_commit_command- {- all_commands.extend(pre_commit_config.commands_with_phase(CommandPhase::PreCommit));- }+ let mut hooks = Vec::new();- // Collect pre-merge commands (if not --no-verify)- if !self.no_verify- && let Some(pre_merge_config) = &project_config.pre_merge_command- {- all_commands.extend(pre_merge_config.commands_with_phase(CommandPhase::PreMerge));+ if !self.no_commit && self.repo.is_dirty()? {+ hooks.push(HookType::PreCommit);}- // Collect post-merge commands- if let Some(post_merge_config) = &project_config.post_merge_command {- all_commands.extend(post_merge_config.commands_with_phase(CommandPhase::PostMerge));+ if !self.no_verify {+ hooks.push(HookType::PreMerge);}+ hooks.push(HookType::PostMerge);++ all_commands.extend(collect_commands_for_hooks(&project_config, &hooks));+let project_id = self.repo.project_identifier()?;Ok((all_commands, project_id))}@@ -117,9 +112,8 @@ pub fn handle_merge(if squash_enabled {false // Squash path handles staging and committing} else {- // Commit immediately when not squashinglet repo_root = repo.worktree_base()?;- let commit_ctx = CommandContext::new(+ let ctx = CommandContext::new(&repo,&config,¤t_branch,@@ -127,11 +121,11 @@ pub fn handle_merge(&repo_root,force,);- let mut options = CommitOptions::new(&commit_ctx);+ let mut options = CommitOptions::new(&ctx);options.target_branch = Some(&target_branch);options.no_verify = no_verify;options.tracked_only = tracked_only;- options.auto_trust = true; // commands already approved in merge batch+ options.auto_trust = true;options.warn_about_untracked = !tracked_only;options.show_no_squash_note = true;diff --git a/src/commands/project_config.rs b/src/commands/project_config.rsindex a5ca3a1aa..698b965a0 100644--- a/src/commands/project_config.rs+++ b/src/commands/project_config.rs@@ -1,6 +1,6 @@use std::path::Path;-use worktrunk::config::ProjectConfig;-use worktrunk::git::{GitError, GitResultExt, Repository};+use worktrunk::config::{Command, ProjectConfig};+use worktrunk::git::{GitError, GitResultExt, HookType, Repository};fn load_project_config_at(repo_root: &Path) -> Result<Option<ProjectConfig>, GitError> {ProjectConfig::load(repo_root).git_context("Failed to load project config")@@ -33,3 +33,24 @@ pub fn require_project_config(repo: &Repository) -> Result<ProjectConfig, GitErr}}}++/// Collect commands for the given hook types, preserving order of the provided hooks.+pub fn collect_commands_for_hooks(+ project_config: &ProjectConfig,+ hooks: &[HookType],+) -> Vec<Command> {+ let mut commands = Vec::new();+ for hook in hooks {+ let cfg = match hook {+ HookType::PostCreate => &project_config.post_create_command,+ HookType::PostStart => &project_config.post_start_command,+ HookType::PreCommit => &project_config.pre_commit_command,+ HookType::PreMerge => &project_config.pre_merge_command,+ HookType::PostMerge => &project_config.post_merge_command,+ };+ if let Some(config) = cfg {+ commands.extend(config.commands_with_phase(*hook));+ }+ }+ commands+}diff --git a/src/commands/standalone.rs b/src/commands/standalone.rsindex 90b66f55b..b882e9673 100644--- a/src/commands/standalone.rs+++ b/src/commands/standalone.rs@@ -11,7 +11,9 @@ use super::commit::{};use super::context::CommandEnv;use super::merge::{execute_post_merge_commands, run_pre_merge_commands};-use super::project_config::{load_project_config, require_project_config};+use super::project_config::{+ collect_commands_for_hooks, load_project_config, require_project_config,+};use super::worktree::{execute_post_create_commands, execute_post_start_commands_sequential};/// Handle `wt beta run-hook` command@@ -93,23 +95,17 @@ fn check_hook_configured<T>(hook: &Option<T>, hook_type: HookType) -> Result<(),/// Handle `wt beta commit` commandpub fn handle_standalone_commit(force: bool, no_verify: bool) -> Result<(), GitError> {- let CommandEnv {- repo,- branch: current_branch,- config,- worktree_path,- } = CommandEnv::current()?;-- let repo_root = repo.worktree_base()?;- let commit_ctx = CommandContext::new(- &repo,- &config,- ¤t_branch,- &worktree_path,+ let env = CommandEnv::current()?;+ let repo_root = env.repo.worktree_base()?;+ let ctx = CommandContext::new(+ &env.repo,+ &env.config,+ &env.branch,+ &env.worktree_path,&repo_root,force,);- let mut options = CommitOptions::new(&commit_ctx);+ let mut options = CommitOptions::new(&ctx);options.no_verify = no_verify;options.auto_trust = false;options.show_no_squash_note = false;@@ -354,7 +350,7 @@ pub fn handle_rebase(target: Option<&str>) -> Result<bool, GitError> {/// Handle `wt beta ask-approvals` command - approve all commands in the projectpub fn handle_standalone_ask_approvals(force: bool, show_all: bool) -> Result<(), GitError> {use super::command_approval::approve_command_batch;- use worktrunk::config::{CommandPhase, WorktrunkConfig};+ use worktrunk::config::WorktrunkConfig;let repo = Repository::current();let project_id = repo.project_identifier()?;@@ -364,32 +360,14 @@ pub fn handle_standalone_ask_approvals(force: bool, show_all: bool) -> Result<()let project_config = require_project_config(&repo)?;// Collect all commands from the project config- let mut commands = Vec::new();-- // post-create-command- if let Some(cmd_config) = &project_config.post_create_command {- commands.extend(cmd_config.commands_with_phase(CommandPhase::PostCreate));- }-- // post-start-command- if let Some(cmd_config) = &project_config.post_start_command {- commands.extend(cmd_config.commands_with_phase(CommandPhase::PostStart));- }-- // pre-commit-command- if let Some(cmd_config) = &project_config.pre_commit_command {- commands.extend(cmd_config.commands_with_phase(CommandPhase::PreCommit));- }-- // pre-merge-command- if let Some(cmd_config) = &project_config.pre_merge_command {- commands.extend(cmd_config.commands_with_phase(CommandPhase::PreMerge));- }-- // post-merge-command- if let Some(cmd_config) = &project_config.post_merge_command {- commands.extend(cmd_config.commands_with_phase(CommandPhase::PostMerge));- }+ let all_hooks = [+ HookType::PostCreate,+ HookType::PostStart,+ HookType::PreCommit,+ HookType::PreMerge,+ HookType::PostMerge,+ ];+ let commands = collect_commands_for_hooks(&project_config, &all_hooks);if commands.is_empty() {let dim = worktrunk::styling::AnstyleStyle::new().dimmed();diff --git a/src/config/commands.rs b/src/config/commands.rsindex bacfbd440..f44aa2d95 100644--- a/src/config/commands.rs+++ b/src/config/commands.rs@@ -3,36 +3,12 @@//! Handles parsing and representation of commands that run during various phases//! of worktree and merge operations.+use crate::git::HookType;use indexmap::IndexMap;use serde::{Deserialize, Serialize};-/// Phase in which a command executes-#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::AsRefStr)]-#[strum(serialize_all = "kebab-case")]-pub enum CommandPhase {- /// After creating a worktree (blocking)- PostCreate,- /// After creating a worktree (background)- PostStart,- /// Before committing during merge (blocking, fail-fast)- PreCommit,- /// Before merging (blocking, fail-fast)- PreMerge,- /// After successful merge (blocking)- PostMerge,-}--impl std::fmt::Display for CommandPhase {- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- let s = self.as_ref();- // Capitalize first letter: "pre-commit" -> "Pre-commit"- let mut chars = s.chars();- match chars.next() {- None => Ok(()),- Some(first) => write!(f, "{}{}", first.to_uppercase(), chars.as_str()),- }- }-}+/// Phase in which a command executes (alias to the canonical hook type)+pub type CommandPhase = HookType;/// Represents a command with its template and optionally expanded form#[derive(Debug, Clone, PartialEq)]diff --git a/src/git/mod.rs b/src/git/mod.rsindex 454631747..a79395cb7 100644--- a/src/git/mod.rs+++ b/src/git/mod.rs@@ -25,7 +25,7 @@ pub(crate) use parse::{// They're accessible as git::HookType, git::Worktree, and git::WorktreeList without needing re-export./// Hook types for git operations-#[derive(Debug, Clone, Copy, clap::ValueEnum, strum::Display)]+#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum, strum::Display)]#[strum(serialize_all = "kebab-case")]pub enum HookType {PostCreate,diff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_mixed_approved_unapproved.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_mixed_approved_unapproved.snapindex 77ef11ac3..0a032c367 100644--- a/tests/snapshots/integration__integration_tests__approval_ui__approval_mixed_approved_unapproved.snap+++ b/tests/snapshots/integration__integration_tests__approval_ui__approval_mixed_approved_unapproved.snap@@ -4,7 +4,7 @@ expression: combined---exit_code: 0----- stdout ------⚪ [2mPost-create declined[0m+⚪ [2mpost-create declined[0m✅ [32mCreated new worktree for [1m[32mtest-mixed[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].test-mixed[0m[0m💡 [2mTo enable automatic cd, run: wt config shell[0m@@ -12,10 +12,10 @@ exit_code: 0🟡 [33m[1m[33mtest-repo[0m[33m wants to execute [1m[33m2[0m[33m commands:[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'First command'[0m[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'Third command'[0m[0m💡 Allow and remember? [1m[y/N][0mdiff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snapindex 6a7abb21f..a6bd8e5f2 100644--- a/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap+++ b/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap@@ -4,7 +4,7 @@ expression: combined---exit_code: 0----- stdout ------⚪ [2mPost-create declined[0m+⚪ [2mpost-create declined[0m✅ [32mCreated new worktree for [1m[32mtest/nested-branch[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].test-nested-branch[0m[0m💡 [2mTo enable automatic cd, run: wt config shell[0m@@ -12,16 +12,16 @@ exit_code: 0🟡 [33m[1m[33mtest-repo[0m[33m wants to execute [1m[33m4[0m[33m commands:[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'Branch: test-nested-branch'[0m[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'Worktree: [REPO].test-nested-branch'[0m[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'Repo: test-repo'[0m[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mcd[0m [REPO].test-nested-branch [36m&&[0m [1m[34mpwd[0m[0m💡 Allow and remember? [1m[y/N][0mdiff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_named_commands.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_named_commands.snapindex f28bad142..da36a7bc4 100644--- a/tests/snapshots/integration__integration_tests__approval_ui__approval_named_commands.snap+++ b/tests/snapshots/integration__integration_tests__approval_ui__approval_named_commands.snap@@ -4,7 +4,7 @@ expression: combined---exit_code: 0----- stdout ------⚪ [2mPost-create declined[0m+⚪ [2mpost-create declined[0m✅ [32mCreated new worktree for [1m[32mtest-named[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].test-named[0m[0m💡 [2mTo enable automatic cd, run: wt config shell[0m@@ -12,13 +12,13 @@ exit_code: 0🟡 [33m[1m[33mtest-repo[0m[33m wants to execute [1m[33m3[0m[33m commands:[0m-🔄 Post-create [1minstall[0m:+🔄 post-create [1minstall[0m:[40m [0m [1m[34mecho[0m [32m'Installing dependencies...'[0m[0m-🔄 Post-create [1mbuild[0m:+🔄 post-create [1mbuild[0m:[40m [0m [1m[34mecho[0m [32m'Building project...'[0m[0m-🔄 Post-create [1mtest[0m:+🔄 post-create [1mtest[0m:[40m [0m [1m[34mecho[0m [32m'Running tests...'[0m[0m💡 Allow and remember? [1m[y/N][0mdiff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snapindex ceb135a20..e14ec6651 100644--- a/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap+++ b/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap@@ -4,7 +4,7 @@ expression: combined---exit_code: 0----- stdout ------⚪ [2mPost-create declined[0m+⚪ [2mpost-create declined[0m✅ [32mCreated new worktree for [1m[32mfeature/test-approval[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].feature-test-approval[0m[0m💡 [2mTo enable automatic cd, run: wt config shell[0m@@ -12,7 +12,7 @@ exit_code: 0🟡 [33m[1m[33mtest-repo[0m[33m wants to execute [1m[33m1[0m[33m command:[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'Worktree path: [REPO].feature-test-approval'[0m[0m💡 Allow and remember? [1m[y/N][0mdiff --git a/tests/snapshots/integration__integration_tests__approval_ui__decline_approval_skips_only_unapproved.snap b/tests/snapshots/integration__integration_tests__approval_ui__decline_approval_skips_only_unapproved.snapindex 225b305c6..aa84dc388 100644--- a/tests/snapshots/integration__integration_tests__approval_ui__decline_approval_skips_only_unapproved.snap+++ b/tests/snapshots/integration__integration_tests__approval_ui__decline_approval_skips_only_unapproved.snap@@ -4,7 +4,7 @@ expression: combined---exit_code: 0----- stdout ------⚪ [2mPost-create declined[0m+⚪ [2mpost-create declined[0m✅ [32mCreated new worktree for [1m[32mtest-decline[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].test-decline[0m[0m💡 [2mTo enable automatic cd, run: wt config shell[0m@@ -12,10 +12,10 @@ exit_code: 0🟡 [33m[1m[33mtest-repo[0m[33m wants to execute [1m[33m2[0m[33m commands:[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'First command'[0m[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'Third command'[0m[0m💡 Allow and remember? [1m[y/N][0mdiff --git a/tests/snapshots/integration__integration_tests__approval_ui__force_does_not_save_approvals_second_run.snap b/tests/snapshots/integration__integration_tests__approval_ui__force_does_not_save_approvals_second_run.snapindex e4a5ff5c2..e13c93d82 100644--- a/tests/snapshots/integration__integration_tests__approval_ui__force_does_not_save_approvals_second_run.snap+++ b/tests/snapshots/integration__integration_tests__approval_ui__force_does_not_save_approvals_second_run.snap@@ -4,7 +4,7 @@ expression: combined---exit_code: 0----- stdout ------⚪ [2mPost-create declined[0m+⚪ [2mpost-create declined[0m✅ [32mCreated new worktree for [1m[32mtest-force-2[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].test-force-2[0m[0m💡 [2mTo enable automatic cd, run: wt config shell[0m@@ -12,7 +12,7 @@ exit_code: 0🟡 [33m[1m[33mtest-repo[0m[33m wants to execute [1m[33m1[0m[33m command:[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'test command'[0m [36m>[0m output.txt[0m💡 Allow and remember? [1m[y/N][0mdiff --git a/tests/snapshots/integration__integration_tests__approval_ui__permission_error_user_output.snap b/tests/snapshots/integration__integration_tests__approval_ui__permission_error_user_output.snapindex e3ab22166..502641a91 100644--- a/tests/snapshots/integration__integration_tests__approval_ui__permission_error_user_output.snap+++ b/tests/snapshots/integration__integration_tests__approval_ui__permission_error_user_output.snap@@ -15,7 +15,7 @@ exit_code: 0🟡 [33m[1m[33mtest-repo[0m[33m wants to execute [1m[33m1[0m[33m command:[0m-🔄 Post-create:+🔄 post-create:[40m [0m [1m[34mecho[0m [32m'test command'[0m[0m💡 Allow and remember? [1m[y/N][0m