use std::process::Command; /// Exposes the current commit and the crate name to the code through `env!()` fn main() { let git_hash = 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().to_owned()) // No repository in a docker build: the hash is given as a build argument .or_else(|| std::env::var("GIT_HASH").ok()) .map(|hash| hash.chars().take(8).collect::()) .unwrap_or_else(|| "unknown".to_owned()); println!("cargo:rustc-env=GIT_HASH={git_hash}"); println!( "cargo:rustc-env=CRATE_NAME={}", env!("CARGO_PKG_NAME").replace("-", "_") ); println!("cargo:rerun-if-changed=.git/HEAD"); println!("cargo:rerun-if-env-changed=GIT_HASH"); }