Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Move `gather_list_data` to `RepositoryCliExt`
Maximilian Roos4c370ffa52a6
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rsindex cb7133249..b35254219 100644--- a/src/commands/list/mod.rs+++ b/src/commands/list/mod.rs@@ -9,9 +9,10 @@ mod spacing_test;#[cfg(test)]mod status_column_tests;+use super::repository_ext::RepositoryCliExt;use columns::ColumnKind;use layout::calculate_responsive_layout;-use model::{ListData, ListItem, gather_list_data};+use model::{ListData, ListItem};use render::{format_ci_status_plain, format_diff_plain, format_header_line, format_list_item_line,};@@ -89,7 +90,7 @@ pub fn handle_list(let Some(ListData {items,current_worktree_path,- }) = gather_list_data(&repo, show_branches, show_full, show_full)?+ }) = repo.gather_list_data(show_branches, show_full, show_full)?else {return Ok(());};diff --git a/src/commands/list/model.rs b/src/commands/list/model.rsindex f188edea8..5ef216f50 100644--- a/src/commands/list/model.rs+++ b/src/commands/list/model.rs@@ -1,7 +1,5 @@-use rayon::prelude::*;use std::path::PathBuf;use worktrunk::git::{GitError, Repository};-use worktrunk::styling::{HINT, HINT_EMOJI, WARNING, WARNING_BOLD, WARNING_EMOJI, println};use super::ci_status::PrStatus;@@ -267,7 +265,7 @@ impl ListItem {impl BranchInfo {/// Create BranchInfo from a branch name, enriching it with git metadata- fn from_branch(+ pub(crate) fn from_branch(branch: &str,repo: &Repository,primary_branch: Option<&str>,@@ -790,7 +788,7 @@ fn parse_git_status(impl WorktreeInfo {/// Create WorktreeInfo from a Worktree, enriching it with git metadata- fn from_worktree(+ pub(crate) fn from_worktree(wt: &worktrunk::git::Worktree,primary: &worktrunk::git::Worktree,fetch_ci: bool,@@ -955,107 +953,3 @@ impl WorktreeInfo {})}}--/// Gather list data (worktrees + optional branches).-pub fn gather_list_data(- repo: &Repository,- show_branches: bool,- fetch_ci: bool,- check_conflicts: bool,-) -> Result<Option<ListData>, GitError> {- let worktrees = repo.list_worktrees()?;-- if worktrees.worktrees.is_empty() {- return Ok(None);- }-- // Get primary worktree - clone it for use in closure- let primary = worktrees.worktrees[0].clone();-- // Get current worktree to identify active one- let current_worktree_path = repo.worktree_root().ok();-- // Gather enhanced information for all worktrees in parallel- let worktree_results: Vec<Result<WorktreeInfo, GitError>> = worktrees- .worktrees- .par_iter()- .map(|wt| WorktreeInfo::from_worktree(wt, &primary, fetch_ci, check_conflicts))- .collect();-- // Build list of items to display (worktrees + optional branches)- let mut items: Vec<ListItem> = Vec::new();-- // Process worktree results- for result in worktree_results {- match result {- Ok(info) => items.push(ListItem::Worktree(info)),- Err(e) => {- // Worktree enrichment failures are critical - propagate error- return Err(e);- }- }- }-- // Process branches in parallel if requested- if show_branches {- let available_branches = repo.available_branches()?;- let primary_branch = primary.branch.as_deref();-- let branch_results: Vec<(String, Result<BranchInfo, GitError>)> = available_branches- .par_iter()- .map(|branch| {- let result = BranchInfo::from_branch(- branch,- repo,- primary_branch,- fetch_ci,- check_conflicts,- );- (branch.clone(), result)- })- .collect();-- for (branch, result) in branch_results {- match result {- Ok(branch_info) => items.push(ListItem::Branch(branch_info)),- Err(e) => {- println!(- "{WARNING_EMOJI} {WARNING}Failed to enrich branch {WARNING_BOLD}{branch}{WARNING_BOLD:#}: {e}{WARNING:#}"- );- println!(- "{HINT_EMOJI} {HINT}This branch will be shown with limited information{HINT:#}"- );- }- }- }- }-- // Sort by:- // 1. Main worktree (primary) always first- // 2. Current worktree second (if not main)- // 3. Remaining worktrees by age (most recent first)- items.sort_by_key(|item| {- let is_primary = item.is_primary();- let is_current = item- .worktree_path()- .and_then(|p| current_worktree_path.as_ref().map(|cp| p == cp))- .unwrap_or(false);-- // Primary sort key: 0 = main, 1 = current (non-main), 2 = others- let priority = if is_primary {- 0- } else if is_current {- 1- } else {- 2- };-- // Secondary sort: timestamp (reversed for descending order)- (priority, std::cmp::Reverse(item.commit_timestamp()))- });-- Ok(Some(ListData {- items,- current_worktree_path,- }))-}diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rsindex 49f8c9b25..8db5ad744 100644--- a/src/commands/repository_ext.rs+++ b/src/commands/repository_ext.rs@@ -1,11 +1,16 @@+use rayon::prelude::*;use std::path::Path;++use super::list::model::{BranchInfo, ListData, ListItem, WorktreeInfo};use worktrunk::config::ProjectConfig;use worktrunk::git::{GitError, GitResultExt, Repository};use worktrunk::styling::{- ERROR, ERROR_EMOJI, HINT, HINT_BOLD, HINT_EMOJI, WARNING, format_with_gutter,+ ERROR, ERROR_EMOJI, HINT, HINT_BOLD, HINT_EMOJI, WARNING, WARNING_BOLD, WARNING_EMOJI,+ format_with_gutter, println,};-/// CLI-specific helpers that operate on `Repository` but live in the binary crate due to orphan rules.+/// CLI-only helpers implemented on [`Repository`] via an extension trait so we can keep orphan+/// implementations inside the binary crate.pub trait RepositoryCliExt {/// Load the project configuration if it exists.fn load_project_config(&self) -> Result<Option<ProjectConfig>, GitError>;@@ -15,6 +20,14 @@ pub trait RepositoryCliExt {/// Warn about untracked files being auto-staged.fn warn_if_auto_staging_untracked(&self) -> Result<(), GitError>;++ /// Gather enriched list data for worktrees (and optional branches).+ fn gather_list_data(+ &self,+ show_branches: bool,+ fetch_ci: bool,+ check_conflicts: bool,+ ) -> Result<Option<ListData>, GitError>;}impl RepositoryCliExt for Repository {@@ -64,6 +77,92 @@ impl RepositoryCliExt for Repository {Ok(())}++ fn gather_list_data(+ &self,+ show_branches: bool,+ fetch_ci: bool,+ check_conflicts: bool,+ ) -> Result<Option<ListData>, GitError> {+ let worktrees = self.list_worktrees()?;++ if worktrees.worktrees.is_empty() {+ return Ok(None);+ }++ let primary = worktrees.worktrees[0].clone();+ let current_worktree_path = self.worktree_root().ok();++ let worktree_results: Vec<Result<WorktreeInfo, GitError>> = worktrees+ .worktrees+ .par_iter()+ .map(|wt| WorktreeInfo::from_worktree(wt, &primary, fetch_ci, check_conflicts))+ .collect();++ let mut items = Vec::new();+ for result in worktree_results {+ match result {+ Ok(info) => items.push(ListItem::Worktree(info)),+ Err(e) => return Err(e),+ }+ }++ if show_branches {+ let available_branches = self.available_branches()?;+ let primary_branch = primary.branch.as_deref();++ let branch_results: Vec<(String, Result<BranchInfo, GitError>)> = available_branches+ .par_iter()+ .map(|branch| {+ let result = BranchInfo::from_branch(+ branch,+ self,+ primary_branch,+ fetch_ci,+ check_conflicts,+ );+ (branch.clone(), result)+ })+ .collect();++ for (branch, result) in branch_results {+ match result {+ Ok(info) => items.push(ListItem::Branch(info)),+ Err(e) => {+ println!(+ "{WARNING_EMOJI} {WARNING}Failed to enrich branch {WARNING_BOLD}{branch}{WARNING_BOLD:#}: {e}{WARNING:#}"+ );+ println!(+ "{HINT_EMOJI} {HINT}This branch will be shown with limited information{HINT:#}"+ );+ }+ }+ }+ }++ items.sort_by_key(|item| {+ let is_primary = item.is_primary();+ let is_current = item+ .worktree_path()+ .and_then(|p| current_worktree_path.as_ref().map(|cp| p == cp))+ .unwrap_or(false);++ let priority = if is_primary {+ 0+ } else if is_current {+ 1+ } else {+ 2+ };++ (priority, std::cmp::Reverse(item.commit_timestamp()))+ });++ Ok(Some(ListData {+ items,+ current_worktree_path,+ }))+ }}fn load_project_config_at(repo_root: &Path) -> Result<Option<ProjectConfig>, GitError> {diff --git a/src/commands/select.rs b/src/commands/select.rsindex 596ddef87..a59060365 100644--- a/src/commands/select.rs+++ b/src/commands/select.rs@@ -6,7 +6,8 @@ use std::sync::Arc;use worktrunk::config::WorktrunkConfig;use worktrunk::git::{GitError, GitResultExt, Repository};-use super::list::model::{ListItem, gather_list_data};+use super::list::model::ListItem;+use super::repository_ext::RepositoryCliExt;use super::worktree::handle_switch;use crate::output::handle_switch_output;@@ -230,7 +231,7 @@ pub fn handle_select() -> Result<(), GitError> {let _state = PreviewState::new();// Gather list data using existing logic- let Some(list_data) = gather_list_data(&repo, false, false, false)? else {+ let Some(list_data) = repo.gather_list_data(false, false, false)? else {return Ok(());};