builtin-grep: tighten path wildcard vs tree traversal.
The earlier code descended into Documentation/technical when given "Documentation/how*" as the pattern, which was too loose. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 2, 2006 at 17:27 UTC
1e3d90e0135274ad89cd8ee0722e2dd043ec0052
1 file changed
+20
-15
builtin-grep.c
+20
-15
@@ -26,7 +26,7 @@ static int pathspec_matches(const char **paths, const char *name)
26
for (i = 0; paths[i]; i++) {
27
const char *match = paths[i];
28
int matchlen = strlen(match);
29
- const char *slash, *cp;
29
+ const char *cp, *meta;
30
31
if ((matchlen <= namelen) &&
32
!strncmp(name, match, matchlen) &&
@@ -38,38 +38,43 @@ static int pathspec_matches(const char **paths, const char *name)
38
if (name[namelen-1] != '/')
39
continue;
40
41
- /* We are being asked if the name directory is worth
41
+ /* We are being asked if the directory ("name") is worth
42
* descending into.
43
*
44
* Find the longest leading directory name that does
45
* not have metacharacter in the pathspec; the name
46
* we are looking at must overlap with that directory.
47
*/
48
- for (cp = match, slash = NULL; cp - match < matchlen; cp++) {
48
+ for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
49
char ch = *cp;
50
- if (ch == '/')
51
- slash = cp;
52
- if (ch == '*' || ch == '[')
50
+ if (ch == '*' || ch == '[' || ch == '?') {
51
+ meta = cp;
52
break;
53
+ }
54
}
55
- if (!slash)
56
- slash = match; /* toplevel */
57
- else
58
- slash++;
59
- if (namelen <= slash - match) {
55
+ if (!meta)
56
+ meta = cp; /* fully literal */
57
+
58
+ if (namelen <= meta - match) {
59
/* Looking at "Documentation/" and
60
* the pattern says "Documentation/howto/", or
62
- * "Documentation/diff*.txt".
61
+ * "Documentation/diff*.txt". The name we
62
+ * have should match prefix.
63
*/
64
if (!memcmp(match, name, namelen))
65
return 1;
66
+ continue;
67
}
67
- else {
68
+
69
+ if (meta - match < namelen) {
70
/* Looking at "Documentation/howto/" and
69
- * the pattern says "Documentation/h*".
71
+ * the pattern says "Documentation/h*";
72
+ * match up to "Do.../h"; this avoids descending
73
+ * into "Documentation/technical/".
74
*/
71
- if (!memcmp(match, name, slash - match))
75
+ if (!memcmp(match, name, meta - match))
76
return 1;
77
+ continue;
78
}
79
}
80
return 0;