use std::path::Path;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ShallowState {
pub shallow: bool,
pub boundary: u64,
}
impl ShallowState {
#[must_use]
pub fn read(root: &Path) -> Self {
let mut command = crate::git_env::git_command(root);
command
.args([
"rev-parse",
"--is-shallow-repository",
"--git-path",
"shallow",
])
.current_dir(root);
let Ok(output) = command.output() else {
return Self::default();
};
let stdout = String::from_utf8_lossy(&output.stdout);
let mut lines = stdout.lines();
let shallow = lines
.next()
.is_some_and(|line| line.trim().eq_ignore_ascii_case("true"));
let boundary = lines
.next()
.and_then(|path| std::fs::read(root.join(path.trim())).ok())
.map_or(0, |bytes| xxhash_rust::xxh3::xxh3_64(&bytes));
Self { shallow, boundary }
}
}
#[must_use]
pub fn mailmap_fingerprint(root: &Path) -> u64 {
let mut command = crate::git_env::git_command(root);
command
.args(["rev-parse", "--show-toplevel"])
.current_dir(root);
let Ok(output) = command.output() else {
return 0;
};
if !output.status.success() {
return 0;
}
let toplevel = String::from_utf8_lossy(&output.stdout);
let path = Path::new(toplevel.trim()).join(".mailmap");
let is_regular_file = std::fs::symlink_metadata(&path).is_ok_and(|meta| meta.is_file());
if !is_regular_file {
return 0;
}
std::fs::read(path).map_or(0, |bytes| xxhash_rust::xxh3::xxh3_64(&bytes))
}
pub fn get_head_sha(root: &Path) -> Option<String> {
crate::repo_refs::head_sha(root).ok().flatten()
}
pub fn is_ancestor(root: &Path, ancestor: &str, descendant: &str) -> bool {
let mut command = crate::git_env::git_command(root);
command
.args(["merge-base", "--is-ancestor", ancestor, descendant])
.current_dir(root);
command.status().is_ok_and(|s| s.success())
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ChurnCoverage {
pub root_key: u64,
pub head_sha: String,
pub shallow: ShallowState,
pub mailmap: u64,
pub earliest_cutoff_secs: Option<u64>,
}
impl ChurnCoverage {
#[must_use]
pub fn covers(&self, wanted: &Self) -> bool {
self.root_key == wanted.root_key
&& self.shallow == wanted.shallow
&& self.mailmap == wanted.mailmap
&& cutoff_covers(self.earliest_cutoff_secs, wanted.earliest_cutoff_secs)
}
#[must_use]
pub fn extended_to(&self, head_sha: &str, rolled_cutoff_secs: Option<u64>) -> Self {
let earliest_cutoff_secs = match (self.earliest_cutoff_secs, rolled_cutoff_secs) {
(Some(have), Some(rolled)) => Some(have.max(rolled)),
(have, rolled) => have.or(rolled),
};
Self {
root_key: self.root_key,
head_sha: head_sha.to_owned(),
shallow: self.shallow,
mailmap: self.mailmap,
earliest_cutoff_secs,
}
}
}
fn cutoff_covers(have: Option<u64>, want: Option<u64>) -> bool {
match (have, want) {
(None, _) => true,
(Some(_), None) => false,
(Some(have), Some(want)) => have <= want,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn coverage(root_key: u64, cutoff: Option<u64>) -> ChurnCoverage {
ChurnCoverage {
root_key,
head_sha: "a".to_owned(),
shallow: ShallowState::default(),
mailmap: 0,
earliest_cutoff_secs: cutoff,
}
}
#[cfg(unix)]
#[test]
fn mailmap_fingerprint_reads_only_a_regular_file() {
let dir = tempfile::tempdir().expect("repo tempdir");
let root = dir.path();
let status = crate::git_fixture::fixture_git_command(root)
.args(["init", "--quiet"])
.status()
.expect("run git init");
assert!(status.success());
assert_eq!(mailmap_fingerprint(root), 0);
std::fs::write(root.join("aliases"), "A <a@x.test> <b@x.test>\n").expect("write aliases");
std::os::unix::fs::symlink(root.join("aliases"), root.join(".mailmap"))
.expect("symlink mailmap");
assert_eq!(mailmap_fingerprint(root), 0);
std::fs::remove_file(root.join(".mailmap")).expect("remove symlink");
std::fs::copy(root.join("aliases"), root.join(".mailmap")).expect("copy mailmap");
assert_ne!(mailmap_fingerprint(root), 0);
}
#[test]
fn covers_requires_the_same_mailmap() {
let have = coverage(1, None);
let mut wanted = coverage(1, None);
wanted.mailmap = 7;
assert!(!have.covers(&wanted));
}
#[test]
fn covers_requires_the_same_root() {
assert!(!coverage(1, Some(100)).covers(&coverage(2, Some(100))));
}
#[test]
fn covers_requires_the_same_shallow_state() {
let wanted = ChurnCoverage {
shallow: ShallowState {
shallow: true,
boundary: 7,
},
..coverage(1, Some(100))
};
assert!(!coverage(1, Some(100)).covers(&wanted));
}
#[test]
fn covers_compares_the_cutoffs() {
let have = coverage(1, Some(100));
assert!(have.covers(&coverage(1, Some(100))));
assert!(have.covers(&coverage(1, Some(150))));
assert!(!have.covers(&coverage(1, Some(99))));
assert!(!have.covers(&coverage(1, None)));
let unbounded = coverage(1, None);
assert!(unbounded.covers(&coverage(1, Some(1))));
assert!(unbounded.covers(&coverage(1, None)));
}
#[test]
fn covers_ignores_the_revision() {
let wanted = ChurnCoverage {
head_sha: "b".to_owned(),
..coverage(1, Some(100))
};
assert!(coverage(1, Some(100)).covers(&wanted));
}
#[test]
fn extended_to_keeps_the_later_cutoff() {
let cases = [
(Some(100), Some(150), Some(150)),
(Some(150), Some(100), Some(150)),
(None, Some(5), Some(5)),
(Some(5), None, Some(5)),
(None, None, None),
];
for (have, rolled, expected) in cases {
let extended = coverage(1, have).extended_to("b", rolled);
assert_eq!(
extended.earliest_cutoff_secs, expected,
"{have:?} {rolled:?}"
);
assert_eq!(extended.head_sha, "b");
}
}
}