4 * Copyright (c) 2006 Junio C Hamano
11 #include "tree-walk.h"
13 #include "parse-options.h"
19 #include "thread-utils.h"
23 static char const * const grep_usage
[] = {
24 "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
28 static int use_threads
= 1;
32 static pthread_t threads
[THREADS
];
34 static void *load_sha1(const unsigned char *sha1
, unsigned long *size
,
36 static void *load_file(const char *filename
, size_t *sz
);
38 enum work_type
{WORK_SHA1
, WORK_FILE
};
40 /* We use one producer thread and THREADS consumer
41 * threads. The producer adds struct work_items to 'todo' and the
42 * consumers pick work items from the same array.
49 /* if type == WORK_SHA1, then 'identifier' is a SHA1,
50 * otherwise type == WORK_FILE, and 'identifier' is a NUL
51 * terminated filename.
58 /* In the range [todo_done, todo_start) in 'todo' we have work_items
59 * that have been or are processed by a consumer thread. We haven't
60 * written the result for these to stdout yet.
62 * The work_items in [todo_start, todo_end) are waiting to be picked
63 * up by a consumer thread.
65 * The ranges are modulo TODO_SIZE.
68 static struct work_item todo
[TODO_SIZE
];
69 static int todo_start
;
73 /* Has all work items been added? */
74 static int all_work_added
;
76 /* This lock protects all the variables above. */
77 static pthread_mutex_t grep_mutex
;
79 /* Used to serialize calls to read_sha1_file. */
80 static pthread_mutex_t read_sha1_mutex
;
82 #define grep_lock() pthread_mutex_lock(&grep_mutex)
83 #define grep_unlock() pthread_mutex_unlock(&grep_mutex)
84 #define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex)
85 #define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex)
87 /* Signalled when a new work_item is added to todo. */
88 static pthread_cond_t cond_add
;
90 /* Signalled when the result from one work_item is written to
93 static pthread_cond_t cond_write
;
95 /* Signalled when we are finished with everything. */
96 static pthread_cond_t cond_result
;
98 static void add_work(enum work_type type
, char *name
, void *id
)
102 while ((todo_end
+1) % ARRAY_SIZE(todo
) == todo_done
) {
103 pthread_cond_wait(&cond_write
, &grep_mutex
);
106 todo
[todo_end
].type
= type
;
107 todo
[todo_end
].name
= name
;
108 todo
[todo_end
].identifier
= id
;
109 todo
[todo_end
].done
= 0;
110 strbuf_reset(&todo
[todo_end
].out
);
111 todo_end
= (todo_end
+ 1) % ARRAY_SIZE(todo
);
113 pthread_cond_signal(&cond_add
);
117 static struct work_item
*get_work(void)
119 struct work_item
*ret
;
122 while (todo_start
== todo_end
&& !all_work_added
) {
123 pthread_cond_wait(&cond_add
, &grep_mutex
);
126 if (todo_start
== todo_end
&& all_work_added
) {
129 ret
= &todo
[todo_start
];
130 todo_start
= (todo_start
+ 1) % ARRAY_SIZE(todo
);
136 static void grep_sha1_async(struct grep_opt
*opt
, char *name
,
137 const unsigned char *sha1
)
142 add_work(WORK_SHA1
, name
, s
);
145 static void grep_file_async(struct grep_opt
*opt
, char *name
,
146 const char *filename
)
148 add_work(WORK_FILE
, name
, xstrdup(filename
));
151 static void work_done(struct work_item
*w
)
157 old_done
= todo_done
;
158 for(; todo
[todo_done
].done
&& todo_done
!= todo_start
;
159 todo_done
= (todo_done
+1) % ARRAY_SIZE(todo
)) {
160 w
= &todo
[todo_done
];
161 write_or_die(1, w
->out
.buf
, w
->out
.len
);
166 if (old_done
!= todo_done
)
167 pthread_cond_signal(&cond_write
);
169 if (all_work_added
&& todo_done
== todo_end
)
170 pthread_cond_signal(&cond_result
);
175 static void *run(void *arg
)
178 struct grep_opt
*opt
= arg
;
181 struct work_item
*w
= get_work();
185 opt
->output_priv
= w
;
186 if (w
->type
== WORK_SHA1
) {
188 void* data
= load_sha1(w
->identifier
, &sz
, w
->name
);
191 hit
|= grep_buffer(opt
, w
->name
, data
, sz
);
194 } else if (w
->type
== WORK_FILE
) {
196 void* data
= load_file(w
->identifier
, &sz
);
198 hit
|= grep_buffer(opt
, w
->name
, data
, sz
);
207 free_grep_patterns(arg
);
210 return (void*) (intptr_t) hit
;
213 static void strbuf_out(struct grep_opt
*opt
, const void *buf
, size_t size
)
215 struct work_item
*w
= opt
->output_priv
;
216 strbuf_add(&w
->out
, buf
, size
);
219 static void start_threads(struct grep_opt
*opt
)
223 pthread_mutex_init(&grep_mutex
, NULL
);
224 pthread_mutex_init(&read_sha1_mutex
, NULL
);
225 pthread_cond_init(&cond_add
, NULL
);
226 pthread_cond_init(&cond_write
, NULL
);
227 pthread_cond_init(&cond_result
, NULL
);
229 for (i
= 0; i
< ARRAY_SIZE(todo
); i
++) {
230 strbuf_init(&todo
[i
].out
, 0);
233 for (i
= 0; i
< ARRAY_SIZE(threads
); i
++) {
235 struct grep_opt
*o
= grep_opt_dup(opt
);
236 o
->output
= strbuf_out
;
237 compile_grep_patterns(o
);
238 err
= pthread_create(&threads
[i
], NULL
, run
, o
);
241 die("grep: failed to create thread: %s",
246 static int wait_all(void)
254 /* Wait until all work is done. */
255 while (todo_done
!= todo_end
)
256 pthread_cond_wait(&cond_result
, &grep_mutex
);
258 /* Wake up all the consumer threads so they can see that there
259 * is no more work to do.
261 pthread_cond_broadcast(&cond_add
);
264 for (i
= 0; i
< ARRAY_SIZE(threads
); i
++) {
266 pthread_join(threads
[i
], &h
);
267 hit
|= (int) (intptr_t) h
;
270 pthread_mutex_destroy(&grep_mutex
);
271 pthread_mutex_destroy(&read_sha1_mutex
);
272 pthread_cond_destroy(&cond_add
);
273 pthread_cond_destroy(&cond_write
);
274 pthread_cond_destroy(&cond_result
);
278 #else /* !NO_PTHREADS */
279 #define read_sha1_lock()
280 #define read_sha1_unlock()
282 static int wait_all(void)
288 static int grep_config(const char *var
, const char *value
, void *cb
)
290 struct grep_opt
*opt
= cb
;
292 switch (userdiff_config(var
, value
)) {
298 if (!strcmp(var
, "color.grep")) {
299 opt
->color
= git_config_colorbool(var
, value
, -1);
302 if (!strcmp(var
, "color.grep.match")) {
304 return config_error_nonbool(var
);
305 color_parse(value
, var
, opt
->color_match
);
308 return git_color_default_config(var
, value
, cb
);
312 * Return non-zero if max_depth is negative or path has no more then max_depth
315 static int accept_subdir(const char *path
, int max_depth
)
320 while ((path
= strchr(path
, '/')) != NULL
) {
330 * Return non-zero if name is a subdirectory of match and is not too deep.
332 static int is_subdir(const char *name
, int namelen
,
333 const char *match
, int matchlen
, int max_depth
)
335 if (matchlen
> namelen
|| strncmp(name
, match
, matchlen
))
338 if (name
[matchlen
] == '\0') /* exact match */
341 if (!matchlen
|| match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
342 return accept_subdir(name
+ matchlen
+ 1, max_depth
);
348 * git grep pathspecs are somewhat different from diff-tree pathspecs;
349 * pathname wildcards are allowed.
351 static int pathspec_matches(const char **paths
, const char *name
, int max_depth
)
354 if (!paths
|| !*paths
)
355 return accept_subdir(name
, max_depth
);
356 namelen
= strlen(name
);
357 for (i
= 0; paths
[i
]; i
++) {
358 const char *match
= paths
[i
];
359 int matchlen
= strlen(match
);
360 const char *cp
, *meta
;
362 if (is_subdir(name
, namelen
, match
, matchlen
, max_depth
))
364 if (!fnmatch(match
, name
, 0))
366 if (name
[namelen
-1] != '/')
369 /* We are being asked if the directory ("name") is worth
372 * Find the longest leading directory name that does
373 * not have metacharacter in the pathspec; the name
374 * we are looking at must overlap with that directory.
376 for (cp
= match
, meta
= NULL
; cp
- match
< matchlen
; cp
++) {
378 if (ch
== '*' || ch
== '[' || ch
== '?') {
384 meta
= cp
; /* fully literal */
386 if (namelen
<= meta
- match
) {
387 /* Looking at "Documentation/" and
388 * the pattern says "Documentation/howto/", or
389 * "Documentation/diff*.txt". The name we
390 * have should match prefix.
392 if (!memcmp(match
, name
, namelen
))
397 if (meta
- match
< namelen
) {
398 /* Looking at "Documentation/howto/" and
399 * the pattern says "Documentation/h*";
400 * match up to "Do.../h"; this avoids descending
401 * into "Documentation/technical/".
403 if (!memcmp(match
, name
, meta
- match
))
411 static void *lock_and_read_sha1_file(const unsigned char *sha1
, enum object_type
*type
, unsigned long *size
)
417 data
= read_sha1_file(sha1
, type
, size
);
420 data
= read_sha1_file(sha1
, type
, size
);
425 static void *load_sha1(const unsigned char *sha1
, unsigned long *size
,
428 enum object_type type
;
429 void *data
= lock_and_read_sha1_file(sha1
, &type
, size
);
432 error("'%s': unable to read %s", name
, sha1_to_hex(sha1
));
437 static int grep_sha1(struct grep_opt
*opt
, const unsigned char *sha1
,
438 const char *filename
, int tree_name_len
)
440 struct strbuf pathbuf
= STRBUF_INIT
;
443 if (opt
->relative
&& opt
->prefix_length
) {
444 quote_path_relative(filename
+ tree_name_len
, -1, &pathbuf
,
446 strbuf_insert(&pathbuf
, 0, filename
, tree_name_len
);
448 strbuf_addstr(&pathbuf
, filename
);
451 name
= strbuf_detach(&pathbuf
, NULL
);
455 grep_sha1_async(opt
, name
, sha1
);
462 void *data
= load_sha1(sha1
, &sz
, name
);
466 hit
= grep_buffer(opt
, name
, data
, sz
);
474 static void *load_file(const char *filename
, size_t *sz
)
480 if (lstat(filename
, &st
) < 0) {
483 error("'%s': %s", filename
, strerror(errno
));
486 if (!S_ISREG(st
.st_mode
))
488 *sz
= xsize_t(st
.st_size
);
489 i
= open(filename
, O_RDONLY
);
492 data
= xmalloc(*sz
+ 1);
493 if (st
.st_size
!= read_in_full(i
, data
, *sz
)) {
494 error("'%s': short read %s", filename
, strerror(errno
));
504 static int grep_file(struct grep_opt
*opt
, const char *filename
)
506 struct strbuf buf
= STRBUF_INIT
;
509 if (opt
->relative
&& opt
->prefix_length
)
510 quote_path_relative(filename
, -1, &buf
, opt
->prefix
);
512 strbuf_addstr(&buf
, filename
);
513 name
= strbuf_detach(&buf
, NULL
);
517 grep_file_async(opt
, name
, filename
);
524 void *data
= load_file(filename
, &sz
);
528 hit
= grep_buffer(opt
, name
, data
, sz
);
536 static int grep_cache(struct grep_opt
*opt
, const char **paths
, int cached
)
542 for (nr
= 0; nr
< active_nr
; nr
++) {
543 struct cache_entry
*ce
= active_cache
[nr
];
544 if (!S_ISREG(ce
->ce_mode
))
546 if (!pathspec_matches(paths
, ce
->name
, opt
->max_depth
))
549 * If CE_VALID is on, we assume worktree file and its cache entry
550 * are identical, even if worktree file has been modified, so use
551 * cache version instead
553 if (cached
|| (ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
)) {
556 hit
|= grep_sha1(opt
, ce
->sha1
, ce
->name
, 0);
559 hit
|= grep_file(opt
, ce
->name
);
563 } while (nr
< active_nr
&&
564 !strcmp(ce
->name
, active_cache
[nr
]->name
));
565 nr
--; /* compensate for loop control */
567 if (hit
&& opt
->status_only
)
570 free_grep_patterns(opt
);
574 static int grep_tree(struct grep_opt
*opt
, const char **paths
,
575 struct tree_desc
*tree
,
576 const char *tree_name
, const char *base
)
580 struct name_entry entry
;
582 int tn_len
= strlen(tree_name
);
583 struct strbuf pathbuf
;
585 strbuf_init(&pathbuf
, PATH_MAX
+ tn_len
);
588 strbuf_add(&pathbuf
, tree_name
, tn_len
);
589 strbuf_addch(&pathbuf
, ':');
590 tn_len
= pathbuf
.len
;
592 strbuf_addstr(&pathbuf
, base
);
595 while (tree_entry(tree
, &entry
)) {
596 int te_len
= tree_entry_len(entry
.path
, entry
.sha1
);
598 strbuf_add(&pathbuf
, entry
.path
, te_len
);
600 if (S_ISDIR(entry
.mode
))
601 /* Match "abc/" against pathspec to
602 * decide if we want to descend into "abc"
605 strbuf_addch(&pathbuf
, '/');
607 down
= pathbuf
.buf
+ tn_len
;
608 if (!pathspec_matches(paths
, down
, opt
->max_depth
))
610 else if (S_ISREG(entry
.mode
))
611 hit
|= grep_sha1(opt
, entry
.sha1
, pathbuf
.buf
, tn_len
);
612 else if (S_ISDIR(entry
.mode
)) {
613 enum object_type type
;
614 struct tree_desc sub
;
618 data
= lock_and_read_sha1_file(entry
.sha1
, &type
, &size
);
620 die("unable to read tree (%s)",
621 sha1_to_hex(entry
.sha1
));
622 init_tree_desc(&sub
, data
, size
);
623 hit
|= grep_tree(opt
, paths
, &sub
, tree_name
, down
);
626 if (hit
&& opt
->status_only
)
629 strbuf_release(&pathbuf
);
633 static int grep_object(struct grep_opt
*opt
, const char **paths
,
634 struct object
*obj
, const char *name
)
636 if (obj
->type
== OBJ_BLOB
)
637 return grep_sha1(opt
, obj
->sha1
, name
, 0);
638 if (obj
->type
== OBJ_COMMIT
|| obj
->type
== OBJ_TREE
) {
639 struct tree_desc tree
;
643 data
= read_object_with_reference(obj
->sha1
, tree_type
,
646 die("unable to read tree (%s)", sha1_to_hex(obj
->sha1
));
647 init_tree_desc(&tree
, data
, size
);
648 hit
= grep_tree(opt
, paths
, &tree
, name
, "");
652 die("unable to grep from object of type %s", typename(obj
->type
));
655 static int context_callback(const struct option
*opt
, const char *arg
,
658 struct grep_opt
*grep_opt
= opt
->value
;
663 grep_opt
->pre_context
= grep_opt
->post_context
= 0;
666 value
= strtol(arg
, (char **)&endp
, 10);
668 return error("switch `%c' expects a numerical value",
671 grep_opt
->pre_context
= grep_opt
->post_context
= value
;
675 static int file_callback(const struct option
*opt
, const char *arg
, int unset
)
677 struct grep_opt
*grep_opt
= opt
->value
;
680 struct strbuf sb
= STRBUF_INIT
;
682 patterns
= fopen(arg
, "r");
684 die_errno("cannot open '%s'", arg
);
685 while (strbuf_getline(&sb
, patterns
, '\n') == 0) {
686 /* ignore empty line like grep does */
689 append_grep_pattern(grep_opt
, strbuf_detach(&sb
, NULL
), arg
,
690 ++lno
, GREP_PATTERN
);
697 static int not_callback(const struct option
*opt
, const char *arg
, int unset
)
699 struct grep_opt
*grep_opt
= opt
->value
;
700 append_grep_pattern(grep_opt
, "--not", "command line", 0, GREP_NOT
);
704 static int and_callback(const struct option
*opt
, const char *arg
, int unset
)
706 struct grep_opt
*grep_opt
= opt
->value
;
707 append_grep_pattern(grep_opt
, "--and", "command line", 0, GREP_AND
);
711 static int open_callback(const struct option
*opt
, const char *arg
, int unset
)
713 struct grep_opt
*grep_opt
= opt
->value
;
714 append_grep_pattern(grep_opt
, "(", "command line", 0, GREP_OPEN_PAREN
);
718 static int close_callback(const struct option
*opt
, const char *arg
, int unset
)
720 struct grep_opt
*grep_opt
= opt
->value
;
721 append_grep_pattern(grep_opt
, ")", "command line", 0, GREP_CLOSE_PAREN
);
725 static int pattern_callback(const struct option
*opt
, const char *arg
,
728 struct grep_opt
*grep_opt
= opt
->value
;
729 append_grep_pattern(grep_opt
, arg
, "-e option", 0, GREP_PATTERN
);
733 static int help_callback(const struct option
*opt
, const char *arg
, int unset
)
738 int cmd_grep(int argc
, const char **argv
, const char *prefix
)
742 int seen_dashdash
= 0;
743 int external_grep_allowed__ignored
;
745 struct object_array list
= { 0, 0, NULL
};
746 const char **paths
= NULL
;
749 struct option options
[] = {
750 OPT_BOOLEAN(0, "cached", &cached
,
751 "search in index instead of in the work tree"),
753 OPT_BOOLEAN('v', "invert-match", &opt
.invert
,
754 "show non-matching lines"),
755 OPT_BOOLEAN('i', "ignore-case", &opt
.ignore_case
,
756 "case insensitive matching"),
757 OPT_BOOLEAN('w', "word-regexp", &opt
.word_regexp
,
758 "match patterns only at word boundaries"),
759 OPT_SET_INT('a', "text", &opt
.binary
,
760 "process binary files as text", GREP_BINARY_TEXT
),
761 OPT_SET_INT('I', NULL
, &opt
.binary
,
762 "don't match patterns in binary files",
763 GREP_BINARY_NOMATCH
),
764 { OPTION_INTEGER
, 0, "max-depth", &opt
.max_depth
, "depth",
765 "descend at most <depth> levels", PARSE_OPT_NONEG
,
768 OPT_BIT('E', "extended-regexp", &opt
.regflags
,
769 "use extended POSIX regular expressions", REG_EXTENDED
),
770 OPT_NEGBIT('G', "basic-regexp", &opt
.regflags
,
771 "use basic POSIX regular expressions (default)",
773 OPT_BOOLEAN('F', "fixed-strings", &opt
.fixed
,
774 "interpret patterns as fixed strings"),
776 OPT_BOOLEAN('n', NULL
, &opt
.linenum
, "show line numbers"),
777 OPT_NEGBIT('h', NULL
, &opt
.pathname
, "don't show filenames", 1),
778 OPT_BIT('H', NULL
, &opt
.pathname
, "show filenames", 1),
779 OPT_NEGBIT(0, "full-name", &opt
.relative
,
780 "show filenames relative to top directory", 1),
781 OPT_BOOLEAN('l', "files-with-matches", &opt
.name_only
,
782 "show only filenames instead of matching lines"),
783 OPT_BOOLEAN(0, "name-only", &opt
.name_only
,
784 "synonym for --files-with-matches"),
785 OPT_BOOLEAN('L', "files-without-match",
786 &opt
.unmatch_name_only
,
787 "show only the names of files without match"),
788 OPT_BOOLEAN('z', "null", &opt
.null_following_name
,
789 "print NUL after filenames"),
790 OPT_BOOLEAN('c', "count", &opt
.count
,
791 "show the number of matches instead of matching lines"),
792 OPT_SET_INT(0, "color", &opt
.color
, "highlight matches", 1),
794 OPT_CALLBACK('C', NULL
, &opt
, "n",
795 "show <n> context lines before and after matches",
797 OPT_INTEGER('B', NULL
, &opt
.pre_context
,
798 "show <n> context lines before matches"),
799 OPT_INTEGER('A', NULL
, &opt
.post_context
,
800 "show <n> context lines after matches"),
801 OPT_NUMBER_CALLBACK(&opt
, "shortcut for -C NUM",
803 OPT_BOOLEAN('p', "show-function", &opt
.funcname
,
804 "show a line with the function name before matches"),
806 OPT_CALLBACK('f', NULL
, &opt
, "file",
807 "read patterns from file", file_callback
),
808 { OPTION_CALLBACK
, 'e', NULL
, &opt
, "pattern",
809 "match <pattern>", PARSE_OPT_NONEG
, pattern_callback
},
810 { OPTION_CALLBACK
, 0, "and", &opt
, NULL
,
811 "combine patterns specified with -e",
812 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, and_callback
},
813 OPT_BOOLEAN(0, "or", &dummy
, ""),
814 { OPTION_CALLBACK
, 0, "not", &opt
, NULL
, "",
815 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, not_callback
},
816 { OPTION_CALLBACK
, '(', NULL
, &opt
, NULL
, "",
817 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
819 { OPTION_CALLBACK
, ')', NULL
, &opt
, NULL
, "",
820 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
822 OPT_BOOLEAN('q', "quiet", &opt
.status_only
,
823 "indicate hit with exit status without output"),
824 OPT_BOOLEAN(0, "all-match", &opt
.all_match
,
825 "show only matches from files that match all patterns"),
827 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored
,
828 "allow calling of grep(1) (ignored by this build)"),
829 { OPTION_CALLBACK
, 0, "help-all", &options
, NULL
, "show usage",
830 PARSE_OPT_HIDDEN
| PARSE_OPT_NOARG
, help_callback
},
835 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
836 * to show usage information and exit.
838 if (argc
== 2 && !strcmp(argv
[1], "-h"))
839 usage_with_options(grep_usage
, options
);
841 memset(&opt
, 0, sizeof(opt
));
843 opt
.prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
846 opt
.pattern_tail
= &opt
.pattern_list
;
847 opt
.header_tail
= &opt
.header_list
;
848 opt
.regflags
= REG_NEWLINE
;
851 strcpy(opt
.color_match
, GIT_COLOR_RED GIT_COLOR_BOLD
);
853 git_config(grep_config
, &opt
);
855 opt
.color
= git_use_color_default
;
858 * If there is no -- then the paths must exist in the working
859 * tree. If there is no explicit pattern specified with -e or
860 * -f, we take the first unrecognized non option to be the
861 * pattern, but then what follows it must be zero or more
862 * valid refs up to the -- (if exists), and then existing
863 * paths. If there is an explicit pattern, then the first
864 * unrecognized non option is the beginning of the refs list
865 * that continues up to the -- (if exists), and then paths.
867 argc
= parse_options(argc
, argv
, prefix
, options
, grep_usage
,
868 PARSE_OPT_KEEP_DASHDASH
|
869 PARSE_OPT_STOP_AT_NON_OPTION
|
870 PARSE_OPT_NO_INTERNAL_HELP
);
873 * skip a -- separator; we know it cannot be
874 * separating revisions from pathnames if
875 * we haven't even had any patterns yet
877 if (argc
> 0 && !opt
.pattern_list
&& !strcmp(argv
[0], "--")) {
882 /* First unrecognized non-option token */
883 if (argc
> 0 && !opt
.pattern_list
) {
884 append_grep_pattern(&opt
, argv
[0], "command line", 0,
890 if (!opt
.pattern_list
)
891 die("no pattern given.");
892 if (!opt
.fixed
&& opt
.ignore_case
)
893 opt
.regflags
|= REG_ICASE
;
894 if ((opt
.regflags
!= REG_NEWLINE
) && opt
.fixed
)
895 die("cannot mix --fixed-strings and regexp");
898 if (online_cpus() == 1 || !grep_threads_ok(&opt
))
907 compile_grep_patterns(&opt
);
909 /* Check revs and then paths */
910 for (i
= 0; i
< argc
; i
++) {
911 const char *arg
= argv
[i
];
912 unsigned char sha1
[20];
914 if (!get_sha1(arg
, sha1
)) {
915 struct object
*object
= parse_object(sha1
);
917 die("bad object %s", arg
);
918 add_object_array(object
, arg
, &list
);
921 if (!strcmp(arg
, "--")) {
928 /* The rest are paths */
929 if (!seen_dashdash
) {
931 for (j
= i
; j
< argc
; j
++)
932 verify_filename(prefix
, argv
[j
]);
936 paths
= get_pathspec(prefix
, argv
+ i
);
938 paths
= xcalloc(2, sizeof(const char *));
948 hit
= grep_cache(&opt
, paths
, cached
);
955 die("both --cached and trees are given.");
957 for (i
= 0; i
< list
.nr
; i
++) {
958 struct object
*real_obj
;
959 real_obj
= deref_tag(list
.objects
[i
].item
, NULL
, 0);
960 if (grep_object(&opt
, paths
, real_obj
, list
.objects
[i
].name
)) {
969 free_grep_patterns(&opt
);