4 use std::path::{Path, PathBuf};
5 use std::process::Command;
8 // Only check .git/HEAD dirty status if it exists - doing so when
9 // building dependent crates may lead to false positives and rebuilds
10 if Path::new(".git/HEAD").exists() {
11 println!("cargo:rerun-if-changed=.git/HEAD");
14 println!("cargo:rerun-if-env-changed=CFG_RELEASE_CHANNEL");
16 let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
18 File::create(out_dir.join("commit-info.txt"))
20 .write_all(commit_info().as_bytes())
24 // Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
25 // (git not installed or if this is not a git repository) just return an empty string.
26 fn commit_info() -> String {
27 match (channel(), commit_hash(), commit_date()) {
28 (channel, Some(hash), Some(date)) => format!("{} ({} {})", channel, hash.trim_end(), date),
33 fn channel() -> String {
34 if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") {
41 fn commit_hash() -> Option<String> {
43 .args(&["rev-parse", "--short", "HEAD"])
46 .and_then(|r| String::from_utf8(r.stdout).ok())
49 fn commit_date() -> Option<String> {
51 .args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
54 .and_then(|r| String::from_utf8(r.stdout).ok())