Skip to content

Commit

Permalink
get to the point where globs probably should have a base (#301)
Browse files Browse the repository at this point in the history
That way, we can assume to get repo-relative paths all the time
and skip stripping the base unless it's truly required.
  • Loading branch information
Byron committed Apr 12, 2022
1 parent f2f3f53 commit 2632988
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 6 additions & 3 deletions git-glob/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ bitflags! {
const SLASH_IS_LITERAL = 1 << 0;
/// Match case insensitively for ascii characters only.
const IGNORE_CASE = 1 << 1;
// TODO: Patterns match from the beginning only.
}
}

Expand Down Expand Up @@ -75,7 +74,11 @@ impl Pattern {
let path = path.into();

if self.mode.contains(pattern::Mode::NO_SUB_DIR) {
let basename = &path[basename_start_pos.unwrap_or_default()..];
let basename = if self.mode.contains(pattern::Mode::ABSOLUTE) {
path
} else {
&path[basename_start_pos.unwrap_or_default()..]
};
self.matches(basename, flags)
} else {
// TODO
Expand All @@ -85,7 +88,7 @@ impl Pattern {

pub fn matches(&self, value: &BStr, options: MatchOptions) -> bool {
match self.first_wildcard_pos {
// "*literal" case
// "*literal" case, overrides starts-with
Some(pos) if self.mode.contains(pattern::Mode::ENDS_WITH) => {
let text = &self.text[pos + 1..];
if options.contains(MatchOptions::IGNORE_CASE) {
Expand Down
20 changes: 17 additions & 3 deletions git-glob/tests/matching/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ fn basename_case_insensitive() {
}

#[test]
#[ignore]
fn absolute_basename_matches_only_from_beginning() {
let pattern = "/foo";
assert!(match_file(pattern, "FoO", Case::Fold));
Expand All @@ -118,6 +117,16 @@ fn absolute_basename_matches_only_from_beginning() {
assert!(!match_file(pattern, "bar/foo", Case::Sensitive));
}

#[test]
#[ignore]
fn absolute_path_matches_only_from_beginning() {
let pattern = "/bar/foo";
assert!(!match_file(pattern, "FoO", Case::Fold));
assert!(match_file(pattern, "bar/Foo", Case::Fold));
assert!(!match_file(pattern, "foo", Case::Sensitive));
assert!(match_file(pattern, "bar/foo", Case::Sensitive));
}

#[test]
fn basename_glob_and_literal_is_ends_with() {
let pattern = "*foo";
Expand All @@ -128,12 +137,14 @@ fn basename_glob_and_literal_is_ends_with() {
assert!(!match_file(pattern, "BarFoo", Case::Sensitive));
assert!(match_file(pattern, "barfoo", Case::Sensitive));
assert!(!match_file(pattern, "barfooo", Case::Sensitive));

assert!(match_file(pattern, "bar/foo", Case::Sensitive));
assert!(match_file(pattern, "bar/bazfoo", Case::Sensitive));
}

#[test]
#[ignore]
fn absolute_basename_glob_and_literal_is_ends_with() {
let _pattern = pat("/*foo");
let pattern = "/*foo";

assert!(match_file(pattern, "FoO", Case::Fold));
assert!(match_file(pattern, "BarFoO", Case::Fold));
Expand All @@ -142,6 +153,9 @@ fn absolute_basename_glob_and_literal_is_ends_with() {
assert!(!match_file(pattern, "BarFoo", Case::Sensitive));
assert!(match_file(pattern, "barfoo", Case::Sensitive));
assert!(!match_file(pattern, "barfooo", Case::Sensitive));

assert!(match_file(pattern, "bar/foo", Case::Sensitive));
assert!(match_file(pattern, "bar/bazfoo", Case::Sensitive));
}

#[test]
Expand Down

0 comments on commit 2632988

Please sign in to comment.