Skip to main content

differential_engine/
gitio.rs

1//! Git subprocess runner. Bytes in, bytes out — UTF-8 decoding happens only at
2//! display boundaries (ADR 0002). Plumbing commands only (ADR 0011).
3
4use std::collections::HashMap;
5use std::ffi::OsStr;
6use std::path::{Path, PathBuf};
7use std::process::{Command, Stdio};
8
9use crate::EngineError;
10use crate::ports;
11
12#[derive(Debug, Clone)]
13pub struct Repo {
14    root: PathBuf,
15}
16
17impl Repo {
18    /// Open the repository containing `dir`.
19    pub fn open(dir: &Path) -> Result<Self, EngineError> {
20        let probe = Repo {
21            root: dir.to_path_buf(),
22        };
23        let out = probe.run(["rev-parse", "--show-toplevel"], None)?;
24        let root = PathBuf::from(String::from_utf8_lossy(trim_newline(&out)).into_owned());
25        Ok(Repo { root })
26    }
27
28    pub fn root(&self) -> &Path {
29        &self.root
30    }
31
32    /// Run a git command with raw byte i/o. Non-zero exit is an error carrying
33    /// the command line and stderr.
34    ///
35    /// PRIVATE, and the point of the ports (ADR 0020): domain code cannot spell
36    /// an arbitrary git command, so each consumer's bounds are an honest
37    /// statement of what it touches.
38    fn run<I, S>(&self, args: I, stdin: Option<&[u8]>) -> Result<Vec<u8>, EngineError>
39    where
40        I: IntoIterator<Item = S>,
41        S: AsRef<OsStr>,
42    {
43        self.run_env(args, stdin, &[])
44    }
45
46    /// Like `run`, with extra environment variables (e.g. GIT_INDEX_FILE).
47    fn run_env<I, S>(
48        &self,
49        args: I,
50        stdin: Option<&[u8]>,
51        env: &[(&str, &OsStr)],
52    ) -> Result<Vec<u8>, EngineError>
53    where
54        I: IntoIterator<Item = S>,
55        S: AsRef<OsStr>,
56    {
57        let mut cmd = Command::new("git");
58        // Belt and braces on top of plumbing: no color, no external diff drivers,
59        // no path quoting surprises.
60        cmd.arg("-c")
61            .arg("core.quotepath=false")
62            .args(args)
63            .current_dir(&self.root)
64            .stdin(if stdin.is_some() {
65                Stdio::piped()
66            } else {
67                Stdio::null()
68            })
69            .stdout(Stdio::piped())
70            .stderr(Stdio::piped());
71        for (k, v) in env {
72            cmd.env(k, v);
73        }
74
75        let mut child = cmd
76            .spawn()
77            .map_err(|e| EngineError::GitSpawn { source: e })?;
78        if let Some(data) = stdin {
79            use std::io::Write;
80            let mut pipe = child.stdin.take().expect("stdin was requested");
81            pipe.write_all(data)
82                .map_err(|e| EngineError::GitSpawn { source: e })?;
83            // pipe drops here, closing stdin
84        }
85        let out = child
86            .wait_with_output()
87            .map_err(|e| EngineError::GitSpawn { source: e })?;
88
89        if !out.status.success() {
90            return Err(EngineError::GitCommand {
91                command: describe(&cmd),
92                code: out.status.code(),
93                stderr: String::from_utf8_lossy(&out.stderr[..out.stderr.len().min(800)])
94                    .into_owned(),
95            });
96        }
97        Ok(out.stdout)
98    }
99
100    /// Run a git command for its exit status, which the caller treats as the
101    /// answer rather than as success or failure.
102    ///
103    /// Separate from `run` rather than loosening it: `run` turning a non-zero
104    /// exit into an error is what makes every other call site safe by default.
105    fn run_status<I, S>(&self, args: I) -> Result<std::process::ExitStatus, EngineError>
106    where
107        I: IntoIterator<Item = S>,
108        S: AsRef<OsStr>,
109    {
110        Command::new("git")
111            .arg("-c")
112            .arg("core.quotepath=false")
113            .args(args)
114            .current_dir(&self.root)
115            .stdin(Stdio::null())
116            .stdout(Stdio::null())
117            .stderr(Stdio::null())
118            .status()
119            .map_err(|e| EngineError::GitSpawn { source: e })
120    }
121
122    /// Blob content at `rev:path`. `Ok(None)` when the path does not exist at
123    /// that revision; any other failure is a real error.
124    fn blob(&self, rev: &str, path: &[u8]) -> Result<Option<Vec<u8>>, EngineError> {
125        Ok(self.blobs(&[(rev, path)])?.pop().flatten())
126    }
127
128    /// Several blobs, one process.
129    ///
130    /// `--batch` states absence as the word "missing" rather than as an exit
131    /// code, so the existence probe this used to spawn first is not merely
132    /// saved but replaced by something more explicit. And it reads a LIST from
133    /// stdin, which is what makes the batch free: the protocol was always
134    /// there, only the loop is new. A spawn costs milliseconds and the reviewer
135    /// reads two blobs for every file it draws (ADR 0021).
136    ///
137    /// The specs go in on stdin, which also keeps paths as raw bytes without an
138    /// `OsString` detour. `-z` is not optional: git paths may contain a
139    /// newline, and line-delimited input splits such a path into two specs that
140    /// both come back "missing" — a wrong answer with no error attached to it.
141    fn blobs(&self, specs: &[(&str, &[u8])]) -> Result<Vec<Option<Vec<u8>>>, EngineError> {
142        if specs.is_empty() {
143            return Ok(Vec::new());
144        }
145        let wire: Vec<Vec<u8>> = specs
146            .iter()
147            .map(|(rev, path)| {
148                let mut s = rev.as_bytes().to_vec();
149                s.push(b':');
150                s.extend_from_slice(path);
151                s
152            })
153            .collect();
154        let mut stdin = Vec::new();
155        for s in &wire {
156            stdin.extend_from_slice(s);
157            stdin.push(0);
158        }
159        let out = self.run(["cat-file", "--batch", "-z"], Some(&stdin))?;
160        parse_batch_blobs(&out, &wire).map_err(|msg| EngineError::GitCommand {
161            command: format!("cat-file --batch ({} specs)", specs.len()),
162            code: None,
163            stderr: msg,
164        })
165    }
166
167    /// Fully resolve a revision to a commit sha.
168    fn rev_parse(&self, rev: &str) -> Result<String, EngineError> {
169        let out = self.run(
170            ["rev-parse", "--verify", &format!("{rev}^{{commit}}")],
171            None,
172        )?;
173        Ok(String::from_utf8_lossy(trim_newline(&out)).into_owned())
174    }
175
176    /// Resolve to a commit sha, or accept a raw tree oid — the endpoints of
177    /// an uncommitted-state review are synthesized trees (ADR 0017), and
178    /// everything downstream of resolution is tree-safe.
179    fn rev_parse_commit_or_tree(&self, rev: &str) -> Result<String, EngineError> {
180        self.rev_parse(rev)
181            .or_else(|_| self.rev_parse_raw(&format!("{rev}^{{tree}}")))
182    }
183
184    /// Resolve any rev expression (tree, `X^{tree}`, blob spec) to an object id.
185    fn rev_parse_raw(&self, expr: &str) -> Result<String, EngineError> {
186        let out = self.run(["rev-parse", "--verify", expr], None)?;
187        Ok(String::from_utf8_lossy(trim_newline(&out)).into_owned())
188    }
189
190    fn merge_base(&self, a: &str, b: &str) -> Result<String, EngineError> {
191        let out = self.run(["merge-base", a, b], None)?;
192        Ok(String::from_utf8_lossy(trim_newline(&out)).into_owned())
193    }
194
195    /// The shared git directory (worktree-safe). Per-repo state such as the
196    /// grouping cache lives under `<common-dir>/differential/`.
197    fn common_dir(&self) -> Result<PathBuf, EngineError> {
198        let out = self.run(["rev-parse", "--git-common-dir"], None)?;
199        let p = PathBuf::from(String::from_utf8_lossy(trim_newline(&out)).into_owned());
200        Ok(if p.is_absolute() {
201            p
202        } else {
203            self.root.join(p)
204        })
205    }
206}
207
208// ------------------------------------------------------------------ ports
209//
210// `Repo` is the ONLY implementation of these, and ADR 0020 forbids a second —
211// invariants 1-4 compare the engine against git's own answer, so a fake git
212// would compare the fake with the fake.
213
214impl ports::ObjectReader for Repo {
215    fn blob(&self, rev: &str, path: &[u8]) -> Result<Option<Vec<u8>>, EngineError> {
216        Repo::blob(self, rev, path)
217    }
218
219    fn blobs(&self, specs: &[(&str, &[u8])]) -> Result<Vec<Option<Vec<u8>>>, EngineError> {
220        Repo::blobs(self, specs)
221    }
222
223    fn require_object(&self, oid: &str) -> Result<(), EngineError> {
224        self.run(["cat-file", "-e", oid], None).map(|_| ())
225    }
226}
227
228impl ports::ObjectWriter for Repo {
229    fn write_blob(&self, content: &[u8]) -> Result<String, EngineError> {
230        let out = self.run(["hash-object", "-w", "--stdin"], Some(content))?;
231        Ok(String::from_utf8_lossy(trim_newline(&out)).into_owned())
232    }
233}
234
235impl ports::RangeResolver for Repo {
236    fn merge_base(&self, a: &str, b: &str) -> Result<String, EngineError> {
237        Repo::merge_base(self, a, b)
238    }
239
240    fn resolve_endpoint(&self, rev: &str) -> Result<String, EngineError> {
241        self.rev_parse_commit_or_tree(rev)
242    }
243}
244
245impl ports::Ancestry for Repo {
246    fn commit_of(&self, spec: &str) -> Result<Option<String>, EngineError> {
247        // A spec that names nothing is a deleted branch, not a git failure:
248        // the caller skips that candidate rather than refusing to open.
249        Ok(self.rev_parse(spec).ok())
250    }
251
252    fn is_ancestor(&self, older: &str, newer: &str) -> Result<bool, EngineError> {
253        // Exit 0 is yes and exit 1 is no — both are answers, which is what
254        // `run_status` is for. Anything else is git failing to answer, and
255        // that must surface rather than read as "no": a review silently
256        // filing itself twice is exactly what this code exists to prevent.
257        let status = self.run_status(["merge-base", "--is-ancestor", older, newer])?;
258        match status.code() {
259            Some(0) => Ok(true),
260            Some(1) => Ok(false),
261            other => Err(EngineError::GitCommand {
262                command: format!("merge-base --is-ancestor {older} {newer}"),
263                code: other,
264                stderr: String::new(),
265            }),
266        }
267    }
268}
269
270impl ports::TreeResolver for Repo {
271    fn tree_of(&self, rev: &str) -> Result<String, EngineError> {
272        self.rev_parse_raw(&format!("{rev}^{{tree}}"))
273    }
274}
275
276impl ports::DiffSource for Repo {
277    // FROZEN ARGV — see the trait docs. Add a method, never edit one.
278    fn raw_records(&self, base: &str, head: &str) -> Result<Vec<u8>, EngineError> {
279        self.run(
280            [
281                "diff-tree",
282                "-r",
283                "-z",
284                "--raw",
285                "--full-index",
286                "--no-renames",
287                base,
288                head,
289            ],
290            None,
291        )
292    }
293
294    fn canonical_patch(&self, base: &str, head: &str) -> Result<Vec<u8>, EngineError> {
295        self.run(
296            [
297                "diff-tree",
298                "-r",
299                "-U0",
300                "--no-renames",
301                "--no-color",
302                "--no-ext-diff",
303                base,
304                head,
305            ],
306            None,
307        )
308    }
309
310    fn rename_records(&self, base: &str, head: &str) -> Result<Vec<u8>, EngineError> {
311        self.run(
312            ["diff-tree", "-r", "-M", "-z", "--name-status", base, head],
313            None,
314        )
315    }
316}
317
318impl ports::RecountSource for Repo {
319    /// Spelled out here rather than delegating to `canonical_patch`, and the
320    /// argv genuinely differs from it (no `--no-color --no-ext-diff`). Both
321    /// facts are deliberate: invariant 4's independence is the whole point, so
322    /// one edit to enumeration's flags must not move both sides of the
323    /// comparison. Do not "tidy" these two into one.
324    fn recount_patch(&self, from: &str, to: &str) -> Result<Vec<u8>, EngineError> {
325        self.run(["diff-tree", "-r", "-U0", "--no-renames", from, to], None)
326    }
327}
328
329impl ports::AttributeSource for Repo {
330    fn check_attr(
331        &self,
332        attr: &str,
333        paths: &[&[u8]],
334    ) -> Result<Vec<ports::AttrValue>, EngineError> {
335        if paths.is_empty() {
336            return Ok(Vec::new());
337        }
338        let mut stdin: Vec<u8> = Vec::new();
339        for p in paths {
340            stdin.extend_from_slice(p);
341            stdin.push(0);
342        }
343        let out = self.run(["check-attr", "-z", "--stdin", attr], Some(&stdin))?;
344        // -z output: path NUL attr NUL value NUL ...
345        let fields: Vec<&[u8]> = out.split(|&b| b == 0).collect();
346        Ok(fields
347            .chunks_exact(3)
348            .map(|triple| ports::AttrValue {
349                path: triple[0].to_vec(),
350                value: triple[2].to_vec(),
351            })
352            .collect())
353    }
354}
355
356/// A scratch index and its temp directory, alive together.
357///
358/// Owning the `TempDir` is what removes the `let _keep = idx;` hazards the
359/// callers used to need: `write_tree` borrows the session, so the index file
360/// provably outlives every use of it.
361pub struct ScratchIndex {
362    repo: Repo,
363    _dir: tempfile::TempDir,
364    index: std::ffi::OsString,
365}
366
367impl ScratchIndex {
368    /// A path that does NOT exist yet: git treats an existing empty file as a
369    /// corrupt index, so hand it a fresh name inside a temp dir rather than a
370    /// pre-created `NamedTempFile`.
371    fn open(repo: &Repo) -> Result<Self, EngineError> {
372        let dir = tempfile::TempDir::new().map_err(|e| EngineError::GitSpawn { source: e })?;
373        let index = dir.path().join("index").into_os_string();
374        Ok(ScratchIndex {
375            repo: repo.clone(),
376            _dir: dir,
377            index,
378        })
379    }
380
381    fn env(&self) -> [(&str, &OsStr); 1] {
382        [("GIT_INDEX_FILE", self.index.as_os_str())]
383    }
384}
385
386impl ports::TreeBuilder for Repo {
387    type Session = ScratchIndex;
388
389    fn begin_from_tree(&self, tree_ish: &str) -> Result<ScratchIndex, EngineError> {
390        let idx = ScratchIndex::open(self)?;
391        self.run_env(["read-tree", tree_ish], None, &idx.env())?;
392        Ok(idx)
393    }
394
395    fn begin_from_current_index(&self) -> Result<ScratchIndex, EngineError> {
396        // "<mode> <oid> <stage>\t<path>" records — exactly the second input
397        // format `update-index --index-info` accepts, so the seed is a byte
398        // pipe with both ends inside this adapter.
399        let entries = self.run(["ls-files", "-s", "-z"], None)?;
400        for record in entries.split(|&b| b == 0) {
401            let meta = record.split(|&b| b == b'\t').next().unwrap_or(record);
402            if meta.ends_with(b" 1") || meta.ends_with(b" 2") || meta.ends_with(b" 3") {
403                return Err(EngineError::Range(
404                    "index has unmerged entries — resolve conflicts before reviewing \
405                     uncommitted changes"
406                        .into(),
407                ));
408            }
409        }
410        let idx = ScratchIndex::open(self)?;
411        if !entries.is_empty() {
412            self.run_env(
413                ["update-index", "-z", "--index-info"],
414                Some(&entries),
415                &idx.env(),
416            )?;
417        }
418        Ok(idx)
419    }
420}
421
422impl ports::IndexSession for ScratchIndex {
423    fn stage(&mut self, entries: &[ports::IndexEntry]) -> Result<(), EngineError> {
424        if entries.is_empty() {
425            return Ok(());
426        }
427        let mut feed: Vec<u8> = Vec::new();
428        for e in entries {
429            feed.extend_from_slice(&index_record(e));
430            feed.push(0);
431        }
432        self.repo
433            .run_env(
434                ["update-index", "-z", "--index-info"],
435                Some(&feed),
436                &self.env(),
437            )
438            .map(|_| ())
439    }
440
441    fn stage_from_worktree(&mut self, nul_paths: &[u8]) -> Result<(), EngineError> {
442        if nul_paths.is_empty() {
443            return Ok(());
444        }
445        self.repo
446            .run_env(
447                ["update-index", "--add", "--remove", "-z", "--stdin"],
448                Some(nul_paths),
449                &self.env(),
450            )
451            .map(|_| ())
452    }
453
454    fn write_tree(&self) -> Result<String, EngineError> {
455        let out = self.repo.run_env(["write-tree"], None, &self.env())?;
456        Ok(String::from_utf8_lossy(&out).trim().to_string())
457    }
458}
459
460const ZERO_OID: &str = "0000000000000000000000000000000000000000";
461
462/// One `update-index --index-info` record: `<mode> <oid>\t<path>`, removal
463/// spelled as mode 0. Git wire format, so it lives with the adapter.
464fn index_record(e: &ports::IndexEntry) -> Vec<u8> {
465    let (mode, oid, path) = match e {
466        ports::IndexEntry::Set { mode, oid, path } => (mode.as_str(), oid.as_str(), path),
467        ports::IndexEntry::Remove { path } => ("0", ZERO_OID, path),
468    };
469    let mut line = format!("{mode} {oid}\t").into_bytes();
470    line.extend_from_slice(path);
471    line
472}
473
474impl ports::WorkingCopy for Repo {
475    fn tracked_paths(&self) -> Result<Vec<u8>, EngineError> {
476        self.run(["ls-files", "-z"], None)
477    }
478
479    /// `diff-index --quiet HEAD --`: exit 1 means differences, which is the
480    /// answer rather than a failure — hence `run_status` instead of `run`.
481    fn has_tracked_changes(&self) -> Result<bool, EngineError> {
482        let status = self.run_status(["diff-index", "--quiet", "HEAD", "--"])?;
483        Ok(!status.success())
484    }
485
486    fn untracked_paths(&self) -> Result<Vec<u8>, EngineError> {
487        self.run(["ls-files", "--others", "--exclude-standard", "-z"], None)
488    }
489}
490
491impl ports::CommitWriter for Repo {
492    fn commit_tree(
493        &self,
494        tree: &str,
495        parent: &str,
496        message: &[u8],
497        identity: ports::CommitIdentity<'_>,
498    ) -> Result<String, EngineError> {
499        let env: [(&str, &OsStr); 4] = [
500            ("GIT_AUTHOR_NAME", OsStr::new(identity.name)),
501            ("GIT_AUTHOR_EMAIL", OsStr::new(identity.email)),
502            ("GIT_COMMITTER_NAME", OsStr::new(identity.name)),
503            ("GIT_COMMITTER_EMAIL", OsStr::new(identity.email)),
504        ];
505        let out = self.run_env(
506            ["commit-tree", tree, "-p", parent, "-F", "-"],
507            Some(message),
508            &env,
509        )?;
510        Ok(String::from_utf8_lossy(trim_newline(&out)).into_owned())
511    }
512}
513
514impl ports::RefWriter for Repo {
515    fn update_ref(&self, name: &str, target: &str) -> Result<(), EngineError> {
516        self.run(["update-ref", name, target], None).map(|_| ())
517    }
518}
519
520impl ports::CommitHistory for Repo {
521    fn has_commits(&self) -> bool {
522        self.rev_parse("HEAD").is_ok()
523    }
524
525    fn recent_commits(
526        &self,
527        from: &str,
528        max: usize,
529    ) -> Result<Vec<ports::CommitSummary>, EngineError> {
530        let raw = self.run(
531            [
532                "rev-list",
533                &format!("--max-count={max}"),
534                "--no-commit-header",
535                "--format=%H%x00%h%x00%s%x00%an",
536                from,
537            ],
538            None,
539        )?;
540        Ok(parse_rev_list(&raw))
541    }
542
543    fn refs_by_commit(&self) -> HashMap<String, Vec<String>> {
544        self.run(
545            [
546                "for-each-ref",
547                "--format=%(objectname)%00%(*objectname)%00%(refname:short)",
548                "refs/heads",
549                "refs/tags",
550                "refs/remotes",
551            ],
552            None,
553        )
554        .map(|out| parse_refs(&out))
555        .unwrap_or_default()
556    }
557}
558
559impl ports::RepoLayout for Repo {
560    fn common_dir(&self) -> Result<PathBuf, EngineError> {
561        Repo::common_dir(self)
562    }
563
564    fn work_root(&self) -> &Path {
565        &self.root
566    }
567}
568
569/// One NUL-separated record, split into its fields.
570///
571/// Both `--format` readers below want the same thing from a line, and both
572/// wrote out the same three-line split to get it. Lossy on purpose: these are
573/// subjects, author names and ref names on their way to a screen, and the
574/// byte-exact paths never come through here.
575fn nul_fields(line: &[u8]) -> Vec<String> {
576    line.split(|&b| b == 0)
577        .map(|f| String::from_utf8_lossy(f).into_owned())
578        .collect()
579}
580
581/// `rev-list --no-commit-header --format=%H%x00%h%x00%s%x00%an` output: one
582/// record per line, fields NUL-separated (subjects are single-line by
583/// definition, so the line split is safe; bytes decode lossily).
584fn parse_rev_list(bytes: &[u8]) -> Vec<ports::CommitSummary> {
585    bytes
586        .split(|&b| b == b'\n')
587        .filter(|l| !l.is_empty())
588        .filter_map(|line| {
589            let fields = nul_fields(line);
590            match fields.as_slice() {
591                [sha, short, subject, author] => Some(ports::CommitSummary {
592                    sha: sha.clone(),
593                    short: short.clone(),
594                    subject: subject.clone(),
595                    author: author.clone(),
596                }),
597                _ => None,
598            }
599        })
600        .collect()
601}
602
603/// `for-each-ref --format='%(objectname)%00%(*objectname)%00%(refname:short)'`
604/// output → sha -> ref names. Plumbing, so unaffected by log.decorate config;
605/// annotated tags carry the peeled commit in the second field.
606///
607/// NOTE the escape: for-each-ref's format language spells NUL `%00`. `%x00`
608/// is a rev-list/log spelling and passes through as literal text here, which
609/// is exactly how this silently produced no decorations at all.
610fn parse_refs(bytes: &[u8]) -> HashMap<String, Vec<String>> {
611    let mut out: HashMap<String, Vec<String>> = HashMap::new();
612    for line in bytes.split(|&b| b == b'\n').filter(|l| !l.is_empty()) {
613        let fields = nul_fields(line);
614        let [oid, peeled, name] = fields.as_slice() else {
615            continue;
616        };
617        if name.is_empty() {
618            continue;
619        }
620        // An annotated tag's own object id is the tag; the commit it points at
621        // is the peeled one.
622        let target = if peeled.is_empty() { oid } else { peeled };
623        out.entry(target.clone()).or_default().push(name.clone());
624    }
625    out
626}
627
628/// A `cat-file --batch -z` stream: one response per spec, in the order asked.
629///
630/// Each is `<oid> SP <type> SP <size> LF <contents> LF`, or
631/// `<spec> SP "missing" LF` when the path is not there. `-z` changes only the
632/// INPUT framing, so headers are still LF-terminated.
633///
634/// Telling the two apart needs the specs, because both near-misses are real:
635///
636/// - a missing response echoes the spec, and a git path may contain a newline,
637///   so scanning for the next LF does not reliably find the end of the line;
638/// - a blob whose contents happen to END with " missing" looks, from the tail,
639///   exactly like an absent one.
640///
641/// Knowing the spec settles both: absence is an EXACT match for
642/// `<spec> " missing\n"` at the current position, and everything else must be a
643/// header — a hex oid, a type, a number — or the repository is broken, which is
644/// not the same as a file being absent and must never render as one.
645fn parse_batch_blobs(out: &[u8], specs: &[Vec<u8>]) -> Result<Vec<Option<Vec<u8>>>, String> {
646    const MISSING: &[u8] = b" missing\n";
647    let mut at = 0usize;
648    let mut got = Vec::with_capacity(specs.len());
649
650    for spec in specs {
651        let rest = out
652            .get(at..)
653            .ok_or_else(|| "output ended early".to_string())?;
654        if rest.starts_with(spec) && rest[spec.len()..].starts_with(MISSING) {
655            got.push(None);
656            at += spec.len() + MISSING.len();
657            continue;
658        }
659        let end = rest
660            .iter()
661            .position(|&b| b == b'\n')
662            .ok_or_else(|| "no header line in cat-file output".to_string())?;
663        let header = String::from_utf8_lossy(&rest[..end]).into_owned();
664        let fields: Vec<&str> = header.split(' ').collect();
665        if fields.len() != 3
666            || fields[0].is_empty()
667            || !fields[0].bytes().all(|b| b.is_ascii_hexdigit())
668        {
669            return Err(format!("unrecognised cat-file response: {header}"));
670        }
671        if fields[1] != "blob" {
672            return Err(format!("{header}: not a blob"));
673        }
674        let size: usize = fields[2]
675            .parse()
676            .map_err(|_| format!("{header}: unparsable size"))?;
677        let body = &rest[end + 1..];
678        if body.len() < size {
679            return Err(format!(
680                "{header}: body is {} bytes, header said {size}",
681                body.len()
682            ));
683        }
684        // git writes an LF after the body. Check it rather than assume it: on
685        // a malformed stream the alternative is to walk on from the wrong
686        // offset and read the next response as this one's neighbour, which
687        // desyncs everything after it without ever reporting a fault.
688        if body.get(size) != Some(&b'\n') {
689            return Err(format!("{header}: body is not LF-terminated"));
690        }
691        got.push(Some(body[..size].to_vec()));
692        // header LF + body + the LF git writes after it
693        at += end + 1 + size + 1;
694    }
695    Ok(got)
696}
697
698fn trim_newline(b: &[u8]) -> &[u8] {
699    let mut end = b.len();
700    while end > 0 && (b[end - 1] == b'\n' || b[end - 1] == b'\r') {
701        end -= 1;
702    }
703    &b[..end]
704}
705
706fn describe(cmd: &Command) -> String {
707    let mut s = String::from("git");
708    for a in cmd.get_args() {
709        s.push(' ');
710        s.push_str(&a.to_string_lossy());
711        if s.len() > 200 {
712            s.push_str(" …");
713            break;
714        }
715    }
716    s
717}
718
719#[cfg(test)]
720mod tests {
721    use super::{parse_batch_blobs, parse_refs, parse_rev_list};
722
723    /// The absent/broken distinction the old two-spawn probe existed for, now
724    /// carried by `--batch`'s own vocabulary — and read against the specs that
725    /// were sent, which is what makes both near-misses decidable.
726    #[test]
727    fn batch_blob_separates_absent_from_broken() {
728        let oid = "e".repeat(40);
729        let one = |spec: &str| vec![spec.as_bytes().to_vec()];
730
731        let found = format!("{oid} blob 6\nhello\n\n");
732        assert_eq!(
733            parse_batch_blobs(found.as_bytes(), &one("HEAD:a"))
734                .unwrap()
735                .remove(0)
736                .as_deref(),
737            Some(&b"hello\n"[..]),
738            "the body is exactly the declared size, trailing LF excluded"
739        );
740
741        // A path that is not there is absence, not failure.
742        assert_eq!(
743            parse_batch_blobs(b"HEAD:nope missing\n", &one("HEAD:nope")).unwrap(),
744            vec![None]
745        );
746        // A blob whose CONTENT ends in " missing" produces exactly the bytes an
747        // absent path produces at the end of the response.
748        let ends_missing = format!("{oid} blob 9\nx missing\n");
749        assert_eq!(
750            parse_batch_blobs(ends_missing.as_bytes(), &one("HEAD:a"))
751                .unwrap()
752                .remove(0)
753                .as_deref(),
754            Some(&b"x missing"[..]),
755            "a blob ending in \" missing\" must not be read as absent"
756        );
757        // A space in the path is not positional: the spec is matched whole.
758        assert_eq!(
759            parse_batch_blobs(b"HEAD:a b.txt missing\n", &one("HEAD:a b.txt")).unwrap(),
760            vec![None]
761        );
762        // Nor is a NEWLINE in one — which is the whole reason the input is
763        // NUL-delimited, and the reason the spec has to be matched rather than
764        // the next LF looked for.
765        assert_eq!(
766            parse_batch_blobs(b"HEAD:we\nird.txt missing\n", &one("HEAD:we\nird.txt")).unwrap(),
767            vec![None]
768        );
769
770        // A tree or a commit at that path is a broken assumption, not a file.
771        assert!(parse_batch_blobs(format!("{oid} tree 42\n").as_bytes(), &one("HEAD:a")).is_err());
772        // A truncated body must never be mistaken for a short file.
773        assert!(
774            parse_batch_blobs(format!("{oid} blob 99\nshort\n").as_bytes(), &one("HEAD:a"))
775                .is_err()
776        );
777        assert!(parse_batch_blobs(b"no newline at all", &one("HEAD:a")).is_err());
778    }
779
780    /// Many specs, one stream: each response has to be found by walking, since
781    /// only the previous one's declared size says where the next begins.
782    #[test]
783    fn batch_blobs_walks_a_stream_of_responses() {
784        let oid = "a".repeat(40);
785        let specs = vec![
786            b"HEAD:one".to_vec(),
787            b"HEAD:gone".to_vec(),
788            b"HEAD:two".to_vec(),
789        ];
790        let mut out = format!("{oid} blob 4\nabcd\n").into_bytes();
791        out.extend_from_slice(b"HEAD:gone missing\n");
792        out.extend_from_slice(format!("{oid} blob 2\nxy\n").as_bytes());
793
794        assert_eq!(
795            parse_batch_blobs(&out, &specs).unwrap(),
796            vec![Some(b"abcd".to_vec()), None, Some(b"xy".to_vec()),],
797            "answers come back in the order the specs were given"
798        );
799
800        // A stream that stops early is a broken repository, not three absences.
801        assert!(parse_batch_blobs(&out[..10], &specs).is_err());
802
803        // And one where the body is not LF-terminated where the header said it
804        // would end: walking on from there would read the next response as this
805        // one's neighbour and desync the rest without reporting a fault.
806        let mut bad = format!("{oid} blob 4\nabcd").into_bytes();
807        bad.extend_from_slice(b"XHEAD:gone missing\n");
808        assert!(parse_batch_blobs(&bad, &specs[..2]).is_err());
809    }
810
811    /// Bytes in, bytes out: a blob that is not UTF-8 survives intact.
812    #[test]
813    fn batch_blob_is_byte_faithful() {
814        let oid = "f".repeat(40);
815        let mut raw = format!("{oid} blob 4\n").into_bytes();
816        raw.extend_from_slice(&[0x00, 0xff, 0xfe, 0x0a, 0x0a]);
817        assert_eq!(
818            parse_batch_blobs(&raw, &[b"HEAD:a".to_vec()])
819                .unwrap()
820                .remove(0)
821                .unwrap(),
822            vec![0x00, 0xff, 0xfe, 0x0a]
823        );
824    }
825
826    #[test]
827    fn parses_nul_separated_records() {
828        let raw = b"aaaa\0a1\0fix the thing\0Alice\nbbbb\0b2\0subject with \xe2\x9c\x93 unicode\0B\xc3\xb6b\n";
829        let entries = parse_rev_list(raw);
830        assert_eq!(entries.len(), 2);
831        assert_eq!(entries[0].sha, "aaaa");
832        assert_eq!(entries[0].short, "a1");
833        assert_eq!(entries[0].subject, "fix the thing");
834        assert_eq!(entries[0].author, "Alice");
835        assert_eq!(entries[1].subject, "subject with ✓ unicode");
836        assert_eq!(entries[1].author, "Böb");
837    }
838
839    #[test]
840    fn tolerates_empty_and_malformed_lines() {
841        assert!(parse_rev_list(b"").is_empty());
842        assert!(parse_rev_list(b"\n\n").is_empty());
843        assert!(parse_rev_list(b"only-two\0fields\n").is_empty());
844    }
845
846    #[test]
847    fn refs_group_by_commit_and_peel_annotated_tags() {
848        // Lightweight ref: own oid is the commit. Annotated tag: the peeled
849        // field carries the commit.
850        let raw = b"aaaa\0\0main\naaaa\0\0origin/main\ntagobj\0aaaa\0v1.0\nbbbb\0\0feature\n";
851        let refs = parse_refs(raw);
852        assert_eq!(
853            refs.get("aaaa").unwrap(),
854            &vec![
855                "main".to_string(),
856                "origin/main".to_string(),
857                "v1.0".to_string()
858            ]
859        );
860        assert_eq!(refs.get("bbbb").unwrap(), &vec!["feature".to_string()]);
861        // The tag object's own id is never a key.
862        assert!(!refs.contains_key("tagobj"));
863    }
864
865    #[test]
866    fn refs_tolerate_junk() {
867        assert!(parse_refs(b"").is_empty());
868        assert!(parse_refs(b"\n\n").is_empty());
869        assert!(parse_refs(b"two\0fields\n").is_empty());
870        // An empty ref name is skipped rather than stored.
871        assert!(parse_refs(b"aaaa\0\0\n").is_empty());
872    }
873}