4 * Copyright (c) 2006 Junio C Hamano
11 #include "tree-walk.h"
17 * git grep pathspecs are somewhat different from diff-tree pathspecs;
18 * pathname wildcards are allowed.
20 static int pathspec_matches(const char **paths, const char *name)
23 if (!paths || !*paths)
25 namelen = strlen(name);
26 for (i = 0; paths[i]; i++) {
27 const char *match = paths[i];
28 int matchlen = strlen(match);
29 const char *cp, *meta;
31 if ((matchlen <= namelen) &&
32 !strncmp(name, match, matchlen) &&
33 (match[matchlen-1] == '/' ||
34 name[matchlen] == '\0' || name[matchlen] == '/'))
36 if (!fnmatch(match, name, 0))
38 if (name[namelen-1] != '/')
41 /* We are being asked if the directory ("name") is worth
44 * Find the longest leading directory name that does
45 * not have metacharacter in the pathspec; the name
46 * we are looking at must overlap with that directory.
48 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
50 if (ch == '*' || ch == '[' || ch == '?') {
56 meta = cp; /* fully literal */
58 if (namelen <= meta - match) {
59 /* Looking at "Documentation/" and
60 * the pattern says "Documentation/howto/", or
61 * "Documentation/diff*.txt". The name we
62 * have should match prefix.
64 if (!memcmp(match, name, namelen))
69 if (meta - match < namelen) {
70 /* Looking at "Documentation/howto/" and
71 * the pattern says "Documentation/h*";
72 * match up to "Do.../h"; this avoids descending
73 * into "Documentation/technical/".
75 if (!memcmp(match, name, meta - match))
84 struct grep_pat *next;
92 struct grep_pat *pattern_list;
93 struct grep_pat **pattern_tail;
98 unsigned unmatch_name_only:1;
100 unsigned word_regexp:1;
102 #define GREP_BINARY_DEFAULT 0
103 #define GREP_BINARY_NOMATCH 1
104 #define GREP_BINARY_TEXT 2
107 unsigned pre_context;
108 unsigned post_context;
111 static void add_pattern(struct grep_opt *opt, const char *pat,
112 const char *origin, int no)
114 struct grep_pat *p = xcalloc(1, sizeof(*p));
118 *opt->pattern_tail = p;
119 opt->pattern_tail = &p->next;
123 static void compile_patterns(struct grep_opt *opt)
126 for (p = opt->pattern_list; p; p = p->next) {
127 int err = regcomp(&p->regexp, p->pattern, opt->regflags);
132 sprintf(where, "In '%s' at %d, ",
135 sprintf(where, "%s, ", p->origin);
138 regerror(err, &p->regexp, errbuf, 1024);
140 die("%s'%s': %s", where, p->pattern, errbuf);
145 static char *end_of_line(char *cp, unsigned long *left)
147 unsigned long l = *left;
148 while (l && *cp != '\n') {
156 static int word_char(char ch)
158 return isalnum(ch) || ch == '_';
161 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
162 const char *name, unsigned lno, char sign)
164 printf("%s%c", name, sign);
166 printf("%d%c", lno, sign);
167 printf("%.*s\n", (int)(eol-bol), bol);
171 * NEEDSWORK: share code with diff.c
173 #define FIRST_FEW_BYTES 8000
174 static int buffer_is_binary(const char *ptr, unsigned long size)
176 if (FIRST_FEW_BYTES < size)
177 size = FIRST_FEW_BYTES;
178 if (memchr(ptr, 0, size))
183 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
185 char *hit = strstr(line, pattern);
187 match->rm_so = match->rm_eo = -1;
191 match->rm_so = hit - line;
192 match->rm_eo = match->rm_so + strlen(pattern);
197 static int grep_buffer(struct grep_opt *opt, const char *name,
198 char *buf, unsigned long size)
201 unsigned long left = size;
203 struct pre_context_line {
206 } *prev = NULL, *pcl;
207 unsigned last_hit = 0;
208 unsigned last_shown = 0;
209 int binary_match_only = 0;
210 const char *hunk_mark = "";
213 if (buffer_is_binary(buf, size)) {
214 switch (opt->binary) {
215 case GREP_BINARY_DEFAULT:
216 binary_match_only = 1;
218 case GREP_BINARY_NOMATCH:
219 return 0; /* Assume unmatch */
226 if (opt->pre_context)
227 prev = xcalloc(opt->pre_context, sizeof(*prev));
228 if (opt->pre_context || opt->post_context)
232 regmatch_t pmatch[10];
237 eol = end_of_line(bol, &left);
241 for (p = opt->pattern_list; p; p = p->next) {
243 regex_t *exp = &p->regexp;
244 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
248 hit = !fixmatch(p->pattern, bol, pmatch);
251 if (hit && opt->word_regexp) {
252 /* Match beginning must be either
253 * beginning of the line, or at word
254 * boundary (i.e. the last char must
255 * not be alnum or underscore).
257 if ((pmatch[0].rm_so < 0) ||
258 (eol - bol) <= pmatch[0].rm_so ||
259 (pmatch[0].rm_eo < 0) ||
260 (eol - bol) < pmatch[0].rm_eo)
261 die("regexp returned nonsense");
262 if (pmatch[0].rm_so != 0 &&
263 word_char(bol[pmatch[0].rm_so-1]))
265 if (pmatch[0].rm_eo != (eol-bol) &&
266 word_char(bol[pmatch[0].rm_eo]))
272 /* "grep -v -e foo -e bla" should list lines
273 * that do not have either, so inversion should
278 if (opt->unmatch_name_only) {
285 if (binary_match_only) {
286 printf("Binary file %s matches\n", name);
289 if (opt->name_only) {
290 printf("%s\n", name);
293 /* Hit at this line. If we haven't shown the
294 * pre-context lines, we would need to show them.
295 * When asked to do "count", this still show
296 * the context which is nonsense, but the user
297 * deserves to get that ;-).
299 if (opt->pre_context) {
301 if (opt->pre_context < lno)
302 from = lno - opt->pre_context;
305 if (from <= last_shown)
306 from = last_shown + 1;
307 if (last_shown && from != last_shown + 1)
310 pcl = &prev[lno-from-1];
311 show_line(opt, pcl->bol, pcl->eol,
317 if (last_shown && lno != last_shown + 1)
320 show_line(opt, bol, eol, name, lno, ':');
321 last_shown = last_hit = lno;
324 lno <= last_hit + opt->post_context) {
325 /* If the last hit is within the post context,
326 * we need to show this line.
328 if (last_shown && lno != last_shown + 1)
330 show_line(opt, bol, eol, name, lno, '-');
333 if (opt->pre_context) {
334 memmove(prev+1, prev,
335 (opt->pre_context-1) * sizeof(*prev));
349 if (opt->unmatch_name_only) {
350 /* We did not see any hit, so we want to show this */
351 printf("%s\n", name);
356 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
357 * which feels mostly useless but sometimes useful. Maybe
358 * make it another option? For now suppress them.
360 if (opt->count && count)
361 printf("%s:%u\n", name, count);
365 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
371 data = read_sha1_file(sha1, type, &size);
373 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
376 hit = grep_buffer(opt, name, data, size);
381 static int grep_file(struct grep_opt *opt, const char *filename)
386 if (lstat(filename, &st) < 0) {
389 error("'%s': %s", filename, strerror(errno));
393 return 0; /* empty file -- no grep hit */
394 if (!S_ISREG(st.st_mode))
396 i = open(filename, O_RDONLY);
399 data = xmalloc(st.st_size + 1);
400 if (st.st_size != xread(i, data, st.st_size)) {
401 error("'%s': short read %s", filename, strerror(errno));
407 i = grep_buffer(opt, filename, data, st.st_size);
412 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
418 for (nr = 0; nr < active_nr; nr++) {
419 struct cache_entry *ce = active_cache[nr];
420 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
422 if (!pathspec_matches(paths, ce->name))
425 hit |= grep_sha1(opt, ce->sha1, ce->name);
427 hit |= grep_file(opt, ce->name);
432 static int grep_tree(struct grep_opt *opt, const char **paths,
433 struct tree_desc *tree,
434 const char *tree_name, const char *base)
440 const unsigned char *sha1;
442 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
445 int offset = sprintf(path_buf, "%s:", tree_name);
446 down = path_buf + offset;
453 len = strlen(path_buf);
457 sha1 = tree_entry_extract(tree, &path, &mode);
458 pathlen = strlen(path);
459 strcpy(path_buf + len, path);
462 /* Match "abc/" against pathspec to
463 * decide if we want to descend into "abc"
466 strcpy(path_buf + len + pathlen, "/");
468 if (!pathspec_matches(paths, down))
470 else if (S_ISREG(mode))
471 hit |= grep_sha1(opt, sha1, path_buf);
472 else if (S_ISDIR(mode)) {
474 struct tree_desc sub;
476 data = read_sha1_file(sha1, type, &sub.size);
478 die("unable to read tree (%s)",
481 hit |= grep_tree(opt, paths, &sub, tree_name, down);
484 update_tree_entry(tree);
489 static int grep_object(struct grep_opt *opt, const char **paths,
490 struct object *obj, const char *name)
492 if (!strcmp(obj->type, blob_type))
493 return grep_sha1(opt, obj->sha1, name);
494 if (!strcmp(obj->type, commit_type) ||
495 !strcmp(obj->type, tree_type)) {
496 struct tree_desc tree;
499 data = read_object_with_reference(obj->sha1, tree_type,
502 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
504 hit = grep_tree(opt, paths, &tree, name, "");
508 die("unable to grep from object of type %s", obj->type);
511 static const char builtin_grep_usage[] =
512 "git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
514 int cmd_grep(int argc, const char **argv, char **envp)
518 int seen_dashdash = 0;
520 struct object_list *list, **tail, *object_list = NULL;
521 const char *prefix = setup_git_directory();
522 const char **paths = NULL;
525 memset(&opt, 0, sizeof(opt));
526 opt.pattern_tail = &opt.pattern_list;
527 opt.regflags = REG_NEWLINE;
530 * If there is no -- then the paths must exist in the working
531 * tree. If there is no explicit pattern specified with -e or
532 * -f, we take the first unrecognized non option to be the
533 * pattern, but then what follows it must be zero or more
534 * valid refs up to the -- (if exists), and then existing
535 * paths. If there is an explicit pattern, then the first
536 * unrecocnized non option is the beginning of the refs list
537 * that continues up to the -- (if exists), and then paths.
542 const char *arg = argv[1];
544 if (!strcmp("--cached", arg)) {
548 if (!strcmp("-a", arg) ||
549 !strcmp("--text", arg)) {
550 opt.binary = GREP_BINARY_TEXT;
553 if (!strcmp("-i", arg) ||
554 !strcmp("--ignore-case", arg)) {
555 opt.regflags |= REG_ICASE;
558 if (!strcmp("-I", arg)) {
559 opt.binary = GREP_BINARY_NOMATCH;
562 if (!strcmp("-v", arg) ||
563 !strcmp("--invert-match", arg)) {
567 if (!strcmp("-E", arg) ||
568 !strcmp("--extended-regexp", arg)) {
569 opt.regflags |= REG_EXTENDED;
572 if (!strcmp("-F", arg) ||
573 !strcmp("--fixed-strings", arg)) {
577 if (!strcmp("-G", arg) ||
578 !strcmp("--basic-regexp", arg)) {
579 opt.regflags &= ~REG_EXTENDED;
582 if (!strcmp("-n", arg)) {
586 if (!strcmp("-H", arg)) {
587 /* We always show the pathname, so this
592 if (!strcmp("-l", arg) ||
593 !strcmp("--files-with-matches", arg)) {
597 if (!strcmp("-L", arg) ||
598 !strcmp("--files-without-match", arg)) {
599 opt.unmatch_name_only = 1;
602 if (!strcmp("-c", arg) ||
603 !strcmp("--count", arg)) {
607 if (!strcmp("-w", arg) ||
608 !strcmp("--word-regexp", arg)) {
612 if (!strncmp("-A", arg, 2) ||
613 !strncmp("-B", arg, 2) ||
614 !strncmp("-C", arg, 2) ||
615 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
619 case 'A': case 'B': case 'C':
622 usage(builtin_grep_usage);
633 if (sscanf(scan, "%u", &num) != 1)
634 usage(builtin_grep_usage);
637 opt.post_context = num;
641 opt.post_context = num;
643 opt.pre_context = num;
648 if (!strcmp("-f", arg)) {
653 usage(builtin_grep_usage);
654 patterns = fopen(argv[1], "r");
656 die("'%s': %s", argv[1], strerror(errno));
657 while (fgets(buf, sizeof(buf), patterns)) {
658 int len = strlen(buf);
659 if (buf[len-1] == '\n')
661 /* ignore empty line like grep does */
664 add_pattern(&opt, strdup(buf), argv[1], ++lno);
671 if (!strcmp("-e", arg)) {
673 add_pattern(&opt, argv[1], "-e option", 0);
678 usage(builtin_grep_usage);
680 if (!strcmp("--", arg))
683 usage(builtin_grep_usage);
685 /* First unrecognized non-option token */
686 if (!opt.pattern_list) {
687 add_pattern(&opt, arg, "command line", 0);
691 /* We are looking at the first path or rev;
692 * it is found at argv[1] after leaving the
700 if (!opt.pattern_list)
701 die("no pattern given.");
702 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
703 die("cannot mix --fixed-strings and regexp");
705 compile_patterns(&opt);
707 /* Check revs and then paths */
708 for (i = 1; i < argc; i++) {
709 const char *arg = argv[i];
710 unsigned char sha1[20];
712 if (!get_sha1(arg, sha1)) {
713 struct object *object = parse_object(sha1);
714 struct object_list *elem;
716 die("bad object %s", arg);
717 elem = object_list_insert(object, tail);
722 if (!strcmp(arg, "--")) {
729 /* The rest are paths */
730 if (!seen_dashdash) {
732 for (j = i; j < argc; j++)
733 verify_filename(prefix, argv[j]);
737 paths = get_pathspec(prefix, argv + i);
739 paths = xcalloc(2, sizeof(const char *));
745 return !grep_cache(&opt, paths, cached);
748 die("both --cached and trees are given.");
750 for (list = object_list; list; list = list->next) {
751 struct object *real_obj;
752 real_obj = deref_tag(list->item, NULL, 0);
753 if (grep_object(&opt, paths, real_obj, list->name))