Skip to main content

changeset_git/
lib.rs

1mod error;
2mod repository;
3mod types;
4
5pub use error::GitError;
6pub use repository::Repository;
7pub use types::{CommitInfo, FileChange, FileStatus, TagInfo};
8
9use std::path::Path;
10
11pub type Result<T> = std::result::Result<T, GitError>;
12
13/// # Errors
14///
15/// Returns an error if the path is not a git repository or if the status check fails.
16pub fn is_working_tree_clean(path: &Path) -> Result<bool> {
17    Repository::open(path)?.is_working_tree_clean()
18}
19
20/// # Errors
21///
22/// Returns an error if the path is not a git repository or if HEAD is detached.
23pub fn current_branch(path: &Path) -> Result<String> {
24    Repository::open(path)?.current_branch()
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use crate::repository::tests::setup_test_repo;
31    use std::fs;
32
33    #[test]
34    fn is_working_tree_clean_via_public_fn() -> anyhow::Result<()> {
35        let (dir, _repo) = setup_test_repo()?;
36
37        assert!(is_working_tree_clean(dir.path())?);
38
39        fs::write(dir.path().join("untracked.txt"), "content")?;
40        assert!(!is_working_tree_clean(dir.path())?);
41
42        Ok(())
43    }
44
45    #[test]
46    fn current_branch_via_public_fn() -> anyhow::Result<()> {
47        let (dir, _repo) = setup_test_repo()?;
48        let branch = current_branch(dir.path())?;
49        assert!(branch == "main" || branch == "master");
50        Ok(())
51    }
52}