Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Allow removing multiple worktrees
Maximilian Roos66c40155bad1
diff --git a/src/main.rs b/src/main.rsindex 59d506d56..b7f15e3d6 100644--- a/src/main.rs+++ b/src/main.rs@@ -2,7 +2,7 @@ use anstyle::Style;use clap::{CommandFactory, Parser, Subcommand};use std::process;use worktrunk::config::WorktrunkConfig;-use worktrunk::git::GitError;+use worktrunk::git::{GitError, Repository};use worktrunk::styling::{SUCCESS_EMOJI, println};mod commands;@@ -123,8 +123,8 @@ enum Commands {/// Finish current worktree, returning to primary if currentRemove {- /// Worktree name or branch to remove (defaults to current worktree)- worktree: Option<String>,+ /// Worktree names or branches to remove (defaults to current worktree if none specified)+ worktrees: Vec<String>,},/// Push changes between worktrees@@ -366,8 +366,50 @@ fn main() {handle_switch(&branch, create, base.as_deref(), force, no_hooks, &config).and_then(|result| handle_switch_output(&result, &branch, execute.as_deref()))}),- Commands::Remove { worktree } => {- handle_remove(worktree.as_deref()).and_then(|result| handle_remove_output(&result))+ Commands::Remove { worktrees } => {+ if worktrees.is_empty() {+ // No worktrees specified, remove current worktree+ handle_remove(None).and_then(|result| handle_remove_output(&result))+ } else {+ // When removing multiple worktrees, we need to handle the current worktree last+ // to avoid deleting the directory we're currently in+ (|| -> Result<(), GitError> {+ let repo = Repository::current();+ let current_worktree = repo.worktree_root().ok();++ // Partition worktrees into current and others+ let mut others = Vec::new();+ let mut current = None;++ for worktree_name in worktrees.iter() {+ // Check if this is the current worktree by comparing branch names+ if let Ok(Some(worktree_path)) = repo.worktree_for_branch(worktree_name) {+ if Some(&worktree_path) == current_worktree.as_ref() {+ current = Some(worktree_name);+ } else {+ others.push(worktree_name);+ }+ } else {+ // Worktree doesn't exist or branch not found, will error when we try to remove+ others.push(worktree_name);+ }+ }++ // Remove others first+ for worktree in others.iter() {+ let result = handle_remove(Some(worktree.as_str()))?;+ handle_remove_output(&result)?;+ }++ // 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()))?;+ handle_remove_output(&result)?;+ }++ Ok(())+ })()+ }}Commands::Push {target,diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rsindex 894227a9d..28a895bda 100644--- a/tests/integration_tests/remove.rs+++ b/tests/integration_tests/remove.rs@@ -170,3 +170,43 @@ fn test_remove_by_name_dirty_target() {None,);}++#[test]+fn test_remove_multiple_worktrees() {+ let mut repo = TestRepo::new();+ repo.commit("Initial commit");+ repo.setup_remote("main");++ // Create three worktrees+ let _worktree_a = repo.add_worktree("feature-a", "feature-a");+ let _worktree_b = repo.add_worktree("feature-b", "feature-b");+ let _worktree_c = repo.add_worktree("feature-c", "feature-c");++ // Remove all three at once from main repo+ snapshot_remove(+ "remove_multiple_worktrees",+ &repo,+ &["feature-a", "feature-b", "feature-c"],+ None,+ );+}++#[test]+fn test_remove_multiple_including_current() {+ let mut repo = TestRepo::new();+ repo.commit("Initial commit");+ repo.setup_remote("main");++ // Create three worktrees+ let worktree_a = repo.add_worktree("feature-a", "feature-a");+ let _worktree_b = repo.add_worktree("feature-b", "feature-b");+ let _worktree_c = repo.add_worktree("feature-c", "feature-c");++ // From worktree A, remove all three (including current)+ snapshot_remove(+ "remove_multiple_including_current",+ &repo,+ &["feature-a", "feature-b", "feature-c"],+ Some(&worktree_a),+ );+}diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex 84ad1d70c..f823b237c 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -11,10 +11,11 @@ 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/.tmpBzDItE/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpqU0guR/test-config.toml---success: trueexit_code: 0@@ -343,7 +344,7 @@ _wt() {return 0;;wt__remove)- opts="-v -h --verbose --help [WORKTREE]"+ opts="-v -h --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_bash_custom_prefix.snap b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snapindex 3e1f2af18..cc8c224b8 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap@@ -13,10 +13,11 @@ 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/.tmpNl0mla/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpKlCu0A/test-config.toml---success: trueexit_code: 0@@ -345,7 +346,7 @@ _wt() {return 0;;wt__remove)- opts="-v -h --verbose --help [WORKTREE]"+ opts="-v -h --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 b6ed3b7ba..562f99bd0 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -11,10 +11,11 @@ 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/.tmpPrc5Yo/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpuVpY4n/test-config.toml---success: trueexit_code: 0@@ -133,7 +134,7 @@ if type -q wt# Register dynamic completionscomplete -c wt -n '__fish_seen_subcommand_from init' -f -a '(__wt_complete)' -d 'Shell to generate code for (bash, fish, zsh)'complete -c wt -n '__fish_seen_subcommand_from switch' -f -a '(__wt_complete)' -d 'Branch name or worktree path'- complete -c wt -n '__fish_seen_subcommand_from remove' -f -a '(__wt_complete)' -d 'Worktree name or branch to remove (defaults to current worktree)'+ complete -c wt -n '__fish_seen_subcommand_from remove' -f -a '(__wt_complete)' -d 'Worktree names or branches to remove (defaults to current worktree if none specified)'complete -c wt -n '__fish_seen_subcommand_from push' -f -a '(__wt_complete)' -d 'Target branch (defaults to default branch)'complete -c wt -n '__fish_seen_subcommand_from merge' -f -a '(__wt_complete)' -d 'Target branch to merge into (defaults to default branch)'enddiff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 6fe31b903..3388536ca 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -11,10 +11,11 @@ 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/.tmpBkXYLv/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpnzShP2/test-config.toml---success: trueexit_code: 0@@ -229,7 +230,7 @@ _arguments "${_arguments_options[@]}" : \'--verbose[Enable verbose output (show git commands and debug info)]' \'-h[Print help]' \'--help[Print help]' \-'::worktree -- Worktree name or branch to remove (defaults to current worktree):_default' \+'*::worktrees -- Worktree names or branches to remove (defaults to current worktree if none specified):_default' \&& ret=0;;(push)diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snapindex 0e3c6511b..378320055 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap@@ -16,17 +16,19 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpSpc2U4/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprfH3Ln/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mValidating merge...[0mπ [36mRunning pre-merge command [1m[36mcheck1[0m:[0m-[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check1 3+[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh+[40m [0m check1 3π [36mRunning pre-merge command [1m[36mcheck2[0m:[0m-[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check2 3+[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh+[40m [0m check2 3π [36mRebasing onto [1m[36mmain[0m...[0mπ [36mPushing 1 commit to [1m[36mmain[0m @ [SHA][0mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snapnew file mode 100644index 000000000..99343eb63--- /dev/null+++ b/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap@@ -0,0 +1,29 @@+---+source: tests/integration_tests/remove.rs+info:+ program: wt+ args:+ - remove+ - feature-a+ - feature-b+ - feature-c+ env:+ CLICOLOR_FORCE: "1"+ 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/.tmpXq8eF5/test-config.toml+---+success: true+exit_code: 0+----- stdout -----+β [32mRemoved worktree for [1mfeature-b[0m[0m+β [32mRemoved worktree for [1mfeature-c[0m[0m+β [32mRemoved worktree, returned to primary at [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.snapnew file mode 100644index 000000000..b78b2c5c7--- /dev/null+++ b/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap@@ -0,0 +1,29 @@+---+source: tests/integration_tests/remove.rs+info:+ program: wt+ args:+ - remove+ - feature-a+ - feature-b+ - feature-c+ env:+ CLICOLOR_FORCE: "1"+ 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/.tmpsOfWO1/test-config.toml+---+success: true+exit_code: 0+----- stdout -----+β [32mRemoved worktree for [1mfeature-a[0m[0m+β [32mRemoved worktree for [1mfeature-b[0m[0m+β [32mRemoved worktree for [1mfeature-c[0m[0m++----- stderr -----