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

boulder: Fix pattern matching #402

Merged
merged 1 commit into from
Jan 17, 2025
Merged
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
18 changes: 14 additions & 4 deletions boulder/src/package/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ pub struct Rule {

impl Rule {
pub fn matches(&self, path: &str) -> bool {
self.pattern == path
|| path.starts_with(&self.pattern)
|| Pattern::new(&self.pattern)
.map(|pattern| pattern.matches(path))
if self.pattern == path {
return true;
}

// Escape the directory in case it contains characters that have special
// meaning in glob patterns (e.g., `[` or `]`).
let escaped_path = Pattern::escape(path);
Pattern::new(&self.pattern)
.map(|pattern| pattern.matches(&escaped_path))
.unwrap_or_default()
// If the supplied pattern is for a directory we want to match anything that's inside said directory,
// Do this by creating a recursive glob pattern by appending `**` if the pattern already ends in a `/` or `/**` if not
|| Pattern::new(format!("{}/**", &self.pattern.strip_suffix("/").unwrap_or(&self.pattern)).as_str())
.map(|pattern| pattern.matches(&escaped_path))
.unwrap_or_default()
}
}
Expand Down
Loading