Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Move `remove` and `prepare` methods to `RepositoryCliExt`

Maximilian Roos80e0b458b9b2
diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rs
index 8db5ad744..dd7deb943 100644
--- a/src/commands/repository_ext.rs
+++ b/src/commands/repository_ext.rs
@@ -1,12 +1,15 @@
use rayon::prelude::*;
-use std::path::Path;
+use std::path::{Path, PathBuf};
+use std::process;
+use std::time::{SystemTime, UNIX_EPOCH};
use super::list::model::{BranchInfo, ListData, ListItem, WorktreeInfo};
+use super::worktree::RemoveResult;
use worktrunk::config::ProjectConfig;
use worktrunk::git::{GitError, GitResultExt, Repository};
use worktrunk::styling::{
- ERROR, ERROR_EMOJI, HINT, HINT_BOLD, HINT_EMOJI, WARNING, WARNING_BOLD, WARNING_EMOJI,
- format_with_gutter, println,
+ CYAN, CYAN_BOLD, ERROR, ERROR_EMOJI, HINT, HINT_BOLD, HINT_EMOJI, WARNING, WARNING_BOLD,
+ WARNING_EMOJI, format_with_gutter, println,
};
/// CLI-only helpers implemented on [`Repository`] via an extension trait so we can keep orphan
@@ -28,6 +31,24 @@ pub trait RepositoryCliExt {
fetch_ci: bool,
check_conflicts: bool,
) -> Result<Option<ListData>, GitError>;
+
+ /// Remove the currently checked-out worktree or switch back to the default branch if invoked
+ /// from the primary repo root.
+ fn remove_current_worktree(&self, no_delete_branch: bool) -> Result<RemoveResult, GitError>;
+
+ /// Remove a worktree identified by branch name.
+ fn remove_worktree_by_name(
+ &self,
+ branch_name: &str,
+ no_delete_branch: bool,
+ ) -> Result<RemoveResult, GitError>;
+
+ /// Prepare the target worktree for push by auto-stashing non-overlapping changes when safe.
+ fn prepare_target_worktree(
+ &self,
+ target_worktree: Option<&PathBuf>,
+ target_branch: &str,
+ ) -> Result<Option<TargetWorktreeStash>, GitError>;
}
impl RepositoryCliExt for Repository {
@@ -163,6 +184,174 @@ impl RepositoryCliExt for Repository {
current_worktree_path,
}))
}
+
+ fn remove_current_worktree(&self, no_delete_branch: bool) -> Result<RemoveResult, GitError> {
+ self.ensure_clean_working_tree()?;
+
+ if self.is_in_worktree()? {
+ let worktree_root = self.worktree_root()?;
+ let current_branch = self
+ .current_branch()?
+ .ok_or_else(|| GitError::CommandFailed("Not on a branch".to_string()))?;
+ let worktrees = self.list_worktrees()?;
+ let primary_worktree_dir = worktrees.worktrees[0].path.clone();
+
+ return Ok(RemoveResult::RemovedWorktree {
+ primary_path: primary_worktree_dir,
+ worktree_path: worktree_root,
+ changed_directory: true,
+ branch_name: current_branch,
+ no_delete_branch,
+ });
+ }
+
+ let current_branch = self.current_branch()?;
+ let default_branch = self.default_branch()?;
+
+ if current_branch.as_deref() == Some(&default_branch) {
+ return Ok(RemoveResult::AlreadyOnDefault(default_branch));
+ }
+
+ if let Err(err) = self.run_command(&["switch", &default_branch]) {
+ return Err(match err {
+ GitError::CommandFailed(msg) => GitError::SwitchFailed {
+ branch: default_branch.clone(),
+ error: msg,
+ },
+ other => other,
+ });
+ }
+
+ Ok(RemoveResult::SwitchedToDefault(default_branch))
+ }
+
+ fn remove_worktree_by_name(
+ &self,
+ branch_name: &str,
+ no_delete_branch: bool,
+ ) -> Result<RemoveResult, GitError> {
+ let worktree_path = match self.worktree_for_branch(branch_name)? {
+ Some(path) => path,
+ None => {
+ return Err(GitError::NoWorktreeFound {
+ branch: branch_name.to_string(),
+ });
+ }
+ };
+
+ if !worktree_path.exists() {
+ return Err(GitError::WorktreeMissing {
+ branch: branch_name.to_string(),
+ });
+ }
+
+ let target_repo = Repository::at(&worktree_path);
+ target_repo.ensure_clean_working_tree()?;
+
+ let current_worktree = self.worktree_root()?;
+ let removing_current = current_worktree == worktree_path;
+
+ let (primary_path, changed_directory) = if removing_current {
+ let worktrees = self.list_worktrees()?;
+ (worktrees.worktrees[0].path.clone(), true)
+ } else {
+ (current_worktree, false)
+ };
+
+ Ok(RemoveResult::RemovedWorktree {
+ primary_path,
+ worktree_path,
+ changed_directory,
+ branch_name: branch_name.to_string(),
+ no_delete_branch,
+ })
+ }
+
+ fn prepare_target_worktree(
+ &self,
+ target_worktree: Option<&PathBuf>,
+ target_branch: &str,
+ ) -> Result<Option<TargetWorktreeStash>, GitError> {
+ let Some(wt_path) = target_worktree else {
+ return Ok(None);
+ };
+
+ let wt_repo = Repository::at(wt_path);
+ if !wt_repo.is_dirty()? {
+ return Ok(None);
+ }
+
+ let push_files = self.changed_files(target_branch, "HEAD")?;
+ let wt_status_output = wt_repo.run_command(&["status", "--porcelain"])?;
+
+ let wt_files: Vec<String> = wt_status_output
+ .lines()
+ .filter_map(|line| {
+ line.split_once(' ')
+ .map(|(_, filename)| filename.trim().to_string())
+ })
+ .collect();
+
+ let overlapping: Vec<String> = push_files
+ .iter()
+ .filter(|f| wt_files.contains(f))
+ .cloned()
+ .collect();
+
+ if !overlapping.is_empty() {
+ return Err(GitError::ConflictingChanges {
+ files: overlapping,
+ worktree_path: wt_path.clone(),
+ });
+ }
+
+ let nanos = SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_nanos();
+ let stash_name = format!(
+ "worktrunk autostash::{}::{}::{}",
+ target_branch,
+ process::id(),
+ nanos
+ );
+
+ crate::output::progress(format!(
+ "{CYAN}Stashing changes in {CYAN_BOLD}{}{CYAN_BOLD:#}{CYAN}...{CYAN:#}",
+ wt_path.display()
+ ))?;
+
+ let stash_output =
+ wt_repo.run_command(&["stash", "push", "--include-untracked", "-m", &stash_name])?;
+
+ if stash_output.contains("No local changes to save") {
+ return Ok(None);
+ }
+
+ let list_output = wt_repo.run_command(&["stash", "list", "--format=%gd%x00%gs%x00"])?;
+ let mut parts = list_output.split('\0');
+ let mut stash_ref = None;
+ while let Some(id) = parts.next() {
+ if id.is_empty() {
+ continue;
+ }
+ if let Some(message) = parts.next()
+ && (message == stash_name || message.ends_with(&stash_name))
+ {
+ stash_ref = Some(id.to_string());
+ break;
+ }
+ }
+
+ let Some(stash_ref) = stash_ref else {
+ return Err(GitError::CommandFailed(format!(
+ "Failed to locate autostash entry '{}'",
+ stash_name
+ )));
+ };
+
+ Ok(Some(TargetWorktreeStash::new(wt_path, stash_ref)))
+ }
}
fn load_project_config_at(repo_root: &Path) -> Result<Option<ProjectConfig>, GitError> {
@@ -176,3 +365,39 @@ fn get_untracked_files(status_output: &str) -> Vec<String> {
.map(|filename| filename.to_string())
.collect()
}
+
+pub(crate) struct TargetWorktreeStash {
+ repo: Repository,
+ path: PathBuf,
+ stash_ref: String,
+}
+
+impl TargetWorktreeStash {
+ pub(crate) fn new(path: &Path, stash_ref: String) -> Self {
+ Self {
+ repo: Repository::at(path),
+ path: path.to_path_buf(),
+ stash_ref,
+ }
+ }
+
+ pub(crate) fn restore(self) -> Result<(), GitError> {
+ crate::output::progress(format!(
+ "{CYAN}Restoring stashed changes in {CYAN_BOLD}{}{CYAN_BOLD:#}{CYAN}...{CYAN:#}",
+ self.path.display()
+ ))?;
+
+ if let Err(_e) = self
+ .repo
+ .run_command(&["stash", "pop", "--quiet", &self.stash_ref])
+ {
+ crate::output::warning(format!(
+ "{WARNING}Failed to restore stash {WARNING_BOLD}{stash_ref}{WARNING_BOLD:#}{WARNING} - run 'git stash pop {stash_ref}' in {WARNING_BOLD}{path}{WARNING_BOLD:#}{WARNING:#}",
+ stash_ref = self.stash_ref,
+ path = self.path.display(),
+ ))?;
+ }
+
+ Ok(())
+ }
+}
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index ddcd10652..03b2ff31b 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -99,12 +99,9 @@
//! The shell wrapper is generated by `wt init <shell>` from templates in `templates/`.
use std::path::PathBuf;
-use std::time::{SystemTime, UNIX_EPOCH};
use worktrunk::config::{CommandPhase, WorktrunkConfig};
use worktrunk::git::{GitError, GitResultExt, Repository};
-use worktrunk::styling::{
- CYAN, CYAN_BOLD, GREEN, GREEN_BOLD, WARNING, WARNING_BOLD, format_with_gutter,
-};
+use worktrunk::styling::{CYAN, CYAN_BOLD, GREEN, GREEN_BOLD, WARNING, format_with_gutter};
use super::command_executor::CommandContext;
use super::hooks::{HookFailureStrategy, HookPipeline};
@@ -333,244 +330,8 @@ pub fn handle_remove(
// Two modes: remove current worktree vs. remove by name
match resolved_name.as_deref() {
- 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,
- no_delete_branch: bool,
-) -> Result<RemoveResult, GitError> {
- // Check for uncommitted changes in current worktree
- repo.ensure_clean_working_tree()?;
-
- // Check if we're in a worktree (fast - just checks git metadata)
- let in_worktree = repo.is_in_worktree()?;
-
- if in_worktree {
- // 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.worktrees[0].path.clone();
-
- // Return paths - deletion will happen in output handler after cd directive is emitted
- Ok(RemoveResult::RemovedWorktree {
- 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
- // Only fetch default_branch if needed (can be slow - network call)
- let current_branch = repo.current_branch()?;
- let default_branch = repo.default_branch()?;
-
- if current_branch.as_deref() == Some(&default_branch) {
- return Ok(RemoveResult::AlreadyOnDefault(default_branch));
- }
-
- // Switch to default branch
- if let Err(err) = repo.run_command(&["switch", &default_branch]) {
- return Err(match err {
- GitError::CommandFailed(msg) => GitError::SwitchFailed {
- branch: default_branch.clone(),
- error: msg,
- },
- other => other,
- });
- }
-
- Ok(RemoveResult::SwitchedToDefault(default_branch))
- }
-}
-
-/// Remove a worktree by branch name (branch_name should already be resolved)
-fn remove_worktree_by_name(
- repo: &Repository,
- branch_name: &str,
- no_delete_branch: bool,
-) -> Result<RemoveResult, GitError> {
- // Find the worktree for this branch
- let worktree_path = repo.worktree_for_branch(branch_name)?;
-
- let worktree_path = match worktree_path {
- Some(path) => path,
- None => {
- return Err(GitError::NoWorktreeFound {
- branch: branch_name.to_string(),
- });
- }
- };
-
- // Check if the target worktree exists on disk
- if !worktree_path.exists() {
- return Err(GitError::WorktreeMissing {
- branch: branch_name.to_string(),
- });
- }
-
- // Check if the target worktree is clean (no uncommitted changes)
- let target_repo = Repository::at(&worktree_path);
- target_repo.ensure_clean_working_tree()?;
-
- // Get the current worktree root to check if we're removing ourselves
- let current_worktree = repo.worktree_root()?;
- let removing_current = current_worktree == worktree_path;
-
- // Return paths for all cases - deletion happens in output handler
- let (primary_path, changed_directory) = if removing_current {
- // Removing current worktree - will cd to primary worktree
- let worktrees = repo.list_worktrees()?;
- (worktrees.worktrees[0].path.clone(), true)
- } else {
- // Removing different worktree - stay in current location
- (repo.worktree_root()?, false)
- };
-
- Ok(RemoveResult::RemovedWorktree {
- primary_path,
- worktree_path,
- changed_directory,
- branch_name: branch_name.to_string(),
- no_delete_branch,
- })
-}
-
-/// Auto-stash dirty target worktree changes when safe (no overlapping files)
-fn prepare_target_worktree(
- repo: &Repository,
- target_worktree: Option<&std::path::PathBuf>,
- target_branch: &str,
-) -> Result<Option<TargetWorktreeStash>, GitError> {
- let Some(wt_path) = target_worktree else {
- return Ok(None);
- };
-
- let wt_repo = Repository::at(wt_path);
- if !wt_repo.is_dirty()? {
- return Ok(None);
- }
-
- // Get files changed in the push
- let push_files = repo.changed_files(target_branch, "HEAD")?;
-
- // Get files changed in the worktree
- let wt_status_output = wt_repo.run_command(&["status", "--porcelain"])?;
-
- let wt_files: Vec<String> = wt_status_output
- .lines()
- .filter_map(|line| {
- // Parse porcelain format: "XY filename"
- line.split_once(' ')
- .map(|(_, filename)| filename.trim().to_string())
- })
- .collect();
-
- // Find overlapping files
- let overlapping: Vec<String> = push_files
- .iter()
- .filter(|f| wt_files.contains(f))
- .cloned()
- .collect();
-
- if !overlapping.is_empty() {
- return Err(GitError::ConflictingChanges {
- files: overlapping,
- worktree_path: wt_path.to_path_buf(),
- });
- }
-
- // Generate unique stash label so we can restore the exact entry even if others are created
- let nanos = SystemTime::now()
- .duration_since(UNIX_EPOCH)
- .unwrap_or_default()
- .as_nanos();
- let stash_name = format!(
- "worktrunk autostash::{}::{}::{}",
- target_branch,
- std::process::id(),
- nanos
- );
-
- crate::output::progress(format!(
- "{CYAN}Stashing changes in {CYAN_BOLD}{}{CYAN_BOLD:#}{CYAN}...{CYAN:#}",
- wt_path.display()
- ))?;
-
- let stash_output =
- wt_repo.run_command(&["stash", "push", "--include-untracked", "-m", &stash_name])?;
-
- if stash_output.contains("No local changes to save") {
- return Ok(None);
- }
-
- let list_output = wt_repo.run_command(&["stash", "list", "--format=%gd%x00%gs%x00"])?;
- let mut parts = list_output.split('\0');
- let mut stash_ref = None;
- while let Some(id) = parts.next() {
- if id.is_empty() {
- continue;
- }
- if let Some(message) = parts.next()
- && (message == stash_name || message.ends_with(&stash_name))
- {
- stash_ref = Some(id.to_string());
- break;
- }
- }
-
- let Some(stash_ref) = stash_ref else {
- return Err(GitError::CommandFailed(format!(
- "Failed to locate autostash entry '{}'",
- stash_name
- )));
- };
-
- Ok(Some(TargetWorktreeStash::new(wt_path, stash_ref)))
-}
-
-/// Auto-restores stashed changes in the target worktree after push completes
-struct TargetWorktreeStash {
- repo: Repository,
- path: std::path::PathBuf,
- stash_ref: String,
-}
-
-impl TargetWorktreeStash {
- fn new(path: &std::path::Path, stash_ref: String) -> Self {
- Self {
- repo: Repository::at(path),
- path: path.to_path_buf(),
- stash_ref,
- }
- }
-
- fn restore(self) -> Result<(), GitError> {
- crate::output::progress(format!(
- "{CYAN}Restoring stashed changes in {CYAN_BOLD}{}{CYAN_BOLD:#}{CYAN}...{CYAN:#}",
- self.path.display()
- ))?;
-
- if let Err(_e) = self
- .repo
- .run_command(&["stash", "pop", "--quiet", &self.stash_ref])
- {
- crate::output::warning(format!(
- "{WARNING}Failed to restore stash {WARNING_BOLD}{stash_ref}{WARNING_BOLD:#}{WARNING} - run 'git stash pop {stash_ref}' in {WARNING_BOLD}{path}{WARNING_BOLD:#}{WARNING:#}",
- stash_ref = self.stash_ref,
- path = self.path.display(),
- ))?;
- }
-
- Ok(())
+ None => repo.remove_current_worktree(no_delete_branch),
+ Some(name) => repo.remove_worktree_by_name(name, no_delete_branch),
}
}
@@ -714,7 +475,7 @@ pub fn handle_push(
// Check for conflicting changes in target worktree (auto-stash safe changes)
let target_worktree = repo.worktree_for_branch(&target_branch)?;
let mut target_worktree_stash =
- prepare_target_worktree(&repo, target_worktree.as_ref(), &target_branch)?;
+ repo.prepare_target_worktree(target_worktree.as_ref(), &target_branch)?;
// Count commits and show what will be pushed
let commit_count = repo.count_commits(&target_branch, "HEAD")?;