//! Build script: embeds the short git commit hash of the build as `WH_GIT_HASH`. use std::process::Command; /// Name of the compile-time environment variable holding the commit hash. const GIT_HASH_ENV: &str = "WH_GIT_HASH"; /// Value used when the hash cannot be read (no git, not a repository). const UNKNOWN_HASH: &str = "unknown"; /// Purpose: export the short commit hash to the crate and rebuild when the commit changes. /// Inputs: none (runs `git` in the package directory). /// Output: none. /// Side effects: prints cargo directives on stdout. fn main() { println!("cargo:rerun-if-changed=.git/HEAD"); println!("cargo:rerun-if-changed=.git/refs"); println!("cargo:rerun-if-changed=.git/packed-refs"); let hash = git_short_hash().unwrap_or_else(|| UNKNOWN_HASH.to_owned()); println!("cargo:rustc-env={GIT_HASH_ENV}={hash}"); } /// Purpose: read the short hash of the checked-out commit. /// Inputs: none. /// Output: the hash, or `None` when git is missing, fails or prints nothing. /// Side effects: runs `git rev-parse --short HEAD`. fn git_short_hash() -> Option { let output = Command::new("git").args(["rev-parse", "--short", "HEAD"]).output().ok()?; if !output.status.success() { return None; } let hash = String::from_utf8(output.stdout).ok()?.trim().to_owned(); (!hash.is_empty()).then_some(hash) }