simplify cached tarball data structure for intended usage

This commit is contained in:
Chris Beck
2025-12-18 10:25:24 -07:00
parent 7038a5fa72
commit 89599e0b07
2 changed files with 7 additions and 12 deletions
+5 -5
View File
@@ -2,7 +2,7 @@
use flate2::read::GzDecoder;
use globset::GlobSet;
use std::{collections::HashMap, io::Read};
use std::{collections::BTreeMap, io::Read};
use tar::Archive;
use tracing::info;
@@ -27,8 +27,8 @@ impl CachedFile {
pub struct CachedTarball {
/// The git SHA this tarball corresponds to.
pub sha: String,
/// Map from file path to file contents.
pub files: HashMap<String, CachedFile>,
/// Map from file path to file contents (sorted by path).
pub files: BTreeMap<String, CachedFile>,
}
impl CachedTarball {
@@ -58,11 +58,11 @@ fn extract_files(
tarball: &[u8],
glob_filter: Option<&GlobSet>,
include_non_utf8: bool,
) -> Result<HashMap<String, CachedFile>, String> {
) -> Result<BTreeMap<String, CachedFile>, String> {
let decoder = GzDecoder::new(tarball);
let mut archive = Archive::new(decoder);
let mut files = HashMap::new();
let mut files = BTreeMap::new();
for entry in archive
.entries()
+2 -7
View File
@@ -291,15 +291,13 @@ impl CodeTool {
.map_err(|e| format!("Invalid glob pattern: {e}"))?
.compile_matcher();
let mut matches: Vec<&str> = cached
let matches: Vec<&str> = cached
.files
.keys()
.filter(|path| glob.is_match(path))
.map(|s| s.as_str())
.collect();
matches.sort();
if matches.is_empty() {
Ok(format!("No files matching '{}'", pattern))
} else {
@@ -371,10 +369,7 @@ impl CodeTool {
let mut file_count = 0;
const MAX_MATCHES: usize = 100;
let mut sorted_files: Vec<_> = cached.files.iter().collect();
sorted_files.sort_by_key(|(path, _)| *path);
'outer: for (path, file) in sorted_files {
'outer: for (path, file) in &cached.files {
// Skip if path doesn't match prefix
if let Some(prefix) = prefix
&& !path.starts_with(prefix)