Skip to content

Commit 5dea6f1

Browse files
committed
fix: assure empty paths are always matching a Search
This improvement was triggered by [this question](rust-lang/cargo#13777 (review)).
1 parent 8d610ab commit 5dea6f1

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

gix-pathspec/src/search/matching.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl Search {
133133
/// is ignored.
134134
/// Returns `false` if this pathspec has no chance of ever matching `relative_path`.
135135
pub fn can_match_relative_path(&self, relative_path: &BStr, is_dir: Option<bool>) -> bool {
136-
if self.patterns.is_empty() {
136+
if self.patterns.is_empty() || relative_path.is_empty() {
137137
return true;
138138
}
139139
let common_prefix_len = self.common_prefix_len.min(relative_path.len());
@@ -194,7 +194,7 @@ impl Search {
194194
/// When `leading` is `true`, then `d` matches `d/d` as well. Thus, `relative_path` must may be
195195
/// partially included in `pathspec`, otherwise it has to be fully included.
196196
pub fn directory_matches_prefix(&self, relative_path: &BStr, leading: bool) -> bool {
197-
if self.patterns.is_empty() {
197+
if self.patterns.is_empty() || relative_path.is_empty() {
198198
return true;
199199
}
200200
let common_prefix_len = self.common_prefix_len.min(relative_path.len());

gix-pathspec/tests/search/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ fn directory_matches_prefix_starting_wildcards_always_match() -> crate::Result {
5757
Ok(())
5858
}
5959

60+
#[test]
61+
fn empty_dir_always_matches() -> crate::Result {
62+
for specs in [
63+
&["*ir"] as &[_],
64+
&[],
65+
&["included", ":!excluded"],
66+
&[":!all", ":!excluded"],
67+
] {
68+
let search = gix_pathspec::Search::from_specs(pathspecs(specs), None, Path::new(""))?;
69+
assert!(search.directory_matches_prefix("".into(), false));
70+
assert!(search.directory_matches_prefix("".into(), false));
71+
for is_dir in [Some(true), Some(false), None] {
72+
assert!(search.can_match_relative_path("".into(), is_dir));
73+
}
74+
}
75+
Ok(())
76+
}
77+
6078
#[test]
6179
fn directory_matches_prefix_leading() -> crate::Result {
6280
let search = gix_pathspec::Search::from_specs(pathspecs(&["d/d/generated/b"]), None, Path::new(""))?;

0 commit comments

Comments
 (0)