-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
31 lines (27 loc) · 986 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
31
use regex::Regex;
use std::fs;
use std::process::Command;
fn main() {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).map_or("undetermined".into(), |o| o);
let output = Command::new("git")
.args(["rev-list", "--count", "HEAD"])
.output()
.unwrap();
let git_count = String::from_utf8(output.stdout).map_or("undetermined".into(), |o| o);
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
println!("cargo:rustc-env=GIT_COUNT={}", git_count);
println!("cargo:rustc-rerun-if-changed=.git/HEAD");
if let Ok(head) = fs::read_to_string(".git/HEAD") {
let re = Regex::new(r"ref: (.*)").unwrap();
if let Some(captures) = re.captures(&head) {
println!(
"cargo:rustc-rerun-if-changed=.git/{}",
captures.get(1).map_or("", |m| m.as_str())
);
}
}
}