Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor(remove): Move removal logic to output handler

Maximilian Roosc78506d02b88
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 212d98153..2437eb75b 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -7,7 +7,7 @@ use worktrunk::styling::{
};
use super::command_executor::{CommandContext, prepare_project_commands};
-use super::worktree::{handle_push, handle_remove, parse_diff_shortstat};
+use super::worktree::{handle_push, parse_diff_shortstat};
use crate::output::execute_command_in_worktree;
/// Extract untracked files from git status --porcelain output
@@ -155,7 +155,9 @@ pub fn handle_merge(
// STEP 4: Only NOW remove the worktree (after all checks passed)
crate::output::progress(format!("πŸ”„ {CYAN}Cleaning up worktree...{CYAN:#}"))?;
- handle_remove(None)?;
+ let worktree_root = repo.worktree_root()?;
+ repo.remove_worktree(&worktree_root)
+ .git_context("Failed to remove worktree")?;
// Print comprehensive summary
crate::output::progress("")?;
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 4f5a8caa7..c2e36b844 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -134,12 +134,14 @@ impl SwitchResult {
pub enum RemoveResult {
/// Already on default branch, no action taken
AlreadyOnDefault(String),
- /// Removed worktree and returned to primary
- RemovedWorktree { primary_path: PathBuf },
+ /// Removed worktree and returned to primary (if needed)
+ RemovedWorktree {
+ primary_path: PathBuf,
+ worktree_path: PathBuf,
+ changed_directory: bool,
+ },
/// Switched to default branch in main repo
SwitchedToDefault(String),
- /// Removed a different worktree (not the current one)
- RemovedOtherWorktree { branch: String },
}
pub fn handle_switch(
@@ -251,12 +253,11 @@ fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError>
let worktree_root = repo.worktree_root()?;
let primary_worktree_dir = repo.main_worktree_root()?;
- // Remove the worktree (fail fast if this fails)
- repo.remove_worktree(&worktree_root)
- .git_context("Failed to remove worktree")?;
-
+ // Return paths - deletion will happen in output handler after cd directive is emitted
Ok(RemoveResult::RemovedWorktree {
primary_path: primary_worktree_dir,
+ worktree_path: worktree_root,
+ changed_directory: true, // We're in the worktree being removed
})
} else {
// In main repo: check if already on default branch
@@ -305,26 +306,20 @@ fn remove_worktree_by_name(repo: &Repository, branch_name: &str) -> Result<Remov
let current_worktree = repo.worktree_root()?;
let removing_current = current_worktree == worktree_path;
- // Get primary worktree path BEFORE removing (while we can still run git commands)
- let primary_worktree_dir = if removing_current {
- Some(repo.main_worktree_root()?)
+ // Return paths for all cases - deletion happens in output handler
+ let (primary_path, changed_directory) = if removing_current {
+ // Removing current worktree - will cd to main worktree
+ (repo.main_worktree_root()?, true)
} else {
- None
+ // Removing different worktree - stay in current location
+ (repo.worktree_root()?, false)
};
- // Remove the worktree (fail fast if this fails)
- repo.remove_worktree(&worktree_path)
- .git_context("Failed to remove worktree")?;
-
- // If we removed the current worktree, return to primary
- if let Some(primary_path) = primary_worktree_dir {
- Ok(RemoveResult::RemovedWorktree { primary_path })
- } else {
- // Stay where we are (no directory change needed)
- Ok(RemoveResult::RemovedOtherWorktree {
- branch: branch_name.to_string(),
- })
- }
+ Ok(RemoveResult::RemovedWorktree {
+ primary_path,
+ worktree_path,
+ changed_directory,
+ })
}
/// Check for conflicting uncommitted changes in target worktree
diff --git a/src/main.rs b/src/main.rs
index ed42a39d7..aaa201822 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -490,7 +490,7 @@ fn main() {
Commands::Remove { worktrees } => {
if worktrees.is_empty() {
// No worktrees specified, remove current worktree
- handle_remove(None).and_then(|result| handle_remove_output(&result))
+ handle_remove(None).and_then(|result| handle_remove_output(&result, None))
} else {
// When removing multiple worktrees, we need to handle the current worktree last
// to avoid deleting the directory we're currently in
@@ -516,30 +516,17 @@ fn main() {
}
}
- // Remove others first
+ // Remove others first, then current last
+ // Progress messages shown by handle_remove_output for all cases
for worktree in others.iter() {
- // Show progress before starting removal
- use worktrunk::styling::CYAN;
- let cyan_bold = CYAN.bold();
- output::progress(format!(
- "πŸ”„ {CYAN}Removing worktree for {cyan_bold}{worktree}{cyan_bold:#}...{CYAN:#}"
- ))?;
-
let result = handle_remove(Some(worktree.as_str()))?;
- handle_remove_output(&result)?;
+ handle_remove_output(&result, Some(worktree.as_str()))?;
}
// Remove current worktree last (if it was in the list)
if let Some(current_name) = current {
- // Show progress before starting removal
- use worktrunk::styling::CYAN;
- let cyan_bold = CYAN.bold();
- output::progress(format!(
- "πŸ”„ {CYAN}Removing worktree for {cyan_bold}{current_name}{cyan_bold:#}...{CYAN:#}"
- ))?;
-
let result = handle_remove(Some(current_name.as_str()))?;
- handle_remove_output(&result)?;
+ handle_remove_output(&result, Some(current_name.as_str()))?;
}
Ok(())
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index e497d98b0..acd0265c7 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -2,7 +2,7 @@
use crate::commands::worktree::{RemoveResult, SwitchResult};
use crate::output::global::format_switch_success;
-use worktrunk::git::GitError;
+use worktrunk::git::{GitError, GitResultExt};
/// Format message for switch operation (mode-specific via output system)
fn format_switch_message(result: &SwitchResult, branch: &str) -> String {
@@ -22,9 +22,10 @@ fn format_switch_message(result: &SwitchResult, branch: &str) -> String {
}
/// Format message for remove operation (includes emoji and color for consistency)
-fn format_remove_message(result: &RemoveResult) -> String {
- use worktrunk::styling::{GREEN, SUCCESS_EMOJI};
+fn format_remove_message(result: &RemoveResult, branch: Option<&str>) -> String {
+ use worktrunk::styling::{AnstyleStyle, GREEN, SUCCESS_EMOJI};
let green_bold = GREEN.bold();
+ let dim = AnstyleStyle::new().dimmed();
match result {
RemoveResult::AlreadyOnDefault(branch) => {
@@ -32,22 +33,28 @@ fn format_remove_message(result: &RemoveResult) -> String {
"{SUCCESS_EMOJI} {GREEN}Already on default branch {GREEN:#}{green_bold}{branch}{green_bold:#}"
)
}
- RemoveResult::RemovedWorktree { primary_path } => {
- format!(
- "{SUCCESS_EMOJI} {GREEN}Removed worktree, returned to primary at {green_bold}{}{green_bold:#}",
- primary_path.display()
- )
+ RemoveResult::RemovedWorktree {
+ primary_path,
+ changed_directory,
+ ..
+ } => {
+ let branch_suffix = branch
+ .map(|b| format!(" for {green_bold}{b}{green_bold:#}"))
+ .unwrap_or_default();
+ if *changed_directory {
+ format!(
+ "{SUCCESS_EMOJI} {GREEN}Removed worktree{branch_suffix}, returned to primary at {GREEN:#}{dim}{}{dim:#}",
+ primary_path.display()
+ )
+ } else {
+ format!("{SUCCESS_EMOJI} {GREEN}Removed worktree{branch_suffix}{GREEN:#}")
+ }
}
RemoveResult::SwitchedToDefault(branch) => {
format!(
"{SUCCESS_EMOJI} {GREEN}Switched to default branch {GREEN:#}{green_bold}{branch}{green_bold:#}"
)
}
- RemoveResult::RemovedOtherWorktree { branch } => {
- format!(
- "{SUCCESS_EMOJI} {GREEN}Removed worktree for {GREEN:#}{green_bold}{branch}{green_bold:#}"
- )
- }
}
}
@@ -90,14 +97,39 @@ pub fn handle_switch_output(
}
/// Handle output for a remove operation
-pub fn handle_remove_output(result: &RemoveResult) -> Result<(), GitError> {
- // For removed worktree, set target directory for shell to cd to
- if let RemoveResult::RemovedWorktree { primary_path } = result {
- super::change_directory(primary_path)?;
+pub fn handle_remove_output(result: &RemoveResult, branch: Option<&str>) -> Result<(), GitError> {
+ // For removed worktree: emit cd directive BEFORE deletion so shell changes directory instantly
+ if let RemoveResult::RemovedWorktree {
+ primary_path,
+ worktree_path,
+ changed_directory,
+ } = result
+ {
+ use worktrunk::styling::CYAN;
+
+ // 1. Emit cd directive if needed - shell will execute this immediately
+ if *changed_directory {
+ super::change_directory(primary_path)?;
+ super::flush()?; // Force flush to ensure shell processes the cd
+ }
+
+ // 2. Show progress message with branch name
+ let cyan_bold = CYAN.bold();
+ let progress_msg = if let Some(b) = branch {
+ format!("πŸ”„ {CYAN}Removing worktree for {cyan_bold}{b}{cyan_bold:#}...{CYAN:#}")
+ } else {
+ format!("πŸ”„ {CYAN}Removing worktree...{CYAN:#}")
+ };
+ super::progress(progress_msg)?;
+
+ // 3. Do the deletion (shell already changed directory if needed)
+ let repo = worktrunk::git::Repository::current();
+ repo.remove_worktree(worktree_path)
+ .git_context("Failed to remove worktree")?;
}
// Show success message (includes emoji and color)
- super::success(format_remove_message(result))?;
+ super::success(format_remove_message(result, branch))?;
// Flush output
super::flush()?;
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap
index 930ee8418..fa1d4e86a 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap
@@ -3,4 +3,4 @@ source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
πŸ”„ Removing worktree for to-remove...
-βœ… Removed worktree for to-remove
+βœ… Removed worktree for to-remove
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap b/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap
index 445eb7a81..00c99e3fa 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap
@@ -15,7 +15,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpCbfiBE/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp5Isv2I/test-config.toml
---
success: true
exit_code: 0
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap b/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap
index 8163d2bdd..52a54fb34 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap
@@ -12,15 +12,15 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpArleFS/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpyXEZSS/test-config.toml
---
success: false
exit_code: 1
----- stdout -----
-πŸ”„ Removing worktree for feature-dirty...
----- stderr -----
❌ Working tree has uncommitted changes
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap
index 5915a1111..696854565 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap
@@ -16,12 +16,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprA25Vy/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpYRN3ow/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-a...
-βœ… Removed worktree for feature-a
+βœ… Removed worktree for feature-a
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap
index 7d447eda1..2abaecb61 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap
@@ -16,12 +16,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp3YUOqW/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpkl7qTG/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-b...
-βœ… Removed worktree for feature-b
+βœ… Removed worktree for feature-b
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap b/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap
index 00a8d2746..62fbe5493 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap
@@ -16,12 +16,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprULsWr/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpjJwPSv/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-current...
-βœ… Removed worktree, returned to primary at [REPO]
+βœ… Removed worktree for feature-current, returned to primary at [REPO]
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snap b/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snap
index 800910d0b..e897c9b64 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snap
@@ -6,14 +6,16 @@ info:
- remove
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpeLeuHp/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpUeasps/test-config.toml
---
success: false
exit_code: 1
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap
index 81c724825..5f69878d0 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap
@@ -15,11 +15,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpLJvDCp/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp4lQBwz/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-βœ… Removed worktree, returned to primary at [REPO]
+πŸ”„ Removing worktree...
+βœ… Removed worktree, returned to primary at [REPO]
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap
index 1fa0100f9..0c6c1d1dd 100644
Binary files a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap and b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap differ
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap
index 4f902670c..9b65bfc07 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap
@@ -18,16 +18,16 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpE0SOHC/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpR7VM26/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-b...
-βœ… Removed worktree for feature-b
+βœ… Removed worktree for feature-b
πŸ”„ Removing worktree for feature-c...
-βœ… Removed worktree for feature-c
+βœ… Removed worktree for feature-c
πŸ”„ Removing worktree for feature-a...
-βœ… Removed worktree, returned to primary at [REPO]
+βœ… Removed worktree for feature-a, returned to primary at [REPO]
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap
index 13eac027c..fd7c313fd 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap
@@ -18,16 +18,16 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpVwovCx/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpu7MewE/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-a...
-βœ… Removed worktree for feature-a
+βœ… Removed worktree for feature-a
πŸ”„ Removing worktree for feature-b...
-βœ… Removed worktree for feature-b
+βœ… Removed worktree for feature-b
πŸ”„ Removing worktree for feature-c...
-βœ… Removed worktree for feature-c
+βœ… Removed worktree for feature-c
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap
index 1eb93195e..6608128b4 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap
@@ -16,12 +16,11 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp9KO7x2/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpifWDYQ/test-config.toml
---
success: false
exit_code: 1
----- stdout -----
-πŸ”„ Removing worktree for nonexistent...
----- stderr -----
❌ No worktree found for branch nonexistent
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap b/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap
index a9ed5e469..52ef783e7 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap
@@ -15,7 +15,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpMx1qX7/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp3FRcyN/test-config.toml
---
success: true
exit_code: 0