forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

build.rs97 lines, 3.4 KiBHistoryRawDownload
1//! The build identity of this binary: version, git commit, commit date. Shared by the server and
2//! the runner, which are the same binary, so a runner can be compared to the server it talks to.
3
4use std::sync::OnceLock;
5
6/// The crate version.
7pub const VERSION: &str = env!("CARGO_PKG_VERSION");
8
9struct Identity {
10 commit: &'static str,
11 date: &'static str,
12 dirty: bool,
13}
14
15static IDENTITY: OnceLock<Identity> = OnceLock::new();
16
17/// Sets the commit, commit date and dirty flag of the running binary. Each binary sets this first
18/// thing in main, so this crate has no per-commit input and a new commit does not rebuild
19/// everything that depends on it. A second call is ignored.
20pub fn set_identity(commit: &'static str, date: &'static str, dirty: bool) {
21 let _ = IDENTITY.set(Identity {
22 commit,
23 date,
24 dirty,
25 });
26}
27
28/// Short git sha of the commit built, or "unknown" before [`set_identity`].
29pub fn commit() -> &'static str {
30 IDENTITY.get().map_or("unknown", |i| i.commit)
31}
32
33/// Commit date (YYYY-MM-DD), or "unknown" before [`set_identity`].
34pub fn date() -> &'static str {
35 IDENTITY.get().map_or("unknown", |i| i.date)
36}
37
38/// True when the tree had uncommitted changes to tracked files at build time.
39pub fn dirty() -> bool {
40 IDENTITY.get().is_some_and(|i| i.dirty)
41}
42
43/// `0.1.0 (cf8e648, 2026-10-20)`, with `cf8e648-dirty` for a dirty tree.
44pub fn display() -> String {
45 format_build(VERSION, commit(), date(), dirty())
46}
47
48pub fn format_build(version: &str, commit: &str, date: &str, dirty: bool) -> String {
49 let dirty = if dirty { "-dirty" } else { "" };
50 format!("{version} ({commit}{dirty}, {date})")
51}
52
53/// The commit sha out of a build string made by [`display`], `None` when it is unknown or the
54/// string is not in that shape.
55pub fn commit_of(build: &str) -> Option<&str> {
56 let inner = build.split_once('(')?.1;
57 let commit = inner.split([',', ')']).next()?.trim();
58 let commit = commit.strip_suffix("-dirty").unwrap_or(commit);
59 (!commit.is_empty() && commit != "unknown").then_some(commit)
60}
61
62/// How a runner's build compares with the server's.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum Freshness {
65 UpToDate,
66 Outdated,
67 /// The runner reported nothing usable (an old runner), or this server's own commit is unknown.
68 Unknown,
69}
70
71/// Compares by commit, ignoring the dirty marker.
72pub fn compare(runner: Option<&str>, server: &str) -> Freshness {
73 match (runner.and_then(commit_of), commit_of(server)) {
74 (Some(r), Some(s)) if r == s => Freshness::UpToDate,
75 (Some(_), Some(_)) => Freshness::Outdated,
76 _ => Freshness::Unknown,
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn up_to_date_compares_the_commit_only() {
86 let server = format_build("0.1.0", "cf8e648", "2026-10-20", false);
87 assert_eq!(server, "0.1.0 (cf8e648, 2026-10-20)");
88 let same = format_build("0.1.0", "cf8e648", "2026-10-20", true);
89 assert_eq!(compare(Some(&same), &server), Freshness::UpToDate);
90 let older = format_build("0.1.0", "4d21658", "2026-10-19", false);
91 assert_eq!(compare(Some(&older), &server), Freshness::Outdated);
92 assert_eq!(compare(None, &server), Freshness::Unknown);
93 assert_eq!(compare(Some("0.1.0"), &server), Freshness::Unknown);
94 let unknown = format_build("0.1.0", "unknown", "unknown", false);
95 assert_eq!(compare(Some(&same), &unknown), Freshness::Unknown);
96 }
97}