Skip to main content

amont_runtime/
pushrefs.rs

1//! The refs git feeds `pre-push` on stdin, read ONCE and shared.
2//!
3//! git writes one line per ref being pushed:
4//!
5//! ```text
6//! <local ref> <local oid> <remote ref> <remote oid>
7//! ```
8//!
9//! This exists because stdin can only be consumed once. While checks were
10//! separate processes with INHERITED stdin, whichever ran first drained it and
11//! the rest saw EOF — silently. Two repos in the fleet had a custom
12//! `pre-push-branch-protect.sh` whose `while read` loop sorted BEFORE
13//! `pre-push-run-tests-js`, so the test gate received no refs and ran nothing.
14//! Nobody noticed, because "no refs" and "nothing to test" look identical.
15//!
16//! Reading it in one place and lending the result to every check removes that
17//! whole class of bug, and makes it impossible to reintroduce by adding a
18//! second stdin reader.
19
20use std::io::BufRead;
21use std::sync::OnceLock;
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct PushRef {
25    pub local_ref: String,
26    pub local_oid: String,
27    pub remote_ref: String,
28    pub remote_oid: String,
29}
30
31/// Lazily-read pushed refs. `OnceLock` rather than `OnceCell` because
32/// pre-commit runs its checks on threads and `Ctx` must therefore be `Sync`.
33#[derive(Default)]
34pub struct PushRefs(OnceLock<Vec<PushRef>>);
35
36impl PushRefs {
37    /// Read on first use. A check that never asks never blocks on stdin —
38    /// which matters, because pre-commit's stdin is not a ref list.
39    pub fn get(&self) -> &[PushRef] {
40        self.0.get_or_init(|| parse(std::io::stdin().lock()))
41    }
42
43    /// Pre-populated, so `.get()` never touches stdin at all.
44    ///
45    /// For a context with no real `pre-push` invocation to read refs from —
46    /// `amont run <pre-push-check>` is the one today — where `.get()`
47    /// would otherwise block reading a TTY that has no ref list coming, or
48    /// (piped from `/dev/null`, say) read nothing and let the check treat an
49    /// empty ref list as "nothing to check": branch-protect calls that "no
50    /// push to a protected branch", and a scope-gated suite just never loops.
51    pub fn preloaded(refs: Vec<PushRef>) -> PushRefs {
52        let cell = OnceLock::new();
53        let _ = cell.set(refs);
54        PushRefs(cell)
55    }
56}
57
58/// Synthesise the one `PushRef` a standalone invocation — no real `pre-push`
59/// on the other end of stdin — has no other way to obtain: `@{u}..HEAD`.
60///
61/// Shared by `list --pushed` and `amont run <pre-push-check>`, both of
62/// which need to answer "what would this check see" without an actual push
63/// in flight.
64pub fn synthetic_from_upstream() -> Result<PushRef, String> {
65    // `None` means "no upstream", i.e. a branch that has never been pushed,
66    // and that is not an error — just nothing this can answer yet.
67    let Some(upstream) =
68        crate::git::stdout(&["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"])
69    else {
70        return Err(
71            "no upstream configured for the current branch — nothing has been \
72             pushed yet, so there is nothing to diff against"
73                .to_string(),
74        );
75    };
76    let Some(local_oid) = crate::git::stdout(&["rev-parse", "HEAD"]) else {
77        return Err("could not resolve HEAD".to_string());
78    };
79    let Some(remote_oid) = crate::git::stdout(&["rev-parse", "@{u}"]) else {
80        return Err(format!("could not resolve upstream {upstream}"));
81    };
82    let local_ref =
83        crate::git::stdout(&["symbolic-ref", "-q", "HEAD"]).unwrap_or_else(|| "HEAD".to_string());
84    Ok(PushRef {
85        local_ref: local_ref.clone(),
86        local_oid,
87        remote_ref: local_ref,
88        remote_oid,
89    })
90}
91
92pub fn parse<R: BufRead>(r: R) -> Vec<PushRef> {
93    r.lines()
94        .map_while(Result::ok)
95        .filter_map(|line| {
96            let mut f = line.split_whitespace();
97            let (a, b, c, d) = (f.next()?, f.next()?, f.next()?, f.next()?);
98            Some(PushRef {
99                local_ref: a.to_owned(),
100                local_oid: b.to_owned(),
101                remote_ref: c.to_owned(),
102                remote_oid: d.to_owned(),
103            })
104        })
105        .collect()
106}
107
108/// Every path touched by the refs being pushed.
109///
110/// Shared because two callers need exactly this list and would otherwise write
111/// the zero-oid and range handling twice: `cargo-test` decides whether a suite
112/// is worth running, and a declared pre-push check decides whether its `scope`
113/// applies. A copy that got the delete case wrong would run a test suite on a
114/// branch deletion.
115pub fn changed_files(refs: &[PushRef]) -> Vec<String> {
116    let zero = crate::git::stdout(&["hash-object", "--stdin"])
117        .map(|h| "0".repeat(h.len()))
118        .unwrap_or_else(|| "0".repeat(40));
119    let mut changed: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
120    for r in refs {
121        changed.extend(changed_files_for(r, &zero));
122    }
123    changed.into_iter().collect()
124}
125
126/// Every path touched by ONE ref being pushed.
127///
128/// Split out from `changed_files` so a caller that runs a suite in a
129/// per-ref worktree — `where_to_run` takes one tip, not the whole ref
130/// list, for exactly this reason — can ask "what did THIS ref change"
131/// instead of the aggregate across every ref in the push.
132pub fn changed_files_for(r: &PushRef, zero: &str) -> Vec<String> {
133    if r.local_oid == zero {
134        return Vec::new(); // deleting a ref pushes no code
135    }
136    if r.remote_oid == zero {
137        // A brand-new ref: no remote tree to diff against, so the
138        // `remote..local` trick below does not apply. Walk every commit
139        // this push would introduce that no remote-tracking ref already
140        // has — `rev-list --not --remotes` — rather than just the tip's
141        // own diff against its parent, which silently missed every
142        // earlier commit on a multi-commit new branch: a crate added two
143        // commits back, with a docs-only commit on top, reported only the
144        // docs file as changed, so a scope-gated check like
145        // `pre-push-cargo-test` never ran.
146        let commits = crate::git::stdout(&["rev-list", &r.local_oid, "--not", "--remotes"]);
147        return match commits {
148            Some(commits) if !commits.is_empty() => diff_tree_stdin(&commits),
149            // No remote-tracking ref to exclude anything against (e.g. no
150            // remote configured at all) — fall back to the tip alone,
151            // which is at least what the check always did before.
152            _ => diff_tree_stdin(&r.local_oid),
153        };
154    }
155    range_changed_files(&r.remote_oid, &r.local_oid)
156}
157
158/// `(commit, files)` for every commit ONE ref would push — the same walks as
159/// [`changed_files_for`], keeping the commit identities its union discards.
160///
161/// For a caller matching per-commit STATE (a `gate_stamp` note) against
162/// per-commit CHANGES, the union is useless: it says the ref touched `a.ts`
163/// without saying which commit did, and the whole question is whether THAT
164/// commit was checked. One `diff-tree` per commit rather than a parse of
165/// `--stdin` output with commit ids left in: a push is a handful of commits,
166/// and this reuses `diff_tree_stdin`'s tested flag set (`-z` for unusual
167/// bytes, `-m` for merges) instead of growing a second parser for the
168/// interleaved id-and-paths stream.
169pub fn commits_and_files_for(r: &PushRef, zero: &str) -> Vec<(String, Vec<String>)> {
170    if r.local_oid == zero {
171        return Vec::new(); // deleting a ref pushes no code
172    }
173    let commits = if r.remote_oid == zero {
174        // Same shape as `changed_files_for`'s new-branch arm, same fallback.
175        match crate::git::stdout(&["rev-list", &r.local_oid, "--not", "--remotes"]) {
176            Some(commits) if !commits.is_empty() => commits,
177            _ => r.local_oid.clone(),
178        }
179    } else {
180        let range = format!("{}..{}", r.remote_oid, r.local_oid);
181        match crate::git::stdout(&["rev-list", &range]) {
182            Some(commits) if !commits.is_empty() => commits,
183            _ => return Vec::new(),
184        }
185    };
186    commits
187        .lines()
188        .map(|c| (c.to_string(), diff_tree_stdin(c)))
189        .collect()
190}
191
192/// Every path touched by ANY commit reachable in `remote..local`, not just
193/// the net difference between the two endpoint trees.
194///
195/// `diff-tree remote..local` looks like the obvious tool, and is wrong: for
196/// `diff`/`diff-tree`, a two-dot range is shorthand for a straight two-tree
197/// comparison (`diff-tree remote local`) — unlike `log`, where the identical
198/// syntax means a commit walk. A file changed by one commit and reverted by
199/// a later one in the same push nets to "unchanged" between the endpoints,
200/// so a scope-gated check never learns the file was touched at all.
201/// `rev-list` walks the commits; `diff-tree --stdin` diffs each one against
202/// its own parent, and the per-commit results are unioned here.
203fn range_changed_files(remote_oid: &str, local_oid: &str) -> Vec<String> {
204    let range = format!("{remote_oid}..{local_oid}");
205    let Some(commits) = crate::git::stdout(&["rev-list", &range]) else {
206        return Vec::new();
207    };
208    if commits.is_empty() {
209        return Vec::new();
210    }
211    diff_tree_stdin(&commits)
212}
213
214/// Feed a newline-separated list of commit ids through `diff-tree --stdin`,
215/// diffing each against its own parent(s), and return the union of paths.
216///
217/// `-z`, not bare `--name-only`. Without it git QUOTES any path holding an
218/// "unusual" byte, non-ASCII included: `é.ts` prints as the nine-byte literal
219/// `"\303\251.ts"`. That string ends with neither `.ts` nor anything else the
220/// callers look for, so `is_js`, `is_rust_path` and `Scope::matches` all miss
221/// it and a scope-gated pre-push check silently never runs on the very commit
222/// that changed the file. `git::split_nul_paths` is the same parser
223/// `stdout_paths` uses, kept in one tested place rather than copied here.
224fn diff_tree_stdin(commits: &str) -> Vec<String> {
225    // `git::stdout` trims the trailing newline `rev-list` itself always
226    // writes — and `diff-tree --stdin` treats "no newline after this hash"
227    // as "the line isn't finished yet", silently dropping the LAST commit
228    // rather than reading it. Put the newline back before feeding it in.
229    //
230    // `-m`: without it, `diff-tree` shows NOTHING for a merge commit at all
231    // — confirmed empirically, not merely documented — so a file edited only
232    // to resolve a conflict (never touched by either parent individually) is
233    // invisible to a scope-gated check. `-m` diffs a merge against EACH
234    // parent and unions the results, which is exactly "did this commit,
235    // merge or not, touch this path" — the caller already de-duplicates the
236    // combined list, so the extra per-parent repeats cost nothing.
237    crate::git::stdout_piped_raw(
238        &[
239            "diff-tree",
240            "--no-commit-id",
241            "--name-only",
242            "-r",
243            "-m",
244            "-z",
245            "--stdin",
246        ],
247        &format!("{}\n", commits.trim_end()),
248    )
249    .map(|raw| crate::git::split_nul_paths(&raw))
250    .unwrap_or_default()
251}
252
253#[cfg(test)]
254mod tests {
255    use super::*;
256
257    #[test]
258    fn parses_the_four_fields() {
259        let got = parse(&b"refs/heads/x aaa refs/heads/y bbb\n"[..]);
260        assert_eq!(got.len(), 1);
261        assert_eq!(got[0].local_ref, "refs/heads/x");
262        assert_eq!(got[0].remote_ref, "refs/heads/y");
263        assert_eq!(got[0].remote_oid, "bbb");
264    }
265
266    #[test]
267    fn several_refs_and_junk_lines() {
268        let got = parse(&b"a 1 b 2\ngarbage\n\nc 3 d 4\n"[..]);
269        assert_eq!(got.len(), 2, "short lines are skipped, not fatal");
270        assert_eq!(got[1].local_ref, "c");
271    }
272
273    #[test]
274    fn empty_stdin_is_no_refs_not_an_error() {
275        assert!(parse(&b""[..]).is_empty());
276    }
277
278    /// Mirrors `git::a_non_ascii_path_is_not_reinterpreted_as_its_quoted_form`,
279    /// because this module used to parse `diff-tree` output ITSELF, by lines,
280    /// with no `-z`.
281    ///
282    /// Under bare `--name-only`, git prints `é.ts` as the nine-byte literal
283    /// `"\303\251.ts"` — backslashes, digits and quotes standing in for two
284    /// UTF-8 bytes. That string ends with neither `.ts` nor `.rs`, so `is_js`,
285    /// `is_rust_path` and `Scope::matches` all said "not mine" and the
286    /// scope-gated pre-push check silently never ran on the one commit that
287    /// changed the file. Feeding the same real bytes through `-z` output must
288    /// hand the path back untouched.
289    #[test]
290    fn a_non_ascii_path_survives_the_diff_tree_parse() {
291        let mut raw = "src/é.ts".as_bytes().to_vec();
292        raw.push(0);
293        raw.extend_from_slice(b"src/plain.ts\0");
294        let got = crate::git::split_nul_paths(&raw);
295        assert_eq!(
296            got,
297            vec!["src/é.ts".to_string(), "src/plain.ts".to_string()]
298        );
299        assert!(
300            got[0].ends_with(".ts"),
301            "the quoted form ends with a quote, not an extension, and is why \
302             a scope-gated check never fired: {:?}",
303            got[0]
304        );
305    }
306}