use std::path::Path;
use std::process::Command;
fn main() {
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let repo_root = manifest_dir
.parent()
.and_then(Path::parent)
.unwrap_or(manifest_dir);
watch_git_head(repo_root);
let output = Command::new("git")
.arg("-C")
.arg(repo_root)
.arg("rev-parse")
.arg("HEAD")
.output();
let Ok(output) = output else {
return;
};
if !output.status.success() {
return;
}
let commit = String::from_utf8_lossy(&output.stdout);
let commit = commit.trim();
if !commit.is_empty() {
println!("cargo:rustc-env=GIT_COMMIT={commit}");
}
}
fn watch_git_head(repo_root: &Path) {
let git_dir = repo_root.join(".git");
let head_path = git_dir.join("HEAD");
println!("cargo:rerun-if-changed={}", head_path.display());
let Ok(head) = std::fs::read_to_string(&head_path) else {
return;
};
let Some(reference) = head.trim().strip_prefix("ref: ") else {
return;
};
println!(
"cargo:rerun-if-changed={}",
git_dir.join(reference).display()
);
let packed_refs = git_dir.join("packed-refs");
if packed_refs.exists() {
println!("cargo:rerun-if-changed={}", packed_refs.display());
}
}