8000 wildmatch: adjust "**" behavior · git/git@40bbee0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40bbee0

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent 164bf83 commit 40bbee0

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

t/t3070-wildmatch.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ match 0 0 foobar 'foo\*bar'
4646
match 1 1 'f\oo' 'f\\oo'
4747
match 1 1 ball '*[al]?'
4848
match 0 0 ten '[ten]'
49-
match 1 1 ten '**[!te]'
49+
match 0 1 ten '**[!te]'
5050
match 0 0 ten '**[!ten]'
5151
match 1 1 ten 't[a-g]n'
5252
match 0 0 ten 't[!a-g]n'
@@ -61,7 +61,8 @@ match 1 1 ']' ']'
6161

6262
# Extended slash-matching features
6363
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'
6566
match 0 0 'foo/bar' 'foo?bar'
6667
match 0 0 'foo/bar' 'foo[/]bar'
6768
match 0 0 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'

wildmatch.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ typedef unsigned char uchar;
2121
#define FALSE 0
2222
#define TRUE 1
2323

24-
#define NOMATCH 1
25-
#define MATCH 0
26-
#define ABORT_ALL -1
27-
#define ABORT_TO_STARSTAR -2
28-
2924
#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
3025
&& *(class) == *(litmatch) \
3126
&& strncmp((char*)class, litmatch, len) == 0)
@@ -90,8 +85,14 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case)
9085
continue;
9186
case '*':
9287
if (*++p == '*') {
88+
const uchar *prev_p = p - 2;
9389
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;
9596
} else
9697
special = FALSE;
9798
if (*p == '\0') {

wildmatch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
/* wildmatch.h */
22

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+
39
int wildmatch(const char *pattern, const char *text, int flags);

0 commit comments

Comments
 (0)
0