Skip to main content

changeset_git/
lib.rs

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