Skip to content

Commit

Permalink
Merge pull request #402 from serpent-os/fix-pattern
Browse files Browse the repository at this point in the history
boulder: Fix pattern matching
  • Loading branch information
ikeycode authored Jan 17, 2025
2 parents 69920ff + a8c40c2 commit f0022a8
Showing 1 changed file with 14 additions and 4 deletions.
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

0 comments on commit f0022a8

Please sign in to comment.