greengate_runtime/attest_inputs.rs
1//! amont's side of attest's input fingerprints and canonical tree.
2//!
3//! The format itself (the `greensummit-inputs` grammar, fingerprints, the
4//! canonical tree, note keys, the opt-out, the completeness proof) belongs to
5//! the `greensummit` library, attest's one implementation of it (decisions
6//! ADR-0037). What stays here is amont's own knowledge on top of it: which
7//! route its LOCAL gate stamps take, and the key those stamps are filed
8//! under. The hook runs at the worktree top, so "the repository" is `.`.
9
10use std::path::Path;
11
12use greensummit::format::Names;
13use greensummit::spec::{at, coverage, plan_exempt, PlanExempt, DEPRECATED_OPT_OUT};
14
15pub use greensummit::spec::{conf_says_plan_exempt_off, CoverageError, Findings, Spec, EXEMPT};
16
17/// The repository a hook stands in: git starts hooks at the worktree top.
18fn here() -> &'static Path {
19 Path::new(".")
20}
21
22/// The spec `tree` commits, when it is there and valid. No spec, an invalid
23/// or ambiguous one, a tree git cannot list: `None`, and the gate stamps fall
24/// back to the tree (or, with no spec at all, the canonical route).
25pub(crate) fn spec_at(tree: &str) -> Option<Spec> {
26 match at(here(), tree) {
27 Ok(Some((_, spec))) => Some(spec),
28 _ => None,
29 }
30}
31
32/// The fingerprint of `gate` on `tree`, as attest computes it.
33pub(crate) fn fingerprint(tree: &str, spec: &Spec, gate: &str) -> Option<String> {
34 greensummit::fingerprint::fingerprint(here(), tree, spec, gate).ok()
35}
36
37/// The synthetic key of (gate, fingerprint) in the inputs ref of the name
38/// set `names` (its input domain builds the key): each set reads its own key
39/// in its own ref, never the other's.
40pub(crate) fn input_key(names: &Names, gate: &str, fp: &str) -> Option<String> {
41 greensummit::fingerprint::key_in(here(), names, gate, fp).ok()
42}
43
44/// Does `gate` declare `file`, itself or a directory above it? Asked of
45/// attest's coverage rule, so the ancestry is attest's, not a copy.
46pub(crate) fn declares(spec: &Spec, gate: &str, file: &str) -> bool {
47 coverage(
48 spec,
49 &[gate.to_string()],
50 &[file.as_bytes().to_vec()],
51 |_| true,
52 )
53 .is_empty()
54}
55
56/// The canonical fingerprint of `tree` when its gates are reused canonically:
57/// no spec at all (a successful listing that lists neither location), the
58/// repository has not opted out, and git could compute it. An invalid,
59/// ambiguous or unlistable spec opens no canonical route, so no canonical
60/// stamps.
61pub(crate) fn canonical_route(tree: &str) -> Option<String> {
62 match at(here(), tree) {
63 Ok(None) => {}
64 _ => return None,
65 }
66 if plan_exempt(here(), tree).is_off() {
67 return None;
68 }
69 greensummit::fingerprint::canon(here(), tree).ok()
70}
71
72/// The key under which the local gate stamps of a canonical fingerprint are
73/// filed in amont's own inputs ref: one note per fingerprint, holding the
74/// tokens of every gate that passed on it. amont's own preimage, never
75/// attest's, so a stamp can never be read as an attestation. `domain` is the
76/// gate name set's (`gate_stamp::GateNames::canon_domain`).
77pub(crate) fn canon_gate_key(domain: &str, canon: &str) -> Option<String> {
78 crate::git::stdout_piped_in(
79 here(),
80 &["hash-object", "--stdin"],
81 format!("{domain} {canon}\n").as_bytes(),
82 )
83 .filter(|s| greensummit::format::is_oid(s))
84}
85
86/// The line to print when HEAD opts out of the canonical tree through
87/// `set planExempt off` in `amont.conf` alone: still honoured, deprecated,
88/// and read by attest only until 2.0.0 (decisions ADR-0037).
89pub fn deprecated_opt_out() -> Option<&'static str> {
90 let tree = crate::git::stdout(&["rev-parse", "--verify", "--quiet", "HEAD^{tree}"])?;
91 (plan_exempt(here(), &tree) == PlanExempt::OffByAmontConf).then_some(DEPRECATED_OPT_OUT)
92}
93
94/// Do the required gates declare, completely, everything `tree` tracks?
95/// attest's proof (`greensummit check-inputs` runs the same one).
96pub fn check(tree: &str, required: &[String]) -> Result<Findings, CoverageError> {
97 greensummit::spec::check_coverage(here(), tree, required)
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 /// A tree git cannot list has no known spec, so it opens no route, and a
105 /// failing listing is not an absent opt-out: fail narrow.
106 #[test]
107 fn a_tree_git_cannot_list_opens_no_route() {
108 let bogus = "0123456789012345678901234567890123456789";
109 assert_eq!(spec_at(bogus), None);
110 assert_eq!(canonical_route(bogus), None);
111 assert!(plan_exempt(here(), bogus).is_off());
112 }
113
114 #[test]
115 fn a_gate_declares_a_file_or_a_directory_above_it() {
116 let spec = greensummit::spec::parse_spec(b"lint src amont.conf\ntest src\n").unwrap();
117 assert!(declares(&spec, "lint", "amont.conf"));
118 assert!(declares(&spec, "lint", "src/main.rs"));
119 assert!(!declares(&spec, "test", "amont.conf"));
120 assert!(
121 !declares(&spec, "fmt", "amont.conf"),
122 "an undeclared gate declares nothing"
123 );
124 }
125}