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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! Stamps the binary with the source revision it was built from (#123).
//!
//! # Why a build script exists for this
//!
//! A running server could not be asked what code it is. The only build identity
//! anywhere was one startup log line carrying `CARGO_PKG_VERSION` — a crate
//! version, which cannot distinguish two builds from different commits of the
//! same version. That is not a hypothetical gap: on 2026-07-31 a live server's
//! running image was found to differ from every preserved copy of "the same"
//! binary, and there was no way to establish which revision was actually
//! serving. Restarting it would have been a substitution presented as a
//! restoration.
//!
//! Only a build script can capture this. The revision is a property of the
//! source tree at compile time and is unavailable to the compiled program by
//! any other means.
//!
//! # Failing to find git is NOT an error
//!
//! A crate built from a crates.io tarball, a vendored copy, or an exported
//! archive has no repository at all. That is a perfectly legitimate build and
//! must not fail. Every probe here degrades to [`UNKNOWN`], which the runtime
//! surface reports as literally "unknown" — an honest absence the operator can
//! see, never a fabricated or defaulted value.
use ;
use Command;
use ;
/// What every probe reports when the answer genuinely cannot be established.
///
/// One sentinel, used everywhere, and it is a word rather than an empty string:
/// an empty value reads as a formatting bug at the far end, where "unknown"
/// reads as the measurement it is.
const UNKNOWN: &str = "unknown";
/// Absolute path of this package's `.git` directory, when there is one.
///
/// Asked of git rather than assembled from the manifest directory, because the
/// repository root is not at a fixed depth above the package and a worktree's
/// `.git` is a FILE pointing elsewhere, not a directory.
/// Register the files whose contents change when the checkout moves.
///
/// `HEAD` covers a detached checkout directly. On a branch, `HEAD` holds a
/// symbolic ref whose contents do NOT change as commits land, so the ref file it
/// names is registered too — otherwise every commit on the same branch would
/// leave the stamp untouched, which is precisely the common case.
/// Full 40-character commit hash of the source this binary is built from.
/// Whether the working tree carried uncommitted changes when this script ran.
///
/// **Stated limitation, and it is why the runtime field says "at build time".**
/// Cargo cannot watch a working tree, so this script re-runs on the triggers
/// registered above and on changes to this package's own sources — not on every
/// build. A tree dirtied in a DIFFERENT workspace crate after this ran can
/// therefore be reported as clean. It is stated rather than silently trusted:
/// `false` means "no uncommitted change was observed", not "the tree is
/// provably clean". `true` is always trustworthy, and so is the commit.
/// Seconds since the Unix epoch at which this script ran.
///
/// Honours `SOURCE_DATE_EPOCH` when set, so a reproducible-build environment
/// gets a reproducible stamp rather than this being the one field that defeats
/// it. Formatting is deferred to the runtime, which already has a date library;
/// carrying an integer across the boundary keeps the build script free of
/// dependencies.
/// Run a git command, returning its trimmed stdout only on a clean exit.
///
/// A missing git binary, a directory that is not a repository, and a command
/// that fails all collapse to `None` — every one of them means the same thing
/// here (the answer cannot be established) and none of them is a build failure.