Commit 40bbee0a authored by Duy Nguyen's avatar Duy Nguyen Committed by Junio C Hamano

wildmatch: adjust "**" behavior

Standard wildmatch() sees consecutive asterisks as "*" that can also
match slashes. But that may be hard to explain to users as
"abc/**/def" can match "abcdef", "abcxyzdef", "abc/def", "abc/x/def",
"abc/x/y/def"...

This patch changes wildmatch so that users can do

- "**/def" -> all paths ending with file/directory 'def'
- "abc/**" - equivalent to "/abc/"
- "abc/**/def" -> "abc/x/def", "abc/x/y/def"...
- otherwise consider the pattern malformed if "**" is found

Basically the magic of "**" only remains if it's wrapped around by
slashes.
Signed-off-by: Duy Nguyen's avatarNguyễn Thái Ngọc Duy <[email protected]>
Signed-off-by: default avatarJunio C Hamano <[email protected]>
parent 164bf83a
...@@ -46,7 +46,7 @@ match 0 0 foobar 'foo\*bar' ...@@ -46,7 +46,7 @@ match 0 0 foobar 'foo\*bar'
match 1 1 'f\oo' 'f\\oo' match 1 1 'f\oo' 'f\\oo'
match 1 1 ball '*[al]?' match 1 1 ball '*[al]?'
match 0 0 ten '[ten]' match 0 0 ten '[ten]'
match 1 1 ten '**[!te]' match 0 1 ten '**[!te]'
match 0 0 ten '**[!ten]' match 0 0 ten '**[!ten]'
match 1 1 ten 't[a-g]n' match 1 1 ten 't[a-g]n'
match 0 0 ten 't[!a-g]n' match 0 0 ten 't[!a-g]n'
...@@ -61,7 +61,8 @@ match 1 1 ']' ']' ...@@ -61,7 +61,8 @@ match 1 1 ']' ']'
# Extended slash-matching features # Extended slash-matching features
match 0 0 'foo/baz/bar' 'foo*bar' match 0 0 'foo/baz/bar' 'foo*bar'
match 1 0 'foo/baz/bar' 'foo**bar' match 0 0 'foo/baz/bar' 'foo**bar'
match 0 1 'foobazbar' 'foo**bar'
match 0 0 'foo/bar' 'foo?bar' match 0 0 'foo/bar' 'foo?bar'
match 0 0 'foo/bar' 'foo[/]bar' match 0 0 'foo/bar' 'foo[/]bar'
match 0 0 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r' match 0 0 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
......
...@@ -21,11 +21,6 @@ typedef unsigned char uchar; ...@@ -21,11 +21,6 @@ typedef unsigned char uchar;
#define FALSE 0 #define FALSE 0
#define TRUE 1 #define TRUE 1
#define NOMATCH 1
#define MATCH 0
#define ABORT_ALL -1
#define ABORT_TO_STARSTAR -2
#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \ #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
&& *(class) == *(litmatch) \ && *(class) == *(litmatch) \
&& strncmp((char*)class, litmatch, len) == 0) && strncmp((char*)class, litmatch, len) == 0)
...@@ -90,8 +85,14 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case) ...@@ -90,8 +85,14 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
continue; continue;
case '*': case '*':
if (*++p == '*') { if (*++p == '*') {
const uchar *prev_p = p - 2;
while (*++p == '*') {} while (*++p == '*') {}
special = TRUE; if ((prev_p == text || *prev_p == '/') ||
(*p == '\0' || *p == '/' ||
(p[0] == '\\' && p[1] == '/'))) {
special = TRUE;
} else
return ABORT_MALFORMED;
} else } else
special = FALSE; special = FALSE;
if (*p == '\0') { if (*p == '\0') {
......
/* wildmatch.h */ /* wildmatch.h */
#define ABORT_MALFORMED 2
#define NOMATCH 1
#define MATCH 0
#define ABORT_ALL -1
#define ABORT_TO_STARSTAR -2
int wildmatch(const char *pattern, const char *text, int flags); int wildmatch(const char *pattern, const char *text, int flags);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment