Skip to content

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.rs
index 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 squashing
let repo_root = repo.worktree_base()?;
- let commit_ctx = CommandContext::new(
+ let ctx = CommandContext::new(
&repo,
&config,
&current_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.rs
index 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.rs
index 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` command
pub 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,
- &current_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 project
pub 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.rs
index 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.rs
index 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.snap
index 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 -----
-⚪ Post-create declined
+⚪ post-create declined
✅ Created new worktree for test-mixed from main at [REPO].test-mixed
💡 To enable automatic cd, run: wt config shell
@@ -12,10 +12,10 @@ exit_code: 0
🟡 test-repo wants to execute 2 commands:
-🔄 Post-create:
+🔄 post-create:
  echo 'First command'
-🔄 Post-create:
+🔄 post-create:
  echo 'Third command'
💡 Allow and remember? [y/N]
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_multiple_commands.snap
index 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 -----
-⚪ Post-create declined
+⚪ post-create declined
✅ Created new worktree for test/nested-branch from main at [REPO].test-nested-branch
💡 To enable automatic cd, run: wt config shell
@@ -12,16 +12,16 @@ exit_code: 0
🟡 test-repo wants to execute 4 commands:
-🔄 Post-create:
+🔄 post-create:
  echo 'Branch: test-nested-branch'
-🔄 Post-create:
+🔄 post-create:
  echo 'Worktree: [REPO].test-nested-branch'
-🔄 Post-create:
+🔄 post-create:
  echo 'Repo: test-repo'
-🔄 Post-create:
+🔄 post-create:
  cd [REPO].test-nested-branch && pwd
💡 Allow and remember? [y/N]
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_named_commands.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_named_commands.snap
index 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 -----
-⚪ Post-create declined
+⚪ post-create declined
✅ Created new worktree for test-named from main at [REPO].test-named
💡 To enable automatic cd, run: wt config shell
@@ -12,13 +12,13 @@ exit_code: 0
🟡 test-repo wants to execute 3 commands:
-🔄 Post-create install:
+🔄 post-create install:
  echo 'Installing dependencies...'
-🔄 Post-create build:
+🔄 post-create build:
  echo 'Building project...'
-🔄 Post-create test:
+🔄 post-create test:
  echo 'Running tests...'
💡 Allow and remember? [y/N]
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap b/tests/snapshots/integration__integration_tests__approval_ui__approval_single_command.snap
index 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 -----
-⚪ Post-create declined
+⚪ post-create declined
✅ Created new worktree for feature/test-approval from main at [REPO].feature-test-approval
💡 To enable automatic cd, run: wt config shell
@@ -12,7 +12,7 @@ exit_code: 0
🟡 test-repo wants to execute 1 command:
-🔄 Post-create:
+🔄 post-create:
  echo 'Worktree path: [REPO].feature-test-approval'
💡 Allow and remember? [y/N]
diff --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.snap
index 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 -----
-⚪ Post-create declined
+⚪ post-create declined
✅ Created new worktree for test-decline from main at [REPO].test-decline
💡 To enable automatic cd, run: wt config shell
@@ -12,10 +12,10 @@ exit_code: 0
🟡 test-repo wants to execute 2 commands:
-🔄 Post-create:
+🔄 post-create:
  echo 'First command'
-🔄 Post-create:
+🔄 post-create:
  echo 'Third command'
💡 Allow and remember? [y/N]
diff --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.snap
index 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 -----
-⚪ Post-create declined
+⚪ post-create declined
✅ Created new worktree for test-force-2 from main at [REPO].test-force-2
💡 To enable automatic cd, run: wt config shell
@@ -12,7 +12,7 @@ exit_code: 0
🟡 test-repo wants to execute 1 command:
-🔄 Post-create:
+🔄 post-create:
  echo 'test command' > output.txt
💡 Allow and remember? [y/N]
diff --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.snap
index 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
🟡 test-repo wants to execute 1 command:
-🔄 Post-create:
+🔄 post-create:
  echo 'test command'
💡 Allow and remember? [y/N]