@matheusbsbs / matheus-bagliano / commits / e0eb889f8e
Fork 0

builtin-grep: wildcard pathspec fixes

This tweaks the pathspec wildcard used in builtin-grep to match that of ls-files. With this: git grep -e DEBUG -- '*/Kconfig*' would work like the shell script version, and you could even do: git grep -e DEBUG --cached -- '*/Kconfig*' ;# from index git grep -e DEBUG v2.6.12 -- '*/Kconfig*' ;# from rev Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 1, 2006 at 12:27 UTC e0eb889f8e6597120527b0312874ff4c8448108e
1 file changed +62 -23
builtin-grep.c
+62 -23
@@ -12,33 +12,66 @@
12 #include "revision.h"
13 #include "builtin.h"
14 #include <regex.h>
15 +#include <fnmatch.h>
16
17 +/*
18 + * git grep pathspecs are somewhat different from diff-tree pathspecs;
19 + * pathname wildcards are allowed.
20 + */
21 static int pathspec_matches(struct diff_options *opt, const char *name)
22 {
18 - int i, j;
19 - int namelen;
23 + int namelen, i;
24 if (!opt->nr_paths)
25 return 1;
26 namelen = strlen(name);
27 for (i = 0; i < opt->nr_paths; i++) {
28 const char *match = opt->paths[i];
29 int matchlen = opt->pathlens[i];
26 - if (matchlen <= namelen) {
27 - if (!strncmp(name, match, matchlen))
28 - return 1;
30 + const char *slash, *cp;
31 +
32 + if ((matchlen <= namelen) &&
33 + !strncmp(name, match, matchlen) &&
34 + (match[matchlen-1] == '/' ||
35 + name[matchlen] == '\0' || name[matchlen] == '/'))
36 + return 1;
37 + if (!fnmatch(match, name, 0))
38 + return 1;
39 + if (name[namelen-1] != '/')
40 continue;
30 - }
31 - /* If name is "Documentation" and pathspec is
32 - * "Documentation/", they should match. Maybe
33 - * we would want to strip it in get_pathspec()???
41 +
42 + /* We are being asked if the name directory is worth
43 + * descending into.
44 + *
45 + * Find the longest leading directory name that does
46 + * not have metacharacter in the pathspec; the name
47 + * we are looking at must overlap with that directory.
48 */
35 - if (strncmp(name, match, namelen))
36 - continue;
37 - for (j = namelen; j < matchlen; j++)
38 - if (match[j] != '/')
49 + for (cp = match, slash = NULL; cp - match < matchlen; cp++) {
50 + char ch = *cp;
51 + if (ch == '/')
52 + slash = cp;
53 + if (ch == '*' || ch == '[')
54 break;
40 - if (matchlen <= j)
41 - return 1;
55 + }
56 + if (!slash)
57 + slash = match; /* toplevel */
58 + else
59 + slash++;
60 + if (namelen <= slash - match) {
61 + /* Looking at "Documentation/" and
62 + * the pattern says "Documentation/howto/", or
63 + * "Documentation/diff*.txt".
64 + */
65 + if (!memcmp(match, name, namelen))
66 + return 1;
67 + }
68 + else {
69 + /* Looking at "Documentation/howto/" and
70 + * the pattern says "Documentation/h*".
71 + */
72 + if (!memcmp(match, name, slash - match))
73 + return 1;
74 + }
75 }
76 return 0;
77 }
@@ -232,17 +265,17 @@ static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
265 int hit = 0;
266 const char *path;
267 const unsigned char *sha1;
235 - char *down_base;
268 + char *down;
269 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
270
271 if (tree_name[0]) {
272 int offset = sprintf(path_buf, "%s:", tree_name);
240 - down_base = path_buf + offset;
241 - strcat(down_base, base);
273 + down = path_buf + offset;
274 + strcat(down, base);
275 }
276 else {
244 - down_base = path_buf;
245 - strcpy(down_base, base);
277 + down = path_buf;
278 + strcpy(down, base);
279 }
280 len = strlen(path_buf);
281
@@ -252,7 +285,14 @@ static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
285 pathlen = strlen(path);
286 strcpy(path_buf + len, path);
287
255 - if (!pathspec_matches(&revs->diffopt, down_base))
288 + if (S_ISDIR(mode))
289 + /* Match "abc/" against pathspec to
290 + * decide if we want to descend into "abc"
291 + * directory.
292 + */
293 + strcpy(path_buf + len + pathlen, "/");
294 +
295 + if (!pathspec_matches(&revs->diffopt, down))
296 ;
297 else if (S_ISREG(mode))
298 hit |= grep_sha1(opt, sha1, path_buf);
@@ -264,9 +304,8 @@ static int grep_tree(struct grep_opt *opt, struct rev_info *revs,
304 if (!data)
305 die("unable to read tree (%s)",
306 sha1_to_hex(sha1));
267 - strcpy(path_buf + len + pathlen, "/");
307 sub.buf = data;
269 - hit = grep_tree(opt, revs, &sub, tree_name, down_base);
308 + hit |= grep_tree(opt, revs, &sub, tree_name, down);
309 free(data);
310 }
311 update_tree_entry(tree);