perf: ommit files larger than 50mb from being processed

This commit is contained in:
Kiril Videlov 2024-03-02 23:15:06 +01:00
parent 5f928c6d44
commit 33572ea955
No known key found for this signature in database

View File

@ -75,7 +75,11 @@ pub fn workdir(
.ignore_submodules(true)
.context_lines(context_lines);
let diff = repository.diff_tree_to_workdir(Some(&tree), Some(&mut diff_opts))?;
let mut diff = repository.diff_tree_to_workdir(Some(&tree), Some(&mut diff_opts))?;
let (mut diff_opts, skipped_files) = without_large_files(50_000_000, &diff, diff_opts);
if !skipped_files.is_empty() {
diff = repository.diff_tree_to_workdir(Some(&tree), Some(&mut diff_opts))?;
}
hunks_by_filepath(repository, &diff)
}
@ -101,6 +105,28 @@ pub fn trees(
hunks_by_filepath(repository, &diff)
}
pub fn without_large_files(
size_limit_bytes: u64,
diff: &git2::Diff,
mut diff_opts: git2::DiffOptions,
) -> (git2::DiffOptions, Vec<String>) {
let mut skipped_files: Vec<String> = Vec::new();
for delta in diff.deltas() {
if delta.new_file().size() > size_limit_bytes {
if let Some(path) = delta.new_file().path() {
if let Some(path) = path.to_str() {
skipped_files.push(path.to_owned().clone());
}
}
} else if let Some(path) = delta.new_file().path() {
if let Some(path) = path.to_str() {
diff_opts.pathspec(path);
}
}
}
(diff_opts, skipped_files)
}
fn hunks_by_filepath(
repository: &Repository,
diff: &git2::Diff,