Skip to content

Commit

Permalink
fix: assure empty paths are always matching a Search
Browse files Browse the repository at this point in the history
This improvement was triggered by [this question](rust-lang/cargo#13777 (review)).
  • Loading branch information
Byron committed Apr 19, 2024
1 parent 8d610ab commit 5dea6f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gix-pathspec/src/search/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Search {
/// is ignored.
/// Returns `false` if this pathspec has no chance of ever matching `relative_path`.
pub fn can_match_relative_path(&self, relative_path: &BStr, is_dir: Option<bool>) -> bool {
if self.patterns.is_empty() {
if self.patterns.is_empty() || relative_path.is_empty() {
return true;
}
let common_prefix_len = self.common_prefix_len.min(relative_path.len());
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Search {
/// When `leading` is `true`, then `d` matches `d/d` as well. Thus, `relative_path` must may be
/// partially included in `pathspec`, otherwise it has to be fully included.
pub fn directory_matches_prefix(&self, relative_path: &BStr, leading: bool) -> bool {
if self.patterns.is_empty() {
if self.patterns.is_empty() || relative_path.is_empty() {
return true;
}
let common_prefix_len = self.common_prefix_len.min(relative_path.len());
Expand Down
18 changes: 18 additions & 0 deletions gix-pathspec/tests/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ fn directory_matches_prefix_starting_wildcards_always_match() -> crate::Result {
Ok(())
}

#[test]
fn empty_dir_always_matches() -> crate::Result {
for specs in [
&["*ir"] as &[_],
&[],
&["included", ":!excluded"],
&[":!all", ":!excluded"],
] {
let search = gix_pathspec::Search::from_specs(pathspecs(specs), None, Path::new(""))?;
assert!(search.directory_matches_prefix("".into(), false));
assert!(search.directory_matches_prefix("".into(), false));
for is_dir in [Some(true), Some(false), None] {
assert!(search.can_match_relative_path("".into(), is_dir));
}
}
Ok(())
}

#[test]
fn directory_matches_prefix_leading() -> crate::Result {
let search = gix_pathspec::Search::from_specs(pathspecs(&["d/d/generated/b"]), None, Path::new(""))?;
Expand Down

0 comments on commit 5dea6f1

Please sign in to comment.