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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! Change-history (VCS) metrics: per-file signals derived from version
//! control history rather than the AST.
//!
//! This is the project's first metric family that is language-agnostic
//! and not AST-derived (issue #328). It surfaces files most likely to
//! harbour bugs or vulnerabilities using the signals the empirical
//! literature most consistently backs — recent churn, commit frequency,
//! author count and ownership dilution, bug- and security-fix history —
//! combined into an ordinal `risk_score`.
//!
//! # Layout
//!
//! The generic surface (`error`, `options`, `stats`, `identity`,
//! `classify`, `score`, `hotspot`, `jit`, and `build_history_index`)
//! carries no backend reference, so a future backend (Mercurial,
//! Jujutsu, …; issue #335) reuses it unchanged. Backend-specific code
//! lives under the `git` module behind the `vcs-git` Cargo feature.
//!
//! Two scoring granularities are offered: `build_history_index` ranks
//! *files* at a ref (issue #328), while `score_commit` scores a single
//! *commit* for just-in-time defect-induction risk (issue #331).
//!
//! v1 deliberately omits a `Backend` trait: with a single backend it
//! would be premature abstraction. `build_history_index` delegates to
//! the one available backend; the trait is extracted when a second
//! backend lands.
pub
pub use ;
pub use ;
pub use Error;
pub use AuthorHashKey;
pub use ;
pub use ;
pub use Stats;
pub use ;
/// Per-function change-history attribution (issue #329), surfaced when a
/// front end opts into per-function VCS metrics. See [`PerFunctionBlame`].
pub use ;
use HashMap;
use ;
/// The result of one history walk: per-file [`Stats`] keyed by
/// repository-relative path, plus walk-level metadata.
/// Walk the change history rooted at `root` and build a per-file
/// [`HistoryIndex`].
///
/// Runs **once** per invocation (before any AST walk): walking history
/// per file would be catastrophic on large repositories. The single
/// available backend is selected automatically by probing the working
/// tree.
///
/// # Errors
///
/// Returns [`Error::NotARepository`] when `root` is not inside a
/// supported VCS working tree, or a backend-specific variant when the
/// walk itself fails.
/// Like [`build_history_index`], but reuse and update the persistent
/// change-history cache per `config` (issue #334).
///
/// On an unchanged tree this replays a cached event log instead of
/// re-walking; when `HEAD` has advanced it walks only the new commits and
/// splices them onto the cached tail. The result is bit-identical to an
/// uncached [`build_history_index`] at the same reference time — the cache
/// is a pure optimization. A missing or corrupt entry is silently
/// recomputed; an entry is ignored when the schema, score, or option
/// fingerprint differs (window changes force a fresh walk). With
/// [`CacheConfig::enabled`] `false` this degrades to a plain walk (still
/// honouring [`CacheConfig::clear`]).
///
/// # Errors
///
/// The same variants as [`build_history_index`], plus [`Error::Cache`]
/// when `--clear-cache` is requested but the cache directory cannot be
/// removed. A failure to *write* a fresh entry is logged, not returned.
/// Parse an `--as-of` timestamp into Unix seconds.
///
/// Accepts RFC 3339 / ISO 8601, a bare `@<unix>` epoch, and the git
/// date spellings gix understands. Front ends use this to fill
/// [`Options::as_of`] for reproducible snapshots.
///
/// # Errors
///
/// Returns [`Error::InvalidTimestamp`] when the input is unparseable.
/// Score a single commit for just-in-time defect-induction risk
/// (issue #331).
///
/// `spec` is any revision spelling the backend resolves to a commit
/// (`HEAD`, a SHA, a tag, `main~3`, …). The commit is scored against its
/// first parent; the touched files' priors and the author's experience
/// are measured from the history *before* it, windowed by `options`.
/// Returns a [`JitReport`] with the feature breakdown, per-group
/// contributions, and the ordinal composite [`JitReport::risk_score`].
///
/// # Errors
///
/// Returns [`Error::NotARepository`] when `root` is not inside a
/// supported VCS working tree, [`Error::ResolveRef`] when `spec` does not
/// resolve to a commit, or a walk/diff variant when the history walk
/// itself fails.
/// Score an arbitrary unified `diff` for just-in-time defect-induction risk
/// (issue #580).
///
/// Unlike [`score_commit`], a bare diff carries **no author, parent, or
/// file history**, so only the *size* and *diffusion* feature groups are
/// computable. The result is a partial [`jit::JitDiffReport`] whose
/// history / experience / purpose groups are **absent from the type**
/// (not present as zero), and whose
/// [`partial_risk_score`](jit::JitDiffReport::partial_risk_score) is **not
/// comparable** to a commit score — rank diffs against other diffs only.
/// See [`jit::JitDiffReport`] for the full contract.
///
/// `diff` must be a git-style unified diff carrying `diff --git` file
/// headers (as produced by `git diff` / `git format-patch`), with one or
/// more file stanzas. Plain `diff -u` output without those headers and
/// combined / merge diffs (`git diff --cc`, `@@@` headers) are not
/// supported. No repository access is needed; `options` does not
/// participate (a bare diff has nothing to window).
///
/// # Errors
///
/// Returns [`Error::InvalidDiff`] when the diff is structurally malformed
/// (a bad `@@` hunk header, or a `+`/`-` body line outside any hunk) or
/// carries diff content with no `diff --git` file header at all (plain
/// `diff -u` or a combined/merge diff).
/// Sample the change-history metrics at `points` evenly-spaced moments
/// across `span_secs`, ending at `options.as_of` (or wall-clock now),
/// building a [`trend::Trend`] time series (issue #333).
///
/// Each point re-anchors at the mainline tip that existed at or before
/// that moment, so the result is a faithful historical snapshot rather
/// than today's tree windowed differently — see [`trend`] for the schema
/// and the cross-snapshot rename limitation. `options` supplies the
/// windows / bot / merge / rename / formula knobs shared by every point;
/// its `reference` selects which mainline to follow.
///
/// # Errors
///
/// Returns [`Error::InvalidTrend`] when `points` is outside
/// `[MIN_TREND_POINTS, MAX_TREND_POINTS]`
/// ([`trend::MIN_TREND_POINTS`] / [`trend::MAX_TREND_POINTS`]),
/// [`Error::NotARepository`] when `root` is not a working tree,
/// [`Error::ResolveRef`] when the base reference does not resolve, or a
/// walk/diff variant when a sampled snapshot fails.
/// Rank `entries` by descending risk score, breaking ties on the file
/// path (ascending), then truncate to the top `top` (`0` = keep all).
///
/// The single definition of the `bca vcs` / `POST /vcs` / `vcs_metrics`
/// output ordering, shared by all three front ends so the float-compare
/// and tie-break contract cannot drift between them. `key` extracts the
/// `(path, risk_score)` pair from each entry, so callers keep their own
/// per-crate entry types.
/// Discover the working-tree root of the repository containing `path`.
///
/// Returns the canonicalised work-tree directory of the repository that
/// encloses `path` (a file or directory), or `None` when `path` is not
/// inside a repository or the repository is bare (no work tree). Repository
/// discovery walks upward from `path` the same way `git` itself does.
///
/// Front ends use this to coalesce a batch of files onto the repository
/// each belongs to — two files in different subdirectories of the same
/// checkout resolve to the **same** root, so a per-repo [`HistoryIndex`]
/// (or [`PerFunctionBlame`] engine) can be built once and shared across
/// them rather than rebuilt per directory (issue #670).
///
/// ```no_run
/// use std::path::Path;
/// // Two files in different subdirectories of the *same* checkout both
/// // resolve to that checkout's work-tree root: `workdir_root` walks
/// // upward from each file and lands on the same `Some(root)`. (Shown
/// // with absolute paths under a real checkout; `no_run` because no such
/// // repository exists at doctest time.)
/// let a = big_code_analysis::vcs::workdir_root(Path::new("/checkout/src/a.rs"));
/// let b = big_code_analysis::vcs::workdir_root(Path::new("/checkout/tests/b.rs"));
/// // When `/checkout` is a git work tree, both are `Some("/checkout")`, so a
/// // per-repo index can be built once and shared across the batch.
/// if let (Some(root_a), Some(root_b)) = (a, b) {
/// assert_eq!(root_a, root_b);
/// }
/// ```