use std::io;
use std::process::Command;
const GIT_HASH_ENV_VAR: &str = "GIT_SHA_SHORT";
fn main() {
match get_git_commit_hash() {
Ok(git_hash) => {
println!("cargo:rustc-env={GIT_HASH_ENV_VAR}={git_hash}");
}
Err(e) => {
println!("cargo:warning=Note: Failed to get git commit hash: {}", e);
println!("cargo:rustc-env={GIT_HASH_ENV_VAR}=unknown");
}
}
}
fn get_git_commit_hash() -> Result<String, io::Error> {
let output = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.output()?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
Err(io::Error::new(io::ErrorKind::Other, "Git command failed"))
}
}