Skip to content

Commit b428aba

Browse files
authored
fix(dev): resolve build.rs HEAD path in worktrees (#25120)
* fix(dev): resolve build.rs HEAD path in worktrees * fix(dev): preserve whitespace in git HEAD path
1 parent 2fe515c commit b428aba

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

build.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,30 @@ fn git_short_hash() -> std::io::Result<String> {
104104
})
105105
}
106106

107+
#[cfg(not(feature = "nightly"))]
108+
fn git_path(path: &str) -> std::io::Result<String> {
109+
let output_result = Command::new("git")
110+
.args(["rev-parse", "--git-path", path])
111+
.output();
112+
113+
output_result.map(|output| {
114+
String::from_utf8(output.stdout)
115+
.expect("valid UTF-8")
116+
.trim_end_matches(['\r', '\n'])
117+
.to_owned()
118+
})
119+
}
120+
107121
fn main() {
108122
// Always rerun if the build script itself changes.
109123
println!("cargo:rerun-if-changed=build.rs");
110124

111125
// re-run if the HEAD has changed. This is only necessary for non-release and nightly builds.
112126
#[cfg(not(feature = "nightly"))]
113-
println!("cargo:rerun-if-changed=.git/HEAD");
127+
println!(
128+
"cargo:rerun-if-changed={}",
129+
git_path("HEAD").expect("git HEAD path detection failed")
130+
);
114131

115132
#[cfg(feature = "protobuf-build")]
116133
{

0 commit comments

Comments
 (0)