]>
Commit | Line | Data |
---|---|---|
5010cb5f JH |
1 | /* |
2 | * Builtin "git grep" | |
3 | * | |
4 | * Copyright (c) 2006 Junio C Hamano | |
5 | */ | |
6 | #include "cache.h" | |
7 | #include "blob.h" | |
8 | #include "tree.h" | |
9 | #include "commit.h" | |
10 | #include "tag.h" | |
1362671f | 11 | #include "tree-walk.h" |
5010cb5f JH |
12 | #include "builtin.h" |
13 | #include <regex.h> | |
e0eb889f | 14 | #include <fnmatch.h> |
1e2398d7 | 15 | #include <sys/wait.h> |
5010cb5f | 16 | |
e0eb889f JH |
17 | /* |
18 | * git grep pathspecs are somewhat different from diff-tree pathspecs; | |
19 | * pathname wildcards are allowed. | |
20 | */ | |
1362671f | 21 | static int pathspec_matches(const char **paths, const char *name) |
5010cb5f | 22 | { |
e0eb889f | 23 | int namelen, i; |
1362671f | 24 | if (!paths || !*paths) |
5010cb5f JH |
25 | return 1; |
26 | namelen = strlen(name); | |
1362671f JH |
27 | for (i = 0; paths[i]; i++) { |
28 | const char *match = paths[i]; | |
29 | int matchlen = strlen(match); | |
1e3d90e0 | 30 | const char *cp, *meta; |
e0eb889f | 31 | |
bb9e15a8 UKK |
32 | if (!matchlen || |
33 | ((matchlen <= namelen) && | |
34 | !strncmp(name, match, matchlen) && | |
35 | (match[matchlen-1] == '/' || | |
36 | name[matchlen] == '\0' || name[matchlen] == '/'))) | |
e0eb889f JH |
37 | return 1; |
38 | if (!fnmatch(match, name, 0)) | |
39 | return 1; | |
40 | if (name[namelen-1] != '/') | |
5010cb5f | 41 | continue; |
e0eb889f | 42 | |
1e3d90e0 | 43 | /* We are being asked if the directory ("name") is worth |
e0eb889f JH |
44 | * descending into. |
45 | * | |
46 | * Find the longest leading directory name that does | |
47 | * not have metacharacter in the pathspec; the name | |
48 | * we are looking at must overlap with that directory. | |
5010cb5f | 49 | */ |
1e3d90e0 | 50 | for (cp = match, meta = NULL; cp - match < matchlen; cp++) { |
e0eb889f | 51 | char ch = *cp; |
1e3d90e0 JH |
52 | if (ch == '*' || ch == '[' || ch == '?') { |
53 | meta = cp; | |
5010cb5f | 54 | break; |
1e3d90e0 | 55 | } |
e0eb889f | 56 | } |
1e3d90e0 JH |
57 | if (!meta) |
58 | meta = cp; /* fully literal */ | |
59 | ||
60 | if (namelen <= meta - match) { | |
e0eb889f JH |
61 | /* Looking at "Documentation/" and |
62 | * the pattern says "Documentation/howto/", or | |
1e3d90e0 JH |
63 | * "Documentation/diff*.txt". The name we |
64 | * have should match prefix. | |
e0eb889f JH |
65 | */ |
66 | if (!memcmp(match, name, namelen)) | |
67 | return 1; | |
1e3d90e0 | 68 | continue; |
e0eb889f | 69 | } |
1e3d90e0 JH |
70 | |
71 | if (meta - match < namelen) { | |
e0eb889f | 72 | /* Looking at "Documentation/howto/" and |
1e3d90e0 JH |
73 | * the pattern says "Documentation/h*"; |
74 | * match up to "Do.../h"; this avoids descending | |
75 | * into "Documentation/technical/". | |
e0eb889f | 76 | */ |
1e3d90e0 | 77 | if (!memcmp(match, name, meta - match)) |
e0eb889f | 78 | return 1; |
1e3d90e0 | 79 | continue; |
e0eb889f | 80 | } |
5010cb5f JH |
81 | } |
82 | return 0; | |
83 | } | |
84 | ||
f9b9faf6 JH |
85 | struct grep_pat { |
86 | struct grep_pat *next; | |
aa8c79ad JH |
87 | const char *origin; |
88 | int no; | |
5010cb5f JH |
89 | const char *pattern; |
90 | regex_t regexp; | |
f9b9faf6 JH |
91 | }; |
92 | ||
93 | struct grep_opt { | |
94 | struct grep_pat *pattern_list; | |
95 | struct grep_pat **pattern_tail; | |
96 | regex_t regexp; | |
5010cb5f JH |
97 | unsigned linenum:1; |
98 | unsigned invert:1; | |
df0e7aa8 | 99 | unsigned name_only:1; |
e23d2d6b | 100 | unsigned unmatch_name_only:1; |
2c866cf1 | 101 | unsigned count:1; |
7839a25e | 102 | unsigned word_regexp:1; |
07ea91d8 | 103 | unsigned fixed:1; |
b8d0f5a0 JH |
104 | #define GREP_BINARY_DEFAULT 0 |
105 | #define GREP_BINARY_NOMATCH 1 | |
106 | #define GREP_BINARY_TEXT 2 | |
107 | unsigned binary:2; | |
5010cb5f JH |
108 | int regflags; |
109 | unsigned pre_context; | |
110 | unsigned post_context; | |
111 | }; | |
112 | ||
aa8c79ad JH |
113 | static void add_pattern(struct grep_opt *opt, const char *pat, |
114 | const char *origin, int no) | |
f9b9faf6 JH |
115 | { |
116 | struct grep_pat *p = xcalloc(1, sizeof(*p)); | |
117 | p->pattern = pat; | |
aa8c79ad JH |
118 | p->origin = origin; |
119 | p->no = no; | |
f9b9faf6 JH |
120 | *opt->pattern_tail = p; |
121 | opt->pattern_tail = &p->next; | |
122 | p->next = NULL; | |
123 | } | |
124 | ||
125 | static void compile_patterns(struct grep_opt *opt) | |
126 | { | |
127 | struct grep_pat *p; | |
128 | for (p = opt->pattern_list; p; p = p->next) { | |
129 | int err = regcomp(&p->regexp, p->pattern, opt->regflags); | |
130 | if (err) { | |
131 | char errbuf[1024]; | |
aa8c79ad JH |
132 | char where[1024]; |
133 | if (p->no) | |
134 | sprintf(where, "In '%s' at %d, ", | |
135 | p->origin, p->no); | |
136 | else if (p->origin) | |
137 | sprintf(where, "%s, ", p->origin); | |
138 | else | |
139 | where[0] = 0; | |
f9b9faf6 JH |
140 | regerror(err, &p->regexp, errbuf, 1024); |
141 | regfree(&p->regexp); | |
aa8c79ad | 142 | die("%s'%s': %s", where, p->pattern, errbuf); |
f9b9faf6 JH |
143 | } |
144 | } | |
145 | } | |
146 | ||
5010cb5f JH |
147 | static char *end_of_line(char *cp, unsigned long *left) |
148 | { | |
149 | unsigned long l = *left; | |
150 | while (l && *cp != '\n') { | |
151 | l--; | |
152 | cp++; | |
153 | } | |
154 | *left = l; | |
155 | return cp; | |
156 | } | |
157 | ||
7839a25e JH |
158 | static int word_char(char ch) |
159 | { | |
160 | return isalnum(ch) || ch == '_'; | |
161 | } | |
162 | ||
5010cb5f JH |
163 | static void show_line(struct grep_opt *opt, const char *bol, const char *eol, |
164 | const char *name, unsigned lno, char sign) | |
165 | { | |
166 | printf("%s%c", name, sign); | |
167 | if (opt->linenum) | |
168 | printf("%d%c", lno, sign); | |
a24f1e25 | 169 | printf("%.*s\n", (int)(eol-bol), bol); |
5010cb5f JH |
170 | } |
171 | ||
b8d0f5a0 JH |
172 | /* |
173 | * NEEDSWORK: share code with diff.c | |
174 | */ | |
175 | #define FIRST_FEW_BYTES 8000 | |
176 | static int buffer_is_binary(const char *ptr, unsigned long size) | |
177 | { | |
178 | if (FIRST_FEW_BYTES < size) | |
179 | size = FIRST_FEW_BYTES; | |
180 | if (memchr(ptr, 0, size)) | |
181 | return 1; | |
182 | return 0; | |
183 | } | |
184 | ||
07ea91d8 JH |
185 | static int fixmatch(const char *pattern, char *line, regmatch_t *match) |
186 | { | |
187 | char *hit = strstr(line, pattern); | |
188 | if (!hit) { | |
189 | match->rm_so = match->rm_eo = -1; | |
190 | return REG_NOMATCH; | |
191 | } | |
192 | else { | |
193 | match->rm_so = hit - line; | |
194 | match->rm_eo = match->rm_so + strlen(pattern); | |
195 | return 0; | |
196 | } | |
197 | } | |
198 | ||
5010cb5f JH |
199 | static int grep_buffer(struct grep_opt *opt, const char *name, |
200 | char *buf, unsigned long size) | |
201 | { | |
202 | char *bol = buf; | |
203 | unsigned long left = size; | |
204 | unsigned lno = 1; | |
205 | struct pre_context_line { | |
206 | char *bol; | |
207 | char *eol; | |
208 | } *prev = NULL, *pcl; | |
209 | unsigned last_hit = 0; | |
210 | unsigned last_shown = 0; | |
b8d0f5a0 | 211 | int binary_match_only = 0; |
5010cb5f | 212 | const char *hunk_mark = ""; |
2c866cf1 | 213 | unsigned count = 0; |
5010cb5f | 214 | |
b8d0f5a0 JH |
215 | if (buffer_is_binary(buf, size)) { |
216 | switch (opt->binary) { | |
217 | case GREP_BINARY_DEFAULT: | |
218 | binary_match_only = 1; | |
219 | break; | |
220 | case GREP_BINARY_NOMATCH: | |
221 | return 0; /* Assume unmatch */ | |
222 | break; | |
223 | default: | |
224 | break; | |
225 | } | |
226 | } | |
227 | ||
5010cb5f JH |
228 | if (opt->pre_context) |
229 | prev = xcalloc(opt->pre_context, sizeof(*prev)); | |
230 | if (opt->pre_context || opt->post_context) | |
231 | hunk_mark = "--\n"; | |
232 | ||
233 | while (left) { | |
234 | regmatch_t pmatch[10]; | |
235 | char *eol, ch; | |
f9b9faf6 JH |
236 | int hit = 0; |
237 | struct grep_pat *p; | |
5010cb5f JH |
238 | |
239 | eol = end_of_line(bol, &left); | |
240 | ch = *eol; | |
241 | *eol = 0; | |
242 | ||
f9b9faf6 | 243 | for (p = opt->pattern_list; p; p = p->next) { |
07ea91d8 JH |
244 | if (!opt->fixed) { |
245 | regex_t *exp = &p->regexp; | |
246 | hit = !regexec(exp, bol, ARRAY_SIZE(pmatch), | |
247 | pmatch, 0); | |
248 | } | |
249 | else { | |
250 | hit = !fixmatch(p->pattern, bol, pmatch); | |
251 | } | |
7839a25e JH |
252 | |
253 | if (hit && opt->word_regexp) { | |
254 | /* Match beginning must be either | |
255 | * beginning of the line, or at word | |
256 | * boundary (i.e. the last char must | |
257 | * not be alnum or underscore). | |
258 | */ | |
259 | if ((pmatch[0].rm_so < 0) || | |
260 | (eol - bol) <= pmatch[0].rm_so || | |
261 | (pmatch[0].rm_eo < 0) || | |
262 | (eol - bol) < pmatch[0].rm_eo) | |
263 | die("regexp returned nonsense"); | |
264 | if (pmatch[0].rm_so != 0 && | |
265 | word_char(bol[pmatch[0].rm_so-1])) | |
02ab1c49 JH |
266 | hit = 0; |
267 | if (pmatch[0].rm_eo != (eol-bol) && | |
7839a25e | 268 | word_char(bol[pmatch[0].rm_eo])) |
02ab1c49 | 269 | hit = 0; |
7839a25e | 270 | } |
f9b9faf6 JH |
271 | if (hit) |
272 | break; | |
273 | } | |
274 | /* "grep -v -e foo -e bla" should list lines | |
275 | * that do not have either, so inversion should | |
276 | * be done outside. | |
277 | */ | |
5010cb5f JH |
278 | if (opt->invert) |
279 | hit = !hit; | |
e23d2d6b JH |
280 | if (opt->unmatch_name_only) { |
281 | if (hit) | |
282 | return 0; | |
283 | goto next_line; | |
284 | } | |
5010cb5f | 285 | if (hit) { |
2c866cf1 | 286 | count++; |
b8d0f5a0 JH |
287 | if (binary_match_only) { |
288 | printf("Binary file %s matches\n", name); | |
289 | return 1; | |
290 | } | |
df0e7aa8 JH |
291 | if (opt->name_only) { |
292 | printf("%s\n", name); | |
293 | return 1; | |
294 | } | |
5010cb5f JH |
295 | /* Hit at this line. If we haven't shown the |
296 | * pre-context lines, we would need to show them. | |
2c866cf1 JH |
297 | * When asked to do "count", this still show |
298 | * the context which is nonsense, but the user | |
299 | * deserves to get that ;-). | |
5010cb5f JH |
300 | */ |
301 | if (opt->pre_context) { | |
302 | unsigned from; | |
303 | if (opt->pre_context < lno) | |
304 | from = lno - opt->pre_context; | |
305 | else | |
306 | from = 1; | |
307 | if (from <= last_shown) | |
308 | from = last_shown + 1; | |
309 | if (last_shown && from != last_shown + 1) | |
310 | printf(hunk_mark); | |
311 | while (from < lno) { | |
312 | pcl = &prev[lno-from-1]; | |
313 | show_line(opt, pcl->bol, pcl->eol, | |
314 | name, from, '-'); | |
315 | from++; | |
316 | } | |
317 | last_shown = lno-1; | |
318 | } | |
319 | if (last_shown && lno != last_shown + 1) | |
320 | printf(hunk_mark); | |
2c866cf1 JH |
321 | if (!opt->count) |
322 | show_line(opt, bol, eol, name, lno, ':'); | |
5010cb5f JH |
323 | last_shown = last_hit = lno; |
324 | } | |
325 | else if (last_hit && | |
326 | lno <= last_hit + opt->post_context) { | |
327 | /* If the last hit is within the post context, | |
328 | * we need to show this line. | |
329 | */ | |
330 | if (last_shown && lno != last_shown + 1) | |
331 | printf(hunk_mark); | |
332 | show_line(opt, bol, eol, name, lno, '-'); | |
333 | last_shown = lno; | |
334 | } | |
335 | if (opt->pre_context) { | |
336 | memmove(prev+1, prev, | |
337 | (opt->pre_context-1) * sizeof(*prev)); | |
338 | prev->bol = bol; | |
339 | prev->eol = eol; | |
340 | } | |
e23d2d6b JH |
341 | |
342 | next_line: | |
5010cb5f JH |
343 | *eol = ch; |
344 | bol = eol + 1; | |
7ed36f56 JH |
345 | if (!left) |
346 | break; | |
5010cb5f JH |
347 | left--; |
348 | lno++; | |
349 | } | |
e23d2d6b JH |
350 | |
351 | if (opt->unmatch_name_only) { | |
352 | /* We did not see any hit, so we want to show this */ | |
353 | printf("%s\n", name); | |
354 | return 1; | |
355 | } | |
356 | ||
2c866cf1 JH |
357 | /* NEEDSWORK: |
358 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, | |
359 | * which feels mostly useless but sometimes useful. Maybe | |
360 | * make it another option? For now suppress them. | |
361 | */ | |
362 | if (opt->count && count) | |
363 | printf("%s:%u\n", name, count); | |
5010cb5f JH |
364 | return !!last_hit; |
365 | } | |
366 | ||
367 | static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name) | |
368 | { | |
369 | unsigned long size; | |
370 | char *data; | |
371 | char type[20]; | |
372 | int hit; | |
373 | data = read_sha1_file(sha1, type, &size); | |
374 | if (!data) { | |
375 | error("'%s': unable to read %s", name, sha1_to_hex(sha1)); | |
376 | return 0; | |
377 | } | |
378 | hit = grep_buffer(opt, name, data, size); | |
379 | free(data); | |
380 | return hit; | |
381 | } | |
382 | ||
383 | static int grep_file(struct grep_opt *opt, const char *filename) | |
384 | { | |
385 | struct stat st; | |
386 | int i; | |
387 | char *data; | |
388 | if (lstat(filename, &st) < 0) { | |
389 | err_ret: | |
390 | if (errno != ENOENT) | |
391 | error("'%s': %s", filename, strerror(errno)); | |
392 | return 0; | |
393 | } | |
394 | if (!st.st_size) | |
395 | return 0; /* empty file -- no grep hit */ | |
396 | if (!S_ISREG(st.st_mode)) | |
397 | return 0; | |
398 | i = open(filename, O_RDONLY); | |
399 | if (i < 0) | |
400 | goto err_ret; | |
401 | data = xmalloc(st.st_size + 1); | |
402 | if (st.st_size != xread(i, data, st.st_size)) { | |
403 | error("'%s': short read %s", filename, strerror(errno)); | |
404 | close(i); | |
405 | free(data); | |
406 | return 0; | |
407 | } | |
408 | close(i); | |
409 | i = grep_buffer(opt, filename, data, st.st_size); | |
410 | free(data); | |
411 | return i; | |
412 | } | |
413 | ||
1e2398d7 LT |
414 | static int exec_grep(int argc, const char **argv) |
415 | { | |
416 | pid_t pid; | |
417 | int status; | |
418 | ||
419 | argv[argc] = NULL; | |
420 | pid = fork(); | |
421 | if (pid < 0) | |
422 | return pid; | |
423 | if (!pid) { | |
424 | execvp("grep", (char **) argv); | |
425 | exit(255); | |
426 | } | |
427 | while (waitpid(pid, &status, 0) < 0) { | |
428 | if (errno == EINTR) | |
429 | continue; | |
430 | return -1; | |
431 | } | |
432 | if (WIFEXITED(status)) { | |
433 | if (!WEXITSTATUS(status)) | |
434 | return 1; | |
435 | return 0; | |
436 | } | |
437 | return -1; | |
438 | } | |
439 | ||
440 | #define MAXARGS 1000 | |
ffa0a7ab JH |
441 | #define ARGBUF 4096 |
442 | #define push_arg(a) do { \ | |
443 | if (nr < MAXARGS) argv[nr++] = (a); \ | |
444 | else die("maximum number of args exceeded"); \ | |
445 | } while (0) | |
1e2398d7 LT |
446 | |
447 | static int external_grep(struct grep_opt *opt, const char **paths, int cached) | |
448 | { | |
fcfe34b5 | 449 | int i, nr, argc, hit, len, status; |
1e2398d7 | 450 | const char *argv[MAXARGS+1]; |
ffa0a7ab JH |
451 | char randarg[ARGBUF]; |
452 | char *argptr = randarg; | |
1e2398d7 LT |
453 | struct grep_pat *p; |
454 | ||
ffa0a7ab JH |
455 | len = nr = 0; |
456 | push_arg("grep"); | |
ffa0a7ab | 457 | if (opt->fixed) |
f6647519 | 458 | push_arg("-F"); |
ffa0a7ab JH |
459 | if (opt->linenum) |
460 | push_arg("-n"); | |
461 | if (opt->regflags & REG_EXTENDED) | |
462 | push_arg("-E"); | |
3026402c RF |
463 | if (opt->regflags & REG_ICASE) |
464 | push_arg("-i"); | |
1e2398d7 | 465 | if (opt->word_regexp) |
ffa0a7ab | 466 | push_arg("-w"); |
1e2398d7 | 467 | if (opt->name_only) |
ffa0a7ab JH |
468 | push_arg("-l"); |
469 | if (opt->unmatch_name_only) | |
470 | push_arg("-L"); | |
471 | if (opt->count) | |
472 | push_arg("-c"); | |
473 | if (opt->post_context || opt->pre_context) { | |
474 | if (opt->post_context != opt->pre_context) { | |
475 | if (opt->pre_context) { | |
476 | push_arg("-B"); | |
477 | len += snprintf(argptr, sizeof(randarg)-len, | |
478 | "%u", opt->pre_context); | |
479 | if (sizeof(randarg) <= len) | |
480 | die("maximum length of args exceeded"); | |
481 | push_arg(argptr); | |
482 | argptr += len; | |
483 | } | |
484 | if (opt->post_context) { | |
485 | push_arg("-A"); | |
486 | len += snprintf(argptr, sizeof(randarg)-len, | |
487 | "%u", opt->post_context); | |
488 | if (sizeof(randarg) <= len) | |
489 | die("maximum length of args exceeded"); | |
490 | push_arg(argptr); | |
491 | argptr += len; | |
492 | } | |
493 | } | |
494 | else { | |
495 | push_arg("-C"); | |
496 | len += snprintf(argptr, sizeof(randarg)-len, | |
497 | "%u", opt->post_context); | |
498 | if (sizeof(randarg) <= len) | |
499 | die("maximum length of args exceeded"); | |
500 | push_arg(argptr); | |
501 | argptr += len; | |
502 | } | |
503 | } | |
1e2398d7 | 504 | for (p = opt->pattern_list; p; p = p->next) { |
ffa0a7ab JH |
505 | push_arg("-e"); |
506 | push_arg(p->pattern); | |
1e2398d7 | 507 | } |
bbb66c60 LT |
508 | |
509 | /* | |
510 | * To make sure we get the header printed out when we want it, | |
511 | * add /dev/null to the paths to grep. This is unnecessary | |
512 | * (and wrong) with "-l" or "-L", which always print out the | |
513 | * name anyway. | |
514 | * | |
515 | * GNU grep has "-H", but this is portable. | |
516 | */ | |
517 | if (!opt->name_only && !opt->unmatch_name_only) | |
518 | push_arg("/dev/null"); | |
1e2398d7 LT |
519 | |
520 | hit = 0; | |
521 | argc = nr; | |
522 | for (i = 0; i < active_nr; i++) { | |
523 | struct cache_entry *ce = active_cache[i]; | |
fbd01abf | 524 | char *name; |
1e2398d7 LT |
525 | if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode))) |
526 | continue; | |
527 | if (!pathspec_matches(paths, ce->name)) | |
528 | continue; | |
bbb66c60 LT |
529 | name = ce->name; |
530 | if (name[0] == '-') { | |
531 | int len = ce_namelen(ce); | |
532 | name = xmalloc(len + 3); | |
533 | memcpy(name, "./", 2); | |
534 | memcpy(name + 2, ce->name, len + 1); | |
535 | } | |
536 | argv[argc++] = name; | |
1e2398d7 LT |
537 | if (argc < MAXARGS) |
538 | continue; | |
fcfe34b5 JH |
539 | status = exec_grep(argc, argv); |
540 | if (0 < status) | |
541 | hit = 1; | |
1e2398d7 LT |
542 | argc = nr; |
543 | } | |
fcfe34b5 JH |
544 | if (argc > nr) { |
545 | status = exec_grep(argc, argv); | |
546 | if (0 < status) | |
547 | hit = 1; | |
548 | } | |
549 | return hit; | |
1e2398d7 LT |
550 | } |
551 | ||
1362671f | 552 | static int grep_cache(struct grep_opt *opt, const char **paths, int cached) |
5010cb5f JH |
553 | { |
554 | int hit = 0; | |
555 | int nr; | |
556 | read_cache(); | |
557 | ||
1e2398d7 LT |
558 | #ifdef __unix__ |
559 | /* | |
560 | * Use the external "grep" command for the case where | |
561 | * we grep through the checked-out files. It tends to | |
562 | * be a lot more optimized | |
563 | */ | |
564 | if (!cached) { | |
565 | hit = external_grep(opt, paths, cached); | |
566 | if (hit >= 0) | |
567 | return hit; | |
568 | } | |
569 | #endif | |
570 | ||
5010cb5f JH |
571 | for (nr = 0; nr < active_nr; nr++) { |
572 | struct cache_entry *ce = active_cache[nr]; | |
573 | if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode))) | |
574 | continue; | |
1362671f | 575 | if (!pathspec_matches(paths, ce->name)) |
5010cb5f JH |
576 | continue; |
577 | if (cached) | |
578 | hit |= grep_sha1(opt, ce->sha1, ce->name); | |
579 | else | |
580 | hit |= grep_file(opt, ce->name); | |
581 | } | |
582 | return hit; | |
583 | } | |
584 | ||
1362671f | 585 | static int grep_tree(struct grep_opt *opt, const char **paths, |
5010cb5f JH |
586 | struct tree_desc *tree, |
587 | const char *tree_name, const char *base) | |
588 | { | |
5010cb5f JH |
589 | int len; |
590 | int hit = 0; | |
4c068a98 | 591 | struct name_entry entry; |
e0eb889f | 592 | char *down; |
5010cb5f JH |
593 | char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100); |
594 | ||
595 | if (tree_name[0]) { | |
596 | int offset = sprintf(path_buf, "%s:", tree_name); | |
e0eb889f JH |
597 | down = path_buf + offset; |
598 | strcat(down, base); | |
5010cb5f JH |
599 | } |
600 | else { | |
e0eb889f JH |
601 | down = path_buf; |
602 | strcpy(down, base); | |
5010cb5f JH |
603 | } |
604 | len = strlen(path_buf); | |
605 | ||
4c068a98 LT |
606 | while (tree_entry(tree, &entry)) { |
607 | strcpy(path_buf + len, entry.path); | |
5010cb5f | 608 | |
4c068a98 | 609 | if (S_ISDIR(entry.mode)) |
e0eb889f JH |
610 | /* Match "abc/" against pathspec to |
611 | * decide if we want to descend into "abc" | |
612 | * directory. | |
613 | */ | |
4c068a98 | 614 | strcpy(path_buf + len + entry.pathlen, "/"); |
e0eb889f | 615 | |
1362671f | 616 | if (!pathspec_matches(paths, down)) |
5010cb5f | 617 | ; |
4c068a98 LT |
618 | else if (S_ISREG(entry.mode)) |
619 | hit |= grep_sha1(opt, entry.sha1, path_buf); | |
620 | else if (S_ISDIR(entry.mode)) { | |
5010cb5f JH |
621 | char type[20]; |
622 | struct tree_desc sub; | |
623 | void *data; | |
4c068a98 | 624 | data = read_sha1_file(entry.sha1, type, &sub.size); |
5010cb5f JH |
625 | if (!data) |
626 | die("unable to read tree (%s)", | |
4c068a98 | 627 | sha1_to_hex(entry.sha1)); |
5010cb5f | 628 | sub.buf = data; |
1362671f | 629 | hit |= grep_tree(opt, paths, &sub, tree_name, down); |
5010cb5f JH |
630 | free(data); |
631 | } | |
5010cb5f JH |
632 | } |
633 | return hit; | |
634 | } | |
635 | ||
1362671f | 636 | static int grep_object(struct grep_opt *opt, const char **paths, |
5010cb5f JH |
637 | struct object *obj, const char *name) |
638 | { | |
885a86ab | 639 | if (obj->type == TYPE_BLOB) |
5010cb5f | 640 | return grep_sha1(opt, obj->sha1, name); |
885a86ab | 641 | if (obj->type == TYPE_COMMIT || obj->type == TYPE_TREE) { |
5010cb5f JH |
642 | struct tree_desc tree; |
643 | void *data; | |
644 | int hit; | |
645 | data = read_object_with_reference(obj->sha1, tree_type, | |
646 | &tree.size, NULL); | |
647 | if (!data) | |
648 | die("unable to read tree (%s)", sha1_to_hex(obj->sha1)); | |
649 | tree.buf = data; | |
1362671f | 650 | hit = grep_tree(opt, paths, &tree, name, ""); |
5010cb5f JH |
651 | free(data); |
652 | return hit; | |
653 | } | |
885a86ab | 654 | die("unable to grep from object of type %s", typename(obj->type)); |
5010cb5f JH |
655 | } |
656 | ||
657 | static const char builtin_grep_usage[] = | |
658 | "git-grep <option>* <rev>* [-e] <pattern> [<path>...]"; | |
659 | ||
088b084b JH |
660 | static const char emsg_invalid_context_len[] = |
661 | "%s: invalid context length argument"; | |
662 | static const char emsg_missing_context_len[] = | |
663 | "missing context length argument"; | |
664 | static const char emsg_missing_argument[] = | |
665 | "option requires an argument -%s"; | |
666 | ||
5010cb5f JH |
667 | int cmd_grep(int argc, const char **argv, char **envp) |
668 | { | |
5010cb5f | 669 | int hit = 0; |
5010cb5f | 670 | int cached = 0; |
5acd64ed | 671 | int seen_dashdash = 0; |
5010cb5f | 672 | struct grep_opt opt; |
1f1e895f | 673 | struct object_array list = { 0, 0, NULL }; |
1362671f JH |
674 | const char *prefix = setup_git_directory(); |
675 | const char **paths = NULL; | |
5acd64ed | 676 | int i; |
5010cb5f JH |
677 | |
678 | memset(&opt, 0, sizeof(opt)); | |
f9b9faf6 | 679 | opt.pattern_tail = &opt.pattern_list; |
5010cb5f JH |
680 | opt.regflags = REG_NEWLINE; |
681 | ||
682 | /* | |
5acd64ed JH |
683 | * If there is no -- then the paths must exist in the working |
684 | * tree. If there is no explicit pattern specified with -e or | |
685 | * -f, we take the first unrecognized non option to be the | |
686 | * pattern, but then what follows it must be zero or more | |
687 | * valid refs up to the -- (if exists), and then existing | |
688 | * paths. If there is an explicit pattern, then the first | |
689 | * unrecocnized non option is the beginning of the refs list | |
690 | * that continues up to the -- (if exists), and then paths. | |
5010cb5f | 691 | */ |
5acd64ed | 692 | |
1362671f JH |
693 | while (1 < argc) { |
694 | const char *arg = argv[1]; | |
695 | argc--; argv++; | |
696 | if (!strcmp("--cached", arg)) { | |
697 | cached = 1; | |
698 | continue; | |
699 | } | |
b8d0f5a0 JH |
700 | if (!strcmp("-a", arg) || |
701 | !strcmp("--text", arg)) { | |
702 | opt.binary = GREP_BINARY_TEXT; | |
703 | continue; | |
704 | } | |
1362671f JH |
705 | if (!strcmp("-i", arg) || |
706 | !strcmp("--ignore-case", arg)) { | |
707 | opt.regflags |= REG_ICASE; | |
708 | continue; | |
709 | } | |
b8d0f5a0 JH |
710 | if (!strcmp("-I", arg)) { |
711 | opt.binary = GREP_BINARY_NOMATCH; | |
712 | continue; | |
713 | } | |
1362671f JH |
714 | if (!strcmp("-v", arg) || |
715 | !strcmp("--invert-match", arg)) { | |
716 | opt.invert = 1; | |
717 | continue; | |
718 | } | |
719 | if (!strcmp("-E", arg) || | |
720 | !strcmp("--extended-regexp", arg)) { | |
721 | opt.regflags |= REG_EXTENDED; | |
722 | continue; | |
723 | } | |
07ea91d8 JH |
724 | if (!strcmp("-F", arg) || |
725 | !strcmp("--fixed-strings", arg)) { | |
726 | opt.fixed = 1; | |
727 | continue; | |
728 | } | |
1362671f JH |
729 | if (!strcmp("-G", arg) || |
730 | !strcmp("--basic-regexp", arg)) { | |
731 | opt.regflags &= ~REG_EXTENDED; | |
732 | continue; | |
733 | } | |
734 | if (!strcmp("-n", arg)) { | |
735 | opt.linenum = 1; | |
736 | continue; | |
737 | } | |
738 | if (!strcmp("-H", arg)) { | |
739 | /* We always show the pathname, so this | |
740 | * is a noop. | |
741 | */ | |
742 | continue; | |
743 | } | |
744 | if (!strcmp("-l", arg) || | |
745 | !strcmp("--files-with-matches", arg)) { | |
746 | opt.name_only = 1; | |
747 | continue; | |
748 | } | |
e23d2d6b JH |
749 | if (!strcmp("-L", arg) || |
750 | !strcmp("--files-without-match", arg)) { | |
751 | opt.unmatch_name_only = 1; | |
752 | continue; | |
753 | } | |
2c866cf1 JH |
754 | if (!strcmp("-c", arg) || |
755 | !strcmp("--count", arg)) { | |
756 | opt.count = 1; | |
757 | continue; | |
758 | } | |
7839a25e JH |
759 | if (!strcmp("-w", arg) || |
760 | !strcmp("--word-regexp", arg)) { | |
761 | opt.word_regexp = 1; | |
762 | continue; | |
763 | } | |
f462ebb4 JH |
764 | if (!strncmp("-A", arg, 2) || |
765 | !strncmp("-B", arg, 2) || | |
766 | !strncmp("-C", arg, 2) || | |
767 | (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) { | |
1362671f | 768 | unsigned num; |
f462ebb4 JH |
769 | const char *scan; |
770 | switch (arg[1]) { | |
771 | case 'A': case 'B': case 'C': | |
772 | if (!arg[2]) { | |
773 | if (argc <= 1) | |
088b084b | 774 | die(emsg_missing_context_len); |
f462ebb4 JH |
775 | scan = *++argv; |
776 | argc--; | |
777 | } | |
778 | else | |
779 | scan = arg + 2; | |
780 | break; | |
781 | default: | |
782 | scan = arg + 1; | |
783 | break; | |
784 | } | |
785 | if (sscanf(scan, "%u", &num) != 1) | |
088b084b | 786 | die(emsg_invalid_context_len, scan); |
1362671f JH |
787 | switch (arg[1]) { |
788 | case 'A': | |
789 | opt.post_context = num; | |
790 | break; | |
f462ebb4 | 791 | default: |
1362671f JH |
792 | case 'C': |
793 | opt.post_context = num; | |
794 | case 'B': | |
795 | opt.pre_context = num; | |
796 | break; | |
5010cb5f | 797 | } |
1362671f JH |
798 | continue; |
799 | } | |
aa8c79ad JH |
800 | if (!strcmp("-f", arg)) { |
801 | FILE *patterns; | |
802 | int lno = 0; | |
803 | char buf[1024]; | |
804 | if (argc <= 1) | |
088b084b | 805 | die(emsg_missing_argument, arg); |
aa8c79ad JH |
806 | patterns = fopen(argv[1], "r"); |
807 | if (!patterns) | |
5acd64ed | 808 | die("'%s': %s", argv[1], strerror(errno)); |
aa8c79ad JH |
809 | while (fgets(buf, sizeof(buf), patterns)) { |
810 | int len = strlen(buf); | |
811 | if (buf[len-1] == '\n') | |
812 | buf[len-1] = 0; | |
813 | /* ignore empty line like grep does */ | |
814 | if (!buf[0]) | |
815 | continue; | |
816 | add_pattern(&opt, strdup(buf), argv[1], ++lno); | |
817 | } | |
818 | fclose(patterns); | |
819 | argv++; | |
820 | argc--; | |
821 | continue; | |
822 | } | |
1362671f JH |
823 | if (!strcmp("-e", arg)) { |
824 | if (1 < argc) { | |
aa8c79ad | 825 | add_pattern(&opt, argv[1], "-e option", 0); |
f9b9faf6 | 826 | argv++; |
1362671f | 827 | argc--; |
5010cb5f JH |
828 | continue; |
829 | } | |
088b084b | 830 | die(emsg_missing_argument, arg); |
1362671f | 831 | } |
5390590f JH |
832 | if (!strcmp("--", arg)) { |
833 | /* later processing wants to have this at argv[1] */ | |
834 | argv--; | |
835 | argc++; | |
5acd64ed | 836 | break; |
5390590f | 837 | } |
5acd64ed | 838 | if (*arg == '-') |
1362671f | 839 | usage(builtin_grep_usage); |
5acd64ed JH |
840 | |
841 | /* First unrecognized non-option token */ | |
f9b9faf6 | 842 | if (!opt.pattern_list) { |
aa8c79ad | 843 | add_pattern(&opt, arg, "command line", 0); |
1362671f JH |
844 | break; |
845 | } | |
846 | else { | |
847 | /* We are looking at the first path or rev; | |
5acd64ed | 848 | * it is found at argv[1] after leaving the |
1362671f JH |
849 | * loop. |
850 | */ | |
851 | argc++; argv--; | |
852 | break; | |
5010cb5f | 853 | } |
5010cb5f | 854 | } |
5acd64ed | 855 | |
f9b9faf6 | 856 | if (!opt.pattern_list) |
5010cb5f | 857 | die("no pattern given."); |
07ea91d8 JH |
858 | if ((opt.regflags != REG_NEWLINE) && opt.fixed) |
859 | die("cannot mix --fixed-strings and regexp"); | |
860 | if (!opt.fixed) | |
861 | compile_patterns(&opt); | |
5acd64ed JH |
862 | |
863 | /* Check revs and then paths */ | |
864 | for (i = 1; i < argc; i++) { | |
865 | const char *arg = argv[i]; | |
1362671f | 866 | unsigned char sha1[20]; |
5acd64ed JH |
867 | /* Is it a rev? */ |
868 | if (!get_sha1(arg, sha1)) { | |
869 | struct object *object = parse_object(sha1); | |
5acd64ed JH |
870 | if (!object) |
871 | die("bad object %s", arg); | |
1f1e895f | 872 | add_object_array(object, arg, &list); |
5acd64ed JH |
873 | continue; |
874 | } | |
875 | if (!strcmp(arg, "--")) { | |
876 | i++; | |
877 | seen_dashdash = 1; | |
878 | } | |
879 | break; | |
1362671f | 880 | } |
5acd64ed JH |
881 | |
882 | /* The rest are paths */ | |
883 | if (!seen_dashdash) { | |
884 | int j; | |
c39c4f47 | 885 | for (j = i; j < argc; j++) |
5acd64ed JH |
886 | verify_filename(prefix, argv[j]); |
887 | } | |
888 | ||
889 | if (i < argc) | |
890 | paths = get_pathspec(prefix, argv + i); | |
1362671f JH |
891 | else if (prefix) { |
892 | paths = xcalloc(2, sizeof(const char *)); | |
893 | paths[0] = prefix; | |
894 | paths[1] = NULL; | |
895 | } | |
5010cb5f | 896 | |
1f1e895f | 897 | if (!list.nr) |
1362671f | 898 | return !grep_cache(&opt, paths, cached); |
aa8c79ad | 899 | |
5010cb5f | 900 | if (cached) |
aa8c79ad | 901 | die("both --cached and trees are given."); |
5010cb5f | 902 | |
1f1e895f | 903 | for (i = 0; i < list.nr; i++) { |
5010cb5f | 904 | struct object *real_obj; |
1f1e895f LT |
905 | real_obj = deref_tag(list.objects[i].item, NULL, 0); |
906 | if (grep_object(&opt, paths, real_obj, list.objects[i].name)) | |
5010cb5f JH |
907 | hit = 1; |
908 | } | |
909 | return !hit; | |
910 | } |