use std::process::Command;

fn main() {
    println!("cargo:rustc-check-cfg=cfg(coverage)");
    println!("cargo:rerun-if-changed=../.git/HEAD");
    println!("cargo:rerun-if-changed=../.git/index");
    println!(
        "cargo:rustc-env=LESAVKA_GIT_SHA={}",
        git_sha().unwrap_or_else(|| "nogit".to_string())
    );
}

fn git_sha() -> Option<String> {
    let output = Command::new("git")
        .args(["-C", "..", "rev-parse", "--short", "HEAD"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let sha = String::from_utf8(output.stdout).ok()?;
    let trimmed = sha.trim();
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}
