Skip to main content

contains

Function contains 

Source
pub fn contains(root: &Path, descendant: &str, ancestor: &str) -> Containment
Expand description

Does descendant contain ancestor?

Containment::CommitUnknown unless both commits resolve here; otherwise is_ancestor decides. The resolution comes first because is_ancestor answers Some(false) for an object this clone does not have, which is the right answer for its own caller and the wrong one here: a record naming a commit nobody can find is not a record from a known other history.

use majordomus_cli::git::{contains, Containment};
use std::process::Command;

let dir = tempfile::tempdir().unwrap();
let git = |args: &[&str]| {
    let out = Command::new("git").arg("-C").arg(dir.path()).args(args).output().unwrap();
    String::from_utf8_lossy(&out.stdout).trim().to_string()
};
git(&["init", "-q"]);
git(&["config", "user.email", "t@example.com"]);
git(&["config", "user.name", "t"]);
git(&["commit", "-q", "--allow-empty", "-m", "one"]);
let old = git(&["rev-parse", "HEAD"]);
git(&["commit", "-q", "--allow-empty", "-m", "two"]);
let new = git(&["rev-parse", "HEAD"]);

assert_eq!(contains(dir.path(), &new, &old), Containment::Contains);
assert_eq!(contains(dir.path(), &old, &new), Containment::DoesNotContain);
// a commit contains itself
assert_eq!(contains(dir.path(), &new, &new), Containment::Contains);
assert_eq!(
    contains(dir.path(), &new, &"0".repeat(40)),
    Containment::CommitUnknown
);