Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: combine pruned worktree + branch deletion into single output line (#1211)
Maximilian Roos04595014dacb
diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex c096ef255..135ad0780 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -653,28 +653,15 @@ fn handle_branch_only_output(deletion_mode: BranchDeletionMode,pruned: bool,) -> anyhow::Result<()> {- // Different message depending on whether we pruned a stale worktree- if pruned {- // Worktree was pruned - informational, not a warning- eprintln!(- "{}",- info_message(cformat!(- "Worktree directory missing for <bold>{branch_name}</>; pruned"- ))- );+ let branch_info = if pruned {+ cformat!("Worktree directory missing for <bold>{branch_name}</>; pruned")} else {- // No worktree at all, but branch exists - informational since branch removal will proceed- eprintln!(- "{}",- info_message(cformat!(- "No worktree found for branch <bold>{branch_name}</>"- ))- );- }+ cformat!("No worktree found for branch <bold>{branch_name}</>")+ };- // Attempt branch deletion (unless --no-delete-branch was specified)+ // If we won't delete the branch, show info and return earlyif deletion_mode.should_keep() {- // User explicitly requested no branch deletion - nothing more to do+ eprintln!("{}", info_message(&branch_info));stderr().flush()?;return Ok(());}@@ -687,9 +674,28 @@ fn handle_branch_only_output(let check_target = default_branch.as_deref().unwrap_or("HEAD");let result = delete_branch_if_safe(&repo, branch_name, check_target, deletion_mode.is_force());- let (deletion, _) = handle_branch_deletion_result(result, branch_name, false)?;+ // Defer "retained" output so we control message ordering (info before retained)+ let (deletion, deferred) = handle_branch_deletion_result(result, branch_name, true)?;- if !matches!(deletion.outcome, BranchDeletionOutcome::NotDeleted) {+ if matches!(deletion.outcome, BranchDeletionOutcome::NotDeleted) {+ // Print info first, then deferred "retained" + hint+ eprintln!("{}", info_message(&branch_info));+ if deferred {+ eprintln!(+ "{}",+ info_message(cformat!(+ "Branch <bold>{branch_name}</> retained; has unmerged changes"+ ))+ );+ let cmd = suggest_command("remove", &[branch_name], &["-D"]);+ eprintln!(+ "{}",+ hint_message(cformat!(+ "To delete the unmerged branch, run <bright-black>{cmd}</>"+ ))+ );+ }+ } else {let flag_note = get_flag_note(deletion_mode,&deletion.outcome,@@ -697,12 +703,24 @@ fn handle_branch_only_output();let flag_text = &flag_note.text;let flag_after = flag_note.after_green();- eprintln!(- "{}",- FormattedMessage::new(cformat!(- "<green>✓ Removed branch <bold>{branch_name}</>{flag_text}</>{flag_after}"- ))- );++ if pruned {+ // Combined: pruned stale metadata & deleted branch in one line+ eprintln!(+ "{}",+ FormattedMessage::new(cformat!(+ "<green>✓ Pruned stale worktree & removed branch <bold>{branch_name}</>{flag_text}</>{flag_after}"+ ))+ );+ } else {+ eprintln!("{}", info_message(&branch_info));+ eprintln!(+ "{}",+ FormattedMessage::new(cformat!(+ "<green>✓ Removed branch <bold>{branch_name}</>{flag_text}</>{flag_after}"+ ))+ );+ }}stderr().flush()?;diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rsindex 3e9763d2f..12bd38ee7 100644--- a/tests/integration_tests/remove.rs+++ b/tests/integration_tests/remove.rs@@ -1740,7 +1740,6 @@ fn test_remove_pruned_worktree_directory_missing(mut repo: TestRepo) {);// `wt remove feature-pruned` should prune the stale metadata and delete the branch- // The info message should say "Worktree directory missing for feature-pruned; pruned stale metadata"assert_cmd_snapshot!(make_snapshot_cmd(&repo,"remove",@@ -1805,6 +1804,49 @@ fn test_remove_pruned_worktree_keep_branch(mut repo: TestRepo) {);}+/// Test pruning a stale worktree with an unmerged branch: should prune metadata,+/// retain branch, and show hint to force-delete+#[rstest]+fn test_remove_pruned_worktree_unmerged_branch(mut repo: TestRepo) {+ // Create a worktree with a real change (unmerged with main)+ let worktree_path = repo.add_worktree("feature-pruned-unmerged");+ std::fs::write(worktree_path.join("unmerged.txt"), "unmerged work\n").unwrap();+ repo.git_command()+ .args(["add", "unmerged.txt"])+ .current_dir(&worktree_path)+ .output()+ .unwrap();+ repo.git_command()+ .args(["commit", "-m", "unmerged work"])+ .current_dir(&worktree_path)+ .output()+ .unwrap();++ // Delete the worktree directory externally (simulating user running `rm -rf`)+ std::fs::remove_dir_all(&worktree_path).expect("Failed to remove worktree directory");++ // Remove: should prune stale metadata but retain the unmerged branch+ assert_cmd_snapshot!(make_snapshot_cmd(+ &repo,+ "remove",+ &["feature-pruned-unmerged"],+ None+ ));++ // Verify the branch still exists (retained because unmerged)+ let branch_exists = repo+ .git_command()+ .args(["branch", "--list", "feature-pruned-unmerged"])+ .output()+ .unwrap();+ assert!(+ !String::from_utf8_lossy(&branch_exists.stdout)+ .trim()+ .is_empty(),+ "Unmerged branch should be retained"+ );+}+// ============================================================================// Instant Removal Tests (move-then-delete optimization)// ============================================================================diff --git a/tests/snapshots/integration__integration_tests__remove__remove_pruned_worktree_directory_missing.snap b/tests/snapshots/integration__integration_tests__remove__remove_pruned_worktree_directory_missing.snapindex d52f6383f..fc043b259 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_pruned_worktree_directory_missing.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_pruned_worktree_directory_missing.snap@@ -25,10 +25,13 @@ info:SHELL: ""TERM: alacrittyUSERPROFILE: "[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: "1735776000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"@@ -38,5 +41,4 @@ exit_code: 0----- stdout ---------- stderr ------[2m○[22m Worktree directory missing for [1mfeature-pruned[22m; pruned-[32m✓ Removed branch [1mfeature-pruned[22m (same commit as [1mmain[22m,[39m [2m_[22m[32m)[39m+[32m✓ Pruned stale worktree & removed branch [1mfeature-pruned[22m (same commit as [1mmain[22m,[39m [2m_[22m[32m)[39mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_pruned_worktree_unmerged_branch.snap b/tests/snapshots/integration__integration_tests__remove__remove_pruned_worktree_unmerged_branch.snapnew file mode 100644index 000000000..05534ed82--- /dev/null+++ b/tests/snapshots/integration__integration_tests__remove__remove_pruned_worktree_unmerged_branch.snap@@ -0,0 +1,46 @@+---+source: tests/integration_tests/remove.rs+info:+ program: wt+ args:+ - remove+ - feature-pruned-unmerged+ 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: "1735776000"+ 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 Worktree directory missing for [1mfeature-pruned-unmerged[22m; pruned+[2m○[22m Branch [1mfeature-pruned-unmerged[22m retained; has unmerged changes+[2m↳[22m [2mTo delete the unmerged branch, run [90mwt remove -D feature-pruned-unmerged[39m[22m