Skip to content

Commit 4398d6e

Browse files
committed
feat: add Repository::is_empty() to emulate the similar git2 API
1 parent c8a63ab commit 4398d6e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

gix/src/repository/location.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::path::{Path, PathBuf};
23

34
use gix_path::realpath::MAX_SYMLINKS;
@@ -108,4 +109,18 @@ impl crate::Repository {
108109
None => crate::repository::Kind::Bare,
109110
}
110111
}
112+
113+
/// Returns `Some(true)` if the reference database [is untouched](gix_ref::file::Store::is_pristine()).
114+
/// Return `None` if a defect in the database makes the answer uncertain.
115+
pub fn is_pristine(&self) -> Option<bool> {
116+
let name = self
117+
.config
118+
.resolved
119+
.string(crate::config::tree::Init::DEFAULT_BRANCH)
120+
.unwrap_or(Cow::Borrowed("master".into()));
121+
let default_branch_ref_name: gix_ref::FullName = format!("refs/heads/{name}")
122+
.try_into()
123+
.unwrap_or_else(|_| gix_ref::FullName::try_from("refs/heads/master").expect("known to be valid"));
124+
self.refs.is_pristine(dbg!(default_branch_ref_name).as_ref())
125+
}
111126
}

gix/tests/gix/repository/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ mod index {
5656
repo.index_or_load_from_head_or_empty()?.entries().is_empty(),
5757
"an empty index is created on the fly"
5858
);
59+
assert_eq!(
60+
repo.is_pristine(),
61+
Some(false),
62+
"not pristine as it things the initial ref was changed to 'main'"
63+
);
64+
assert_eq!(
65+
repo.refs.is_pristine("refs/heads/main".try_into()?),
66+
Some(true),
67+
"This is a quirk of default values in gix and the way we override the initial branch for test fixtures"
68+
);
5969
Ok(())
6070
}
6171
}

0 commit comments

Comments
 (0)