Skip to main content

changed_between

Function changed_between 

Source
pub fn changed_between(
    root: &Path,
    evidence_commit: &str,
    presented: &Presented,
) -> Option<BTreeSet<String>>
Expand description

Every path that differs between the evidence commit and the presented revision, or None when git cannot answer — which no caller may read as “nothing changed”.

For the working tree this is the evidence module’s own working-tree diff: tracked changes committed since, staged or edited, and untracked files. For a presented commit it is the committed difference between the two commits, and nothing in the checkout.

An evidence commit git would read as an option (--output=<file>, which makes git diff write that file) is None before git is asked: a ledger row is data, and no data may choose git’s options.

use majordomus_cli::evidence::freshness::{changed_between, presented_commit, Presented};
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 e = git(&["rev-parse", "HEAD"]);
std::fs::write(dir.path().join("a.md"), "a").unwrap();
git(&["add", "-A"]);
git(&["commit", "-q", "-m", "two"]);
std::fs::write(dir.path().join("b.md"), "b").unwrap();

// the working tree carries the untracked file; the commit does not
let here = changed_between(dir.path(), &e, &Presented::WorkingTree).unwrap();
assert!(here.contains("a.md") && here.contains("b.md"));
let committed = presented_commit(dir.path(), "HEAD", None).unwrap();
let there = changed_between(dir.path(), &e, &committed).unwrap();
assert_eq!(there.into_iter().collect::<Vec<_>>(), ["a.md"]);