Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: inline worktree removal into callers (#1683)
Maximilian Roos4dbd428f72b4
diff --git a/src/commands/mod.rs b/src/commands/mod.rsindex 50ee13f00..f8a228386 100644--- a/src/commands/mod.rs+++ b/src/commands/mod.rs@@ -43,13 +43,13 @@ pub(crate) use list::handle_list;pub(crate) use merge::{MergeOptions, handle_merge};#[cfg(unix)]pub(crate) use picker::handle_picker;+pub(crate) use repository_ext::RemoveTarget;pub(crate) use step_commands::{PromoteResult, RebaseResult, SquashResult, handle_promote, handle_rebase, handle_squash,step_commit, step_copy_ignored, step_diff, step_prune, step_relocate, step_show_squash_prompt,};pub(crate) use worktree::{- OperationMode, handle_remove, handle_remove_current, handle_remove_path,- is_worktree_at_expected_path, resolve_worktree_arg, worktree_display_name,+ OperationMode, is_worktree_at_expected_path, resolve_worktree_arg, worktree_display_name,};// Re-export Shell from the canonical locationdiff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex 7bd8b5c6b..04664929c 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -21,8 +21,9 @@ 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::{- SwitchBranchInfo, SwitchResult, execute_switch, handle_remove, handle_remove_path,+ BranchDeletionMode, SwitchBranchInfo, SwitchResult, execute_switch,offer_bare_repo_worktree_path_fix, path_mismatch, plan_switch,};use crate::output::{handle_remove_output, handle_switch_output};@@ -345,26 +346,18 @@ pub fn handle_picker(let config = repo.user_config();// Safe removal: no force-delete (-D), no force-worktree (-f)- let result = if let Some(data) = worktree_data.filter(|d| d.detached) {+ 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)- handle_remove_path(- &data.path, false, // keep_branch: delete branch (default behavior)- false, // force_delete: no -D- false, // force_worktree: no -f- config,- )+ RemoveTarget::Path(&data.path)} else {- let branch_name = selected.output().to_string();- handle_remove(- &branch_name,- false, // keep_branch: delete branch (default behavior)- false, // force_delete: no -D- false, // force_worktree: no -f- config,- )- }- .context("Failed to remove worktree")?;+ 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 quiethandle_remove_output(&result, true, false, false)?;diff --git a/src/commands/worktree/mod.rs b/src/commands/worktree/mod.rsindex 2aa0fecf0..4582aa598 100644--- a/src/commands/worktree/mod.rs+++ b/src/commands/worktree/mod.rs@@ -80,14 +80,12 @@mod hooks;mod push;-mod remove;mod resolve;mod switch;mod types;// Re-export public types and functionspub use push::{handle_no_ff_merge, handle_push};-pub use remove::{handle_remove, handle_remove_current, handle_remove_path};pub(crate) use resolve::paths_match;pub use resolve::{compute_worktree_path, is_worktree_at_expected_path, offer_bare_repo_worktree_path_fix,diff --git a/src/commands/worktree/remove.rs b/src/commands/worktree/remove.rsdeleted file mode 100644index a991206bf..000000000--- a/src/commands/worktree/remove.rs+++ /dev/null@@ -1,65 +0,0 @@-//! Worktree remove operations.--use worktrunk::config::UserConfig;-use worktrunk::git::Repository;--use super::types::{BranchDeletionMode, RemoveResult};-use crate::commands::repository_ext::{RemoveTarget, RepositoryCliExt};--/// Remove a worktree by branch name.-pub fn handle_remove(- worktree_name: &str,- keep_branch: bool,- force_delete: bool,- force_worktree: bool,- config: &UserConfig,-) -> anyhow::Result<RemoveResult> {- let repo = Repository::current()?;-- // Progress message is shown in handle_removed_worktree_output() after pre-remove hooks run- repo.prepare_worktree_removal(- RemoveTarget::Branch(worktree_name),- BranchDeletionMode::from_flags(keep_branch, force_delete),- force_worktree,- config,- )-}--/// Remove a worktree by path (supports detached HEAD worktrees).-pub fn handle_remove_path(- path: &std::path::Path,- keep_branch: bool,- force_delete: bool,- force_worktree: bool,- config: &UserConfig,-) -> anyhow::Result<RemoveResult> {- let repo = Repository::current()?;-- repo.prepare_worktree_removal(- RemoveTarget::Path(path),- BranchDeletionMode::from_flags(keep_branch, force_delete),- force_worktree,- config,- )-}--/// Handle removing the current worktree (supports detached HEAD state).-///-/// This is the path-based removal that handles the "@" shorthand, including-/// when HEAD is detached.-pub fn handle_remove_current(- keep_branch: bool,- force_delete: bool,- force_worktree: bool,- config: &UserConfig,-) -> anyhow::Result<RemoveResult> {- let repo = Repository::current()?;-- // Progress message is shown in handle_removed_worktree_output() after pre-remove hooks run- repo.prepare_worktree_removal(- RemoveTarget::Current,- BranchDeletionMode::from_flags(keep_branch, force_delete),- force_worktree,- config,- )-}diff --git a/src/main.rs b/src/main.rsindex 46d6d803d..5c262be90 100644--- a/src/main.rs+++ b/src/main.rs@@ -43,16 +43,17 @@ pub(crate) use crate::cli::OutputFormat;#[cfg(unix)]use commands::handle_picker;-use commands::worktree::{handle_no_ff_merge, handle_push};+use commands::repository_ext::RepositoryCliExt;+use commands::worktree::{BranchDeletionMode, handle_no_ff_merge, handle_push};use commands::{- MergeOptions, OperationMode, RebaseResult, SquashResult, SwitchOptions, add_approvals,- clear_approvals, handle_completions, handle_config_create, handle_config_show,+ MergeOptions, OperationMode, RebaseResult, RemoveTarget, SquashResult, SwitchOptions,+ add_approvals, clear_approvals, handle_completions, handle_config_create, handle_config_show,handle_config_update, handle_configure_shell, handle_hints_clear, handle_hints_get,handle_hook_show, handle_init, handle_list, handle_logs_get, handle_merge, handle_promote,- handle_rebase, handle_remove, handle_remove_current, handle_remove_path, handle_show_theme,- handle_squash, handle_state_clear, handle_state_clear_all, handle_state_get, handle_state_set,- handle_state_show, handle_switch, handle_unconfigure_shell, resolve_worktree_arg, run_hook,- step_commit, step_copy_ignored, step_diff, step_eval, step_for_each, step_prune, step_relocate,+ handle_rebase, handle_show_theme, handle_squash, handle_state_clear, handle_state_clear_all,+ handle_state_get, handle_state_set, handle_state_show, handle_switch, handle_unconfigure_shell,+ resolve_worktree_arg, run_hook, step_commit, step_copy_ignored, step_diff, step_eval,+ step_for_each, step_prune, step_relocate,};use output::handle_remove_output;@@ -599,6 +600,8 @@ fn validate_remove_targets(.collect()};+ let deletion_mode = BranchDeletionMode::from_flags(keep_branch, force_delete);+let mut plans = RemovePlans {others: Vec::new(),branch_only: Vec::new(),@@ -623,7 +626,12 @@ fn validate_remove_targets(let is_current = current_worktree.as_ref() == Some(&path_canonical);if is_current {- match handle_remove_current(keep_branch, force_delete, force, config) {+ match repo.prepare_worktree_removal(+ RemoveTarget::Current,+ deletion_mode,+ force,+ config,+ ) {Ok(result) => plans.current = Some(result),Err(e) => plans.record_error(e),}@@ -632,18 +640,23 @@ fn validate_remove_targets(// Non-current worktree: remove by branch name, or by path for// detached worktrees (which have no branch).- let result = if let Some(ref branch_name) = branch {- handle_remove(branch_name, keep_branch, force_delete, force, config)+ let target = if let Some(ref branch_name) = branch {+ RemoveTarget::Branch(branch_name)} else {- handle_remove_path(&path_canonical, keep_branch, force_delete, force, config)+ RemoveTarget::Path(&path_canonical)};- match result {+ match repo.prepare_worktree_removal(target, deletion_mode, force, config) {Ok(result) => plans.others.push(result),Err(e) => plans.record_error(e),}}ResolvedWorktree::BranchOnly { branch } => {- match handle_remove(&branch, keep_branch, force_delete, force, config) {+ match repo.prepare_worktree_removal(+ RemoveTarget::Branch(&branch),+ deletion_mode,+ force,+ config,+ ) {Ok(result) => plans.branch_only.push(result),Err(e) => plans.record_error(e),}@@ -691,13 +704,14 @@ fn handle_remove_command(spec: RemoveCommandArgs) -> anyhow::Result<()> {if branches.is_empty() {// Single worktree removal: validate FIRST, then approve, then execute- let result = handle_remove_current(- !spec.delete_branch,- spec.force_delete,- spec.force,- &config,- )- .context("Failed to remove worktree")?;+ let result = repo+ .prepare_worktree_removal(+ RemoveTarget::Current,+ BranchDeletionMode::from_flags(!spec.delete_branch, spec.force_delete),+ spec.force,+ &config,+ )+ .context("Failed to remove worktree")?;// Early exit for benchmarking time-to-first-outputif std::env::var_os("WORKTRUNK_FIRST_OUTPUT").is_some() {