//! Bake the git SHA of HEAD into the binary at compile time, surfaced
//! as `git_sha` in /health. Compile-time (not runtime `git -C` like
//! service-step2glb) because the deployed binary can outlive the repo
//! checkout it was built from — the watchdog resets the repo *then*
//! rebuilds, and a failed rebuild keeps the old binary running.

use std::process::Command;

fn main() {
    let sha = Command::new("git")
        .args(["rev-parse", "HEAD"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| "unknown".into());
    println!("cargo:rustc-env=GIT_SHA={sha}");

    // Recompile when HEAD moves even if no source file changed
    // (e.g. a docs- or cli-only commit) so the SHA can't go stale.
    println!("cargo:rerun-if-changed=../.git/HEAD");
    println!("cargo:rerun-if-changed=../.git/refs/heads/main");
}