Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ fn is_git_repo(path: String) -> PyResult<bool> {
}

#[pyfunction]
fn is_clean(_path: String) -> PyResult<bool> {
todo!("repo::is_clean (gix status, empty)")
fn is_clean(path: String) -> PyResult<bool> {
Ok(crate::repo::is_clean(std::path::Path::new(&path))?)
}

#[pyfunction]
Expand Down
62 changes: 62 additions & 0 deletions src/repo/is_clean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::error::GitxtendError;
use crate::repo::Result;
use std::path::Path;

/// True iff the working tree is clean — no staged, modified, or untracked
/// entries — i.e. `git status --porcelain` would be empty.
pub fn is_clean(path: &Path) -> Result<bool> {
let repo = gix::open(path).map_err(GitxtendError::from_err)?;
let mut iter = repo
.status(gix::progress::Discard)
.map_err(GitxtendError::from_err)?
.untracked_files(gix::status::UntrackedFiles::Files)
.into_iter(Vec::<gix::bstr::BString>::new())
.map_err(GitxtendError::from_err)?;
// clean == the status iterator yields no entries (staged, modified, or untracked)
Ok(iter.next().is_none())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::repo::fixtures::{self, repo, write};

fn porcelain_clean(p: &std::path::Path) -> bool {
fixtures::git(p, &["status", "--porcelain"]).is_empty()
}

#[test]
fn test_is_clean_fresh_repo() {
let td = repo();
assert!(is_clean(td.path()).unwrap());
assert_eq!(is_clean(td.path()).unwrap(), porcelain_clean(td.path()));
}

#[test]
fn test_is_clean_untracked_file() {
let td = repo();
write(td.path(), "new.txt", "x");
assert!(!is_clean(td.path()).unwrap());
assert_eq!(is_clean(td.path()).unwrap(), porcelain_clean(td.path()));
}

#[test]
fn test_is_clean_modified_tracked_file() {
let td = repo();
write(td.path(), "README.md", "orig");
fixtures::git(td.path(), &["add", "README.md"]);
fixtures::git(td.path(), &["commit", "-m", "Add README"]);
write(td.path(), "README.md", "Modified content");
assert!(!is_clean(td.path()).unwrap());
assert_eq!(is_clean(td.path()).unwrap(), porcelain_clean(td.path()));
}

#[test]
fn test_is_clean_staged_change() {
let td = repo();
write(td.path(), "new.txt", "x");
fixtures::git(td.path(), &["add", "new.txt"]);
assert!(!is_clean(td.path()).unwrap());
assert_eq!(is_clean(td.path()).unwrap(), porcelain_clean(td.path()));
}
}
3 changes: 3 additions & 0 deletions src/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub use rev_list_count::rev_list_count;
mod log_subjects;
pub use log_subjects::log_subjects;

mod is_clean;
pub use is_clean::is_clean;

/// Temp-dir git fixtures shared by the per-method parity tests.
///
/// Fixtures are built with the real `git` CLI, so each parity test asserts
Expand Down
Loading