Skip to content

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

Include worktree identifier in uncommitted changes errors

Maximilian Roos0662a34851d8
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index c04ec9e06..40457e32c 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -85,6 +85,7 @@ pub fn handle_merge(
if !commit && repo.is_dirty()? {
return Err(worktrunk::git::GitError::UncommittedChanges {
action: Some("merge with --no-commit".into()),
+ worktree: Some(current_branch.clone()),
}
.into());
}
@@ -195,7 +196,7 @@ pub fn handle_merge(
if remove_effective {
// STEP 1: Check for uncommitted changes before attempting cleanup
// This prevents showing "Cleaning up worktree..." before failing
- repo.ensure_clean_working_tree(Some("remove worktree after merge"))?;
+ repo.ensure_clean_working_tree(Some("remove worktree after merge"), Some(&current_branch))?;
// STEP 2: Remove worktree via shared remove output handler so final message matches wt remove
let worktree_root = repo.worktree_root()?;
diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rs
index 33ebe0756..c193ce542 100644
--- a/src/commands/repository_ext.rs
+++ b/src/commands/repository_ext.rs
@@ -106,7 +106,7 @@ impl RepositoryCliExt for Repository {
}
let target_repo = Repository::at(&worktree_path);
- target_repo.ensure_clean_working_tree(Some("remove worktree"))?;
+ target_repo.ensure_clean_working_tree(Some("remove worktree"), Some(branch_name))?;
let current_worktree = self.worktree_root()?;
let removing_current = current_worktree == worktree_path;
@@ -166,7 +166,7 @@ impl RepositoryCliExt for Repository {
let branch_name = current_wt.and_then(|wt| wt.branch.clone());
// Ensure the working tree is clean
- self.ensure_clean_working_tree(Some("remove worktree"))?;
+ self.ensure_clean_working_tree(Some("remove worktree"), branch_name.as_deref())?;
// Get main worktree path (we're removing current, so we'll cd to main)
let main_path = worktrees.main().path.clone();
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index a3de0506d..2d38ffbc0 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -575,7 +575,7 @@ pub fn handle_remove_by_path(
// Check that the worktree is clean
let target_repo = Repository::at(path);
- target_repo.ensure_clean_working_tree(Some("remove worktree"))?;
+ target_repo.ensure_clean_working_tree(Some("remove worktree"), branch.as_deref())?;
// We're not in this worktree, so no directory change needed
let current_path = repo.worktree_root()?;
diff --git a/src/git/error.rs b/src/git/error.rs
index 26a3780da..3c5528aa4 100644
--- a/src/git/error.rs
+++ b/src/git/error.rs
@@ -42,6 +42,8 @@ pub enum GitError {
},
UncommittedChanges {
action: Option<String>,
+ /// Branch or worktree identifier (for multi-worktree operations)
+ worktree: Option<String>,
},
BranchAlreadyExists {
branch: String,
@@ -134,12 +136,18 @@ impl std::fmt::Display for GitError {
)
}
- GitError::UncommittedChanges { action } => {
- let message = match action {
- Some(action) => {
- format!("Cannot {action}: working tree has uncommitted changes")
+ GitError::UncommittedChanges { action, worktree } => {
+ let message = match (action, worktree) {
+ (Some(action), Some(wt)) => {
+ cformat!("Cannot {action}: <bold>{wt}</> has uncommitted changes")
+ }
+ (Some(action), None) => {
+ cformat!("Cannot {action}: working tree has uncommitted changes")
+ }
+ (None, Some(wt)) => {
+ cformat!("<bold>{wt}</> has uncommitted changes")
}
- None => "Working tree has uncommitted changes".to_string(),
+ (None, None) => cformat!("Working tree has uncommitted changes"),
};
cwrite!(
f,
@@ -466,9 +474,19 @@ mod tests {
let err = GitError::UncommittedChanges {
action: Some("remove worktree".into()),
+ worktree: None,
+ };
+ let output = err.to_string();
+ assert!(output.contains("Cannot remove worktree"));
+
+ // With worktree specified
+ let err = GitError::UncommittedChanges {
+ action: Some("remove worktree".into()),
+ worktree: Some("feature-branch".into()),
};
let output = err.to_string();
assert!(output.contains("Cannot remove worktree"));
+ assert!(output.contains("feature-branch"));
}
#[test]
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index eae523858..b2eff96e0 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -539,11 +539,17 @@ impl Repository {
/// Ensure the working tree is clean (no uncommitted changes).
///
/// Returns an error if there are uncommitted changes.
- /// `action` describes what was blocked (e.g., "remove worktree").
- pub fn ensure_clean_working_tree(&self, action: Option<&str>) -> anyhow::Result<()> {
+ /// - `action` describes what was blocked (e.g., "remove worktree").
+ /// - `worktree` identifies which worktree (e.g., branch name) for multi-worktree operations.
+ pub fn ensure_clean_working_tree(
+ &self,
+ action: Option<&str>,
+ worktree: Option<&str>,
+ ) -> anyhow::Result<()> {
if self.is_dirty()? {
return Err(GitError::UncommittedChanges {
action: action.map(String::from),
+ worktree: worktree.map(String::from),
}
.into());
}
diff --git a/tests/git_error_display.rs b/tests/git_error_display.rs
index 790c31738..933dc0502 100644
--- a/tests/git_error_display.rs
+++ b/tests/git_error_display.rs
@@ -97,11 +97,22 @@ fn display_detached_head_no_action() {
fn display_uncommitted_changes() {
let err = GitError::UncommittedChanges {
action: Some("remove worktree".into()),
+ worktree: None,
};
assert_snapshot!("uncommitted_changes", err.to_string());
}
+#[test]
+fn display_uncommitted_changes_with_worktree() {
+ let err = GitError::UncommittedChanges {
+ action: Some("remove worktree".into()),
+ worktree: Some("feature-branch".into()),
+ };
+
+ assert_snapshot!("uncommitted_changes_with_worktree", err.to_string());
+}
+
#[test]
fn display_branch_already_exists() {
let err = GitError::BranchAlreadyExists {
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index ab2f6f8f3..d56d3cc32 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -2502,7 +2502,7 @@ fn test_merge_no_commit_with_dirty_tree() {
----- stdout -----
----- stderr -----
- ❌ Cannot merge with --no-commit: working tree has uncommitted changes
+ ❌ Cannot merge with --no-commit: feature has uncommitted changes
πŸ’‘ Commit or stash changes first
");
diff --git a/tests/snapshots/git_error_display__uncommitted_changes_with_worktree.snap b/tests/snapshots/git_error_display__uncommitted_changes_with_worktree.snap
new file mode 100644
index 000000000..c06ea56ee
--- /dev/null
+++ b/tests/snapshots/git_error_display__uncommitted_changes_with_worktree.snap
@@ -0,0 +1,7 @@
+---
+source: tests/git_error_display.rs
+expression: err.to_string()
+---
+❌ Cannot remove worktree: feature-branch has uncommitted changes
+
+πŸ’‘ Commit or stash changes first
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_no_commit.snap b/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_no_commit.snap
index 1f2ec5eec..65a4e4ac9 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_no_commit.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_no_commit.snap
@@ -25,6 +25,6 @@ exit_code: 1
----- stdout -----
----- stderr -----
-❌ Cannot merge with --no-commit: working tree has uncommitted changes
+❌ Cannot merge with --no-commit: feature has uncommitted changes
πŸ’‘ Commit or stash changes first
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap b/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap
index e290dd3a3..53c536966 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap
@@ -23,6 +23,6 @@ exit_code: 1
----- stdout -----
----- stderr -----
-❌ Cannot remove worktree: working tree has uncommitted changes
+❌ Cannot remove worktree: feature-dirty has uncommitted changes
πŸ’‘ Commit or stash changes first