From e0960212c1499c76cc4abc8b2112baea97ecf1b4 Mon Sep 17 00:00:00 2001 From: aur-build Date: Thu, 19 Nov 2026 19:38:09 +0000 Subject: git: read a tree with one cat-file --batch instead of one process per file read_tree cost a cat-file -e, an ls-tree and a cat-file blob per file, so the number of processes a detection paid for scaled with how many files a recipe ships. One ls-tree plus one batch read is now constant, and a commit that is gone from the mirror surfaces from ls-tree itself - still fail closed, still the same message. --- src/git.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) (limited to 'src/git.rs') diff --git a/src/git.rs b/src/git.rs index 8605db7..16c5ce4 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,5 +1,6 @@ +use std::collections::BTreeMap; use std::path::Path; -use std::process::{Command, Output}; +use std::process::{Command, Output, Stdio}; use crate::error::{Error, Result}; @@ -144,6 +145,78 @@ pub fn read_blob(mirror: &Path, commit: &str, path: &str) -> Result> { Ok(out.stdout) } +pub fn cat_file_batch(mirror: &Path, oids: &[String]) -> Result>> { + let mut out = BTreeMap::new(); + if oids.is_empty() { + return Ok(out); + } + let mut child = Command::new("git") + .current_dir(mirror) + .env("GIT_TERMINAL_PROMPT", "0") + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("LC_ALL", "C") + .args(["cat-file", "--batch"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .map_err(|e| Error::git(format!("git cat-file --batch: {e}")))?; + + // The ids go in on their own thread: git streams blobs back as it reads the + // list, so a tree big enough to fill the pipe buffer would deadlock a + // write-everything-then-read-anything version here. + let mut stdin = child.stdin.take().expect("piped stdin"); + let input = oids.join("\n") + "\n"; + let writer = std::thread::spawn(move || { + use std::io::Write; + let _ = stdin.write_all(input.as_bytes()); + }); + + use std::io::{BufRead, BufReader, Read}; + let mut stderr = child.stderr.take().expect("piped stderr"); + let mut stdout = BufReader::new(child.stdout.take().expect("piped stdout")); + loop { + let mut header = String::new(); + if stdout.read_line(&mut header)? == 0 { + break; + } + let mut fields = header.trim_end_matches('\n').split(' '); + let id = fields.next().unwrap_or_default().to_string(); + match (fields.next(), fields.next().and_then(|s| s.parse().ok())) { + (Some("missing"), _) => continue, + (Some("blob"), Some(size)) => { + let mut body = vec![0u8; size]; + stdout.read_exact(&mut body)?; + // `--batch` puts an LF after every body, even an empty one. + let mut newline = [0u8; 1]; + stdout.read_exact(&mut newline)?; + out.insert(id, body); + } + _ => { + return Err(Error::git(format!( + "unreadable cat-file header {:?}", + header.trim_end() + ))) + } + } + } + + let _ = writer.join(); + let status = child.wait().map_err(|e| Error::git(e.to_string()))?; + // A missing object is a line in the output, not a non-zero exit; a non-zero + // exit means the process itself failed, which is not the caller's problem to + // distinguish from an absent blob. + if !status.success() { + let mut detail = Vec::new(); + stderr.read_to_end(&mut detail)?; + return Err(Error::git(format!( + "git cat-file --batch exited {status}: {}", + String::from_utf8_lossy(&detail).trim() + ))); + } + Ok(out) +} + pub fn materialize(mirror: &Path, commit: &str, dest: &Path) -> Result<()> { std::fs::create_dir_all(dest)?; let out = Command::new("git") -- cgit v1.2.3