From ca353352f8dced335506ffb4bd839213b64afefb Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Wed, 9 Jul 2025 22:03:49 -0600 Subject: [PATCH] git::Worktree::drop() now force-drops the worktree This avoids leaving prunable worktrees around if we dirtied the worktree and didn't commit. This can happen in the following situation: 1. User runs `ent new`. 2. ent creates a new directory for the issue. 3. ent opens an editor to let the user type in the description of the new issue. The editor saves to `ISSUE/description`. 4. User changes their mind and no longer wants to make a new issue, so they save an empty buffer and exit the editor. 5. ent sees that the file is empty, and returns an error from Issue::edit_description(). 6. ent propagates the error up through program exit, and eventually the git::Worktree struct is dropped. Since the worktree is dirty (it has the new issue dir with an empty description file in it), `git worktree remove` fails. But `git worktree remove --force` works! --- src/git.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/git.rs b/src/git.rs index 6018392..d83389e 100644 --- a/src/git.rs +++ b/src/git.rs @@ -20,9 +20,24 @@ pub struct Worktree { impl Drop for Worktree { fn drop(&mut self) { - let _result = std::process::Command::new("git") - .args(["worktree", "remove", &self.path.path().to_string_lossy()]) + let result = std::process::Command::new("git") + .args([ + "worktree", + "remove", + "--force", + &self.path.path().to_string_lossy(), + ]) .output(); + match result { + Err(e) => { + println!("failed to run git: {:#?}", e); + } + Ok(result) => { + if !result.status.success() { + println!("failed to remove git worktree: {:#?}", result); + } + } + } } }