Stamp the version as YYYY.MM.DD-<branch>-<sha>
build.rs builds the string (git, or SLINUXM_* env for builds without .git) and the lib exposes it as VERSION. --version, /healthz, /api/v1/ping, MCP server info and OTel service.version use it; OpenAPI info.version stays on the semver so docs/openapi.json does not change per commit. The flake passes the commit date and short rev to the package.
This commit is contained in:
1 parent
435a54a02d
commit
1a30b20ac0
18 files changed
+322
-18
No files matched your search
@@ -0,0 +1,106 @@
|
||||
//! Stamps the binary with `YYYY.MM.DD-<branch>-<sha>` as `SLINUXM_VERSION`.
|
||||
//!
|
||||
//! Cargo.toml keeps a semver `version` (CalVer with zero-padded parts is not
|
||||
//! valid semver). Every part can be supplied through the environment, which
|
||||
//! is how a build without a `.git` directory (Nix, tarballs) gets them.
|
||||
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[path = "src/version.rs"]
|
||||
mod version;
|
||||
|
||||
const ENV_VARS: [&str; 4] = [
|
||||
"SLINUXM_VERSION",
|
||||
"SLINUXM_GIT_SHA",
|
||||
"SLINUXM_GIT_BRANCH",
|
||||
"SLINUXM_BUILD_DATE",
|
||||
];
|
||||
|
||||
fn env(name: &str) -> Option<String> {
|
||||
std::env::var(name).ok().filter(|v| !v.trim().is_empty())
|
||||
}
|
||||
|
||||
fn git(args: &[&str]) -> Option<String> {
|
||||
let out = Command::new("git").args(args).output().ok()?;
|
||||
if !out.status.success() {
|
||||
return None;
|
||||
}
|
||||
let text = String::from_utf8(out.stdout).ok()?;
|
||||
let text = text.trim();
|
||||
(!text.is_empty()).then(|| text.to_owned())
|
||||
}
|
||||
|
||||
/// Registers the files that change HEAD. `--git-path` resolves worktrees,
|
||||
/// where `.git` is a file and the refs live in the main repository.
|
||||
fn watch_git(branch: Option<&str>) {
|
||||
let mut paths = vec!["HEAD".to_owned(), "packed-refs".to_owned()];
|
||||
if let Some(b) = branch.filter(|b| *b != "HEAD") {
|
||||
paths.push(format!("refs/heads/{b}"));
|
||||
}
|
||||
for p in paths {
|
||||
if let Some(resolved) = git(&["rev-parse", "--git-path", &p]) {
|
||||
println!("cargo:rerun-if-changed={resolved}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wall-clock UTC unless `SLINUXM_BUILD_DATE` is set. `SOURCE_DATE_EPOCH` is
|
||||
/// deliberately ignored: Nix dev shells export it as a fixed 1980 timestamp.
|
||||
fn build_date() -> String {
|
||||
if let Some(d) = env("SLINUXM_BUILD_DATE") {
|
||||
return d;
|
||||
}
|
||||
let secs = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs());
|
||||
version::date_from_epoch(secs)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for name in ENV_VARS {
|
||||
println!("cargo:rerun-if-env-changed={name}");
|
||||
}
|
||||
// Naming any rerun path switches off cargo's default of watching the whole
|
||||
// package, so the sources are listed too: the dirty flag must be rechecked
|
||||
// whenever they change.
|
||||
for p in ["build.rs", "Cargo.toml", "Cargo.lock", "src"] {
|
||||
println!("cargo:rerun-if-changed={p}");
|
||||
}
|
||||
|
||||
let value = match env("SLINUXM_VERSION") {
|
||||
Some(v) => {
|
||||
assert!(
|
||||
version::is_valid(&v),
|
||||
"SLINUXM_VERSION {v:?} is not YYYY.MM.DD-<branch>-<sha>"
|
||||
);
|
||||
v
|
||||
}
|
||||
None => {
|
||||
let in_git = Path::new(".git").exists() || git(&["rev-parse", "--git-dir"]).is_some();
|
||||
let git_branch = in_git
|
||||
.then(|| git(&["rev-parse", "--abbrev-ref", "HEAD"]))
|
||||
.flatten();
|
||||
if in_git {
|
||||
watch_git(git_branch.as_deref());
|
||||
}
|
||||
|
||||
let branch = env("SLINUXM_GIT_BRANCH")
|
||||
.or(git_branch)
|
||||
.map_or_else(|| "unknown".to_owned(), |b| version::sanitize_branch(&b));
|
||||
|
||||
let sha = env("SLINUXM_GIT_SHA").or_else(|| {
|
||||
let sha = git(&["rev-parse", "--short=7", "HEAD"])?;
|
||||
let dirty = git(&["status", "--porcelain", "--untracked-files=no"]).is_some();
|
||||
Some(if dirty { format!("{sha}-dirty") } else { sha })
|
||||
});
|
||||
let sha = sha.map_or_else(|| "unknown".to_owned(), |s| version::sanitize_sha(&s));
|
||||
|
||||
let v = version::compose(&build_date(), &branch, &sha);
|
||||
assert!(version::is_valid(&v), "built an invalid version: {v:?}");
|
||||
v
|
||||
}
|
||||
};
|
||||
println!("cargo:rustc-env=SLINUXM_VERSION={value}");
|
||||
}
|
||||
Reference in new issue
Block a user