builtin-grep: -w fix
[git/git.git] / builtin-grep.c
CommitLineData
5010cb5f
JH
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"
1362671f 11#include "tree-walk.h"
5010cb5f
JH
12#include "builtin.h"
13#include <regex.h>
e0eb889f 14#include <fnmatch.h>
5010cb5f 15
e0eb889f
JH
16/*
17 * git grep pathspecs are somewhat different from diff-tree pathspecs;
18 * pathname wildcards are allowed.
19 */
1362671f 20static int pathspec_matches(const char **paths, const char *name)
5010cb5f 21{
e0eb889f 22 int namelen, i;
1362671f 23 if (!paths || !*paths)
5010cb5f
JH
24 return 1;
25 namelen = strlen(name);
1362671f
JH
26 for (i = 0; paths[i]; i++) {
27 const char *match = paths[i];
28 int matchlen = strlen(match);
1e3d90e0 29 const char *cp, *meta;
e0eb889f
JH
30
31 if ((matchlen <= namelen) &&
32 !strncmp(name, match, matchlen) &&
33 (match[matchlen-1] == '/' ||
34 name[matchlen] == '\0' || name[matchlen] == '/'))
35 return 1;
36 if (!fnmatch(match, name, 0))
37 return 1;
38 if (name[namelen-1] != '/')
5010cb5f 39 continue;
e0eb889f 40
1e3d90e0 41 /* We are being asked if the directory ("name") is worth
e0eb889f
JH
42 * descending into.
43 *
44 * Find the longest leading directory name that does
45 * not have metacharacter in the pathspec; the name
46 * we are looking at must overlap with that directory.
5010cb5f 47 */
1e3d90e0 48 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
e0eb889f 49 char ch = *cp;
1e3d90e0
JH
50 if (ch == '*' || ch == '[' || ch == '?') {
51 meta = cp;
5010cb5f 52 break;
1e3d90e0 53 }
e0eb889f 54 }
1e3d90e0
JH
55 if (!meta)
56 meta = cp; /* fully literal */
57
58 if (namelen <= meta - match) {
e0eb889f
JH
59 /* Looking at "Documentation/" and
60 * the pattern says "Documentation/howto/", or
1e3d90e0
JH
61 * "Documentation/diff*.txt". The name we
62 * have should match prefix.
e0eb889f
JH
63 */
64 if (!memcmp(match, name, namelen))
65 return 1;
1e3d90e0 66 continue;
e0eb889f 67 }
1e3d90e0
JH
68
69 if (meta - match < namelen) {
e0eb889f 70 /* Looking at "Documentation/howto/" and
1e3d90e0
JH
71 * the pattern says "Documentation/h*";
72 * match up to "Do.../h"; this avoids descending
73 * into "Documentation/technical/".
e0eb889f 74 */
1e3d90e0 75 if (!memcmp(match, name, meta - match))
e0eb889f 76 return 1;
1e3d90e0 77 continue;
e0eb889f 78 }
5010cb5f
JH
79 }
80 return 0;
81}
82
f9b9faf6
JH
83struct grep_pat {
84 struct grep_pat *next;
aa8c79ad
JH
85 const char *origin;
86 int no;
5010cb5f
JH
87 const char *pattern;
88 regex_t regexp;
f9b9faf6
JH
89};
90
91struct grep_opt {
92 struct grep_pat *pattern_list;
93 struct grep_pat **pattern_tail;
94 regex_t regexp;
5010cb5f
JH
95 unsigned linenum:1;
96 unsigned invert:1;
df0e7aa8 97 unsigned name_only:1;
e23d2d6b 98 unsigned unmatch_name_only:1;
2c866cf1 99 unsigned count:1;
7839a25e 100 unsigned word_regexp:1;
b8d0f5a0
JH
101#define GREP_BINARY_DEFAULT 0
102#define GREP_BINARY_NOMATCH 1
103#define GREP_BINARY_TEXT 2
104 unsigned binary:2;
5010cb5f
JH
105 int regflags;
106 unsigned pre_context;
107 unsigned post_context;
108};
109
aa8c79ad
JH
110static void add_pattern(struct grep_opt *opt, const char *pat,
111 const char *origin, int no)
f9b9faf6
JH
112{
113 struct grep_pat *p = xcalloc(1, sizeof(*p));
114 p->pattern = pat;
aa8c79ad
JH
115 p->origin = origin;
116 p->no = no;
f9b9faf6
JH
117 *opt->pattern_tail = p;
118 opt->pattern_tail = &p->next;
119 p->next = NULL;
120}
121
122static void compile_patterns(struct grep_opt *opt)
123{
124 struct grep_pat *p;
125 for (p = opt->pattern_list; p; p = p->next) {
126 int err = regcomp(&p->regexp, p->pattern, opt->regflags);
127 if (err) {
128 char errbuf[1024];
aa8c79ad
JH
129 char where[1024];
130 if (p->no)
131 sprintf(where, "In '%s' at %d, ",
132 p->origin, p->no);
133 else if (p->origin)
134 sprintf(where, "%s, ", p->origin);
135 else
136 where[0] = 0;
f9b9faf6
JH
137 regerror(err, &p->regexp, errbuf, 1024);
138 regfree(&p->regexp);
aa8c79ad 139 die("%s'%s': %s", where, p->pattern, errbuf);
f9b9faf6
JH
140 }
141 }
142}
143
5010cb5f
JH
144static char *end_of_line(char *cp, unsigned long *left)
145{
146 unsigned long l = *left;
147 while (l && *cp != '\n') {
148 l--;
149 cp++;
150 }
151 *left = l;
152 return cp;
153}
154
7839a25e
JH
155static int word_char(char ch)
156{
157 return isalnum(ch) || ch == '_';
158}
159
5010cb5f
JH
160static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
161 const char *name, unsigned lno, char sign)
162{
163 printf("%s%c", name, sign);
164 if (opt->linenum)
165 printf("%d%c", lno, sign);
a24f1e25 166 printf("%.*s\n", (int)(eol-bol), bol);
5010cb5f
JH
167}
168
b8d0f5a0
JH
169/*
170 * NEEDSWORK: share code with diff.c
171 */
172#define FIRST_FEW_BYTES 8000
173static int buffer_is_binary(const char *ptr, unsigned long size)
174{
175 if (FIRST_FEW_BYTES < size)
176 size = FIRST_FEW_BYTES;
177 if (memchr(ptr, 0, size))
178 return 1;
179 return 0;
180}
181
5010cb5f
JH
182static int grep_buffer(struct grep_opt *opt, const char *name,
183 char *buf, unsigned long size)
184{
185 char *bol = buf;
186 unsigned long left = size;
187 unsigned lno = 1;
188 struct pre_context_line {
189 char *bol;
190 char *eol;
191 } *prev = NULL, *pcl;
192 unsigned last_hit = 0;
193 unsigned last_shown = 0;
b8d0f5a0 194 int binary_match_only = 0;
5010cb5f 195 const char *hunk_mark = "";
2c866cf1 196 unsigned count = 0;
5010cb5f 197
b8d0f5a0
JH
198 if (buffer_is_binary(buf, size)) {
199 switch (opt->binary) {
200 case GREP_BINARY_DEFAULT:
201 binary_match_only = 1;
202 break;
203 case GREP_BINARY_NOMATCH:
204 return 0; /* Assume unmatch */
205 break;
206 default:
207 break;
208 }
209 }
210
5010cb5f
JH
211 if (opt->pre_context)
212 prev = xcalloc(opt->pre_context, sizeof(*prev));
213 if (opt->pre_context || opt->post_context)
214 hunk_mark = "--\n";
215
216 while (left) {
217 regmatch_t pmatch[10];
218 char *eol, ch;
f9b9faf6
JH
219 int hit = 0;
220 struct grep_pat *p;
5010cb5f
JH
221
222 eol = end_of_line(bol, &left);
223 ch = *eol;
224 *eol = 0;
225
f9b9faf6
JH
226 for (p = opt->pattern_list; p; p = p->next) {
227 regex_t *exp = &p->regexp;
228 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
229 pmatch, 0);
7839a25e
JH
230
231 if (hit && opt->word_regexp) {
232 /* Match beginning must be either
233 * beginning of the line, or at word
234 * boundary (i.e. the last char must
235 * not be alnum or underscore).
236 */
237 if ((pmatch[0].rm_so < 0) ||
238 (eol - bol) <= pmatch[0].rm_so ||
239 (pmatch[0].rm_eo < 0) ||
240 (eol - bol) < pmatch[0].rm_eo)
241 die("regexp returned nonsense");
242 if (pmatch[0].rm_so != 0 &&
243 word_char(bol[pmatch[0].rm_so-1]))
02ab1c49
JH
244 hit = 0;
245 if (pmatch[0].rm_eo != (eol-bol) &&
7839a25e 246 word_char(bol[pmatch[0].rm_eo]))
02ab1c49 247 hit = 0;
7839a25e 248 }
f9b9faf6
JH
249 if (hit)
250 break;
251 }
252 /* "grep -v -e foo -e bla" should list lines
253 * that do not have either, so inversion should
254 * be done outside.
255 */
5010cb5f
JH
256 if (opt->invert)
257 hit = !hit;
e23d2d6b
JH
258 if (opt->unmatch_name_only) {
259 if (hit)
260 return 0;
261 goto next_line;
262 }
5010cb5f 263 if (hit) {
2c866cf1 264 count++;
b8d0f5a0
JH
265 if (binary_match_only) {
266 printf("Binary file %s matches\n", name);
267 return 1;
268 }
df0e7aa8
JH
269 if (opt->name_only) {
270 printf("%s\n", name);
271 return 1;
272 }
5010cb5f
JH
273 /* Hit at this line. If we haven't shown the
274 * pre-context lines, we would need to show them.
2c866cf1
JH
275 * When asked to do "count", this still show
276 * the context which is nonsense, but the user
277 * deserves to get that ;-).
5010cb5f
JH
278 */
279 if (opt->pre_context) {
280 unsigned from;
281 if (opt->pre_context < lno)
282 from = lno - opt->pre_context;
283 else
284 from = 1;
285 if (from <= last_shown)
286 from = last_shown + 1;
287 if (last_shown && from != last_shown + 1)
288 printf(hunk_mark);
289 while (from < lno) {
290 pcl = &prev[lno-from-1];
291 show_line(opt, pcl->bol, pcl->eol,
292 name, from, '-');
293 from++;
294 }
295 last_shown = lno-1;
296 }
297 if (last_shown && lno != last_shown + 1)
298 printf(hunk_mark);
2c866cf1
JH
299 if (!opt->count)
300 show_line(opt, bol, eol, name, lno, ':');
5010cb5f
JH
301 last_shown = last_hit = lno;
302 }
303 else if (last_hit &&
304 lno <= last_hit + opt->post_context) {
305 /* If the last hit is within the post context,
306 * we need to show this line.
307 */
308 if (last_shown && lno != last_shown + 1)
309 printf(hunk_mark);
310 show_line(opt, bol, eol, name, lno, '-');
311 last_shown = lno;
312 }
313 if (opt->pre_context) {
314 memmove(prev+1, prev,
315 (opt->pre_context-1) * sizeof(*prev));
316 prev->bol = bol;
317 prev->eol = eol;
318 }
e23d2d6b
JH
319
320 next_line:
5010cb5f
JH
321 *eol = ch;
322 bol = eol + 1;
7ed36f56
JH
323 if (!left)
324 break;
5010cb5f
JH
325 left--;
326 lno++;
327 }
e23d2d6b
JH
328
329 if (opt->unmatch_name_only) {
330 /* We did not see any hit, so we want to show this */
331 printf("%s\n", name);
332 return 1;
333 }
334
2c866cf1
JH
335 /* NEEDSWORK:
336 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
337 * which feels mostly useless but sometimes useful. Maybe
338 * make it another option? For now suppress them.
339 */
340 if (opt->count && count)
341 printf("%s:%u\n", name, count);
5010cb5f
JH
342 return !!last_hit;
343}
344
345static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name)
346{
347 unsigned long size;
348 char *data;
349 char type[20];
350 int hit;
351 data = read_sha1_file(sha1, type, &size);
352 if (!data) {
353 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
354 return 0;
355 }
356 hit = grep_buffer(opt, name, data, size);
357 free(data);
358 return hit;
359}
360
361static int grep_file(struct grep_opt *opt, const char *filename)
362{
363 struct stat st;
364 int i;
365 char *data;
366 if (lstat(filename, &st) < 0) {
367 err_ret:
368 if (errno != ENOENT)
369 error("'%s': %s", filename, strerror(errno));
370 return 0;
371 }
372 if (!st.st_size)
373 return 0; /* empty file -- no grep hit */
374 if (!S_ISREG(st.st_mode))
375 return 0;
376 i = open(filename, O_RDONLY);
377 if (i < 0)
378 goto err_ret;
379 data = xmalloc(st.st_size + 1);
380 if (st.st_size != xread(i, data, st.st_size)) {
381 error("'%s': short read %s", filename, strerror(errno));
382 close(i);
383 free(data);
384 return 0;
385 }
386 close(i);
387 i = grep_buffer(opt, filename, data, st.st_size);
388 free(data);
389 return i;
390}
391
1362671f 392static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
5010cb5f
JH
393{
394 int hit = 0;
395 int nr;
396 read_cache();
397
398 for (nr = 0; nr < active_nr; nr++) {
399 struct cache_entry *ce = active_cache[nr];
400 if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
401 continue;
1362671f 402 if (!pathspec_matches(paths, ce->name))
5010cb5f
JH
403 continue;
404 if (cached)
405 hit |= grep_sha1(opt, ce->sha1, ce->name);
406 else
407 hit |= grep_file(opt, ce->name);
408 }
409 return hit;
410}
411
1362671f 412static int grep_tree(struct grep_opt *opt, const char **paths,
5010cb5f
JH
413 struct tree_desc *tree,
414 const char *tree_name, const char *base)
415{
416 unsigned mode;
417 int len;
418 int hit = 0;
419 const char *path;
420 const unsigned char *sha1;
e0eb889f 421 char *down;
5010cb5f
JH
422 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
423
424 if (tree_name[0]) {
425 int offset = sprintf(path_buf, "%s:", tree_name);
e0eb889f
JH
426 down = path_buf + offset;
427 strcat(down, base);
5010cb5f
JH
428 }
429 else {
e0eb889f
JH
430 down = path_buf;
431 strcpy(down, base);
5010cb5f
JH
432 }
433 len = strlen(path_buf);
434
435 while (tree->size) {
436 int pathlen;
437 sha1 = tree_entry_extract(tree, &path, &mode);
438 pathlen = strlen(path);
439 strcpy(path_buf + len, path);
440
e0eb889f
JH
441 if (S_ISDIR(mode))
442 /* Match "abc/" against pathspec to
443 * decide if we want to descend into "abc"
444 * directory.
445 */
446 strcpy(path_buf + len + pathlen, "/");
447
1362671f 448 if (!pathspec_matches(paths, down))
5010cb5f
JH
449 ;
450 else if (S_ISREG(mode))
451 hit |= grep_sha1(opt, sha1, path_buf);
452 else if (S_ISDIR(mode)) {
453 char type[20];
454 struct tree_desc sub;
455 void *data;
456 data = read_sha1_file(sha1, type, &sub.size);
457 if (!data)
458 die("unable to read tree (%s)",
459 sha1_to_hex(sha1));
5010cb5f 460 sub.buf = data;
1362671f 461 hit |= grep_tree(opt, paths, &sub, tree_name, down);
5010cb5f
JH
462 free(data);
463 }
464 update_tree_entry(tree);
465 }
466 return hit;
467}
468
1362671f 469static int grep_object(struct grep_opt *opt, const char **paths,
5010cb5f
JH
470 struct object *obj, const char *name)
471{
472 if (!strcmp(obj->type, blob_type))
473 return grep_sha1(opt, obj->sha1, name);
474 if (!strcmp(obj->type, commit_type) ||
475 !strcmp(obj->type, tree_type)) {
476 struct tree_desc tree;
477 void *data;
478 int hit;
479 data = read_object_with_reference(obj->sha1, tree_type,
480 &tree.size, NULL);
481 if (!data)
482 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
483 tree.buf = data;
1362671f 484 hit = grep_tree(opt, paths, &tree, name, "");
5010cb5f
JH
485 free(data);
486 return hit;
487 }
488 die("unable to grep from object of type %s", obj->type);
489}
490
491static const char builtin_grep_usage[] =
492"git-grep <option>* <rev>* [-e] <pattern> [<path>...]";
493
494int cmd_grep(int argc, const char **argv, char **envp)
495{
5010cb5f 496 int hit = 0;
5010cb5f 497 int cached = 0;
5acd64ed 498 int seen_dashdash = 0;
5010cb5f 499 struct grep_opt opt;
1362671f
JH
500 struct object_list *list, **tail, *object_list = NULL;
501 const char *prefix = setup_git_directory();
502 const char **paths = NULL;
5acd64ed 503 int i;
5010cb5f
JH
504
505 memset(&opt, 0, sizeof(opt));
f9b9faf6 506 opt.pattern_tail = &opt.pattern_list;
5010cb5f
JH
507 opt.regflags = REG_NEWLINE;
508
509 /*
5acd64ed
JH
510 * If there is no -- then the paths must exist in the working
511 * tree. If there is no explicit pattern specified with -e or
512 * -f, we take the first unrecognized non option to be the
513 * pattern, but then what follows it must be zero or more
514 * valid refs up to the -- (if exists), and then existing
515 * paths. If there is an explicit pattern, then the first
516 * unrecocnized non option is the beginning of the refs list
517 * that continues up to the -- (if exists), and then paths.
5010cb5f 518 */
5acd64ed
JH
519
520 tail = &object_list;
1362671f
JH
521 while (1 < argc) {
522 const char *arg = argv[1];
523 argc--; argv++;
524 if (!strcmp("--cached", arg)) {
525 cached = 1;
526 continue;
527 }
b8d0f5a0
JH
528 if (!strcmp("-a", arg) ||
529 !strcmp("--text", arg)) {
530 opt.binary = GREP_BINARY_TEXT;
531 continue;
532 }
1362671f
JH
533 if (!strcmp("-i", arg) ||
534 !strcmp("--ignore-case", arg)) {
535 opt.regflags |= REG_ICASE;
536 continue;
537 }
b8d0f5a0
JH
538 if (!strcmp("-I", arg)) {
539 opt.binary = GREP_BINARY_NOMATCH;
540 continue;
541 }
1362671f
JH
542 if (!strcmp("-v", arg) ||
543 !strcmp("--invert-match", arg)) {
544 opt.invert = 1;
545 continue;
546 }
547 if (!strcmp("-E", arg) ||
548 !strcmp("--extended-regexp", arg)) {
549 opt.regflags |= REG_EXTENDED;
550 continue;
551 }
552 if (!strcmp("-G", arg) ||
553 !strcmp("--basic-regexp", arg)) {
554 opt.regflags &= ~REG_EXTENDED;
555 continue;
556 }
557 if (!strcmp("-n", arg)) {
558 opt.linenum = 1;
559 continue;
560 }
561 if (!strcmp("-H", arg)) {
562 /* We always show the pathname, so this
563 * is a noop.
564 */
565 continue;
566 }
567 if (!strcmp("-l", arg) ||
568 !strcmp("--files-with-matches", arg)) {
569 opt.name_only = 1;
570 continue;
571 }
e23d2d6b
JH
572 if (!strcmp("-L", arg) ||
573 !strcmp("--files-without-match", arg)) {
574 opt.unmatch_name_only = 1;
575 continue;
576 }
2c866cf1
JH
577 if (!strcmp("-c", arg) ||
578 !strcmp("--count", arg)) {
579 opt.count = 1;
580 continue;
581 }
7839a25e
JH
582 if (!strcmp("-w", arg) ||
583 !strcmp("--word-regexp", arg)) {
584 opt.word_regexp = 1;
585 continue;
586 }
f462ebb4
JH
587 if (!strncmp("-A", arg, 2) ||
588 !strncmp("-B", arg, 2) ||
589 !strncmp("-C", arg, 2) ||
590 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
1362671f 591 unsigned num;
f462ebb4
JH
592 const char *scan;
593 switch (arg[1]) {
594 case 'A': case 'B': case 'C':
595 if (!arg[2]) {
596 if (argc <= 1)
597 usage(builtin_grep_usage);
598 scan = *++argv;
599 argc--;
600 }
601 else
602 scan = arg + 2;
603 break;
604 default:
605 scan = arg + 1;
606 break;
607 }
608 if (sscanf(scan, "%u", &num) != 1)
5010cb5f 609 usage(builtin_grep_usage);
1362671f
JH
610 switch (arg[1]) {
611 case 'A':
612 opt.post_context = num;
613 break;
f462ebb4 614 default:
1362671f
JH
615 case 'C':
616 opt.post_context = num;
617 case 'B':
618 opt.pre_context = num;
619 break;
5010cb5f 620 }
1362671f
JH
621 continue;
622 }
aa8c79ad
JH
623 if (!strcmp("-f", arg)) {
624 FILE *patterns;
625 int lno = 0;
626 char buf[1024];
627 if (argc <= 1)
628 usage(builtin_grep_usage);
629 patterns = fopen(argv[1], "r");
630 if (!patterns)
5acd64ed 631 die("'%s': %s", argv[1], strerror(errno));
aa8c79ad
JH
632 while (fgets(buf, sizeof(buf), patterns)) {
633 int len = strlen(buf);
634 if (buf[len-1] == '\n')
635 buf[len-1] = 0;
636 /* ignore empty line like grep does */
637 if (!buf[0])
638 continue;
639 add_pattern(&opt, strdup(buf), argv[1], ++lno);
640 }
641 fclose(patterns);
642 argv++;
643 argc--;
644 continue;
645 }
1362671f
JH
646 if (!strcmp("-e", arg)) {
647 if (1 < argc) {
aa8c79ad 648 add_pattern(&opt, argv[1], "-e option", 0);
f9b9faf6 649 argv++;
1362671f 650 argc--;
5010cb5f
JH
651 continue;
652 }
1362671f
JH
653 usage(builtin_grep_usage);
654 }
5acd64ed
JH
655 if (!strcmp("--", arg))
656 break;
657 if (*arg == '-')
1362671f 658 usage(builtin_grep_usage);
5acd64ed
JH
659
660 /* First unrecognized non-option token */
f9b9faf6 661 if (!opt.pattern_list) {
aa8c79ad 662 add_pattern(&opt, arg, "command line", 0);
1362671f
JH
663 break;
664 }
665 else {
666 /* We are looking at the first path or rev;
5acd64ed 667 * it is found at argv[1] after leaving the
1362671f
JH
668 * loop.
669 */
670 argc++; argv--;
671 break;
5010cb5f 672 }
5010cb5f 673 }
5acd64ed 674
f9b9faf6 675 if (!opt.pattern_list)
5010cb5f 676 die("no pattern given.");
f9b9faf6 677 compile_patterns(&opt);
5acd64ed
JH
678
679 /* Check revs and then paths */
680 for (i = 1; i < argc; i++) {
681 const char *arg = argv[i];
1362671f 682 unsigned char sha1[20];
5acd64ed
JH
683 /* Is it a rev? */
684 if (!get_sha1(arg, sha1)) {
685 struct object *object = parse_object(sha1);
686 struct object_list *elem;
687 if (!object)
688 die("bad object %s", arg);
689 elem = object_list_insert(object, tail);
690 elem->name = arg;
691 tail = &elem->next;
692 continue;
693 }
694 if (!strcmp(arg, "--")) {
695 i++;
696 seen_dashdash = 1;
697 }
698 break;
1362671f 699 }
5acd64ed
JH
700
701 /* The rest are paths */
702 if (!seen_dashdash) {
703 int j;
c39c4f47 704 for (j = i; j < argc; j++)
5acd64ed
JH
705 verify_filename(prefix, argv[j]);
706 }
707
708 if (i < argc)
709 paths = get_pathspec(prefix, argv + i);
1362671f
JH
710 else if (prefix) {
711 paths = xcalloc(2, sizeof(const char *));
712 paths[0] = prefix;
713 paths[1] = NULL;
714 }
5010cb5f 715
1362671f
JH
716 if (!object_list)
717 return !grep_cache(&opt, paths, cached);
aa8c79ad 718
5010cb5f 719 if (cached)
aa8c79ad 720 die("both --cached and trees are given.");
5010cb5f 721
1362671f 722 for (list = object_list; list; list = list->next) {
5010cb5f
JH
723 struct object *real_obj;
724 real_obj = deref_tag(list->item, NULL, 0);
1362671f 725 if (grep_object(&opt, paths, real_obj, list->name))
5010cb5f
JH
726 hit = 1;
727 }
728 return !hit;
729}