Skip to content

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

feat(error): list dirty files in `UncommittedChanges` (#2653)

Maximilian Roos4dd84ca18991
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 403e52e98..a9c076a3f 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -153,13 +153,17 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {
let current_wt = repo.current_worktree();
// Validate --no-commit: requires clean working tree
- if !commit && current_wt.is_dirty()? {
- return Err(worktrunk::git::GitError::UncommittedChanges {
- action: Some("merge with --no-commit".into()),
- branch: Some(current_branch),
- force_hint: false,
+ if !commit {
+ let dirty_files = current_wt.dirty_files()?;
+ if !dirty_files.is_empty() {
+ return Err(worktrunk::git::GitError::UncommittedChanges {
+ action: Some("merge with --no-commit".into()),
+ branch: Some(current_branch),
+ force_hint: false,
+ dirty_files,
+ }
+ .into());
}
- .into());
}
// --no-commit implies --no-squash
diff --git a/src/git/error.rs b/src/git/error.rs
index ec10226f6..902c505bd 100644
--- a/src/git/error.rs
+++ b/src/git/error.rs
@@ -387,6 +387,12 @@ pub enum GitError {
branch: Option<String>,
/// When true, hint mentions --force as an alternative to stashing
force_hint: bool,
+ /// Raw `git status --porcelain` lines (one per dirty entry). Rendered
+ /// as a dim, indented block between the title and the hint, so the
+ /// user knows *which* files are blocking — important when the dirty
+ /// state is unexpected (e.g., post-merge cleanup catching a stray file).
+ /// Empty vec is allowed (nothing extra rendered).
+ dirty_files: Vec<String>,
},
BranchAlreadyExists {
branch: String,
@@ -788,7 +794,10 @@ impl GitError {
}
GitError::UncommittedChanges {
- branch, force_hint, ..
+ branch,
+ force_hint,
+ dirty_files,
+ ..
} => {
let title = self.title();
let hint = if *force_hint {
@@ -801,7 +810,11 @@ impl GitError {
} else {
"Commit or stash changes first".to_string()
};
- write!(f, "{}\n{}", error_message(&title), hint_message(hint))
+ write!(f, "{}", error_message(&title))?;
+ for line in dirty_files {
+ write!(f, "\n {}", cformat!("<dim>{}</>", line))?;
+ }
+ write!(f, "\n{}", hint_message(hint))
}
GitError::BranchAlreadyExists { branch } => {
@@ -2085,6 +2098,7 @@ mod tests {
action: Some("push".into()),
branch: None,
force_hint: false,
+ dirty_files: Vec::new(),
};
let display = err.render();
assert_snapshot!(display, @"
@@ -2098,6 +2112,7 @@ mod tests {
action: None,
branch: Some("feature".into()),
force_hint: false,
+ dirty_files: Vec::new(),
};
assert_snapshot!(err.render(), @"
✗ feature has uncommitted changes
@@ -2109,6 +2124,7 @@ mod tests {
action: None,
branch: None,
force_hint: false,
+ dirty_files: Vec::new(),
};
assert_snapshot!(err.render(), @"
✗ Working tree has uncommitted changes
@@ -2120,11 +2136,28 @@ mod tests {
action: Some("remove worktree".into()),
branch: Some("feature".into()),
force_hint: true,
+ dirty_files: Vec::new(),
};
assert_snapshot!(err.render(), @"
✗ Cannot remove worktree: feature has uncommitted changes
↳ Commit or stash changes first, or to lose uncommitted changes, run wt remove --force feature
");
+
+ // With dirty_files populated — surfaces *which* files block the action.
+ // Each line is the raw `git status --porcelain` entry, dim, indented two
+ // columns.
+ let err = GitError::UncommittedChanges {
+ action: Some("remove worktree after merge".into()),
+ branch: Some("feature-auth".into()),
+ force_hint: false,
+ dirty_files: vec![" M auth.rs".into(), "?? .DS_Store".into()],
+ };
+ assert_snapshot!(err.render(), @"
+ ✗ Cannot remove worktree after merge: feature-auth has uncommitted changes
+  M auth.rs
+ ?? .DS_Store
+ ↳ Commit or stash changes first
+ ");
}
#[test]
diff --git a/src/git/repository/working_tree.rs b/src/git/repository/working_tree.rs
index a3acc3b5c..d508aaa21 100644
--- a/src/git/repository/working_tree.rs
+++ b/src/git/repository/working_tree.rs
@@ -333,6 +333,17 @@ impl<'a> WorkingTree<'a> {
Ok(!stdout.trim().is_empty())
}
+ /// Return the raw `git status --porcelain` lines for a dirty working tree
+ /// (one entry per line, trailing newline stripped, in git's order). Returns
+ /// an empty vec when the tree is clean. Use this where the error message
+ /// should tell the user *what* is dirty — e.g., when constructing
+ /// [`GitError::UncommittedChanges`] in [`Self::ensure_clean`]. The same
+ /// caveats as [`Self::is_dirty`] apply (skip-worktree files are invisible).
+ pub fn dirty_files(&self) -> anyhow::Result<Vec<String>> {
+ let stdout = self.run_command(&["status", "--porcelain"])?;
+ Ok(stdout.lines().map(str::to_owned).collect())
+ }
+
/// Get the root directory of this worktree (top-level of the working tree).
///
/// Returns the canonicalized absolute path to the top-level directory.
@@ -426,11 +437,13 @@ impl<'a> WorkingTree<'a> {
branch: Option<&str>,
force_hint: bool,
) -> anyhow::Result<()> {
- if self.is_dirty()? {
+ let dirty_files = self.dirty_files()?;
+ if !dirty_files.is_empty() {
return Err(GitError::UncommittedChanges {
action: Some(action.into()),
branch: branch.map(String::from),
force_hint,
+ dirty_files,
}
.into());
}
diff --git a/tests/integration_tests/git_error_display.rs b/tests/integration_tests/git_error_display.rs
index 42ddfcdbc..0418a435c 100644
--- a/tests/integration_tests/git_error_display.rs
+++ b/tests/integration_tests/git_error_display.rs
@@ -234,6 +234,7 @@ fn display_uncommitted_changes() {
action: Some("remove worktree".into()),
branch: None,
force_hint: false,
+ dirty_files: Vec::new(),
};
assert_snapshot!("uncommitted_changes", err.render());
@@ -245,6 +246,7 @@ fn display_uncommitted_changes_with_branch() {
action: Some("remove worktree".into()),
branch: Some("feature-branch".into()),
force_hint: false,
+ dirty_files: Vec::new(),
};
assert_snapshot!("uncommitted_changes_with_branch", err.render());
@@ -256,11 +258,24 @@ fn display_uncommitted_changes_with_force_hint() {
action: Some("remove worktree".into()),
branch: Some("feature-branch".into()),
force_hint: true,
+ dirty_files: Vec::new(),
};
assert_snapshot!("uncommitted_changes_with_force_hint", err.render());
}
+#[test]
+fn display_uncommitted_changes_with_dirty_files() {
+ let err = GitError::UncommittedChanges {
+ action: Some("remove worktree after merge".into()),
+ branch: Some("feature-auth".into()),
+ force_hint: false,
+ dirty_files: vec![" M auth.rs".into(), "?? .DS_Store".into()],
+ };
+
+ assert_snapshot!("uncommitted_changes_with_dirty_files", err.render());
+}
+
#[test]
fn display_branch_already_exists() {
let err = GitError::BranchAlreadyExists {
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__uncommitted_changes_with_dirty_files.snap b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__uncommitted_changes_with_dirty_files.snap
new file mode 100644
index 000000000..47f8fdc0f
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__uncommitted_changes_with_dirty_files.snap
@@ -0,0 +1,8 @@
+---
+source: tests/integration_tests/git_error_display.rs
+expression: err.render()
+---
+✗ Cannot remove worktree after merge: feature-auth has uncommitted changes
+  M auth.rs
+ ?? .DS_Store
+↳ Commit or stash changes first
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_with_no_commit.snap b/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_with_no_commit.snap
index d5cc049d0..7a5dfd280 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_with_no_commit.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_error_uncommitted_changes_with_no_commit.snap
@@ -13,10 +13,15 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -32,13 +37,17 @@ info:
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -47,4 +56,5 @@ exit_code: 1
----- stderr -----
✗ Cannot merge with --no-commit: feature has uncommitted changes
+ ?? dirty.txt
↳ Commit or stash changes first
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_no_commit_with_dirty_tree.snap b/tests/snapshots/integration__integration_tests__merge__merge_no_commit_with_dirty_tree.snap
index 8d42f2c26..52ca755ab 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_no_commit_with_dirty_tree.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_no_commit_with_dirty_tree.snap
@@ -12,10 +12,15 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -31,13 +36,17 @@ info:
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -46,4 +55,5 @@ exit_code: 1
----- stderr -----
✗ Cannot merge with --no-commit: feature has uncommitted changes
+ ?? uncommitted.txt
↳ 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 3f31bc497..114a5a218 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
@@ -11,10 +11,15 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -30,13 +35,17 @@ info:
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -45,4 +54,5 @@ exit_code: 1
----- stderr -----
✗ Cannot remove worktree: feature-dirty has uncommitted changes
+ ?? dirty.txt
↳ Commit or stash changes first, or to lose uncommitted changes, run wt remove --force feature-dirty
diff --git a/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_main.snap b/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_main.snap
index 5b2846f11..51b8bbec7 100644
--- a/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_main.snap
+++ b/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_main.snap
@@ -12,10 +12,15 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -31,13 +36,17 @@ info:
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -46,4 +55,5 @@ exit_code: 1
----- stderr -----
✗ Cannot promote: main has uncommitted changes
+ ?? dirty.txt
↳ Commit or stash changes first
diff --git a/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_target.snap b/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_target.snap
index 35a4f4761..bf096998c 100644
--- a/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_target.snap
+++ b/tests/snapshots/integration__integration_tests__step_promote__promote_dirty_target.snap
@@ -12,10 +12,15 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -31,13 +36,17 @@ info:
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -46,4 +55,5 @@ exit_code: 1
----- stderr -----
✗ Cannot promote: feature has uncommitted changes
+ ?? dirty.txt
↳ Commit or stash changes first