facett-demo/build.rs
//! Build script — emit the git build-stamp so `facett-demo --version` and the
//! gallery header can print `facett-demo v<ver> (<git hash>)`, satisfying the
//! nordisk LAW that an operator must always be able to tell WHICH build is
//! running. This is facett-demo's ONLY build-time work; it reads git metadata as
//! **plain files only** — NO `git`, NO `std::process::Command`, NO shell (the
//! nordisk pure-Rust zero-shell law).
fn main() {
emit_git_hash();
}
/// Emit `cargo:rustc-env=FACETT_GIT_HASH=<short sha>` — the short git commit the
/// binary was built from — by reading the git metadata as **plain files only**: NO
/// `git`, NO `std::process::Command`, NO shell. Handles a normal `.git` dir, a
/// linked-worktree `.git` FILE (`gitdir:` → the worktree gitdir with its own HEAD
/// but a shared `commondir` for refs), a workspace subdir (walks up to the repo
/// root), loose refs, `packed-refs`, and detached HEAD. Falls back to `unknown` so
/// `env!("FACETT_GIT_HASH")` always resolves (e.g. a crates.io tarball build).
fn emit_git_hash() {
use std::path::{Path, PathBuf};
fn find_git_dir(start: &Path) -> Option<PathBuf> {
let mut cur = Some(start);
while let Some(d) = cur {
let g = d.join(".git");
if g.is_dir() {
return Some(g);
}
if g.is_file() {
// Linked worktree: `.git` is a file `gitdir: <path>`.
if let Ok(s) = std::fs::read_to_string(&g)
&& let Some(p) = s.lines().find_map(|l| l.strip_prefix("gitdir:"))
{
return Some(PathBuf::from(p.trim()));
}
}
cur = d.parent();
}
None
}
fn resolve(git_dir: &Path) -> Option<String> {
let head_path = git_dir.join("HEAD");
let head = std::fs::read_to_string(&head_path).ok()?;
println!("cargo:rerun-if-changed={}", head_path.display());
let head = head.trim();
// Refs live in the COMMON git dir (for a worktree, `commondir` points there;
// otherwise it is the git dir itself).
let common = std::fs::read_to_string(git_dir.join("commondir"))
.ok()
.map(|s| git_dir.join(s.trim()))
.and_then(|p| std::fs::canonicalize(&p).ok())
.unwrap_or_else(|| git_dir.to_path_buf());
if let Some(reference) = head.strip_prefix("ref:") {
let reference = reference.trim();
let loose = common.join(reference);
if let Ok(s) = std::fs::read_to_string(&loose) {
println!("cargo:rerun-if-changed={}", loose.display());
return Some(s.trim().to_string());
}
// packed-refs fallback (the loose ref file may not exist).
let packed_path = common.join("packed-refs");
println!("cargo:rerun-if-changed={}", packed_path.display());
let packed = std::fs::read_to_string(&packed_path).ok()?;
return packed.lines().find_map(|l| {
let l = l.trim();
if l.starts_with('#') || l.starts_with('^') {
return None;
}
let (sha, name) = l.split_once(' ')?;
(name == reference).then(|| sha.to_string())
});
}
// Detached HEAD: the file already holds the sha.
Some(head.to_string())
}
let manifest = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR set by cargo");
let hash = find_git_dir(Path::new(&manifest))
.and_then(|g| resolve(&g))
.map(|sha| sha.chars().take(12).collect::<String>())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".to_string());
println!("cargo:rustc-env=FACETT_GIT_HASH={hash}");
}