Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: add --force hint when removing dirty worktrees (#671)
Maximilian Roos63def67f0d8c
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex b2d3434a1..fb33d1958 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -88,6 +88,7 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {return Err(worktrunk::git::GitError::UncommittedChanges {action: Some("merge with --no-commit".into()),branch: Some(current_branch.clone()),+ force_hint: false,}.into());}@@ -203,8 +204,11 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {if remove_effective {// STEP 1: Check for uncommitted changes before attempting cleanup// This prevents showing "Cleaning up worktree..." before failing- repo.current_worktree()- .ensure_clean("remove worktree after merge", Some(¤t_branch))?;+ repo.current_worktree().ensure_clean(+ "remove worktree after merge",+ Some(¤t_branch),+ false,+ )?;// STEP 2: Remove worktree via shared remove output handler so final message matches wt removelet worktree_root = repo.current_worktree().root()?.to_path_buf();diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rsindex 212b2152b..68be180c8 100644--- a/src/commands/repository_ext.rs+++ b/src/commands/repository_ext.rs@@ -163,7 +163,7 @@ impl RepositoryCliExt for Repository {// Check working tree cleanliness (unless --force, which passes through to git)if !force_worktree {- target_wt.ensure_clean("remove worktree", branch_name.as_deref())?;+ target_wt.ensure_clean("remove worktree", branch_name.as_deref(), true)?;}// Compute main_path and changed_directory based on whether we're removing currentdiff --git a/src/git/error.rs b/src/git/error.rsindex c3d1f7808..70f63a08b 100644--- a/src/git/error.rs+++ b/src/git/error.rs@@ -49,6 +49,8 @@ pub enum GitError {action: Option<String>,/// Branch name (for multi-worktree operations)branch: Option<String>,+ /// When true, hint mentions --force as an alternative to stashing+ force_hint: bool,},BranchAlreadyExists {branch: String,@@ -171,7 +173,11 @@ impl std::fmt::Display for GitError {)}- GitError::UncommittedChanges { action, branch } => {+ GitError::UncommittedChanges {+ action,+ branch,+ force_hint,+ } => {let message = match (action, branch) {(Some(action), Some(b)) => {cformat!("Cannot {action}: <bold>{b}</> has uncommitted changes")@@ -184,12 +190,17 @@ impl std::fmt::Display for GitError {}(None, None) => cformat!("Working tree has uncommitted changes"),};- write!(- f,- "{}\n{}",- error_message(&message),- hint_message("Commit or stash changes first")- )+ let hint = if *force_hint {+ // Construct full command: "wt remove [branch] --force"+ let args: Vec<&str> = branch.as_deref().into_iter().collect();+ let cmd = suggest_command("remove", &args, &["--force"]);+ cformat!(+ "Commit or stash changes first, or to lose uncommitted changes, run <bright-black>{cmd}</>"+ )+ } else {+ "Commit or stash changes first".to_string()+ };+ write!(f, "{}\n{}", error_message(&message), hint_message(hint))}GitError::BranchAlreadyExists { branch } => {@@ -1051,15 +1062,18 @@ mod tests {let err = GitError::UncommittedChanges {action: Some("push".into()),branch: None,+ force_hint: false,};let display = err.to_string();assert!(display.contains("Cannot push"));assert!(display.contains("working tree"));+ assert!(!display.contains("--force"));// Branch onlylet err = GitError::UncommittedChanges {action: None,branch: Some("feature".into()),+ force_hint: false,};let display = err.to_string();assert!(display.contains("feature"));@@ -1069,9 +1083,21 @@ mod tests {let err = GitError::UncommittedChanges {action: None,branch: None,+ force_hint: false,};let display = err.to_string();assert!(display.contains("Working tree"));++ // With force_hint+ let err = GitError::UncommittedChanges {+ action: Some("remove worktree".into()),+ branch: Some("feature".into()),+ force_hint: true,+ };+ let display = err.to_string();+ assert!(display.contains("Cannot remove worktree"));+ assert!(display.contains("wt remove feature --force"));+ assert!(display.contains("to lose uncommitted changes, run"));}#[test]diff --git a/src/git/repository/working_tree.rs b/src/git/repository/working_tree.rsindex b3922983d..ec4eab179 100644--- a/src/git/repository/working_tree.rs+++ b/src/git/repository/working_tree.rs@@ -188,11 +188,18 @@ impl<'a> WorkingTree<'a> {/// Returns an error if there are uncommitted changes./// - `action` describes what was blocked (e.g., "remove worktree")./// - `branch` identifies which branch for multi-worktree operations.- pub fn ensure_clean(&self, action: &str, branch: Option<&str>) -> anyhow::Result<()> {+ /// - `force_hint` when true, the error hint mentions `--force` as an alternative.+ pub fn ensure_clean(+ &self,+ action: &str,+ branch: Option<&str>,+ force_hint: bool,+ ) -> anyhow::Result<()> {if self.is_dirty()? {return Err(GitError::UncommittedChanges {action: Some(action.into()),branch: branch.map(String::from),+ force_hint,}.into());}diff --git a/tests/integration_tests/git_error_display.rs b/tests/integration_tests/git_error_display.rsindex eb879b483..dcd0b3412 100644--- a/tests/integration_tests/git_error_display.rs+++ b/tests/integration_tests/git_error_display.rs@@ -100,6 +100,7 @@ fn display_uncommitted_changes() {let err = GitError::UncommittedChanges {action: Some("remove worktree".into()),branch: None,+ force_hint: false,};assert_snapshot!("uncommitted_changes", err.to_string());@@ -110,11 +111,23 @@ fn display_uncommitted_changes_with_branch() {let err = GitError::UncommittedChanges {action: Some("remove worktree".into()),branch: Some("feature-branch".into()),+ force_hint: false,};assert_snapshot!("uncommitted_changes_with_branch", err.to_string());}+#[test]+fn display_uncommitted_changes_with_force_hint() {+ let err = GitError::UncommittedChanges {+ action: Some("remove worktree".into()),+ branch: Some("feature-branch".into()),+ force_hint: true,+ };++ assert_snapshot!("uncommitted_changes_with_force_hint", err.to_string());+}+#[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_force_hint.snap b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__uncommitted_changes_with_force_hint.snapnew file mode 100644index 000000000..6232f1d7e--- /dev/null+++ b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__uncommitted_changes_with_force_hint.snap@@ -0,0 +1,6 @@+---+source: tests/integration_tests/git_error_display.rs+expression: err.to_string()+---+[31m✗[39m [31mCannot remove worktree: [1mfeature-branch[22m has uncommitted changes[39m+[2m↳[22m [2mCommit or stash changes first, or to lose uncommitted changes, run [90mwt remove feature-branch --force[39m[22mdiff --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.snapindex d02e04171..58760d1b9 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@@ -18,6 +18,7 @@ info:HOME: "[TEST_HOME]"LANG: CLC_ALL: C+ MOCK_CONFIG_DIR: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpFvoLMc/mock-binPATH: "[PATH]"RUST_LOG: warnSOURCE_DATE_EPOCH: "1735776000"@@ -33,4 +34,4 @@ exit_code: 1----- stderr -----[31m✗[39m [31mCannot remove worktree: [1mfeature-dirty[22m has uncommitted changes[39m-[2m↳[22m [2mCommit or stash changes first[22m+[2m↳[22m [2mCommit or stash changes first, or to lose uncommitted changes, run [90mwt remove feature-dirty --force[39m[22m