diff options
Diffstat (limited to 'src/watch/mirror.rs')
| -rw-r--r-- | src/watch/mirror.rs | 34 |
1 files changed, 24 insertions, 10 deletions
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<Tree> { - 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<String> = 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 }) } |
