Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
perf: thread worktree list through prepare_worktree_removal (#2025)
Maximilian Roos447de074f611
diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex aef66a53a..8d47b5b40 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -207,6 +207,7 @@ impl CommandCollector for PickerCollector {false,config,caller_path,+ None,);match preparation {diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rsindex 166df05e8..54fe1b53f 100644--- a/src/commands/repository_ext.rs+++ b/src/commands/repository_ext.rs@@ -7,7 +7,7 @@ use anyhow::{Context, bail};use color_print::cformat;use worktrunk::config::UserConfig;use worktrunk::git::{- GitError, IntegrationReason, Repository, parse_porcelain_z, parse_untracked_files,+ GitError, IntegrationReason, Repository, WorktreeInfo, parse_porcelain_z, parse_untracked_files,};use worktrunk::path::format_path_for_display;use worktrunk::styling::{eprintln, format_with_gutter, progress_message, warning_message};@@ -38,6 +38,9 @@ pub trait RepositoryCliExt {/// worktree is "current". Pass `None` for normal CLI usage (discovers from/// CWD). Pass `Some` when calling from a context where CWD may have changed/// (e.g., background threads in the picker).+ ///+ /// `worktrees` provides a pre-fetched worktree list to avoid redundant+ /// `git worktree list` calls. Pass `None` to fetch on demand.fn prepare_worktree_removal(&self,target: RemoveTarget,@@ -45,6 +48,7 @@ pub trait RepositoryCliExt {force_worktree: bool,config: &UserConfig,current_path: Option<PathBuf>,+ worktrees: Option<&[WorktreeInfo]>,) -> anyhow::Result<RemoveResult>;/// Prepare the target worktree for push by auto-stashing non-overlapping changes when safe.@@ -81,9 +85,17 @@ impl RepositoryCliExt for Repository {force_worktree: bool,config: &UserConfig,current_path: Option<PathBuf>,+ worktrees: Option<&[WorktreeInfo]>,) -> anyhow::Result<RemoveResult> {let current_path = current_path.map_or_else(|| self.current_worktree().root(), Ok)?;- let worktrees = self.list_worktrees()?;+ let owned_worktrees;+ let worktrees = match worktrees {+ Some(wts) => wts,+ None => {+ owned_worktrees = self.list_worktrees()?;+ &owned_worktrees+ }+ };// Primary worktree path: prefer default branch's worktree, fall back to first// worktree, then repo base for bare repos with no worktrees.let primary_path = self.home_path()?;diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rsindex e2e1a8c4b..faf8a129a 100644--- a/src/commands/step_commands.rs+++ b/src/commands/step_commands.rs@@ -25,7 +25,7 @@ use rayon::prelude::*;use worktrunk::HookType;use worktrunk::config::{CopyIgnoredConfig, UserConfig};use worktrunk::copy::{copy_dir_recursive, copy_leaf};-use worktrunk::git::Repository;+use worktrunk::git::{Repository, WorktreeInfo};use worktrunk::path::format_path_for_display;use worktrunk::shell_exec::Cmd;use worktrunk::styling::{@@ -1394,6 +1394,7 @@ pub fn step_prune(config: &UserConfig,foreground: bool,run_hooks: bool,+ worktrees: &[WorktreeInfo],) -> anyhow::Result<bool> {let target = match candidate.kind {CandidateKind::Current => RemoveTarget::Current,@@ -1419,6 +1420,7 @@ pub fn step_prune(false,config,None,+ Some(worktrees),) {Ok(plan) => plan,Err(_) => {@@ -1609,7 +1611,9 @@ pub fn step_prune(dry_run_info.push((candidate, info));} else if is_current {deferred_current = Some(candidate);- } else if try_remove(&candidate, &repo, &config, foreground, run_hooks)? {+ } else if try_remove(+ &candidate, &repo, &config, foreground, run_hooks, &worktrees,+ )? {removed.push(candidate);}continue;@@ -1660,7 +1664,9 @@ pub fn step_prune(suffix,};dry_run_info.push((candidate, info));- } else if try_remove(&candidate, &repo, &config, foreground, run_hooks)? {+ } else if try_remove(+ &candidate, &repo, &config, foreground, run_hooks, &worktrees,+ )? {removed.push(candidate);}}@@ -1731,7 +1737,7 @@ pub fn step_prune(// Remove deferred current worktree last (cd-to-primary happens here)if let Some(current) = deferred_current- && try_remove(¤t, &repo, &config, foreground, run_hooks)?+ && try_remove(¤t, &repo, &config, foreground, run_hooks, &worktrees)?{removed.push(current);}diff --git a/src/main.rs b/src/main.rsindex c66357cfa..b3baa4879 100644--- a/src/main.rs+++ b/src/main.rs@@ -628,6 +628,7 @@ fn validate_remove_targets(};let deletion_mode = BranchDeletionMode::from_flags(keep_branch, force_delete);+ let worktrees = repo.list_worktrees().ok();let mut plans = RemovePlans {others: Vec::new(),@@ -659,6 +660,7 @@ fn validate_remove_targets(force,config,None,+ worktrees.as_deref(),) {Ok(result) => plans.current = Some(result),Err(e) => plans.record_error(e),@@ -673,7 +675,14 @@ fn validate_remove_targets(} else {RemoveTarget::Path(&path_canonical)};- match repo.prepare_worktree_removal(target, deletion_mode, force, config, None) {+ match repo.prepare_worktree_removal(+ target,+ deletion_mode,+ force,+ config,+ None,+ worktrees.as_deref(),+ ) {Ok(result) => plans.others.push(result),Err(e) => plans.record_error(e),}@@ -685,6 +694,7 @@ fn validate_remove_targets(force,config,None,+ worktrees.as_deref(),) {Ok(result) => plans.branch_only.push(result),Err(e) => plans.record_error(e),@@ -754,6 +764,7 @@ fn handle_remove_command(args: RemoveArgs) -> anyhow::Result<()> {args.force,&config,None,+ None,).context("Failed to remove worktree")?;