Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: improve prune summary to distinguish candidate types (#1232)
Maximilian Rooseef38e57c89f
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rsindex 8f02932fa..18a3d52a5 100644--- a/src/commands/step_commands.rs+++ b/src/commands/step_commands.rs@@ -1316,29 +1316,46 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->BranchOnly,}- /// Build a human-readable count like "2 worktrees, 1 branch".- fn count_summary(candidates: &[Candidate]) -> String {- let worktree_count = candidates- .iter()- .filter(|c| !matches!(c.kind, CandidateKind::BranchOnly))- .count();- let branch_count = candidates.len() - worktree_count;+ /// Build a human-readable count like "2 worktrees (with branches), 1 branch".+ fn prune_summary(candidates: &[Candidate]) -> String {+ let mut worktree_with_branch = 0;+ let mut branch_only = 0;+ let mut detached_worktree = 0;+ for c in candidates {+ match (&c.kind, &c.branch) {+ (CandidateKind::BranchOnly, _) => branch_only += 1,+ (CandidateKind::Current | CandidateKind::Other, Some(_)) => {+ worktree_with_branch += 1;+ }+ (CandidateKind::Current | CandidateKind::Other, None) => {+ detached_worktree += 1;+ }+ }+ }let mut parts = Vec::new();- if worktree_count > 0 {- let noun = if worktree_count == 1 {- "worktree"+ if worktree_with_branch > 0 {+ let noun = if worktree_with_branch == 1 {+ "worktree (with branch)"} else {- "worktrees"+ "worktrees (with branches)"};- parts.push(format!("{worktree_count} {noun}"));+ parts.push(format!("{worktree_with_branch} {noun}"));}- if branch_count > 0 {- let noun = if branch_count == 1 {+ if branch_only > 0 {+ let noun = if branch_only == 1 {"branch"} else {"branches"};- parts.push(format!("{branch_count} {noun}"));+ parts.push(format!("{branch_only} {noun}"));+ }+ if detached_worktree > 0 {+ let noun = if detached_worktree == 1 {+ "detached worktree"+ } else {+ "detached worktrees"+ };+ parts.push(format!("{detached_worktree} {noun}"));}parts.join(", ")}@@ -1510,10 +1527,12 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->info_message(cformat!("<bold>{}</> — {}", c.label, c.reason_desc,)));}- let summary = count_summary(&candidates);eprintln!("{}",- hint_message(format!("{summary} would be removed (dry run)"))+ hint_message(format!(+ "{} would be removed (dry run)",+ prune_summary(&candidates)+ )));return Ok(());}@@ -1541,12 +1560,16 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->for c in &candidates {let target = match c.kind {CandidateKind::Current => RemoveTarget::Current,- CandidateKind::BranchOnly => {- RemoveTarget::Branch(c.branch.as_ref().expect("BranchOnly has branch"))- }+ CandidateKind::BranchOnly => RemoveTarget::Branch(+ c.branch+ .as_ref()+ .context("BranchOnly candidate missing branch")?,+ ),CandidateKind::Other => match &c.branch {Some(branch) => RemoveTarget::Branch(branch),- None => RemoveTarget::Path(c.path.as_ref().expect("detached has path")),+ None => {+ RemoveTarget::Path(c.path.as_ref().context("detached candidate missing path")?)+ }},};let plan =@@ -1559,8 +1582,10 @@ pub fn step_prune(dry_run: bool, yes: bool, min_age: &str, foreground: bool) ->handle_remove_output(result, !foreground, run_hooks)?;}- let summary = count_summary(&candidates);- eprintln!("{}", success_message(format!("Pruned {summary}")));+ eprintln!(+ "{}",+ success_message(format!("Pruned {}", prune_summary(&candidates)))+ );Ok(())}diff --git a/tests/integration_tests/step_prune.rs b/tests/integration_tests/step_prune.rsindex f398bf1dd..aad0981b9 100644--- a/tests/integration_tests/step_prune.rs+++ b/tests/integration_tests/step_prune.rs@@ -329,6 +329,29 @@ fn test_prune_min_age_passes(mut repo: TestRepo) {assert_cmd_snapshot!(cmd);}+/// Dry-run with mixed worktrees + orphan branches shows both counts.+///+/// Exercises the "N worktrees, M branches would be removed (dry run)" path+/// where the summary must distinguish worktree candidates from branch-only+/// candidates.+#[rstest]+fn test_prune_dry_run_mixed_worktrees_and_branches(mut repo: TestRepo) {+ repo.commit("initial");++ // Two worktrees at same commit as main (integrated)+ repo.add_worktree("merged-a");+ repo.add_worktree("merged-b");++ // One orphan branch (integrated, no worktree)+ repo.create_branch("orphan-integrated");++ // Far-future epoch so everything passes the age guard+ let mut cmd = make_snapshot_cmd(&repo, "step", &["prune", "--dry-run"], None);+ cmd.env("WORKTRUNK_TEST_EPOCH", "1893456000"); // 2030-01-01++ assert_cmd_snapshot!(cmd);+}+/// Stale candidate + young worktrees: shows both the candidate and skipped count.////// A stale worktree (directory deleted) bypasses the age check because it goesdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_current_worktree.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_current_worktree.snapindex 3418e31e7..b8767a361 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_current_worktree.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_current_worktree.snap@@ -46,4 +46,4 @@ exit_code: 0[36m◎ Removing [1mcurrent-merged[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m[33m▲[39m [33mCannot change directory — shell integration not installed[39m[2m↳[22m [2mTo enable automatic cd, run [90mwt config shell install[39m[22m-[32m✓[39m [32mPruned 1 worktree[39m+[32m✓[39m [32mPruned 1 worktree (with branch)[39mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run.snapindex 26a676ba9..872a85d49 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run.snap@@ -1,6 +1,5 @@---source: tests/integration_tests/step_prune.rs-assertion_line: 34info:program: wtargs:@@ -46,4 +45,4 @@ exit_code: 0----- stderr -----[2m○[22m [1mmerged-a[22m — same commit as main[2m○[22m [1mmerged-b[22m — same commit as main-[2m↳[22m [2m2 worktrees would be removed (dry run)[22m+[2m↳[22m [2m2 worktrees (with branches) would be removed (dry run)[22mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run_mixed_worktrees_and_branches.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run_mixed_worktrees_and_branches.snapnew file mode 100644index 000000000..f83c36632--- /dev/null+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run_mixed_worktrees_and_branches.snap@@ -0,0 +1,48 @@+---+source: tests/integration_tests/step_prune.rs+info:+ program: wt+ args:+ - step+ - prune+ - "--dry-run"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ PATH: "[PATH]"+ PSModulePath: ""+ RUST_LOG: warn+ SHELL: ""+ TERM: alacritty+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"+ WORKTRUNK_TEST_EPOCH: "1893456000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[2m○[22m [1mmerged-a[22m — same commit as main+[2m○[22m [1mmerged-b[22m — same commit as main+[2m○[22m [1morphan-integrated[22m — same commit as main+[2m↳[22m [2m2 worktrees (with branches), 1 branch would be removed (dry run)[22mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_min_age_passes.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_min_age_passes.snapindex 7c217c814..730521f72 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_min_age_passes.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_min_age_passes.snap@@ -43,4 +43,4 @@ exit_code: 0----- stderr -----[2m○[22m [1mold-merged[22m — same commit as main-[2m↳[22m [2m1 worktree would be removed (dry run)[22m+[2m↳[22m [2m1 worktree (with branch) would be removed (dry run)[22mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_multiple.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_multiple.snapindex 463fde594..9b00ab266 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_multiple.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_multiple.snap@@ -46,4 +46,4 @@ exit_code: 0[36m◎ Removing [1mmerged-a[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m[36m◎ Removing [1mmerged-b[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m[36m◎ Removing [1mmerged-c[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m-[32m✓[39m [32mPruned 3 worktrees[39m+[32m✓[39m [32mPruned 3 worktrees (with branches)[39mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_removes_integrated_detached.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_removes_integrated_detached.snapindex 7cf0c1537..0248952e8 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_removes_integrated_detached.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_removes_integrated_detached.snap@@ -46,4 +46,4 @@ exit_code: 0[36m◎[39m [36mRemoving worktree @ [1m_REPO_.detached-integrated[22m in background (detached HEAD, no branch to delete)[39m[2m○[22m No worktree found for branch [1mdetached-integrated[22m[32m✓ Removed branch [1mdetached-integrated[22m (same commit as [1mmain[22m,[39m [2m_[22m[32m)[39m-[32m✓[39m [32mPruned 1 worktree, 1 branch[39m+[32m✓[39m [32mPruned 1 branch, 1 detached worktree[39mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_removes_merged.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_removes_merged.snapindex f79a2e47a..6d1dca9f3 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_removes_merged.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_removes_merged.snap@@ -44,4 +44,4 @@ exit_code: 0----- stderr -----[36m◎ Removing [1mmerged-branch[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m-[32m✓[39m [32mPruned 1 worktree[39m+[32m✓[39m [32mPruned 1 worktree (with branch)[39mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_locked.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_locked.snapindex f79a2e47a..6d1dca9f3 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_locked.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_locked.snap@@ -44,4 +44,4 @@ exit_code: 0----- stderr -----[36m◎ Removing [1mmerged-branch[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m-[32m✓[39m [32mPruned 1 worktree[39m+[32m✓[39m [32mPruned 1 worktree (with branch)[39mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged.snapindex 8ab9fca58..5cedffea7 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged.snap@@ -44,4 +44,4 @@ exit_code: 0----- stderr -----[36m◎ Removing [1mmerged-one[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m-[32m✓[39m [32mPruned 1 worktree[39m+[32m✓[39m [32mPruned 1 worktree (with branch)[39mdiff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged_detached.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged_detached.snapindex 63c5e70c6..4c3459d61 100644--- a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged_detached.snap+++ b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged_detached.snap@@ -44,4 +44,4 @@ exit_code: 0----- stderr -----[2m○[22m [1mmerged-branch[22m — same commit as main-[2m↳[22m [2m1 worktree would be removed (dry run)[22m+[2m↳[22m [2m1 worktree (with branch) would be removed (dry run)[22m