Skip to main content

amont_runtime/
pushed_tree.rs

1//! Run a pre-push check against what is being PUSHED.
2//!
3//! `rust-test` and `run-tests-js` take their file set from the pushed refs —
4//! correct — and then run the suite with `current_dir` set to the developer's
5//! working tree. So the suite can pass on an uncommitted fix, or fail on an
6//! uncommitted experiment, and in neither case has it tested the commits being
7//! pushed. Same gap as the pre-commit one, from the other end.
8//!
9//! ## Why not the stash
10//!
11//! Holding unstaged changes aside is the pre-commit answer, and it is the wrong
12//! instrument here. A push is not a staging operation: the difference that
13//! matters is not tree-versus-index but tree-versus-the-commit-you-are-sending,
14//! and that includes everything staged-but-uncommitted too. Setting all of it
15//! aside for the length of a test suite would leave the developer looking at a
16//! tree that is not theirs for minutes at a time.
17//!
18//! ## What it costs, which is the whole question
19//!
20//! `git worktree add --detach <tip>` materialises the pushed commit somewhere
21//! else and the suite runs there. The tree is untouched, and an interrupted run
22//! leaves a worktree rather than a mangled checkout.
23//!
24//! The cost is real: a second checkout, and a build that cannot reuse the
25//! primary tree's `target/` cache, so the first push after this lands is a cold
26//! build. That is why it is opt-in — `git config amont.testPushedTree true`
27//! — rather than the default. The default keeps today's behaviour and now SAYS
28//! what it is testing, which was the actual bug: not that it used the tree, but
29//! that nobody knew it did.
30
31use std::path::{Path, PathBuf};
32
33use crate::ui::warning_sign;
34
35/// Whether the user asked for the accurate-but-slower answer.
36///
37/// Read through [`crate::config`], so `on`, `yes` and every capitalisation work
38/// exactly as git-config(1) says they do — and a value git cannot parse takes
39/// the default while SAYING so, rather than reading as a quiet "no".
40pub fn enabled() -> bool {
41    crate::config::boolean_or("amont.testPushedTree", false)
42}
43
44/// A command to run inside a fresh snapshot before any suite does.
45///
46/// A checkout is not a workspace: a pnpm monorepo has no `node_modules` in
47/// a worktree git just created, and a suite started there fails on
48/// `Cannot find module` having tested nothing. What makes it a workspace is
49/// per-repository knowledge — `pnpm install --offline --frozen-lockfile`,
50/// `npm ci`, nothing at all for a Rust crate — so it is a git-config value,
51/// set once per clone by the person who knows:
52///
53/// ```sh
54/// git config amont.snapshotPrepare "pnpm install --offline --frozen-lockfile"
55/// ```
56///
57/// Runs through the shell, in the snapshot, before the gate, and a failure
58/// is the snapshot's failure: no suite runs on a tree that was never made
59/// runnable. Used by both snapshot consumers — `amont.testPushedTree` at
60/// push time and the background rehearsal — so the two cannot drift.
61const PREPARE: &str = "amont.snapshotPrepare";
62
63pub fn prepare_command() -> Option<String> {
64    crate::config::string_value(PREPARE).filter(|s| !s.trim().is_empty())
65}
66
67/// A checkout of the pushed commit, removed when it goes out of scope.
68pub struct PushedTree {
69    path: PathBuf,
70    repo: PathBuf,
71}
72
73impl PushedTree {
74    /// Materialise `tip`, or `None` when that is not possible — in which case
75    /// the caller falls back to the working tree and says so.
76    /// Takes the repository explicitly rather than relying on the working
77    /// directory: `set_current_dir` is process-global, so a test that changed
78    /// it would race every other test in the binary.
79    pub fn create(repo: &Path, tip: &str) -> Option<PushedTree> {
80        let base = std::env::temp_dir().join(unique_name("amont-push"));
81        Self::create_at(base, repo, tip)
82    }
83
84    /// The actual work, over an explicit path — split out so a test can hand
85    /// it a path it controls, since `create`'s own path is unpredictable BY
86    /// DESIGN and cannot be aimed at a fixture.
87    ///
88    /// `create_dir`, not a `remove_dir_all` before creating: that deleted
89    /// whatever was already at this path BEFORE this process had established
90    /// it owned it — an unpredictable name makes landing on an existing path
91    /// unlikely, not impossible, and "unlikely" is not the bar for a delete.
92    /// `create_dir` is exclusive: it fails loudly on anything already there
93    /// instead of removing it, and git's own `worktree add` is content to
94    /// receive a directory that already exists as long as it is empty —
95    /// which this one, having just been created, provably is.
96    fn create_at(base: PathBuf, repo: &Path, tip: &str) -> Option<PushedTree> {
97        std::fs::create_dir(&base).ok()?;
98        let ok = crate::git::succeeds(&[
99            "-C",
100            repo.to_str()?,
101            "worktree",
102            "add",
103            "--detach",
104            "--quiet",
105            base.to_str()?,
106            tip,
107        ]);
108        if !ok {
109            // Ours to clean up: we created it moments ago, so nothing else
110            // could have raced in ahead of us to make this delete unsafe.
111            let _ = std::fs::remove_dir_all(&base);
112            return None;
113        }
114        let tree = PushedTree {
115            path: base,
116            repo: repo.to_path_buf(),
117        };
118        if !tree.prepare() {
119            // `Drop` removes the worktree; the caller hears `None` and says
120            // what it is doing instead.
121            return None;
122        }
123        Some(tree)
124    }
125
126    /// Run `amont.snapshotPrepare`, if set, inside the checkout. True when
127    /// there was nothing to run or it exited 0.
128    fn prepare(&self) -> bool {
129        let Some(script) = prepare_command() else {
130            return true;
131        };
132        println!("preparing the snapshot: {script}");
133        #[cfg(unix)]
134        let mut cmd = {
135            let mut c = std::process::Command::new("sh");
136            c.arg("-c").arg(&script);
137            c
138        };
139        #[cfg(not(unix))]
140        let mut cmd = {
141            let mut c = std::process::Command::new("cmd");
142            c.arg("/C").arg(&script);
143            c
144        };
145        cmd.current_dir(&self.path)
146            .stdin(std::process::Stdio::null());
147        crate::hooks::common::strip_git_env(&mut cmd);
148        let ok = crate::hooks::common::bounded_success(&mut cmd, PREPARE);
149        if !ok {
150            println!(
151                "{} {PREPARE} failed in the snapshot — nothing can be tested there",
152                warning_sign()
153            );
154        }
155        ok
156    }
157
158    pub fn path(&self) -> &Path {
159        &self.path
160    }
161}
162
163/// `<prefix>-<pid>-<hash>`: the pid stays for a human correlating a leftover
164/// directory with a hung process, but the pid ALONE made the name guessable —
165/// `ps` hands it to anyone on the box — and a pre-planted directory or
166/// symlink at a predictable path in a world-writable `/tmp` is somebody
167/// else's decision about where our checkout goes.
168///
169/// It is no longer the difference between safe and destructive: `create_at`
170/// takes the path with an exclusive `create_dir` and refuses anything already
171/// there, rather than the `remove_dir_all`-first shape this comment used to
172/// describe. Unpredictability is now defence in depth — it means the refusal
173/// is not something an attacker can arrange on demand.
174///
175/// No dependency for real randomness here (this crate ships dependency-free),
176/// so the tail is a hash of the wall clock and the pid through
177/// `RandomState`'s own OS-seeded key — enough that guessing it in advance is
178/// impractical, not a cryptographic promise.
179fn unique_name(prefix: &str) -> String {
180    use std::collections::hash_map::RandomState;
181    use std::hash::{BuildHasher, Hash, Hasher};
182
183    let pid = std::process::id();
184    let mut hasher = RandomState::new().build_hasher();
185    pid.hash(&mut hasher);
186    std::time::SystemTime::now()
187        .duration_since(std::time::UNIX_EPOCH)
188        .unwrap_or_default()
189        .as_nanos()
190        .hash(&mut hasher);
191    format!("{prefix}-{pid}-{:016x}", hasher.finish())
192}
193
194impl Drop for PushedTree {
195    fn drop(&mut self) {
196        // `--force`: the suite may have written into it, and a build artefact
197        // must not be a reason to leave a worktree registered forever.
198        let _ = crate::git::succeeds(&[
199            "-C",
200            self.repo.to_str().unwrap_or_default(),
201            "worktree",
202            "remove",
203            "--force",
204            self.path.to_str().unwrap_or_default(),
205        ]);
206        let _ = std::fs::remove_dir_all(&self.path);
207    }
208}
209
210/// Tips whose snapshot was ASKED FOR and could not be made, so their suite
211/// ran against the working tree after all.
212///
213/// Read by `dispatch::stamp_tips`, which otherwise decides a tip is vouchable
214/// from `enabled()` — the CONFIG — and would stamp a tip whose content was
215/// never tested. See [`fell_back`].
216///
217/// A `Mutex` because pre-push checks run concurrently and each loops over its
218/// own refs; a poisoned lock is treated as "it fell back", which costs a
219/// redundant gate run and never a false stamp.
220static FELL_BACK: std::sync::OnceLock<std::sync::Mutex<std::collections::HashSet<String>>> =
221    std::sync::OnceLock::new();
222
223fn fallbacks() -> &'static std::sync::Mutex<std::collections::HashSet<String>> {
224    FELL_BACK.get_or_init(|| std::sync::Mutex::new(std::collections::HashSet::new()))
225}
226
227fn note_fallback(tip: &str) {
228    if let Ok(mut set) = fallbacks().lock() {
229        set.insert(tip.to_string());
230    }
231}
232
233/// Did the snapshot for `tip` fail, leaving its suite on the working tree?
234///
235/// `true` also when the lock is poisoned: a stamp is a claim that the gate
236/// ran on exactly this content, and a claim we cannot check is one we do not
237/// make.
238pub fn fell_back(tip: &str) -> bool {
239    match fallbacks().lock() {
240        Ok(set) => set.contains(tip),
241        Err(_) => true,
242    }
243}
244
245/// Where a pre-push suite should run, and whether that is the honest answer.
246///
247/// Takes ONE commit, not the whole ref list: a push can carry several refs
248/// (`git push origin a b`), each with its own tip, and a caller that checked
249/// out only the first one and ran every ref's tests against it would test
250/// the second ref's code against the first ref's tree. Callers loop over
251/// their own refs and pass each one's tip in turn.
252///
253/// Returns the directory plus the guard that owns it — dropping the guard
254/// removes the worktree, so the caller must hold it for the length of the run.
255pub fn where_to_run(tip: &str, fallback: &str) -> (PathBuf, Option<PushedTree>) {
256    // Inside a rehearsal the working tree IS a snapshot of the tip: a
257    // second checkout would test the same content twice, and the warning
258    // below would be false.
259    if crate::rehearsal::in_snapshot() {
260        return (PathBuf::from(fallback), None);
261    }
262    if !enabled() {
263        // Today's behaviour, but no longer silent about it. NOT a fallback:
264        // nobody asked for a snapshot, and `stamp_tips` already knows to
265        // vouch for the working tree only when it IS the tip.
266        println!(
267            "{} testing the WORKING TREE, not the pushed commits \
268             (`git config amont.testPushedTree true` to test what you are pushing)",
269            warning_sign()
270        );
271        return (PathBuf::from(fallback), None);
272    }
273    match PushedTree::create(Path::new(fallback), tip) {
274        Some(tree) => {
275            let path = tree.path().to_path_buf();
276            (path, Some(tree))
277        }
278        None => {
279            // Recorded, not just printed. `stamp_tips` used to read
280            // `enabled()` and conclude "the suite ran on the tip itself",
281            // which is exactly what did NOT happen here: the gate passed on
282            // a possibly-dirty working tree and the tip was stamped anyway,
283            // so the next push of that tree — a retry after a dropped
284            // connection, or a tag on the merged commit — skipped a gate
285            // nobody had run on its content. `gate_stamp`'s own promise is
286            // that no path here can let an unchecked commit through.
287            note_fallback(tip);
288            println!(
289                "{} could not check out {tip} to test it; testing the working tree instead \
290                 (nothing will be stamped for it)",
291                warning_sign()
292            );
293            (PathBuf::from(fallback), None)
294        }
295    }
296}
297
298#[cfg(test)]
299mod tests {
300    use super::*;
301
302    /// The whole point: two calls must not name the same path, or a
303    /// predictable name is right back to being predictable.
304    #[test]
305    fn unique_name_does_not_repeat() {
306        let a = unique_name("amont-push");
307        let b = unique_name("amont-push");
308        assert_ne!(a, b);
309        assert!(a.starts_with("amont-push-"));
310    }
311
312    /// The point of the whole fix: something already at the target path is
313    /// left ALONE, not deleted to make way. `create_at` fails closed
314    /// (`None`) instead of the old `remove_dir_all`-first shape, which would
315    /// have destroyed `sentinel.txt` here to clear the path for a worktree
316    /// that was never going to use it anyway (the repo below is fake).
317    #[test]
318    fn an_existing_path_is_left_alone_not_cleared() {
319        let base = std::env::temp_dir().join(format!("pushed-collision-{}", std::process::id()));
320        let _ = std::fs::remove_dir_all(&base);
321        std::fs::create_dir_all(&base).unwrap();
322        std::fs::write(base.join("sentinel.txt"), "do not delete me").unwrap();
323
324        let got = PushedTree::create_at(base.clone(), Path::new("/does/not/matter"), "HEAD");
325        assert!(
326            got.is_none(),
327            "must refuse rather than reuse a path it did not create"
328        );
329        assert_eq!(
330            std::fs::read_to_string(base.join("sentinel.txt")).unwrap(),
331            "do not delete me",
332            "an existing path must never be cleared to make room"
333        );
334        let _ = std::fs::remove_dir_all(&base);
335    }
336
337    fn repo(name: &str) -> PathBuf {
338        let d = std::env::temp_dir().join(format!("pushed-{name}-{}", std::process::id()));
339        let _ = std::fs::remove_dir_all(&d);
340        std::fs::create_dir_all(&d).unwrap();
341        for args in [
342            vec!["init", "-q", "--template=", "."],
343            vec!["config", "user.email", "t@t.test"],
344            vec!["config", "user.name", "t"],
345            // Git for Windows rewrites line endings on checkout; a byte
346            // comparison would otherwise assert git's newline policy.
347            vec!["config", "core.autocrlf", "false"],
348        ] {
349            std::process::Command::new("git")
350                .args(&args)
351                .current_dir(&d)
352                .output()
353                .expect("git");
354        }
355        d
356    }
357
358    /// The point: the checkout holds the COMMIT, not whatever the developer
359    /// has open.
360    #[test]
361    fn the_worktree_holds_the_committed_content() {
362        let d = repo("tree");
363        let git = |args: &[&str]| {
364            std::process::Command::new("git")
365                .args(args)
366                .current_dir(&d)
367                .output()
368                .expect("git")
369        };
370        std::fs::write(d.join("a.txt"), "committed\n").unwrap();
371        git(&["add", "-A"]);
372        git(&["commit", "-qm", "seed"]);
373        let head = String::from_utf8_lossy(&git(&["rev-parse", "HEAD"]).stdout)
374            .trim()
375            .to_string();
376        // Uncommitted, and it must not travel.
377        std::fs::write(d.join("a.txt"), "dirty, not pushed\n").unwrap();
378
379        let tree = PushedTree::create(&d, &head).expect("worktree");
380        let seen = std::fs::read_to_string(tree.path().join("a.txt")).unwrap();
381        let at = tree.path().to_path_buf();
382        drop(tree);
383
384        assert_eq!(seen, "committed\n", "the worktree saw the dirty tree");
385        assert!(!at.exists(), "the worktree outlived its guard");
386        let _ = std::fs::remove_dir_all(&d);
387    }
388}