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