forked from MarcelRaschke/stacks-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
58 lines (52 loc) · 1.37 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::process::Command;
fn current_git_hash() -> Option<String> {
let commit = Command::new("git")
.arg("log")
.arg("-1")
.arg("--pretty=format:%h") // Abbreviated commit hash
.current_dir(env!("CARGO_MANIFEST_DIR"))
.output();
if let Ok(commit) = commit {
if let Ok(commit) = String::from_utf8(commit.stdout) {
return Some(commit);
}
}
None
}
fn current_git_branch() -> Option<String> {
let commit = Command::new("git")
.arg("rev-parse")
.arg("--abbrev-ref")
.arg("HEAD")
.output();
if let Ok(commit) = commit {
if let Ok(commit) = String::from_utf8(commit.stdout) {
return Some(commit);
}
}
None
}
fn is_working_tree_clean() -> bool {
let status = Command::new("git")
.arg("diff")
.arg("--quiet")
.arg("--exit-code")
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status();
if let Ok(status) = status {
status.code() == Some(0)
} else {
true
}
}
fn main() {
if let Some(git) = current_git_hash() {
println!("cargo:rustc-env=GIT_COMMIT={}", git);
}
if let Some(git) = current_git_branch() {
println!("cargo:rustc-env=GIT_BRANCH={}", git);
}
if !is_working_tree_clean() {
println!("cargo:rustc-env=GIT_TREE_CLEAN=+");
}
}