Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: consolidate integration reason computation into Repository method (#689)
Maximilian Roos8c2014975b60
diff --git a/src/commands/branch_deletion.rs b/src/commands/branch_deletion.rsindex 7581e9a57..485716612 100644--- a/src/commands/branch_deletion.rs+++ b/src/commands/branch_deletion.rs@@ -4,14 +4,7 @@//! deleted after its worktree is removed. It checks if the branch's content has//! been integrated into the target branch.-use worktrunk::git::{IntegrationReason, Repository, check_integration, compute_integration_lazy};--/// Result of an integration check, including which target was used.-pub struct IntegrationResult {- pub reason: Option<IntegrationReason>,- /// The target that was actually checked against (may be upstream if ahead of local)- pub effective_target: String,-}+use worktrunk::git::{IntegrationReason, Repository};/// Outcome of a branch deletion attempt.pub enum BranchDeletionOutcome {@@ -30,36 +23,6 @@ pub struct BranchDeletionResult {pub effective_target: String,}-/// Check if a branch's content has been integrated into the target.-///-/// Returns the reason if the branch is safe to delete (ordered by check cost):-/// - `SameCommit`: Branch HEAD is literally the same commit as target-/// - `Ancestor`: Branch is ancestor of target (target has moved past)-/// - `NoAddedChanges`: Branch has no file changes beyond merge-base (empty three-dot diff)-/// - `TreesMatch`: The branch's tree SHA matches the target's tree SHA (squash merge/rebase)-/// - `MergeAddsNothing`: Merge simulation shows branch would add nothing (squash + target advanced)-///-/// Also returns the effective target used (may be upstream if it's ahead of local).-///-/// Errors are propagated rather than silently returning None.-pub fn get_integration_reason(- repo: &Repository,- branch_name: &str,- target: &str,-) -> anyhow::Result<IntegrationResult> {- let effective_target = repo.effective_integration_target(target);-- // Use lazy computation with short-circuit evaluation.- // Expensive checks (would_merge_add) are skipped if cheaper ones succeed.- let signals = compute_integration_lazy(repo, branch_name, &effective_target)?;- let reason = check_integration(&signals);-- Ok(IntegrationResult {- reason,- effective_target,- })-}-/// Attempt to delete a branch if it's integrated or force_delete is set.////// Returns `BranchDeletionResult` with:@@ -71,10 +34,7 @@ pub fn delete_branch_if_safe(target: &str,force_delete: bool,) -> anyhow::Result<BranchDeletionResult> {- let IntegrationResult {- reason,- effective_target,- } = get_integration_reason(repo, branch_name, target)?;+ let (effective_target, reason) = repo.integration_reason(branch_name, target)?;// Determine outcome based on integration and force flaglet outcome = match (reason, force_delete) {diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex fb33d1958..cf0f52638 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -212,11 +212,8 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {// STEP 2: Remove worktree via shared remove output handler so final message matches wt removelet worktree_root = repo.current_worktree().root()?.to_path_buf();- // After a successful merge, compute integration reason from main_path- let effective_target = repo.effective_integration_target(&target_branch);- let signals =- worktrunk::git::compute_integration_lazy(repo, ¤t_branch, &effective_target)?;- let integration_reason = worktrunk::git::check_integration(&signals);+ // After a successful merge, get integration reason+ let (_, integration_reason) = repo.integration_reason(¤t_branch, &target_branch)?;// Compute expected_path for path mismatch detectionlet expected_path = get_path_mismatch(repo, ¤t_branch, &worktree_root, config);let remove_result = RemoveResult::RemovedWorktree {diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rsindex 68be180c8..ec2e8b183 100644--- a/src/commands/repository_ext.rs+++ b/src/commands/repository_ext.rs@@ -184,7 +184,7 @@ impl RepositoryCliExt for Repository {// Pre-compute integration reason to avoid race conditions when removing// multiple worktrees in background mode.let integration_reason = compute_integration_reason(- &main_path,+ self,branch_name.as_deref(),target_branch.as_deref(),deletion_mode,@@ -330,7 +330,7 @@ impl RepositoryCliExt for Repository {/// Note: Integration is computed even for `Keep` mode so we can inform the user/// if the flag had an effect (branch was integrated) or not (branch was unmerged).fn compute_integration_reason(- main_path: &Path,+ repo: &Repository,branch_name: Option<&str>,target_branch: Option<&str>,deletion_mode: BranchDeletionMode,@@ -341,15 +341,9 @@ fn compute_integration_reason(return None;}let (branch, target) = branch_name.zip(target_branch)?;- // Use main_path for git operations - this is where the user will end up,- // and integration checks need to run from a stable location that won't be- // removed during the operation.- let repo = Repository::at(main_path).ok()?;- let effective_target = repo.effective_integration_target(target);- // Use lazy computation with short-circuit. On error, return None (informational only).- let signals =- worktrunk::git::compute_integration_lazy(&repo, branch, &effective_target).ok()?;- worktrunk::git::check_integration(&signals)+ // On error, return None (informational only)+ let (_, reason) = repo.integration_reason(branch, target).ok()?;+ reason}/// Warn about untracked files that will be auto-staged.diff --git a/src/git/repository/integration.rs b/src/git/repository/integration.rsindex 3242c3ceb..bca81ba30 100644--- a/src/git/repository/integration.rs+++ b/src/git/repository/integration.rs@@ -4,6 +4,7 @@//! (same commit, ancestor, trees match, etc.).use super::Repository;+use crate::git::{IntegrationReason, check_integration, compute_integration_lazy};impl Repository {/// Check if base is an ancestor of head (i.e., would be a fast-forward).@@ -189,4 +190,39 @@ impl Repository {self.run_command(&["rev-parse", spec]).map(|output| output.trim().to_string())}++ /// Check if a branch is integrated into a target.+ ///+ /// This is a convenience method that combines [`compute_integration_lazy()`] and+ /// [`check_integration()`]. The `target` is transformed via [`Self::effective_integration_target()`]+ /// before checking, which may use an upstream ref if it's ahead of the local target.+ ///+ /// Uses lazy evaluation with short-circuit: stops as soon as any check confirms+ /// integration, avoiding expensive operations like merge simulation when cheaper+ /// checks succeed.+ ///+ /// Returns `(effective_target, reason)` where:+ /// - `effective_target` is the ref actually checked (may be upstream like "origin/main")+ /// - `reason` is `Some(reason)` if integrated, `None` if not+ ///+ /// # Example+ /// ```no_run+ /// use worktrunk::git::Repository;+ ///+ /// let repo = Repository::current()?;+ /// let (effective_target, reason) = repo.integration_reason("feature", "main")?;+ /// if let Some(r) = reason {+ /// println!("Branch integrated into {}: {}", effective_target, r.description());+ /// }+ /// # Ok::<(), anyhow::Error>(())+ /// ```+ pub fn integration_reason(+ &self,+ branch: &str,+ target: &str,+ ) -> anyhow::Result<(String, Option<IntegrationReason>)> {+ let effective_target = self.effective_integration_target(target);+ let signals = compute_integration_lazy(self, branch, &effective_target)?;+ Ok((effective_target, check_integration(&signals)))+ }}