Skip to content

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.md
index 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.rs
index 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 divergence
let 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 unknown
Some(&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.snap
index 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
@ main ^ ./repo a1e809f5 1d Initial commit
+ diverged ! ↑ 💬 +40 -60 ↑1 ./repo.diverged a86ce3fb 1d Diverged commit
+ feature-changes !? ↑ 🤖 +50 -100 ↑1 ./repo.feature-changes 38944dae 1d Add 100 lines
-+ fix ? _ 💬 ./repo.fix a1e809f5 1d Initial commit
++ fix ? – 💬 ./repo.fix a1e809f5 1d Initial commit
⚪ Showing 4 worktrees, 2 with changes, 2 ahead
diff --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.snap
index 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 -----
Branch Status HEAD± main↕ main…± Path Remote⇅ CI Commit Age Message
@ main ^ ./repo a1e809f5 1d Initial commit
-+ feature-a ? _ ./repo.feature-a a1e809f5 1d Initial commit
++ feature-a ? – ./repo.feature-a a1e809f5 1d Initial commit
+ feature-b ↑ ↑1 +1 -1 ./repo.feature-b 86bdffd3 1d Test commit
⚪ Showing 3 worktrees, 1 ahead
diff --git a/tests/snapshots/integration__integration_tests__list__with_user_marker.snap b/tests/snapshots/integration__integration_tests__list__with_user_marker.snap
index 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
@ main ^ ./repo a058e792 1d Initial commit
+ feature-api ↑ 🤖 ↑1 ./repo.feature-api 95e48b49 1d Add REST API endpoints
+ review-ui ? ↑ 💬 ↑1 ./repo.review-ui 46b6a187 1d Add dashboard component
-+ wip-docs ? _ ./repo.wip-docs a058e792 1d Initial commit
++ wip-docs ? – ./repo.wip-docs a058e792 1d Initial commit
⚪ Showing 4 worktrees, 2 ahead
diff --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.snap
index 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 -----
Branch Status HEAD± main↕ Path Remote⇅ Commit Age Message
@ main ^ ./repo a1e809f5 1d Initial commit
-+ huge ? _ ./repo.huge a1e809f5 1d Initial commit
++ huge ? – ./repo.huge a1e809f5 1d Initial commit
+ tiny ! – +1 -1 ./repo.tiny a1e809f5 1d Initial commit
⚪ Showing 3 worktrees, 1 with changes
diff --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.snap
index 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 -----
Branch Status HEAD± main↕ Path Remote⇅ Commit Age Message
@ main ^ ./repo a1e809f5 1d Initial commit
-+ feature-large ? _ ./repo.feature-large a1e809f5 1d Initial commit
-+ feature-medium ? _ ./repo.feature-medium a1e809f5 1d Initial commit
-+ feature-small ? _ ./repo.feature-small a1e809f5 1d Initial commit
++ feature-large ? – ./repo.feature-large a1e809f5 1d Initial commit
++ feature-medium ? – ./repo.feature-medium a1e809f5 1d Initial commit
++ feature-small ? – ./repo.feature-small a1e809f5 1d Initial commit
⚪ Showing 4 worktrees