Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor populate_items to handle single item
Maximilian Roosd44c39fef09c
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rsindex 8cb255bb9..91924e775 100644--- a/src/commands/list/collect.rs+++ b/src/commands/list/collect.rs@@ -372,7 +372,7 @@ fn apply_default(items: &mut [ListItem], status_contexts: &mut [StatusContext],////// Callers decide how to handle timeout:/// - `collect()`: Shows user-facing diagnostic (interactive command)-/// - `populate_items()`: Logs silently (used by statusline)+/// - `populate_item()`: Logs silently (used by statusline)fn drain_results(rx: chan::Receiver<Result<TaskResult, TaskError>>,items: &mut [ListItem],@@ -1225,7 +1225,7 @@ pub use super::collect_progressive_impl::CollectOptions;/// Build a ListItem for a single worktree with identity fields only.///-/// Computed fields (counts, diffs, CI) are left as None. Use `populate_items()`+/// Computed fields (counts, diffs, CI) are left as None. Use `populate_item()`/// to fill them in.pub fn build_worktree_item(wt: &Worktree,@@ -1269,75 +1269,59 @@ pub fn build_worktree_item(////// This is the blocking version used by statusline. For progressive rendering/// with callbacks, see the `collect()` function.-pub fn populate_items(- items: &mut [ListItem],+pub fn populate_item(+ item: &mut ListItem,default_branch: &str,target: &str,options: CollectOptions,) -> anyhow::Result<()> {use std::sync::Arc;- if items.is_empty() {+ // Extract worktree data (skip if not a worktree item)+ let Some(data) = item.worktree_data() else {return Ok(());- }+ };// Create channel for task resultslet (tx, rx) = chan::unbounded::<Result<TaskResult, TaskError>>();- // Track expected results per item (populated at spawn time)+ // Track expected results (populated at spawn time)let expected_results = Arc::new(ExpectedResults::default());// Collect errors (logged silently for statusline)let mut errors: Vec<TaskError> = Vec::new();- // Collect worktree info: (index, path, head, branch)- let worktree_info: Vec<_> = items- .iter()- .enumerate()- .filter_map(|(idx, item)| {- item.worktree_data().map(|data| {- (- idx,- data.path.clone(),- item.head.clone(),- item.branch.clone(),- )- })- })- .collect();-- // Spawn collection in background thread+ // Extract data for background thread (can't send borrows across threads)+ let wt = Worktree {+ path: data.path.clone(),+ head: item.head.clone(),+ branch: item.branch.clone(),+ bare: false,+ detached: false,+ locked: None,+ prunable: None,+ };let default_branch_clone = default_branch.to_string();let target_clone = target.to_string();let expected_results_clone = expected_results.clone();++ // Spawn collection in background threadstd::thread::spawn(move || {- for (idx, path, head, branch) in worktree_info {- // Create a minimal Worktree struct for the collection function- let wt = Worktree {- path,- head,- branch,- bare: false,- detached: false,- locked: None,- prunable: None,- };- super::collect_progressive_impl::collect_worktree_progressive(- &wt,- idx,- &default_branch_clone,- &target_clone,- &options,- tx.clone(),- &expected_results_clone,- );- }+ super::collect_progressive_impl::collect_worktree_progressive(+ &wt,+ 0, // Single item, always index 0+ &default_branch_clone,+ &target_clone,+ &options,+ tx,+ &expected_results_clone,+ );});- // Drain task results (blocking until all complete)+ // Drain task results (blocking until complete)let drain_outcome = drain_results(rx,- items,+ std::slice::from_mut(item),&mut errors,&expected_results,|_item_idx, item, ctx| {@@ -1347,12 +1331,12 @@ pub fn populate_items(// Handle timeout (silent for statusline - just log it)if let DrainOutcome::TimedOut { received_count, .. } = drain_outcome {- log::warn!("populate_items timed out after 30s ({received_count} results received)");+ log::warn!("populate_item timed out after 30s ({received_count} results received)");}// Log errors silently (statusline shouldn't spam warnings)if !errors.is_empty() {- log::warn!("populate_items had {} task errors", errors.len());+ log::warn!("populate_item had {} task errors", errors.len());for error in &errors {let kind_str: &'static str = error.kind.into();log::debug!(@@ -1365,9 +1349,7 @@ pub fn populate_items(}// Populate display fields (including status_line for statusline command)- for item in items.iter_mut() {- item.finalize_display();- }+ item.finalize_display();Ok(())}diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rsindex 1ea3bc0da..6a9791985 100644--- a/src/commands/list/mod.rs+++ b/src/commands/list/mod.rs@@ -137,7 +137,7 @@ use progressive::RenderMode;use worktrunk::git::Repository;// Re-export for statusline and other consumers-pub use collect::{CollectOptions, build_worktree_item, populate_items};+pub use collect::{CollectOptions, build_worktree_item, populate_item};pub fn handle_list(format: crate::OutputFormat,diff --git a/src/commands/statusline.rs b/src/commands/statusline.rsindex eb71bdc43..7aec194e0 100644--- a/src/commands/statusline.rs+++ b/src/commands/statusline.rs@@ -243,21 +243,21 @@ fn get_git_status(repo: &Repository, cwd: &Path) -> Result<Option<String>> {let is_main = wt.path == main_worktree.path;// Build item with identity fields- let mut items = vec![list::build_worktree_item(wt, is_main, true, false)];+ let mut item = list::build_worktree_item(wt, is_main, true, false);// Populate computed fields (parallel git operations) and format status_line// Compute everything (same as --full) for complete status symbols// Pass default_branch for stable informational stats,// and integration_target for integration status checks.- list::populate_items(- &mut items,+ list::populate_item(+ &mut item,&default_branch,&integration_target,CollectOptions::default(),)?;// Return the pre-formatted statusline- if let Some(ref statusline) = items[0].display.statusline {+ if let Some(ref statusline) = item.display.statusline {Ok(Some(statusline.clone()))} else {// Fallback: just show branch name