From 6194dfc247863a9bc1f5e0c2efd8f62fd66e9892 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 5 Feb 2019 11:49:38 -0800 Subject: [PATCH] Don't rely on PWD to find .git folder in build script --- cli/build.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cli/build.rs b/cli/build.rs index b24eef82..fc4702ae 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::{env, fs, io}; fn main() { @@ -11,7 +12,16 @@ fn main() { } fn read_git_sha() -> io::Result { - let git_path = env::current_dir().unwrap().parent().unwrap().join(".git"); + let mut repo_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let mut git_path; + loop { + git_path = repo_path.join(".git"); + if git_path.exists() { + break; + } else { + assert!(repo_path.pop()); + } + } let git_head_path = git_path.join("HEAD"); println!("cargo:rerun-if-changed={}", git_head_path.to_str().unwrap()); let mut head_content = fs::read_to_string(&git_head_path)?;