Trivial support for cloning and fetching via ftp://.
[git/git.git] / builtin-grep.c
CommitLineData
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 21static 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
79d3696c
JH
85enum grep_pat_token {
86 GREP_PATTERN,
87 GREP_AND,
88 GREP_OPEN_PAREN,
89 GREP_CLOSE_PAREN,
90 GREP_NOT,
91 GREP_OR,
92};
93
f9b9faf6
JH
94struct grep_pat {
95 struct grep_pat *next;
aa8c79ad
JH
96 const char *origin;
97 int no;
79d3696c 98 enum grep_pat_token token;
5010cb5f
JH
99 const char *pattern;
100 regex_t regexp;
f9b9faf6
JH
101};
102
79d3696c
JH
103enum grep_expr_node {
104 GREP_NODE_ATOM,
105 GREP_NODE_NOT,
106 GREP_NODE_AND,
107 GREP_NODE_OR,
108};
109
110struct grep_expr {
111 enum grep_expr_node node;
112 union {
113 struct grep_pat *atom;
114 struct grep_expr *unary;
115 struct {
116 struct grep_expr *left;
117 struct grep_expr *right;
118 } binary;
119 } u;
120};
121
f9b9faf6
JH
122struct grep_opt {
123 struct grep_pat *pattern_list;
124 struct grep_pat **pattern_tail;
79d3696c 125 struct grep_expr *pattern_expression;
0d042fec 126 int prefix_length;
f9b9faf6 127 regex_t regexp;
5010cb5f
JH
128 unsigned linenum:1;
129 unsigned invert:1;
df0e7aa8 130 unsigned name_only:1;
e23d2d6b 131 unsigned unmatch_name_only:1;
2c866cf1 132 unsigned count:1;
7839a25e 133 unsigned word_regexp:1;
07ea91d8 134 unsigned fixed:1;
b8d0f5a0
JH
135#define GREP_BINARY_DEFAULT 0
136#define GREP_BINARY_NOMATCH 1
137#define GREP_BINARY_TEXT 2
138 unsigned binary:2;
79d3696c 139 unsigned extended:1;
0d042fec 140 unsigned relative:1;
5010cb5f
JH
141 int regflags;
142 unsigned pre_context;
143 unsigned post_context;
144};
145
aa8c79ad 146static void add_pattern(struct grep_opt *opt, const char *pat,
79d3696c 147 const char *origin, int no, enum grep_pat_token t)
f9b9faf6
JH
148{
149 struct grep_pat *p = xcalloc(1, sizeof(*p));
150 p->pattern = pat;
aa8c79ad
JH
151 p->origin = origin;
152 p->no = no;
79d3696c 153 p->token = t;
f9b9faf6
JH
154 *opt->pattern_tail = p;
155 opt->pattern_tail = &p->next;
156 p->next = NULL;
157}
158
79d3696c
JH
159static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
160{
161 int err = regcomp(&p->regexp, p->pattern, opt->regflags);
162 if (err) {
163 char errbuf[1024];
164 char where[1024];
165 if (p->no)
166 sprintf(where, "In '%s' at %d, ",
167 p->origin, p->no);
168 else if (p->origin)
169 sprintf(where, "%s, ", p->origin);
170 else
171 where[0] = 0;
172 regerror(err, &p->regexp, errbuf, 1024);
173 regfree(&p->regexp);
174 die("%s'%s': %s", where, p->pattern, errbuf);
175 }
176}
177
79d3696c
JH
178static struct grep_expr *compile_pattern_expr(struct grep_pat **);
179static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
180{
181 struct grep_pat *p;
182 struct grep_expr *x;
183
79d3696c
JH
184 p = *list;
185 switch (p->token) {
186 case GREP_PATTERN: /* atom */
187 x = xcalloc(1, sizeof (struct grep_expr));
188 x->node = GREP_NODE_ATOM;
189 x->u.atom = p;
190 *list = p->next;
191 return x;
192 case GREP_OPEN_PAREN:
193 *list = p->next;
194 x = compile_pattern_expr(list);
195 if (!x)
196 return NULL;
197 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
198 die("unmatched parenthesis");
199 *list = (*list)->next;
200 return x;
201 default:
202 return NULL;
203 }
204}
205
206static struct grep_expr *compile_pattern_not(struct grep_pat **list)
207{
208 struct grep_pat *p;
209 struct grep_expr *x;
210
79d3696c
JH
211 p = *list;
212 switch (p->token) {
213 case GREP_NOT:
214 if (!p->next)
215 die("--not not followed by pattern expression");
216 *list = p->next;
217 x = xcalloc(1, sizeof (struct grep_expr));
218 x->node = GREP_NODE_NOT;
219 x->u.unary = compile_pattern_not(list);
220 if (!x->u.unary)
221 die("--not followed by non pattern expression");
222 return x;
223 default:
224 return compile_pattern_atom(list);
225 }
226}
227
228static struct grep_expr *compile_pattern_and(struct grep_pat **list)
229{
230 struct grep_pat *p;
231 struct grep_expr *x, *y, *z;
232
79d3696c
JH
233 x = compile_pattern_not(list);
234 p = *list;
235 if (p && p->token == GREP_AND) {
236 if (!p->next)
237 die("--and not followed by pattern expression");
238 *list = p->next;
239 y = compile_pattern_and(list);
240 if (!y)
241 die("--and not followed by pattern expression");
242 z = xcalloc(1, sizeof (struct grep_expr));
243 z->node = GREP_NODE_AND;
244 z->u.binary.left = x;
245 z->u.binary.right = y;
246 return z;
247 }
248 return x;
249}
250
251static struct grep_expr *compile_pattern_or(struct grep_pat **list)
252{
253 struct grep_pat *p;
254 struct grep_expr *x, *y, *z;
255
79d3696c
JH
256 x = compile_pattern_and(list);
257 p = *list;
258 if (x && p && p->token != GREP_CLOSE_PAREN) {
259 y = compile_pattern_or(list);
260 if (!y)
261 die("not a pattern expression %s", p->pattern);
262 z = xcalloc(1, sizeof (struct grep_expr));
263 z->node = GREP_NODE_OR;
264 z->u.binary.left = x;
265 z->u.binary.right = y;
266 return z;
267 }
268 return x;
269}
270
271static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
272{
79d3696c
JH
273 return compile_pattern_or(list);
274}
275
f9b9faf6
JH
276static void compile_patterns(struct grep_opt *opt)
277{
278 struct grep_pat *p;
79d3696c
JH
279
280 /* First compile regexps */
f9b9faf6 281 for (p = opt->pattern_list; p; p = p->next) {
79d3696c
JH
282 if (p->token == GREP_PATTERN)
283 compile_regexp(p, opt);
284 else
285 opt->extended = 1;
f9b9faf6 286 }
79d3696c
JH
287
288 if (!opt->extended)
289 return;
290
291 /* Then bundle them up in an expression.
292 * A classic recursive descent parser would do.
293 */
294 p = opt->pattern_list;
295 opt->pattern_expression = compile_pattern_expr(&p);
79d3696c
JH
296 if (p)
297 die("incomplete pattern expression: %s", p->pattern);
f9b9faf6
JH
298}
299
5010cb5f
JH
300static char *end_of_line(char *cp, unsigned long *left)
301{
302 unsigned long l = *left;
303 while (l && *cp != '\n') {
304 l--;
305 cp++;
306 }
307 *left = l;
308 return cp;
309}
310
7839a25e
JH
311static int word_char(char ch)
312{
313 return isalnum(ch) || ch == '_';
314}
315
5010cb5f
JH
316static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
317 const char *name, unsigned lno, char sign)
318{
319 printf("%s%c", name, sign);
320 if (opt->linenum)
321 printf("%d%c", lno, sign);
a24f1e25 322 printf("%.*s\n", (int)(eol-bol), bol);
5010cb5f
JH
323}
324
b8d0f5a0
JH
325/*
326 * NEEDSWORK: share code with diff.c
327 */
328#define FIRST_FEW_BYTES 8000
329static int buffer_is_binary(const char *ptr, unsigned long size)
330{
331 if (FIRST_FEW_BYTES < size)
332 size = FIRST_FEW_BYTES;
b756776d 333 return !!memchr(ptr, 0, size);
b8d0f5a0
JH
334}
335
07ea91d8
JH
336static int fixmatch(const char *pattern, char *line, regmatch_t *match)
337{
338 char *hit = strstr(line, pattern);
339 if (!hit) {
340 match->rm_so = match->rm_eo = -1;
341 return REG_NOMATCH;
342 }
343 else {
344 match->rm_so = hit - line;
345 match->rm_eo = match->rm_so + strlen(pattern);
346 return 0;
347 }
348}
349
79d3696c
JH
350static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
351{
352 int hit = 0;
f25b7939 353 int at_true_bol = 1;
79d3696c
JH
354 regmatch_t pmatch[10];
355
f25b7939 356 again:
79d3696c
JH
357 if (!opt->fixed) {
358 regex_t *exp = &p->regexp;
359 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
360 pmatch, 0);
361 }
362 else {
363 hit = !fixmatch(p->pattern, bol, pmatch);
364 }
365
366 if (hit && opt->word_regexp) {
79d3696c
JH
367 if ((pmatch[0].rm_so < 0) ||
368 (eol - bol) <= pmatch[0].rm_so ||
369 (pmatch[0].rm_eo < 0) ||
370 (eol - bol) < pmatch[0].rm_eo)
371 die("regexp returned nonsense");
f25b7939
JH
372
373 /* Match beginning must be either beginning of the
374 * line, or at word boundary (i.e. the last char must
375 * not be a word char). Similarly, match end must be
376 * either end of the line, or at word boundary
377 * (i.e. the next char must not be a word char).
378 */
379 if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
380 !word_char(bol[pmatch[0].rm_so-1])) &&
381 ((pmatch[0].rm_eo == (eol-bol)) ||
382 !word_char(bol[pmatch[0].rm_eo])) )
383 ;
384 else
79d3696c 385 hit = 0;
f25b7939
JH
386
387 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
388 /* There could be more than one match on the
389 * line, and the first match might not be
390 * strict word match. But later ones could be!
391 */
392 bol = pmatch[0].rm_so + bol + 1;
393 at_true_bol = 0;
394 goto again;
395 }
79d3696c
JH
396 }
397 return hit;
398}
399
400static int match_expr_eval(struct grep_opt *opt,
401 struct grep_expr *x,
402 char *bol, char *eol)
403{
404 switch (x->node) {
405 case GREP_NODE_ATOM:
406 return match_one_pattern(opt, x->u.atom, bol, eol);
407 break;
408 case GREP_NODE_NOT:
409 return !match_expr_eval(opt, x->u.unary, bol, eol);
410 case GREP_NODE_AND:
411 return (match_expr_eval(opt, x->u.binary.left, bol, eol) &&
412 match_expr_eval(opt, x->u.binary.right, bol, eol));
413 case GREP_NODE_OR:
414 return (match_expr_eval(opt, x->u.binary.left, bol, eol) ||
415 match_expr_eval(opt, x->u.binary.right, bol, eol));
416 }
417 die("Unexpected node type (internal error) %d\n", x->node);
418}
419
420static int match_expr(struct grep_opt *opt, char *bol, char *eol)
421{
422 struct grep_expr *x = opt->pattern_expression;
423 return match_expr_eval(opt, x, bol, eol);
424}
425
426static int match_line(struct grep_opt *opt, char *bol, char *eol)
427{
428 struct grep_pat *p;
429 if (opt->extended)
430 return match_expr(opt, bol, eol);
431 for (p = opt->pattern_list; p; p = p->next) {
432 if (match_one_pattern(opt, p, bol, eol))
433 return 1;
434 }
435 return 0;
436}
437
5010cb5f
JH
438static int grep_buffer(struct grep_opt *opt, const char *name,
439 char *buf, unsigned long size)
440{
441 char *bol = buf;
442 unsigned long left = size;
443 unsigned lno = 1;
444 struct pre_context_line {
445 char *bol;
446 char *eol;
447 } *prev = NULL, *pcl;
448 unsigned last_hit = 0;
449 unsigned last_shown = 0;
b8d0f5a0 450 int binary_match_only = 0;
5010cb5f 451 const char *hunk_mark = "";
2c866cf1 452 unsigned count = 0;
5010cb5f 453
b8d0f5a0
JH
454 if (buffer_is_binary(buf, size)) {
455 switch (opt->binary) {
456 case GREP_BINARY_DEFAULT:
457 binary_match_only = 1;
458 break;
459 case GREP_BINARY_NOMATCH:
460 return 0; /* Assume unmatch */
461 break;
462 default:
463 break;
464 }
465 }
466
5010cb5f
JH
467 if (opt->pre_context)
468 prev = xcalloc(opt->pre_context, sizeof(*prev));
469 if (opt->pre_context || opt->post_context)
470 hunk_mark = "--\n";
471
472 while (left) {
5010cb5f 473 char *eol, ch;
f9b9faf6 474 int hit = 0;
5010cb5f
JH
475
476 eol = end_of_line(bol, &left);
477 ch = *eol;
478 *eol = 0;
479
79d3696c 480 hit = match_line(opt, bol, eol);
7839a25e 481
f9b9faf6
JH
482 /* "grep -v -e foo -e bla" should list lines
483 * that do not have either, so inversion should
484 * be done outside.
485 */
5010cb5f
JH
486 if (opt->invert)
487 hit = !hit;
e23d2d6b
JH
488 if (opt->unmatch_name_only) {
489 if (hit)
490 return 0;
491 goto next_line;
492 }
5010cb5f 493 if (hit) {
2c866cf1 494 count++;
b8d0f5a0
JH
495 if (binary_match_only) {
496 printf("Binary file %s matches\n", name);
497 return 1;
498 }
df0e7aa8
JH
499 if (opt->name_only) {
500 printf("%s\n", name);
501 return 1;
502 }
5010cb5f
JH
503 /* Hit at this line. If we haven't shown the
504 * pre-context lines, we would need to show them.
2c866cf1
JH
505 * When asked to do "count", this still show
506 * the context which is nonsense, but the user
507 * deserves to get that ;-).
5010cb5f
JH
508 */
509 if (opt->pre_context) {
510 unsigned from;
511 if (opt->pre_context < lno)
512 from = lno - opt->pre_context;
513 else
514 from = 1;
515 if (from <= last_shown)
516 from = last_shown + 1;
517 if (last_shown && from != last_shown + 1)
518 printf(hunk_mark);
519 while (from < lno) {
520 pcl = &prev[lno-from-1];
521 show_line(opt, pcl->bol, pcl->eol,
522 name, from, '-');
523 from++;
524 }
525 last_shown = lno-1;
526 }
527 if (last_shown && lno != last_shown + 1)
528 printf(hunk_mark);
2c866cf1
JH
529 if (!opt->count)
530 show_line(opt, bol, eol, name, lno, ':');
5010cb5f
JH
531 last_shown = last_hit = lno;
532 }
533 else if (last_hit &&
534 lno <= last_hit + opt->post_context) {
535 /* If the last hit is within the post context,
536 * we need to show this line.
537 */
538 if (last_shown && lno != last_shown + 1)
539 printf(hunk_mark);
540 show_line(opt, bol, eol, name, lno, '-');
541 last_shown = lno;
542 }
543 if (opt->pre_context) {
544 memmove(prev+1, prev,
545 (opt->pre_context-1) * sizeof(*prev));
546 prev->bol = bol;
547 prev->eol = eol;
548 }
e23d2d6b
JH
549
550 next_line:
5010cb5f
JH
551 *eol = ch;
552 bol = eol + 1;
7ed36f56
JH
553 if (!left)
554 break;
5010cb5f
JH
555 left--;
556 lno++;
557 }
e23d2d6b
JH
558
559 if (opt->unmatch_name_only) {
560 /* We did not see any hit, so we want to show this */
561 printf("%s\n", name);
562 return 1;
563 }
564
2c866cf1
JH
565 /* NEEDSWORK:
566 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
567 * which feels mostly useless but sometimes useful. Maybe
568 * make it another option? For now suppress them.
569 */
570 if (opt->count && count)
571 printf("%s:%u\n", name, count);
5010cb5f
JH
572 return !!last_hit;
573}
574
0d042fec 575static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
5010cb5f
JH
576{
577 unsigned long size;
578 char *data;
579 char type[20];
0d042fec 580 char *to_free = NULL;
5010cb5f 581 int hit;
0d042fec 582
5010cb5f
JH
583 data = read_sha1_file(sha1, type, &size);
584 if (!data) {
585 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
586 return 0;
587 }
0d042fec
JH
588 if (opt->relative && opt->prefix_length) {
589 static char name_buf[PATH_MAX];
590 char *cp;
591 int name_len = strlen(name) - opt->prefix_length + 1;
592
593 if (!tree_name_len)
594 name += opt->prefix_length;
595 else {
596 if (ARRAY_SIZE(name_buf) <= name_len)
597 cp = to_free = xmalloc(name_len);
598 else
599 cp = name_buf;
600 memcpy(cp, name, tree_name_len);
601 strcpy(cp + tree_name_len,
602 name + tree_name_len + opt->prefix_length);
603 name = cp;
604 }
605 }
5010cb5f
JH
606 hit = grep_buffer(opt, name, data, size);
607 free(data);
0d042fec 608 free(to_free);
5010cb5f
JH
609 return hit;
610}
611
612static int grep_file(struct grep_opt *opt, const char *filename)
613{
614 struct stat st;
615 int i;
616 char *data;
617 if (lstat(filename, &st) < 0) {
618 err_ret:
619 if (errno != ENOENT)
620 error("'%s': %s", filename, strerror(errno));
621 return 0;
622 }
623 if (!st.st_size)
624 return 0; /* empty file -- no grep hit */
625 if (!S_ISREG(st.st_mode))
626 return 0;
627 i = open(filename, O_RDONLY);
628 if (i < 0)
629 goto err_ret;
630 data = xmalloc(st.st_size + 1);
631 if (st.st_size != xread(i, data, st.st_size)) {
632 error("'%s': short read %s", filename, strerror(errno));
633 close(i);
634 free(data);
635 return 0;
636 }
637 close(i);
0d042fec
JH
638 if (opt->relative && opt->prefix_length)
639 filename += opt->prefix_length;
5010cb5f
JH
640 i = grep_buffer(opt, filename, data, st.st_size);
641 free(data);
642 return i;
643}
644
1e2398d7
LT
645static int exec_grep(int argc, const char **argv)
646{
647 pid_t pid;
648 int status;
649
650 argv[argc] = NULL;
651 pid = fork();
652 if (pid < 0)
653 return pid;
654 if (!pid) {
655 execvp("grep", (char **) argv);
656 exit(255);
657 }
658 while (waitpid(pid, &status, 0) < 0) {
659 if (errno == EINTR)
660 continue;
661 return -1;
662 }
663 if (WIFEXITED(status)) {
664 if (!WEXITSTATUS(status))
665 return 1;
666 return 0;
667 }
668 return -1;
669}
670
671#define MAXARGS 1000
ffa0a7ab
JH
672#define ARGBUF 4096
673#define push_arg(a) do { \
674 if (nr < MAXARGS) argv[nr++] = (a); \
675 else die("maximum number of args exceeded"); \
676 } while (0)
1e2398d7
LT
677
678static int external_grep(struct grep_opt *opt, const char **paths, int cached)
679{
fcfe34b5 680 int i, nr, argc, hit, len, status;
1e2398d7 681 const char *argv[MAXARGS+1];
ffa0a7ab
JH
682 char randarg[ARGBUF];
683 char *argptr = randarg;
1e2398d7
LT
684 struct grep_pat *p;
685
0d042fec 686 if (opt->extended || (opt->relative && opt->prefix_length))
79d3696c 687 return -1;
ffa0a7ab
JH
688 len = nr = 0;
689 push_arg("grep");
ffa0a7ab 690 if (opt->fixed)
f6647519 691 push_arg("-F");
ffa0a7ab
JH
692 if (opt->linenum)
693 push_arg("-n");
694 if (opt->regflags & REG_EXTENDED)
695 push_arg("-E");
3026402c
RF
696 if (opt->regflags & REG_ICASE)
697 push_arg("-i");
1e2398d7 698 if (opt->word_regexp)
ffa0a7ab 699 push_arg("-w");
1e2398d7 700 if (opt->name_only)
ffa0a7ab
JH
701 push_arg("-l");
702 if (opt->unmatch_name_only)
703 push_arg("-L");
704 if (opt->count)
705 push_arg("-c");
706 if (opt->post_context || opt->pre_context) {
707 if (opt->post_context != opt->pre_context) {
708 if (opt->pre_context) {
709 push_arg("-B");
710 len += snprintf(argptr, sizeof(randarg)-len,
711 "%u", opt->pre_context);
712 if (sizeof(randarg) <= len)
713 die("maximum length of args exceeded");
714 push_arg(argptr);
715 argptr += len;
716 }
717 if (opt->post_context) {
718 push_arg("-A");
719 len += snprintf(argptr, sizeof(randarg)-len,
720 "%u", opt->post_context);
721 if (sizeof(randarg) <= len)
722 die("maximum length of args exceeded");
723 push_arg(argptr);
724 argptr += len;
725 }
726 }
727 else {
728 push_arg("-C");
729 len += snprintf(argptr, sizeof(randarg)-len,
730 "%u", opt->post_context);
731 if (sizeof(randarg) <= len)
732 die("maximum length of args exceeded");
733 push_arg(argptr);
734 argptr += len;
735 }
736 }
1e2398d7 737 for (p = opt->pattern_list; p; p = p->next) {
ffa0a7ab
JH
738 push_arg("-e");
739 push_arg(p->pattern);
1e2398d7 740 }
bbb66c60
LT
741
742 /*
743 * To make sure we get the header printed out when we want it,
744 * add /dev/null to the paths to grep. This is unnecessary
745 * (and wrong) with "-l" or "-L", which always print out the
746 * name anyway.
747 *
748 * GNU grep has "-H", but this is portable.
749 */
750 if (!opt->name_only && !opt->unmatch_name_only)
751 push_arg("/dev/null");
1e2398d7
LT
752
753 hit = 0;
754 argc = nr;
755 for (i = 0; i < active_nr; i++) {
756 struct cache_entry *ce = active_cache[i];
fbd01abf 757 char *name;
1e2398d7
LT
758 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
759 continue;
760 if (!pathspec_matches(paths, ce->name))
761 continue;
bbb66c60
LT
762 name = ce->name;
763 if (name[0] == '-') {
764 int len = ce_namelen(ce);
765 name = xmalloc(len + 3);
766 memcpy(name, "./", 2);
767 memcpy(name + 2, ce->name, len + 1);
768 }
769 argv[argc++] = name;
1e2398d7
LT
770 if (argc < MAXARGS)
771 continue;
fcfe34b5
JH
772 status = exec_grep(argc, argv);
773 if (0 < status)
774 hit = 1;
1e2398d7
LT
775 argc = nr;
776 }
fcfe34b5
JH
777 if (argc > nr) {
778 status = exec_grep(argc, argv);
779 if (0 < status)
780 hit = 1;
781 }
782 return hit;
1e2398d7
LT
783}
784
1362671f 785static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
5010cb5f
JH
786{
787 int hit = 0;
788 int nr;
789 read_cache();
790
1e2398d7
LT
791#ifdef __unix__
792 /*
793 * Use the external "grep" command for the case where
794 * we grep through the checked-out files. It tends to
795 * be a lot more optimized
796 */
797 if (!cached) {
798 hit = external_grep(opt, paths, cached);
799 if (hit >= 0)
800 return hit;
801 }
802#endif
803
5010cb5f
JH
804 for (nr = 0; nr < active_nr; nr++) {
805 struct cache_entry *ce = active_cache[nr];
806 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
807 continue;
1362671f 808 if (!pathspec_matches(paths, ce->name))
5010cb5f
JH
809 continue;
810 if (cached)
0d042fec 811 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
5010cb5f
JH
812 else
813 hit |= grep_file(opt, ce->name);
814 }
815 return hit;
816}
817
1362671f 818static int grep_tree(struct grep_opt *opt, const char **paths,
5010cb5f
JH
819 struct tree_desc *tree,
820 const char *tree_name, const char *base)
821{
5010cb5f
JH
822 int len;
823 int hit = 0;
4c068a98 824 struct name_entry entry;
e0eb889f 825 char *down;
0d042fec
JH
826 int tn_len = strlen(tree_name);
827 char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
5010cb5f 828
0d042fec
JH
829 if (tn_len) {
830 tn_len = sprintf(path_buf, "%s:", tree_name);
831 down = path_buf + tn_len;
e0eb889f 832 strcat(down, base);
5010cb5f
JH
833 }
834 else {
e0eb889f
JH
835 down = path_buf;
836 strcpy(down, base);
5010cb5f
JH
837 }
838 len = strlen(path_buf);
839
4c068a98
LT
840 while (tree_entry(tree, &entry)) {
841 strcpy(path_buf + len, entry.path);
5010cb5f 842
4c068a98 843 if (S_ISDIR(entry.mode))
e0eb889f
JH
844 /* Match "abc/" against pathspec to
845 * decide if we want to descend into "abc"
846 * directory.
847 */
4c068a98 848 strcpy(path_buf + len + entry.pathlen, "/");
e0eb889f 849
1362671f 850 if (!pathspec_matches(paths, down))
5010cb5f 851 ;
4c068a98 852 else if (S_ISREG(entry.mode))
0d042fec 853 hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
4c068a98 854 else if (S_ISDIR(entry.mode)) {
5010cb5f
JH
855 char type[20];
856 struct tree_desc sub;
857 void *data;
4c068a98 858 data = read_sha1_file(entry.sha1, type, &sub.size);
5010cb5f
JH
859 if (!data)
860 die("unable to read tree (%s)",
4c068a98 861 sha1_to_hex(entry.sha1));
5010cb5f 862 sub.buf = data;
1362671f 863 hit |= grep_tree(opt, paths, &sub, tree_name, down);
5010cb5f
JH
864 free(data);
865 }
5010cb5f
JH
866 }
867 return hit;
868}
869
1362671f 870static int grep_object(struct grep_opt *opt, const char **paths,
5010cb5f
JH
871 struct object *obj, const char *name)
872{
1974632c 873 if (obj->type == OBJ_BLOB)
0d042fec 874 return grep_sha1(opt, obj->sha1, name, 0);
1974632c 875 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
5010cb5f
JH
876 struct tree_desc tree;
877 void *data;
878 int hit;
879 data = read_object_with_reference(obj->sha1, tree_type,
880 &tree.size, NULL);
881 if (!data)
882 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
883 tree.buf = data;
1362671f 884 hit = grep_tree(opt, paths, &tree, name, "");
5010cb5f
JH
885 free(data);
886 return hit;
887 }
885a86ab 888 die("unable to grep from object of type %s", typename(obj->type));
5010cb5f
JH
889}
890
891static const char builtin_grep_usage[] =
892"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
893
088b084b
JH
894static const char emsg_invalid_context_len[] =
895"%s: invalid context length argument";
896static const char emsg_missing_context_len[] =
897"missing context length argument";
898static const char emsg_missing_argument[] =
899"option requires an argument -%s";
900
a633fca0 901int cmd_grep(int argc, const char **argv, const char *prefix)
5010cb5f 902{
5010cb5f 903 int hit = 0;
5010cb5f 904 int cached = 0;
5acd64ed 905 int seen_dashdash = 0;
5010cb5f 906 struct grep_opt opt;
1f1e895f 907 struct object_array list = { 0, 0, NULL };
1362671f 908 const char **paths = NULL;
5acd64ed 909 int i;
5010cb5f
JH
910
911 memset(&opt, 0, sizeof(opt));
0d042fec
JH
912 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
913 opt.relative = 1;
f9b9faf6 914 opt.pattern_tail = &opt.pattern_list;
5010cb5f
JH
915 opt.regflags = REG_NEWLINE;
916
917 /*
5acd64ed
JH
918 * If there is no -- then the paths must exist in the working
919 * tree. If there is no explicit pattern specified with -e or
920 * -f, we take the first unrecognized non option to be the
921 * pattern, but then what follows it must be zero or more
922 * valid refs up to the -- (if exists), and then existing
923 * paths. If there is an explicit pattern, then the first
82e5a82f 924 * unrecognized non option is the beginning of the refs list
5acd64ed 925 * that continues up to the -- (if exists), and then paths.
5010cb5f 926 */
5acd64ed 927
1362671f
JH
928 while (1 < argc) {
929 const char *arg = argv[1];
930 argc--; argv++;
931 if (!strcmp("--cached", arg)) {
932 cached = 1;
933 continue;
934 }
b8d0f5a0
JH
935 if (!strcmp("-a", arg) ||
936 !strcmp("--text", arg)) {
937 opt.binary = GREP_BINARY_TEXT;
938 continue;
939 }
1362671f
JH
940 if (!strcmp("-i", arg) ||
941 !strcmp("--ignore-case", arg)) {
942 opt.regflags |= REG_ICASE;
943 continue;
944 }
b8d0f5a0
JH
945 if (!strcmp("-I", arg)) {
946 opt.binary = GREP_BINARY_NOMATCH;
947 continue;
948 }
1362671f
JH
949 if (!strcmp("-v", arg) ||
950 !strcmp("--invert-match", arg)) {
951 opt.invert = 1;
952 continue;
953 }
954 if (!strcmp("-E", arg) ||
955 !strcmp("--extended-regexp", arg)) {
956 opt.regflags |= REG_EXTENDED;
957 continue;
958 }
07ea91d8
JH
959 if (!strcmp("-F", arg) ||
960 !strcmp("--fixed-strings", arg)) {
961 opt.fixed = 1;
962 continue;
963 }
1362671f
JH
964 if (!strcmp("-G", arg) ||
965 !strcmp("--basic-regexp", arg)) {
966 opt.regflags &= ~REG_EXTENDED;
967 continue;
968 }
969 if (!strcmp("-n", arg)) {
970 opt.linenum = 1;
971 continue;
972 }
973 if (!strcmp("-H", arg)) {
974 /* We always show the pathname, so this
975 * is a noop.
976 */
977 continue;
978 }
979 if (!strcmp("-l", arg) ||
980 !strcmp("--files-with-matches", arg)) {
981 opt.name_only = 1;
982 continue;
983 }
e23d2d6b
JH
984 if (!strcmp("-L", arg) ||
985 !strcmp("--files-without-match", arg)) {
986 opt.unmatch_name_only = 1;
987 continue;
988 }
2c866cf1
JH
989 if (!strcmp("-c", arg) ||
990 !strcmp("--count", arg)) {
991 opt.count = 1;
992 continue;
993 }
7839a25e
JH
994 if (!strcmp("-w", arg) ||
995 !strcmp("--word-regexp", arg)) {
996 opt.word_regexp = 1;
997 continue;
998 }
f462ebb4
JH
999 if (!strncmp("-A", arg, 2) ||
1000 !strncmp("-B", arg, 2) ||
1001 !strncmp("-C", arg, 2) ||
1002 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
1362671f 1003 unsigned num;
f462ebb4
JH
1004 const char *scan;
1005 switch (arg[1]) {
1006 case 'A': case 'B': case 'C':
1007 if (!arg[2]) {
1008 if (argc <= 1)
088b084b 1009 die(emsg_missing_context_len);
f462ebb4
JH
1010 scan = *++argv;
1011 argc--;
1012 }
1013 else
1014 scan = arg + 2;
1015 break;
1016 default:
1017 scan = arg + 1;
1018 break;
1019 }
1020 if (sscanf(scan, "%u", &num) != 1)
088b084b 1021 die(emsg_invalid_context_len, scan);
1362671f
JH
1022 switch (arg[1]) {
1023 case 'A':
1024 opt.post_context = num;
1025 break;
f462ebb4 1026 default:
1362671f
JH
1027 case 'C':
1028 opt.post_context = num;
1029 case 'B':
1030 opt.pre_context = num;
1031 break;
5010cb5f 1032 }
1362671f
JH
1033 continue;
1034 }
aa8c79ad
JH
1035 if (!strcmp("-f", arg)) {
1036 FILE *patterns;
1037 int lno = 0;
1038 char buf[1024];
1039 if (argc <= 1)
088b084b 1040 die(emsg_missing_argument, arg);
aa8c79ad
JH
1041 patterns = fopen(argv[1], "r");
1042 if (!patterns)
5acd64ed 1043 die("'%s': %s", argv[1], strerror(errno));
aa8c79ad
JH
1044 while (fgets(buf, sizeof(buf), patterns)) {
1045 int len = strlen(buf);
1046 if (buf[len-1] == '\n')
1047 buf[len-1] = 0;
1048 /* ignore empty line like grep does */
1049 if (!buf[0])
1050 continue;
9befac47 1051 add_pattern(&opt, xstrdup(buf), argv[1], ++lno,
79d3696c 1052 GREP_PATTERN);
aa8c79ad
JH
1053 }
1054 fclose(patterns);
1055 argv++;
1056 argc--;
1057 continue;
1058 }
79d3696c
JH
1059 if (!strcmp("--not", arg)) {
1060 add_pattern(&opt, arg, "command line", 0, GREP_NOT);
1061 continue;
1062 }
1063 if (!strcmp("--and", arg)) {
1064 add_pattern(&opt, arg, "command line", 0, GREP_AND);
1065 continue;
1066 }
1067 if (!strcmp("--or", arg))
1068 continue; /* no-op */
1069 if (!strcmp("(", arg)) {
1070 add_pattern(&opt, arg, "command line", 0, GREP_OPEN_PAREN);
1071 continue;
1072 }
1073 if (!strcmp(")", arg)) {
1074 add_pattern(&opt, arg, "command line", 0, GREP_CLOSE_PAREN);
1075 continue;
1076 }
1362671f
JH
1077 if (!strcmp("-e", arg)) {
1078 if (1 < argc) {
79d3696c
JH
1079 add_pattern(&opt, argv[1], "-e option", 0,
1080 GREP_PATTERN);
f9b9faf6 1081 argv++;
1362671f 1082 argc--;
5010cb5f
JH
1083 continue;
1084 }
088b084b 1085 die(emsg_missing_argument, arg);
1362671f 1086 }
0d042fec
JH
1087 if (!strcmp("--full-name", arg)) {
1088 opt.relative = 0;
1089 continue;
1090 }
5390590f
JH
1091 if (!strcmp("--", arg)) {
1092 /* later processing wants to have this at argv[1] */
1093 argv--;
1094 argc++;
5acd64ed 1095 break;
5390590f 1096 }
5acd64ed 1097 if (*arg == '-')
1362671f 1098 usage(builtin_grep_usage);
5acd64ed
JH
1099
1100 /* First unrecognized non-option token */
f9b9faf6 1101 if (!opt.pattern_list) {
79d3696c
JH
1102 add_pattern(&opt, arg, "command line", 0,
1103 GREP_PATTERN);
1362671f
JH
1104 break;
1105 }
1106 else {
1107 /* We are looking at the first path or rev;
5acd64ed 1108 * it is found at argv[1] after leaving the
1362671f
JH
1109 * loop.
1110 */
1111 argc++; argv--;
1112 break;
5010cb5f 1113 }
5010cb5f 1114 }
5acd64ed 1115
f9b9faf6 1116 if (!opt.pattern_list)
5010cb5f 1117 die("no pattern given.");
07ea91d8
JH
1118 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
1119 die("cannot mix --fixed-strings and regexp");
1120 if (!opt.fixed)
1121 compile_patterns(&opt);
5acd64ed
JH
1122
1123 /* Check revs and then paths */
1124 for (i = 1; i < argc; i++) {
1125 const char *arg = argv[i];
1362671f 1126 unsigned char sha1[20];
5acd64ed
JH
1127 /* Is it a rev? */
1128 if (!get_sha1(arg, sha1)) {
1129 struct object *object = parse_object(sha1);
5acd64ed
JH
1130 if (!object)
1131 die("bad object %s", arg);
1f1e895f 1132 add_object_array(object, arg, &list);
5acd64ed
JH
1133 continue;
1134 }
1135 if (!strcmp(arg, "--")) {
1136 i++;
1137 seen_dashdash = 1;
1138 }
1139 break;
1362671f 1140 }
5acd64ed
JH
1141
1142 /* The rest are paths */
1143 if (!seen_dashdash) {
1144 int j;
c39c4f47 1145 for (j = i; j < argc; j++)
5acd64ed
JH
1146 verify_filename(prefix, argv[j]);
1147 }
1148
0d042fec 1149 if (i < argc) {
5acd64ed 1150 paths = get_pathspec(prefix, argv + i);
0d042fec
JH
1151 if (opt.prefix_length && opt.relative) {
1152 /* Make sure we do not get outside of paths */
1153 for (i = 0; paths[i]; i++)
1154 if (strncmp(prefix, paths[i], opt.prefix_length))
1155 die("git-grep: cannot generate relative filenames containing '..'");
1156 }
1157 }
1362671f
JH
1158 else if (prefix) {
1159 paths = xcalloc(2, sizeof(const char *));
1160 paths[0] = prefix;
1161 paths[1] = NULL;
1162 }
5010cb5f 1163
1f1e895f 1164 if (!list.nr)
1362671f 1165 return !grep_cache(&opt, paths, cached);
aa8c79ad 1166
5010cb5f 1167 if (cached)
aa8c79ad 1168 die("both --cached and trees are given.");
5010cb5f 1169
1f1e895f 1170 for (i = 0; i < list.nr; i++) {
5010cb5f 1171 struct object *real_obj;
1f1e895f
LT
1172 real_obj = deref_tag(list.objects[i].item, NULL, 0);
1173 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
5010cb5f
JH
1174 hit = 1;
1175 }
1176 return !hit;
1177}