From 2e3b4b774a9ff2eec46d04b48023cbfc2617ccea Mon Sep 17 00:00:00 2001 From: Junfeng Li Date: Thu, 30 Aug 2018 21:15:51 -0700 Subject: [PATCH] Get proper git SHA when TRAVIS_COMMIT is not available. --- build.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index 6c0de0728..9ac4a6a9a 100644 --- a/build.rs +++ b/build.rs @@ -1,9 +1,15 @@ -use std::fs::read_to_string; - fn main() { let git_hash = option_env!("TRAVIS_COMMIT") - .map(|s| s.into()) - .unwrap_or_else(|| read_to_string(".git/refs/heads/next").unwrap_or_default()); + .map(ToOwned::to_owned) + .unwrap_or_else(|| { + let stdout = std::process::Command::new("git") + .arg("rev-parse") + .arg("HEAD") + .output() + .expect("Failed to get git commit SHA") + .stdout; + String::from_utf8(stdout).expect("Failed to construct string") + }); println!("cargo:rustc-env=GIT_HASH={}", git_hash); }