The commit that runs: in the footer and at /version
Every page's footer names the commit the binary is built from, with a link to it on the forge, and /version answers with the full hash, open without a login like /healthz, so a script can tell which release is live. build.rs writes COMMIT: from VISA_COMMIT, which the flake sets to its source's revision (a Nix build has no .git), else from git in a checkout, else empty. The flake sets it on the package alone, not on the dependencies' build, which so stays cached from commit to commit.
This commit is contained in:
1 parent
a2cedcdd7e
commit
17426dba12
9 files changed
+140
-4
No files matched your search
@@ -1,7 +1,8 @@
|
||||
//! Lists every file of templates/ and static/ for `src/embedded.rs`: one
|
||||
//! `(name, include_bytes!(..))` pair per file, sorted by name, so the binary
|
||||
//! carries its pages and needs no files next to it. Also a short hash of the
|
||||
//! static files for cache busting (`/static/app.css?v=<hash>`).
|
||||
//! static files for cache busting (`/static/app.css?v=<hash>`), and the
|
||||
//! commit the binary is built from (`COMMIT`).
|
||||
|
||||
use std::fmt::Write as _;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -39,10 +40,50 @@ fn main() {
|
||||
hash >> 16
|
||||
)
|
||||
.unwrap();
|
||||
// The commit, for the footer and /version. A Nix build has no .git, so
|
||||
// the flake passes its source's revision in VISA_COMMIT; a build in a
|
||||
// checkout asks git; otherwise it stays empty.
|
||||
println!("cargo:rerun-if-env-changed=VISA_COMMIT");
|
||||
let commit = std::env::var("VISA_COMMIT")
|
||||
.ok()
|
||||
.filter(|c| !c.is_empty())
|
||||
.or_else(|| git_head(&root))
|
||||
.unwrap_or_default();
|
||||
writeln!(out, "pub const COMMIT: &str = {commit:?};").unwrap();
|
||||
let dest = Path::new(&std::env::var_os("OUT_DIR").unwrap()).join("embedded.rs");
|
||||
std::fs::write(dest, out).unwrap();
|
||||
}
|
||||
|
||||
/// HEAD's commit in a git checkout. The script runs again when HEAD or the
|
||||
/// branch it names moves.
|
||||
fn git_head(root: &Path) -> Option<String> {
|
||||
let git = |args: &[&str]| {
|
||||
std::process::Command::new("git")
|
||||
.args(args)
|
||||
.current_dir(root)
|
||||
.output()
|
||||
.ok()
|
||||
.filter(|o| o.status.success())
|
||||
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
|
||||
};
|
||||
let head = git(&["rev-parse", "HEAD"])?;
|
||||
let dir = PathBuf::from(git(&["rev-parse", "--absolute-git-dir"])?);
|
||||
let common = PathBuf::from(git(&[
|
||||
"rev-parse",
|
||||
"--path-format=absolute",
|
||||
"--git-common-dir",
|
||||
])?);
|
||||
let mut watch = vec![dir.join("HEAD"), common.join("packed-refs")];
|
||||
if let Some(branch) = git(&["symbolic-ref", "-q", "HEAD"]) {
|
||||
watch.push(common.join(branch));
|
||||
}
|
||||
// a path that does not exist would make cargo run the script every time
|
||||
for p in watch.iter().filter(|p| p.exists()) {
|
||||
println!("cargo:rerun-if-changed={}", p.display());
|
||||
}
|
||||
Some(head)
|
||||
}
|
||||
|
||||
fn walk(base: &Path, dir: &Path, out: &mut Vec<(String, String)>) {
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user