use std::path::Path;
use std::process::Command;
fn main() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set by cargo");
let repo_root = Path::new(&manifest_dir)
.parent()
.expect("roust-rs/ has a parent directory (the repo root)")
.to_path_buf();
let git_head = repo_root.join(".git").join("HEAD");
println!("cargo:rerun-if-changed={}", git_head.display());
if let Ok(head_contents) = std::fs::read_to_string(&git_head) {
if let Some(ref_path) = head_contents.trim().strip_prefix("ref: ") {
let ref_file = repo_root.join(".git").join(ref_path);
println!("cargo:rerun-if-changed={}", ref_file.display());
}
}
let sha = run_git(&repo_root, &["rev-parse", "--short", "HEAD"])
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".to_string());
let dirty_label = match run_git(&repo_root, &["status", "--porcelain", "--", "roust-rs/"]) {
Some(out) if !out.is_empty() => "dirty",
Some(_) => "clean",
None => "clean",
};
println!("cargo:rustc-env=ROUST_GIT_SHA={sha}");
println!("cargo:rustc-env=ROUST_GIT_DIRTY={dirty_label}");
}
fn run_git(repo_root: &Path, args: &[&str]) -> Option<String> {
Command::new("git")
.args(args)
.current_dir(repo_root)
.output()
.ok()
.filter(|o| o.status.success())
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|s| s.trim().to_string())
}