Skip to content

Commit e4b57b1

Browse files
committed
push base path handling to the caller (#301)
The caller merely has to make sure that relative paths are passed, and this happens automatically if the base is handled correctly, either by using `std::path::Path` to strip prefixes or to assure that there is a trailing slash in the base.
1 parent 3912ee6 commit e4b57b1

File tree

5 files changed

+11
-72
lines changed

5 files changed

+11
-72
lines changed

git-attributes/tests/parse/attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn line_numbers_are_counted_correctly() {
3030
(pattern(r"!foo.html", Mode::NO_SUB_DIR, None), vec![set("x")], 8),
3131
(pattern(r"#a/path", Mode::empty(), None), vec![unset("a")], 10),
3232
(
33-
pattern(r"*", Mode::ABSOLUTE | Mode::NO_SUB_DIR | Mode::ENDS_WITH, Some(0)),
33+
pattern(r"/*", Mode::ABSOLUTE | Mode::NO_SUB_DIR | Mode::ENDS_WITH, Some(1)),
3434
vec![unspecified("b")],
3535
11
3636
),

git-attributes/tests/parse/ignore.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ fn line_numbers_are_counted_correctly() {
2020
("*.[oa]".into(), Mode::NO_SUB_DIR, 2),
2121
("*.html".into(), Mode::NO_SUB_DIR | Mode::ENDS_WITH, 5),
2222
("foo.html".into(), Mode::NO_SUB_DIR | Mode::NEGATIVE, 8),
23-
("*".into(), Mode::NO_SUB_DIR | Mode::ENDS_WITH | Mode::ABSOLUTE, 11),
24-
("foo".into(), Mode::NEGATIVE | Mode::NO_SUB_DIR | Mode::ABSOLUTE, 12),
25-
("foo/*".into(), Mode::ABSOLUTE, 13),
26-
("foo/bar".into(), Mode::ABSOLUTE | Mode::NEGATIVE, 14)
23+
("/*".into(), Mode::NO_SUB_DIR | Mode::ENDS_WITH | Mode::ABSOLUTE, 11),
24+
("/foo".into(), Mode::NEGATIVE | Mode::NO_SUB_DIR | Mode::ABSOLUTE, 12),
25+
("/foo/*".into(), Mode::ABSOLUTE, 13),
26+
("/foo/bar".into(), Mode::ABSOLUTE | Mode::NEGATIVE, 14)
2727
]
2828
);
2929
}

git-glob/src/pattern.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ impl Pattern {
5252
}
5353

