-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
32 lines (24 loc) · 964 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
32
use chrono::{DateTime, TimeZone, Utc};
use git2::Repository;
use std::error::Error;
fn main() {
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs");
let (git_hash, commit_date) =
get_git_info().unwrap_or_else(|_| (String::from("unknown"), String::from("unknown")));
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
println!("cargo:rustc-env=COMMIT_DATE={}", commit_date);
}
fn get_git_info() -> Result<(String, String), Box<dyn Error>> {
let repo = Repository::open_from_env().or_else(|_| Repository::discover("."))?;
let head = repo.head()?;
let commit = head.peel_to_commit()?;
let hash = commit.id().to_string();
let timestamp = commit.time();
let datetime: DateTime<Utc> = Utc
.timestamp_opt(timestamp.seconds(), 0)
.single()
.ok_or("Invalid timestamp")?;
let date = datetime.format("%Y-%m-%d").to_string();
Ok((hash, date))
}