Skip to content

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

Refactor effective_integration_target into Repository method

Maximilian Roos0a67bb5b63f7
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index 075e9ecaf..93c65dba4 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -498,6 +498,9 @@ pub fn collect(
}
let default_branch = repo.default_branch()?;
+ // Effective target for integration checks: upstream if ahead of local, else local.
+ // This handles the case where a branch was merged remotely but user hasn't pulled yet.
+ let integration_target = repo.effective_integration_target(&default_branch);
// Main worktree is the worktree on the default branch (if exists), else first worktree
let main_worktree = worktrees
.worktrees
@@ -698,7 +701,7 @@ pub fn collect(
// Spawn worktree collection in background thread
let sorted_worktrees_clone = sorted_worktrees.clone();
let tx_worktrees = tx.clone();
- let default_branch_clone = default_branch.clone();
+ let integration_target_clone = integration_target.clone();
let expected_results_wt = expected_results.clone();
let options_wt = options.clone();
std::thread::spawn(move || {
@@ -706,12 +709,12 @@ pub fn collect(
.par_iter()
.enumerate()
.for_each(|(idx, wt)| {
- // Always pass default_branch for ahead/behind/diff computation
- // Status symbols will filter based on is_main flag
+ // Pass integration_target for ahead/behind/diff/integration checks.
+ // May be upstream (origin/main) if it's ahead of local main.
super::collect_progressive_impl::collect_worktree_progressive(
wt,
idx,
- &default_branch_clone,
+ &integration_target_clone,
&options_wt,
tx_worktrees.clone(),
&expected_results_wt,
@@ -742,7 +745,7 @@ pub fn collect(
let main_path = main_worktree.path.clone();
let tx_branches = tx.clone();
- let default_branch_clone = default_branch.clone();
+ let integration_target_clone = integration_target.clone();
let expected_results_br = expected_results.clone();
let options_br = options.clone();
std::thread::spawn(move || {
@@ -754,7 +757,7 @@ pub fn collect(
commit_sha,
&main_path,
*item_idx,
- &default_branch_clone,
+ &integration_target_clone,
&options_br,
tx_branches.clone(),
&expected_results_br,
@@ -777,13 +780,9 @@ pub fn collect(
|item_idx, item, ctx| {
// Compute/recompute status symbols as data arrives (both modes)
// This is idempotent and updates status as new data (like upstream) arrives
- let item_default_branch = if item.is_main() {
- None
- } else {
- Some(default_branch.as_str())
- };
+ // Main worktree case is handled inside check_integration_state()
item.compute_status_symbols(
- item_default_branch,
+ Some(integration_target.as_str()),
ctx.has_merge_tree_conflicts,
ctx.user_marker.clone(),
ctx.working_tree_status,
@@ -1000,11 +999,14 @@ pub fn build_worktree_item(
/// Spawns parallel git operations and collects results. Modifies items in place
/// with: commit details, ahead/behind, diffs, upstream, CI, etc.
///
+/// The `integration_target` parameter is the effective target for integration checks
+/// (may be upstream like `origin/main` if it's ahead of local `main`).
+///
/// This is the blocking version used by statusline. For progressive rendering
/// with callbacks, see the `collect()` function.
pub fn populate_items(
items: &mut [ListItem],
- default_branch: &str,
+ integration_target: &str,
options: CollectOptions,
) -> anyhow::Result<()> {
use std::sync::Arc;
@@ -1036,7 +1038,7 @@ pub fn populate_items(
.collect();
// Spawn collection in background thread
- let default_branch_clone = default_branch.to_string();
+ let integration_target_clone = integration_target.to_string();
let expected_results_clone = expected_results.clone();
std::thread::spawn(move || {
for (idx, path, head, branch) in worktree_info {
@@ -1053,7 +1055,7 @@ pub fn populate_items(
super::collect_progressive_impl::collect_worktree_progressive(
&wt,
idx,
- &default_branch_clone,
+ &integration_target_clone,
&options,
tx.clone(),
&expected_results_clone,
@@ -1062,15 +1064,12 @@ pub fn populate_items(
});
// Drain task results (blocking until all complete)
+ // TODO: This callback duplicates logic from collect()'s drain_results callback.
+ // Consider unifying by making collect() take options to skip progressive rendering.
let drain_outcome = drain_results(rx, items, &expected_results, |_item_idx, item, ctx| {
- // Compute status symbols as data arrives (same logic as in collect())
- let item_default_branch = if item.is_main() {
- None
- } else {
- Some(default_branch)
- };
+ // Main worktree case is handled inside check_integration_state()
item.compute_status_symbols(
- item_default_branch,
+ Some(integration_target),
ctx.has_merge_tree_conflicts,
ctx.user_marker.clone(),
ctx.working_tree_status,
diff --git a/src/commands/statusline.rs b/src/commands/statusline.rs
index ea2339a7a..db501b72a 100644
--- a/src/commands/statusline.rs
+++ b/src/commands/statusline.rs
@@ -229,6 +229,8 @@ fn get_git_status(repo: &Repository, cwd: &Path) -> Result<Option<String>> {
return Ok(Some(wt.branch.as_deref().unwrap_or("HEAD").to_string()));
}
};
+ // Effective target for integration checks: upstream if ahead of local, else local.
+ let integration_target = repo.effective_integration_target(&default_branch);
// Determine if this is the main worktree
let main_worktree = worktrees
@@ -243,7 +245,7 @@ fn get_git_status(repo: &Repository, cwd: &Path) -> Result<Option<String>> {
// Populate computed fields (parallel git operations) and format status_line
// Compute everything (same as --full) for complete status symbols
- list::populate_items(&mut items, &default_branch, CollectOptions::default())?;
+ list::populate_items(&mut items, &integration_target, CollectOptions::default())?;
// Return the pre-formatted statusline
if let Some(ref statusline) = items[0].display.statusline {
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index 00a993ea9..ef3aea461 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -740,6 +740,44 @@ impl Repository {
}
}
+ /// Determine the effective target for integration checks.
+ ///
+ /// If the upstream of the local target (e.g., `origin/main`) is strictly ahead of
+ /// the local target (i.e., local is an ancestor of upstream but not the same commit),
+ /// uses the upstream. This handles the common case where a branch was merged remotely
+ /// but the user hasn't pulled yet.
+ ///
+ /// When local and upstream are the same commit, prefers local for clearer messaging.
+ ///
+ /// Returns the effective target ref to check against.
+ ///
+ /// Used by both `wt list` and `wt remove` to ensure consistent integration detection.
+ ///
+ /// TODO(future): When local and remote have diverged (neither is ancestor),
+ /// check integration against both and delete only if integrated into both.
+ /// Current behavior: uses only local in diverged state, may miss remote-merged branches.
+ pub fn effective_integration_target(&self, local_target: &str) -> String {
+ // Get the upstream ref for the local target (e.g., origin/main for main)
+ let upstream = match self.upstream_branch(local_target) {
+ Ok(Some(upstream)) => upstream,
+ _ => return local_target.to_string(),
+ };
+
+ // If local and upstream are the same commit, prefer local for clearer messaging
+ if self.same_commit(local_target, &upstream).unwrap_or(false) {
+ return local_target.to_string();
+ }
+
+ // Check if local is strictly behind upstream (local is ancestor of upstream)
+ // This means upstream has commits that local doesn't have
+ // On error, fall back to local target (defensive: don't fail due to git errors)
+ if self.is_ancestor(local_target, &upstream).unwrap_or(false) {
+ return upstream;
+ }
+
+ local_target.to_string()
+ }
+
/// Get merge/rebase status for the worktree.
pub fn worktree_state(&self) -> anyhow::Result<Option<String>> {
let git_dir = self.git_dir()?;
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index b8379b61f..76a4d1dd7 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -53,42 +53,6 @@ fn format_switch_success_message(
}
}
-/// Determine the effective target for integration checks.
-///
-/// If the upstream of the local target (e.g., `origin/main`) is strictly ahead of
-/// the local target (i.e., local is an ancestor of upstream but not the same commit),
-/// uses the upstream. This handles the common case where a branch was merged remotely
-/// but the user hasn't pulled yet.
-///
-/// When local and upstream are the same commit, prefers local for clearer messaging.
-///
-/// Returns the effective target ref to check against.
-///
-/// TODO(future): When local and remote have diverged (neither is ancestor),
-/// check integration against both and delete only if integrated into both.
-/// Current behavior: uses only local in diverged state, may miss remote-merged branches.
-fn effective_integration_target(repo: &Repository, local_target: &str) -> String {
- // Get the upstream ref for the local target (e.g., origin/main for main)
- let upstream = match repo.upstream_branch(local_target) {
- Ok(Some(upstream)) => upstream,
- _ => return local_target.to_string(),
- };
-
- // If local and upstream are the same commit, prefer local for clearer messaging
- if repo.same_commit(local_target, &upstream).unwrap_or(false) {
- return local_target.to_string();
- }
-
- // Check if local is strictly behind upstream (local is ancestor of upstream)
- // This means upstream has commits that local doesn't have
- // On error, fall back to local target (defensive: don't fail remove due to git errors)
- if repo.is_ancestor(local_target, &upstream).unwrap_or(false) {
- return upstream;
- }
-
- local_target.to_string()
-}
-
/// Result of an integration check, including which target was used.
struct IntegrationResult {
reason: Option<IntegrationReason>,
@@ -110,7 +74,7 @@ struct IntegrationResult {
/// This fail-safe default prevents accidental branch deletion when integration cannot
/// be determined.
fn get_integration_reason(repo: &Repository, branch_name: &str, target: &str) -> IntegrationResult {
- let effective_target = effective_integration_target(repo, target);
+ let effective_target = repo.effective_integration_target(target);
let reason = check_integration_against(repo, branch_name, &effective_target);