Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust exclude matching logic to better handle directories #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,36 +145,41 @@ pub fn process_directory(args: &Cli) -> Result<()> {

fn is_excluded(entry: &ignore::DirEntry, args: &Cli) -> bool {
if let Some(excludes) = &args.exclude {
let path_str = entry.path().to_string_lossy();

let path = entry.path();
for exclude in excludes {
match exclude {
Exclude::Pattern(pattern) => {
let pattern = if !pattern.starts_with("./") && !pattern.starts_with("/") {
format!("./{}", pattern)
Comment on lines -153 to -154
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was pretty much needed because ignore::DirEntry was using absolute paths. and if i do things like -e "src/*" it does not work without it.

// Normalize the pattern
let pattern = pattern.trim_start_matches("./"); // Remove leading `./`
let pattern = if pattern.ends_with('/') {
format!("{}**/*", pattern) // If it ends with `/`, match all contents
} else {
pattern.clone()
pattern.to_string()
};

if let Ok(glob) = globset::GlobBuilder::new(&pattern)
.case_insensitive(false)
.build()
{
let matcher = glob.compile_matcher();
let check_path = if !path_str.starts_with("./") {
format!("./{}", path_str)
} else {
path_str.to_string()
};

if matcher.is_match(&check_path) {

// ✅ Only check full path unless explicitly prefixed with `**/`
if matcher.is_match(path) {
return true;
}

// ✅ If pattern starts with `**/`, allow ancestor matching
if pattern.starts_with("**/") {
for ancestor in path.ancestors() {
if matcher.is_match(ancestor) {
return true;
}
}
}
}
}
Exclude::File(path) => {
let matches = entry.path().ends_with(path);
if matches {
if entry.path().ends_with(path) {
return true;
}
}
Expand Down