gitweb: Remove git_to_hash function
[git.git] / builtin-grep.c
blob8213ce240232a1dc8a0a498972323a33e8fcb7a0
1 /*
2 * Builtin "git grep"
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"
11 #include "tree-walk.h"
12 #include "builtin.h"
13 #include <regex.h>
14 #include <fnmatch.h>
15 #include <sys/wait.h>
18 * git grep pathspecs are somewhat different from diff-tree pathspecs;
19 * pathname wildcards are allowed.
21 static int pathspec_matches(const char **paths, const char *name)
23 int namelen, i;
24 if (!paths || !*paths)
25 return 1;
26 namelen = strlen(name);
27 for (i = 0; paths[i]; i++) {
28 const char *match = paths[i];
29 int matchlen = strlen(match);
30 const char *cp, *meta;
32 if (!matchlen ||
33 ((matchlen <= namelen) &&
34 !strncmp(name, match, matchlen) &&
35 (match[matchlen-1] == '/' ||
36 name[matchlen] == '\0' || name[matchlen] == '/')))
37 return 1;
38 if (!fnmatch(match, name, 0))
39 return 1;
40 if (name[namelen-1] != '/')
41 continue;
43 /* We are being asked if the directory ("name") is worth
44 * descending into.
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.
50 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
51 char ch = *cp;
52 if (ch == '*' || ch == '[' || ch == '?') {
53 meta = cp;
54 break;
57 if (!meta)
58 meta = cp; /* fully literal */
60 if (namelen <= meta - match) {
61 /* Looking at "Documentation/" and
62 * the pattern says "Documentation/howto/", or
63 * "Documentation/diff*.txt". The name we
64 * have should match prefix.
66 if (!memcmp(match, name, namelen))
67 return 1;
68 continue;
71 if (meta - match < namelen) {
72 /* Looking at "Documentation/howto/" and
73 * the pattern says "Documentation/h*";
74 * match up to "Do.../h"; this avoids descending
75 * into "Documentation/technical/".
77 if (!memcmp(match, name, meta - match))
78 return 1;
79 continue;
82 return 0;
85 enum grep_pat_token {
86 GREP_PATTERN,
87 GREP_AND,
88 GREP_OPEN_PAREN,
89 GREP_CLOSE_PAREN,
90 GREP_NOT,
91 GREP_OR,
94 struct grep_pat {
95 struct grep_pat *next;
96 const char *origin;
97 int no;
98 enum grep_pat_token token;
99 const char *pattern;
100 regex_t regexp;
103 enum grep_expr_node {
104 GREP_NODE_ATOM,
105 GREP_NODE_NOT,
106 GREP_NODE_AND,
107 GREP_NODE_OR,
110 struct 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;
122 struct grep_opt {
123 struct grep_pat *pattern_list;
124 struct grep_pat **pattern_tail;
125 struct grep_expr *pattern_expression;
126 int prefix_length;
127 regex_t regexp;
128 unsigned linenum:1;
129 unsigned invert:1;
130 unsigned name_only:1;
131 unsigned unmatch_name_only:1;
132 unsigned count:1;
133 unsigned word_regexp:1;
134 unsigned fixed:1;
135 #define GREP_BINARY_DEFAULT 0
136 #define GREP_BINARY_NOMATCH 1
137 #define GREP_BINARY_TEXT 2
138 unsigned binary:2;
139 unsigned extended:1;
140 unsigned relative:1;
141 int regflags;
142 unsigned pre_context;
143 unsigned post_context;
146 static void add_pattern(struct grep_opt *opt, const char *pat,
147 const char *origin, int no, enum grep_pat_token t)
149 struct grep_pat *p = xcalloc(1, sizeof(*p));
150 p->pattern = pat;
151 p->origin = origin;
152 p->no = no;
153 p->token = t;
154 *opt->pattern_tail = p;
155 opt->pattern_tail = &p->next;
156 p->next = NULL;
159 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
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);
178 static struct grep_expr *compile_pattern_expr(struct grep_pat **);
179 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
181 struct grep_pat *p;
182 struct grep_expr *x;
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;
206 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
208 struct grep_pat *p;
209 struct grep_expr *x;
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);
228 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
230 struct grep_pat *p;
231 struct grep_expr *x, *y, *z;
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;
248 return x;
251 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
253 struct grep_pat *p;
254 struct grep_expr *x, *y, *z;
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;
268 return x;
271 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
273 return compile_pattern_or(list);
276 static void compile_patterns(struct grep_opt *opt)
278 struct grep_pat *p;
280 /* First compile regexps */
281 for (p = opt->pattern_list; p; p = p->next) {
282 if (p->token == GREP_PATTERN)
283 compile_regexp(p, opt);
284 else
285 opt->extended = 1;
288 if (!opt->extended)
289 return;
291 /* Then bundle them up in an expression.
292 * A classic recursive descent parser would do.
294 p = opt->pattern_list;
295 opt->pattern_expression = compile_pattern_expr(&p);
296 if (p)
297 die("incomplete pattern expression: %s", p->pattern);
300 static char *end_of_line(char *cp, unsigned long *left)
302 unsigned long l = *left;
303 while (l && *cp != '\n') {
304 l--;
305 cp++;
307 *left = l;
308 return cp;
311 static int word_char(char ch)
313 return isalnum(ch) || ch == '_';
316 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
317 const char *name, unsigned lno, char sign)
319 printf("%s%c", name, sign);
320 if (opt->linenum)
321 printf("%d%c", lno, sign);
322 printf("%.*s\n", (int)(eol-bol), bol);
326 * NEEDSWORK: share code with diff.c
328 #define FIRST_FEW_BYTES 8000
329 static int buffer_is_binary(const char *ptr, unsigned long size)
331 if (FIRST_FEW_BYTES < size)
332 size = FIRST_FEW_BYTES;
333 return !!memchr(ptr, 0, size);
336 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
338 char *hit = strstr(line, pattern);
339 if (!hit) {
340 match->rm_so = match->rm_eo = -1;
341 return REG_NOMATCH;
343 else {
344 match->rm_so = hit - line;
345 match->rm_eo = match->rm_so + strlen(pattern);
346 return 0;
350 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
352 int hit = 0;
353 int at_true_bol = 1;
354 regmatch_t pmatch[10];
356 again:
357 if (!opt->fixed) {
358 regex_t *exp = &p->regexp;
359 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
360 pmatch, 0);
362 else {
363 hit = !fixmatch(p->pattern, bol, pmatch);
366 if (hit && opt->word_regexp) {
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");
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).
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])) )
384 else
385 hit = 0;
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!
392 bol = pmatch[0].rm_so + bol + 1;
393 at_true_bol = 0;
394 goto again;
397 return hit;
400 static int match_expr_eval(struct grep_opt *opt,
401 struct grep_expr *x,
402 char *bol, char *eol)
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));
417 die("Unexpected node type (internal error) %d\n", x->node);
420 static int match_expr(struct grep_opt *opt, char *bol, char *eol)
422 struct grep_expr *x = opt->pattern_expression;
423 return match_expr_eval(opt, x, bol, eol);
426 static int match_line(struct grep_opt *opt, char *bol, char *eol)
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;
435 return 0;
438 static int grep_buffer(struct grep_opt *opt, const char *name,
439 char *buf, unsigned long size)
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;
450 int binary_match_only = 0;
451 const char *hunk_mark = "";
452 unsigned count = 0;
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;
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";
472 while (left) {
473 char *eol, ch;
474 int hit = 0;
476 eol = end_of_line(bol, &left);
477 ch = *eol;
478 *eol = 0;
480 hit = match_line(opt, bol, eol);
482 /* "grep -v -e foo -e bla" should list lines
483 * that do not have either, so inversion should
484 * be done outside.
486 if (opt->invert)
487 hit = !hit;
488 if (opt->unmatch_name_only) {
489 if (hit)
490 return 0;
491 goto next_line;
493 if (hit) {
494 count++;
495 if (binary_match_only) {
496 printf("Binary file %s matches\n", name);
497 return 1;
499 if (opt->name_only) {
500 printf("%s\n", name);
501 return 1;
503 /* Hit at this line. If we haven't shown the
504 * pre-context lines, we would need to show them.
505 * When asked to do "count", this still show
506 * the context which is nonsense, but the user
507 * deserves to get that ;-).
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++;
525 last_shown = lno-1;
527 if (last_shown && lno != last_shown + 1)
528 printf(hunk_mark);
529 if (!opt->count)
530 show_line(opt, bol, eol, name, lno, ':');
531 last_shown = last_hit = lno;
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.
538 if (last_shown && lno != last_shown + 1)
539 printf(hunk_mark);
540 show_line(opt, bol, eol, name, lno, '-');
541 last_shown = lno;
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;
550 next_line:
551 *eol = ch;
552 bol = eol + 1;
553 if (!left)
554 break;
555 left--;
556 lno++;
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;
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.
570 if (opt->count && count)
571 printf("%s:%u\n", name, count);
572 return !!last_hit;
575 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
577 unsigned long size;
578 char *data;
579 char type[20];
580 char *to_free = NULL;
581 int hit;
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;
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;
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;
606 hit = grep_buffer(opt, name, data, size);
607 free(data);
608 free(to_free);
609 return hit;
612 static int grep_file(struct grep_opt *opt, const char *filename)
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;
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;
637 close(i);
638 if (opt->relative && opt->prefix_length)
639 filename += opt->prefix_length;
640 i = grep_buffer(opt, filename, data, st.st_size);
641 free(data);
642 return i;
645 static int exec_grep(int argc, const char **argv)
647 pid_t pid;
648 int status;
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);
658 while (waitpid(pid, &status, 0) < 0) {
659 if (errno == EINTR)
660 continue;
661 return -1;
663 if (WIFEXITED(status)) {
664 if (!WEXITSTATUS(status))
665 return 1;
666 return 0;
668 return -1;
671 #define MAXARGS 1000
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)
678 static int external_grep(struct grep_opt *opt, const char **paths, int cached)
680 int i, nr, argc, hit, len, status;
681 const char *argv[MAXARGS+1];
682 char randarg[ARGBUF];
683 char *argptr = randarg;
684 struct grep_pat *p;
686 if (opt->extended || (opt->relative && opt->prefix_length))
687 return -1;
688 len = nr = 0;
689 push_arg("grep");
690 if (opt->fixed)
691 push_arg("-F");
692 if (opt->linenum)
693 push_arg("-n");
694 if (opt->regflags & REG_EXTENDED)
695 push_arg("-E");
696 if (opt->regflags & REG_ICASE)
697 push_arg("-i");
698 if (opt->word_regexp)
699 push_arg("-w");
700 if (opt->name_only)
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;
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;
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;
737 for (p = opt->pattern_list; p; p = p->next) {
738 push_arg("-e");
739 push_arg(p->pattern);
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.
748 * GNU grep has "-H", but this is portable.
750 if (!opt->name_only && !opt->unmatch_name_only)
751 push_arg("/dev/null");
753 hit = 0;
754 argc = nr;
755 for (i = 0; i < active_nr; i++) {
756 struct cache_entry *ce = active_cache[i];
757 char *name;
758 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
759 continue;
760 if (!pathspec_matches(paths, ce->name))
761 continue;
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);
769 argv[argc++] = name;
770 if (argc < MAXARGS)
771 continue;
772 status = exec_grep(argc, argv);
773 if (0 < status)
774 hit = 1;
775 argc = nr;
777 if (argc > nr) {
778 status = exec_grep(argc, argv);
779 if (0 < status)
780 hit = 1;
782 return hit;
785 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
787 int hit = 0;
788 int nr;
789 read_cache();
791 #ifdef __unix__
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
797 if (!cached) {
798 hit = external_grep(opt, paths, cached);
799 if (hit >= 0)
800 return hit;
802 #endif
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;
808 if (!pathspec_matches(paths, ce->name))
809 continue;
810 if (cached)
811 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
812 else
813 hit |= grep_file(opt, ce->name);
815 return hit;
818 static int grep_tree(struct grep_opt *opt, const char **paths,
819 struct tree_desc *tree,
820 const char *tree_name, const char *base)
822 int len;
823 int hit = 0;
824 struct name_entry entry;
825 char *down;
826 int tn_len = strlen(tree_name);
827 char *path_buf = xmalloc(PATH_MAX + tn_len + 100);
829 if (tn_len) {
830 tn_len = sprintf(path_buf, "%s:", tree_name);
831 down = path_buf + tn_len;
832 strcat(down, base);
834 else {
835 down = path_buf;
836 strcpy(down, base);
838 len = strlen(path_buf);
840 while (tree_entry(tree, &entry)) {
841 strcpy(path_buf + len, entry.path);
843 if (S_ISDIR(entry.mode))
844 /* Match "abc/" against pathspec to
845 * decide if we want to descend into "abc"
846 * directory.
848 strcpy(path_buf + len + entry.pathlen, "/");
850 if (!pathspec_matches(paths, down))
852 else if (S_ISREG(entry.mode))
853 hit |= grep_sha1(opt, entry.sha1, path_buf, tn_len);
854 else if (S_ISDIR(entry.mode)) {
855 char type[20];
856 struct tree_desc sub;
857 void *data;
858 data = read_sha1_file(entry.sha1, type, &sub.size);
859 if (!data)
860 die("unable to read tree (%s)",
861 sha1_to_hex(entry.sha1));
862 sub.buf = data;
863 hit |= grep_tree(opt, paths, &sub, tree_name, down);
864 free(data);
867 return hit;
870 static int grep_object(struct grep_opt *opt, const char **paths,
871 struct object *obj, const char *name)
873 if (obj->type == OBJ_BLOB)
874 return grep_sha1(opt, obj->sha1, name, 0);
875 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
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;
884 hit = grep_tree(opt, paths, &tree, name, "");
885 free(data);
886 return hit;
888 die("unable to grep from object of type %s", typename(obj->type));
891 static const char builtin_grep_usage[] =
892 "git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
894 static const char emsg_invalid_context_len[] =
895 "%s: invalid context length argument";
896 static const char emsg_missing_context_len[] =
897 "missing context length argument";
898 static const char emsg_missing_argument[] =
899 "option requires an argument -%s";
901 int cmd_grep(int argc, const char **argv, const char *prefix)
903 int hit = 0;
904 int cached = 0;
905 int seen_dashdash = 0;
906 struct grep_opt opt;
907 struct object_array list = { 0, 0, NULL };
908 const char **paths = NULL;
909 int i;
911 memset(&opt, 0, sizeof(opt));
912 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
913 opt.relative = 1;
914 opt.pattern_tail = &opt.pattern_list;
915 opt.regflags = REG_NEWLINE;
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
924 * unrecognized non option is the beginning of the refs list
925 * that continues up to the -- (if exists), and then paths.
928 while (1 < argc) {
929 const char *arg = argv[1];
930 argc--; argv++;
931 if (!strcmp("--cached", arg)) {
932 cached = 1;
933 continue;
935 if (!strcmp("-a", arg) ||
936 !strcmp("--text", arg)) {
937 opt.binary = GREP_BINARY_TEXT;
938 continue;
940 if (!strcmp("-i", arg) ||
941 !strcmp("--ignore-case", arg)) {
942 opt.regflags |= REG_ICASE;
943 continue;
945 if (!strcmp("-I", arg)) {
946 opt.binary = GREP_BINARY_NOMATCH;
947 continue;
949 if (!strcmp("-v", arg) ||
950 !strcmp("--invert-match", arg)) {
951 opt.invert = 1;
952 continue;
954 if (!strcmp("-E", arg) ||
955 !strcmp("--extended-regexp", arg)) {
956 opt.regflags |= REG_EXTENDED;
957 continue;
959 if (!strcmp("-F", arg) ||
960 !strcmp("--fixed-strings", arg)) {
961 opt.fixed = 1;
962 continue;
964 if (!strcmp("-G", arg) ||
965 !strcmp("--basic-regexp", arg)) {
966 opt.regflags &= ~REG_EXTENDED;
967 continue;
969 if (!strcmp("-n", arg)) {
970 opt.linenum = 1;
971 continue;
973 if (!strcmp("-H", arg)) {
974 /* We always show the pathname, so this
975 * is a noop.
977 continue;
979 if (!strcmp("-l", arg) ||
980 !strcmp("--files-with-matches", arg)) {
981 opt.name_only = 1;
982 continue;
984 if (!strcmp("-L", arg) ||
985 !strcmp("--files-without-match", arg)) {
986 opt.unmatch_name_only = 1;
987 continue;
989 if (!strcmp("-c", arg) ||
990 !strcmp("--count", arg)) {
991 opt.count = 1;
992 continue;
994 if (!strcmp("-w", arg) ||
995 !strcmp("--word-regexp", arg)) {
996 opt.word_regexp = 1;
997 continue;
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')) {
1003 unsigned num;
1004 const char *scan;
1005 switch (arg[1]) {
1006 case 'A': case 'B': case 'C':
1007 if (!arg[2]) {
1008 if (argc <= 1)
1009 die(emsg_missing_context_len);
1010 scan = *++argv;
1011 argc--;
1013 else
1014 scan = arg + 2;
1015 break;
1016 default:
1017 scan = arg + 1;
1018 break;
1020 if (sscanf(scan, "%u", &num) != 1)
1021 die(emsg_invalid_context_len, scan);
1022 switch (arg[1]) {
1023 case 'A':
1024 opt.post_context = num;
1025 break;
1026 default:
1027 case 'C':
1028 opt.post_context = num;
1029 case 'B':
1030 opt.pre_context = num;
1031 break;
1033 continue;
1035 if (!strcmp("-f", arg)) {
1036 FILE *patterns;
1037 int lno = 0;
1038 char buf[1024];
1039 if (argc <= 1)
1040 die(emsg_missing_argument, arg);
1041 patterns = fopen(argv[1], "r");
1042 if (!patterns)
1043 die("'%s': %s", argv[1], strerror(errno));
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;
1051 add_pattern(&opt, strdup(buf), argv[1], ++lno,
1052 GREP_PATTERN);
1054 fclose(patterns);
1055 argv++;
1056 argc--;
1057 continue;
1059 if (!strcmp("--not", arg)) {
1060 add_pattern(&opt, arg, "command line", 0, GREP_NOT);
1061 continue;
1063 if (!strcmp("--and", arg)) {
1064 add_pattern(&opt, arg, "command line", 0, GREP_AND);
1065 continue;
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;
1073 if (!strcmp(")", arg)) {
1074 add_pattern(&opt, arg, "command line", 0, GREP_CLOSE_PAREN);
1075 continue;
1077 if (!strcmp("-e", arg)) {
1078 if (1 < argc) {
1079 add_pattern(&opt, argv[1], "-e option", 0,
1080 GREP_PATTERN);
1081 argv++;
1082 argc--;
1083 continue;
1085 die(emsg_missing_argument, arg);
1087 if (!strcmp("--full-name", arg)) {
1088 opt.relative = 0;
1089 continue;
1091 if (!strcmp("--", arg)) {
1092 /* later processing wants to have this at argv[1] */
1093 argv--;
1094 argc++;
1095 break;
1097 if (*arg == '-')
1098 usage(builtin_grep_usage);
1100 /* First unrecognized non-option token */
1101 if (!opt.pattern_list) {
1102 add_pattern(&opt, arg, "command line", 0,
1103 GREP_PATTERN);
1104 break;
1106 else {
1107 /* We are looking at the first path or rev;
1108 * it is found at argv[1] after leaving the
1109 * loop.
1111 argc++; argv--;
1112 break;
1116 if (!opt.pattern_list)
1117 die("no pattern given.");
1118 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
1119 die("cannot mix --fixed-strings and regexp");
1120 if (!opt.fixed)
1121 compile_patterns(&opt);
1123 /* Check revs and then paths */
1124 for (i = 1; i < argc; i++) {
1125 const char *arg = argv[i];
1126 unsigned char sha1[20];
1127 /* Is it a rev? */
1128 if (!get_sha1(arg, sha1)) {
1129 struct object *object = parse_object(sha1);
1130 if (!object)
1131 die("bad object %s", arg);
1132 add_object_array(object, arg, &list);
1133 continue;
1135 if (!strcmp(arg, "--")) {
1136 i++;
1137 seen_dashdash = 1;
1139 break;
1142 /* The rest are paths */
1143 if (!seen_dashdash) {
1144 int j;
1145 for (j = i; j < argc; j++)
1146 verify_filename(prefix, argv[j]);
1149 if (i < argc) {
1150 paths = get_pathspec(prefix, argv + i);
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 '..'");
1158 else if (prefix) {
1159 paths = xcalloc(2, sizeof(const char *));
1160 paths[0] = prefix;
1161 paths[1] = NULL;
1164 if (!list.nr)
1165 return !grep_cache(&opt, paths, cached);
1167 if (cached)
1168 die("both --cached and trees are given.");
1170 for (i = 0; i < list.nr; i++) {
1171 struct object *real_obj;
1172 real_obj = deref_tag(list.objects[i].item, NULL, 0);
1173 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
1174 hit = 1;
1176 return !hit;