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;
90 struct grep_pat *pattern_list;
91 struct grep_pat **pattern_tail;
96 unsigned unmatch_name_only:1;
98 unsigned word_regexp:1;
99 #define GREP_BINARY_DEFAULT 0
100 #define GREP_BINARY_NOMATCH 1
101 #define GREP_BINARY_TEXT 2
104 unsigned pre_context;
105 unsigned post_context;
108 static void add_pattern(struct grep_opt *opt, const char *pat)
110 struct grep_pat *p = xcalloc(1, sizeof(*p));
112 *opt->pattern_tail = p;
113 opt->pattern_tail = &p->next;
117 static void compile_patterns(struct grep_opt *opt)
120 for (p = opt->pattern_list; p; p = p->next) {
121 int err = regcomp(&p->regexp, p->pattern, opt->regflags);
124 regerror(err, &p->regexp, errbuf, 1024);
126 die("'%s': %s", p->pattern, errbuf);
131 static char *end_of_line(char *cp, unsigned long *left)
133 unsigned long l = *left;
134 while (l && *cp != '\n') {
142 static int word_char(char ch)
144 return isalnum(ch) || ch == '_';
147 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
148 const char *name, unsigned lno, char sign)
150 printf("%s%c", name, sign);
152 printf("%d%c", lno, sign);
153 printf("%.*s\n", (int)(eol-bol), bol);
157 * NEEDSWORK: share code with diff.c
159 #define FIRST_FEW_BYTES 8000
160 static int buffer_is_binary(const char *ptr, unsigned long size)
162 if (FIRST_FEW_BYTES < size)
163 size = FIRST_FEW_BYTES;
164 if (memchr(ptr, 0, size))
169 static int grep_buffer(struct grep_opt *opt, const char *name,
170 char *buf, unsigned long size)
173 unsigned long left = size;
175 struct pre_context_line {
178 } *prev = NULL, *pcl;
179 unsigned last_hit = 0;
180 unsigned last_shown = 0;
181 int binary_match_only = 0;
182 const char *hunk_mark = "";
185 if (buffer_is_binary(buf, size)) {
186 switch (opt->binary) {
187 case GREP_BINARY_DEFAULT:
188 binary_match_only = 1;
190 case GREP_BINARY_NOMATCH:
191 return 0; /* Assume unmatch */
198 if (opt->pre_context)
199 prev = xcalloc(opt->pre_context, sizeof(*prev));
200 if (opt->pre_context || opt->post_context)
204 regmatch_t pmatch[10];
209 eol = end_of_line(bol, &left);
213 for (p = opt->pattern_list; p; p = p->next) {
214 regex_t *exp = &p->regexp;
215 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
218 if (hit && opt->word_regexp) {
219 /* Match beginning must be either
220 * beginning of the line, or at word
221 * boundary (i.e. the last char must
222 * not be alnum or underscore).
224 if ((pmatch[0].rm_so < 0) ||
225 (eol - bol) <= pmatch[0].rm_so ||
226 (pmatch[0].rm_eo < 0) ||
227 (eol - bol) < pmatch[0].rm_eo)
228 die("regexp returned nonsense");
229 if (pmatch[0].rm_so != 0 &&
230 word_char(bol[pmatch[0].rm_so-1]))
231 continue; /* not a word boundary */
232 if ((eol-bol) < pmatch[0].rm_eo &&
233 word_char(bol[pmatch[0].rm_eo]))
234 continue; /* not a word boundary */
239 /* "grep -v -e foo -e bla" should list lines
240 * that do not have either, so inversion should
245 if (opt->unmatch_name_only) {
252 if (binary_match_only) {
253 printf("Binary file %s matches\n", name);
256 if (opt->name_only) {
257 printf("%s\n", name);
260 /* Hit at this line. If we haven't shown the
261 * pre-context lines, we would need to show them.
262 * When asked to do "count", this still show
263 * the context which is nonsense, but the user
264 * deserves to get that ;-).
266 if (opt->pre_context) {
268 if (opt->pre_context < lno)
269 from = lno - opt->pre_context;
272 if (from <= last_shown)
273 from = last_shown + 1;
274 if (last_shown && from != last_shown + 1)
277 pcl = &prev[lno-from-1];
278 show_line(opt, pcl->bol, pcl->eol,
284 if (last_shown && lno != last_shown + 1)
287 show_line(opt, bol, eol, name, lno, ':');
288 last_shown = last_hit = lno;
291 lno <= last_hit + opt->post_context) {
292 /* If the last hit is within the post context,
293 * we need to show this line.
295 if (last_shown && lno != last_shown + 1)
297 show_line(opt, bol, eol, name, lno, '-');
300 if (opt->pre_context) {
301 memmove(prev+1, prev,
302 (opt->pre_context-1) * sizeof(*prev));
316 if (opt->unmatch_name_only) {
317 /* We did not see any hit, so we want to show this */
318 printf("%s\n", name);
323 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
324 * which feels mostly useless but sometimes useful. Maybe
325 * make it another option? For now suppress them.
327 if (opt->count && count)
328 printf("%s:%u\n", name, count);
332 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
338 data = read_sha1_file(sha1, type, &size);
340 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
343 hit = grep_buffer(opt, name, data, size);
348 static int grep_file(struct grep_opt *opt, const char *filename)
353 if (lstat(filename, &st) < 0) {
356 error("'%s': %s", filename, strerror(errno));
360 return 0; /* empty file -- no grep hit */
361 if (!S_ISREG(st.st_mode))
363 i = open(filename, O_RDONLY);
366 data = xmalloc(st.st_size + 1);
367 if (st.st_size != xread(i, data, st.st_size)) {
368 error("'%s': short read %s", filename, strerror(errno));
374 i = grep_buffer(opt, filename, data, st.st_size);
379 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
385 for (nr = 0; nr < active_nr; nr++) {
386 struct cache_entry *ce = active_cache[nr];
387 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
389 if (!pathspec_matches(paths, ce->name))
392 hit |= grep_sha1(opt, ce->sha1, ce->name);
394 hit |= grep_file(opt, ce->name);
399 static int grep_tree(struct grep_opt *opt, const char **paths,
400 struct tree_desc *tree,
401 const char *tree_name, const char *base)
407 const unsigned char *sha1;
409 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
412 int offset = sprintf(path_buf, "%s:", tree_name);
413 down = path_buf + offset;
420 len = strlen(path_buf);
424 sha1 = tree_entry_extract(tree, &path, &mode);
425 pathlen = strlen(path);
426 strcpy(path_buf + len, path);
429 /* Match "abc/" against pathspec to
430 * decide if we want to descend into "abc"
433 strcpy(path_buf + len + pathlen, "/");
435 if (!pathspec_matches(paths, down))
437 else if (S_ISREG(mode))
438 hit |= grep_sha1(opt, sha1, path_buf);
439 else if (S_ISDIR(mode)) {
441 struct tree_desc sub;
443 data = read_sha1_file(sha1, type, &sub.size);
445 die("unable to read tree (%s)",
448 hit |= grep_tree(opt, paths, &sub, tree_name, down);
451 update_tree_entry(tree);
456 static int grep_object(struct grep_opt *opt, const char **paths,
457 struct object *obj, const char *name)
459 if (!strcmp(obj->type, blob_type))
460 return grep_sha1(opt, obj->sha1, name);
461 if (!strcmp(obj->type, commit_type) ||
462 !strcmp(obj->type, tree_type)) {
463 struct tree_desc tree;
466 data = read_object_with_reference(obj->sha1, tree_type,
469 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
471 hit = grep_tree(opt, paths, &tree, name, "");
475 die("unable to grep from object of type %s", obj->type);
478 static const char builtin_grep_usage[] =
479 "git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
481 int cmd_grep(int argc, const char **argv, char **envp)
484 int no_more_flags = 0;
485 int seen_noncommit = 0;
488 struct object_list *list, **tail, *object_list = NULL;
489 const char *prefix = setup_git_directory();
490 const char **paths = NULL;
492 memset(&opt, 0, sizeof(opt));
493 opt.pattern_tail = &opt.pattern_list;
494 opt.regflags = REG_NEWLINE;
497 * No point using rev_info, really.
500 const char *arg = argv[1];
502 if (!strcmp("--cached", arg)) {
506 if (!strcmp("-a", arg) ||
507 !strcmp("--text", arg)) {
508 opt.binary = GREP_BINARY_TEXT;
511 if (!strcmp("-i", arg) ||
512 !strcmp("--ignore-case", arg)) {
513 opt.regflags |= REG_ICASE;
516 if (!strcmp("-I", arg)) {
517 opt.binary = GREP_BINARY_NOMATCH;
520 if (!strcmp("-v", arg) ||
521 !strcmp("--invert-match", arg)) {
525 if (!strcmp("-E", arg) ||
526 !strcmp("--extended-regexp", arg)) {
527 opt.regflags |= REG_EXTENDED;
530 if (!strcmp("-G", arg) ||
531 !strcmp("--basic-regexp", arg)) {
532 opt.regflags &= ~REG_EXTENDED;
535 if (!strcmp("-n", arg)) {
539 if (!strcmp("-H", arg)) {
540 /* We always show the pathname, so this
545 if (!strcmp("-l", arg) ||
546 !strcmp("--files-with-matches", arg)) {
550 if (!strcmp("-L", arg) ||
551 !strcmp("--files-without-match", arg)) {
552 opt.unmatch_name_only = 1;
555 if (!strcmp("-c", arg) ||
556 !strcmp("--count", arg)) {
560 if (!strcmp("-w", arg) ||
561 !strcmp("--word-regexp", arg)) {
565 if (!strncmp("-A", arg, 2) ||
566 !strncmp("-B", arg, 2) ||
567 !strncmp("-C", arg, 2) ||
568 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
572 case 'A': case 'B': case 'C':
575 usage(builtin_grep_usage);
586 if (sscanf(scan, "%u", &num) != 1)
587 usage(builtin_grep_usage);
590 opt.post_context = num;
594 opt.post_context = num;
596 opt.pre_context = num;
601 if (!strcmp("-e", arg)) {
603 add_pattern(&opt, argv[1]);
608 usage(builtin_grep_usage);
610 if (!strcmp("--", arg)) {
614 /* Either unrecognized option or a single pattern */
615 if (!no_more_flags && *arg == '-')
616 usage(builtin_grep_usage);
617 if (!opt.pattern_list) {
618 add_pattern(&opt, arg);
622 /* We are looking at the first path or rev;
623 * it is found at argv[0] after leaving the
630 if (!opt.pattern_list)
631 die("no pattern given.");
632 compile_patterns(&opt);
635 struct object *object;
636 struct object_list *elem;
637 const char *arg = argv[1];
638 unsigned char sha1[20];
639 if (get_sha1(arg, sha1) < 0)
641 object = parse_object(sha1);
643 die("bad object %s", arg);
644 elem = object_list_insert(object, tail);
650 paths = get_pathspec(prefix, argv + 1);
652 paths = xcalloc(2, sizeof(const char *));
658 return !grep_cache(&opt, paths, cached);
660 * Do not walk "grep -e foo master next pu -- Documentation/"
661 * but do walk "grep -e foo master..next -- Documentation/".
662 * Ranged request mixed with a blob or tree object, like
663 * "grep -e foo v1.0.0:Documentation/ master..next"
664 * so detect that and complain.
666 for (list = object_list; list; list = list->next) {
667 struct object *real_obj;
668 real_obj = deref_tag(list->item, NULL, 0);
669 if (strcmp(real_obj->type, commit_type))
673 die("both --cached and revisions given.");
675 for (list = object_list; list; list = list->next) {
676 struct object *real_obj;
677 real_obj = deref_tag(list->item, NULL, 0);
678 if (grep_object(&opt, paths, real_obj, list->name))