Merge branch 'maint-1.5.6' into maint-1.6.0
[git.git] / builtin-grep.c
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"
11 #include "tree-walk.h"
12 #include "builtin.h"
13 #include "grep.h"
14
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
22
23 /*
24  * git grep pathspecs are somewhat different from diff-tree pathspecs;
25  * pathname wildcards are allowed.
26  */
27 static int pathspec_matches(const char **paths, const char *name)
28 {
29         int namelen, i;
30         if (!paths || !*paths)
31                 return 1;
32         namelen = strlen(name);
33         for (i = 0; paths[i]; i++) {
34                 const char *match = paths[i];
35                 int matchlen = strlen(match);
36                 const char *cp, *meta;
37
38                 if (!matchlen ||
39                     ((matchlen <= namelen) &&
40                      !strncmp(name, match, matchlen) &&
41                      (match[matchlen-1] == '/' ||
42                       name[matchlen] == '\0' || name[matchlen] == '/')))
43                         return 1;
44                 if (!fnmatch(match, name, 0))
45                         return 1;
46                 if (name[namelen-1] != '/')
47                         continue;
48
49                 /* We are being asked if the directory ("name") is worth
50                  * descending into.
51                  *
52                  * Find the longest leading directory name that does
53                  * not have metacharacter in the pathspec; the name
54                  * we are looking at must overlap with that directory.
55                  */
56                 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
57                         char ch = *cp;
58                         if (ch == '*' || ch == '[' || ch == '?') {
59                                 meta = cp;
60                                 break;
61                         }
62                 }
63                 if (!meta)
64                         meta = cp; /* fully literal */
65
66                 if (namelen <= meta - match) {
67                         /* Looking at "Documentation/" and
68                          * the pattern says "Documentation/howto/", or
69                          * "Documentation/diff*.txt".  The name we
70                          * have should match prefix.
71                          */
72                         if (!memcmp(match, name, namelen))
73                                 return 1;
74                         continue;
75                 }
76
77                 if (meta - match < namelen) {
78                         /* Looking at "Documentation/howto/" and
79                          * the pattern says "Documentation/h*";
80                          * match up to "Do.../h"; this avoids descending
81                          * into "Documentation/technical/".
82                          */
83                         if (!memcmp(match, name, meta - match))
84                                 return 1;
85                         continue;
86                 }
87         }
88         return 0;
89 }
90
91 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
92 {
93         unsigned long size;
94         char *data;
95         enum object_type type;
96         char *to_free = NULL;
97         int hit;
98
99         data = read_sha1_file(sha1, &type, &size);
100         if (!data) {
101                 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
102                 return 0;
103         }
104         if (opt->relative && opt->prefix_length) {
105                 static char name_buf[PATH_MAX];
106                 char *cp;
107                 int name_len = strlen(name) - opt->prefix_length + 1;
108
109                 if (!tree_name_len)
110                         name += opt->prefix_length;
111                 else {
112                         if (ARRAY_SIZE(name_buf) <= name_len)
113                                 cp = to_free = xmalloc(name_len);
114                         else
115                                 cp = name_buf;
116                         memcpy(cp, name, tree_name_len);
117                         strcpy(cp + tree_name_len,
118                                name + tree_name_len + opt->prefix_length);
119                         name = cp;
120                 }
121         }
122         hit = grep_buffer(opt, name, data, size);
123         free(data);
124         free(to_free);
125         return hit;
126 }
127
128 static int grep_file(struct grep_opt *opt, const char *filename)
129 {
130         struct stat st;
131         int i;
132         char *data;
133         size_t sz;
134
135         if (lstat(filename, &st) < 0) {
136         err_ret:
137                 if (errno != ENOENT)
138                         error("'%s': %s", filename, strerror(errno));
139                 return 0;
140         }
141         if (!st.st_size)
142                 return 0; /* empty file -- no grep hit */
143         if (!S_ISREG(st.st_mode))
144                 return 0;
145         sz = xsize_t(st.st_size);
146         i = open(filename, O_RDONLY);
147         if (i < 0)
148                 goto err_ret;
149         data = xmalloc(sz + 1);
150         if (st.st_size != read_in_full(i, data, sz)) {
151                 error("'%s': short read %s", filename, strerror(errno));
152                 close(i);
153                 free(data);
154                 return 0;
155         }
156         close(i);
157         if (opt->relative && opt->prefix_length)
158                 filename += opt->prefix_length;
159         i = grep_buffer(opt, filename, data, sz);
160         free(data);
161         return i;
162 }
163
164 #if !NO_EXTERNAL_GREP
165 static int exec_grep(int argc, const char **argv)
166 {
167         pid_t pid;
168         int status;
169
170         argv[argc] = NULL;
171         pid = fork();
172         if (pid < 0)
173                 return pid;
174         if (!pid) {
175                 execvp("grep", (char **) argv);
176                 exit(255);
177         }
178         while (waitpid(pid, &status, 0) < 0) {
179                 if (errno == EINTR)
180                         continue;
181                 return -1;
182         }
183         if (WIFEXITED(status)) {
184                 if (!WEXITSTATUS(status))
185                         return 1;
186                 return 0;
187         }
188         return -1;
189 }
190
191 #define MAXARGS 1000
192 #define ARGBUF 4096
193 #define push_arg(a) do { \
194         if (nr < MAXARGS) argv[nr++] = (a); \
195         else die("maximum number of args exceeded"); \
196         } while (0)
197
198 /*
199  * If you send a singleton filename to grep, it does not give
200  * the name of the file.  GNU grep has "-H" but we would want
201  * that behaviour in a portable way.
202  *
203  * So we keep two pathnames in argv buffer unsent to grep in
204  * the main loop if we need to do more than one grep.
205  */
206 static int flush_grep(struct grep_opt *opt,
207                       int argc, int arg0, const char **argv, int *kept)
208 {
209         int status;
210         int count = argc - arg0;
211         const char *kept_0 = NULL;
212
213         if (count <= 2) {
214                 /*
215                  * Because we keep at least 2 paths in the call from
216                  * the main loop (i.e. kept != NULL), and MAXARGS is
217                  * far greater than 2, this usually is a call to
218                  * conclude the grep.  However, the user could attempt
219                  * to overflow the argv buffer by giving too many
220                  * options to leave very small number of real
221                  * arguments even for the call in the main loop.
222                  */
223                 if (kept)
224                         die("insanely many options to grep");
225
226                 /*
227                  * If we have two or more paths, we do not have to do
228                  * anything special, but we need to push /dev/null to
229                  * get "-H" behaviour of GNU grep portably but when we
230                  * are not doing "-l" nor "-L" nor "-c".
231                  */
232                 if (count == 1 &&
233                     !opt->name_only &&
234                     !opt->unmatch_name_only &&
235                     !opt->count) {
236                         argv[argc++] = "/dev/null";
237                         argv[argc] = NULL;
238                 }
239         }
240
241         else if (kept) {
242                 /*
243                  * Called because we found many paths and haven't finished
244                  * iterating over the cache yet.  We keep two paths
245                  * for the concluding call.  argv[argc-2] and argv[argc-1]
246                  * has the last two paths, so save the first one away,
247                  * replace it with NULL while sending the list to grep,
248                  * and recover them after we are done.
249                  */
250                 *kept = 2;
251                 kept_0 = argv[argc-2];
252                 argv[argc-2] = NULL;
253                 argc -= 2;
254         }
255
256         status = exec_grep(argc, argv);
257
258         if (kept_0) {
259                 /*
260                  * Then recover them.  Now the last arg is beyond the
261                  * terminating NULL which is at argc, and the second
262                  * from the last is what we saved away in kept_0
263                  */
264                 argv[arg0++] = kept_0;
265                 argv[arg0] = argv[argc+1];
266         }
267         return status;
268 }
269
270 static int external_grep(struct grep_opt *opt, const char **paths, int cached)
271 {
272         int i, nr, argc, hit, len, status;
273         const char *argv[MAXARGS+1];
274         char randarg[ARGBUF];
275         char *argptr = randarg;
276         struct grep_pat *p;
277
278         if (opt->extended || (opt->relative && opt->prefix_length))
279                 return -1;
280         len = nr = 0;
281         push_arg("grep");
282         if (opt->fixed)
283                 push_arg("-F");
284         if (opt->linenum)
285                 push_arg("-n");
286         if (!opt->pathname)
287                 push_arg("-h");
288         if (opt->regflags & REG_EXTENDED)
289                 push_arg("-E");
290         if (opt->regflags & REG_ICASE)
291                 push_arg("-i");
292         if (opt->binary == GREP_BINARY_NOMATCH)
293                 push_arg("-I");
294         if (opt->word_regexp)
295                 push_arg("-w");
296         if (opt->name_only)
297                 push_arg("-l");
298         if (opt->unmatch_name_only)
299                 push_arg("-L");
300         if (opt->count)
301                 push_arg("-c");
302         if (opt->post_context || opt->pre_context) {
303                 if (opt->post_context != opt->pre_context) {
304                         if (opt->pre_context) {
305                                 push_arg("-B");
306                                 len += snprintf(argptr, sizeof(randarg)-len,
307                                                 "%u", opt->pre_context) + 1;
308                                 if (sizeof(randarg) <= len)
309                                         die("maximum length of args exceeded");
310                                 push_arg(argptr);
311                                 argptr += len;
312                         }
313                         if (opt->post_context) {
314                                 push_arg("-A");
315                                 len += snprintf(argptr, sizeof(randarg)-len,
316                                                 "%u", opt->post_context) + 1;
317                                 if (sizeof(randarg) <= len)
318                                         die("maximum length of args exceeded");
319                                 push_arg(argptr);
320                                 argptr += len;
321                         }
322                 }
323                 else {
324                         push_arg("-C");
325                         len += snprintf(argptr, sizeof(randarg)-len,
326                                         "%u", opt->post_context) + 1;
327                         if (sizeof(randarg) <= len)
328                                 die("maximum length of args exceeded");
329                         push_arg(argptr);
330                         argptr += len;
331                 }
332         }
333         for (p = opt->pattern_list; p; p = p->next) {
334                 push_arg("-e");
335                 push_arg(p->pattern);
336         }
337
338         hit = 0;
339         argc = nr;
340         for (i = 0; i < active_nr; i++) {
341                 struct cache_entry *ce = active_cache[i];
342                 char *name;
343                 int kept;
344                 if (!S_ISREG(ce->ce_mode))
345                         continue;
346                 if (!pathspec_matches(paths, ce->name))
347                         continue;
348                 name = ce->name;
349                 if (name[0] == '-') {
350                         int len = ce_namelen(ce);
351                         name = xmalloc(len + 3);
352                         memcpy(name, "./", 2);
353                         memcpy(name + 2, ce->name, len + 1);
354                 }
355                 argv[argc++] = name;
356                 if (MAXARGS <= argc) {
357                         status = flush_grep(opt, argc, nr, argv, &kept);
358                         if (0 < status)
359                                 hit = 1;
360                         argc = nr + kept;
361                 }
362                 if (ce_stage(ce)) {
363                         do {
364                                 i++;
365                         } while (i < active_nr &&
366                                  !strcmp(ce->name, active_cache[i]->name));
367                         i--; /* compensate for loop control */
368                 }
369         }
370         if (argc > nr) {
371                 status = flush_grep(opt, argc, nr, argv, NULL);
372                 if (0 < status)
373                         hit = 1;
374         }
375         return hit;
376 }
377 #endif
378
379 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
380 {
381         int hit = 0;
382         int nr;
383         read_cache();
384
385 #if !NO_EXTERNAL_GREP
386         /*
387          * Use the external "grep" command for the case where
388          * we grep through the checked-out files. It tends to
389          * be a lot more optimized
390          */
391         if (!cached) {
392                 hit = external_grep(opt, paths, cached);
393                 if (hit >= 0)
394                         return hit;
395         }
396 #endif
397
398         for (nr = 0; nr < active_nr; nr++) {
399                 struct cache_entry *ce = active_cache[nr];
400                 if (!S_ISREG(ce->ce_mode))
401                         continue;
402                 if (!pathspec_matches(paths, ce->name))
403                         continue;
404                 if (cached) {
405                         if (ce_stage(ce))
406                                 continue;
407                         hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
408                 }
409                 else
410                         hit |= grep_file(opt, ce->name);
411                 if (ce_stage(ce)) {
412                         do {
413                                 nr++;
414                         } while (nr < active_nr &&
415                                  !strcmp(ce->name, active_cache[nr]->name));
416                         nr--; /* compensate for loop control */
417                 }
418         }
419         free_grep_patterns(opt);
420         return hit;
421 }
422
423 static int grep_tree(struct grep_opt *opt, const char **paths,
424                      struct tree_desc *tree,
425                      const char *tree_name, const char *base)
426 {
427         int len;
428         int hit = 0;
429         struct name_entry entry;
430         char *down;
431         int tn_len = strlen(tree_name);
432         struct strbuf pathbuf;
433
434         strbuf_init(&pathbuf, PATH_MAX + tn_len);
435
436         if (tn_len) {
437                 strbuf_add(&pathbuf, tree_name, tn_len);
438                 strbuf_addch(&pathbuf, ':');
439                 tn_len = pathbuf.len;
440         }
441         strbuf_addstr(&pathbuf, base);
442         len = pathbuf.len;
443
444         while (tree_entry(tree, &entry)) {
445                 int te_len = tree_entry_len(entry.path, entry.sha1);
446                 pathbuf.len = len;
447                 strbuf_add(&pathbuf, entry.path, te_len);
448
449                 if (S_ISDIR(entry.mode))
450                         /* Match "abc/" against pathspec to
451                          * decide if we want to descend into "abc"
452                          * directory.
453                          */
454                         strbuf_addch(&pathbuf, '/');
455
456                 down = pathbuf.buf + tn_len;
457                 if (!pathspec_matches(paths, down))
458                         ;
459                 else if (S_ISREG(entry.mode))
460                         hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
461                 else if (S_ISDIR(entry.mode)) {
462                         enum object_type type;
463                         struct tree_desc sub;
464                         void *data;
465                         unsigned long size;
466
467                         data = read_sha1_file(entry.sha1, &type, &size);
468                         if (!data)
469                                 die("unable to read tree (%s)",
470                                     sha1_to_hex(entry.sha1));
471                         init_tree_desc(&sub, data, size);
472                         hit |= grep_tree(opt, paths, &sub, tree_name, down);
473                         free(data);
474                 }
475         }
476         strbuf_release(&pathbuf);
477         return hit;
478 }
479
480 static int grep_object(struct grep_opt *opt, const char **paths,
481                        struct object *obj, const char *name)
482 {
483         if (obj->type == OBJ_BLOB)
484                 return grep_sha1(opt, obj->sha1, name, 0);
485         if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
486                 struct tree_desc tree;
487                 void *data;
488                 unsigned long size;
489                 int hit;
490                 data = read_object_with_reference(obj->sha1, tree_type,
491                                                   &size, NULL);
492                 if (!data)
493                         die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
494                 init_tree_desc(&tree, data, size);
495                 hit = grep_tree(opt, paths, &tree, name, "");
496                 free(data);
497                 return hit;
498         }
499         die("unable to grep from object of type %s", typename(obj->type));
500 }
501
502 static const char builtin_grep_usage[] =
503 "git grep <option>* [-e] <pattern> <rev>* [[--] <path>...]";
504
505 static const char emsg_invalid_context_len[] =
506 "%s: invalid context length argument";
507 static const char emsg_missing_context_len[] =
508 "missing context length argument";
509 static const char emsg_missing_argument[] =
510 "option requires an argument -%s";
511
512 int cmd_grep(int argc, const char **argv, const char *prefix)
513 {
514         int hit = 0;
515         int cached = 0;
516         int seen_dashdash = 0;
517         struct grep_opt opt;
518         struct object_array list = { 0, 0, NULL };
519         const char **paths = NULL;
520         int i;
521
522         memset(&opt, 0, sizeof(opt));
523         opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
524         opt.relative = 1;
525         opt.pathname = 1;
526         opt.pattern_tail = &opt.pattern_list;
527         opt.regflags = REG_NEWLINE;
528
529         /*
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          * unrecognized non option is the beginning of the refs list
537          * that continues up to the -- (if exists), and then paths.
538          */
539
540         while (1 < argc) {
541                 const char *arg = argv[1];
542                 argc--; argv++;
543                 if (!strcmp("--cached", arg)) {
544                         cached = 1;
545                         continue;
546                 }
547                 if (!strcmp("-a", arg) ||
548                     !strcmp("--text", arg)) {
549                         opt.binary = GREP_BINARY_TEXT;
550                         continue;
551                 }
552                 if (!strcmp("-i", arg) ||
553                     !strcmp("--ignore-case", arg)) {
554                         opt.regflags |= REG_ICASE;
555                         continue;
556                 }
557                 if (!strcmp("-I", arg)) {
558                         opt.binary = GREP_BINARY_NOMATCH;
559                         continue;
560                 }
561                 if (!strcmp("-v", arg) ||
562                     !strcmp("--invert-match", arg)) {
563                         opt.invert = 1;
564                         continue;
565                 }
566                 if (!strcmp("-E", arg) ||
567                     !strcmp("--extended-regexp", arg)) {
568                         opt.regflags |= REG_EXTENDED;
569                         continue;
570                 }
571                 if (!strcmp("-F", arg) ||
572                     !strcmp("--fixed-strings", arg)) {
573                         opt.fixed = 1;
574                         continue;
575                 }
576                 if (!strcmp("-G", arg) ||
577                     !strcmp("--basic-regexp", arg)) {
578                         opt.regflags &= ~REG_EXTENDED;
579                         continue;
580                 }
581                 if (!strcmp("-n", arg)) {
582                         opt.linenum = 1;
583                         continue;
584                 }
585                 if (!strcmp("-h", arg)) {
586                         opt.pathname = 0;
587                         continue;
588                 }
589                 if (!strcmp("-H", arg)) {
590                         opt.pathname = 1;
591                         continue;
592                 }
593                 if (!strcmp("-l", arg) ||
594                     !strcmp("--name-only", arg) ||
595                     !strcmp("--files-with-matches", arg)) {
596                         opt.name_only = 1;
597                         continue;
598                 }
599                 if (!strcmp("-L", arg) ||
600                     !strcmp("--files-without-match", arg)) {
601                         opt.unmatch_name_only = 1;
602                         continue;
603                 }
604                 if (!strcmp("-c", arg) ||
605                     !strcmp("--count", arg)) {
606                         opt.count = 1;
607                         continue;
608                 }
609                 if (!strcmp("-w", arg) ||
610                     !strcmp("--word-regexp", arg)) {
611                         opt.word_regexp = 1;
612                         continue;
613                 }
614                 if (!prefixcmp(arg, "-A") ||
615                     !prefixcmp(arg, "-B") ||
616                     !prefixcmp(arg, "-C") ||
617                     (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
618                         unsigned num;
619                         const char *scan;
620                         switch (arg[1]) {
621                         case 'A': case 'B': case 'C':
622                                 if (!arg[2]) {
623                                         if (argc <= 1)
624                                                 die(emsg_missing_context_len);
625                                         scan = *++argv;
626                                         argc--;
627                                 }
628                                 else
629                                         scan = arg + 2;
630                                 break;
631                         default:
632                                 scan = arg + 1;
633                                 break;
634                         }
635                         if (strtoul_ui(scan, 10, &num))
636                                 die(emsg_invalid_context_len, scan);
637                         switch (arg[1]) {
638                         case 'A':
639                                 opt.post_context = num;
640                                 break;
641                         default:
642                         case 'C':
643                                 opt.post_context = num;
644                         case 'B':
645                                 opt.pre_context = num;
646                                 break;
647                         }
648                         continue;
649                 }
650                 if (!strcmp("-f", arg)) {
651                         FILE *patterns;
652                         int lno = 0;
653                         char buf[1024];
654                         if (argc <= 1)
655                                 die(emsg_missing_argument, arg);
656                         patterns = fopen(argv[1], "r");
657                         if (!patterns)
658                                 die("'%s': %s", argv[1], strerror(errno));
659                         while (fgets(buf, sizeof(buf), patterns)) {
660                                 int len = strlen(buf);
661                                 if (len && buf[len-1] == '\n')
662                                         buf[len-1] = 0;
663                                 /* ignore empty line like grep does */
664                                 if (!buf[0])
665                                         continue;
666                                 append_grep_pattern(&opt, xstrdup(buf),
667                                                     argv[1], ++lno,
668                                                     GREP_PATTERN);
669                         }
670                         fclose(patterns);
671                         argv++;
672                         argc--;
673                         continue;
674                 }
675                 if (!strcmp("--not", arg)) {
676                         append_grep_pattern(&opt, arg, "command line", 0,
677                                             GREP_NOT);
678                         continue;
679                 }
680                 if (!strcmp("--and", arg)) {
681                         append_grep_pattern(&opt, arg, "command line", 0,
682                                             GREP_AND);
683                         continue;
684                 }
685                 if (!strcmp("--or", arg))
686                         continue; /* no-op */
687                 if (!strcmp("(", arg)) {
688                         append_grep_pattern(&opt, arg, "command line", 0,
689                                             GREP_OPEN_PAREN);
690                         continue;
691                 }
692                 if (!strcmp(")", arg)) {
693                         append_grep_pattern(&opt, arg, "command line", 0,
694                                             GREP_CLOSE_PAREN);
695                         continue;
696                 }
697                 if (!strcmp("--all-match", arg)) {
698                         opt.all_match = 1;
699                         continue;
700                 }
701                 if (!strcmp("-e", arg)) {
702                         if (1 < argc) {
703                                 append_grep_pattern(&opt, argv[1],
704                                                     "-e option", 0,
705                                                     GREP_PATTERN);
706                                 argv++;
707                                 argc--;
708                                 continue;
709                         }
710                         die(emsg_missing_argument, arg);
711                 }
712                 if (!strcmp("--full-name", arg)) {
713                         opt.relative = 0;
714                         continue;
715                 }
716                 if (!strcmp("--", arg)) {
717                         /* later processing wants to have this at argv[1] */
718                         argv--;
719                         argc++;
720                         break;
721                 }
722                 if (*arg == '-')
723                         usage(builtin_grep_usage);
724
725                 /* First unrecognized non-option token */
726                 if (!opt.pattern_list) {
727                         append_grep_pattern(&opt, arg, "command line", 0,
728                                             GREP_PATTERN);
729                         break;
730                 }
731                 else {
732                         /* We are looking at the first path or rev;
733                          * it is found at argv[1] after leaving the
734                          * loop.
735                          */
736                         argc++; argv--;
737                         break;
738                 }
739         }
740
741         if (!opt.pattern_list)
742                 die("no pattern given.");
743         if ((opt.regflags != REG_NEWLINE) && opt.fixed)
744                 die("cannot mix --fixed-strings and regexp");
745         compile_grep_patterns(&opt);
746
747         /* Check revs and then paths */
748         for (i = 1; i < argc; i++) {
749                 const char *arg = argv[i];
750                 unsigned char sha1[20];
751                 /* Is it a rev? */
752                 if (!get_sha1(arg, sha1)) {
753                         struct object *object = parse_object(sha1);
754                         if (!object)
755                                 die("bad object %s", arg);
756                         add_object_array(object, arg, &list);
757                         continue;
758                 }
759                 if (!strcmp(arg, "--")) {
760                         i++;
761                         seen_dashdash = 1;
762                 }
763                 break;
764         }
765
766         /* The rest are paths */
767         if (!seen_dashdash) {
768                 int j;
769                 for (j = i; j < argc; j++)
770                         verify_filename(prefix, argv[j]);
771         }
772
773         if (i < argc) {
774                 paths = get_pathspec(prefix, argv + i);
775                 if (opt.prefix_length && opt.relative) {
776                         /* Make sure we do not get outside of paths */
777                         for (i = 0; paths[i]; i++)
778                                 if (strncmp(prefix, paths[i], opt.prefix_length))
779                                         die("git grep: cannot generate relative filenames containing '..'");
780                 }
781         }
782         else if (prefix) {
783                 paths = xcalloc(2, sizeof(const char *));
784                 paths[0] = prefix;
785                 paths[1] = NULL;
786         }
787
788         if (!list.nr) {
789                 if (!cached)
790                         setup_work_tree();
791                 return !grep_cache(&opt, paths, cached);
792         }
793
794         if (cached)
795                 die("both --cached and trees are given.");
796
797         for (i = 0; i < list.nr; i++) {
798                 struct object *real_obj;
799                 real_obj = deref_tag(list.objects[i].item, NULL, 0);
800                 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
801                         hit = 1;
802         }
803         free_grep_patterns(&opt);
804         return !hit;
805 }