use std::env; fn main() { let rev = get_value_from_env("GIT_VERSION") .or_else(|| get_value_from_command("git", ["rev-parse", "--short", "HEAD"])) .unwrap_or_else(|| "unknown".to_owned()); let branch = get_value_from_env("GIT_BRANCH") .or_else(|| get_value_from_command("git", ["rev-parse", "--abbrev-ref", "HEAD"])) .unwrap_or_else(|| "unknown".to_owned()); println!("cargo:rustc-env=GIT_REVISION={}", rev); println!("cargo:rustc-env=GIT_BRANCH={}", branch); println!("cargo:rerun-if-env-changed=GIT_REVISION"); } fn get_value_from_env(key: &str) -> Option { env::var(key).map_or_else(|_| None, Some) } fn get_value_from_command, S: AsRef>( cmd: &str, args: I, ) -> Option { std::process::Command::new(cmd) .args(args) .output() .map_or_else( |_| None, |out| { if !out.status.success() { return None; } match std::str::from_utf8(&out.stdout) { Ok(value) => Some(value.to_owned()), Err(_) => None, } }, ) }