amont-runtime 1.3.0

The amont hook logic: registry, dispatchers, checks and the trust model
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! What a check IS, as one value rather than four tables.
//!
//! Before this, a check was spread across `REGISTRY` (name → fn), two ordered
//! name lists, and a language table in the fleet crate — four places keyed by
//! the same string, held together by reconciliation tests. Those tests were
//! good, but they policed a shape that should not have been splittable. With
//! the metadata attached to the check, there is nothing left to reconcile.
//!
//! It also gives external checks somewhere to exist. A third party cannot add a
//! Rust module without rebuilding the binary, so extension means a declared
//! command implementing this same trait — and the dispatcher not caring which
//! kind it is holding.

use crate::registry::Ctx;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stage {
    PreCommit,
    PrePush,
}

impl Stage {
    pub fn as_str(self) -> &'static str {
        match self {
            Stage::PreCommit => "pre-commit",
            Stage::PrePush => "pre-push",
        }
    }
}

/// When a check is relevant, declared rather than reimplemented by every
/// reader.
///
/// A CONJUNCTION, not a choice: ruff is `.py` files AND a ruff config; clippy
/// is `.rs` AND `Cargo.toml`. An earlier design offered these as alternatives
/// plus a `Custom` escape hatch, which would have swallowed nearly every check
/// and left the dashboard knowing nothing.
/// A git operation that is part-way through.
///
/// Detected from the marker files git writes into `$GIT_DIR`, which is how git
/// itself and every prompt-writer answers the question.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitState {
    Merge,
    Rebase,
    CherryPick,
    Revert,
    Bisect,
}

impl GitState {
    /// The marker git writes. `rebase-merge` and `rebase-apply` are
    /// DIRECTORIES; the rest are files, and `Path::exists` covers both.
    pub fn markers(self) -> &'static [&'static str] {
        match self {
            GitState::Merge => &["MERGE_HEAD"],
            GitState::Rebase => &["REBASE_HEAD", "rebase-merge", "rebase-apply"],
            GitState::CherryPick => &["CHERRY_PICK_HEAD"],
            GitState::Revert => &["REVERT_HEAD"],
            GitState::Bisect => &["BISECT_LOG"],
        }
    }

    pub fn as_str(self) -> &'static str {
        match self {
            GitState::Merge => "a merge",
            GitState::Rebase => "a rebase",
            GitState::CherryPick => "a cherry-pick",
            GitState::Revert => "a revert",
            GitState::Bisect => "a bisect",
        }
    }

    pub const ALL: [GitState; 5] = [
        GitState::Merge,
        GitState::Rebase,
        GitState::CherryPick,
        GitState::Revert,
        GitState::Bisect,
    ];
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Scope {
    /// Extensions that trigger it. Empty means any change.
    pub files: &'static [&'static str],
    /// Config paths that opt a repository in. Empty means always on.
    pub opt_in: &'static [&'static str],
    /// Git operations during which this check does not run.
    ///
    /// The other half of "when does this apply". `files` and `opt_in` say which
    /// REPOSITORIES and which CHANGES; this says which repository STATES — a
    /// question that used to be answered by one hard-coded `CHERRY_PICK_HEAD`
    /// test in one dispatcher, with the other carrying a comment admitting it
    /// had none because the shell version had none.
    pub not_during: &'static [GitState],
}

impl Scope {
    pub const ALWAYS: Scope = Scope {
        files: &[],
        opt_in: &[],
        not_during: &[],
    };

    pub const fn files(files: &'static [&'static str]) -> Scope {
        Scope {
            files,
            opt_in: &[],
            not_during: &[],
        }
    }

    pub const fn new(files: &'static [&'static str], opt_in: &'static [&'static str]) -> Scope {
        Scope {
            files,
            opt_in,
            not_during: &[],
        }
    }

    /// The same scope, silent during these operations.
    pub const fn not_during(self, states: &'static [GitState]) -> Scope {
        Scope {
            files: self.files,
            opt_in: self.opt_in,
            not_during: states,
        }
    }

    /// Would this check ever fire, given the paths a repository contains?
    ///
    /// Deliberately coarse for checks that resolve an ancestor at run time —
    /// `cargo-fmt` declares `Cargo.toml` meaning "somewhere here" while
    /// enforcing "nearest above the staged file". The dispatcher asks the
    /// precise question by running the check; this answers the dashboard's
    /// question, "would it ever fire", where over-approximating is the safe
    /// direction.
    pub fn matches(&self, paths: &[String]) -> bool {
        let by_ext = self.files.is_empty()
            || paths
                .iter()
                .any(|path| self.files.iter().any(|ext| path.ends_with(ext)));
        let opted_in = self.opt_in.is_empty()
            || paths.iter().any(|p| {
                let name = p.rsplit('/').next().unwrap_or(p);
                self.opt_in.iter().any(|c| {
                    // A trailing `*` is a prefix match: `.kube-linter*.yaml`.
                    match c.split_once('*') {
                        Some((pre, suf)) => name.starts_with(pre) && name.ends_with(suf),
                        None => name == *c,
                    }
                })
            });
        by_ext && opted_in
    }
}

