Skip to main content

identify

Function identify 

Source
pub fn identify(
    declared_commit: Option<&str>,
    declared_dirty: Option<&str>,
    git: impl FnMut(&[&str]) -> Option<String>,
) -> (String, &'static str)
Expand description

Decide the commit and the dirty flag from what the build was handed and what git says.

declared_commit is MAJORDOMUS_BUILD_COMMIT and declared_dirty is MAJORDOMUS_BUILD_DIRTY; git runs git with the given arguments and returns its standard output on success. The commit is the declared one when it names something (neither empty nor unknown), else git’s HEAD, else unknown. The flag is true/false when it was declared (true/1, false/0); otherwise it is read from git only when git also named the commit — a commit handed in from outside says nothing about the tree it came from — and is unknown when neither said.

use majordomus_cli::build_identity::{identify, STATUS_ARGS};

// git names the commit and finds a tracked change in the crate: the build is dirty
let git = |args: &[&str]| {
    Some(if args == STATUS_ARGS { " M src/lib.rs\n" } else { "abc123\n" }.to_string())
};
assert_eq!(identify(None, None, git), ("abc123".to_string(), "true"));

// a declared commit says nothing about the tree, so its dirtiness stays `unknown`
let never = |args: &[&str]| -> Option<String> { panic!("git asked {args:?}") };
assert_eq!(identify(Some(" def456 "), None, never), ("def456".to_string(), "unknown"));