Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: simplify merge command abstractions (#400)
Maximilian Roos0f1417c7830e
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 5d4452b66..be9038e61 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -26,65 +26,41 @@ pub struct MergeOptions<'a> {pub stage_mode: super::commit::StageMode,}-/// Reason why a worktree was preserved (not removed) after merge-enum PreserveReason {- /// User explicitly passed --no-remove- NoRemoveFlag,- /// Running from the main worktree (can't remove main worktree)- IsMainWorktree,- /// Current branch is the same as the target branch- AlreadyOnTarget,-}--/// Context for collecting merge commands-struct MergeCommandCollector<'a> {- repo: &'a Repository,- no_commit: bool,- no_verify: bool,+/// Collect all commands that will be executed during merge.+///+/// Returns (commands, project_identifier) for batch approval.+fn collect_merge_commands(+ repo: &Repository,+ commit: bool,+ verify: bool,will_remove: bool,-}--/// Commands collected for batch approval with their project identifier-/// - `Vec<HookCommand>`: Commands with their hook types for approval display-/// - `String`: Project identifier for config lookup-type CollectedCommands = (Vec<HookCommand>, String);--impl<'a> MergeCommandCollector<'a> {- /// Collect all commands that will be executed during merge- ///- /// Returns original (unexpanded) commands for approval matching- fn collect(self) -> anyhow::Result<CollectedCommands> {- let mut all_commands = Vec::new();- let project_config = match self.repo.load_project_config()? {- Some(cfg) => cfg,- None => return Ok((all_commands, self.repo.project_identifier()?.to_string())),- };-- // Collect original commands (not expanded) for approval- // Expansion happens later in prepare_commands during execution+) -> anyhow::Result<(Vec<HookCommand>, String)> {+ let mut all_commands = Vec::new();+ let project_config = match repo.load_project_config()? {+ Some(cfg) => cfg,+ None => return Ok((all_commands, repo.project_identifier()?.to_string())),+ };- // 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)- let mut hooks = Vec::new();+ // Collect pre-commit commands if we'll commit (direct or via squash)+ let mut hooks = Vec::new();- if !self.no_commit && !self.no_verify && self.repo.is_dirty()? {- hooks.push(HookType::PreCommit);- }+ if commit && verify && repo.is_dirty()? {+ hooks.push(HookType::PreCommit);+ }- if !self.no_verify {- hooks.push(HookType::PreMerge);- hooks.push(HookType::PostMerge);- if self.will_remove {- hooks.push(HookType::PreRemove);- hooks.push(HookType::PostSwitch);- }+ if verify {+ hooks.push(HookType::PreMerge);+ hooks.push(HookType::PostMerge);+ if will_remove {+ hooks.push(HookType::PreRemove);+ hooks.push(HookType::PostSwitch);}+ }- all_commands.extend(collect_commands_for_hooks(&project_config, &hooks));+ all_commands.extend(collect_commands_for_hooks(&project_config, &hooks));- let project_id = self.repo.project_identifier()?.to_string();- Ok((all_commands, project_id))- }+ let project_id = repo.project_identifier()?.to_string();+ Ok((all_commands, project_id))}pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {@@ -129,13 +105,8 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {let remove_effective = remove && !on_target && !in_main;// Collect and approve all commands upfront for batch permission request- let (all_commands, project_id) = MergeCommandCollector {- repo,- no_commit: !commit,- no_verify: !verify,- will_remove: remove_effective,- }- .collect()?;+ let (all_commands, project_id) =+ collect_merge_commands(repo, commit, verify, remove_effective)?;// Approve all commands in a single batch (shows templates, not expanded values)let approved = approve_command_batch(&all_commands, &project_id, config, yes, false)?;@@ -253,19 +224,19 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {force_worktree: false,};// Run hooks during merge removal (pass through verify flag)- // Approval was handled at the gate (MergeCommandCollector)+ // Approval was handled at the gate (collect_merge_commands)crate::output::handle_remove_output(&remove_result, true, verify)?;} else {- // Print comprehensive summary (worktree preserved)- // Priority: main worktree > on target > --no-remove flag- let reason = if in_main {- PreserveReason::IsMainWorktree+ // Worktree preserved - show reason (priority: main worktree > on target > --no-remove flag)+ let message = if in_main {+ "Worktree preserved (main worktree)"} else if on_target {- PreserveReason::AlreadyOnTarget+ "Worktree preserved (already on target branch)"} else {- PreserveReason::NoRemoveFlag+ "Worktree preserved (--no-remove)"};- handle_merge_summary_output(reason)?;+ crate::output::print(info_message(message))?;+ crate::output::flush()?;}if verify {@@ -299,18 +270,6 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {Ok(())}-/// Handle output for merge summary using global output context-fn handle_merge_summary_output(reason: PreserveReason) -> anyhow::Result<()> {- let message = match reason {- PreserveReason::IsMainWorktree => "Worktree preserved (main worktree)",- PreserveReason::AlreadyOnTarget => "Worktree preserved (already on target branch)",- PreserveReason::NoRemoveFlag => "Worktree preserved (--no-remove)",- };- crate::output::print(info_message(message))?;- crate::output::flush()?;-- Ok(())-}/// Run pre-merge commands sequentially (blocking, fail-fast)////// Runs user hooks first, then project hooks.diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 671aba2a9..c936a5377 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -367,13 +367,6 @@ pub struct SwitchBranchInfo {pub expected_path: Option<PathBuf>,}-impl SwitchBranchInfo {- /// The branch name- pub fn branch(&self) -> &str {- &self.branch- }-}-/// How the branch should be handled after worktree removal.////// This enum replaces the previous `no_delete_branch: bool, force_delete: bool` pattern,diff --git a/src/main.rs b/src/main.rsindex 9078a23c7..2bc2afdba 100644--- a/src/main.rs+++ b/src/main.rs@@ -1282,7 +1282,7 @@ fn main() {let ctx = CommandContext::new(&repo,&config,- Some(branch_info.branch()),+ Some(&branch_info.branch),result.path(),&repo_root,yes,diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex c56aba186..025927bac 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -281,7 +281,7 @@ pub fn handle_switch_output(let path = result.path();let path_display = format_path_for_display(path);- let branch = branch_info.branch();+ let branch = &branch_info.branch;// Check if shell integration is active (directive file set)let is_shell_integration_active = super::is_shell_integration_active();