use std::process::Command; /// Exposes the current commit and the crate name to the code through `env!()` fn main() { // The docker build has no git repository: CI passes the commit in instead // (`--build-arg GIT_HASH=...`), so the running image stays traceable. let git_hash = std::env::var("GIT_HASH") .ok() .filter(|hash| !hash.is_empty() && hash != "unknown") .or_else(|| { Command::new("git") .args(["rev-parse", "HEAD"]) .output() .ok() .filter(|output| output.status.success()) .and_then(|output| String::from_utf8(output.stdout).ok()) }) .map(|hash| hash.trim().chars().take(8).collect::()) .unwrap_or_else(|| "unknown".to_owned()); println!("cargo:rustc-env=GIT_HASH={git_hash}"); println!("cargo:rerun-if-env-changed=GIT_HASH"); println!( "cargo:rustc-env=CRATE_NAME={}", env!("CARGO_PKG_NAME").replace("-", "_") ); println!("cargo:rerun-if-changed=.git/HEAD"); }