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: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Oct 15, 2012 at 13:26 UTC 40bbee0ab07d0ee4f21b11d597c878245c1b05a6
3 files changed +16 -8
t/t3070-wildmatch.sh
+3 -2
@@ -46,7 +46,7 @@ match 0 0 foobar 'foo\*bar'
46 match 1 1 'f\oo' 'f\\oo'
47 match 1 1 ball '*[al]?'
48 match 0 0 ten '[ten]'
49 -match 1 1 ten '**[!te]'
49 +match 0 1 ten '**[!te]'
50 match 0 0 ten '**[!ten]'
51 match 1 1 ten 't[a-g]n'
52 match 0 0 ten 't[!a-g]n'
@@ -61,7 +61,8 @@ match 1 1 ']' ']'
61
62 # Extended slash-matching features
63 match 0 0 'foo/baz/bar' 'foo*bar'
64 -match 1 0 'foo/baz/bar' 'foo**bar'
64 +match 0 0 'foo/baz/bar' 'foo**bar'
65 +match 0 1 'foobazbar' 'foo**bar'
66 match 0 0 'foo/bar' 'foo?bar'
67 match 0 0 'foo/bar' 'foo[/]bar'
68 match 0 0 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
wildmatch.c
+7 -6
@@ -21,11 +21,6 @@ typedef unsigned char uchar;
21 #define FALSE 0
22 #define TRUE 1
23
24 -#define NOMATCH 1
25 -#define MATCH 0
26 -#define ABORT_ALL -1
27 -#define ABORT_TO_STARSTAR -2
28 -
24 #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
25 && *(class) == *(litmatch) \
26 && strncmp((char*)class, litmatch, len) == 0)
@@ -90,8 +85,14 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
85 continue;
86 case '*':
87 if (*++p == '*') {
88 + const uchar *prev_p = p - 2;
89 while (*++p == '*') {}
94 - special = TRUE;
90 + if ((prev_p == text || *prev_p == '/') ||
91 + (*p == '\0' || *p == '/' ||
92 + (p[0] == '\\' && p[1] == '/'))) {
93 + special = TRUE;
94 + } else
95 + return ABORT_MALFORMED;
96 } else
97 special = FALSE;
98 if (*p == '\0') {
wildmatch.h
+6
@@ -1,3 +1,9 @@
1 /* wildmatch.h */
2
3 +#define ABORT_MALFORMED 2
4 +#define NOMATCH 1
5 +#define MATCH 0
6 +#define ABORT_ALL -1
7 +#define ABORT_TO_STARSTAR -2
8 +
9 int wildmatch(const char *pattern, const char *text, int flags);