Skip to main content

amont_runtime/hooks/
common.rs

1//! Shared plumbing for the linter-orchestration hooks.
2//!
3//! Nine of them do the same four things: collect staged files of some kind,
4//! bail out if there are none, resolve a tool, run it. In shell that was ~65
5//! lines apiece, mostly duplicated; here it is a handful of helpers and each
6//! hook keeps only what is actually specific to it.
7
8use crate::git;
9use crate::ui::{error_sign, valid_sign, warning_sign};
10use std::path::Path;
11use std::process::{Command, Stdio};
12use std::sync::OnceLock;
13
14/// Staged files, deletions excluded, whose name ends with one of `exts`.
15/// The file set every check asks about, when it is not the staged one.
16///
17/// Set at most once, before any check runs, by `amont run --all-files`. A
18/// process-level override rather than a parameter because a check's signature
19/// is `(&[OsString])` — it never sees a `Ctx` — and threading a file set
20/// through twenty of them to serve one mode would be a worse trade than a
21/// value that is written once and read many times.
22///
23/// Same shape as `PushRefs`: read once, lent to every check that asks.
24static OVERRIDE: OnceLock<Vec<String>> = OnceLock::new();
25
26/// Set once the file set stops being the index.
27///
28/// `restage`'s own doc says what makes re-staging safe: the pre-commit stage
29/// holds the unstaged changes aside, so the tree contains the staged content
30/// and nothing else, and anything a formatter touched is by definition part of
31/// this commit. `amont run --all-files` replaces the file set with every
32/// tracked path — which is that precondition being FALSE.
33///
34/// With `amont.fix true`, every fixer's `restage(&files)` would then `git
35/// add` everything in the working tree that differs from the index, turning a
36/// read-only "does my tree pass" query into `git add .`. That is the hazard §2
37/// of docs/index-fidelity-and-run-modes.md names.
38///
39/// The gate hangs off the OVERRIDE rather than off a flag threaded through
40/// twenty check signatures, because the override IS the fact that matters. It
41/// therefore covers built-ins and `manifest::External::run` (which consults
42/// `fixing_enabled` in two places) in one change, and a future check cannot
43/// forget it.
44static NOT_THE_INDEX: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
45
46/// Make every subsequent `staged_files` answer from `files` instead of the
47/// index. Only the first call counts.
48pub fn override_file_set(files: Vec<String>) {
49    // Set unconditionally, even if a set already won the `OnceLock`: the
50    // statement "the file set is not the index" is true from the first call
51    // onwards regardless of which one supplied the paths.
52    NOT_THE_INDEX.store(true, std::sync::atomic::Ordering::SeqCst);
53    let _ = OVERRIDE.set(files);
54}
55
56/// Whether the file set every check sees is something other than the index.
57pub fn not_the_index() -> bool {
58    NOT_THE_INDEX.load(std::sync::atomic::Ordering::SeqCst)
59}
60
61/// An empty `exts` returns them all.
62///
63/// The UNFILTERED list is read from git ONCE per process and lent to every
64/// caller — the per-stage snapshot. Eleven of the pre-commit checks ask this
65/// question, concurrently, and each used to pay its own `git diff` spawn for
66/// an answer that cannot change while the stage runs: the index-fidelity
67/// hold pins the tree, and a fixer's `restage()` re-adds only paths already
68/// on this list. `PushRefs` ("read once and lent") and `Overrides` ("ONE
69/// subprocess for the whole stage") are the same pattern; this was the last
70/// hot question still answered per asker.
71pub fn staged_files(exts: &[&str]) -> Vec<String> {
72    if let Some(all) = OVERRIDE.get() {
73        return all
74            .iter()
75            .filter(|f| exts.is_empty() || exts.iter().any(|e| f.ends_with(e)))
76            .cloned()
77            .collect();
78    }
79    static INDEX: OnceLock<Vec<String>> = OnceLock::new();
80    INDEX
81        .get_or_init(|| {
82            match git::stdout_paths(&["diff", "--diff-filter=d", "--cached", "--name-only"]) {
83                Some(files) => files,
84                // The third member of a bug family (`repo_hooks`, the push
85                // gates): git FAILING is not git answering "empty", and a
86                // stage that judges an empty set on a git failure reports
87                // clean having verified nothing. Say so — once, this cache
88                // being the once — and still fail open: pre-commit's job is
89                // never to block a commit over its own plumbing.
90                None => {
91                    warn(
92                        "git would not list the staged files — the checks are judging \
93                         an EMPTY set, not a verified one",
94                    );
95                    Vec::new()
96                }
97            }
98        })
99        .iter()
100        .filter(|f| exts.is_empty() || exts.iter().any(|e| f.ends_with(e)))
101        .cloned()
102        .collect()
103}
104
105/// Repo root, or "." when git cannot say.
106///
107/// **For CHECK BODIES ONLY.** The fallback is safe there and nowhere else: git
108/// invokes a hook with the working tree as the current directory, so a check
109/// that reaches this line is already standing in the repository, and "." is the
110/// right answer rather than a guess.
111///
112/// Anything a user types — `amont agents-md`, `install`, `trust`, `restore`
113/// — can be typed from any directory on the machine, and there the fallback is
114/// not a fallback but a wrong answer that reads as a right one. Use
115/// [`repo_root_checked`] at every command entry point.
116pub fn repo_root() -> String {
117    // Cached: the answer is a property of the process's repository, and
118    // every check asked it through its own subprocess.
119    static ROOT: OnceLock<String> = OnceLock::new();
120    ROOT.get_or_init(|| {
121        git::stdout(&["rev-parse", "--show-toplevel"]).unwrap_or_else(|| ".".into())
122    })
123    .clone()
124}
125
126/// Repo root, or an error naming the problem.
127///
128/// The same question as [`repo_root`] without the "." — because "." is a
129/// PLAUSIBLE root, and that is what made it dangerous. `amont agents-md`
130/// run outside a repository did not fail; it resolved the root to the current
131/// directory and wrote `./AGENTS.md` into whatever directory the user happened
132/// to be standing in, then printed `wrote ./AGENTS.md` as if that were the
133/// answer. Same shape in `install`'s two prompts, in `trust` (which then
134/// looked for a manifest, and would have recorded trust, under `.`) and in
135/// `restore`.
136///
137/// Every one of those is a command somebody types, and a command somebody
138/// types is a command they can type from `~`. There is no correct behaviour
139/// available to this function when git cannot answer, so it does not invent
140/// one.
141pub fn repo_root_checked() -> Result<String, String> {
142    git::stdout(&["rev-parse", "--show-toplevel"])
143        .filter(|s| !s.is_empty())
144        .ok_or_else(|| "not inside a git repository".to_string())
145}
146
147/// Resolve a tool, preferring the repo's PINNED copy so the hook matches CI.
148///
149///
150/// Order: `<root>/node_modules/.bin/<tool>`, then the MAIN worktree's (a linked
151/// worktree has no node_modules of its own — this is why the shell version
152/// consulted the git common dir), then PATH.
153pub fn resolve_tool(root: &str, tool: &str) -> Option<Vec<String>> {
154    // Same extension problem as `which`: an npm-installed binary is `eslint.cmd`
155    // on Windows, so the bare name misses the repo's PINNED copy and the hook
156    // silently falls through to an ambient one.
157    if let Some(p) = in_bin_dir(&format!("{root}/node_modules/.bin"), tool) {
158        return Some(vec![p]);
159    }
160    if let Some(common) = git::stdout(&["rev-parse", "--path-format=absolute", "--git-common-dir"])
161    {
162        if let Some(main) = Path::new(&common).parent() {
163            if let Some(p) = in_bin_dir(&main.join("node_modules/.bin").to_string_lossy(), tool) {
164                return Some(vec![p]);
165            }
166        }
167    }
168    if let Some(full) = which(tool) {
169        return Some(vec![full]);
170    }
171    // `npx --no-install`: never silently download a random latest version — a
172    // hook that quietly pulls a different linter than CI uses is worse than one
173    // that skips.
174    if which("npx").is_some()
175        && Command::new(program("npx"))
176            .args(["--no-install", tool, "--version"])
177            .current_dir(root)
178            .stdin(Stdio::null())
179            .stdout(Stdio::null())
180            .stderr(Stdio::null())
181            .status()
182            .map(|s| s.success())
183            .unwrap_or(false)
184    {
185        return Some(vec![
186            program("npx"),
187            "--no-install".to_string(),
188            tool.to_string(),
189        ]);
190    }
191    None
192}
193
194/// First match for `tool` on PATH.
195///
196/// Windows executables carry an extension — `git` is `git.exe`, an npm-installed
197/// `eslint` is `eslint.cmd` — so the bare name finds nothing there. PATHEXT is
198/// the OS's own list of what counts as executable; fall back to the usual set
199/// when it is unset. Found by the Windows CI job on its first run, where
200/// `which("git")` returned None on a machine that plainly has git.
201pub fn which(tool: &str) -> Option<String> {
202    let path = std::env::var_os("PATH")?;
203    let exts: Vec<String> = if cfg!(windows) {
204        std::env::var("PATHEXT")
205            .unwrap_or_else(|_| ".COM;.EXE;.BAT;.CMD".into())
206            .split(';')
207            .filter(|e| !e.is_empty())
208            .map(|e| e.to_lowercase())
209            .collect()
210    } else {
211        Vec::new()
212    };
213    for dir in std::env::split_paths(&path) {
214        // On Windows the EXTENSION forms come first. A node install ships both
215        // `npm` (an extensionless shell script, for MSYS) and `npm.cmd` in the
216        // same directory; preferring the bare name hands CreateProcess a shell
217        // script it cannot execute — "%1 is not a valid Win32 application" —
218        // and the hook reports an installed tool as broken.
219        for e in &exts {
220            let c = dir.join(format!("{tool}{e}"));
221            if c.is_file() {
222                return Some(c.to_string_lossy().into_owned());
223            }
224        }
225        let bare = dir.join(tool);
226        if bare.is_file() {
227            return Some(bare.to_string_lossy().into_owned());
228        }
229    }
230    None
231}
232
233/// `<dir>/<tool>`, trying the Windows executable extensions too.
234fn in_bin_dir(dir: &str, tool: &str) -> Option<String> {
235    let bare = Path::new(dir).join(tool);
236    if bare.is_file() {
237        return Some(bare.to_string_lossy().into_owned());
238    }
239    if cfg!(windows) {
240        for e in [".cmd", ".exe", ".bat", ".ps1"] {
241            let c = Path::new(dir).join(format!("{tool}{e}"));
242            if c.is_file() {
243                return Some(c.to_string_lossy().into_owned());
244            }
245        }
246    }
247    None
248}
249
250/// Resolve a tool name to a full path for spawning.
251///
252/// `Command::new("npm")` cannot execute `npm.cmd`: Rust does no PATHEXT
253/// resolution, so on Windows every bare-name spawn fails with "program not
254/// found" and the hook reports the tool as broken rather than absent. Found by
255/// the Windows job on its first FULL-suite run — the smoke never spawned a
256/// tool, so it could not have surfaced this.
257///
258/// Falls back to the name unchanged, so a caller still gets a sensible error.
259pub fn program(name: &str) -> String {
260    which(name).unwrap_or_else(|| name.to_string())
261}
262
263/// The first of `names` that exists at the repo root — how these hooks decide
264/// a repo has opted into a tool.
265pub fn first_existing(root: &str, names: &[&str]) -> Option<String> {
266    names
267        .iter()
268        .find(|n| Path::new(root).join(n).exists())
269        .map(|n| (*n).to_string())
270}
271
272/// Strip git's own environment before handing a Command to another tool.
273///
274/// git exports GIT_DIR, GIT_INDEX_FILE, GIT_WORK_TREE and friends to every
275/// hook. Those OVERRIDE the working directory, so any tool that shells out to
276/// git operates on the hook's repository no matter where it was launched.
277///
278/// That is not hypothetical: `pre-push-cargo-test` runs a project's test suite,
279/// and this repo's own suite creates throwaway repos and commits to them. With
280/// GIT_DIR inherited, `git commit` in a test wrote into the REAL repository —
281/// an actual stray commit, authored by the test fixture, pushed to a branch.
282///
283/// A test suite should behave exactly as it does when run by hand, which means
284/// seeing no git environment at all.
285pub fn strip_git_env(cmd: &mut Command) {
286    for (k, _) in std::env::vars_os() {
287        let key = k.to_string_lossy();
288        if key.starts_with("GIT_") {
289            cmd.env_remove(&k);
290        }
291    }
292}
293
294/// The wall-clock budget for one check's spawned command, in seconds.
295///
296/// `amont.timeout`, default 600 — ten minutes, the figure the generated
297/// agent guidance already tells tooling to allow a whole commit or push; a
298/// single check that outlives it is not slow, it is stuck. `0` disables.
299/// Read once per process: twenty concurrent checks must not each spawn a
300/// `git config` to learn the same number.
301pub fn check_timeout() -> u64 {
302    static TIMEOUT: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
303    *TIMEOUT.get_or_init(|| crate::config::integer_or("amont.timeout", 600, 0..=86_400) as u64)
304}
305
306/// What became of a command run under the deadline.
307pub enum Ran {
308    Status(std::process::ExitStatus),
309    /// Killed at the deadline; carries the budget it exceeded, in seconds.
310    TimedOut(u64),
311}
312
313/// `cmd.status()`, bounded by [`check_timeout`].
314///
315/// Without a bound, one hung tool — a linter deadlocked on a lock file, a
316/// plugin doing network I/O — blocked the commit FOREVER, and it hung inside
317/// the index-fidelity hold: the user's unstaged changes parked in `$GIT_DIR`,
318/// their tree showing staged content only, for as long as they were willing
319/// to wait. The learned response to that is `--no-verify`, permanently —
320/// which disarms every check to escape one.
321///
322/// The kill reaches the direct child only. A grandchild that detached
323/// survives, orphaned — but the COMMIT is no longer hostage to it, which is
324/// the property that matters.
325pub fn status_within(cmd: &mut Command) -> std::io::Result<Ran> {
326    status_within_secs(cmd, check_timeout())
327}
328
329/// [`status_within`] with an explicit budget — the testable seam.
330pub fn status_within_secs(cmd: &mut Command, budget_secs: u64) -> std::io::Result<Ran> {
331    if budget_secs == 0 {
332        return cmd.status().map(Ran::Status);
333    }
334    let mut child = cmd.spawn()?;
335    wait_within(&mut child, budget_secs)
336}
337
338/// [`status_within`], with the child's stdout and stderr CAPTURED into the
339/// calling check's slot instead of inherited — the other half of one-check-
340/// one-block: a linter's twelve lines used to land on the shared terminal
341/// between two other checks' lines. Falls back to plain [`status_within`]
342/// when no slot is installed on this thread (`amont.progress false`, or a
343/// spawn outside a stage), which is byte-for-byte the old behaviour.
344///
345/// stdout and stderr merge in ARRIVAL order inside the block, which is what
346/// the terminal showed before. The readers are threads, not processes, and
347/// they are joined before the status is returned so a block can never grow
348/// after its check finished.
349pub fn status_streamed(cmd: &mut Command) -> std::io::Result<Ran> {
350    let Some((stage, idx)) = crate::live::current_sink() else {
351        return status_within(cmd);
352    };
353    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
354    if crate::live::watching() {
355        // The block lands on a real terminal but the tool sees a pipe and
356        // would strip its colors; the big three opt-in knobs put them back.
357        cmd.env("FORCE_COLOR", "1")
358            .env("CLICOLOR_FORCE", "1")
359            .env("CARGO_TERM_COLOR", "always");
360    }
361    let budget = check_timeout();
362    let mut child = cmd.spawn()?;
363    let mut readers = Vec::new();
364    for pipe in [
365        child
366            .stdout
367            .take()
368            .map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
369        child
370            .stderr
371            .take()
372            .map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
373    ]
374    .into_iter()
375    .flatten()
376    {
377        let stage = std::sync::Arc::clone(&stage);
378        readers.push(std::thread::spawn(move || {
379            let mut pipe = pipe;
380            let mut chunk = [0u8; 4096];
381            loop {
382                match std::io::Read::read(&mut pipe, &mut chunk) {
383                    Ok(0) | Err(_) => break,
384                    Ok(n) => stage.append_raw(idx, &chunk[..n]),
385                }
386            }
387        }));
388    }
389    let ran = wait_within(&mut child, budget)?;
390    for r in readers {
391        let _ = r.join();
392    }
393    Ok(ran)
394}
395
396/// Run to completion under the `amont.timeout` deadline with stdout and
397/// stderr CAPTURED into a string the caller can parse — what the audit
398/// checks need: their verdict lives in the tool's output, not its exit
399/// code alone. Arrival-ordered merge of both streams, like
400/// [`status_streamed`]'s blocks. `None` when the child cannot be spawned.
401pub fn capture_within(cmd: &mut Command) -> Option<(Ran, String)> {
402    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
403    let budget = check_timeout();
404    let mut child = cmd.spawn().ok()?;
405    let text = std::sync::Arc::new(std::sync::Mutex::new(String::new()));
406    let mut readers = Vec::new();
407    for pipe in [
408        child
409            .stdout
410            .take()
411            .map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
412        child
413            .stderr
414            .take()
415            .map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
416    ]
417    .into_iter()
418    .flatten()
419    {
420        let text = std::sync::Arc::clone(&text);
421        readers.push(std::thread::spawn(move || {
422            let mut pipe = pipe;
423            let mut chunk = [0u8; 4096];
424            loop {
425                match std::io::Read::read(&mut pipe, &mut chunk) {
426                    Ok(0) | Err(_) => break,
427                    Ok(n) => {
428                        let piece = String::from_utf8_lossy(&chunk[..n]).into_owned();
429                        text.lock()
430                            .unwrap_or_else(|p| p.into_inner())
431                            .push_str(&piece);
432                    }
433                }
434            }
435        }));
436    }
437    let ran = wait_within(&mut child, budget).ok()?;
438    for r in readers {
439        let _ = r.join();
440    }
441    let text = std::sync::Arc::try_unwrap(text)
442        .map(|m| m.into_inner().unwrap_or_else(|p| p.into_inner()))
443        .unwrap_or_default();
444    Some((ran, text))
445}
446
447/// The deadline loop over an already-spawned child — shared by the
448/// inherited and captured runners.
449fn wait_within(child: &mut std::process::Child, budget_secs: u64) -> std::io::Result<Ran> {
450    if budget_secs == 0 {
451        return child.wait().map(Ran::Status);
452    }
453    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(budget_secs);
454    loop {
455        if let Some(status) = child.try_wait()? {
456            return Ok(Ran::Status(status));
457        }
458        if std::time::Instant::now() >= deadline {
459            let _ = child.kill();
460            let _ = child.wait();
461            return Ok(Ran::TimedOut(budget_secs));
462        }
463        std::thread::sleep(std::time::Duration::from_millis(25));
464    }
465}
466
467/// Say a command was killed at the deadline, and how to change the deadline.
468pub fn say_timed_out(what: &str, budget_secs: u64) {
469    fail(&format!(
470        "{} timed out after {budget_secs}s — killed. {} raises the budget",
471        hl(what),
472        hl("git config amont.timeout <secs>")
473    ));
474}
475
476/// [`status_within`], collapsed to "did it exit 0" — the shape the one-shot
477/// tool spawns want. A timeout says so, names `what`, and reads as failure.
478pub fn bounded_success(cmd: &mut Command, what: &str) -> bool {
479    match status_streamed(cmd) {
480        Ok(Ran::Status(s)) => s.success(),
481        Ok(Ran::TimedOut(b)) => {
482            say_timed_out(what, b);
483            false
484        }
485        Err(_) => false,
486    }
487}
488
489/// Run `argv` from `root`, inheriting stdio. True when it exits 0.
490pub fn run(root: &str, argv: &[String], extra: &[String]) -> bool {
491    let Some((program, rest)) = argv.split_first() else {
492        return true;
493    };
494    let mut cmd = Command::new(program);
495    cmd.args(rest)
496        .args(extra)
497        .current_dir(root)
498        .stdin(Stdio::null());
499    strip_git_env(&mut cmd);
500    bounded_success(&mut cmd, program)
501}
502
503/// As [`run`], but with the tool's own output discarded.
504///
505/// For a pass whose only job is to decide something — prettier's `--check`,
506/// ruff's `--fix` sweep — where the offenders are printed once, by the pass
507/// that reports them, rather than twice.
508pub fn run_quiet(root: &str, argv: &[String], extra: &[String]) -> bool {
509    let Some((program, rest)) = argv.split_first() else {
510        return true;
511    };
512    let mut cmd = Command::new(program);
513    cmd.args(rest)
514        .args(extra)
515        .current_dir(root)
516        .stdin(Stdio::null())
517        .stdout(Stdio::null())
518        .stderr(Stdio::null());
519    strip_git_env(&mut cmd);
520    // Deliberately NOT the streamed runner: this helper's contract is that
521    // the output is discarded, and capture would resurrect it into the block.
522    match status_within(&mut cmd) {
523        Ok(Ran::Status(s)) => s.success(),
524        Ok(Ran::TimedOut(b)) => {
525            say_timed_out(program, b);
526            false
527        }
528        Err(_) => false,
529    }
530}
531
532/// Whether the user asked for checks to repair what they find.
533///
534/// OFF by default. `git config amont.fix true` turns it on, per repository,
535/// because a hook that edits your files without being asked is a larger
536/// surprise than one that complains — and because with index fidelity in place
537/// the repair lands in the commit you are making, which is a bigger claim to
538/// make on somebody's behalf than printing an error.
539pub fn fixing_enabled() -> bool {
540    // Never while the file set is not the index — see `NOT_THE_INDEX`.
541    !not_the_index() && fixing_requested()
542}
543
544/// What the CONFIG says, ignoring whether the current run may act on it.
545///
546/// Split out so `run_all` can tell the difference between "fixing is off" and
547/// "you asked for fixing and this mode will not do it", and say the second out
548/// loud instead of silently ignoring the key.
549pub fn fixing_requested() -> bool {
550    crate::config::boolean_or("amont.fix", false)
551}
552
553/// What a re-stage actually did. THREE answers, because the old `bool`
554/// conflated two of them and the conflation shipped unformatted code.
555///
556/// `prettier.rs` read `if run_quiet(write) && restage(&files) { … Fixed }`. When
557/// `git add` FAILED, `restage` returned `false` — indistinguishable from
558/// "nothing needed staging" — so control fell through to a second `--check`
559/// pass, which inspected the NOW-FORMATTED WORKING TREE, passed, printed
560/// "Prettier passed" and returned `Outcome::Passed`. The index still held the
561/// unformatted content, so the commit contained unformatted code and the hook
562/// said it had passed. `manifest.rs` had the same shape.
563#[derive(Debug, Clone, PartialEq, Eq)]
564pub enum Restaged {
565    /// No path differed from the index — nothing to do, and nothing wrong.
566    Nothing,
567    /// `git add` succeeded; the index now holds the repair.
568    Staged,
569    /// `git add` failed, carrying the paths it could not stage. The index
570    /// holds content the fixer has already replaced on disk, so this MUST be
571    /// loud at every call site — and naming the files is the difference
572    /// between a message somebody can act on and one they cannot.
573    Failed(Vec<String>),
574}
575
576/// Serialises this process's own `git add` calls.
577///
578/// pre-commit runs its checks concurrently (`dispatch.rs`), and up to three of
579/// them can re-stage. git takes `$GIT_DIR/index.lock` exclusively, so two
580/// concurrent `git add`s in the same repository make one of them fail — which,
581/// before `Restaged`, was silently read as "nothing moved". Holding this across
582/// the `git add` removes self-contention entirely; the retry below is only for
583/// OTHER processes.
584static INDEX_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
585
586/// Re-stage exactly the paths a fixer rewrote, and say what happened.
587///
588/// Safe ONLY because the pre-commit stage holds unstaged changes aside: the
589/// tree contains the staged content and nothing else, so anything a formatter
590/// touched is by definition part of this commit. Without that, re-staging would
591/// sweep in work the author deliberately kept back.
592pub fn restage(paths: &[String]) -> Restaged {
593    // Belt and braces alongside `fixing_enabled`: a future fixer that forgets
594    // the gate still cannot turn `amont run --all-files` into `git add .`.
595    if not_the_index() {
596        return Restaged::Nothing;
597    }
598    let changed: Vec<String> = paths
599        .iter()
600        .filter(|p| !git::succeeds(&["diff", "--quiet", "--", p]))
601        .cloned()
602        .collect();
603    if changed.is_empty() {
604        return Restaged::Nothing;
605    }
606    let mut args = vec!["add", "--"];
607    args.extend(changed.iter().map(String::as_str));
608
609    let _serialised = INDEX_LOCK.lock().unwrap_or_else(|e| e.into_inner());
610    // Another PROCESS can hold `index.lock` — a `git status` from an editor, a
611    // second hook in a linked worktree. Back off and retry rather than
612    // reporting a transient collision as a failed repair. `git add` of the same
613    // paths is idempotent: it records the paths' current worktree content, so
614    // running it twice records the same thing twice and cannot double-stage.
615    const BACKOFF_MS: [u64; 3] = [50, 150, 400];
616    if git::succeeds(&args) {
617        return Restaged::Staged;
618    }
619    for wait in BACKOFF_MS {
620        std::thread::sleep(std::time::Duration::from_millis(wait));
621        if git::succeeds(&args) {
622            return Restaged::Staged;
623        }
624    }
625    Restaged::Failed(changed)
626}
627
628pub fn ok(msg: &str) {
629    crate::live::say(&format!("{} {msg}", valid_sign()));
630}
631pub fn fail(msg: &str) {
632    crate::live::say(&format!("{} {msg}", error_sign()));
633}
634pub fn warn(msg: &str) {
635    crate::live::say(&format!("{} {msg}", warning_sign()));
636}
637/// A line with no sign of its own — what a check's direct `println!` becomes,
638/// so it lands in the check's block instead of interleaving. See `live::say`.
639pub fn say(msg: &str) {
640    crate::live::say(msg);
641}
642
643/// Orange, for the fragments these hooks highlight.
644pub fn hl(s: &str) -> String {
645    crate::ui::highlight(s)
646}
647
648#[cfg(test)]
649mod tests {
650
651    /// The deadline kills what outlives it and reports what finished.
652    #[cfg(unix)]
653    #[test]
654    fn the_deadline_kills_a_sleeper_and_spares_a_finisher() {
655        let started = std::time::Instant::now();
656        let mut slow = Command::new(program("sleep"));
657        slow.arg("300").stdin(Stdio::null());
658        match status_within_secs(&mut slow, 1) {
659            Ok(Ran::TimedOut(1)) => {}
660            other => panic!("expected TimedOut(1), got {:?}", other.map(|_| "ran")),
661        }
662        assert!(
663            started.elapsed() < std::time::Duration::from_secs(60),
664            "the kill did not happen at the deadline"
665        );
666
667        let mut quick = Command::new(program("true"));
668        quick.stdin(Stdio::null());
669        match status_within_secs(&mut quick, 60) {
670            Ok(Ran::Status(s)) => assert!(s.success()),
671            other => panic!("expected a clean exit, got {:?}", other.map(|_| "?")),
672        }
673    }
674
675    use super::*;
676
677    #[test]
678    fn which_finds_a_real_binary_and_not_a_fake_one() {
679        assert!(which("git").is_some());
680        assert!(which("definitely-not-a-real-binary-xyz").is_none());
681    }
682
683    /// On Windows a tool can exist BOTH as an extensionless shell script and as
684    /// a .cmd/.exe in the same directory; only the latter is executable by
685    /// CreateProcess, so the extension forms must win.
686    #[test]
687    #[cfg(windows)]
688    fn windows_prefers_an_executable_extension_over_a_bare_file() {
689        let dir = std::env::temp_dir().join("amont-which-order");
690        let _ = std::fs::create_dir_all(&dir);
691        std::fs::write(dir.join("faketool"), "#!/bin/sh\n").unwrap();
692        std::fs::write(dir.join("faketool.cmd"), "@echo off\n").unwrap();
693        let saved = std::env::var_os("PATH");
694        std::env::set_var("PATH", &dir);
695        let found = which("faketool").unwrap();
696        if let Some(p) = saved {
697            std::env::set_var("PATH", p);
698        }
699        assert!(found.ends_with(".cmd"), "got {found}");
700        let _ = std::fs::remove_dir_all(&dir);
701    }
702
703    /// "Nothing moved" and "`git add` FAILED" are different answers, and the
704    /// old `bool` gave the same one for both.
705    ///
706    /// That conflation is what shipped unformatted code: `prettier.rs` read
707    /// `if wrote && restage(&files)`, so a failed `git add` fell through to a
708    /// second `--check` against the now-formatted WORKING TREE, which passed —
709    /// while the INDEX still held the unformatted content the commit would
710    /// carry.
711    ///
712    /// An absolute path outside any repository is a `git add` git will always
713    /// refuse, which is the only way to reach the failing branch without
714    /// sabotaging a real index.
715    #[test]
716    fn restage_distinguishes_nothing_from_failure() {
717        let outside = std::env::temp_dir()
718            .join("amont-restage-outside-any-repo")
719            .to_string_lossy()
720            .into_owned();
721        assert_eq!(
722            restage(std::slice::from_ref(&outside)),
723            Restaged::Failed(vec![outside]),
724            "a `git add` git refuses must report Failed, never Nothing"
725        );
726        assert_eq!(
727            restage(&[]),
728            Restaged::Nothing,
729            "no paths is nothing to do, and nothing wrong"
730        );
731    }
732
733    /// No check may hand `Command` a bare program name.
734    ///
735    /// `Command::new` does NO PATHEXT resolution, so `Command::new("npm")`
736    /// cannot execute `npm.cmd` and `Command::new("uvx")` cannot execute
737    /// `uvx.exe`: the spawn fails with "program not found" and a
738    /// `Severity::Block` check reports an installed tool as broken. That is the
739    /// incident `program()` exists for, and it kept recurring — `yamllint` and
740    /// three sites in `python_tools` were still doing it, THREE OF THEM after
741    /// `which()` had already succeeded and discarded the answer.
742    ///
743    /// A source scan rather than a runtime assertion because the failure only
744    /// reproduces on Windows, and the whole point is to catch the next one on
745    /// every platform. Comment lines are skipped: `program()`'s own doc quotes
746    /// the offending call. The needle is assembled from two pieces so this
747    /// module — which the scan also reads — does not match itself.
748    #[test]
749    fn no_hook_spawns_a_bare_program_name() {
750        let needle = concat!("Command", "::new(");
751        let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/src/hooks");
752        let mut scanned = 0usize;
753        for entry in std::fs::read_dir(dir).expect("hooks dir").flatten() {
754            let path = entry.path();
755            if path.extension().and_then(|e| e.to_str()) != Some("rs") {
756                continue;
757            }
758            scanned += 1;
759            let src = std::fs::read_to_string(&path).expect("read a hook module");
760            for (n, line) in src.lines().enumerate() {
761                if line.trim_start().starts_with("//") {
762                    continue;
763                }
764                let Some(after) = line.split_once(needle) else {
765                    continue;
766                };
767                assert!(
768                    !after.1.starts_with('"'),
769                    "{}:{} spawns a bare name — route it through `program()` or \
770                     the path `which()` already resolved: {}",
771                    path.display(),
772                    n + 1,
773                    line.trim()
774                );
775            }
776        }
777        assert!(
778            scanned > 10,
779            "the scan found almost nothing: {scanned} files"
780        );
781    }
782
783    #[test]
784    fn first_existing_picks_the_earliest_present_name() {
785        let dir = std::env::temp_dir().join("amont-first-existing-test");
786        let _ = std::fs::create_dir_all(&dir);
787        let root = dir.to_string_lossy().into_owned();
788        let _ = std::fs::write(dir.join("second"), "x");
789        assert_eq!(
790            first_existing(&root, &["first", "second", "third"]).as_deref(),
791            Some("second")
792        );
793        assert_eq!(first_existing(&root, &["nope"]), None);
794        let _ = std::fs::remove_dir_all(&dir);
795    }
796}