Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Block integration when untracked files present
Maximilian Roosabee28e718a1
diff --git a/docs/content/claude-code.md b/docs/content/claude-code.mdindex 04e5868a7..f40c2b1f4 100644--- a/docs/content/claude-code.md+++ b/docs/content/claude-code.md@@ -20,7 +20,7 @@ The plugin adds status indicators to `wt list`:@ <b>main</b> <span class=d>^</span> <b>./repo</b> <span class=d>a058e792</span> <span class=d>1d</span> <span class=d>Initial commit</span>+ feature-api <span class=d>↑</span> 🤖 <span class=g>↑1</span> ./repo.feature-api <span class=d>95e48b49</span> <span class=d>1d</span> <span class=d>Add REST API endpoints</span>+ review-ui <span class=c>?</span> <span class=d>↑</span> 💬 <span class=g>↑1</span> ./repo.review-ui <span class=d>46b6a187</span> <span class=d>1d</span> <span class=d>Add dashboard component</span>-+ <span class=d>wip-docs</span> <span class=c>?</span> <span class=d>_</span> <span class=d>./repo.wip-docs</span> <span class=d>a058e792</span> <span class=d>1d</span> <span class=d>Initial commit</span>++ wip-docs <span class=c>?</span> <span class=d>–</span> ./repo.wip-docs <span class=d>a058e792</span> <span class=d>1d</span> <span class=d>Initial commit</span>⚪ <span class=d>Showing 4 worktrees, 2 ahead</span>{% end %}diff --git a/src/commands/list/model.rs b/src/commands/list/model.rsindex 8dbd2bd25..7a46c4973 100644--- a/src/commands/list/model.rs+++ b/src/commands/list/model.rs@@ -496,16 +496,19 @@ impl ListItem {self.would_merge_add,data.working_tree_diff.as_ref(),&data.working_tree_diff_with_main,+ working_tree_status.is_some_and(|s| s.untracked),);// Separately detect SameCommit: same commit as main but with uncommitted work// This is NOT an integration state (has work that would be lost on delete)+ let has_tracked_changes = data+ .working_tree_diff+ .as_ref()+ .is_some_and(|d| !d.is_empty());+ let has_untracked = working_tree_status.is_some_and(|s| s.untracked);let is_same_commit_dirty = self.is_ancestor == Some(true)&& self.counts.as_ref().is_some_and(|c| c.behind == 0)- && data- .working_tree_diff- .as_ref()- .is_some_and(|d| !d.is_empty());+ && (has_tracked_changes || has_untracked);// Compute main state: combines is_main, would_conflict, integration, and divergencelet main_state = MainState::from_integration_and_counts(@@ -542,6 +545,7 @@ impl ListItem {self.would_merge_add,Some(&empty_diff), // branches are always "clean" (no working tree)&None, // no working_tree_diff_with_main for branches+ false, // branches have no working tree, can't have untracked files);// Compute main state@@ -587,14 +591,17 @@ fn check_integration_state(would_merge_add: Option<bool>,working_tree_diff: Option<&LineDiff>,working_tree_diff_with_main: &Option<Option<LineDiff>>,+ has_untracked: bool,) -> Option<MainState> {if is_main || default_branch.is_none() {return None;}- // Require working_tree_diff to be loaded and empty. Don't assume clean when unknown- // to avoid premature integration state (which would cause UI flash during progressive loading).- let is_clean = working_tree_diff.is_some_and(|d| d.is_empty());+ // Require working_tree_diff to be loaded and empty, and no untracked files.+ // Don't assume clean when unknown to avoid premature integration state+ // (which would cause UI flash during progressive loading).+ // Untracked files would be lost on worktree removal, so they block integration.+ let is_clean = working_tree_diff.is_some_and(|d| d.is_empty()) && !has_untracked;// Priority 1: Same commit as main + clean working tree = Empty (safe to delete)if is_ancestor == Some(true) && behind_main == Some(0) && is_clean {@@ -1516,6 +1523,7 @@ mod tests {None, // would_merge_add unknownSome(&dirty_working_tree), // has uncommitted changes&working_tree_matches_main, // working tree matches main+ false, // no untracked files),None,"Priority 5 should reject dirty working tree"@@ -1533,9 +1541,62 @@ mod tests {None,Some(&clean_working_tree),&working_tree_matches_main,+ false, // no untracked files),Some(MainState::Integrated(IntegrationReason::TreesMatch)),"Priority 5 should accept clean working tree");}++ #[test]+ fn test_check_integration_state_untracked_blocks_integration() {+ // Untracked files should block integration even when tracked files are clean.+ // This prevents incorrectly suggesting a worktree is safe to remove when+ // it contains untracked files that would be lost.++ let clean_working_tree = LineDiff {+ added: 0,+ deleted: 0,+ };+ let working_tree_matches_main = Some(Some(LineDiff {+ added: 0,+ deleted: 0,+ }));++ // Clean tracked files but has untracked: should NOT return Integrated+ assert_eq!(+ check_integration_state(+ false,+ Some("main"),+ Some(false),+ None,+ false,+ None,+ None,+ Some(&clean_working_tree),+ &working_tree_matches_main,+ true, // has untracked files+ ),+ None,+ "Untracked files should block integration"+ );++ // Same scenario without untracked: SHOULD return Integrated+ assert_eq!(+ check_integration_state(+ false,+ Some("main"),+ Some(false),+ None,+ false,+ None,+ None,+ Some(&clean_working_tree),+ &working_tree_matches_main,+ false, // no untracked files+ ),+ Some(MainState::Integrated(IntegrationReason::TreesMatch)),+ "Without untracked files, should show as integrated"+ );+ }}diff --git a/tests/snapshots/integration__integration_tests__list__large_diffs_alignment.snap b/tests/snapshots/integration__integration_tests__list__large_diffs_alignment.snapindex a3e59526e..904861e50 100644--- a/tests/snapshots/integration__integration_tests__list__large_diffs_alignment.snap+++ b/tests/snapshots/integration__integration_tests__list__large_diffs_alignment.snap@@ -31,6 +31,6 @@ exit_code: 0@ [1mmain[0m [2m^[22m [1m./repo[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit+ diverged [36m![39m [2m↑[22m 💬 [32m+40[0m [31m-60[0m [32m↑1[0m ./repo.diverged [2ma86ce3fb[0m [2m1d[0m [2mDiverged commit+ feature-changes [36m![39m[36m?[39m [2m↑[22m 🤖 [32m+50[0m [31m-100[0m [32m↑1[0m ./repo.feature-changes [2m38944dae[0m [2m1d[0m [2mAdd 100 lines-+ [2mfix[0m [36m?[39m [2m_[22m 💬 [2m./repo.fix[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ fix [36m?[39m [2m–[22m 💬 ./repo.fix [2ma1e809f5[0m [2m1d[0m [2mInitial commit⚪ [2mShowing 4 worktrees, 2 with changes, 2 aheaddiff --git a/tests/snapshots/integration__integration_tests__list__task_dag_full_with_diffs.snap b/tests/snapshots/integration__integration_tests__list__task_dag_full_with_diffs.snapindex 529b7b5d4..8f4f69b33 100644--- a/tests/snapshots/integration__integration_tests__list__task_dag_full_with_diffs.snap+++ b/tests/snapshots/integration__integration_tests__list__task_dag_full_with_diffs.snap@@ -31,7 +31,7 @@ exit_code: 0----- stderr -----[1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mmain…±[0m [1mPath[0m [1mRemote⇅[0m [1mCI[0m [1mCommit[0m [1mAge[0m [1mMessage@ [1mmain[0m [2m^[22m [1m./repo[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit-+ [2mfeature-a[0m [36m?[39m [2m_[22m [2m./repo.feature-a[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ feature-a [36m?[39m [2m–[22m ./repo.feature-a [2ma1e809f5[0m [2m1d[0m [2mInitial commit+ feature-b [2m↑[22m [32m↑1[0m [32m+1[0m [31m-1[0m ./repo.feature-b [2m86bdffd3[0m [2m1d[0m [2mTest commit⚪ [2mShowing 3 worktrees, 1 aheaddiff --git a/tests/snapshots/integration__integration_tests__list__with_user_marker.snap b/tests/snapshots/integration__integration_tests__list__with_user_marker.snapindex 3defb4e40..c8d56d02f 100644--- a/tests/snapshots/integration__integration_tests__list__with_user_marker.snap+++ b/tests/snapshots/integration__integration_tests__list__with_user_marker.snap@@ -31,6 +31,6 @@ exit_code: 0@ [1mmain[0m [2m^[22m [1m./repo[0m [2ma058e792[0m [2m1d[0m [2mInitial commit+ feature-api [2m↑[22m 🤖 [32m↑1[0m ./repo.feature-api [2m95e48b49[0m [2m1d[0m [2mAdd REST API endpoints+ review-ui [36m?[39m [2m↑[22m 💬 [32m↑1[0m ./repo.review-ui [2m46b6a187[0m [2m1d[0m [2mAdd dashboard component-+ [2mwip-docs[0m [36m?[39m [2m_[22m [2m./repo.wip-docs[0m [2ma058e792[0m [2m1d[0m [2mInitial commit++ wip-docs [36m?[39m [2m–[22m ./repo.wip-docs [2ma058e792[0m [2m1d[0m [2mInitial commit⚪ [2mShowing 4 worktrees, 2 aheaddiff --git a/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_extreme_diffs.snap b/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_extreme_diffs.snapindex a9684c4d3..771be0dcf 100644--- a/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_extreme_diffs.snap+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_extreme_diffs.snap@@ -29,7 +29,7 @@ exit_code: 0----- stderr -----[1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mPath[0m [1mRemote⇅[0m [1mCommit[0m [1mAge[0m [1mMessage@ [1mmain[0m [2m^[22m [1m./repo[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit-+ [2mhuge[0m [36m?[39m [2m_[22m [2m./repo.huge[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ huge [36m?[39m [2m–[22m ./repo.huge [2ma1e809f5[0m [2m1d[0m [2mInitial commit+ tiny [36m![39m [2m–[22m [32m+1[0m [31m-1[0m ./repo.tiny [2ma1e809f5[0m [2m1d[0m [2mInitial commit⚪ [2mShowing 3 worktrees, 1 with changesdiff --git a/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_varying_diffs.snap b/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_varying_diffs.snapindex 9a9519a8c..e41f7c88a 100644--- a/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_varying_diffs.snap+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__alignment_varying_diffs.snap@@ -29,8 +29,8 @@ exit_code: 0----- stderr -----[1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mPath[0m [1mRemote⇅[0m [1mCommit[0m [1mAge[0m [1mMessage@ [1mmain[0m [2m^[22m [1m./repo[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit-+ [2mfeature-large[0m [36m?[39m [2m_[22m [2m./repo.feature-large[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit-+ [2mfeature-medium[0m [36m?[39m [2m_[22m [2m./repo.feature-medium[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit-+ [2mfeature-small[0m [36m?[39m [2m_[22m [2m./repo.feature-small[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ feature-large [36m?[39m [2m–[22m ./repo.feature-large [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ feature-medium [36m?[39m [2m–[22m ./repo.feature-medium [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ feature-small [36m?[39m [2m–[22m ./repo.feature-small [2ma1e809f5[0m [2m1d[0m [2mInitial commit⚪ [2mShowing 4 worktrees