Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor: Extract worktree conflict check into a new function
Maximilian Roos9d9a72ca4fe4
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 42fcab2ea..d8f3ac4bc 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -56,8 +56,7 @@ pub fn handle_switch(.to_str().ok_or_else(|| GitError::CommandFailed("Invalid UTF-8 in path".to_string()))?;- let worktree_name = config.format_path(repo_name, branch);- let worktree_path = repo_root.join(worktree_name);+ let worktree_path = repo_root.join(config.format_path(repo_name, branch));// Create the worktree// Build git worktree add command@@ -159,6 +158,60 @@ pub fn handle_remove(internal: bool) -> Result<(), GitError> {Ok(())}+/// Check for conflicting uncommitted changes in target worktree+fn check_worktree_conflicts(+ repo: &Repository,+ target_worktree: &Option<std::path::PathBuf>,+ target_branch: &str,+) -> Result<(), GitError> {+ let Some(wt_path) = target_worktree else {+ return Ok(());+ };++ let wt_repo = Repository::at(wt_path);+ if !wt_repo.is_dirty()? {+ return Ok(());+ }++ // 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() {+ eprintln!(+ "{}",+ format_error("Cannot push: conflicting uncommitted changes in:")+ );+ for file in &overlapping {+ eprintln!(" - {}", file);+ }+ return Err(GitError::CommandFailed(format!(+ "Commit or stash changes in {} first",+ wt_path.display()+ )));+ }++ Ok(())+}+pub fn handle_push(target: Option<&str>, allow_merge_commits: bool) -> Result<(), GitError> {let repo = Repository::current();@@ -204,50 +257,9 @@ pub fn handle_push(target: Option<&str>, allow_merge_commits: bool) -> Result<().map_err(|e| GitError::CommandFailed(e.to_string()))?;}- // Find worktree for target branch+ // Check for conflicting changes in target worktreelet target_worktree = repo.worktree_for_branch(&target_branch)?;-- if let Some(ref wt_path) = target_worktree {- // Check if target worktree is dirty- let wt_repo = Repository::at(wt_path);- if wt_repo.is_dirty()? {- // 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() {- eprintln!(- "{}",- format_error("Cannot push: conflicting uncommitted changes in:")- );- for file in &overlapping {- eprintln!(" - {}", file);- }- return Err(GitError::CommandFailed(format!(- "Commit or stash changes in {} first",- wt_path.display()- )));- }- }- }+ check_worktree_conflicts(&repo, &target_worktree, &target_branch)?;// Count commits and show infolet commit_count = repo.count_commits(&target_branch, "HEAD")?;diff --git a/src/git.rs b/src/git.rsindex a228829ce..39dde7b2a 100644--- a/src/git.rs+++ b/src/git.rs@@ -198,7 +198,7 @@ impl Repository {pub fn changed_files(&self, base: &str, head: &str) -> Result<Vec<String>, GitError> {let range = format!("{}..{}", base, head);let stdout = self.run_command(&["diff", "--name-only", &range])?;- Ok(stdout.lines().map(|s| s.to_string()).collect())+ Ok(stdout.lines().map(String::from).collect())}/// Get commit timestamp in seconds since epoch.@@ -323,7 +323,7 @@ impl Repository {/// Get commit subjects (first line of commit message) from a range.pub fn commit_subjects(&self, range: &str) -> Result<Vec<String>, GitError> {let output = self.run_command(&["log", "--format=%s", range])?;- Ok(output.lines().map(|s| s.to_string()).collect())+ Ok(output.lines().map(String::from).collect())}/// Check if there are staged changes.