-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
30 lines (26 loc) · 857 Bytes
/
build.rs
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
use std::process::Command;
fn main() {
let env = "TDATE_GIT_HASH";
let git_hash = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|x| String::from_utf8(x.stdout).ok())
.map(|hash| hash[..8].to_owned());
if let Some(hash) = git_hash {
let dirty = Command::new("git")
.args(["diff", "--stat"])
.output()
.ok()
.filter(|output| output.status.success())
.map(|output| !matches!(output.stdout.len(), 0));
match dirty {
Some(true) => println!("cargo:rustc-env={}={}-dirty", env, hash),
Some(false) => println!("cargo:rustc-env={}={}", env, hash),
_ => unreachable!("How can we have a git hash, yet not know if the tree is dirty?"),
}
} else {
println!("cargo:rustc-env={}=unknown", env);
}
}