Skip to content

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

fix: Update layout.rs tests to use Option type for working_tree_diff_with_main

Maximilian Roos4ac0dcd9abf8
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index 4deb09f1e..12d15fe0c 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -674,7 +674,7 @@ mod tests {
behind: 2,
},
working_tree_diff: (100, 50),
- working_tree_diff_with_main: (0, 0),
+ working_tree_diff_with_main: Some((0, 0)),
branch_diff: BranchDiffTotals { diff: (200, 30) },
is_primary: false,
upstream: UpstreamStatus::from_parts(Some("origin".to_string()), 4, 0),
@@ -748,7 +748,7 @@ mod tests {
behind: 10,
},
working_tree_diff: (100, 50),
- working_tree_diff_with_main: (0, 0),
+ working_tree_diff_with_main: Some((0, 0)),
branch_diff: BranchDiffTotals { diff: (200, 30) },
is_primary: false,
upstream: UpstreamStatus::from_parts(Some("origin".to_string()), 4, 2),
@@ -821,7 +821,7 @@ mod tests {
behind: 0,
},
working_tree_diff: (0, 0),
- working_tree_diff_with_main: (0, 0),
+ working_tree_diff_with_main: Some((0, 0)),
branch_diff: BranchDiffTotals { diff: (0, 0) },
is_primary: true, // Primary worktree: no ahead/behind shown
upstream: UpstreamStatus::default(),
@@ -895,7 +895,7 @@ mod tests {
behind: 0,
},
working_tree_diff: (0, 0), // Hidden: no dirty changes
- working_tree_diff_with_main: (0, 0),
+ working_tree_diff_with_main: Some((0, 0)),
branch_diff: BranchDiffTotals { diff: (0, 0) }, // Hidden: no diff
is_primary: true, // Hidden: no ahead/behind for primary
upstream: UpstreamStatus::default(), // Hidden: no upstream
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index 40a08a2d3..23ed99105 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -27,7 +27,12 @@ pub struct WorktreeInfo {
#[serde(flatten)]
pub counts: AheadBehind,
pub working_tree_diff: (usize, usize),
- pub working_tree_diff_with_main: (usize, usize),
+ /// Diff between working tree and main branch.
+ /// `None` means "not computed" (optimization: skipped when trees differ).
+ /// `Some((0, 0))` means working tree matches main exactly.
+ /// `Some((a, d))` means a lines added, d deleted vs main.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub working_tree_diff_with_main: Option<(usize, usize)>,
#[serde(flatten)]
pub branch_diff: BranchDiffTotals,
pub is_primary: bool,
@@ -318,11 +323,46 @@ impl WorktreeInfo {
let base_branch = primary.branch.as_deref().filter(|_| !is_primary);
let counts = AheadBehind::compute(&wt_repo, base_branch, &wt.head)?;
- let working_tree_diff = wt_repo.working_tree_diff_stats()?;
+ // Fast dirty check - only compute expensive numstat if working tree is dirty
+ let is_dirty = wt_repo
+ .run_command(&["diff-index", "--quiet", "HEAD", "--"])
+ .is_err(); // Exit code 1 = dirty
+
+ let working_tree_diff = if is_dirty {
+ wt_repo.working_tree_diff_stats()?
+ } else {
+ (0, 0) // Clean working tree
+ };
+
+ // Use tree equality check instead of expensive diff for "matches main"
let working_tree_diff_with_main = if let Some(base) = base_branch {
- wt_repo.working_tree_diff_vs_ref(base)?
+ // Get tree hashes for HEAD and base branch
+ let head_tree = wt_repo
+ .run_command(&["rev-parse", "HEAD^{tree}"])?
+ .trim()
+ .to_string();
+ let base_tree = wt_repo
+ .run_command(&["rev-parse", &format!("{}^{{tree}}", base)])?
+ .trim()
+ .to_string();
+
+ if head_tree == base_tree {
+ // Trees are identical - check if working tree is also clean
+ if is_dirty {
+ // Rare case: trees match but working tree has uncommitted changes
+ // Need to compute actual diff to get accurate line counts
+ Some(wt_repo.working_tree_diff_vs_ref(base)?)
+ } else {
+ // Trees match and working tree is clean → matches main exactly
+ Some((0, 0))
+ }
+ } else {
+ // Trees differ - skip the expensive scan
+ // Return None to indicate "not computed" (optimization)
+ None
+ }
} else {
- (0, 0)
+ Some((0, 0)) // Primary worktree always matches itself
};
let branch_diff = BranchDiffTotals::compute(&wt_repo, base_branch, &wt.head)?;
let upstream = UpstreamStatus::calculate(&wt_repo, wt.branch.as_deref(), &wt.head)?;
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index b360a5813..25e77ce09 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -207,7 +207,7 @@ pub fn format_all_states(item: &ListItem) -> String {
if let Some(info) = item.worktree_info() {
// State priority: "matches main" takes precedence over "no commits"
// since it's more specific (working tree identical to main)
- if !info.is_primary && info.working_tree_diff_with_main == (0, 0) {
+ if !info.is_primary && info.working_tree_diff_with_main == Some((0, 0)) {
states.push("(matches main)".to_string());
} else if !info.is_primary && item.counts().ahead == 0 && info.working_tree_diff == (0, 0) {
// Only show "no commits" if working tree doesn't match main
@@ -270,7 +270,7 @@ fn optional_reason_state(label: &str, reason: Option<&str>) -> Option<String> {
/// Dims when (using OR logic):
/// - No commits AND clean working tree (ahead == 0 AND working_tree_diff == (0, 0)):
/// The worktree has no commits ahead and no uncommitted changes
-/// - Working tree matches main (working_tree_diff_with_main == (0, 0)):
+/// - Working tree matches main (working_tree_diff_with_main == Some((0, 0))):
/// The working tree contents are identical to main, regardless of commit history
///
/// Either condition alone is sufficient to dim, as both indicate "no unique work here".
@@ -286,7 +286,7 @@ fn is_potentially_removable(item: &ListItem) -> bool {
let no_commits_and_clean = counts.ahead == 0 && info.working_tree_diff == (0, 0);
// Condition 2: Working tree matches main (regardless of commit history)
- let matches_main = info.working_tree_diff_with_main == (0, 0);
+ let matches_main = info.working_tree_diff_with_main == Some((0, 0));
no_commits_and_clean || matches_main
} else {
diff --git a/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap b/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap
index 6ce2b48df..68f2cd70f 100644
--- a/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap
+++ b/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap
@@ -1,5 +1,6 @@
---
source: tests/integration_tests/list.rs
+assertion_line: 73
info:
program: wt
args:
@@ -16,7 +17,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpE2ELVC/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpTx0Jtg/test-config.toml
---
success: true
exit_code: 0
@@ -76,10 +77,6 @@ exit_code: 0
1,
1
],
- "working_tree_diff_with_main": [
- 2,
- 1
- ],
"branch_diff": [
1,
0
@@ -114,10 +111,6 @@ exit_code: 0
0,
0
],
- "working_tree_diff_with_main": [
- 1,
- 1
- ],
"branch_diff": [
0,
0