Skip to content

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

refactor: Group command approvals for merge

Maximilian Roos0b9d091caf1d
diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rs
index f5e0922b1..9effa9c25 100644
--- a/src/commands/command_executor.rs
+++ b/src/commands/command_executor.rs
@@ -40,23 +40,20 @@ impl<'a> CommandContext<'a> {
}
}
-pub fn prepare_project_commands<F>(
+/// Expand commands from a CommandConfig without approval
+///
+/// This is the canonical command expansion implementation.
+/// Returns expanded commands as (name, command) tuples.
+fn expand_commands(
command_config: &CommandConfig,
ctx: &CommandContext<'_>,
- auto_trust: bool,
extra_vars: &[(&str, &str)],
- approval_context: &str,
- mut on_skip: F,
-) -> Result<Vec<PreparedCommand>, GitError>
-where
- F: FnMut(Option<&str>, &str),
-{
+) -> Result<Vec<(Option<String>, String)>, GitError> {
let commands = command_config.commands();
if commands.is_empty() {
return Ok(Vec::new());
}
- let project_id = ctx.repo.project_identifier()?;
let repo_root = ctx.repo_root;
let repo_name = repo_root
.file_name()
@@ -76,8 +73,41 @@ where
base_extras.insert(key.to_string(), value.to_string());
}
- let mut prepared = Vec::new();
+ let mut expanded = Vec::new();
+
+ for (name, command) in commands {
+ let extras_owned = base_extras.clone();
+ let extras_ref: HashMap<&str, &str> = extras_owned
+ .iter()
+ .map(|(k, v)| (k.as_str(), v.as_str()))
+ .collect();
+
+ let expanded_cmd = expand_template(command, repo_name, ctx.branch, &extras_ref);
+ expanded.push((name.clone(), expanded_cmd));
+ }
+
+ Ok(expanded)
+}
+pub fn prepare_project_commands<F>(
+ command_config: &CommandConfig,
+ ctx: &CommandContext<'_>,
+ auto_trust: bool,
+ extra_vars: &[(&str, &str)],
+ approval_context: &str,
+ mut on_skip: F,
+) -> Result<Vec<PreparedCommand>, GitError>
+where
+ F: FnMut(Option<&str>, &str),
+{
+ let commands = command_config.commands();
+ if commands.is_empty() {
+ return Ok(Vec::new());
+ }
+
+ let project_id = ctx.repo.project_identifier()?;
+
+ // Approve using original command strings (before expansion)
if !auto_trust
&& !approve_command_batch(
commands,
@@ -93,20 +123,11 @@ where
return Err(GitError::CommandNotApproved);
}
- for (name, command) in commands {
- let extras_owned = base_extras.clone();
- let extras_ref: HashMap<&str, &str> = extras_owned
- .iter()
- .map(|(k, v)| (k.as_str(), v.as_str()))
- .collect();
-
- let expanded = expand_template(command, repo_name, ctx.branch, &extras_ref);
-
- prepared.push(PreparedCommand {
- name: name.clone(),
- expanded,
- });
- }
+ // Expand commands after approval
+ let expanded = expand_commands(command_config, ctx, extra_vars)?;
- Ok(prepared)
+ Ok(expanded
+ .into_iter()
+ .map(|(name, expanded)| PreparedCommand { name, expanded })
+ .collect())
}
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index fa23238de..807fc945c 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -6,10 +6,64 @@ use worktrunk::styling::{
format_bash_with_gutter, format_with_gutter,
};
+use super::command_approval::approve_command_batch;
use super::command_executor::{CommandContext, prepare_project_commands};
use super::worktree::{handle_push, parse_diff_shortstat};
use crate::output::execute_command_in_worktree;
+/// Context for collecting merge commands
+struct MergeCommandCollector<'a> {
+ repo: &'a Repository,
+ no_commit: bool,
+ no_verify: bool,
+ squash_enabled: bool,
+}
+
+/// Collected commands with project identifier
+type CollectedCommands = (Vec<(Option<String>, String)>, String);
+
+impl<'a> MergeCommandCollector<'a> {
+ /// Collect all commands that will be executed during merge
+ ///
+ /// Returns original (unexpanded) commands for approval matching
+ fn collect(self) -> Result<CollectedCommands, GitError> {
+ let mut all_commands = Vec::new();
+ let project_config = match ProjectConfig::load(&self.repo.worktree_root()?)
+ .git_context("Failed to load project config")?
+ {
+ Some(cfg) => cfg,
+ None => return Ok((all_commands, self.repo.project_identifier()?)),
+ };
+
+ // Collect original commands (not expanded) for approval
+ // Expansion happens later in prepare_project_commands during execution
+
+ // Collect pre-commit commands (if we'll commit without squashing)
+ if !self.no_commit
+ && self.repo.is_dirty()?
+ && !self.squash_enabled
+ && let Some(pre_commit_config) = &project_config.pre_commit_command
+ {
+ all_commands.extend(pre_commit_config.commands().to_vec());
+ }
+
+ // 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().to_vec());
+ }
+
+ // Collect post-merge commands
+ if let Some(post_merge_config) = &project_config.post_merge_command {
+ all_commands.extend(post_merge_config.commands().to_vec());
+ }
+
+ let project_id = self.repo.project_identifier()?;
+ Ok((all_commands, project_id))
+ }
+}
+
/// Extract untracked files from git status --porcelain output
fn get_untracked_files(status_output: &str) -> Vec<String> {
let mut untracked = Vec::new();
@@ -91,6 +145,24 @@ pub fn handle_merge(
// Load config for LLM integration
let config = WorktrunkConfig::load().git_context("Failed to load config")?;
+ // Collect and approve all commands upfront for batch permission request
+ let (all_commands, project_id) = MergeCommandCollector {
+ repo: &repo,
+ no_commit,
+ no_verify,
+ squash_enabled,
+ }
+ .collect()?;
+
+ // Approve all commands in a single batch
+ approve_command_batch(
+ &all_commands,
+ &project_id,
+ &config,
+ force,
+ "Merge operation",
+ )?;
+
// Handle uncommitted changes (skip if --no-commit) - track whether commit occurred
let committed = if !no_commit && repo.is_dirty()? {
if squash_enabled {