4 * Copyright (c) 2006 Junio C Hamano
11 #include "tree-walk.h"
13 #include "parse-options.h"
18 #ifndef NO_EXTERNAL_GREP
20 #define NO_EXTERNAL_GREP 0
22 #define NO_EXTERNAL_GREP 1
26 static char const * const grep_usage
[] = {
27 "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
31 static int grep_config(const char *var
, const char *value
, void *cb
)
33 struct grep_opt
*opt
= cb
;
35 switch (userdiff_config(var
, value
)) {
41 if (!strcmp(var
, "color.grep")) {
42 opt
->color
= git_config_colorbool(var
, value
, -1);
45 if (!strcmp(var
, "color.grep.external"))
46 return git_config_string(&(opt
->color_external
), var
, value
);
47 if (!strcmp(var
, "color.grep.match")) {
49 return config_error_nonbool(var
);
50 color_parse(value
, var
, opt
->color_match
);
53 return git_color_default_config(var
, value
, cb
);
57 * Return non-zero if max_depth is negative or path has no more then max_depth
60 static int accept_subdir(const char *path
, int max_depth
)
65 while ((path
= strchr(path
, '/')) != NULL
) {
75 * Return non-zero if name is a subdirectory of match and is not too deep.
77 static int is_subdir(const char *name
, int namelen
,
78 const char *match
, int matchlen
, int max_depth
)
80 if (matchlen
> namelen
|| strncmp(name
, match
, matchlen
))
83 if (name
[matchlen
] == '\0') /* exact match */
86 if (!matchlen
|| match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
87 return accept_subdir(name
+ matchlen
+ 1, max_depth
);
93 * git grep pathspecs are somewhat different from diff-tree pathspecs;
94 * pathname wildcards are allowed.
96 static int pathspec_matches(const char **paths
, const char *name
, int max_depth
)
99 if (!paths
|| !*paths
)
100 return accept_subdir(name
, max_depth
);
101 namelen
= strlen(name
);
102 for (i
= 0; paths
[i
]; i
++) {
103 const char *match
= paths
[i
];
104 int matchlen
= strlen(match
);
105 const char *cp
, *meta
;
107 if (is_subdir(name
, namelen
, match
, matchlen
, max_depth
))
109 if (!fnmatch(match
, name
, 0))
111 if (name
[namelen
-1] != '/')
114 /* We are being asked if the directory ("name") is worth
117 * Find the longest leading directory name that does
118 * not have metacharacter in the pathspec; the name
119 * we are looking at must overlap with that directory.
121 for (cp
= match
, meta
= NULL
; cp
- match
< matchlen
; cp
++) {
123 if (ch
== '*' || ch
== '[' || ch
== '?') {
129 meta
= cp
; /* fully literal */
131 if (namelen
<= meta
- match
) {
132 /* Looking at "Documentation/" and
133 * the pattern says "Documentation/howto/", or
134 * "Documentation/diff*.txt". The name we
135 * have should match prefix.
137 if (!memcmp(match
, name
, namelen
))
142 if (meta
- match
< namelen
) {
143 /* Looking at "Documentation/howto/" and
144 * the pattern says "Documentation/h*";
145 * match up to "Do.../h"; this avoids descending
146 * into "Documentation/technical/".
148 if (!memcmp(match
, name
, meta
- match
))
156 static int grep_sha1(struct grep_opt
*opt
, const unsigned char *sha1
, const char *name
, int tree_name_len
)
160 enum object_type type
;
162 struct strbuf pathbuf
= STRBUF_INIT
;
164 data
= read_sha1_file(sha1
, &type
, &size
);
166 error("'%s': unable to read %s", name
, sha1_to_hex(sha1
));
169 if (opt
->relative
&& opt
->prefix_length
) {
170 quote_path_relative(name
+ tree_name_len
, -1, &pathbuf
, opt
->prefix
);
171 strbuf_insert(&pathbuf
, 0, name
, tree_name_len
);
174 hit
= grep_buffer(opt
, name
, data
, size
);
175 strbuf_release(&pathbuf
);
180 static int grep_file(struct grep_opt
*opt
, const char *filename
)
186 struct strbuf buf
= STRBUF_INIT
;
188 if (lstat(filename
, &st
) < 0) {
191 error("'%s': %s", filename
, strerror(errno
));
195 return 0; /* empty file -- no grep hit */
196 if (!S_ISREG(st
.st_mode
))
198 sz
= xsize_t(st
.st_size
);
199 i
= open(filename
, O_RDONLY
);
202 data
= xmalloc(sz
+ 1);
203 if (st
.st_size
!= read_in_full(i
, data
, sz
)) {
204 error("'%s': short read %s", filename
, strerror(errno
));
210 if (opt
->relative
&& opt
->prefix_length
)
211 filename
= quote_path_relative(filename
, -1, &buf
, opt
->prefix
);
212 i
= grep_buffer(opt
, filename
, data
, sz
);
213 strbuf_release(&buf
);
218 #if !NO_EXTERNAL_GREP
219 static int exec_grep(int argc
, const char **argv
)
229 execvp("grep", (char **) argv
);
232 while (waitpid(pid
, &status
, 0) < 0) {
237 if (WIFEXITED(status
)) {
238 if (!WEXITSTATUS(status
))
247 #define push_arg(a) do { \
248 if (nr < MAXARGS) argv[nr++] = (a); \
249 else die("maximum number of args exceeded"); \
253 * If you send a singleton filename to grep, it does not give
254 * the name of the file. GNU grep has "-H" but we would want
255 * that behaviour in a portable way.
257 * So we keep two pathnames in argv buffer unsent to grep in
258 * the main loop if we need to do more than one grep.
260 static int flush_grep(struct grep_opt
*opt
,
261 int argc
, int arg0
, const char **argv
, int *kept
)
264 int count
= argc
- arg0
;
265 const char *kept_0
= NULL
;
269 * Because we keep at least 2 paths in the call from
270 * the main loop (i.e. kept != NULL), and MAXARGS is
271 * far greater than 2, this usually is a call to
272 * conclude the grep. However, the user could attempt
273 * to overflow the argv buffer by giving too many
274 * options to leave very small number of real
275 * arguments even for the call in the main loop.
278 die("insanely many options to grep");
281 * If we have two or more paths, we do not have to do
282 * anything special, but we need to push /dev/null to
283 * get "-H" behaviour of GNU grep portably but when we
284 * are not doing "-l" nor "-L" nor "-c".
288 !opt
->unmatch_name_only
&&
290 argv
[argc
++] = "/dev/null";
297 * Called because we found many paths and haven't finished
298 * iterating over the cache yet. We keep two paths
299 * for the concluding call. argv[argc-2] and argv[argc-1]
300 * has the last two paths, so save the first one away,
301 * replace it with NULL while sending the list to grep,
302 * and recover them after we are done.
305 kept_0
= argv
[argc
-2];
310 if (opt
->pre_context
|| opt
->post_context
) {
312 * grep handles hunk marks between files, but we need to
313 * do that ourselves between multiple calls.
315 if (opt
->show_hunk_mark
)
316 write_or_die(1, "--\n", 3);
318 opt
->show_hunk_mark
= 1;
321 status
= exec_grep(argc
, argv
);
325 * Then recover them. Now the last arg is beyond the
326 * terminating NULL which is at argc, and the second
327 * from the last is what we saved away in kept_0
329 argv
[arg0
++] = kept_0
;
330 argv
[arg0
] = argv
[argc
+1];
335 static void grep_add_color(struct strbuf
*sb
, const char *escape_seq
)
337 size_t orig_len
= sb
->len
;
339 while (*escape_seq
) {
340 if (*escape_seq
== 'm')
341 strbuf_addch(sb
, ';');
342 else if (*escape_seq
!= '\033' && *escape_seq
!= '[')
343 strbuf_addch(sb
, *escape_seq
);
346 if (sb
->len
> orig_len
&& sb
->buf
[sb
->len
- 1] == ';')
347 strbuf_setlen(sb
, sb
->len
- 1);
350 static int external_grep(struct grep_opt
*opt
, const char **paths
, int cached
)
352 int i
, nr
, argc
, hit
, len
, status
;
353 const char *argv
[MAXARGS
+1];
354 char randarg
[ARGBUF
];
355 char *argptr
= randarg
;
358 if (opt
->extended
|| (opt
->relative
&& opt
->prefix_length
))
368 if (opt
->regflags
& REG_EXTENDED
)
370 if (opt
->regflags
& REG_ICASE
)
372 if (opt
->binary
== GREP_BINARY_NOMATCH
)
374 if (opt
->word_regexp
)
378 if (opt
->unmatch_name_only
)
380 if (opt
->null_following_name
)
381 /* in GNU grep git's "-z" translates to "-Z" */
385 if (opt
->post_context
|| opt
->pre_context
) {
386 if (opt
->post_context
!= opt
->pre_context
) {
387 if (opt
->pre_context
) {
389 len
+= snprintf(argptr
, sizeof(randarg
)-len
,
390 "%u", opt
->pre_context
) + 1;
391 if (sizeof(randarg
) <= len
)
392 die("maximum length of args exceeded");
396 if (opt
->post_context
) {
398 len
+= snprintf(argptr
, sizeof(randarg
)-len
,
399 "%u", opt
->post_context
) + 1;
400 if (sizeof(randarg
) <= len
)
401 die("maximum length of args exceeded");
408 len
+= snprintf(argptr
, sizeof(randarg
)-len
,
409 "%u", opt
->post_context
) + 1;
410 if (sizeof(randarg
) <= len
)
411 die("maximum length of args exceeded");
416 for (p
= opt
->pattern_list
; p
; p
= p
->next
) {
418 push_arg(p
->pattern
);
421 struct strbuf sb
= STRBUF_INIT
;
423 grep_add_color(&sb
, opt
->color_match
);
424 setenv("GREP_COLOR", sb
.buf
, 1);
427 strbuf_addstr(&sb
, "mt=");
428 grep_add_color(&sb
, opt
->color_match
);
429 strbuf_addstr(&sb
, ":sl=:cx=:fn=:ln=:bn=:se=");
430 setenv("GREP_COLORS", sb
.buf
, 1);
434 if (opt
->color_external
&& strlen(opt
->color_external
) > 0)
435 push_arg(opt
->color_external
);
437 unsetenv("GREP_COLOR");
438 unsetenv("GREP_COLORS");
440 unsetenv("GREP_OPTIONS");
444 for (i
= 0; i
< active_nr
; i
++) {
445 struct cache_entry
*ce
= active_cache
[i
];
448 if (!S_ISREG(ce
->ce_mode
))
450 if (!pathspec_matches(paths
, ce
->name
, opt
->max_depth
))
453 if (name
[0] == '-') {
454 int len
= ce_namelen(ce
);
455 name
= xmalloc(len
+ 3);
456 memcpy(name
, "./", 2);
457 memcpy(name
+ 2, ce
->name
, len
+ 1);
460 if (MAXARGS
<= argc
) {
461 status
= flush_grep(opt
, argc
, nr
, argv
, &kept
);
469 } while (i
< active_nr
&&
470 !strcmp(ce
->name
, active_cache
[i
]->name
));
471 i
--; /* compensate for loop control */
475 status
= flush_grep(opt
, argc
, nr
, argv
, NULL
);
483 static int grep_cache(struct grep_opt
*opt
, const char **paths
, int cached
,
484 int external_grep_allowed
)
490 #if !NO_EXTERNAL_GREP
492 * Use the external "grep" command for the case where
493 * we grep through the checked-out files. It tends to
494 * be a lot more optimized
496 if (!cached
&& external_grep_allowed
) {
497 hit
= external_grep(opt
, paths
, cached
);
504 for (nr
= 0; nr
< active_nr
; nr
++) {
505 struct cache_entry
*ce
= active_cache
[nr
];
506 if (!S_ISREG(ce
->ce_mode
))
508 if (!pathspec_matches(paths
, ce
->name
, opt
->max_depth
))
511 * If CE_VALID is on, we assume worktree file and its cache entry
512 * are identical, even if worktree file has been modified, so use
513 * cache version instead
515 if (cached
|| (ce
->ce_flags
& CE_VALID
)) {
518 hit
|= grep_sha1(opt
, ce
->sha1
, ce
->name
, 0);
521 hit
|= grep_file(opt
, ce
->name
);
525 } while (nr
< active_nr
&&
526 !strcmp(ce
->name
, active_cache
[nr
]->name
));
527 nr
--; /* compensate for loop control */
530 free_grep_patterns(opt
);
534 static int grep_tree(struct grep_opt
*opt
, const char **paths
,
535 struct tree_desc
*tree
,
536 const char *tree_name
, const char *base
)
540 struct name_entry entry
;
542 int tn_len
= strlen(tree_name
);
543 struct strbuf pathbuf
;
545 strbuf_init(&pathbuf
, PATH_MAX
+ tn_len
);
548 strbuf_add(&pathbuf
, tree_name
, tn_len
);
549 strbuf_addch(&pathbuf
, ':');
550 tn_len
= pathbuf
.len
;
552 strbuf_addstr(&pathbuf
, base
);
555 while (tree_entry(tree
, &entry
)) {
556 int te_len
= tree_entry_len(entry
.path
, entry
.sha1
);
558 strbuf_add(&pathbuf
, entry
.path
, te_len
);
560 if (S_ISDIR(entry
.mode
))
561 /* Match "abc/" against pathspec to
562 * decide if we want to descend into "abc"
565 strbuf_addch(&pathbuf
, '/');
567 down
= pathbuf
.buf
+ tn_len
;
568 if (!pathspec_matches(paths
, down
, opt
->max_depth
))
570 else if (S_ISREG(entry
.mode
))
571 hit
|= grep_sha1(opt
, entry
.sha1
, pathbuf
.buf
, tn_len
);
572 else if (S_ISDIR(entry
.mode
)) {
573 enum object_type type
;
574 struct tree_desc sub
;
578 data
= read_sha1_file(entry
.sha1
, &type
, &size
);
580 die("unable to read tree (%s)",
581 sha1_to_hex(entry
.sha1
));
582 init_tree_desc(&sub
, data
, size
);
583 hit
|= grep_tree(opt
, paths
, &sub
, tree_name
, down
);
587 strbuf_release(&pathbuf
);
591 static int grep_object(struct grep_opt
*opt
, const char **paths
,
592 struct object
*obj
, const char *name
)
594 if (obj
->type
== OBJ_BLOB
)
595 return grep_sha1(opt
, obj
->sha1
, name
, 0);
596 if (obj
->type
== OBJ_COMMIT
|| obj
->type
== OBJ_TREE
) {
597 struct tree_desc tree
;
601 data
= read_object_with_reference(obj
->sha1
, tree_type
,
604 die("unable to read tree (%s)", sha1_to_hex(obj
->sha1
));
605 init_tree_desc(&tree
, data
, size
);
606 hit
= grep_tree(opt
, paths
, &tree
, name
, "");
610 die("unable to grep from object of type %s", typename(obj
->type
));
613 static int context_callback(const struct option
*opt
, const char *arg
,
616 struct grep_opt
*grep_opt
= opt
->value
;
621 grep_opt
->pre_context
= grep_opt
->post_context
= 0;
624 value
= strtol(arg
, (char **)&endp
, 10);
626 return error("switch `%c' expects a numerical value",
629 grep_opt
->pre_context
= grep_opt
->post_context
= value
;
633 static int file_callback(const struct option
*opt
, const char *arg
, int unset
)
635 struct grep_opt
*grep_opt
= opt
->value
;
638 struct strbuf sb
= STRBUF_INIT
;
640 patterns
= fopen(arg
, "r");
642 die_errno("cannot open '%s'", arg
);
643 while (strbuf_getline(&sb
, patterns
, '\n') == 0) {
644 /* ignore empty line like grep does */
647 append_grep_pattern(grep_opt
, strbuf_detach(&sb
, NULL
), arg
,
648 ++lno
, GREP_PATTERN
);
655 static int not_callback(const struct option
*opt
, const char *arg
, int unset
)
657 struct grep_opt
*grep_opt
= opt
->value
;
658 append_grep_pattern(grep_opt
, "--not", "command line", 0, GREP_NOT
);
662 static int and_callback(const struct option
*opt
, const char *arg
, int unset
)
664 struct grep_opt
*grep_opt
= opt
->value
;
665 append_grep_pattern(grep_opt
, "--and", "command line", 0, GREP_AND
);
669 static int open_callback(const struct option
*opt
, const char *arg
, int unset
)
671 struct grep_opt
*grep_opt
= opt
->value
;
672 append_grep_pattern(grep_opt
, "(", "command line", 0, GREP_OPEN_PAREN
);
676 static int close_callback(const struct option
*opt
, const char *arg
, int unset
)
678 struct grep_opt
*grep_opt
= opt
->value
;
679 append_grep_pattern(grep_opt
, ")", "command line", 0, GREP_CLOSE_PAREN
);
683 static int pattern_callback(const struct option
*opt
, const char *arg
,
686 struct grep_opt
*grep_opt
= opt
->value
;
687 append_grep_pattern(grep_opt
, arg
, "-e option", 0, GREP_PATTERN
);
691 static int help_callback(const struct option
*opt
, const char *arg
, int unset
)
696 int cmd_grep(int argc
, const char **argv
, const char *prefix
)
700 int external_grep_allowed
= 1;
701 int seen_dashdash
= 0;
703 struct object_array list
= { 0, 0, NULL
};
704 const char **paths
= NULL
;
707 struct option options
[] = {
708 OPT_BOOLEAN(0, "cached", &cached
,
709 "search in index instead of in the work tree"),
711 OPT_BOOLEAN('v', "invert-match", &opt
.invert
,
712 "show non-matching lines"),
713 OPT_BIT('i', "ignore-case", &opt
.regflags
,
714 "case insensitive matching", REG_ICASE
),
715 OPT_BOOLEAN('w', "word-regexp", &opt
.word_regexp
,
716 "match patterns only at word boundaries"),
717 OPT_SET_INT('a', "text", &opt
.binary
,
718 "process binary files as text", GREP_BINARY_TEXT
),
719 OPT_SET_INT('I', NULL
, &opt
.binary
,
720 "don't match patterns in binary files",
721 GREP_BINARY_NOMATCH
),
722 { OPTION_INTEGER
, 0, "max-depth", &opt
.max_depth
, "depth",
723 "descend at most <depth> levels", PARSE_OPT_NONEG
,
726 OPT_BIT('E', "extended-regexp", &opt
.regflags
,
727 "use extended POSIX regular expressions", REG_EXTENDED
),
728 OPT_NEGBIT('G', "basic-regexp", &opt
.regflags
,
729 "use basic POSIX regular expressions (default)",
731 OPT_BOOLEAN('F', "fixed-strings", &opt
.fixed
,
732 "interpret patterns as fixed strings"),
734 OPT_BOOLEAN('n', NULL
, &opt
.linenum
, "show line numbers"),
735 OPT_NEGBIT('h', NULL
, &opt
.pathname
, "don't show filenames", 1),
736 OPT_BIT('H', NULL
, &opt
.pathname
, "show filenames", 1),
737 OPT_NEGBIT(0, "full-name", &opt
.relative
,
738 "show filenames relative to top directory", 1),
739 OPT_BOOLEAN('l', "files-with-matches", &opt
.name_only
,
740 "show only filenames instead of matching lines"),
741 OPT_BOOLEAN(0, "name-only", &opt
.name_only
,
742 "synonym for --files-with-matches"),
743 OPT_BOOLEAN('L', "files-without-match",
744 &opt
.unmatch_name_only
,
745 "show only the names of files without match"),
746 OPT_BOOLEAN('z', "null", &opt
.null_following_name
,
747 "print NUL after filenames"),
748 OPT_BOOLEAN('c', "count", &opt
.count
,
749 "show the number of matches instead of matching lines"),
750 OPT_SET_INT(0, "color", &opt
.color
, "highlight matches", 1),
752 OPT_CALLBACK('C', NULL
, &opt
, "n",
753 "show <n> context lines before and after matches",
755 OPT_INTEGER('B', NULL
, &opt
.pre_context
,
756 "show <n> context lines before matches"),
757 OPT_INTEGER('A', NULL
, &opt
.post_context
,
758 "show <n> context lines after matches"),
759 OPT_NUMBER_CALLBACK(&opt
, "shortcut for -C NUM",
761 OPT_BOOLEAN('p', "show-function", &opt
.funcname
,
762 "show a line with the function name before matches"),
764 OPT_CALLBACK('f', NULL
, &opt
, "file",
765 "read patterns from file", file_callback
),
766 { OPTION_CALLBACK
, 'e', NULL
, &opt
, "pattern",
767 "match <pattern>", PARSE_OPT_NONEG
, pattern_callback
},
768 { OPTION_CALLBACK
, 0, "and", &opt
, NULL
,
769 "combine patterns specified with -e",
770 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, and_callback
},
771 OPT_BOOLEAN(0, "or", &dummy
, ""),
772 { OPTION_CALLBACK
, 0, "not", &opt
, NULL
, "",
773 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, not_callback
},
774 { OPTION_CALLBACK
, '(', NULL
, &opt
, NULL
, "",
775 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
777 { OPTION_CALLBACK
, ')', NULL
, &opt
, NULL
, "",
778 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
780 OPT_BOOLEAN(0, "all-match", &opt
.all_match
,
781 "show only matches from files that match all patterns"),
784 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed
,
785 "allow calling of grep(1) (ignored by this build)"),
787 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed
,
788 "allow calling of grep(1) (default)"),
790 { OPTION_CALLBACK
, 0, "help-all", &options
, NULL
, "show usage",
791 PARSE_OPT_HIDDEN
| PARSE_OPT_NOARG
, help_callback
},
796 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
797 * to show usage information and exit.
799 if (argc
== 2 && !strcmp(argv
[1], "-h"))
800 usage_with_options(grep_usage
, options
);
802 memset(&opt
, 0, sizeof(opt
));
804 opt
.prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
807 opt
.pattern_tail
= &opt
.pattern_list
;
808 opt
.regflags
= REG_NEWLINE
;
811 strcpy(opt
.color_match
, GIT_COLOR_RED GIT_COLOR_BOLD
);
813 git_config(grep_config
, &opt
);
815 opt
.color
= git_use_color_default
;
818 * If there is no -- then the paths must exist in the working
819 * tree. If there is no explicit pattern specified with -e or
820 * -f, we take the first unrecognized non option to be the
821 * pattern, but then what follows it must be zero or more
822 * valid refs up to the -- (if exists), and then existing
823 * paths. If there is an explicit pattern, then the first
824 * unrecognized non option is the beginning of the refs list
825 * that continues up to the -- (if exists), and then paths.
827 argc
= parse_options(argc
, argv
, prefix
, options
, grep_usage
,
828 PARSE_OPT_KEEP_DASHDASH
|
829 PARSE_OPT_STOP_AT_NON_OPTION
|
830 PARSE_OPT_NO_INTERNAL_HELP
);
832 /* First unrecognized non-option token */
833 if (argc
> 0 && !opt
.pattern_list
) {
834 append_grep_pattern(&opt
, argv
[0], "command line", 0,
840 if ((opt
.color
&& !opt
.color_external
) || opt
.funcname
)
841 external_grep_allowed
= 0;
842 if (!opt
.pattern_list
)
843 die("no pattern given.");
844 if ((opt
.regflags
!= REG_NEWLINE
) && opt
.fixed
)
845 die("cannot mix --fixed-strings and regexp");
846 compile_grep_patterns(&opt
);
848 /* Check revs and then paths */
849 for (i
= 0; i
< argc
; i
++) {
850 const char *arg
= argv
[i
];
851 unsigned char sha1
[20];
853 if (!get_sha1(arg
, sha1
)) {
854 struct object
*object
= parse_object(sha1
);
856 die("bad object %s", arg
);
857 add_object_array(object
, arg
, &list
);
860 if (!strcmp(arg
, "--")) {
867 /* The rest are paths */
868 if (!seen_dashdash
) {
870 for (j
= i
; j
< argc
; j
++)
871 verify_filename(prefix
, argv
[j
]);
875 paths
= get_pathspec(prefix
, argv
+ i
);
877 paths
= xcalloc(2, sizeof(const char *));
885 return !grep_cache(&opt
, paths
, cached
, external_grep_allowed
);
889 die("both --cached and trees are given.");
891 for (i
= 0; i
< list
.nr
; i
++) {
892 struct object
*real_obj
;
893 real_obj
= deref_tag(list
.objects
[i
].item
, NULL
, 0);
894 if (grep_object(&opt
, paths
, real_obj
, list
.objects
[i
].name
))
897 free_grep_patterns(&opt
);