-
Notifications
You must be signed in to change notification settings - Fork 25
/
build.rs
53 lines (44 loc) · 1.16 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
use std::{
io::{self, Error, ErrorKind},
process::Command,
};
fn is_inside_git_work_tree() -> bool {
Command::new("git")
.arg("rev-parse")
.arg("--is-inside-work-tree")
.output()
.map(|output| String::from_utf8_lossy(&output.stdout).trim() == "true")
.unwrap_or(false)
}
fn error(message: String) -> io::Error {
Error::new(ErrorKind::Other, message)
}
fn commit_hash() -> io::Result<String> {
let output = Command::new("git").arg("rev-parse").arg("HEAD").output()?;
if !output.status.success() {
return Err(error(format!(
"Status error: `git rev-parse HEAD` failed: {}",
output.status
)));
}
let hash = String::from_utf8_lossy(&output.stdout)
.into_owned()
.trim()
.to_owned();
if !hash.chars().all(|c| "0123456789abcdef".contains(c)) {
return Err(error(format!(
"Invalid hash from `git rev-parse HEAD`: {}",
hash
)));
}
Ok(hash)
}
fn main() -> io::Result<()> {
if is_inside_git_work_tree() {
let hash = commit_hash()?;
println!("cargo:rustc-env=GIT_HEAD_PARTIAL_HASH= ({})", &hash[0..12]);
} else {
println!("cargo:rustc-env=GIT_HEAD_PARTIAL_HASH=");
};
Ok(())
}