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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! 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.
///
/// Cargo treats a registered path that DOES NOT EXIST as always changed, so
/// every path printed here is checked first. Registering a path that might not
/// exist is not harmless: it makes this script, and with it the whole crate and
/// every test binary of it, rebuild on every invocation, forever, with no
/// warning anywhere. That is what a ref path assembled under a linked
/// worktree's git dir does (see the common-dir note below).
///
/// Three triggers, each covering what the others cannot:
///
/// 1. `HEAD` in this checkout's git dir. A detached checkout carries the commit
/// there directly; on a branch it holds a symbolic ref whose contents do not
/// change as commits land.
/// 2. `logs/HEAD` in this checkout's git dir — the HEAD reflog. Every move of
/// this checkout (commit, checkout, reset, merge, fast-forward) appends to
/// it, whether the branch ref is loose or packed, so it is the one file
/// that changes for every revision change a build script can observe.
/// 3. The branch's loose ref file and `packed-refs`, in the repository's
/// COMMON dir, for a repository with reflogs disabled.
///
/// The loose ref and `packed-refs` live in the COMMON dir, not the checkout's
/// git dir: in a linked worktree `--git-dir` is the worktree's private
/// directory (its `HEAD`, index and reflogs) and holds no `refs/heads` at all.
/// A ref path assembled under it never exists.
/// Register a path with cargo only when it exists right now.
///
/// A loose ref that is later packed disappears; its absence then re-runs this
/// script once (a missing registered path counts as changed), and the re-run
/// registers `packed-refs` in its place. A ref that is later written loose
/// again is caught by the HEAD reflog. Either way no path that does not exist
/// is ever left registered.
/// Absolute path of the repository's COMMON git directory — where `refs/` and
/// `packed-refs` live for every worktree of the repository.
///
/// For the main checkout this is the same directory as [`git_dir`]; for a
/// linked worktree it is the parent repository's `.git`. Asked in absolute
/// form first; a git too old for `--path-format` answers the plain form
/// relative to the package directory, which is where the command ran.
/// 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.