From 1509c42734e360d32c235f8e54dda5835f52e82b Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Wed, 9 Jul 2025 22:20:37 -0600 Subject: [PATCH] add git::worktree_is_dirty() This returns Ok(true) if the worktree has any modified files (staged or unstaged), or any added (staged) files. Ok(false) if not. Ignores untracked files. --- src/git.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/git.rs b/src/git.rs index d83389e..54b5735 100644 --- a/src/git.rs +++ b/src/git.rs @@ -106,6 +106,17 @@ pub fn git_branch_exists(branch: &str) -> Result { return Ok(result.status.success()); } +pub fn worktree_is_dirty(dir: &str) -> Result { + // `git status --porcelain` prints a terse list of files added or + // modified (both staged and not), and new untracked files. So if + // says *anything at all* it means the worktree is dirty. + let result = std::process::Command::new("git") + .args(["status", "--porcelain", "--untracked-files=no"]) + .current_dir(dir) + .output()?; + return Ok(result.stdout.len() > 0); +} + pub fn git_commit_file(file: &std::path::Path) -> Result<(), GitError> { let mut git_dir = std::path::PathBuf::from(file); git_dir.pop();