-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
34 lines (32 loc) · 1.55 KB
/
Copy pathbuild.rs
File metadata and controls
34 lines (32 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//! Bake a LocalStamp for `st2 --version` on a plain `cargo build`, per the shared
//! build-versioning contract. Emitted as `ST2_BUILD_STAMP_LOCAL` — a private var
//! distinct from the fleet's `CLI_BUILD_STAMP`, so the flake's authoritative
//! NixStamp can never be overridden by this (see src/version.rs). A hermetic Nix
//! build has no `.git`, so this yields nothing there and the NixStamp is used.
//!
use std::process::Command;
fn git(args: &[&str]) -> Option<String> {
let out = Command::new("git").args(args).output().ok()?;
if !out.status.success() {
return None;
}
let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
(!s.is_empty()).then_some(s)
}
fn main() {
if let Some(rev) = git(&["rev-parse", "--short", "HEAD"]) {
let dirty = git(&["status", "--porcelain"]).is_some_and(|s| !s.is_empty());
let commit_ts = git(&["log", "-1", "--format=%ct"])
.and_then(|s| s.parse::<i64>().ok())
.unwrap_or(0);
// Hand-assembled JSON: the short-sha is hex so no escaping is needed, and
// this avoids a build-dependency just to serialize three fields.
let stamp =
format!(r#"{{"type":"local","rev":"{rev}","commitTs":{commit_ts},"dirty":{dirty}}}"#);
println!("cargo:rustc-env=ST2_BUILD_STAMP_LOCAL={stamp}");
}
// Rebuild the stamp when HEAD moves or the working tree changes (dirty flag).
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs");
println!("cargo:rerun-if-changed=.git/index");
}