Update --exclude option to filter out entire subfolders

Before this change, using `--exclude foobar` would search for the exact PATH `foobar`, which always resulted in false negatives. With this change, both the file `./foobar` and the folder `./foobar/`, including its contents, are filtered out as expected.
This commit is contained in:
Yvan Sraka 2023-09-19 08:27:10 +02:00 committed by Astro
parent dd2d313fde
commit c756086198

View File

@ -1,7 +1,7 @@
use clap::{Arg, ArgAction, Command};
#[cfg(feature = "json-out")]
use serde_json::json;
use std::{fs, collections::HashSet};
use std::{fs, collections::HashSet, path::Path};
mod binding;
mod dead_code;
@ -122,7 +122,13 @@ fn main() {
};
let is_included: Box<dyn Fn(&String) -> bool> = if let Some(excludes) = matches.get_many("EXCLUDES") {
let excludes = excludes.cloned().collect::<HashSet<String>>();
Box::new(move |s| ! excludes.contains(s))
Box::new(move |s| {
let s = Path::new(s).canonicalize().unwrap();
!excludes.iter().any(|exclude| {
let exclude = Path::new(exclude).canonicalize().unwrap();
s.starts_with(&exclude)
})
})
} else {
Box::new(|_| true)
};