Skip to content

Commit

Permalink
wildmatch: fix "**" special case
Browse files Browse the repository at this point in the history
"**" is adjusted to only be effective when surrounded by slashes, in
40bbee0 (wildmatch: adjust "**" behavior - 2012-10-15). Except that
the commit did it wrong:

1. when it checks for "the preceding slash unless ** is at the
   beginning", it compares to wrong pointer. It should have compared
   to the beginning of the pattern, not the text.

2. prev_p points to the character before "**", not the first "*". The
   correct comparison must be "prev_p < pattern" or
   "prev_p + 1 == pattern", not "prev_p == pattern".

3. The pattern must be surrounded by slashes unless it's at the
   beginning or the end of the pattern. We do two checks: one for the
   preceding slash and one the trailing slash. Both checks must be
   met. The use of "||" is wrong.

This patch fixes all above.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Nguyễn Thái Ngọc Duy authored and Junio C Hamano committed Jan 1, 2013
1 parent d5d80e1 commit 3a078de
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion t/t3070-wildmatch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ match 0 0 'deep/foo/bar/baz/' '**/bar/*'
match 1 0 'deep/foo/bar/baz/' '**/bar/**'
match 0 0 'deep/foo/bar' '**/bar/*'
match 1 0 'deep/foo/bar/' '**/bar/**'
match 1 0 'foo/bar/baz' '**/bar**'
match 0 0 'foo/bar/baz' '**/bar**'
match 1 0 'foo/bar/baz/x' '*/bar/**'
match 0 0 'deep/foo/bar/baz/x' '*/bar/**'
match 1 0 'deep/foo/bar/baz/x' '**/bar/*/*'
Expand Down
3 changes: 2 additions & 1 deletion wildmatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef unsigned char uchar;
static int dowild(const uchar *p, const uchar *text, int force_lower_case)
{
uchar p_ch;
const uchar *pattern = p;

for ( ; (p_ch = *p) != '\0'; text++, p++) {
int matched, special;
Expand Down Expand Up @@ -87,7 +88,7 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
if (*++p == '*') {
const uchar *prev_p = p - 2;
while (*++p == '*') {}
if ((prev_p == text || *prev_p == '/') ||
if ((prev_p < pattern || *prev_p == '/') &&
(*p == '\0' || *p == '/' ||
(p[0] == '\\' && p[1] == '/'))) {
/*
Expand Down

0 comments on commit 3a078de

Please sign in to comment.