Skip to content

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

Refactor: Embed `worktrunk::git::Worktree` in `WorktreeInfo`

Maximilian Roosfa2e67e2b828
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index ae7b4e638..b6dbba09e 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -54,7 +54,12 @@ pub fn calculate_column_widths(infos: &[WorktreeInfo]) -> ColumnWidths {
for info in infos {
// Branch name
- let branch_len = info.branch.as_deref().unwrap_or("(detached)").width();
+ let branch_len = info
+ .worktree
+ .branch
+ .as_deref()
+ .unwrap_or("(detached)")
+ .width();
max_branch = max_branch.max(branch_len);
// Time
@@ -120,7 +125,10 @@ pub fn calculate_column_widths(infos: &[WorktreeInfo]) -> ColumnWidths {
/// Calculate responsive layout based on terminal width
pub fn calculate_responsive_layout(infos: &[WorktreeInfo]) -> LayoutConfig {
let terminal_width = get_terminal_width();
- let paths: Vec<&Path> = infos.iter().map(|info| info.path.as_path()).collect();
+ let paths: Vec<&Path> = infos
+ .iter()
+ .map(|info| info.worktree.path.as_path())
+ .collect();
let common_prefix = find_common_prefix(&paths);
// Count how many rows have each sparse column
@@ -154,10 +162,10 @@ pub fn calculate_responsive_layout(infos: &[WorktreeInfo]) -> LayoutConfig {
.iter()
.filter(|i| {
i.worktree_state.is_some()
- || (i.detached && i.branch.is_some())
- || i.bare
- || i.locked.is_some()
- || i.prunable.is_some()
+ || (i.worktree.detached && i.worktree.branch.is_some())
+ || i.worktree.bare
+ || i.worktree.locked.is_some()
+ || i.worktree.prunable.is_some()
})
.count();
@@ -279,9 +287,15 @@ mod tests {
use crate::commands::list::WorktreeInfo;
let info1 = WorktreeInfo {
- path: PathBuf::from("/test"),
- head: "abc123".to_string(),
- branch: Some("main".to_string()),
+ worktree: worktrunk::git::Worktree {
+ path: PathBuf::from("/test"),
+ head: "abc123".to_string(),
+ branch: Some("main".to_string()),
+ bare: false,
+ detached: false,
+ locked: None,
+ prunable: None,
+ },
timestamp: 0,
commit_message: "Test".to_string(),
ahead: 3,
@@ -289,10 +303,6 @@ mod tests {
working_tree_diff: (100, 50),
branch_diff: (200, 30),
is_primary: false,
- detached: false,
- bare: false,
- locked: None,
- prunable: None,
upstream_remote: Some("origin".to_string()),
upstream_ahead: 4,
upstream_behind: 0,
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index c3b67b4f8..3ba7431c1 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -8,9 +8,7 @@ use layout::calculate_responsive_layout;
use render::{format_header_line, format_worktree_line};
pub struct WorktreeInfo {
- pub path: std::path::PathBuf,
- pub head: String,
- pub branch: Option<String>,
+ pub worktree: worktrunk::git::Worktree,
pub timestamp: i64,
pub commit_message: String,
pub ahead: usize,
@@ -18,10 +16,6 @@ pub struct WorktreeInfo {
pub working_tree_diff: (usize, usize),
pub branch_diff: (usize, usize),
pub is_primary: bool,
- pub detached: bool,
- pub bare: bool,
- pub locked: Option<String>,
- pub prunable: Option<String>,
pub upstream_remote: Option<String>,
pub upstream_ahead: usize,
pub upstream_behind: usize,
@@ -30,7 +24,7 @@ pub struct WorktreeInfo {
impl WorktreeInfo {
/// Create WorktreeInfo from a Worktree, enriching it with git metadata
- fn from_worktree(wt: &worktrunk::git::Worktree, primary: &worktrunk::git::Worktree) -> Self {
+ fn from_worktree(wt: worktrunk::git::Worktree, primary: &worktrunk::git::Worktree) -> Self {
let wt_repo = Repository::at(&wt.path);
let is_primary = wt.path == primary.path;
@@ -84,9 +78,7 @@ impl WorktreeInfo {
let worktree_state = wt_repo.worktree_state().unwrap_or(None);
WorktreeInfo {
- path: wt.path.clone(),
- head: wt.head.clone(),
- branch: wt.branch.clone(),
+ worktree: wt,
timestamp,
commit_message,
ahead,
@@ -94,10 +86,6 @@ impl WorktreeInfo {
working_tree_diff,
branch_diff,
is_primary,
- detached: wt.detached,
- bare: wt.bare,
- locked: wt.locked.clone(),
- prunable: wt.prunable.clone(),
upstream_remote,
upstream_ahead,
upstream_behind,
@@ -114,8 +102,8 @@ pub fn handle_list() -> Result<(), GitError> {
return Ok(());
}
- // First worktree is the primary
- let primary = &worktrees[0];
+ // First worktree is the primary - clone it for use in closure
+ let primary = worktrees[0].clone();
// Get current worktree to identify active one
let current_worktree_path = repo.worktree_root().ok();
@@ -135,14 +123,14 @@ pub fn handle_list() -> Result<(), GitError> {
let mut infos: Vec<WorktreeInfo> = if std::env::var("WT_SEQUENTIAL").is_ok() {
// Sequential iteration (for benchmarking)
worktrees
- .iter()
- .map(|wt| WorktreeInfo::from_worktree(wt, primary))
+ .into_iter()
+ .map(|wt| WorktreeInfo::from_worktree(wt, &primary))
.collect()
} else {
// Parallel iteration (default)
worktrees
- .par_iter()
- .map(|wt| WorktreeInfo::from_worktree(wt, primary))
+ .into_par_iter()
+ .map(|wt| WorktreeInfo::from_worktree(wt, &primary))
.collect()
};
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index b43bc4dbe..11abb8f63 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -13,20 +13,20 @@ pub fn format_all_states(info: &WorktreeInfo) -> String {
}
// Don't show detached state if branch is None (already shown in branch column)
- if info.detached && info.branch.is_some() {
+ if info.worktree.detached && info.worktree.branch.is_some() {
states.push("(detached)".to_string());
}
- if info.bare {
+ if info.worktree.bare {
states.push("(bare)".to_string());
}
- if let Some(ref reason) = info.locked {
+ if let Some(ref reason) = info.worktree.locked {
if reason.is_empty() {
states.push("(locked)".to_string());
} else {
states.push(format!("(locked: {})", reason));
}
}
- if let Some(ref reason) = info.prunable {
+ if let Some(ref reason) = info.worktree.prunable {
if reason.is_empty() {
states.push("(prunable)".to_string());
} else {
@@ -134,12 +134,12 @@ pub fn format_worktree_line(
let yellow_style = theme.neutral;
let dim_style = theme.dim;
- let branch_display = info.branch.as_deref().unwrap_or("(detached)");
- let short_head = &info.head[..8.min(info.head.len())];
+ let branch_display = info.worktree.branch.as_deref().unwrap_or("(detached)");
+ let short_head = &info.worktree.head[..8.min(info.worktree.head.len())];
// Determine styles: current worktree is bold magenta, primary is cyan
let is_current = current_worktree_path
- .map(|p| p == &info.path)
+ .map(|p| p == &info.worktree.path)
.unwrap_or(false);
let text_style = match (is_current, info.is_primary) {
(true, _) => Some(current_style),
@@ -289,7 +289,7 @@ pub fn format_worktree_line(
}
// Path (no padding needed, it's the last column, use shortened path)
- let path_str = shorten_path(&info.path, &layout.common_prefix);
+ let path_str = shorten_path(&info.worktree.path, &layout.common_prefix);
if let Some(style) = text_style {
line.push_styled(path_str, style);
} else {
@@ -311,9 +311,15 @@ mod tests {
fn test_column_alignment_with_all_columns() {
// Create test data with all columns populated
let info = WorktreeInfo {
- path: PathBuf::from("/test/path"),
- head: "abc12345".to_string(),
- branch: Some("test-branch".to_string()),
+ worktree: worktrunk::git::Worktree {
+ path: PathBuf::from("/test/path"),
+ head: "abc12345".to_string(),
+ branch: Some("test-branch".to_string()),
+ bare: false,
+ detached: false,
+ locked: Some("test lck".to_string()), // "(locked: test lck)" = 18 chars
+ prunable: None,
+ },
timestamp: 0,
commit_message: "Test message".to_string(),
ahead: 3,
@@ -321,10 +327,6 @@ mod tests {
working_tree_diff: (100, 50),
branch_diff: (200, 30),
is_primary: false,
- detached: false,
- bare: false,
- locked: Some("test lck".to_string()), // "(locked: test lck)" = 18 chars
- prunable: None,
upstream_remote: Some("origin".to_string()),
upstream_ahead: 4,
upstream_behind: 0,
@@ -467,7 +469,7 @@ mod tests {
));
data.push_raw(" ");
// Path
- data.push_raw(shorten_path(&info.path, &layout.common_prefix));
+ data.push_raw(shorten_path(&info.worktree.path, &layout.common_prefix));
// Verify both lines have columns at the same positions
// We'll check this by verifying specific column start positions
@@ -482,7 +484,7 @@ mod tests {
// (Path is variable width, so we only check up to there)
let header_width_without_path = header.width() - "Path".len();
let data_width_without_path =
- data.width() - shorten_path(&info.path, &layout.common_prefix).len();
+ data.width() - shorten_path(&info.worktree.path, &layout.common_prefix).len();
assert_eq!(
header_width_without_path, data_width_without_path,