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/watch/mirror.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/watch/mirror.rs') diff --git a/src/watch/mirror.rs b/src/watch/mirror.rs index 89c175d..6616bc1 100644 --- a/src/watch/mirror.rs +++ b/src/watch/mirror.rs @@ -67,19 +67,33 @@ impl Tree { } pub fn read_tree(mirror: &Path, commit: &str) -> Result { - if !git::has_commit(mirror, commit) { - return Err(Error::fail_closed(format!( - "commit {commit} is missing from the local mirror; history was rewritten" - ))); - } - let blobs = git::ls_tree(mirror, commit)?; + // One `ls-tree` and one `cat-file --batch` for the whole tree, so the number + // of files a recipe ships does not decide how many processes a read costs. + // A commit that is gone from the mirror surfaces here, from `ls-tree`, rather + // than from a separate `cat-file -e` probe first. + let blobs = match git::ls_tree(mirror, commit) { + Ok(blobs) => blobs, + Err(e) => { + let text = e.to_string().to_lowercase(); + if text.contains("not a tree object") || text.contains("not a valid object name") { + return Err(Error::fail_closed(format!( + "commit {commit} is missing from the local mirror; history was rewritten" + ))); + } + return Err(e); + } + }; + let oids: Vec = blobs.iter().map(|(_, sha)| sha.clone()).collect(); + let contents = git::cat_file_batch(mirror, &oids)?; let mut files = BTreeMap::new(); - for (path, _) in &blobs { - let bytes = git::read_blob(mirror, commit, path)?; - if bytes.len() > 4 * 1024 * 1024 || !looks_textual(&bytes) { + for (path, sha) in &blobs { + let bytes = contents + .get(sha) + .ok_or_else(|| Error::not_found(format!("{path} not in mirror")))?; + if bytes.len() > 4 * 1024 * 1024 || !looks_textual(bytes) { continue; } - files.insert(path.clone(), String::from_utf8_lossy(&bytes).into_owned()); + files.insert(path.clone(), String::from_utf8_lossy(bytes).into_owned()); } Ok(Tree { blobs, files }) } -- cgit v1.2.3