From ee9987fb3b4c1262bac7e4fa73101e3ea25ab037 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Fri, 20 Nov 2026 10:41:50 +0100 Subject: [PATCH 1/2] fix: --version named the commit of an enclosing repository Sources unpacked inside another git checkout got that checkout's commit. The revision now comes from git only at the top of a work tree. --- build.rs | 19 ++--- src/revision.rs | 41 ++++++++++ tests/revision.rs | 189 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 236 insertions(+), 13 deletions(-) create mode 100644 src/revision.rs create mode 100644 tests/revision.rs diff --git a/build.rs b/build.rs index d1f4630..0aa7915 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,7 @@ -//! Records the build date so `--version` can report it. +//! Records the build date and the revision so `--version` can report them. -use std::process::Command; +#[path = "src/revision.rs"] +mod revision; fn main() { // SOURCE_DATE_EPOCH keeps reproducible builds reproducible; OpenWrt and @@ -19,20 +20,12 @@ fn main() { // The build date alone cannot identify a build: distro packaging derives // SOURCE_DATE_EPOCH from the package, so two builds of different commits - // share it. Packagers can pass the revision explicitly; a git checkout - // finds it itself. + // share it. Packagers can pass the revision explicitly, and the top of a + // git checkout finds it itself. let revision = std::env::var("LIBRESPEED_REVISION") .ok() .filter(|r| !r.is_empty()) - .or_else(|| { - Command::new("git") - .args(["rev-parse", "--short=12", "HEAD"]) - .output() - .ok() - .filter(|o| o.status.success()) - .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) - .filter(|r| !r.is_empty()) - }) + .or_else(revision::git_revision) .unwrap_or_else(|| "unknown".to_string()); println!("cargo:rustc-env=LIBRESPEED_BUILD_DATE={date}"); diff --git a/src/revision.rs b/src/revision.rs new file mode 100644 index 0000000..737c5e0 --- /dev/null +++ b/src/revision.rs @@ -0,0 +1,41 @@ +//! The git revision build.rs gives `--version`. build.rs and +//! tests/revision.rs include this file by path, it is not a module of the +//! library. + +use std::process::Command; + +/// The commit checked out in the current directory, or `None` unless that +/// directory is the top of a git work tree. +/// +/// Git cannot tell whether the work tree is this project's, so sources copied +/// to the top of another repository still get its commit. +pub fn git_revision() -> Option { + // Below the top, as for sources unpacked inside another checkout, or + // inside a git directory, the commit would be that repository's. + if git(&["rev-parse", "--is-inside-work-tree"])? != "true" + || !git(&["rev-parse", "--show-prefix"])?.is_empty() + { + return None; + } + git(&["rev-parse", "--short=12", "HEAD"]).filter(|r| !r.is_empty()) +} + +/// What git printed, without its last line break, or `None` when it failed. +/// +/// `GIT_DIR` and `GIT_WORK_TREE` are cleared, because they name a repository +/// wherever the build runs, and git exports `GIT_DIR` to its hooks. The rest +/// of the environment is respected, `GIT_CEILING_DIRECTORIES` included, which +/// OpenWrt sets so git stays inside its build directory. +fn git(args: &[&str]) -> Option { + let output = Command::new("git") + .args(args) + .env_remove("GIT_DIR") + .env_remove("GIT_WORK_TREE") + .output() + .ok()?; + output.status.success().then(|| { + String::from_utf8_lossy(&output.stdout) + .trim_end_matches(['\r', '\n']) + .to_string() + }) +} diff --git a/tests/revision.rs b/tests/revision.rs new file mode 100644 index 0000000..fce9c6e --- /dev/null +++ b/tests/revision.rs @@ -0,0 +1,189 @@ +//! The revision build.rs gives `--version`, checked by building a small crate +//! whose build script includes src/revision.rs as build.rs does, in temporary +//! git repositories. +//! +//! Not run on Windows, where it has not been tried. + +#![cfg(unix)] + +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; + +const REVISION_RS: &str = include_str!("../src/revision.rs"); + +const BUILD_RS: &str = r#"#[path = "revision.rs"] +mod revision; + +fn main() { + println!("cargo:rerun-if-env-changed=LIBRESPEED_REVISION"); + let revision = revision::git_revision().unwrap_or_else(|| "unknown".to_string()); + println!("cargo:rustc-env=REVISION={revision}"); +} +"#; + +const MAIN_RS: &str = r#"fn main() { + println!("{}", env!("REVISION")); +} +"#; + +// An include list like the crate's own, so Cargo finds the files without +// asking git, which refuses inside a bare repository. +const CARGO_TOML: &str = r#"[package] +name = "revision-fixture" +version = "0.0.0" +edition = "2021" +include = ["/build.rs", "/revision.rs", "/src"] + +[workspace] +"#; + +/// An empty directory of its own for each test. +fn scratch(name: &str) -> PathBuf { + let dir = Path::new(env!("CARGO_TARGET_TMPDIR")) + .join("revision") + .join(name); + if dir.exists() { + fs::remove_dir_all(&dir).expect("clear the scratch directory"); + } + fs::create_dir_all(&dir).expect("create the scratch directory"); + dir +} + +/// git in `dir`, without the user's configuration or the repository of a hook +/// the tests may run from. +fn git_command(dir: &Path) -> Command { + let mut command = Command::new("git"); + command + .current_dir(dir) + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_AUTHOR_NAME", "test") + .env("GIT_AUTHOR_EMAIL", "test@example.com") + .env("GIT_COMMITTER_NAME", "test") + .env("GIT_COMMITTER_EMAIL", "test@example.com") + .env_remove("GIT_DIR") + .env_remove("GIT_WORK_TREE") + .env_remove("GIT_INDEX_FILE"); + command +} + +/// Runs git in `dir` and returns its output. +fn git(dir: &Path, args: &[&str]) -> String { + let output = git_command(dir).args(args).output().expect("run git"); + assert!( + output.status.success(), + "git {args:?} in {}: {}", + dir.display(), + String::from_utf8_lossy(&output.stderr) + ); + String::from_utf8_lossy(&output.stdout).trim().to_string() +} + +fn commit(dir: &Path, message: &str) { + git(dir, &["commit", "-q", "--allow-empty", "-m", message]); +} + +fn head(dir: &Path) -> String { + git(dir, &["rev-parse", "--short=12", "HEAD"]) +} + +fn write_fixture(dir: &Path) { + fs::create_dir_all(dir.join("src")).expect("create the fixture"); + for (path, text) in [ + ("Cargo.toml", CARGO_TOML), + ("build.rs", BUILD_RS), + ("revision.rs", REVISION_RS), + ("src/main.rs", MAIN_RS), + ] { + fs::write(dir.join(path), text).expect("write the fixture"); + } +} + +/// A repository holding the fixture on the branch main, with one commit on top +/// of it. +fn repository(dir: &Path, init: &[&str]) { + fs::create_dir_all(dir).expect("create the repository"); + git(dir, init); + git(dir, &["symbolic-ref", "HEAD", "refs/heads/main"]); + write_fixture(dir); + git(dir, &["add", "-A"]); + commit(dir, "fixture"); + commit(dir, "base"); +} + +/// Builds and runs the fixture in `dir` and returns the revision it reports and +/// what its build script printed, nothing when the script did not run. +fn build(dir: &Path, target: &Path, env: &[(&str, &Path)]) -> (String, String) { + let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into()); + // -vv shows what the build script prints, and only when it runs, on + // stdout before the output of the fixture. + let output = Command::new(cargo) + .args(["run", "--offline", "-vv"]) + .current_dir(dir) + .env("CARGO_TARGET_DIR", target) + .env("CARGO_TERM_COLOR", "never") + .envs(env.iter().copied()) + .output() + .expect("run cargo"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + output.status.success(), + "cargo run in {}: {stderr}", + dir.display() + ); + let stdout = String::from_utf8_lossy(&output.stdout); + let (mut fixture, mut script) = (Vec::new(), String::new()); + for line in stdout.lines() { + match line.strip_prefix("[revision-fixture 0.0.0] ") { + Some(printed) => script += &format!("{printed}\n"), + None => fixture.push(line), + } + } + (fixture.join("\n"), script) +} + +#[test] +fn the_top_of_a_work_tree_reports_its_commit() { + let root = scratch("top"); + let repo = root.join("repo"); + repository(&repo, &["init", "-q"]); + assert_eq!(build(&repo, &root.join("target"), &[]).0, head(&repo)); + fs::remove_dir_all(root).expect("remove the scratch directory"); +} + +#[test] +fn no_revision_below_the_top_of_a_work_tree() { + let root = scratch("below"); + let outer = root.join("outer"); + repository(&outer, &["init", "-q"]); + + // Sources unpacked inside another checkout. + let sources = outer.join("sources"); + write_fixture(&sources); + assert_eq!( + build(&sources, &root.join("target-below"), &[]).0, + "unknown" + ); + + // The same, with the GIT_DIR git gives a hook of that checkout. + let git_dir = outer.join(".git"); + let env = [("GIT_DIR", git_dir.as_path())]; + assert_eq!( + build(&sources, &root.join("target-git-dir"), &env).0, + "unknown" + ); + + // Inside the git directory of a bare repository with a commit. + let bare = root.join("bare.git"); + git(&root, &["init", "-q", "--bare", "bare.git"]); + git(&bare, &["symbolic-ref", "HEAD", "refs/heads/main"]); + git( + &outer, + &["push", "-q", bare.to_str().unwrap(), "HEAD:refs/heads/main"], + ); + let inside = bare.join("sources"); + write_fixture(&inside); + assert_eq!(build(&inside, &root.join("target-bare"), &[]).0, "unknown"); + fs::remove_dir_all(root).expect("remove the scratch directory"); +} From 55a7ca017c4ef46a3e6d57954d8e7173b67b2af6 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Fri, 20 Nov 2026 10:41:51 +0100 Subject: [PATCH 2/2] fix: --version kept the revision of an earlier commit Cargo did not rerun the build script when HEAD moved. It now watches the files git rewrites for that, which a test checks with refs in files and in a reftable. --- src/revision.rs | 44 +++++++++++++ tests/revision.rs | 163 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) diff --git a/src/revision.rs b/src/revision.rs index 737c5e0..1d15b8a 100644 --- a/src/revision.rs +++ b/src/revision.rs @@ -2,6 +2,7 @@ //! tests/revision.rs include this file by path, it is not a module of the //! library. +use std::path::PathBuf; use std::process::Command; /// The commit checked out in the current directory, or `None` unless that @@ -17,9 +18,52 @@ pub fn git_revision() -> Option { { return None; } + watch_refs(); git(&["rev-parse", "--short=12", "HEAD"]).filter(|r| !r.is_empty()) } +/// Tells Cargo to run the build script again when the checked-out commit +/// changes. +/// +/// Cargo compares the mtime of each named path, of the newest file in it for +/// a directory, with the time the script last started. A commit, a checkout, +/// a reset or a branch update rewrites one of these: the HEAD file of this +/// worktree, a file in the shared `refs/heads`, which a commit after +/// `git pack-refs` adds back, or the `tables.list` of a reftable, the +/// worktree's own for its HEAD and the shared one for the branches. A path +/// that does not exist would make Cargo rerun the script on every build, so +/// only the existing ones are named. The price is a rebuild now and then while +/// HEAD stays: after a change to any branch, in another worktree too, after +/// `git pack-refs`, and in a reftable after any ref update, a fetch included. +fn watch_refs() { + let Some(answer) = git(&[ + "rev-parse", + "--path-format=absolute", + "--git-path", + "HEAD", + "--git-path", + "refs/heads", + "--git-path", + "reftable/tables.list", + "--git-common-dir", + ]) else { + return; + }; + let mut paths: Vec = answer.lines().map(PathBuf::from).collect(); + // The last answer is the common directory. Git before 2.31 does not know + // --path-format and prints it back as a line of its own, which does not + // exist and is skipped, and the paths after it are relative to this + // directory, the package root. + if let Some(common) = paths.pop() { + paths.push(common.join("reftable").join("tables.list")); + } + // In the main worktree both tables.list are the same file. + paths.dedup(); + for path in paths.iter().filter(|path| path.exists()) { + println!("cargo:rerun-if-changed={}", path.display()); + } +} + /// What git printed, without its last line break, or `None` when it failed. /// /// `GIT_DIR` and `GIT_WORK_TREE` are cleared, because they name a repository diff --git a/tests/revision.rs b/tests/revision.rs index fce9c6e..e996a8e 100644 --- a/tests/revision.rs +++ b/tests/revision.rs @@ -187,3 +187,166 @@ fn no_revision_below_the_top_of_a_work_tree() { assert_eq!(build(&inside, &root.join("target-bare"), &[]).0, "unknown"); fs::remove_dir_all(root).expect("remove the scratch directory"); } + +/// A checkout with the fixture and its own target directory. +struct Tree { + dir: PathBuf, + target: PathBuf, + reported: String, +} + +impl Tree { + fn new(dir: PathBuf, target: PathBuf) -> Self { + Tree { + dir, + target, + reported: String::new(), + } + } + + /// Builds after HEAD moved and checks the new commit is reported. + fn follows(&mut self, step: &str) { + let head = head(&self.dir); + assert_ne!(head, self.reported, "{step}: HEAD did not move"); + let (revision, _) = build(&self.dir, &self.target, &[]); + assert_eq!(revision, head, "{step}"); + self.reported = revision; + } + + /// Builds with nothing changed and checks the build script did not run. + fn stays(&self, step: &str) { + let (revision, script) = build(&self.dir, &self.target, &[]); + assert!(script.is_empty(), "{step}: the build script ran again"); + assert_eq!(revision, self.reported, "{step}"); + } + + /// Moves HEAD in each way git does, building after each move. + fn moves(&mut self, branch: &str) { + let dir = self.dir.clone(); + self.follows("first build"); + self.stays("nothing changed"); + commit(&dir, "next"); + self.follows("commit"); + git( + &dir, + &["commit", "-q", "--allow-empty", "--amend", "-m", "amended"], + ); + self.follows("amend"); + git( + &dir, + &["checkout", "-q", "-b", &format!("{branch}-side"), "HEAD~1"], + ); + self.follows("switch to another branch"); + git(&dir, &["checkout", "-q", "--detach", "HEAD~1"]); + self.follows("detached checkout"); + commit(&dir, "detached"); + self.follows("commit on a detached HEAD"); + git(&dir, &["checkout", "-q", branch]); + self.follows("switch back"); + git(&dir, &["reset", "-q", "--hard", "HEAD~1"]); + self.follows("reset"); + git(&dir, &["pack-refs", "--all"]); + commit(&dir, "after pack-refs"); + self.follows("commit after pack-refs"); + self.stays("nothing changed again"); + } +} + +/// HEAD moves in a repository and in a linked worktree of it, and the branch +/// of the worktree moves from the main checkout. +fn follows_head(name: &str, init: &[&str]) { + let root = scratch(name); + let main = root.join("main"); + repository(&main, init); + let mut tree = Tree::new(main.clone(), root.join("target-main")); + tree.moves("main"); + + git( + &main, + &["worktree", "add", "-q", "-b", "linked", "../linked"], + ); + let mut linked = Tree::new(root.join("linked"), root.join("target-linked")); + linked.moves("linked"); + git( + &main, + &["update-ref", "refs/heads/linked", "refs/heads/main"], + ); + linked.follows("branch moved from the main checkout"); + linked.stays("nothing changed after the move"); + fs::remove_dir_all(root).expect("remove the scratch directory"); +} + +#[test] +fn the_revision_follows_head_with_refs_in_files() { + follows_head("files", &["init", "-q"]); +} + +#[test] +fn the_revision_follows_head_with_refs_in_a_reftable() { + let probe = scratch("reftable-probe"); + let supported = git_command(&probe) + .args(["init", "-q", "--ref-format=reftable"]) + .output() + .is_ok_and(|output| output.status.success()); + fs::remove_dir_all(probe).expect("remove the scratch directory"); + if !supported { + eprintln!("skipped: this git cannot create a reftable repository"); + return; + } + follows_head("reftable", &["init", "-q", "--ref-format=reftable"]); +} + +/// Git before 2.31, which does not know `--path-format` and prints it back as a +/// line of its own, as builtin/rev-parse.c in v2.30.0 does. +const OLD_GIT: &str = r#"#!/bin/sh +if [ "$1" = rev-parse ]; then + for arg in "$@"; do + shift + case "$arg" in + --path-format=*) printf '%s\n' "$arg" ;; + *) set -- "$@" "$arg" ;; + esac + done +fi +exec "$REAL_GIT" "$@" +"#; + +#[test] +fn the_revision_follows_head_with_git_before_2_31() { + use std::os::unix::fs::PermissionsExt; + + let root = scratch("old-git"); + let repo = root.join("repo"); + repository(&repo, &["init", "-q"]); + let bin = root.join("bin"); + fs::create_dir(&bin).expect("create the bin directory"); + fs::write(bin.join("git"), OLD_GIT).expect("write the old git"); + fs::set_permissions(bin.join("git"), fs::Permissions::from_mode(0o755)) + .expect("make the old git executable"); + let real = Command::new("sh") + .args(["-c", "command -v git"]) + .output() + .expect("find git"); + let real = PathBuf::from(String::from_utf8_lossy(&real.stdout).trim()); + let path = std::env::join_paths( + std::iter::once(bin).chain(std::env::split_paths(&std::env::var_os("PATH").unwrap())), + ) + .expect("join PATH"); + let env = [("PATH", Path::new(&path)), ("REAL_GIT", real.as_path())]; + let target = root.join("target"); + + let (revision, script) = build(&repo, &target, &env); + assert_eq!(revision, head(&repo), "first build"); + // The paths the build script named are relative, so the stand-in was used. + assert!( + script.contains("cargo:rerun-if-changed=.git/HEAD\n"), + "{script}" + ); + assert!(!script.contains("--path-format"), "{script}"); + commit(&repo, "next"); + assert_eq!(build(&repo, &target, &env).0, head(&repo), "commit"); + let (revision, script) = build(&repo, &target, &env); + assert!(script.is_empty(), "the build script ran again"); + assert_eq!(revision, head(&repo), "nothing changed"); + fs::remove_dir_all(root).expect("remove the scratch directory"); +}