use std::process::Command; fn git(args: &[&str]) -> Option { let output = Command::new("git") .args(args) .output() .ok()?; if !output.status.success() { return None; } Some(String::from_utf8(output.stdout) .expect("git output not utf8") .trim() .to_string()) } fn main() { // Re-run if HEAD changes println!("cargo:rerun-if-changed=.git/HEAD"); println!("cargo:rerun-if-changed=.git/refs"); if let Some(git_hash) = git(&["rev-parse", "HEAD"]) { println!("cargo:rustc-env=GIT_HASH={git_hash}"); } if let Some(git_hash_short) = git(&["rev-parse", "--short", "HEAD"]) { println!("cargo:rustc-env=GIT_HASH_SHORT={git_hash_short}"); } let git_dirty = Command::new("git") .args(["diff-index", "--quiet", "HEAD", "--"]) .status() .map(|s| !s.success()) .unwrap_or(false); if git_dirty { println!("cargo:rustc-env=GIT_DIRTY=1"); } }