-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.rs
31 lines (27 loc) · 911 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 git2::{Error, Repository};
fn main() -> Result<(), Error> {
let Ok(repo) = Repository::discover(".") else {
println!("cargo:rustc-env=GIT_SHA=UNKNOWN");
println!("cargo:rustc-env=GIT_SHA_SHORT=UNKNOWN");
return Ok(());
};
let repo_path = repo.path();
let head = repo.find_reference("HEAD")?;
let commit = head.peel_to_commit()?;
let short_id = repo.revparse_single("HEAD")?.short_id()?;
println!("cargo:rustc-env=GIT_SHA={}", commit.id());
println!(
"cargo:rustc-env=GIT_SHA_SHORT={}",
short_id.as_str().unwrap_or_default()
);
println!(
"cargo:rerun-if-changed={}",
repo_path.join("HEAD").display()
);
if let Ok(resolved) = head.resolve() {
if let Some(name) = resolved.name() {
println!("cargo:rerun-if-changed={}", repo_path.join(name).display());
}
}
Ok(())
}