Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: in-place worktree removal in picker via alt-r (#1677)
Maximilian Roosd416d0a93d27
diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex 8b164387f..d4a3f35e2 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -8,25 +8,32 @@ mod pager;mod preview;mod summary;+use std::cell::RefCell;use std::io::IsTerminal;-use std::sync::Arc;+use std::path::PathBuf;+use std::rc::Rc;+use std::sync::atomic::AtomicUsize;+use std::sync::{Arc, Mutex};use std::time::Instant;use anyhow::Context;+// bounded/unbounded/Sender are re-exported by skim::preludeuse dashmap::DashMap;use skim::prelude::*;+use skim::reader::CommandCollector;use worktrunk::git::{Repository, current_or_recover};+use super::branch_deletion::delete_branch_if_safe;use super::handle_switch::{approve_switch_hooks, run_pre_switch_hooks, spawn_switch_background_hooks, switch_extra_vars,};use super::list::collect;use super::repository_ext::{RemoveTarget, RepositoryCliExt};use super::worktree::{- BranchDeletionMode, SwitchBranchInfo, SwitchResult, execute_switch,+ BranchDeletionMode, RemoveResult, SwitchBranchInfo, SwitchResult, execute_switch,offer_bare_repo_worktree_path_fix, path_mismatch, plan_switch,};-use crate::output::{handle_remove_output, handle_switch_output};+use crate::output::handle_switch_output;use items::{HeaderSkimItem, PreviewCache, WorktreeSkimItem};use preview::{PreviewLayout, PreviewMode, PreviewState};@@ -37,8 +44,152 @@ enum PickerAction {Switch,/// Create a new worktree from the search query (alt-c).Create,- /// Remove the selected worktree (alt-r for "remove").- Remove,+}++/// Custom command collector for skim's `reload` action.+///+/// When alt-r is pressed, skim runs `execute-silent` to write the selected branch+/// name to a signal file, then `reload` invokes this collector. The collector reads+/// the signal file, performs the worktree removal, filters the item from the list,+/// and streams the remaining items back to skim — all without leaving the picker.+///+/// Cursor position resets to the first item after reload. This is a skim 0.20+/// limitation: `act_reload` clears the selection, then `restart_matcher` races+/// the reader thread — if items aren't in the pool yet, the empty matcher result+/// clamps `line_cursor` to 0. The fix is skim 4.0's `Action::Custom(ActionCallback)`+/// which gives `&mut App` access to manipulate `item_list` directly without `reload`.+struct PickerCollector {+ items: Arc<Mutex<Vec<Arc<dyn SkimItem>>>>,+ signal_path: PathBuf,+ repo: Repository,+}++impl PickerCollector {+ /// Execute worktree removal silently: stop fsmonitor, remove worktree, delete branch.+ ///+ /// No output, no hooks, no cd directives — we're inside skim's TUI.+ fn execute_removal(result: &RemoveResult) -> anyhow::Result<()> {+ let RemoveResult::RemovedWorktree {+ main_path,+ worktree_path,+ branch_name,+ deletion_mode,+ target_branch,+ force_worktree,+ ..+ } = result+ else {+ return Ok(());+ };++ let repo = Repository::at(main_path)?;+ let _ = repo+ .worktree_at(worktree_path)+ .run_command(&["fsmonitor--daemon", "stop"]);+ repo.remove_worktree(worktree_path, *force_worktree)?;++ if let Some(branch) = branch_name+ && !deletion_mode.should_keep()+ {+ let target = target_branch.as_deref().unwrap_or("HEAD");+ let _ = delete_branch_if_safe(&repo, branch, target, deletion_mode.is_force());+ }++ Ok(())+ }+}++impl CommandCollector for PickerCollector {+ fn invoke(+ &mut self,+ _cmd: &str,+ components_to_stop: Arc<AtomicUsize>,+ ) -> (SkimItemReceiver, Sender<i32>) {+ // Read the removal signal (item output text written by execute-silent)+ if let Ok(signal) = std::fs::read_to_string(&self.signal_path) {+ let selected_output = signal.trim().to_string();+ if !selected_output.is_empty() {+ let config = self.repo.user_config();++ // Check if this is a detached HEAD worktree — these have no branch+ // name, so we need to remove by path instead.+ let detached_path = {+ let items = self.items.lock().unwrap();+ items+ .iter()+ .find(|item| item.output().as_ref() == selected_output)+ .and_then(|item| item.as_any().downcast_ref::<WorktreeSkimItem>())+ .and_then(|skim_item| skim_item.item.worktree_data())+ .filter(|data| data.detached)+ .map(|data| data.path.clone())+ };++ // Safe removal via prepare + execute (no output, no hooks,+ // no cd directive — we're inside skim's TUI).+ let target = if let Some(path) = &detached_path {+ RemoveTarget::Path(path)+ } else {+ RemoveTarget::Branch(&selected_output)+ };+ let removal = self+ .repo+ .prepare_worktree_removal(target, BranchDeletionMode::SafeDelete, false, config)+ .and_then(|result| {+ Self::execute_removal(&result)?;+ Ok(result)+ });+ match removal {+ Ok(result) => {+ // If we removed the current worktree, update process CWD+ // so skim and git commands continue to work.+ if let RemoveResult::RemovedWorktree {+ changed_directory: true,+ main_path,+ ..+ } = &result+ {+ let _ = std::env::set_current_dir(main_path);+ }+ let mut items = self.items.lock().unwrap();+ if let Some(path) = &detached_path {+ // Detached items all share output "(detached)" —+ // match by worktree path to remove only this one.+ items.retain(|item| {+ item.as_any()+ .downcast_ref::<WorktreeSkimItem>()+ .and_then(|s| s.item.worktree_data())+ .is_none_or(|d| d.path != *path)+ });+ } else {+ items.retain(|item| item.output().as_ref() != selected_output);+ }+ }+ Err(e) => {+ log::warn!("picker: failed to remove '{selected_output}': {e:#}");+ }+ }++ // Clear signal for next removal+ let _ = std::fs::write(&self.signal_path, "");+ }+ }++ // Stream remaining items through a channel for skim to consume.+ // Uses unbounded channel so all items are sent immediately without blocking.+ let items = self.items.lock().unwrap();+ let (tx, rx) = unbounded();+ for item in items.iter() {+ let _ = tx.send(Arc::clone(item));+ }+ drop(tx);++ // Dummy interrupt channel — no subprocess to kill.+ // The reader's collect_item thread handles its own components_to_stop accounting;+ // we just need a valid Sender to satisfy the trait signature.+ let _ = components_to_stop;+ let (tx_interrupt, _rx_interrupt) = bounded(1);+ (rx, tx_interrupt)+ }}pub fn handle_picker(@@ -173,6 +324,24 @@ pub fn handle_picker(let num_items = items.len().saturating_sub(1);let preview_window_spec = state.initial_layout.to_preview_window_spec(num_items);+ // Signal file for alt-r removal communication. execute-silent writes the branch+ // name here; the PickerCollector reads it on reload. Cleaned up in PreviewState::Drop.+ let signal_path = state.path.with_extension("remove");++ // Shared items list: the PickerCollector reads and modifies this on reload.+ let shared_items: Arc<Mutex<Vec<Arc<dyn SkimItem>>>> = Arc::new(Mutex::new(items));++ // Custom collector for skim's reload action — performs removal and streams+ // updated items back, all without leaving the picker.+ let collector = PickerCollector {+ items: Arc::clone(&shared_items),+ signal_path: signal_path.clone(),+ repo: repo.clone(),+ };++ let signal_path_escaped =+ shell_escape::escape(signal_path.display().to_string().into()).into_owned();+// Configure skim options with Rust-based preview and mode switching keybindingslet options = SkimOptionsBuilder::default().height("90%".to_string())@@ -203,6 +372,7 @@ pub fn handle_picker("fg:-1,bg:-1,header:-1,matched:108,current:237,current_bg:251,current_match:108".to_string(),))+ .cmd_collector(Rc::new(RefCell::new(collector)) as Rc<RefCell<dyn CommandCollector>>).bind(vec![// Mode switching (1/2/3/4/5 keys change preview content)format!(@@ -227,8 +397,13 @@ pub fn handle_picker(),// 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(),+ // Remove selected worktree: write branch name to signal file, then+ // reload triggers PickerCollector which performs the removal and+ // streams updated items back — all without leaving the picker.+ format!(+ "alt-r:execute-silent(echo {{}} > {0})+reload(remove)",+ signal_path_escaped+ ),// 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(),@@ -240,13 +415,15 @@ pub fn handle_picker(.build().map_err(|e| anyhow::anyhow!("Failed to build skim options: {}", e))?;- // Create item receiver+ // Send initial items to skim via channel+ let items = shared_items.lock().unwrap();let (tx, rx): (SkimItemSender, SkimItemReceiver) = unbounded();- for item in items {- tx.send(item)+ for item in items.iter() {+ tx.send(Arc::clone(item)).map_err(|e| anyhow::anyhow!("Failed to send item to skim: {}", e))?;}drop(tx);+ drop(items);// Pre-compute all preview modes for all worktrees in parallel via rayon.// Each (worktree, mode) pair is a separate rayon task, allowing the thread pool@@ -307,17 +484,17 @@ pub fn handle_picker(}}- // Run skim+ // Run skim (single invocation — alt-r uses reload, not re-launch)let output = Skim::run_with(&options, Some(rx));- // Handle selection+ // Handle selection (signal file cleaned up by PreviewState::Drop)if let Some(out) = output&& !out.is_abort{- // Determine action: create (alt-c), remove (alt-r), or switch (enter)+ // Determine action: create (alt-c) or switch (enter)+ // Remove is handled inline via reload — it never reaches accept.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,};@@ -333,124 +510,85 @@ pub fn handle_picker(return Ok(());}- match action {- PickerAction::Remove => {- let selected = out.selected_items.first().context("No worktree selected")?;+ let should_create = matches!(action, PickerAction::Create);- // Access the underlying worktree data to determine removal strategy- let worktree_data = selected+ // Get branch name: from query if creating new, from selected item if switching.+ // For detached worktrees, use the path (same as `wt switch /path` from CLI).+ let selected = out.selected_items.first();+ let selected_name = selected.map(|item| {+ if !should_create+ && let Some(data) = item.as_any().downcast_ref::<WorktreeSkimItem>()- .and_then(|skim_item| skim_item.item.worktree_data());-- let config = repo.user_config();-- // Safe removal: no force-delete (-D), no force-worktree (-f)- let branch_name;- let target = if let Some(data) = worktree_data.filter(|d| d.detached) {- // Detached worktrees have no branch name — remove by path- // (same as `wt remove /path/to/worktree` from the CLI)- RemoveTarget::Path(&data.path)- } else {- branch_name = selected.output().to_string();- RemoveTarget::Branch(&branch_name)- };- let result = repo- .prepare_worktree_removal(target, BranchDeletionMode::SafeDelete, false, config)- .context("Failed to remove worktree")?;-- // Execute removal in foreground, no hooks, not quiet- handle_remove_output(&result, true, false, false)?;+ .and_then(|s| s.item.worktree_data())+ .filter(|d| d.detached)+ {+ return data.path.to_string_lossy().into_owned();}- PickerAction::Create | PickerAction::Switch => {- let should_create = matches!(action, PickerAction::Create);-- // Get branch name: from query if creating new, from selected item if switching.- // For detached worktrees, use the path (same as `wt switch /path` from CLI).- let selected = out.selected_items.first();- let selected_name = selected.map(|item| {- if !should_create- && let Some(data) = item- .as_any()- .downcast_ref::<WorktreeSkimItem>()- .and_then(|s| s.item.worktree_data())- .filter(|d| d.detached)- {- return data.path.to_string_lossy().into_owned();- }- item.output().to_string()- });- let query = out.query.trim().to_string();- let identifier = resolve_identifier(&action, query, selected_name)?;-- // Load config — reuse recovered repo if we recovered earlier- let repo = if is_recovered {- repo.clone()- } else {- Repository::current().context("Failed to switch worktree")?- };- // Load config, offering bare repo worktree-path fix if needed.- // Reload from disk so mutations are picked up by plan_switch.- let mut config =- worktrunk::config::UserConfig::load().context("Failed to load config")?;- offer_bare_repo_worktree_path_fix(&repo, &mut config)?;-- // Run pre-switch hooks before branch resolution or worktree creation.- // {{ branch }} receives the raw user input (before resolution).- // Skip when recovered — the source worktree is gone, nothing to run hooks against.- if !is_recovered {- run_pre_switch_hooks(&repo, &config, &identifier, true)?;- }-- // Switch to existing worktree or create new one- let plan = plan_switch(&repo, &identifier, should_create, None, false, &config)?;- let hooks_approved = approve_switch_hooks(&repo, &config, &plan, false, true)?;- let (result, branch_info) =- execute_switch(&repo, plan, &config, false, hooks_approved)?;-- // Compute path mismatch lazily (deferred from plan_switch for existing worktrees).- // Skip for detached HEAD worktrees (branch is None).- let branch_info = match &result {- SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {- let expected_path = branch_info- .branch- .as_deref()- .and_then(|b| path_mismatch(&repo, b, path, &config));- SwitchBranchInfo {- expected_path,- ..branch_info- }- }- _ => branch_info,- };+ item.output().to_string()+ });+ let query = out.query.trim().to_string();+ let identifier = resolve_identifier(&action, query, selected_name)?;++ // Load config — reuse recovered repo if we recovered earlier+ let repo = if is_recovered {+ repo.clone()+ } else {+ Repository::current().context("Failed to switch worktree")?+ };+ // Load config, offering bare repo worktree-path fix if needed.+ // Reload from disk so mutations are picked up by plan_switch.+ let mut config = worktrunk::config::UserConfig::load().context("Failed to load config")?;+ offer_bare_repo_worktree_path_fix(&repo, &mut config)?;++ // Run pre-switch hooks before branch resolution or worktree creation.+ // {{ branch }} receives the raw user input (before resolution).+ // Skip when recovered — the source worktree is gone, nothing to run hooks against.+ if !is_recovered {+ run_pre_switch_hooks(&repo, &config, &identifier, true)?;+ }- // Show success message; emit cd directive if shell integration is active- // 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,- change_dir,- Some(&source_root),- &cwd,- )?;-- // Spawn background hooks after success message- if hooks_approved {- let extra_vars = switch_extra_vars(&result);- spawn_switch_background_hooks(- &repo,- &config,- &result,- branch_info.branch.as_deref(),- false,- &extra_vars,- hooks_display_path.as_deref(),- )?;+ // Switch to existing worktree or create new one+ let plan = plan_switch(&repo, &identifier, should_create, None, false, &config)?;+ let hooks_approved = approve_switch_hooks(&repo, &config, &plan, false, true)?;+ let (result, branch_info) = execute_switch(&repo, plan, &config, false, hooks_approved)?;++ // Compute path mismatch lazily (deferred from plan_switch for existing worktrees).+ // Skip for detached HEAD worktrees (branch is None).+ let branch_info = match &result {+ SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {+ let expected_path = branch_info+ .branch+ .as_deref()+ .and_then(|b| path_mismatch(&repo, b, path, &config));+ SwitchBranchInfo {+ expected_path,+ ..branch_info}}+ _ => branch_info,+ };++ // Show success message; emit cd directive if shell integration is active+ // 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, change_dir, Some(&source_root), &cwd)?;++ // Spawn background hooks after success message+ if hooks_approved {+ let extra_vars = switch_extra_vars(&result);+ spawn_switch_background_hooks(+ &repo,+ &config,+ &result,+ branch_info.branch.as_deref(),+ false,+ &extra_vars,+ hooks_display_path.as_deref(),+ )?;}}@@ -485,22 +623,16 @@ fn resolve_identifier(}}},- PickerAction::Remove => match selected_name {- Some(name) => Ok(name),- None => {- anyhow::bail!(- "No worktree selected — type a name that matches an existing worktree"- );- }- },}}#[cfg(test)]pub mod tests {use super::preview::{PreviewLayout, PreviewMode, PreviewStateData};- use super::{PickerAction, resolve_identifier};+ use super::{PickerAction, PickerCollector, resolve_identifier};+ use crate::commands::worktree::{BranchDeletionMode, RemoveResult};use std::fs;+ use worktrunk::shell_exec::Cmd;#[test]fn test_preview_state_data_roundtrip() {@@ -569,18 +701,73 @@ pub mod tests {// Create with empty query is an errorlet result = resolve_identifier(&PickerAction::Create, String::new(), None);assert!(result.unwrap_err().to_string().contains("no branch name"));+ }- // Remove returns the selected name- let result = resolve_identifier(&PickerAction::Remove, String::new(), Some("main".into()));- assert_eq!(result.unwrap(), "main");+ #[test]+ fn test_execute_removal_removes_worktree_and_branch() {+ let tmp = tempfile::TempDir::new().unwrap();+ let repo_path = tmp.path().join("repo");+ let wt_path = tmp.path().join("repo.feature");++ Cmd::new("git")+ .args(["init", "--initial-branch=main", repo_path.to_str().unwrap()])+ .run()+ .unwrap();+ fs::write(repo_path.join("file.txt"), "hello").unwrap();+ Cmd::new("git")+ .args(["add", "."])+ .current_dir(&repo_path)+ .run()+ .unwrap();+ Cmd::new("git")+ .args(["commit", "-m", "init"])+ .current_dir(&repo_path)+ .run()+ .unwrap();+ Cmd::new("git")+ .args([+ "worktree",+ "add",+ "-b",+ "feature",+ wt_path.to_str().unwrap(),+ ])+ .current_dir(&repo_path)+ .run()+ .unwrap();+ assert!(wt_path.exists());++ let result = RemoveResult::RemovedWorktree {+ main_path: repo_path.clone(),+ worktree_path: wt_path.clone(),+ changed_directory: false,+ branch_name: Some("feature".to_string()),+ deletion_mode: BranchDeletionMode::SafeDelete,+ target_branch: Some("main".to_string()),+ integration_reason: None,+ force_worktree: false,+ expected_path: None,+ removed_commit: None,+ };- // Remove with no selection is an error- let result = resolve_identifier(&PickerAction::Remove, String::new(), None);- assert!(- result- .unwrap_err()- .to_string()- .contains("No worktree selected")- );+ PickerCollector::execute_removal(&result).unwrap();+ assert!(!wt_path.exists(), "worktree should be removed");++ let output = Cmd::new("git")+ .args(["branch", "--list", "feature"])+ .current_dir(&repo_path)+ .run()+ .unwrap();+ assert!(output.stdout.is_empty(), "branch should be deleted");+ }++ #[test]+ fn test_execute_removal_branch_only_is_noop() {+ let result = RemoveResult::BranchOnly {+ branch_name: "gone".to_string(),+ deletion_mode: BranchDeletionMode::SafeDelete,+ pruned: false,+ };+ PickerCollector::execute_removal(&result).unwrap();}}diff --git a/src/commands/picker/preview.rs b/src/commands/picker/preview.rsindex 957172e0e..32342183d 100644--- a/src/commands/picker/preview.rs+++ b/src/commands/picker/preview.rs@@ -202,6 +202,8 @@ impl PreviewState {impl Drop for PreviewState {fn drop(&mut self) {let _ = fs::remove_file(&self.path);+ // Clean up the removal signal file used by alt-r (see PickerCollector)+ let _ = fs::remove_file(self.path.with_extension("remove"));}}