aboutsummaryrefslogtreecommitdiffstats
path: root/build.rs
blob: 78068b8621001024a3ea3f5b9ba0ccd515b37331 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::process::Command;

fn git(args: &[&str]) -> Option<String> {
    let output = Command::new("git")
        .args(args)
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    Some(String::from_utf8(output.stdout)
        .expect("git output not utf8")
        .trim()
        .to_string())
}

fn main() {
    // Re-run if HEAD changes
    println!("cargo:rerun-if-changed=.git/HEAD");
    println!("cargo:rerun-if-changed=.git/refs");

    if let Some(git_hash) = git(&["rev-parse", "HEAD"]) {
        println!("cargo:rustc-env=GIT_HASH={git_hash}");
    }

    if let Some(git_hash_short) = git(&["rev-parse", "--short", "HEAD"]) {
        println!("cargo:rustc-env=GIT_HASH_SHORT={git_hash_short}");
    }

    let git_dirty = Command::new("git")
        .args(["diff-index", "--quiet", "HEAD", "--"])
        .status()
        .map(|s| !s.success())
        .unwrap_or(false);

    if git_dirty {
        println!("cargo:rustc-env=GIT_DIRTY=1");
    }
}