pub trait Ancestry {
// Required methods
fn has(&self, commit: &str) -> Option<bool>;
fn contains(&self, descendant: &str, ancestor: &str) -> Option<bool>;
}Expand description
What git knows about commits, asked through a trait so that the judgement is testable
without a repository. Both answers are three-valued: None is “git could not be asked”,
which is not a false, and collapsing the two is how a record from a history this clone
has never seen gets read as current.
use majordomus_cli::served::Ancestry;
struct OneCommit;
impl Ancestry for OneCommit {
fn has(&self, c: &str) -> Option<bool> { Some(c == "a".repeat(40)) }
fn contains(&self, d: &str, a: &str) -> Option<bool> { Some(d == a) }
}
assert_eq!(OneCommit.has(&"a".repeat(40)), Some(true));
assert_eq!(OneCommit.has(&"f".repeat(40)), Some(false));Required Methods§
Sourcefn has(&self, commit: &str) -> Option<bool>
fn has(&self, commit: &str) -> Option<bool>
Does this clone hold commit? None when git cannot be asked.
use majordomus_cli::served::{Ancestry, GitAncestry};
use std::path::Path;
// a sha no repository holds resolves to a `false`, not to an error
let git = GitAncestry { root: Path::new(env!("CARGO_MANIFEST_DIR")) };
assert_eq!(git.has(&"f".repeat(40)), Some(false));Sourcefn contains(&self, descendant: &str, ancestor: &str) -> Option<bool>
fn contains(&self, descendant: &str, ancestor: &str) -> Option<bool>
Does descendant contain ancestor (equal, or reachable from it)? None when git
cannot be asked.
use majordomus_cli::served::{Ancestry, GitAncestry};
use std::path::Path;
let git = GitAncestry { root: Path::new(env!("CARGO_MANIFEST_DIR")) };
// every commit contains itself, which is what makes an exact match a proof
let head = String::from_utf8(
std::process::Command::new("git").args(["-C", env!("CARGO_MANIFEST_DIR"), "rev-parse", "HEAD"])
.output().unwrap().stdout).unwrap();
let head = head.trim();
assert_eq!(git.contains(head, head), Some(true));Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".