t9001: use older Getopt::Long boolean prefix '--no' rather than '--no-'
[git.git] / builtin-grep.c
blobf88a912ace9195c387566770432a904e2d7adcb7
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 "grep.h"
15 #ifndef NO_EXTERNAL_GREP
16 #ifdef __unix__
17 #define NO_EXTERNAL_GREP 0
18 #else
19 #define NO_EXTERNAL_GREP 1
20 #endif
21 #endif
23 static int builtin_grep;
25 static int grep_config(const char *var, const char *value, void *cb)
27 struct grep_opt *opt = cb;
29 if (!strcmp(var, "color.grep")) {
30 opt->color = git_config_colorbool(var, value, -1);
31 return 0;
33 if (!strcmp(var, "color.grep.external"))
34 return git_config_string(&(opt->color_external), var, value);
35 if (!strcmp(var, "color.grep.match")) {
36 if (!value)
37 return config_error_nonbool(var);
38 color_parse(value, var, opt->color_match);
39 return 0;
41 return git_color_default_config(var, value, cb);
45 * git grep pathspecs are somewhat different from diff-tree pathspecs;
46 * pathname wildcards are allowed.
48 static int pathspec_matches(const char **paths, const char *name)
50 int namelen, i;
51 if (!paths || !*paths)
52 return 1;
53 namelen = strlen(name);
54 for (i = 0; paths[i]; i++) {
55 const char *match = paths[i];
56 int matchlen = strlen(match);
57 const char *cp, *meta;
59 if (!matchlen ||
60 ((matchlen <= namelen) &&
61 !strncmp(name, match, matchlen) &&
62 (match[matchlen-1] == '/' ||
63 name[matchlen] == '\0' || name[matchlen] == '/')))
64 return 1;
65 if (!fnmatch(match, name, 0))
66 return 1;
67 if (name[namelen-1] != '/')
68 continue;
70 /* We are being asked if the directory ("name") is worth
71 * descending into.
73 * Find the longest leading directory name that does
74 * not have metacharacter in the pathspec; the name
75 * we are looking at must overlap with that directory.
77 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
78 char ch = *cp;
79 if (ch == '*' || ch == '[' || ch == '?') {
80 meta = cp;
81 break;
84 if (!meta)
85 meta = cp; /* fully literal */
87 if (namelen <= meta - match) {
88 /* Looking at "Documentation/" and
89 * the pattern says "Documentation/howto/", or
90 * "Documentation/diff*.txt". The name we
91 * have should match prefix.
93 if (!memcmp(match, name, namelen))
94 return 1;
95 continue;
98 if (meta - match < namelen) {
99 /* Looking at "Documentation/howto/" and
100 * the pattern says "Documentation/h*";
101 * match up to "Do.../h"; this avoids descending
102 * into "Documentation/technical/".
104 if (!memcmp(match, name, meta - match))
105 return 1;
106 continue;
109 return 0;
112 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
114 unsigned long size;
115 char *data;
116 enum object_type type;
117 char *to_free = NULL;
118 int hit;
120 data = read_sha1_file(sha1, &type, &size);
121 if (!data) {
122 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
123 return 0;
125 if (opt->relative && opt->prefix_length) {
126 static char name_buf[PATH_MAX];
127 char *cp;
128 int name_len = strlen(name) - opt->prefix_length + 1;
130 if (!tree_name_len)
131 name += opt->prefix_length;
132 else {
133 if (ARRAY_SIZE(name_buf) <= name_len)
134 cp = to_free = xmalloc(name_len);
135 else
136 cp = name_buf;
137 memcpy(cp, name, tree_name_len);
138 strcpy(cp + tree_name_len,
139 name + tree_name_len + opt->prefix_length);
140 name = cp;
143 hit = grep_buffer(opt, name, data, size);
144 free(data);
145 free(to_free);
146 return hit;
149 static int grep_file(struct grep_opt *opt, const char *filename)
151 struct stat st;
152 int i;
153 char *data;
154 size_t sz;
156 if (lstat(filename, &st) < 0) {
157 err_ret:
158 if (errno != ENOENT)
159 error("'%s': %s", filename, strerror(errno));
160 return 0;
162 if (!st.st_size)
163 return 0; /* empty file -- no grep hit */
164 if (!S_ISREG(st.st_mode))
165 return 0;
166 sz = xsize_t(st.st_size);
167 i = open(filename, O_RDONLY);
168 if (i < 0)
169 goto err_ret;
170 data = xmalloc(sz + 1);
171 if (st.st_size != read_in_full(i, data, sz)) {
172 error("'%s': short read %s", filename, strerror(errno));
173 close(i);
174 free(data);
175 return 0;
177 close(i);
178 if (opt->relative && opt->prefix_length)
179 filename += opt->prefix_length;
180 i = grep_buffer(opt, filename, data, sz);
181 free(data);
182 return i;
185 #if !NO_EXTERNAL_GREP
186 static int exec_grep(int argc, const char **argv)
188 pid_t pid;
189 int status;
191 argv[argc] = NULL;
192 pid = fork();
193 if (pid < 0)
194 return pid;
195 if (!pid) {
196 execvp("grep", (char **) argv);
197 exit(255);
199 while (waitpid(pid, &status, 0) < 0) {
200 if (errno == EINTR)
201 continue;
202 return -1;
204 if (WIFEXITED(status)) {
205 if (!WEXITSTATUS(status))
206 return 1;
207 return 0;
209 return -1;
212 #define MAXARGS 1000
213 #define ARGBUF 4096
214 #define push_arg(a) do { \
215 if (nr < MAXARGS) argv[nr++] = (a); \
216 else die("maximum number of args exceeded"); \
217 } while (0)
220 * If you send a singleton filename to grep, it does not give
221 * the name of the file. GNU grep has "-H" but we would want
222 * that behaviour in a portable way.
224 * So we keep two pathnames in argv buffer unsent to grep in
225 * the main loop if we need to do more than one grep.
227 static int flush_grep(struct grep_opt *opt,
228 int argc, int arg0, const char **argv, int *kept)
230 int status;
231 int count = argc - arg0;
232 const char *kept_0 = NULL;
234 if (count <= 2) {
236 * Because we keep at least 2 paths in the call from
237 * the main loop (i.e. kept != NULL), and MAXARGS is
238 * far greater than 2, this usually is a call to
239 * conclude the grep. However, the user could attempt
240 * to overflow the argv buffer by giving too many
241 * options to leave very small number of real
242 * arguments even for the call in the main loop.
244 if (kept)
245 die("insanely many options to grep");
248 * If we have two or more paths, we do not have to do
249 * anything special, but we need to push /dev/null to
250 * get "-H" behaviour of GNU grep portably but when we
251 * are not doing "-l" nor "-L" nor "-c".
253 if (count == 1 &&
254 !opt->name_only &&
255 !opt->unmatch_name_only &&
256 !opt->count) {
257 argv[argc++] = "/dev/null";
258 argv[argc] = NULL;
262 else if (kept) {
264 * Called because we found many paths and haven't finished
265 * iterating over the cache yet. We keep two paths
266 * for the concluding call. argv[argc-2] and argv[argc-1]
267 * has the last two paths, so save the first one away,
268 * replace it with NULL while sending the list to grep,
269 * and recover them after we are done.
271 *kept = 2;
272 kept_0 = argv[argc-2];
273 argv[argc-2] = NULL;
274 argc -= 2;
277 status = exec_grep(argc, argv);
279 if (kept_0) {
281 * Then recover them. Now the last arg is beyond the
282 * terminating NULL which is at argc, and the second
283 * from the last is what we saved away in kept_0
285 argv[arg0++] = kept_0;
286 argv[arg0] = argv[argc+1];
288 return status;
291 static void grep_add_color(struct strbuf *sb, const char *escape_seq)
293 size_t orig_len = sb->len;
295 while (*escape_seq) {
296 if (*escape_seq == 'm')
297 strbuf_addch(sb, ';');
298 else if (*escape_seq != '\033' && *escape_seq != '[')
299 strbuf_addch(sb, *escape_seq);
300 escape_seq++;
302 if (sb->len > orig_len && sb->buf[sb->len - 1] == ';')
303 strbuf_setlen(sb, sb->len - 1);
306 static int external_grep(struct grep_opt *opt, const char **paths, int cached)
308 int i, nr, argc, hit, len, status;
309 const char *argv[MAXARGS+1];
310 char randarg[ARGBUF];
311 char *argptr = randarg;
312 struct grep_pat *p;
314 if (opt->extended || (opt->relative && opt->prefix_length))
315 return -1;
316 len = nr = 0;
317 push_arg("grep");
318 if (opt->fixed)
319 push_arg("-F");
320 if (opt->linenum)
321 push_arg("-n");
322 if (!opt->pathname)
323 push_arg("-h");
324 if (opt->regflags & REG_EXTENDED)
325 push_arg("-E");
326 if (opt->regflags & REG_ICASE)
327 push_arg("-i");
328 if (opt->binary == GREP_BINARY_NOMATCH)
329 push_arg("-I");
330 if (opt->word_regexp)
331 push_arg("-w");
332 if (opt->name_only)
333 push_arg("-l");
334 if (opt->unmatch_name_only)
335 push_arg("-L");
336 if (opt->null_following_name)
337 /* in GNU grep git's "-z" translates to "-Z" */
338 push_arg("-Z");
339 if (opt->count)
340 push_arg("-c");
341 if (opt->post_context || opt->pre_context) {
342 if (opt->post_context != opt->pre_context) {
343 if (opt->pre_context) {
344 push_arg("-B");
345 len += snprintf(argptr, sizeof(randarg)-len,
346 "%u", opt->pre_context) + 1;
347 if (sizeof(randarg) <= len)
348 die("maximum length of args exceeded");
349 push_arg(argptr);
350 argptr += len;
352 if (opt->post_context) {
353 push_arg("-A");
354 len += snprintf(argptr, sizeof(randarg)-len,
355 "%u", opt->post_context) + 1;
356 if (sizeof(randarg) <= len)
357 die("maximum length of args exceeded");
358 push_arg(argptr);
359 argptr += len;
362 else {
363 push_arg("-C");
364 len += snprintf(argptr, sizeof(randarg)-len,
365 "%u", opt->post_context) + 1;
366 if (sizeof(randarg) <= len)
367 die("maximum length of args exceeded");
368 push_arg(argptr);
369 argptr += len;
372 for (p = opt->pattern_list; p; p = p->next) {
373 push_arg("-e");
374 push_arg(p->pattern);
376 if (opt->color) {
377 struct strbuf sb = STRBUF_INIT;
379 grep_add_color(&sb, opt->color_match);
380 setenv("GREP_COLOR", sb.buf, 1);
382 strbuf_reset(&sb);
383 strbuf_addstr(&sb, "mt=");
384 grep_add_color(&sb, opt->color_match);
385 strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se=");
386 setenv("GREP_COLORS", sb.buf, 1);
388 strbuf_release(&sb);
390 if (opt->color_external && strlen(opt->color_external) > 0)
391 push_arg(opt->color_external);
394 hit = 0;
395 argc = nr;
396 for (i = 0; i < active_nr; i++) {
397 struct cache_entry *ce = active_cache[i];
398 char *name;
399 int kept;
400 if (!S_ISREG(ce->ce_mode))
401 continue;
402 if (!pathspec_matches(paths, ce->name))
403 continue;
404 name = ce->name;
405 if (name[0] == '-') {
406 int len = ce_namelen(ce);
407 name = xmalloc(len + 3);
408 memcpy(name, "./", 2);
409 memcpy(name + 2, ce->name, len + 1);
411 argv[argc++] = name;
412 if (MAXARGS <= argc) {
413 status = flush_grep(opt, argc, nr, argv, &kept);
414 if (0 < status)
415 hit = 1;
416 argc = nr + kept;
418 if (ce_stage(ce)) {
419 do {
420 i++;
421 } while (i < active_nr &&
422 !strcmp(ce->name, active_cache[i]->name));
423 i--; /* compensate for loop control */
426 if (argc > nr) {
427 status = flush_grep(opt, argc, nr, argv, NULL);
428 if (0 < status)
429 hit = 1;
431 return hit;
433 #endif
435 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
437 int hit = 0;
438 int nr;
439 read_cache();
441 #if !NO_EXTERNAL_GREP
443 * Use the external "grep" command for the case where
444 * we grep through the checked-out files. It tends to
445 * be a lot more optimized
447 if (!cached && !builtin_grep) {
448 hit = external_grep(opt, paths, cached);
449 if (hit >= 0)
450 return hit;
452 #endif
454 for (nr = 0; nr < active_nr; nr++) {
455 struct cache_entry *ce = active_cache[nr];
456 if (!S_ISREG(ce->ce_mode))
457 continue;
458 if (!pathspec_matches(paths, ce->name))
459 continue;
461 * If CE_VALID is on, we assume worktree file and its cache entry
462 * are identical, even if worktree file has been modified, so use
463 * cache version instead
465 if (cached || (ce->ce_flags & CE_VALID)) {
466 if (ce_stage(ce))
467 continue;
468 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
470 else
471 hit |= grep_file(opt, ce->name);
472 if (ce_stage(ce)) {
473 do {
474 nr++;
475 } while (nr < active_nr &&
476 !strcmp(ce->name, active_cache[nr]->name));
477 nr--; /* compensate for loop control */
480 free_grep_patterns(opt);
481 return hit;
484 static int grep_tree(struct grep_opt *opt, const char **paths,
485 struct tree_desc *tree,
486 const char *tree_name, const char *base)
488 int len;
489 int hit = 0;
490 struct name_entry entry;
491 char *down;
492 int tn_len = strlen(tree_name);
493 struct strbuf pathbuf;
495 strbuf_init(&pathbuf, PATH_MAX + tn_len);
497 if (tn_len) {
498 strbuf_add(&pathbuf, tree_name, tn_len);
499 strbuf_addch(&pathbuf, ':');
500 tn_len = pathbuf.len;
502 strbuf_addstr(&pathbuf, base);
503 len = pathbuf.len;
505 while (tree_entry(tree, &entry)) {
506 int te_len = tree_entry_len(entry.path, entry.sha1);
507 pathbuf.len = len;
508 strbuf_add(&pathbuf, entry.path, te_len);
510 if (S_ISDIR(entry.mode))
511 /* Match "abc/" against pathspec to
512 * decide if we want to descend into "abc"
513 * directory.
515 strbuf_addch(&pathbuf, '/');
517 down = pathbuf.buf + tn_len;
518 if (!pathspec_matches(paths, down))
520 else if (S_ISREG(entry.mode))
521 hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
522 else if (S_ISDIR(entry.mode)) {
523 enum object_type type;
524 struct tree_desc sub;
525 void *data;
526 unsigned long size;
528 data = read_sha1_file(entry.sha1, &type, &size);
529 if (!data)
530 die("unable to read tree (%s)",
531 sha1_to_hex(entry.sha1));
532 init_tree_desc(&sub, data, size);
533 hit |= grep_tree(opt, paths, &sub, tree_name, down);
534 free(data);
537 strbuf_release(&pathbuf);
538 return hit;
541 static int grep_object(struct grep_opt *opt, const char **paths,
542 struct object *obj, const char *name)
544 if (obj->type == OBJ_BLOB)
545 return grep_sha1(opt, obj->sha1, name, 0);
546 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
547 struct tree_desc tree;
548 void *data;
549 unsigned long size;
550 int hit;
551 data = read_object_with_reference(obj->sha1, tree_type,
552 &size, NULL);
553 if (!data)
554 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
555 init_tree_desc(&tree, data, size);
556 hit = grep_tree(opt, paths, &tree, name, "");
557 free(data);
558 return hit;
560 die("unable to grep from object of type %s", typename(obj->type));
563 static const char builtin_grep_usage[] =
564 "git grep <option>* [-e] <pattern> <rev>* [[--] <path>...]";
566 static const char emsg_invalid_context_len[] =
567 "%s: invalid context length argument";
568 static const char emsg_missing_context_len[] =
569 "missing context length argument";
570 static const char emsg_missing_argument[] =
571 "option requires an argument -%s";
573 int cmd_grep(int argc, const char **argv, const char *prefix)
575 int hit = 0;
576 int cached = 0;
577 int seen_dashdash = 0;
578 struct grep_opt opt;
579 struct object_array list = { 0, 0, NULL };
580 const char **paths = NULL;
581 int i;
583 memset(&opt, 0, sizeof(opt));
584 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
585 opt.relative = 1;
586 opt.pathname = 1;
587 opt.pattern_tail = &opt.pattern_list;
588 opt.regflags = REG_NEWLINE;
590 strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD);
591 opt.color = -1;
592 git_config(grep_config, &opt);
593 if (opt.color == -1)
594 opt.color = git_use_color_default;
597 * If there is no -- then the paths must exist in the working
598 * tree. If there is no explicit pattern specified with -e or
599 * -f, we take the first unrecognized non option to be the
600 * pattern, but then what follows it must be zero or more
601 * valid refs up to the -- (if exists), and then existing
602 * paths. If there is an explicit pattern, then the first
603 * unrecognized non option is the beginning of the refs list
604 * that continues up to the -- (if exists), and then paths.
607 while (1 < argc) {
608 const char *arg = argv[1];
609 argc--; argv++;
610 if (!strcmp("--cached", arg)) {
611 cached = 1;
612 continue;
614 if (!strcmp("--no-ext-grep", arg)) {
615 builtin_grep = 1;
616 continue;
618 if (!strcmp("-a", arg) ||
619 !strcmp("--text", arg)) {
620 opt.binary = GREP_BINARY_TEXT;
621 continue;
623 if (!strcmp("-i", arg) ||
624 !strcmp("--ignore-case", arg)) {
625 opt.regflags |= REG_ICASE;
626 continue;
628 if (!strcmp("-I", arg)) {
629 opt.binary = GREP_BINARY_NOMATCH;
630 continue;
632 if (!strcmp("-v", arg) ||
633 !strcmp("--invert-match", arg)) {
634 opt.invert = 1;
635 continue;
637 if (!strcmp("-E", arg) ||
638 !strcmp("--extended-regexp", arg)) {
639 opt.regflags |= REG_EXTENDED;
640 continue;
642 if (!strcmp("-F", arg) ||
643 !strcmp("--fixed-strings", arg)) {
644 opt.fixed = 1;
645 continue;
647 if (!strcmp("-G", arg) ||
648 !strcmp("--basic-regexp", arg)) {
649 opt.regflags &= ~REG_EXTENDED;
650 continue;
652 if (!strcmp("-n", arg)) {
653 opt.linenum = 1;
654 continue;
656 if (!strcmp("-h", arg)) {
657 opt.pathname = 0;
658 continue;
660 if (!strcmp("-H", arg)) {
661 opt.pathname = 1;
662 continue;
664 if (!strcmp("-l", arg) ||
665 !strcmp("--name-only", arg) ||
666 !strcmp("--files-with-matches", arg)) {
667 opt.name_only = 1;
668 continue;
670 if (!strcmp("-L", arg) ||
671 !strcmp("--files-without-match", arg)) {
672 opt.unmatch_name_only = 1;
673 continue;
675 if (!strcmp("-z", arg) ||
676 !strcmp("--null", arg)) {
677 opt.null_following_name = 1;
678 continue;
680 if (!strcmp("-c", arg) ||
681 !strcmp("--count", arg)) {
682 opt.count = 1;
683 continue;
685 if (!strcmp("-w", arg) ||
686 !strcmp("--word-regexp", arg)) {
687 opt.word_regexp = 1;
688 continue;
690 if (!prefixcmp(arg, "-A") ||
691 !prefixcmp(arg, "-B") ||
692 !prefixcmp(arg, "-C") ||
693 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
694 unsigned num;
695 const char *scan;
696 switch (arg[1]) {
697 case 'A': case 'B': case 'C':
698 if (!arg[2]) {
699 if (argc <= 1)
700 die(emsg_missing_context_len);
701 scan = *++argv;
702 argc--;
704 else
705 scan = arg + 2;
706 break;
707 default:
708 scan = arg + 1;
709 break;
711 if (strtoul_ui(scan, 10, &num))
712 die(emsg_invalid_context_len, scan);
713 switch (arg[1]) {
714 case 'A':
715 opt.post_context = num;
716 break;
717 default:
718 case 'C':
719 opt.post_context = num;
720 case 'B':
721 opt.pre_context = num;
722 break;
724 continue;
726 if (!strcmp("-f", arg)) {
727 FILE *patterns;
728 int lno = 0;
729 char buf[1024];
730 if (argc <= 1)
731 die(emsg_missing_argument, arg);
732 patterns = fopen(argv[1], "r");
733 if (!patterns)
734 die("'%s': %s", argv[1], strerror(errno));
735 while (fgets(buf, sizeof(buf), patterns)) {
736 int len = strlen(buf);
737 if (len && buf[len-1] == '\n')
738 buf[len-1] = 0;
739 /* ignore empty line like grep does */
740 if (!buf[0])
741 continue;
742 append_grep_pattern(&opt, xstrdup(buf),
743 argv[1], ++lno,
744 GREP_PATTERN);
746 fclose(patterns);
747 argv++;
748 argc--;
749 continue;
751 if (!strcmp("--not", arg)) {
752 append_grep_pattern(&opt, arg, "command line", 0,
753 GREP_NOT);
754 continue;
756 if (!strcmp("--and", arg)) {
757 append_grep_pattern(&opt, arg, "command line", 0,
758 GREP_AND);
759 continue;
761 if (!strcmp("--or", arg))
762 continue; /* no-op */
763 if (!strcmp("(", arg)) {
764 append_grep_pattern(&opt, arg, "command line", 0,
765 GREP_OPEN_PAREN);
766 continue;
768 if (!strcmp(")", arg)) {
769 append_grep_pattern(&opt, arg, "command line", 0,
770 GREP_CLOSE_PAREN);
771 continue;
773 if (!strcmp("--all-match", arg)) {
774 opt.all_match = 1;
775 continue;
777 if (!strcmp("-e", arg)) {
778 if (1 < argc) {
779 append_grep_pattern(&opt, argv[1],
780 "-e option", 0,
781 GREP_PATTERN);
782 argv++;
783 argc--;
784 continue;
786 die(emsg_missing_argument, arg);
788 if (!strcmp("--full-name", arg)) {
789 opt.relative = 0;
790 continue;
792 if (!strcmp("--color", arg)) {
793 opt.color = 1;
794 continue;
796 if (!strcmp("--no-color", arg)) {
797 opt.color = 0;
798 continue;
800 if (!strcmp("--", arg)) {
801 /* later processing wants to have this at argv[1] */
802 argv--;
803 argc++;
804 break;
806 if (*arg == '-')
807 usage(builtin_grep_usage);
809 /* First unrecognized non-option token */
810 if (!opt.pattern_list) {
811 append_grep_pattern(&opt, arg, "command line", 0,
812 GREP_PATTERN);
813 break;
815 else {
816 /* We are looking at the first path or rev;
817 * it is found at argv[1] after leaving the
818 * loop.
820 argc++; argv--;
821 break;
825 if (opt.color && !opt.color_external)
826 builtin_grep = 1;
827 if (!opt.pattern_list)
828 die("no pattern given.");
829 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
830 die("cannot mix --fixed-strings and regexp");
831 compile_grep_patterns(&opt);
833 /* Check revs and then paths */
834 for (i = 1; i < argc; i++) {
835 const char *arg = argv[i];
836 unsigned char sha1[20];
837 /* Is it a rev? */
838 if (!get_sha1(arg, sha1)) {
839 struct object *object = parse_object(sha1);
840 if (!object)
841 die("bad object %s", arg);
842 add_object_array(object, arg, &list);
843 continue;
845 if (!strcmp(arg, "--")) {
846 i++;
847 seen_dashdash = 1;
849 break;
852 /* The rest are paths */
853 if (!seen_dashdash) {
854 int j;
855 for (j = i; j < argc; j++)
856 verify_filename(prefix, argv[j]);
859 if (i < argc) {
860 paths = get_pathspec(prefix, argv + i);
861 if (opt.prefix_length && opt.relative) {
862 /* Make sure we do not get outside of paths */
863 for (i = 0; paths[i]; i++)
864 if (strncmp(prefix, paths[i], opt.prefix_length))
865 die("git grep: cannot generate relative filenames containing '..'");
868 else if (prefix) {
869 paths = xcalloc(2, sizeof(const char *));
870 paths[0] = prefix;
871 paths[1] = NULL;
874 if (!list.nr) {
875 if (!cached)
876 setup_work_tree();
877 return !grep_cache(&opt, paths, cached);
880 if (cached)
881 die("both --cached and trees are given.");
883 for (i = 0; i < list.nr; i++) {
884 struct object *real_obj;
885 real_obj = deref_tag(list.objects[i].item, NULL, 0);
886 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
887 hit = 1;
889 free_grep_patterns(&opt);
890 return !hit;