use std::process::Command;
fn git_output(args: &[&str]) -> Option<String> {
let output = Command::new("git").args(args).output().ok()?;
if !output.status.success() {
return None;
}
String::from_utf8(output.stdout)
.ok()
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty())
}
fn main() {
for path in ["HEAD", "packed-refs"] {
if let Some(path) = git_output(&["rev-parse", "--git-path", path]) {
println!("cargo:rerun-if-changed={path}");
}
}
if let Some(reference) = git_output(&["symbolic-ref", "-q", "HEAD"]) {
if let Some(path) = git_output(&["rev-parse", "--git-path", &reference]) {
println!("cargo:rerun-if-changed={path}");
}
}
let git_id =
git_output(&["rev-parse", "--short=7", "HEAD"]).unwrap_or_else(|| "unknown".to_string());
println!("cargo:rustc-env=GRAPHWALKER_GIT_ID={git_id}");
}