Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: add alt-d shortcut to delete worktrees from switch picker (#1253)
worktrunk-botafc3754a5a4c
diff --git a/docs/content/switch.md b/docs/content/switch.mdindex 82cf59ae3..1365e80d7 100644--- a/docs/content/switch.md+++ b/docs/content/switch.md@@ -93,6 +93,7 @@ When called without arguments, `wt switch` opens an interactive picker to browse| (type) | Filter worktrees || `Enter` | Switch to selected worktree || `Alt-c` | Create new worktree from query |+| `Alt-r` | Remove selected worktree || `Esc` | Cancel || `1`–`5` | Switch preview tab || `Alt-p` | Toggle preview panel |diff --git a/skills/worktrunk/reference/switch.md b/skills/worktrunk/reference/switch.mdindex 99dedad84..c39f6cd3a 100644--- a/skills/worktrunk/reference/switch.md+++ b/skills/worktrunk/reference/switch.md@@ -70,6 +70,7 @@ When called without arguments, `wt switch` opens an interactive picker to browse| (type) | Filter worktrees || `Enter` | Switch to selected worktree || `Alt-c` | Create new worktree from query |+| `Alt-r` | Remove selected worktree || `Esc` | Cancel || `1`–`5` | Switch preview tab || `Alt-p` | Toggle preview panel |diff --git a/src/cli/mod.rs b/src/cli/mod.rsindex daeae4512..c298d667f 100644--- a/src/cli/mod.rs+++ b/src/cli/mod.rs@@ -319,6 +319,7 @@ When called without arguments, `wt switch` opens an interactive picker to browse| (type) | Filter worktrees || `Enter` | Switch to selected worktree || `Alt-c` | Create new worktree from query |+| `Alt-r` | Remove selected worktree || `Esc` | Cancel || `1`–`5` | Switch preview tab || `Alt-p` | Toggle preview panel |diff --git a/src/commands/select/items.rs b/src/commands/select/items.rsindex 33b677acd..e986bbac6 100644--- a/src/commands/select/items.rs+++ b/src/commands/select/items.rs@@ -145,7 +145,7 @@ impl WorktreeSkimItem {// Controls use dim yellow to distinguish from dimmed (white) tabslet controls = cformat!(- "<dim,yellow>Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scroll | alt-p: toggle</>"+ "<dim,yellow>Enter: switch | alt-c: create | alt-r: remove | Esc: cancel | ctrl-u/d: scroll | alt-p: toggle</>");// End each tab and controls with full reset to prevent style bleeding@@ -427,6 +427,7 @@ mod tests {assert!(output.contains("4: remote⇅"));assert!(output.contains("5: summary"));assert!(output.contains("Enter: switch"));+ assert!(output.contains("alt-r: remove"));// Verify structure: tabs on first line, controls on secondassert!(output.contains(" | "));assert!(output.ends_with("\n\n"));diff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rsindex ce3f16287..67ccf2353 100644--- a/src/commands/select/mod.rs+++ b/src/commands/select/mod.rs@@ -21,13 +21,23 @@ use super::handle_switch::{};use super::list::collect;use super::worktree::{- SwitchBranchInfo, SwitchResult, execute_switch, get_path_mismatch, plan_switch,+ SwitchBranchInfo, SwitchResult, execute_switch, get_path_mismatch, handle_remove, plan_switch,};-use crate::output::handle_switch_output;+use crate::output::{handle_remove_output, handle_switch_output};use items::{HeaderSkimItem, PreviewCache, WorktreeSkimItem};use preview::{PreviewLayout, PreviewMode, PreviewState};+/// Action selected by the user in the picker.+enum PickerAction {+ /// Switch to the selected worktree (Enter key).+ Switch,+ /// Create a new worktree from the search query (alt-c).+ Create,+ /// Remove the selected worktree (alt-r for "remove").+ Remove,+}+pub fn handle_select(cli_branches: bool, cli_remotes: bool) -> anyhow::Result<()> {// Interactive picker requires a terminal for the TUIif !std::io::stdin().is_terminal() {@@ -203,6 +213,8 @@ pub fn handle_select(cli_branches: bool, cli_remotes: bool) -> anyhow::Result<()),// Create new worktree with query as branch name (alt-c for "create")"alt-c:accept(create)".to_string(),+ // Remove selected worktree (alt-r for "remove")+ "alt-r:accept(remove)".to_string(),// Preview toggle (alt-p shows/hides preview)// Note: skim doesn't support change-preview-window like fzf, only toggle"alt-p:toggle-preview".to_string(),@@ -288,78 +300,111 @@ pub fn handle_select(cli_branches: bool, cli_remotes: bool) -> anyhow::Result<()if let Some(out) = output&& !out.is_abort{- // Determine if user wants to create a new worktree (alt-n) or switch to existing (enter)- let create_new =- matches!(out.final_event, Event::EvActAccept(Some(ref label)) if label == "create");-- // Get branch name: from query if creating new, from selected item if switching- let (identifier, should_create) = if create_new {- let query = out.query.trim().to_string();- if query.is_empty() {- anyhow::bail!("Cannot create worktree: no branch name entered");- }- (query, true)- } else {- // Enter pressed: skim accept always includes a selection (abort handled above)- let selected = out- .selected_items- .first()- .expect("skim accept has selection");- (selected.output().to_string(), false)- };-- // Load config — reuse recovered repo if we recovered earlier- let repo = if is_recovered {- repo.clone()- } else {- Repository::current().context("Failed to switch worktree")?+ // Determine action: create (alt-c), remove (alt-r), or switch (enter)+ let action = match &out.final_event {+ Event::EvActAccept(Some(label)) if label == "create" => PickerAction::Create,+ Event::EvActAccept(Some(label)) if label == "remove" => PickerAction::Remove,+ _ => PickerAction::Switch,};- let config = repo.user_config();- // Run pre-switch hooks before anything else (before branch validation, planning, etc.)- // Skip when recovered — the source worktree is gone, nothing to run hooks against.- if !is_recovered {- run_pre_switch_hooks(&repo, config, true)?;- }+ match action {+ PickerAction::Remove => {+ // Get the selected worktree's branch name+ let selected = out+ .selected_items+ .first()+ .expect("skim accept has selection");+ let branch_name = selected.output().to_string();++ let config = repo.user_config();++ // Safe removal: no force-delete (-D), no force-worktree (-f)+ let result = handle_remove(+ &branch_name,+ false, // no_delete_branch: delete branch (default behavior)+ false, // force_delete: no -D+ false, // force_worktree: no -f+ config,+ )+ .context("Failed to remove worktree")?;++ // Execute removal in foreground, no hooks, not quiet+ handle_remove_output(&result, false, false, false)?;+ }+ PickerAction::Create | PickerAction::Switch => {+ let should_create = matches!(action, PickerAction::Create);++ // Get branch name: from query if creating new, from selected item if switching+ let identifier = if should_create {+ let query = out.query.trim().to_string();+ if query.is_empty() {+ anyhow::bail!("Cannot create worktree: no branch name entered");+ }+ query+ } else {+ // Enter pressed: skim accept always includes a selection (abort handled above)+ let selected = out+ .selected_items+ .first()+ .expect("skim accept has selection");+ selected.output().to_string()+ };++ // Load config — reuse recovered repo if we recovered earlier+ let repo = if is_recovered {+ repo.clone()+ } else {+ Repository::current().context("Failed to switch worktree")?+ };+ let config = repo.user_config();++ // Run pre-switch hooks before anything else (before branch validation, planning, etc.)+ // Skip when recovered — the source worktree is gone, nothing to run hooks against.+ if !is_recovered {+ run_pre_switch_hooks(&repo, config, true)?;+ }- // Switch to existing worktree or create new one- let plan = plan_switch(&repo, &identifier, should_create, None, false, config)?;- let skip_hooks = !approve_switch_hooks(&repo, config, &plan, false, true)?;- let (result, branch_info) = execute_switch(&repo, plan, config, false, skip_hooks)?;-- // Compute path mismatch lazily (deferred from plan_switch for existing worktrees)- let branch_info = match &result {- SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {- let expected_path = get_path_mismatch(&repo, &branch_info.branch, path, config);- SwitchBranchInfo {- expected_path,- ..branch_info+ // Switch to existing worktree or create new one+ let plan = plan_switch(&repo, &identifier, should_create, None, false, config)?;+ let skip_hooks = !approve_switch_hooks(&repo, config, &plan, false, true)?;+ let (result, branch_info) = execute_switch(&repo, plan, config, false, skip_hooks)?;++ // Compute path mismatch lazily (deferred from plan_switch for existing worktrees)+ let branch_info = match &result {+ SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {+ let expected_path =+ get_path_mismatch(&repo, &branch_info.branch, path, config);+ SwitchBranchInfo {+ expected_path,+ ..branch_info+ }+ }+ _ => branch_info,+ };++ // Show success message; emit cd directive if shell integration is active+ // Interactive picker always performs cd (change_dir: true)+ // When recovered from a deleted worktree, fall back to repo_path().+ let fallback_path = repo.repo_path()?.to_path_buf();+ let cwd = std::env::current_dir().unwrap_or(fallback_path.clone());+ let source_root = repo.current_worktree().root().unwrap_or(fallback_path);+ let hooks_display_path =+ handle_switch_output(&result, &branch_info, true, Some(&source_root), &cwd)?;++ // Spawn background hooks after success message+ if !skip_hooks {+ let extra_vars = switch_extra_vars(&result);+ spawn_switch_background_hooks(+ &repo,+ config,+ &result,+ &branch_info.branch,+ false,+ &extra_vars,+ hooks_display_path.as_deref(),+ )?;}}- _ => branch_info,- };-- // Show success message; emit cd directive if shell integration is active- // Interactive picker always performs cd (change_dir: true)- // When recovered from a deleted worktree, fall back to repo_path().- let fallback_path = repo.repo_path()?.to_path_buf();- let cwd = std::env::current_dir().unwrap_or(fallback_path.clone());- let source_root = repo.current_worktree().root().unwrap_or(fallback_path);- let hooks_display_path =- handle_switch_output(&result, &branch_info, true, Some(&source_root), &cwd)?;-- // Spawn background hooks after success message- if !skip_hooks {- let extra_vars = switch_extra_vars(&result);- spawn_switch_background_hooks(- &repo,- config,- &result,- &branch_info.branch,- false,- &extra_vars,- hooks_display_path.as_deref(),- )?;}}diff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snapindex 5f0b9cdb7..a465d4745 100644--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap@@ -11,6 +11,7 @@ info:GIT_EDITOR: ""LANG: CLC_ALL: C+ NO_COLOR: ""PSModulePath: ""RUST_LOG: warnSHELL: ""@@ -165,6 +166,7 @@ When called without arguments, [2mwt switch[0m opens an interactive picker to(type) Filter worktrees[2mEnter[0m Switch to selected worktree[2mAlt-c[0m Create new worktree from query+ [2mAlt-r[0m Remove selected worktree[2mEsc[0m Cancel[2m1[0m–[2m5[0m Switch preview tab[2mAlt-p[0m Toggle preview paneldiff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape_preview.snapindex 70b578f57..0b4ece9e5 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape_preview.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape_preview.snap@@ -3,6 +3,6 @@ source: tests/integration_tests/switch_picker.rsexpression: preview---1: HEAD± | 2: log | 3: main…± | 4: remote⇅ | 5: summary [N/M]-Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr+Enter: switch | alt-c: create | alt-r: remove | Esc: cancel○ main has no uncommitted changesdiff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees_preview.snapindex 70b578f57..0b4ece9e5 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees_preview.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees_preview.snap@@ -3,6 +3,6 @@ source: tests/integration_tests/switch_picker.rsexpression: preview---1: HEAD± | 2: log | 3: main…± | 4: remote⇅ | 5: summary [N/M]-Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr+Enter: switch | alt-c: create | alt-r: remove | Esc: cancel○ main has no uncommitted changesdiff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_preview.snapindex a11a5eaca..7e06b9e37 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_preview.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_preview.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/switch_picker.rsexpression: preview---1: HEAD± | 2: log | 3: main…± | 4: remote⇅ | 5: summary [N/M]-Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr+Enter: switch | alt-c: create | alt-r: remove | Esc: cancel* [HASH] +1 [TIME] (feature) Add file 5 with content* [HASH] +1 [TIME] Add file 4 with contentdiff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_preview.snapindex 494330b69..c2a59d666 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_preview.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_preview.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/switch_picker.rsexpression: preview---1: HEAD± | 2: log | 3: main…± | 4: remote⇅ | 5: summary [N/M]-Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr+Enter: switch | alt-c: create | alt-r: remove | Esc: cancelfeature_code.rs | 6 ++++++tests.rs | 4 ++++diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_summary_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_summary_preview.snapindex 3d208e81f..08eabcc76 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_summary_preview.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_summary_preview.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/switch_picker.rsexpression: preview---1: HEAD± | 2: log | 3: main…± | 4: remote⇅ | 5: summary [N/M]-Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr+Enter: switch | alt-c: create | alt-r: remove | Esc: cancelConfigure [commit.generation] command to enable LLM summaridiff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_preview.snapindex 6807cadde..f9f0539ed 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_preview.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_preview.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/switch_picker.rsexpression: preview---1: HEAD± | 2: log | 3: main…± | 4: remote⇅ | 5: summary [N/M]-Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr+Enter: switch | alt-c: create | alt-r: remove | Esc: canceltracked.txt | 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches_preview.snapindex 70b578f57..0b4ece9e5 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches_preview.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches_preview.snap@@ -3,6 +3,6 @@ source: tests/integration_tests/switch_picker.rsexpression: preview---1: HEAD± | 2: log | 3: main…± | 4: remote⇅ | 5: summary [N/M]-Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr+Enter: switch | alt-c: create | alt-r: remove | Esc: cancel○ main has no uncommitted changes