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.rsindex 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 summarycrate::output::progress("")?;diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 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 takenAlreadyOnDefault(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 repoSwitchedToDefault(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 emittedOk(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<Removlet 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 worktreediff --git a/src/main.rs b/src/main.rsindex 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 casesfor 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.rsindex 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 outputsuper::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.snapindex 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.rsexpression: output.normalized().as_ref()---π [36mRemoving worktree for [1m[36mto-remove[0m...[0m-β [32mRemoved worktree for [0m[1m[32mto-remove[0m+β [32mRemoved worktree for [1m[32mto-remove[0m[0mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap b/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0diff --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.snapindex 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/nullGIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_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: falseexit_code: 1----- stdout ------π [36mRemoving worktree for [1m[36mfeature-dirty[0m...[0m----- stderr -----β [31mWorking tree has uncommitted changes[0mdiff --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.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-a[0m...[0m-β [32mRemoved worktree for [0m[1m[32mfeature-a[0m+β [32mRemoved worktree for [1m[32mfeature-a[0m[0m----- 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.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-b[0m...[0m-β [32mRemoved worktree for [0m[1m[32mfeature-b[0m+β [32mRemoved worktree for [1m[32mfeature-b[0m[0m----- 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.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-current[0m...[0m-β [32mRemoved worktree, returned to primary at [1m[32m[REPO][0m+β [32mRemoved worktree for [1m[32mfeature-current[0m, returned to primary at [0m[2m[REPO][0m----- 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.snapindex 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:- removeenv: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/nullGIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_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: falseexit_code: 1diff --git a/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0----- stdout ------β [32mRemoved worktree, returned to primary at [1m[32m[REPO][0m+π [36mRemoving worktree...[0m+β [32mRemoved worktree, returned to primary at [0m[2m[REPO][0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snapindex 1fa0100f9..0c6c1d1dd 100644Binary files a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap and b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap differdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-b[0m...[0m-β [32mRemoved worktree for [0m[1m[32mfeature-b[0m+β [32mRemoved worktree for [1m[32mfeature-b[0m[0mπ [36mRemoving worktree for [1m[36mfeature-c[0m...[0m-β [32mRemoved worktree for [0m[1m[32mfeature-c[0m+β [32mRemoved worktree for [1m[32mfeature-c[0m[0mπ [36mRemoving worktree for [1m[36mfeature-a[0m...[0m-β [32mRemoved worktree, returned to primary at [1m[32m[REPO][0m+β [32mRemoved worktree for [1m[32mfeature-a[0m, returned to primary at [0m[2m[REPO][0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-a[0m...[0m-β [32mRemoved worktree for [0m[1m[32mfeature-a[0m+β [32mRemoved worktree for [1m[32mfeature-a[0m[0mπ [36mRemoving worktree for [1m[36mfeature-b[0m...[0m-β [32mRemoved worktree for [0m[1m[32mfeature-b[0m+β [32mRemoved worktree for [1m[32mfeature-b[0m[0mπ [36mRemoving worktree for [1m[36mfeature-c[0m...[0m-β [32mRemoved worktree for [0m[1m[32mfeature-c[0m+β [32mRemoved worktree for [1m[32mfeature-c[0m[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snapindex 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: CLC_ALL: CSOURCE_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: falseexit_code: 1----- stdout ------π [36mRemoving worktree for [1m[36mnonexistent[0m...[0m----- stderr -----β [31mNo worktree found for branch [0m[1m[31mnonexistent[0mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap b/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snapindex 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: CLC_ALL: CSOURCE_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: trueexit_code: 0