Skip to content

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.rs
index 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.rs
index 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.rs
index 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 worktree
diff --git a/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_executes.snap b/tests/snapshots/integration__integration_tests__remove__pre_remove_hook_executes.snap
index 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: 28
info:
program: wt
args:
@@ -29,7 +30,7 @@ exit_code: 0
----- stdout -----
----- stderr -----
-πŸ”„ Removing worktree for feature-hook...
+πŸ”„ Removing feature-hook worktree...
πŸ”„ Running project pre-remove:
  echo 'About to remove worktree'
About to remove worktree
diff --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.snap
index 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: 28
info:
program: wt
args:
@@ -7,6 +8,7 @@ info:
- "--no-background"
- feature-fail
env:
+ 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/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: false
exit_code: 1
----- stdout -----
----- stderr -----
-πŸ”„ Removing worktree for feature-fail...
+πŸ”„ Removing feature-fail worktree...
πŸ”„ Running project pre-remove:
  exit 1
❌ pre-remove command failed: exit status: 1
diff --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.snap
index 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: 28
info:
program: wt
args:
@@ -30,5 +31,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-πŸ”„ Removing worktree for feature-skip...
+πŸ”„ Removing feature-skip worktree...
βœ… Removed feature-skip worktree & branch (same commit as main)
diff --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.snap
index 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: 28
info:
program: wt
args:
@@ -29,7 +30,7 @@ exit_code: 0
----- stdout -----
----- stderr -----
-πŸ”„ Removing worktree for feature-templates...
+πŸ”„ Removing feature-templates worktree...
πŸ”„ Running project pre-remove branch:
  echo 'Branch: feature-templates'
Branch: feature-templates
diff --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.snap
index 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: 640
info:
program: wt
args:
@@ -24,5 +25,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-πŸ”„ Removing worktree for main...
+πŸ”„ Removing main worktree...
βœ… Removed main worktree & branch
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_foreground.snap b/tests/snapshots/integration__integration_tests__remove__remove_foreground.snap
index 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: 28
info:
program: wt
args:
@@ -29,5 +30,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-πŸ”„ Removing worktree for feature-fg...
+πŸ”„ Removing feature-fg worktree...
βœ… Removed feature-fg worktree & branch (same commit as main)
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_foreground_unmerged.snap b/tests/snapshots/integration__integration_tests__remove__remove_foreground_unmerged.snap
new file mode 100644
index 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 -----
+πŸ”„ Removing feature-unmerged-fg worktree...
+βœ… Removed feature-unmerged-fg worktree; retaining unmerged branch
+πŸ’‘ wt remove -D deletes unmerged branches