5454
/// Match the given `path` which takes slashes (and only slashes) literally, and is relative to the repository root.
55-
/// Note that `path` is assumed to be relative to the repository, and that `base_path` is assumed to contain `path`
56-
/// and is also relative to the repository.
55+
/// Note that `path` is assumed to be relative to the repository.
5756
///
5857
/// We may take various shortcuts which is when `basename_start_pos` and `is_dir` come into play.
5958
/// `basename_start_pos` is the index at which the `path`'s basename starts.
@@ -65,7 +64,6 @@ impl Pattern {
6564
&self,
6665
path: impl Into<&'a BStr>,
6766
basename_start_pos: Option<usize>,
68-
base_path: Option<&BStr>,
6967
is_dir: bool,
7068
case: Case,
7169
) -> bool {
@@ -84,15 +82,7 @@ impl Pattern {
8482
path.rfind_byte(b'/').map(|p| p + 1),
8583
"BUG: invalid cached basename_start_pos provided"
8684
);
87-
debug_assert!(
88-
base_path.map_or(true, |p| p.ends_with(b"/")),
89-
"base must end with a trailing slash"
90-
);
9185
debug_assert!(!path.starts_with(b"/"), "input path must be relative");
92-
debug_assert!(
93-
base_path.map(|base| path.starts_with(base)).unwrap_or(true),
94-
"repo-relative paths must be pre-filtered to match our base."
95-
);
9686

9787
let (text, first_wildcard_pos) = self
9888
.mode
@@ -101,21 +91,12 @@ impl Pattern {
10191
.unwrap_or((self.text.as_bstr(), self.first_wildcard_pos));
10292
if self.mode.contains(pattern::Mode::NO_SUB_DIR) {
10393
let basename = if self.mode.contains(pattern::Mode::ABSOLUTE) {
104-
base_path
105-
.and_then(|base| path.strip_prefix(base.as_ref()).map(|b| b.as_bstr()))
106-
.unwrap_or(path)
94+
path
10795
} else {
10896
&path[basename_start_pos.unwrap_or_default()..]
10997
};
11098
self.matches_inner(text, first_wildcard_pos, basename, flags)
11199
} else {
112-
let path = match base_path {
113-
Some(base) => match path.strip_prefix(base.as_ref()) {
114-
Some(path) => path.as_bstr(),
115-
None => return false,
116-
},
117-
None => path,
118-
};
119100
self.matches_inner(text, first_wildcard_pos, path, flags)
120101
}
121102
}

git-glob/tests/matching/mod.rs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ fn compare_baseline_with_ours() {
7272
pattern.matches_repo_relative_path(
7373
value,
7474
basename_start_pos(value),
75-
None,
7675
false, // TODO: does it make sense to pretend it is a dir and see what happens?
7776
*case,
7877
)
@@ -112,11 +111,11 @@ fn non_dirs_for_must_be_dir_patterns_are_ignored() {
112111
);
113112
let path = "hello";
114113
assert!(
115-
!pattern.matches_repo_relative_path(path, None, None, false /* is-dir */, Case::Sensitive),
114+
!pattern.matches_repo_relative_path(path, None, false /* is-dir */, Case::Sensitive),
116115
"non-dirs never match a dir pattern"
117116
);
118117
assert!(
119-
pattern.matches_repo_relative_path(path, None, None, true /* is-dir */, Case::Sensitive),
118+
pattern.matches_repo_relative_path(path, None, true /* is-dir */, Case::Sensitive),
120119
"dirs can match a dir pattern with the normal rules"
121120
);
122121
}
@@ -146,13 +145,6 @@ fn basename_matches_from_end() {
146145
assert!(!match_file(pat, "barfoo", Case::Sensitive));
147146
}
148147

149-
#[test]
150-
#[should_panic]
151-
fn base_path_must_match_or_panic_occours_in_debug_mode() {
152-
let pat = pat("foo");
153-
assert!(match_file_with_base(&pat, "other/FoO", "base/", Case::Fold));
154-
}
155-
156148
#[test]
157149
fn absolute_basename_matches_only_from_beginning() {
158150
let pat = &pat("/foo");
@@ -161,13 +153,6 @@ fn absolute_basename_matches_only_from_beginning() {
161153
assert!(match_file(pat, "foo", Case::Sensitive));
162154
assert!(!match_file(pat, "Foo", Case::Sensitive));
163155
assert!(!match_file(pat, "bar/foo", Case::Sensitive));
164-
165-
let base = "base/";
166-
assert!(match_file_with_base(pat, "base/FoO", base, Case::Fold));
167-
assert!(!match_file_with_base(pat, "base/bar/Foo", base, Case::Fold));
168-
assert!(match_file_with_base(pat, "base/foo", base, Case::Sensitive));
169-
assert!(!match_file_with_base(pat, "base/Foo", base, Case::Sensitive));
170-
assert!(!match_file_with_base(pat, "base/bar/foo", base, Case::Sensitive));
171156
}
172157

173158
#[test]
@@ -178,13 +163,6 @@ fn absolute_path_matches_only_from_beginning() {
178163
assert!(!match_file(pat, "foo", Case::Sensitive));
179164
assert!(match_file(pat, "bar/foo", Case::Sensitive));
180165
assert!(!match_file(pat, "bar/Foo", Case::Sensitive));
181-
182-
let base = "base/";
183-
assert!(!match_file_with_base(pat, "base/FoO", base, Case::Fold));
184-
assert!(match_file_with_base(pat, "base/bar/Foo", base, Case::Fold));
185-
assert!(!match_file_with_base(pat, "base/foo", base, Case::Sensitive));
186-
assert!(match_file_with_base(pat, "base/bar/foo", base, Case::Sensitive));
187-
assert!(!match_file_with_base(pat, "base/bar/Foo", base, Case::Sensitive));
188166
}
189167

190168
#[test]
@@ -193,22 +171,13 @@ fn absolute_path_with_recursive_glob_detects_mismatches_quickly() {
193171
assert!(!match_file(pat, "FoO", Case::Fold));
194172
assert!(!match_file(pat, "bar/Fooo", Case::Fold));
195173
assert!(!match_file(pat, "baz/bar/Foo", Case::Fold));
196-
197-
let base = "base/";
198-
assert!(!match_file_with_base(pat, "base/FoO", base, Case::Fold));
199-
assert!(!match_file_with_base(pat, "base/bar/Fooo", base, Case::Fold));
200-
assert!(!match_file_with_base(pat, "base/baz/bar/foo", base, Case::Sensitive));
201174
}
202175

203176
#[test]
204177
fn absolute_path_with_recursive_glob_can_do_case_insensitive_prefix_search() {
205178
let pat = &pat("/bar/foo/**");
206179
assert!(!match_file(pat, "bar/Foo/match", Case::Sensitive));
207180
assert!(match_file(pat, "bar/Foo/match", Case::Fold));
208-
209-
let base = "base/";
210-
assert!(!match_file_with_base(pat, "base/bar/Foo/match", base, Case::Sensitive));
211-
assert!(match_file_with_base(pat, "base/bar/Foo/match", base, Case::Fold));
212181
}
213182

214183
#[test]
@@ -303,18 +272,7 @@ fn match_file<'a>(pattern: &git_glob::Pattern, path: impl Into<&'a BStr>, case:
303272

304273
fn match_path<'a>(pattern: &git_glob::Pattern, path: impl Into<&'a BStr>, is_dir: bool, case: Case) -> bool {
305274
let path = path.into();
306-
pattern.matches_repo_relative_path(path, basename_start_pos(path), None, is_dir, case)
307-
}
308-
309-
fn match_file_with_base<'a>(
310-
pattern: &git_glob::Pattern,
311-
path: impl Into<&'a BStr>,
312-
base: impl Into<&'a BStr>,
313-
case: Case,
314-
) -> bool {
315-
let path = path.into();
316-
let base = base.into();
317-
pattern.matches_repo_relative_path(path, basename_start_pos(path), Some(base), false, case)
275+
pattern.matches_repo_relative_path(path, basename_start_pos(path), is_dir, case)
318276
}
319277

320278
fn basename_start_pos(value: &BStr) -> Option<usize> {

git-glob/tests/wildmatch/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl Display for MatchResult {
363363
}
364364

365365
fn match_file_path(pattern: &git_glob::Pattern, path: &str, case: Case) -> bool {
366-
pattern.matches_repo_relative_path(path, basename_of(path), None, false /* is_dir */, case)
366+
pattern.matches_repo_relative_path(path, basename_of(path), false /* is_dir */, case)
367367
}
368368
fn basename_of(path: &str) -> Option<usize> {
369369
path.rfind('/').map(|pos| pos + 1)

0 commit comments

Comments
 (0)