Expand description
Which commit an executable was built from, and whether that tree was dirty: the decision
build.rs makes once, at build time, and compiles in as MAJORDOMUS_COMMIT and
MAJORDOMUS_DIRTY (read back as crate::COMMIT and crate::DIRTY).
It lives here rather than inline in the build script so that it can be tested: a build
script has no test harness, and the constants it emits can only be compared with
themselves. build.rs compiles this file through #[path], the way it compiles
generation.rs, so there is one definition of the decision and the tests below exercise
the one the build runs. For the same reason nothing outside std may be used here, and
every item must be reached by the build script or it is dead code in that compilation.
The dirty flag describes the crate’s own directory (the build script asks git with the
pathspec . from CARGO_MANIFEST_DIR), as it stood when the build script last ran: an
edit to documentation elsewhere in the repository does not mark the executable dirty, and
a commit that touches no crate source does not rerun the build script, so the flag says
what the build saw and not what the tree says now.
use majordomus_cli::build_identity::{identify, HEAD_ARGS, STATUS_ARGS};
// a plain `cargo build` in a checkout: git names the commit and reports a clean crate
let sha = "0123456789abcdef0123456789abcdef01234567";
let git = |args: &[&str]| match args {
a if a == HEAD_ARGS => Some(format!("{sha}\n")),
a if a == STATUS_ARGS => Some(String::new()),
_ => None,
};
assert_eq!(identify(None, None, git), (sha.to_string(), "false"));
// a release build hands the commit and the flag in, and git is never asked
let never = |args: &[&str]| -> Option<String> { panic!("git asked {args:?}") };
assert_eq!(identify(Some(sha), Some("0"), never), (sha.to_string(), "false"));
// a source tarball has neither git nor a declaration: both halves are `unknown`
assert_eq!(identify(None, None, |_| None), ("unknown".to_string(), "unknown"));Constants§
- HEAD_
ARGS - The arguments the build script hands git to learn the commit.
- STATUS_
ARGS - The arguments the build script hands git to learn whether the crate’s tree is dirty: tracked changes only, under the build script’s working directory (the crate), and without the optional index refresh — a build is a read and must not rewrite the index of a repository another session may be staging into.
Functions§
- identify
- Decide the commit and the dirty flag from what the build was handed and what git says.