guardy 0.2.4

Fast, secure git hooks in Rust with secret scanning and protected file synchronization
Documentation
use std::process::Command;

fn main() {
    // Get git commit SHA
    let output = Command::new("git")
        .args(["rev-parse", "--short", "HEAD"])
        .output()
        .unwrap_or_else(|_| {
            // If git command fails, use a default value
            std::process::Command::new("echo")
                .arg("unknown")
                .output()
                .unwrap()
        });

    let git_sha = String::from_utf8_lossy(&output.stdout).trim().to_string();

    // Pass the git SHA to the compiler
    println!("cargo:rustc-env=GIT_SHA={git_sha}");

    // Get git branch
    let branch_output = Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .output()
        .unwrap_or_else(|_| {
            std::process::Command::new("echo")
                .arg("unknown")
                .output()
                .unwrap()
        });

    let git_branch = String::from_utf8_lossy(&branch_output.stdout)
        .trim()
        .to_string();

    println!("cargo:rustc-env=GIT_BRANCH={git_branch}");

    // Get target triple
    let target = std::env::var("TARGET").unwrap_or_else(|_| "unknown".to_string());
    println!("cargo:rustc-env=TARGET={target}");

    // Get build profile
    let profile = std::env::var("PROFILE").unwrap_or_else(|_| "unknown".to_string());
    println!("cargo:rustc-env=PROFILE={profile}");

    // Get rustc version
    let rustc_output = Command::new("rustc")
        .args(["--version"])
        .output()
        .unwrap_or_else(|_| {
            std::process::Command::new("echo")
                .arg("unknown")
                .output()
                .unwrap()
        });
    let rustc_version = String::from_utf8_lossy(&rustc_output.stdout)
        .trim()
        .to_string();
    println!("cargo:rustc-env=RUSTC_VERSION={rustc_version}");

    // Get build timestamp
    let timestamp = chrono::Utc::now()
        .format("%Y-%m-%d %H:%M:%S UTC")
        .to_string();
    println!("cargo:rustc-env=BUILD_TIMESTAMP={timestamp}");

    // Rerun build if git HEAD changes
    println!("cargo:rerun-if-changed=.git/HEAD");
    println!("cargo:rerun-if-changed=.git/index");
}