/// What a check meant, as opposed to what it printed.
///
/// The fourth variant is the point. Fifteen sites used to warn and return 0,
/// collapsing two different situations: "I ran and found something you should
/// know" and "I could not run at all". `ruff config found but no ruff binary`
/// was indistinguishable from ruff running clean — to the dispatcher, and to
/// the dashboard. A repository where a check has silently never executed read
/// as one where it passes.
///
/// That is the same invisibility `hook.skip` had before skipped checks were
/// announced, and it took three PRs to notice there.
///
/// The check still prints its own message; this only classifies the result.
///
/// Deliberately NO `Default`. It used to be `Failed`, to fill the slot of a
/// check whose thread died — a real rule, but `Default` means "the neutral
/// value" to every reader and to every `#[derive(Default)]` that might later
/// contain one. The rule is now written where it applies, in the runner.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Outcome {
    Passed,
    /// Ran, found a problem. Whether that blocks is `Severity`, not this.
    Failed,
    /// Ran, found something worth saying, which does not block.
    Warned,
    /// Ran, found a problem, and REPAIRED it. The commit proceeds with the
    /// repair staged, which is neither `Passed` (something happened, and the
    /// author should know their files changed) nor `Failed`.
    Fixed,
    /// COULD NOT RUN — a tool is missing, or the opt-in config is absent.
    Unavailable,
}

/// What a HOOK concluded — the only thing git actually reads.
///
/// Distinct from `Outcome`, which is what one CHECK concluded. Git has exactly
/// two questions to ask a hook, so this has exactly two answers, and the `i32`
/// that expresses them lives at the process boundary rather than being threaded
/// through every hook, dispatcher and handler as it used to be.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verdict {
    Proceed,
    Block,
}

impl Verdict {
    /// The exit code git reads. The ONLY place a hook result becomes a number.
    pub fn exit_code(self) -> i32 {
        match self {
            Verdict::Proceed => 0,
            Verdict::Block => 1,
        }
    }

    pub fn blocking(blocked: bool) -> Verdict {
        if blocked {
            Verdict::Block
        } else {
            Verdict::Proceed
        }
    }
}

/// Whether a failing check stops the commit or merely reports.
///
/// Declared per check and overridable per repository with
/// `git config amont.severity.<check> warn`. That is a better escape hatch
/// than `hook.skip`, which is all-or-nothing and invisible enough that
/// `hook.skip = e` disables all twenty checks: a downgrade keeps the signal and
/// removes only the block.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
    Block,
    Warn,
}

impl Severity {
    /// The ONE mapping from configured text to severity.
    ///
    /// There were four: this key's reader, the manifest's severity column, the
    /// dashboard's copy, and the dashboard's reverse mapping for `--json`. They
    /// agreed, but nothing made them — and the dashboard's copy is its
    /// prediction of what the dispatcher will do, which is the one thing it must
    /// never get wrong.
    ///
    /// `None` for anything else, deliberately: git validates nothing here, so an
    /// unrecognised value must fall back to the declared severity rather than
    /// silently disable a check.
    pub fn parse(value: &str) -> Option<Severity> {
        match value {
            "warn" => Some(Severity::Warn),
            "block" => Some(Severity::Block),
            _ => None,
        }
    }

    /// How it is written in config and in `--json`.
    pub fn as_str(self) -> &'static str {
        match self {
            Severity::Block => "block",
            Severity::Warn => "warn",
        }
    }
}

/// Whether a check can rewrite the files it inspects.
///
/// Off by default and per check, never global: a hook that edits your files
/// without being asked is a larger surprise than one that complains.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Fix {
    /// Reports only. Every check, until somebody declares otherwise.
    None,
    /// Runs a command that rewrites files, and stages what it changed.
    ///
    /// Only reachable from a `Stage::PreCommit` declaration. A pre-push hook
    /// must not modify the worktree or index: the pushed commit would then
    /// differ from the tree the developer is looking at.
    Rewrite,
}

