diff --git a/rust/build.rs b/rust/build.rs index 5dbb4a5..83459aa 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -1,4 +1,4 @@ -use std::process::Command; +use std::{fs, path::PathBuf, process::Command}; // https://stackoverflow.com/a/44407625 fn main() { @@ -7,6 +7,17 @@ fn main() { .output() .unwrap(); let git_hash = String::from_utf8(output.stdout).unwrap(); - println!("cargo:rustc-rerun-if-changed=.git/HEAD"); + match fs::read_to_string("../.git/HEAD") { + Ok(contents) => { + if let Some(branch_path) = contents.split(" ").skip(1).next() { + let path = PathBuf::from(".git").join(branch_path); + let path = path.to_str().unwrap_or(".git/HEAD"); + println!("cargo:rustc-rerun-if-changed={}", path); + } + } + Err(err) => { + println!("cargo::warning=Failed to read .git/HEAD: {}", err); + } + } println!("cargo:rustc-env=GIT_HASH={}", git_hash); }