2022-03-31 19:14:21 +00:00
|
|
|
// Include the GIT_HASH, in `GIT_HASH` environment variable at build
|
|
|
|
// time, panic'ing if it can not be found
|
2021-02-18 10:50:14 +00:00
|
|
|
//
|
|
|
|
// https://stackoverflow.com/questions/43753491/include-git-commit-hash-as-string-into-rust-program
|
|
|
|
use std::process::Command;
|
|
|
|
|
2022-03-31 19:14:21 +00:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Populate env!(GIT_HASH) with the current git commit
|
|
|
|
println!("cargo:rustc-env=GIT_HASH={}", get_git_hash());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_git_hash() -> String {
|
|
|
|
let out = match std::env::var("VERSION_HASH") {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => {
|
|
|
|
let output = Command::new("git")
|
2022-11-05 01:45:06 +00:00
|
|
|
.args(["describe", "--always", "--dirty", "--abbrev=64"])
|
2022-03-31 19:14:21 +00:00
|
|
|
.output()
|
|
|
|
.expect("failed to execute git rev-parse to read the current git hash");
|
|
|
|
|
|
|
|
String::from_utf8(output.stdout).expect("non-utf8 found in git hash")
|
2021-02-18 10:50:14 +00:00
|
|
|
}
|
2022-03-31 19:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!out.is_empty(), "attempting to embed empty git hash");
|
|
|
|
out
|
2021-02-18 10:50:14 +00:00
|
|
|
}
|