@@ -1,4 +1,5 @@
11use std::borrow::Cow;
2+use std::path::Path;
23use std::process::Command;
34
45const VERSION: &str = include_str!("../VERSION");
@@ -11,7 +12,7 @@ fn main() {
1112 .filter(|output| output.status.success())
1213 .and_then(|x| String::from_utf8(x.stdout).ok());
1314
14− let version: Cow<_> = match git_hash {
15+ let version: Cow<_> = match &git_hash {
1516 Some(git_hash) => format!("{} ({})", VERSION, &git_hash[..8]).into(),
1617 None => VERSION.into(),
1718 };
@@ -23,4 +24,40 @@ fn main() {
2324
2425 println!("cargo:rerun-if-changed=../VERSION");
2526 println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version);
27+
28+ if git_hash.is_none() {
29+ return;
30+ }
31+
32+ // we need to revparse because the git dir could be anywhere if you are
33+ // using detached worktrees but there is no good way to obtain an OsString
34+ // from command output so for now we can't accept non-utf8 paths here
35+ // probably rare enouch where it doesn't matter tough we could use gitoxide
36+ // here but that would be make it a hard dependency and slow compile times
37+ let Some(git_dir): Option<String> = Command::new("git")
38+ .args(["rev-parse", "--git-dir"])
39+ .output()
40+ .ok()
41+ .filter(|output| output.status.success())
42+ .and_then(|x| String::from_utf8(x.stdout).ok())
43+ else{ return; };
44+ // If heads starts pointing at something else (different branch)
45+ // we need to return
46+ let head = Path::new(&git_dir).join("HEAD");
47+ if head.exists() {
48+ println!("cargo:rerun-if-changed={}", head.display());
49+ }
50+ // if the thing head points to (branch) itself changes
51+ // we need to return
52+ let Some(head_ref): Option<String> = Command::new("git")
53+ .args(["symbolic-ref", "HEAD"])
54+ .output()
55+ .ok()
56+ .filter(|output| output.status.success())
57+ .and_then(|x| String::from_utf8(x.stdout).ok())
58+ else{ return; };
59+ let head_ref = Path::new(&git_dir).join(head_ref);
60+ if head_ref.exists() {
61+ println!("cargo:rerun-if-changed={}", head_ref.display());
62+ }
2663}