Code

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