Skip to content

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.rs
index 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.rs
index 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 goes
diff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_current_worktree.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_current_worktree.snap
index 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
◎ Removing current-merged worktree & branch in background (same commit as main, _)
▲ Cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
-✓ Pruned 1 worktree
+✓ Pruned 1 worktree (with branch)
diff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_dry_run.snap
index 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: 34
info:
program: wt
args:
@@ -46,4 +45,4 @@ exit_code: 0
----- stderr -----
○ merged-a — same commit as main
○ merged-b — same commit as main
-↳ 2 worktrees would be removed (dry run)
+↳ 2 worktrees (with branches) would be removed (dry run)
diff --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.snap
new file mode 100644
index 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 -----
+○ merged-a — same commit as main
+○ merged-b — same commit as main
+○ orphan-integrated — same commit as main
+↳ 2 worktrees (with branches), 1 branch would be removed (dry run)
diff --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.snap
index 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 -----
○ old-merged — same commit as main
-↳ 1 worktree would be removed (dry run)
+↳ 1 worktree (with branch) would be removed (dry run)
diff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_multiple.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_multiple.snap
index 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
◎ Removing merged-a worktree & branch in background (same commit as main, _)
◎ Removing merged-b worktree & branch in background (same commit as main, _)
◎ Removing merged-c worktree & branch in background (same commit as main, _)
-✓ Pruned 3 worktrees
+✓ Pruned 3 worktrees (with branches)
diff --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.snap
index 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
◎ Removing worktree @ _REPO_.detached-integrated in background (detached HEAD, no branch to delete)
○ No worktree found for branch detached-integrated
✓ Removed branch detached-integrated (same commit as main, _)
-✓ Pruned 1 worktree, 1 branch
+✓ Pruned 1 branch, 1 detached worktree
diff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_removes_merged.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_removes_merged.snap
index 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 -----
◎ Removing merged-branch worktree & branch in background (same commit as main, _)
-✓ Pruned 1 worktree
+✓ Pruned 1 worktree (with branch)
diff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_locked.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_locked.snap
index 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 -----
◎ Removing merged-branch worktree & branch in background (same commit as main, _)
-✓ Pruned 1 worktree
+✓ Pruned 1 worktree (with branch)
diff --git a/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged.snap b/tests/snapshots/integration__integration_tests__step_prune__prune_skips_unmerged.snap
index 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 -----
◎ Removing merged-one worktree & branch in background (same commit as main, _)
-✓ Pruned 1 worktree
+✓ Pruned 1 worktree (with branch)
diff --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.snap
index 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 -----
○ merged-branch — same commit as main
-↳ 1 worktree would be removed (dry run)
+↳ 1 worktree (with branch) would be removed (dry run)