//! Record build metadata (git commit and branch) at compile time so the //! 'am version' command can display it, like Nushell's version table. //! //! When git is unavailable (e.g. building from a source tarball) the values //! are simply empty. use std::process::Command; fn git(args: &[&str]) -> String { Command::new("git") .args(args) .output() .ok() .filter(|o| o.status.success()) .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) .unwrap_or_default() } fn main() { println!("cargo:rerun-if-changed=.git/HEAD"); println!("cargo:rerun-if-changed=.git/refs/heads"); let commit = git(&["rev-parse", "HEAD"]); let branch = git(&["rev-parse", "--abbrev-ref", "HEAD"]); // A detached HEAD reports the literal branch name "HEAD". let branch = if branch == "HEAD" { String::new() } else { branch }; println!("cargo:rustc-env=AM_GIT_COMMIT={commit}"); println!("cargo:rustc-env=AM_GIT_BRANCH={branch}"); }