Skip to content

Commit 14b7f84

Browse files
authored
[rust_verify] fix: rerun-if-changed paths in build.rs (#2826)
1 parent 5d26023 commit 14b7f84

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

source/cargo-verus-toolchains/src/versions.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::process::Command;
1+
use std::{path::PathBuf, process::Command};
22

33
use anyhow::{Context, Result, bail};
44

@@ -69,6 +69,33 @@ fn get_git_rev(abbreviate_to: Option<usize>) -> Result<String> {
6969
Ok(raw_rev.trim().to_owned())
7070
}
7171

72+
/// Get the existing Git files that determine the current `HEAD` commit.
73+
///
74+
/// This works for ordinary repositories, linked worktrees, both symbolic and detached `HEAD`s,
75+
/// and packed refs.
76+
pub fn get_git_head_paths() -> Result<Vec<PathBuf>> {
77+
let mut paths = vec![get_git_path("HEAD")?.context("Git HEAD is missing")?];
78+
79+
if let Ok(head_ref) = run_command(&["git", "symbolic-ref", "--quiet", "HEAD"])
80+
&& let Some(path) = get_git_path(head_ref.trim())?
81+
{
82+
paths.push(path);
83+
}
84+
85+
if let Some(path) = get_git_path("packed-refs")? {
86+
paths.push(path);
87+
}
88+
89+
Ok(paths)
90+
}
91+
92+
/// Get the absolute path to a Git file when it exists.
93+
fn get_git_path(path: &str) -> Result<Option<PathBuf>> {
94+
let path = run_command(&["git", "rev-parse", "--path-format=absolute", "--git-path", path])?;
95+
let path = PathBuf::from(path.trim());
96+
Ok(path.exists().then_some(path))
97+
}
98+
7299
fn run_command(program_and_args: &[&str]) -> Result<String> {
73100
let mut command = Command::new(program_and_args[0]);
74101
command.args(&program_and_args[1..]);

source/rust_verify/build.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::process::Command;
22

3-
use cargo_verus_toolchains::versions::get_verus_version;
3+
use cargo_verus_toolchains::versions::{get_git_head_paths, get_verus_version};
44

55
fn main() {
66
let (default_version, default_sha) = get_verus_version(true).expect("version info");
@@ -12,7 +12,9 @@ fn main() {
1212
run_command(&["rustup", "show", "active-toolchain"]).expect("active toolchain")
1313
});
1414

15-
println!("cargo::rerun-if-changed=../.git/HEAD");
15+
for path in get_git_head_paths().expect("Git HEAD paths") {
16+
println!("cargo::rerun-if-changed={}", path.display());
17+
}
1618
println!("cargo::rerun-if-env-changed=VARGO_BUILD_PROFILE");
1719
println!("cargo::rerun-if-env-changed=VARGO_BUILD_VERSION");
1820
println!("cargo::rerun-if-env-changed=VARGO_BUILD_SHA");

0 commit comments

Comments
 (0)