impl Fix {
    /// How it is written in `--json`. No `parse()`: nothing reads a `Fix`
    /// back out of text — the manifest's `fix` marker has its own, unrelated
    /// parsing path in `manifest.rs`.
    pub fn as_str(self) -> &'static str {
        match self {
            Fix::None => "none",
            Fix::Rewrite => "rewrite",
        }
    }
}

/// One check, whether compiled in or declared by the repository.
///
/// `Sync` because `pre-commit` hands every check to its own thread. Both
/// implementations satisfy it for free, and requiring it here is what lets the
/// dispatcher hold `&'static dyn Check` without caring which kind it has.
pub trait Check: Sync {
    fn name(&self) -> &str;
    fn stage(&self) -> Stage;
    fn scope(&self) -> Scope;
    fn severity(&self) -> Severity;
    /// Whether this check can repair what it finds. `None` for almost all.
    fn fix(&self) -> Fix {
        Fix::None
    }
    fn run(&self, ctx: &Ctx) -> Outcome;
}

/// A check compiled into the binary.
pub struct Builtin {
    pub name: &'static str,
    pub stage: Stage,
    pub scope: Scope,
    pub severity: Severity,
    pub run: fn(&Ctx) -> Outcome,
    /// Almost always `Fix::None`; see `CHECKS`.
    pub fix: Fix,
}

impl Check for Builtin {
    fn name(&self) -> &str {
        self.name
    }
    fn stage(&self) -> Stage {
        self.stage
    }
    fn scope(&self) -> Scope {
        self.scope
    }
    fn severity(&self) -> Severity {
        self.severity
    }
    fn fix(&self) -> Fix {
        self.fix
    }
    fn run(&self, ctx: &Ctx) -> Outcome {
        (self.run)(ctx)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Round-trips, and refuses everything else. A `Some` for an unknown value
    /// would turn a typo into a silent disable.
    #[test]
    fn severity_parses_exactly_the_two_words_it_documents() {
        for s in [Severity::Block, Severity::Warn] {
            assert_eq!(Severity::parse(s.as_str()), Some(s));
        }
        for bad in ["", "Warn", "WARN", "advisory", "true", "1", " warn"] {
            assert_eq!(Severity::parse(bad), None, "{bad:?} must not parse");
        }
    }

    #[test]
    fn fix_says_how_it_is_written_in_json() {
        assert_eq!(Fix::None.as_str(), "none");
        assert_eq!(Fix::Rewrite.as_str(), "rewrite");
    }

    #[test]
    fn always_matches_anything() {
        assert!(Scope::ALWAYS.matches(&[]));
        assert!(Scope::ALWAYS.matches(&["README.md".into()]));
    }

    #[test]
    fn extensions_gate_on_the_file_type() {
        let s = Scope::files(&[".rs"]);
        assert!(s.matches(&["src/main.rs".into()]));
        assert!(!s.matches(&["README.md".into()]));
    }

    /// The case the enum could not express: BOTH conditions must hold.
    #[test]
    fn files_and_opt_in_are_a_conjunction() {
        let ruff = Scope::new(&[".py"], &["ruff.toml", "pyproject.toml"]);
        assert!(
            !ruff.matches(&["a.py".into()]),
            "python alone is not enough — the repo must opt in"
        );
        assert!(
            !ruff.matches(&["pyproject.toml".into()]),
            "and a config alone is not enough without python"
        );
        assert!(ruff.matches(&["a.py".into(), "pyproject.toml".into()]));
    }

    /// `.kube-linter*.yaml` is a real config name in this repo's own hooks.
    #[test]
    fn a_trailing_star_is_a_prefix_match() {
        let s = Scope::new(&[".yaml"], &[".kube-linter*.yaml"]);
        assert!(s.matches(&["k8s/x.yaml".into(), ".kube-linter-prod.yaml".into()]));
        assert!(!s.matches(&["k8s/x.yaml".into(), ".kube-lint.yaml".into()]));
    }

    /// Opt-in matches a BASENAME anywhere, which is what makes the coarse
    /// answer right for a check that resolves an ancestor when it runs.
    #[test]
    fn opt_in_matches_a_nested_manifest() {
        let cargo = Scope::new(&[".rs"], &["Cargo.toml"]);
        assert!(cargo.matches(&["crates/a/src/lib.rs".into(), "crates/a/Cargo.toml".into()]));
    }
}