Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor branch deletion output and improve unmerged branch messaging
Maximilian Roosae7c2c6160af
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex e639f6aa9..3a0fe2828 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -526,7 +526,7 @@ pub fn handle_remove(// Show progress (unless running in background - output handler will show command)if !background {crate::output::print(progress_message(cformat!(- "Removing worktree for <bold>{worktree_name}</>..."+ "Removing <bold>{worktree_name}</> worktree...")))?;}diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex 60fb72aa1..4bec92d27 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -219,22 +219,28 @@ fn delete_branch_if_safe(/// - `NotDeleted`: We checked and chose not to delete (not integrated) - show info/// - `Err(e)`: Git command failed - show warning with actual error///-/// Returns the `BranchDeletionResult` on success, preserving the effective target.+/// Returns (result, needs_hint) where needs_hint indicates the caller should print+/// the unmerged branch hint after any success message.+///+/// When `defer_output` is true, info and hint are suppressed (caller will handle).fn handle_branch_deletion_result(result: anyhow::Result<BranchDeletionResult>,branch_name: &str,-) -> anyhow::Result<BranchDeletionResult> {- match &result {- Ok(r) if !matches!(r.outcome, BranchDeletionOutcome::NotDeleted) => result,- Ok(_) => {+ defer_output: bool,+) -> anyhow::Result<(BranchDeletionResult, bool)> {+ match result {+ Ok(r) if !matches!(r.outcome, BranchDeletionOutcome::NotDeleted) => Ok((r, false)),+ Ok(r) => {// Branch not integrated - we chose not to delete (not a failure)- super::print(info_message(cformat!(- "Branch <bold>{branch_name}</> retained; has unmerged changes"- )))?;- super::print(hint_message(cformat!(- "<bright-black>wt remove -D</> deletes unmerged branches"- )))?;- result+ if !defer_output {+ super::print(info_message(cformat!(+ "Branch <bold>{branch_name}</> retained; has unmerged changes"+ )))?;+ super::print(hint_message(cformat!(+ "<bright-black>wt remove -D</> deletes unmerged branches"+ )))?;+ }+ Ok((r, defer_output))}Err(e) => {// Git command failed - this is an error (we decided to delete but couldn't)@@ -242,7 +248,7 @@ fn handle_branch_deletion_result("Failed to delete branch <bold>{branch_name}</>")))?;super::gutter(format_with_gutter(&e.to_string(), "", None))?;- result+ Err(e)}}}@@ -298,10 +304,18 @@ fn format_remove_worktree_message(outcome,BranchDeletionOutcome::ForceDeleted | BranchDeletionOutcome::Integrated(_));- let action_suffix = if no_delete_branch || !branch_deleted {++ // Determine action suffix based on what happened to the branch+ let action_suffix = if branch_deleted {+ "worktree & branch"+ } else if no_delete_branch {+ // User explicitly kept the branch via --no-delete-branch"worktree"+ } else if matches!(outcome, BranchDeletionOutcome::NotDeleted) {+ // Branch retained because it has unmerged changes+ "worktree; retaining unmerged branch"} else {- "worktree & branch"+ "worktree"};if changed_directory {@@ -508,7 +522,7 @@ 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, force_delete);- let deletion = handle_branch_deletion_result(result, branch_name)?;+ let (deletion, _) = handle_branch_deletion_result(result, branch_name, false)?;if !matches!(deletion.outcome, BranchDeletionOutcome::NotDeleted) {let flag_note = get_flag_note(@@ -696,19 +710,20 @@ fn handle_removed_worktree_output(// Delete the branch (unless --no-delete-branch was specified)// Only show effective_target in message if we had a meaningful target (not tautological "HEAD" fallback)- let (outcome, effective_target) = if !no_delete_branch {+ let (outcome, effective_target, show_hint) = if !no_delete_branch {let deletion_repo = worktrunk::git::Repository::at(main_path);let check_target = target_branch.unwrap_or("HEAD");let result =delete_branch_if_safe(&deletion_repo, branch_name, check_target, force_delete);- let deletion = handle_branch_deletion_result(result, branch_name)?;+ let (deletion, needs_hint) = handle_branch_deletion_result(result, branch_name, true)?;// Only use effective_target for display if we had a real target (not "HEAD" fallback)let display_target = target_branch.map(|_| deletion.effective_target);- (deletion.outcome, display_target)+ (deletion.outcome, display_target, needs_hint)} else {(BranchDeletionOutcome::NotDeleted,target_branch.map(String::from),+ false,)};@@ -723,6 +738,13 @@ fn handle_removed_worktree_output(effective_target.as_deref(),)))?;+ // Show hint for unmerged branches (after success message)+ if show_hint {+ super::print(hint_message(cformat!(+ "<bright-black>wt remove -D</> deletes unmerged branches"+ )))?;+ }+super::flush()?;Ok(())}diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rsindex 88b6376ad..d8a459253 100644--- a/tests/integration_tests/remove.rs+++ b/tests/integration_tests/remove.rs@@ -255,6 +255,32 @@ fn test_remove_foreground(mut repo: TestRepo) {);}+#[rstest]+fn test_remove_foreground_unmerged(mut repo: TestRepo) {+ // Create a worktree with an unmerged commit+ let worktree_path = repo.add_worktree("feature-unmerged-fg");++ // Add a commit to the feature branch that's not in main+ std::fs::write(worktree_path.join("feature.txt"), "new feature").unwrap();+ repo.git_command(&["add", "feature.txt"])+ .current_dir(&worktree_path)+ .output()+ .unwrap();+ repo.git_command(&["commit", "-m", "Add feature"])+ .current_dir(&worktree_path)+ .output()+ .unwrap();++ // Remove it with --no-background flag from main repo+ // Branch deletion should fail but worktree removal should succeed+ snapshot_remove(+ "remove_foreground_unmerged",+ &repo,+ &["--no-background", "feature-unmerged-fg"],+ None,+ );+}+#[rstest]fn test_remove_no_delete_branch(mut repo: TestRepo) {// Create a worktreediff --git a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_executes.snap b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_executes.snapindex 07293275d..a54797eb8 100644--- a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_executes.snap+++ b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_executes.snap@@ -1,5 +1,6 @@---source: tests/integration_tests/remove.rs+assertion_line: 28info:program: wtargs:@@ -29,7 +30,7 @@ exit_code: 0----- stdout ---------- stderr ------π [36mRemoving worktree for [1mfeature-hook[22m...[39m+π [36mRemoving [1mfeature-hook[22m worktree...[39mπ [36mRunning project pre-remove:[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'About to remove worktree'[0m[2m[0mAbout to remove worktreediff --git a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_failure_aborts.snap b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_failure_aborts.snapindex 964eeee90..247c3bd51 100644--- a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_failure_aborts.snap+++ b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_failure_aborts.snap@@ -1,5 +1,6 @@---source: tests/integration_tests/remove.rs+assertion_line: 28info:program: wtargs:@@ -7,6 +8,7 @@ info:- "--no-background"- feature-failenv:+ APPDATA: "[TEST_CONFIG_HOME]"CLICOLOR_FORCE: "1"COLUMNS: "150"GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"@@ -14,17 +16,21 @@ info:GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"GIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""+ HOME: "[TEST_HOME]"LANG: CLC_ALL: C+ PATH: "[PATH]"SOURCE_DATE_EPOCH: "1735776000"+ USERPROFILE: "[TEST_HOME]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"---success: falseexit_code: 1----- stdout ---------- stderr ------π [36mRemoving worktree for [1mfeature-fail[22m...[39m+π [36mRemoving [1mfeature-fail[22m worktree...[39mπ [36mRunning project pre-remove:[39m[107m [0m [2m[0m[2m[34mexit[0m[2m 1[0mβ [31mpre-remove command failed: exit status: 1[39mdiff --git a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_skipped_with_no_verify.snap b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_skipped_with_no_verify.snapindex 9828b07c7..6076566b8 100644--- a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_skipped_with_no_verify.snap+++ b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_skipped_with_no_verify.snap@@ -1,5 +1,6 @@---source: tests/integration_tests/remove.rs+assertion_line: 28info:program: wtargs:@@ -30,5 +31,5 @@ exit_code: 0----- stdout ---------- stderr ------π [36mRemoving worktree for [1mfeature-skip[22m...[39m+π [36mRemoving [1mfeature-skip[22m worktree...[39mβ [32mRemoved [1mfeature-skip[22m worktree & branch (same commit as main)[39mdiff --git a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_template_variables.snap b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_template_variables.snapindex 275f92baa..e596f1a3e 100644--- a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_template_variables.snap+++ b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_template_variables.snap@@ -1,5 +1,6 @@---source: tests/integration_tests/remove.rs+assertion_line: 28info:program: wtargs:@@ -29,7 +30,7 @@ exit_code: 0----- stdout ---------- stderr ------π [36mRemoving worktree for [1mfeature-templates[22m...[39m+π [36mRemoving [1mfeature-templates[22m worktree...[39mπ [36mRunning project pre-remove [1mbranch[22m:[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'Branch: feature-templates'[0m[2m[0mBranch: feature-templatesdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_default_branch_no_tautology.snap b/tests/snapshots/integration__integration_tests__remove__remove_default_branch_no_tautology.snapindex 00b7571f0..87008ad05 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_default_branch_no_tautology.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_default_branch_no_tautology.snap@@ -1,5 +1,6 @@---source: tests/integration_tests/remove.rs+assertion_line: 640info:program: wtargs:@@ -24,5 +25,5 @@ exit_code: 0----- stdout ---------- stderr ------π [36mRemoving worktree for [1mmain[22m...[39m+π [36mRemoving [1mmain[22m worktree...[39mβ [32mRemoved [1mmain[22m worktree & branch[39mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_foreground.snap b/tests/snapshots/integration__integration_tests__remove__remove_foreground.snapindex 1b3a1ba55..9f719d178 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_foreground.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_foreground.snap@@ -1,5 +1,6 @@---source: tests/integration_tests/remove.rs+assertion_line: 28info:program: wtargs:@@ -29,5 +30,5 @@ exit_code: 0----- stdout ---------- stderr ------π [36mRemoving worktree for [1mfeature-fg[22m...[39m+π [36mRemoving [1mfeature-fg[22m worktree...[39mβ [32mRemoved [1mfeature-fg[22m worktree & branch (same commit as main)[39mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_foreground_unmerged.snap b/tests/snapshots/integration__integration_tests__remove__remove_foreground_unmerged.snapnew file mode 100644index 000000000..01f468d39--- /dev/null+++ b/tests/snapshots/integration__integration_tests__remove__remove_foreground_unmerged.snap@@ -0,0 +1,35 @@+---+source: tests/integration_tests/remove.rs+assertion_line: 28+info:+ program: wt+ args:+ - remove+ - "--no-background"+ - feature-unmerged-fg+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ 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: ""+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ PATH: "[PATH]"+ SOURCE_DATE_EPOCH: "1735776000"+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+π [36mRemoving [1mfeature-unmerged-fg[22m worktree...[39m+β [32mRemoved [1mfeature-unmerged-fg[22m worktree; retaining unmerged branch[39m+π‘ [2m[90mwt remove -D[39m deletes unmerged branches[22m