-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathbuild.rs
More file actions
49 lines (41 loc) · 1.07 KB
/
build.rs
File metadata and controls
49 lines (41 loc) · 1.07 KB
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
use std::path::Path;
use std::process::Command;
struct CommitInfo {
hash: String,
short_hash: String,
date: String,
}
fn commit_info_from_git() -> Option<CommitInfo> {
if !Path::new(".git").exists() {
return None;
}
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.arg("--abbrev=9")
.output()
{
Ok(output) if output.status.success() => output,
_ => return None,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace().map(|s| s.to_string());
Some(CommitInfo {
hash: parts.next()?,
short_hash: parts.next()?,
date: parts.next()?,
})
}
fn commit_info() {
let Some(git) = commit_info_from_git() else {
return;
};
println!("cargo:rustc-env=GIT_HASH={}", git.hash);
println!("cargo:rustc-env=GIT_SHORT_HASH={}", git.short_hash);
println!("cargo:rustc-env=GIT_DATE={}", git.date);
}
fn main() {
commit_info();
}