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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Build script: expose git commit metadata as compile-time environment
//! variables so `build_info::{GIT_HASH, COMMIT_DATE}` are always populated.
//!
//! Why: Run artifacts must be attributable to the exact binary that produced
//! them. `CARGO_PKG_VERSION` alone collapses every commit on a dev branch into
//! the same version string — during the 2026-07-16 L4 validation that gap
//! forced provenance to be reverse-inferred from `cargo install`'s mtime reset
//! and produced a WRONG conclusion (#2823). Pairing the version with the short
//! git SHA and the commit date gives a deterministic identifier for
//! correlation.
//! What: Queries `git rev-parse --short HEAD` and `git log -1 --date=short
//! --format=%cd` at build time, exposing them as `GIT_COMMIT_HASH` and
//! `GIT_COMMIT_DATE`. Both fall back to `"unknown"` when git is unavailable or
//! the source is not a git checkout (e.g. a crates.io tarball), so the build
//! never breaks. The commit *date* is used rather than a wall-clock build time
//! so the values stay deterministic and reproducible across rebuilds.
//! Test: `build_info::tests::{provenance_constants_are_well_formed,
//! version_string_contains_version}` assert both render; after `cargo build`,
//! `tcode --version` prints either real values or the literal `"unknown"`.
use Path;
use Command;
/// Run a `git` invocation and return its trimmed stdout, or `None` when git is
/// missing, errors, or the directory is not a checkout.
///
/// Why: Every provenance probe needs the same "never fail the build" semantics;
/// centralising the fallible path keeps `main` declarative and guarantees a
/// non-checkout source tree degrades to `"unknown"` instead of breaking.
/// What: Spawns `git <args>`, requires a success status, and rejects empty
/// output so a blank result never masquerades as a real value.
/// Test: Exercised on every build; the `None` path is what a crates.io tarball
/// build takes.
/// Emit `cargo:rerun-if-changed` for `path` only when it actually exists.
///
/// Why: Cargo treats a `rerun-if-changed` path that does NOT exist as
/// perpetually changed, so declaring a missing path re-runs this script on
/// every single `cargo build`. That is not hypothetical: the original directive
/// here was a RELATIVE `.git/HEAD`, which Cargo resolves against the *package*
/// root (`crates/trusty-code/.git/HEAD` — absent even in a normal checkout), so
/// this script never cached in any tree. Guarding on existence keeps a build
/// with nothing to watch (the crates.io tarball) on Cargo's default "re-run
/// when sources change" behaviour instead of thrashing.
/// What: Canonicalizes `path` and prints the directive only if that succeeds
/// (which itself proves existence); otherwise does nothing. Canonicalizing
/// matters because `git rev-parse --git-path` returns a path relative to the
/// CWD in a plain checkout but an absolute one in a linked worktree — emitting
/// an absolute path either way removes any doubt about what Cargo resolves the
/// directive against.
/// Test: `build_info::tests::packed_branch_ref_leaves_only_the_reflog_witnessing_a_commit`
/// pins the packed-ref premise; also observable as a second consecutive
/// `cargo build` being a no-op.