git add -p: new "quit" command at the prompt.
[git/kusma.git] / builtin-grep.c
blob89489ddcf8edb85160036b0336ab4ce80c3cb6bc
1 /*
2 * Builtin "git grep"
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #include "cache.h"
7 #include "blob.h"
8 #include "tree.h"
9 #include "commit.h"
10 #include "tag.h"
11 #include "tree-walk.h"
12 #include "builtin.h"
13 #include "grep.h"
15 #ifndef NO_EXTERNAL_GREP
16 #ifdef __unix__
17 #define NO_EXTERNAL_GREP 0
18 #else
19 #define NO_EXTERNAL_GREP 1
20 #endif
21 #endif
23 static int builtin_grep;
25 static int grep_config(const char *var, const char *value, void *cb)
27 struct grep_opt *opt = cb;
29 if (!strcmp(var, "grep.color") || !strcmp(var, "color.grep")) {
30 opt->color = git_config_colorbool(var, value, -1);
31 return 0;
33 if (!strcmp(var, "grep.color.external") ||
34 !strcmp(var, "color.grep.external")) {
35 return git_config_string(&(opt->color_external), var, value);
37 if (!strcmp(var, "grep.color.match") ||
38 !strcmp(var, "color.grep.match")) {
39 if (!value)
40 return config_error_nonbool(var);
41 color_parse(value, var, opt->color_match);
42 return 0;
44 return git_color_default_config(var, value, cb);
48 * git grep pathspecs are somewhat different from diff-tree pathspecs;
49 * pathname wildcards are allowed.
51 static int pathspec_matches(const char **paths, const char *name)
53 int namelen, i;
54 if (!paths || !*paths)
55 return 1;
56 namelen = strlen(name);
57 for (i = 0; paths[i]; i++) {
58 const char *match = paths[i];
59 int matchlen = strlen(match);
60 const char *cp, *meta;
62 if (!matchlen ||
63 ((matchlen <= namelen) &&
64 !strncmp(name, match, matchlen) &&
65 (match[matchlen-1] == '/' ||
66 name[matchlen] == '\0' || name[matchlen] == '/')))
67 return 1;
68 if (!fnmatch(match, name, 0))
69 return 1;
70 if (name[namelen-1] != '/')
71 continue;
73 /* We are being asked if the directory ("name") is worth
74 * descending into.
76 * Find the longest leading directory name that does
77 * not have metacharacter in the pathspec; the name
78 * we are looking at must overlap with that directory.
80 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
81 char ch = *cp;
82 if (ch == '*' || ch == '[' || ch == '?') {
83 meta = cp;
84 break;
87 if (!meta)
88 meta = cp; /* fully literal */
90 if (namelen <= meta - match) {
91 /* Looking at "Documentation/" and
92 * the pattern says "Documentation/howto/", or
93 * "Documentation/diff*.txt". The name we
94 * have should match prefix.
96 if (!memcmp(match, name, namelen))
97 return 1;
98 continue;
101 if (meta - match < namelen) {
102 /* Looking at "Documentation/howto/" and
103 * the pattern says "Documentation/h*";
104 * match up to "Do.../h"; this avoids descending
105 * into "Documentation/technical/".
107 if (!memcmp(match, name, meta - match))
108 return 1;
109 continue;
112 return 0;
115 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len)
117 unsigned long size;
118 char *data;
119 enum object_type type;
120 char *to_free = NULL;
121 int hit;
123 data = read_sha1_file(sha1, &type, &size);
124 if (!data) {
125 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
126 return 0;
128 if (opt->relative && opt->prefix_length) {
129 static char name_buf[PATH_MAX];
130 char *cp;
131 int name_len = strlen(name) - opt->prefix_length + 1;
133 if (!tree_name_len)
134 name += opt->prefix_length;
135 else {
136 if (ARRAY_SIZE(name_buf) <= name_len)
137 cp = to_free = xmalloc(name_len);
138 else
139 cp = name_buf;
140 memcpy(cp, name, tree_name_len);
141 strcpy(cp + tree_name_len,
142 name + tree_name_len + opt->prefix_length);
143 name = cp;
146 hit = grep_buffer(opt, name, data, size);
147 free(data);
148 free(to_free);
149 return hit;
152 static int grep_file(struct grep_opt *opt, const char *filename)
154 struct stat st;
155 int i;
156 char *data;
157 size_t sz;
159 if (lstat(filename, &st) < 0) {
160 err_ret:
161 if (errno != ENOENT)
162 error("'%s': %s", filename, strerror(errno));
163 return 0;
165 if (!st.st_size)
166 return 0; /* empty file -- no grep hit */
167 if (!S_ISREG(st.st_mode))
168 return 0;
169 sz = xsize_t(st.st_size);
170 i = open(filename, O_RDONLY);
171 if (i < 0)
172 goto err_ret;
173 data = xmalloc(sz + 1);
174 if (st.st_size != read_in_full(i, data, sz)) {
175 error("'%s': short read %s", filename, strerror(errno));
176 close(i);
177 free(data);
178 return 0;
180 close(i);
181 if (opt->relative && opt->prefix_length)
182 filename += opt->prefix_length;
183 i = grep_buffer(opt, filename, data, sz);
184 free(data);
185 return i;
188 #if !NO_EXTERNAL_GREP
189 static int exec_grep(int argc, const char **argv)
191 pid_t pid;
192 int status;
194 argv[argc] = NULL;
195 pid = fork();
196 if (pid < 0)
197 return pid;
198 if (!pid) {
199 execvp("grep", (char **) argv);
200 exit(255);
202 while (waitpid(pid, &status, 0) < 0) {
203 if (errno == EINTR)
204 continue;
205 return -1;
207 if (WIFEXITED(status)) {
208 if (!WEXITSTATUS(status))
209 return 1;
210 return 0;
212 return -1;
215 #define MAXARGS 1000
216 #define ARGBUF 4096
217 #define push_arg(a) do { \
218 if (nr < MAXARGS) argv[nr++] = (a); \
219 else die("maximum number of args exceeded"); \
220 } while (0)
223 * If you send a singleton filename to grep, it does not give
224 * the name of the file. GNU grep has "-H" but we would want
225 * that behaviour in a portable way.
227 * So we keep two pathnames in argv buffer unsent to grep in
228 * the main loop if we need to do more than one grep.
230 static int flush_grep(struct grep_opt *opt,
231 int argc, int arg0, const char **argv, int *kept)
233 int status;
234 int count = argc - arg0;
235 const char *kept_0 = NULL;
237 if (count <= 2) {
239 * Because we keep at least 2 paths in the call from
240 * the main loop (i.e. kept != NULL), and MAXARGS is
241 * far greater than 2, this usually is a call to
242 * conclude the grep. However, the user could attempt
243 * to overflow the argv buffer by giving too many
244 * options to leave very small number of real
245 * arguments even for the call in the main loop.
247 if (kept)
248 die("insanely many options to grep");
251 * If we have two or more paths, we do not have to do
252 * anything special, but we need to push /dev/null to
253 * get "-H" behaviour of GNU grep portably but when we
254 * are not doing "-l" nor "-L" nor "-c".
256 if (count == 1 &&
257 !opt->name_only &&
258 !opt->unmatch_name_only &&
259 !opt->count) {
260 argv[argc++] = "/dev/null";
261 argv[argc] = NULL;
265 else if (kept) {
267 * Called because we found many paths and haven't finished
268 * iterating over the cache yet. We keep two paths
269 * for the concluding call. argv[argc-2] and argv[argc-1]
270 * has the last two paths, so save the first one away,
271 * replace it with NULL while sending the list to grep,
272 * and recover them after we are done.
274 *kept = 2;
275 kept_0 = argv[argc-2];
276 argv[argc-2] = NULL;
277 argc -= 2;
280 status = exec_grep(argc, argv);
282 if (kept_0) {
284 * Then recover them. Now the last arg is beyond the
285 * terminating NULL which is at argc, and the second
286 * from the last is what we saved away in kept_0
288 argv[arg0++] = kept_0;
289 argv[arg0] = argv[argc+1];
291 return status;
294 static void grep_add_color(struct strbuf *sb, const char *escape_seq)
296 size_t orig_len = sb->len;
298 while (*escape_seq) {
299 if (*escape_seq == 'm')
300 strbuf_addch(sb, ';');
301 else if (*escape_seq != '\033' && *escape_seq != '[')
302 strbuf_addch(sb, *escape_seq);
303 escape_seq++;
305 if (sb->len > orig_len && sb->buf[sb->len - 1] == ';')
306 strbuf_setlen(sb, sb->len - 1);
309 static int external_grep(struct grep_opt *opt, const char **paths, int cached)
311 int i, nr, argc, hit, len, status;
312 const char *argv[MAXARGS+1];
313 char randarg[ARGBUF];
314 char *argptr = randarg;
315 struct grep_pat *p;
317 if (opt->extended || (opt->relative && opt->prefix_length))
318 return -1;
319 len = nr = 0;
320 push_arg("grep");
321 if (opt->fixed)
322 push_arg("-F");
323 if (opt->linenum)
324 push_arg("-n");
325 if (!opt->pathname)
326 push_arg("-h");
327 if (opt->regflags & REG_EXTENDED)
328 push_arg("-E");
329 if (opt->regflags & REG_ICASE)
330 push_arg("-i");
331 if (opt->binary == GREP_BINARY_NOMATCH)
332 push_arg("-I");
333 if (opt->word_regexp)
334 push_arg("-w");
335 if (opt->name_only)
336 push_arg("-l");
337 if (opt->unmatch_name_only)
338 push_arg("-L");
339 if (opt->null_following_name)
340 /* in GNU grep git's "-z" translates to "-Z" */
341 push_arg("-Z");
342 if (opt->count)
343 push_arg("-c");
344 if (opt->post_context || opt->pre_context) {
345 if (opt->post_context != opt->pre_context) {
346 if (opt->pre_context) {
347 push_arg("-B");
348 len += snprintf(argptr, sizeof(randarg)-len,
349 "%u", opt->pre_context) + 1;
350 if (sizeof(randarg) <= len)
351 die("maximum length of args exceeded");
352 push_arg(argptr);
353 argptr += len;
355 if (opt->post_context) {
356 push_arg("-A");
357 len += snprintf(argptr, sizeof(randarg)-len,
358 "%u", opt->post_context) + 1;
359 if (sizeof(randarg) <= len)
360 die("maximum length of args exceeded");
361 push_arg(argptr);
362 argptr += len;
365 else {
366 push_arg("-C");
367 len += snprintf(argptr, sizeof(randarg)-len,
368 "%u", opt->post_context) + 1;
369 if (sizeof(randarg) <= len)
370 die("maximum length of args exceeded");
371 push_arg(argptr);
372 argptr += len;
375 for (p = opt->pattern_list; p; p = p->next) {
376 push_arg("-e");
377 push_arg(p->pattern);
379 if (opt->color) {
380 struct strbuf sb = STRBUF_INIT;
382 grep_add_color(&sb, opt->color_match);
383 setenv("GREP_COLOR", sb.buf, 1);
385 strbuf_reset(&sb);
386 strbuf_addstr(&sb, "mt=");
387 grep_add_color(&sb, opt->color_match);
388 strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se=");
389 setenv("GREP_COLORS", sb.buf, 1);
391 strbuf_release(&sb);
393 if (opt->color_external && strlen(opt->color_external) > 0)
394 push_arg(opt->color_external);
397 hit = 0;
398 argc = nr;
399 for (i = 0; i < active_nr; i++) {
400 struct cache_entry *ce = active_cache[i];
401 char *name;
402 int kept;
403 if (!S_ISREG(ce->ce_mode))
404 continue;
405 if (!pathspec_matches(paths, ce->name))
406 continue;
407 name = ce->name;
408 if (name[0] == '-') {
409 int len = ce_namelen(ce);
410 name = xmalloc(len + 3);
411 memcpy(name, "./", 2);
412 memcpy(name + 2, ce->name, len + 1);
414 argv[argc++] = name;
415 if (MAXARGS <= argc) {
416 status = flush_grep(opt, argc, nr, argv, &kept);
417 if (0 < status)
418 hit = 1;
419 argc = nr + kept;
421 if (ce_stage(ce)) {
422 do {
423 i++;
424 } while (i < active_nr &&
425 !strcmp(ce->name, active_cache[i]->name));
426 i--; /* compensate for loop control */
429 if (argc > nr) {
430 status = flush_grep(opt, argc, nr, argv, NULL);
431 if (0 < status)
432 hit = 1;
434 return hit;
436 #endif
438 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
440 int hit = 0;
441 int nr;
442 read_cache();
444 #if !NO_EXTERNAL_GREP
446 * Use the external "grep" command for the case where
447 * we grep through the checked-out files. It tends to
448 * be a lot more optimized
450 if (!cached && !builtin_grep) {
451 hit = external_grep(opt, paths, cached);
452 if (hit >= 0)
453 return hit;
455 #endif
457 for (nr = 0; nr < active_nr; nr++) {
458 struct cache_entry *ce = active_cache[nr];
459 if (!S_ISREG(ce->ce_mode))
460 continue;
461 if (!pathspec_matches(paths, ce->name))
462 continue;
464 * If CE_VALID is on, we assume worktree file and its cache entry
465 * are identical, even if worktree file has been modified, so use
466 * cache version instead
468 if (cached || (ce->ce_flags & CE_VALID)) {
469 if (ce_stage(ce))
470 continue;
471 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
473 else
474 hit |= grep_file(opt, ce->name);
475 if (ce_stage(ce)) {
476 do {
477 nr++;
478 } while (nr < active_nr &&
479 !strcmp(ce->name, active_cache[nr]->name));
480 nr--; /* compensate for loop control */
483 free_grep_patterns(opt);
484 return hit;
487 static int grep_tree(struct grep_opt *opt, const char **paths,
488 struct tree_desc *tree,
489 const char *tree_name, const char *base)
491 int len;
492 int hit = 0;
493 struct name_entry entry;
494 char *down;
495 int tn_len = strlen(tree_name);
496 struct strbuf pathbuf;
498 strbuf_init(&pathbuf, PATH_MAX + tn_len);
500 if (tn_len) {
501 strbuf_add(&pathbuf, tree_name, tn_len);
502 strbuf_addch(&pathbuf, ':');
503 tn_len = pathbuf.len;
505 strbuf_addstr(&pathbuf, base);
506 len = pathbuf.len;
508 while (tree_entry(tree, &entry)) {
509 int te_len = tree_entry_len(entry.path, entry.sha1);
510 pathbuf.len = len;
511 strbuf_add(&pathbuf, entry.path, te_len);
513 if (S_ISDIR(entry.mode))
514 /* Match "abc/" against pathspec to
515 * decide if we want to descend into "abc"
516 * directory.
518 strbuf_addch(&pathbuf, '/');
520 down = pathbuf.buf + tn_len;
521 if (!pathspec_matches(paths, down))
523 else if (S_ISREG(entry.mode))
524 hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
525 else if (S_ISDIR(entry.mode)) {
526 enum object_type type;
527 struct tree_desc sub;
528 void *data;
529 unsigned long size;
531 data = read_sha1_file(entry.sha1, &type, &size);
532 if (!data)
533 die("unable to read tree (%s)",
534 sha1_to_hex(entry.sha1));
535 init_tree_desc(&sub, data, size);
536 hit |= grep_tree(opt, paths, &sub, tree_name, down);
537 free(data);
540 strbuf_release(&pathbuf);
541 return hit;
544 static int grep_object(struct grep_opt *opt, const char **paths,
545 struct object *obj, const char *name)
547 if (obj->type == OBJ_BLOB)
548 return grep_sha1(opt, obj->sha1, name, 0);
549 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
550 struct tree_desc tree;
551 void *data;
552 unsigned long size;
553 int hit;
554 data = read_object_with_reference(obj->sha1, tree_type,
555 &size, NULL);
556 if (!data)
557 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
558 init_tree_desc(&tree, data, size);
559 hit = grep_tree(opt, paths, &tree, name, "");
560 free(data);
561 return hit;
563 die("unable to grep from object of type %s", typename(obj->type));
566 static const char builtin_grep_usage[] =
567 "git grep <option>* [-e] <pattern> <rev>* [[--] <path>...]";
569 static const char emsg_invalid_context_len[] =
570 "%s: invalid context length argument";
571 static const char emsg_missing_context_len[] =
572 "missing context length argument";
573 static const char emsg_missing_argument[] =
574 "option requires an argument -%s";
576 int cmd_grep(int argc, const char **argv, const char *prefix)
578 int hit = 0;
579 int cached = 0;
580 int seen_dashdash = 0;
581 struct grep_opt opt;
582 struct object_array list = { 0, 0, NULL };
583 const char **paths = NULL;
584 int i;
586 memset(&opt, 0, sizeof(opt));
587 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
588 opt.relative = 1;
589 opt.pathname = 1;
590 opt.pattern_tail = &opt.pattern_list;
591 opt.regflags = REG_NEWLINE;
593 strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD);
594 opt.color = -1;
595 git_config(grep_config, &opt);
596 if (opt.color == -1)
597 opt.color = git_use_color_default;
600 * If there is no -- then the paths must exist in the working
601 * tree. If there is no explicit pattern specified with -e or
602 * -f, we take the first unrecognized non option to be the
603 * pattern, but then what follows it must be zero or more
604 * valid refs up to the -- (if exists), and then existing
605 * paths. If there is an explicit pattern, then the first
606 * unrecognized non option is the beginning of the refs list
607 * that continues up to the -- (if exists), and then paths.
610 while (1 < argc) {
611 const char *arg = argv[1];
612 argc--; argv++;
613 if (!strcmp("--cached", arg)) {
614 cached = 1;
615 continue;
617 if (!strcmp("--no-ext-grep", arg)) {
618 builtin_grep = 1;
619 continue;
621 if (!strcmp("-a", arg) ||
622 !strcmp("--text", arg)) {
623 opt.binary = GREP_BINARY_TEXT;
624 continue;
626 if (!strcmp("-i", arg) ||
627 !strcmp("--ignore-case", arg)) {
628 opt.regflags |= REG_ICASE;
629 continue;
631 if (!strcmp("-I", arg)) {
632 opt.binary = GREP_BINARY_NOMATCH;
633 continue;
635 if (!strcmp("-v", arg) ||
636 !strcmp("--invert-match", arg)) {
637 opt.invert = 1;
638 continue;
640 if (!strcmp("-E", arg) ||
641 !strcmp("--extended-regexp", arg)) {
642 opt.regflags |= REG_EXTENDED;
643 continue;
645 if (!strcmp("-F", arg) ||
646 !strcmp("--fixed-strings", arg)) {
647 opt.fixed = 1;
648 continue;
650 if (!strcmp("-G", arg) ||
651 !strcmp("--basic-regexp", arg)) {
652 opt.regflags &= ~REG_EXTENDED;
653 continue;
655 if (!strcmp("-n", arg)) {
656 opt.linenum = 1;
657 continue;
659 if (!strcmp("-h", arg)) {
660 opt.pathname = 0;
661 continue;
663 if (!strcmp("-H", arg)) {
664 opt.pathname = 1;
665 continue;
667 if (!strcmp("-l", arg) ||
668 !strcmp("--name-only", arg) ||
669 !strcmp("--files-with-matches", arg)) {
670 opt.name_only = 1;
671 continue;
673 if (!strcmp("-L", arg) ||
674 !strcmp("--files-without-match", arg)) {
675 opt.unmatch_name_only = 1;
676 continue;
678 if (!strcmp("-z", arg) ||
679 !strcmp("--null", arg)) {
680 opt.null_following_name = 1;
681 continue;
683 if (!strcmp("-c", arg) ||
684 !strcmp("--count", arg)) {
685 opt.count = 1;
686 continue;
688 if (!strcmp("-w", arg) ||
689 !strcmp("--word-regexp", arg)) {
690 opt.word_regexp = 1;
691 continue;
693 if (!prefixcmp(arg, "-A") ||
694 !prefixcmp(arg, "-B") ||
695 !prefixcmp(arg, "-C") ||
696 (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
697 unsigned num;
698 const char *scan;
699 switch (arg[1]) {
700 case 'A': case 'B': case 'C':
701 if (!arg[2]) {
702 if (argc <= 1)
703 die(emsg_missing_context_len);
704 scan = *++argv;
705 argc--;
707 else
708 scan = arg + 2;
709 break;
710 default:
711 scan = arg + 1;
712 break;
714 if (strtoul_ui(scan, 10, &num))
715 die(emsg_invalid_context_len, scan);
716 switch (arg[1]) {
717 case 'A':
718 opt.post_context = num;
719 break;
720 default:
721 case 'C':
722 opt.post_context = num;
723 case 'B':
724 opt.pre_context = num;
725 break;
727 continue;
729 if (!strcmp("-f", arg)) {
730 FILE *patterns;
731 int lno = 0;
732 char buf[1024];
733 if (argc <= 1)
734 die(emsg_missing_argument, arg);
735 patterns = fopen(argv[1], "r");
736 if (!patterns)
737 die("'%s': %s", argv[1], strerror(errno));
738 while (fgets(buf, sizeof(buf), patterns)) {
739 int len = strlen(buf);
740 if (len && buf[len-1] == '\n')
741 buf[len-1] = 0;
742 /* ignore empty line like grep does */
743 if (!buf[0])
744 continue;
745 append_grep_pattern(&opt, xstrdup(buf),
746 argv[1], ++lno,
747 GREP_PATTERN);
749 fclose(patterns);
750 argv++;
751 argc--;
752 continue;
754 if (!strcmp("--not", arg)) {
755 append_grep_pattern(&opt, arg, "command line", 0,
756 GREP_NOT);
757 continue;
759 if (!strcmp("--and", arg)) {
760 append_grep_pattern(&opt, arg, "command line", 0,
761 GREP_AND);
762 continue;
764 if (!strcmp("--or", arg))
765 continue; /* no-op */
766 if (!strcmp("(", arg)) {
767 append_grep_pattern(&opt, arg, "command line", 0,
768 GREP_OPEN_PAREN);
769 continue;
771 if (!strcmp(")", arg)) {
772 append_grep_pattern(&opt, arg, "command line", 0,
773 GREP_CLOSE_PAREN);
774 continue;
776 if (!strcmp("--all-match", arg)) {
777 opt.all_match = 1;
778 continue;
780 if (!strcmp("-e", arg)) {
781 if (1 < argc) {
782 append_grep_pattern(&opt, argv[1],
783 "-e option", 0,
784 GREP_PATTERN);
785 argv++;
786 argc--;
787 continue;
789 die(emsg_missing_argument, arg);
791 if (!strcmp("--full-name", arg)) {
792 opt.relative = 0;
793 continue;
795 if (!strcmp("--color", arg)) {
796 opt.color = 1;
797 continue;
799 if (!strcmp("--no-color", arg)) {
800 opt.color = 0;
801 continue;
803 if (!strcmp("--", arg)) {
804 /* later processing wants to have this at argv[1] */
805 argv--;
806 argc++;
807 break;
809 if (*arg == '-')
810 usage(builtin_grep_usage);
812 /* First unrecognized non-option token */
813 if (!opt.pattern_list) {
814 append_grep_pattern(&opt, arg, "command line", 0,
815 GREP_PATTERN);
816 break;
818 else {
819 /* We are looking at the first path or rev;
820 * it is found at argv[1] after leaving the
821 * loop.
823 argc++; argv--;
824 break;
828 if (opt.color && !opt.color_external)
829 builtin_grep = 1;
830 if (!opt.pattern_list)
831 die("no pattern given.");
832 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
833 die("cannot mix --fixed-strings and regexp");
834 compile_grep_patterns(&opt);
836 /* Check revs and then paths */
837 for (i = 1; i < argc; i++) {
838 const char *arg = argv[i];
839 unsigned char sha1[20];
840 /* Is it a rev? */
841 if (!get_sha1(arg, sha1)) {
842 struct object *object = parse_object(sha1);
843 if (!object)
844 die("bad object %s", arg);
845 add_object_array(object, arg, &list);
846 continue;
848 if (!strcmp(arg, "--")) {
849 i++;
850 seen_dashdash = 1;
852 break;
855 /* The rest are paths */
856 if (!seen_dashdash) {
857 int j;
858 for (j = i; j < argc; j++)
859 verify_filename(prefix, argv[j]);
862 if (i < argc) {
863 paths = get_pathspec(prefix, argv + i);
864 if (opt.prefix_length && opt.relative) {
865 /* Make sure we do not get outside of paths */
866 for (i = 0; paths[i]; i++)
867 if (strncmp(prefix, paths[i], opt.prefix_length))
868 die("git grep: cannot generate relative filenames containing '..'");
871 else if (prefix) {
872 paths = xcalloc(2, sizeof(const char *));
873 paths[0] = prefix;
874 paths[1] = NULL;
877 if (!list.nr) {
878 if (!cached)
879 setup_work_tree();
880 return !grep_cache(&opt, paths, cached);
883 if (cached)
884 die("both --cached and trees are given.");
886 for (i = 0; i < list.nr; i++) {
887 struct object *real_obj;
888 real_obj = deref_tag(list.objects[i].item, NULL, 0);
889 if (grep_object(&opt, paths, real_obj, list.objects[i].name))
890 hit = 1;
892 free_grep_patterns(&opt);
893 return !hit;