@@ -1,4 +1,5 @@
1use std::borrow::Cow;
1use std::borrow::Cow;
2use std::process::Command;
3use std::process::Command;
4const VERSION: &str = include_str!("../VERSION");
5const VERSION: &str = include_str!("../VERSION");
@@ -11,7 +12,7 @@ fn main() {
11 .filter(|output| output.status.success())
12 .filter(|output| output.status.success())
12 .and_then(|x| String::from_utf8(x.stdout).ok());
13 .and_then(|x| String::from_utf8(x.stdout).ok());
14− let version: Cow<_> = match git_hash {
15+ let version: Cow<_> = match &git_hash {
15 Some(git_hash) => format!("{} ({})", VERSION, &git_hash[..8]).into(),
16 Some(git_hash) => format!("{} ({})", VERSION, &git_hash[..8]).into(),
16 None => VERSION.into(),
17 None => VERSION.into(),
@@ -23,4 +24,40 @@ fn main() {
24 println!("cargo:rerun-if-changed=../VERSION");
25 println!("cargo:rerun-if-changed=../VERSION");
25 println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version);
26 println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version);
28+ if git_hash.is_none() {
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"])
41+ .filter(|output| output.status.success())
42+ .and_then(|x| String::from_utf8(x.stdout).ok())
44+ // If heads starts pointing at something else (different branch)
46+ let head = Path::new(&git_dir).join("HEAD");
48+ println!("cargo:rerun-if-changed={}", head.display());
50+ // if the thing head points to (branch) itself changes
52+ let Some(head_ref): Option<String> = Command::new("git")
53+ .args(["symbolic-ref", "HEAD"])
56+ .filter(|output| output.status.success())
57+ .and_then(|x| String::from_utf8(x.stdout).ok())
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());