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/// Every path touched by ANY commit reachable in `remote..local`, not just
159/// the net difference between the two endpoint trees.
160///
161/// `diff-tree remote..local` looks like the obvious tool, and is wrong: for
162/// `diff`/`diff-tree`, a two-dot range is shorthand for a straight two-tree
163/// comparison (`diff-tree remote local`) — unlike `log`, where the identical
164/// syntax means a commit walk. A file changed by one commit and reverted by
165/// a later one in the same push nets to "unchanged" between the endpoints,
166/// so a scope-gated check never learns the file was touched at all.
167/// `rev-list` walks the commits; `diff-tree --stdin` diffs each one against
168/// its own parent, and the per-commit results are unioned here.
169fn range_changed_files(remote_oid: &str, local_oid: &str) -> Vec<String> {
170    let range = format!("{remote_oid}..{local_oid}");
171    let Some(commits) = crate::git::stdout(&["rev-list", &range]) else {
172        return Vec::new();
173    };
174    if commits.is_empty() {
175        return Vec::new();
176    }
177    diff_tree_stdin(&commits)
178}
179
180/// Feed a newline-separated list of commit ids through `diff-tree --stdin`,
181/// diffing each against its own parent(s), and return the union of paths.
182///
183/// `-z`, not bare `--name-only`. Without it git QUOTES any path holding an
184/// "unusual" byte, non-ASCII included: `é.ts` prints as the nine-byte literal
185/// `"\303\251.ts"`. That string ends with neither `.ts` nor anything else the
186/// callers look for, so `is_js`, `is_rust_path` and `Scope::matches` all miss
187/// it and a scope-gated pre-push check silently never runs on the very commit
188/// that changed the file. `git::split_nul_paths` is the same parser
189/// `stdout_paths` uses, kept in one tested place rather than copied here.
190fn diff_tree_stdin(commits: &str) -> Vec<String> {
191    // `git::stdout` trims the trailing newline `rev-list` itself always
192    // writes — and `diff-tree --stdin` treats "no newline after this hash"
193    // as "the line isn't finished yet", silently dropping the LAST commit
194    // rather than reading it. Put the newline back before feeding it in.
195    //
196    // `-m`: without it, `diff-tree` shows NOTHING for a merge commit at all
197    // — confirmed empirically, not merely documented — so a file edited only
198    // to resolve a conflict (never touched by either parent individually) is
199    // invisible to a scope-gated check. `-m` diffs a merge against EACH
200    // parent and unions the results, which is exactly "did this commit,
201    // merge or not, touch this path" — the caller already de-duplicates the
202    // combined list, so the extra per-parent repeats cost nothing.
203    crate::git::stdout_piped_raw(
204        &[
205            "diff-tree",
206            "--no-commit-id",
207            "--name-only",
208            "-r",
209            "-m",
210            "-z",
211            "--stdin",
212        ],
213        &format!("{}\n", commits.trim_end()),
214    )
215    .map(|raw| crate::git::split_nul_paths(&raw))
216    .unwrap_or_default()
217}
218
219#[cfg(test)]
220mod tests {
221    use super::*;
222
223    #[test]
224    fn parses_the_four_fields() {
225        let got = parse(&b"refs/heads/x aaa refs/heads/y bbb\n"[..]);
226        assert_eq!(got.len(), 1);
227        assert_eq!(got[0].local_ref, "refs/heads/x");
228        assert_eq!(got[0].remote_ref, "refs/heads/y");
229        assert_eq!(got[0].remote_oid, "bbb");
230    }
231
232    #[test]
233    fn several_refs_and_junk_lines() {
234        let got = parse(&b"a 1 b 2\ngarbage\n\nc 3 d 4\n"[..]);
235        assert_eq!(got.len(), 2, "short lines are skipped, not fatal");
236        assert_eq!(got[1].local_ref, "c");
237    }
238
239    #[test]
240    fn empty_stdin_is_no_refs_not_an_error() {
241        assert!(parse(&b""[..]).is_empty());
242    }
243
244    /// Mirrors `git::a_non_ascii_path_is_not_reinterpreted_as_its_quoted_form`,
245    /// because this module used to parse `diff-tree` output ITSELF, by lines,
246    /// with no `-z`.
247    ///
248    /// Under bare `--name-only`, git prints `é.ts` as the nine-byte literal
249    /// `"\303\251.ts"` — backslashes, digits and quotes standing in for two
250    /// UTF-8 bytes. That string ends with neither `.ts` nor `.rs`, so `is_js`,
251    /// `is_rust_path` and `Scope::matches` all said "not mine" and the
252    /// scope-gated pre-push check silently never ran on the one commit that
253    /// changed the file. Feeding the same real bytes through `-z` output must
254    /// hand the path back untouched.
255    #[test]
256    fn a_non_ascii_path_survives_the_diff_tree_parse() {
257        let mut raw = "src/é.ts".as_bytes().to_vec();
258        raw.push(0);
259        raw.extend_from_slice(b"src/plain.ts\0");
260        let got = crate::git::split_nul_paths(&raw);
261        assert_eq!(
262            got,
263            vec!["src/é.ts".to_string(), "src/plain.ts".to_string()]
264        );
265        assert!(
266            got[0].ends_with(".ts"),
267            "the quoted form ends with a quote, not an extension, and is why \
268             a scope-gated check never fired: {:?}",
269            got[0]
270        );
271    }
272}