Skip to content

Commit

Permalink
fix: file globbing is now more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Apr 16, 2024
1 parent 9d01c77 commit 5051900
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/task/file_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,27 @@ impl FileHashes {
let mut ignore_builder = OverrideBuilder::new(root);
for ignore_line in filters {
let path = root.join(ignore_line.as_ref());
if ignore_line.as_ref().ends_with('/') {
ignore_builder.add(&format!("{}**", ignore_line.as_ref()))?;
let mut pat = if ignore_line.as_ref().ends_with('/') {
format!("{}**", ignore_line.as_ref())
} else if path.exists() && path.is_dir() {
ignore_builder.add(&format!("{}/**", ignore_line.as_ref()))?;
format!("{}/**", ignore_line.as_ref())
} else {
ignore_builder.add(ignore_line.as_ref())?;
ignore_line.as_ref().to_owned()
};

if pat.starts_with('!') && !pat.starts_with("!/") {
// make sure there is a `/` at the 2nd place so that the pattern reads
// `!/**/lib.rs` instead of `!**/lib.rs`
pat.insert(1, '/');
} else {
// Same for the others, make sure they start in the right folder
if !pat.starts_with('/') {
pat.insert(0, '/');
}
}
ignore_builder.add(&pat)?;
}

let filter = ignore_builder.build()?;

// Spawn a thread that will collect the results from a channel.
Expand Down Expand Up @@ -232,11 +245,16 @@ mod test {
assert!(hashes.files.contains_key(Path::new("src/bla/lib.rs")));
assert!(!hashes.files.contains_key(Path::new("Cargo.toml")));

let hashes =
FileHashes::from_files(target_dir.path(), vec!["main.rs"])
.await
.unwrap();
let hashes = FileHashes::from_files(target_dir.path(), vec!["main.rs"])
.await
.unwrap();

assert!(!hashes.files.contains_key(Path::new("src/main.rs")));

let hashes = FileHashes::from_files(target_dir.path(), vec!["src/lib.rs", "src/*.rs"])
.await
.unwrap();

assert!(hashes.files.contains_key(Path::new("src/lib.rs")));
}
}

0 comments on commit 5051900

Please sign in to comment.