-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
38 lines (32 loc) · 927 Bytes
/
Copy pathbuild.rs
File metadata and controls
38 lines (32 loc) · 927 Bytes
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
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
let git_hash = get_git_hash();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
}
fn get_git_hash() -> String {
let hash = match Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
{
Ok(output) if output.status.success() => String::from_utf8(output.stdout)
.ok()
.map(|s| s.trim().to_string()),
_ => None,
};
let Some(hash) = hash else {
return "unknown".to_string();
};
let is_dirty = Command::new("git")
.args(["status", "--porcelain", "--untracked-files=no"])
.output()
.ok()
.map(|output| !output.stdout.is_empty())
.unwrap_or(false);
if is_dirty {
format!("{}-dirty", hash)
} else {
hash
}
}