Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Add --no-delete-branch option to `wt remove`
Maximilian Rooscffc49de492d
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex f76b90ed4..e15053148 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -143,6 +143,8 @@ pub enum RemoveResult {primary_path: PathBuf,worktree_path: PathBuf,changed_directory: bool,+ branch_name: String,+ no_delete_branch: bool,},/// Switched to default branch in main repoSwitchedToDefault(String),@@ -271,7 +273,10 @@ pub fn handle_switch())}-pub fn handle_remove(worktree_name: Option<&str>) -> Result<RemoveResult, GitError> {+pub fn handle_remove(+ worktree_name: Option<&str>,+ no_delete_branch: bool,+) -> Result<RemoveResult, GitError> {let repo = Repository::current();// Resolve "@" to current branch early so progress message shows resolved name@@ -292,13 +297,16 @@ pub fn handle_remove(worktree_name: Option<&str>) -> Result<RemoveResult, GitErr// Two modes: remove current worktree vs. remove by namematch resolved_name.as_deref() {- None => remove_current_worktree(&repo),- Some(name) => remove_worktree_by_name(&repo, name),+ None => remove_current_worktree(&repo, no_delete_branch),+ Some(name) => remove_worktree_by_name(&repo, name, no_delete_branch),}}/// Remove the current worktree (original behavior)-fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError> {+fn remove_current_worktree(+ repo: &Repository,+ no_delete_branch: bool,+) -> Result<RemoveResult, GitError> {// Check for uncommitted changes in current worktreerepo.ensure_clean_working_tree()?;@@ -309,6 +317,9 @@ fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError>// In worktree: navigate to primary worktree and remove this one// (no need to check default branch in this path)let worktree_root = repo.worktree_root()?;+ let current_branch = repo+ .current_branch()?+ .ok_or_else(|| GitError::CommandFailed("Not on a branch".to_string()))?;let worktrees = repo.list_worktrees()?;let primary_worktree_dir = worktrees.primary().path.clone();@@ -317,6 +328,8 @@ fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError>primary_path: primary_worktree_dir,worktree_path: worktree_root,changed_directory: true, // We're in the worktree being removed+ branch_name: current_branch,+ no_delete_branch,})} else {// In main repo: check if already on default branch@@ -337,7 +350,11 @@ fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError>}/// Remove a worktree by branch name (branch_name should already be resolved)-fn remove_worktree_by_name(repo: &Repository, branch_name: &str) -> Result<RemoveResult, GitError> {+fn remove_worktree_by_name(+ repo: &Repository,+ branch_name: &str,+ no_delete_branch: bool,+) -> Result<RemoveResult, GitError> {// Find the worktree for this branchlet worktree_path = repo.worktree_for_branch(branch_name)?;@@ -379,6 +396,8 @@ fn remove_worktree_by_name(repo: &Repository, branch_name: &str) -> Result<Removprimary_path,worktree_path,changed_directory,+ branch_name: branch_name.to_string(),+ no_delete_branch,})}diff --git a/src/main.rs b/src/main.rsindex 5a1dc51e3..ded1732be 100644--- a/src/main.rs+++ b/src/main.rs@@ -317,21 +317,26 @@ Remove Multiple Worktrees:CLEANUP:-When removing a worktree:+When removing a worktree (by default):1. Validates worktree has no uncommitted changes2. Changes directory (if removing current worktree)3. Deletes worktree directory4. Removes git worktree metadata- 5. Deletes associated branch+ 5. Deletes associated branch (uses git branch -d, safe delete)+ - If branch has unmerged commits, shows warning but continues+ - Use --no-delete-branch to skip branch deletionEXAMPLES:-Remove current worktree:+Remove current worktree and branch:wt remove-Remove specific worktree:+Remove specific worktree and branch:wt remove feature-branch+Remove worktree but keep branch:+ wt remove --no-delete-branch feature-branch+Remove multiple worktrees:wt remove old-feature another-branch@@ -340,6 +345,10 @@ Switch to default in primary:Remove {/// Worktree names or branches to remove (use '@' for current, defaults to current if none specified)worktrees: Vec<String>,++ /// Don't delete the branch after removing the worktree (by default, branches are deleted)+ #[arg(long = "no-delete-branch")]+ no_delete_branch: bool,},/// Merge worktree into target branch@@ -628,10 +637,14 @@ fn main() {Ok(())}),- Commands::Remove { worktrees } => {+ Commands::Remove {+ worktrees,+ no_delete_branch,+ } => {if worktrees.is_empty() {// No worktrees specified, remove current worktree- handle_remove(None).and_then(|result| handle_remove_output(&result, None))+ handle_remove(None, no_delete_branch)+ .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@@ -663,13 +676,13 @@ fn main() {// Remove others first, then current last// Progress messages shown by handle_remove_output for all casesfor worktree in others.iter() {- let result = handle_remove(Some(worktree.as_str()))?;+ let result = handle_remove(Some(worktree.as_str()), no_delete_branch)?;handle_remove_output(&result, Some(worktree.as_str()))?;}// Remove current worktree last (if it was in the list)if let Some(current_name) = current {- let result = handle_remove(Some(current_name.as_str()))?;+ let result = handle_remove(Some(current_name.as_str()), no_delete_branch)?;handle_remove_output(&result, Some(current_name.as_str()))?;}diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex 224f84c6a..ae976dcad 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -36,18 +36,29 @@ fn format_remove_message(result: &RemoveResult, branch: Option<&str>) -> StringRemoveResult::RemovedWorktree {primary_path,changed_directory,+ branch_name,+ no_delete_branch,..} => {+ // Build the action description+ let action = if *no_delete_branch {+ "Removed worktree"+ } else {+ "Removed worktree & branch"+ };+let branch_suffix = branch+ .or(Some(branch_name)).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_bold}{}{green_bold:#}{GREEN:#}",+ "{SUCCESS_EMOJI} {GREEN}{action}{branch_suffix}, returned to primary at {green_bold}{}{green_bold:#}{GREEN:#}",primary_path.display())} else {- format!("{SUCCESS_EMOJI} {GREEN}Removed worktree{branch_suffix}{GREEN:#}")+ format!("{SUCCESS_EMOJI} {GREEN}{action}{branch_suffix}{GREEN:#}")}}RemoveResult::SwitchedToDefault(branch) => {@@ -107,6 +118,8 @@ pub fn handle_remove_output(result: &RemoveResult, branch: Option<&str>) -> Resuprimary_path,worktree_path,changed_directory,+ branch_name,+ no_delete_branch,} = result{// 1. Emit cd directive if needed - shell will execute this immediately@@ -120,6 +133,28 @@ pub fn handle_remove_output(result: &RemoveResult, branch: Option<&str>) -> Resulet repo = worktrunk::git::Repository::current();repo.remove_worktree(worktree_path).git_context("Failed to remove worktree")?;++ // 3. Delete the branch (unless --no-delete-branch was specified)+ if !no_delete_branch {+ // Create a Repository instance from the primary path to ensure we're running+ // the command from a valid directory (the worktree we just removed may have+ // been the current directory)+ let primary_repo = worktrunk::git::Repository::at(primary_path);++ // Use safe delete (-d) which fails if branch has unmerged commits+ let result = primary_repo.run_command(&["branch", "-d", branch_name]);+ if let Err(e) = result {+ // If branch deletion fails, show a warning but don't error+ // This matches the user's request: "print a nice message, don't raise some big error"+ use worktrunk::styling::{WARNING, WARNING_EMOJI};+ let warning_bold = WARNING.bold();+ // Normalize error message to single line to prevent formatting issues+ let error_msg = e.to_string().replace('\n', " ").trim().to_string();+ super::progress(format!(+ "{WARNING_EMOJI} {WARNING}Could not delete branch {warning_bold}{branch_name}{warning_bold:#}: {error_msg}{WARNING:#}"+ ))?;+ }+ }}// Show success message (includes emoji and color)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 fa1d4e86a..4f81c4f6c 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@@ -1,6 +1,6 @@---source: tests/integration_tests/shell_wrapper.rs-expression: output.normalized().as_ref()+expression: output.normalized()---π [36mRemoving worktree for [1m[36mto-remove[0m...[0m-β [32mRemoved worktree for [1m[32mto-remove[0m[0m+β [32mRemoved worktree & branch for [1m[32mto-remove[0m[0mdiff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex e4e84eb42..65f9be467 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpIW4lMG/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpIA7lzg/test-config.toml---success: trueexit_code: 0@@ -481,7 +481,7 @@ _wt() {return 0;;wt__remove)- opts="-v -h --verbose --help [WORKTREES]..."+ opts="-v -h --no-delete-branch --verbose --help [WORKTREES]..."if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; thenCOMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 5933fc856..692625e2d 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprYnT86/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpwoMAjs/test-config.toml---success: trueexit_code: 0@@ -280,6 +280,7 @@ complete -c wt -n "__fish_wt_using_subcommand switch" -s f -l force -d 'Skip concomplete -c wt -n "__fish_wt_using_subcommand switch" -l no-verify -d 'Skip all project hooks (post-create, post-start)'complete -c wt -n "__fish_wt_using_subcommand switch" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'complete -c wt -n "__fish_wt_using_subcommand switch" -s h -l help -d 'Print help'+complete -c wt -n "__fish_wt_using_subcommand remove" -l no-delete-branch -d 'Don\'t delete the branch after removing the worktree (by default, branches are deleted)'complete -c wt -n "__fish_wt_using_subcommand remove" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'complete -c wt -n "__fish_wt_using_subcommand remove" -s h -l help -d 'Print help'complete -c wt -n "__fish_wt_using_subcommand merge" -l no-squash -d 'Disable squashing commits (by default, commits are squashed into one before merging)'diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex cf3d1d834..bccae9aa1 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpacy9En/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpO74VSQ/test-config.toml---success: trueexit_code: 0@@ -329,6 +329,7 @@ _arguments "${_arguments_options[@]}" : \;;(remove)_arguments "${_arguments_options[@]}" : \+'--no-delete-branch[Don'\''t delete the branch after removing the worktree (by default, branches are deleted)]' \'-v[Enable verbose output (show git commands and debug info)]' \'--verbose[Enable verbose output (show git commands and debug info)]' \'-h[Print help]' \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.snapindex 696854565..51b066d46 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/.tmpYRN3ow/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpjpw5Wg/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-a[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-a[0m[0m+β [32mRemoved worktree & branch 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 2abaecb61..31cccb2b3 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/.tmpkl7qTG/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp8wSstb/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-b[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-b[0m[0m+β [32mRemoved worktree & branch 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 916eb609e..9a8a7249e 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/.tmpEsMvsL/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp5limTL/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-current[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-current[0m, returned to primary at [1m[32m[REPO][0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-current[0m, returned to primary at [1m[32m[REPO][0m[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snapindex 39a9348d5..ec897a4d2 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap@@ -15,12 +15,12 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpZPWxJo/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpjheYz9/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mRemoving worktree...[0m-β [32mRemoved worktree, returned to primary at [1m[32m[REPO][0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-wt[0m, returned to primary at [1m[32m[REPO][0m[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 01780bdca..3c66de82b 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 49625f3a3..2b593fd6a 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/.tmplyBFa9/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp4D6efo/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-b[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-b[0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-b[0m[0mπ [36mRemoving worktree for [1m[36mfeature-c[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-c[0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-c[0m[0mπ [36mRemoving worktree for [1m[36mfeature-a[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-a[0m, returned to primary at [1m[32m[REPO][0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-a[0m, returned to primary at [1m[32m[REPO][0m[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 fd7c313fd..8a3b6060a 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/.tmpu7MewE/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpXGv0t9/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mRemoving worktree for [1m[36mfeature-a[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-a[0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-a[0m[0mπ [36mRemoving worktree for [1m[36mfeature-b[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-b[0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-b[0m[0mπ [36mRemoving worktree for [1m[36mfeature-c[0m...[0m-β [32mRemoved worktree for [1m[32mfeature-c[0m[0m+β [32mRemoved worktree & branch for [1m[32mfeature-c[0m[0m----- stderr -----