4 * Copyright (c) 2006 Junio C Hamano
11 #include "tree-walk.h"
13 #include "parse-options.h"
21 #include "thread-utils.h"
24 static char const * const grep_usage
[] = {
25 "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
29 static int use_threads
= 1;
33 static pthread_t threads
[THREADS
];
35 static void *load_sha1(const unsigned char *sha1
, unsigned long *size
,
37 static void *load_file(const char *filename
, size_t *sz
);
39 enum work_type
{WORK_SHA1
, WORK_FILE
};
41 /* We use one producer thread and THREADS consumer
42 * threads. The producer adds struct work_items to 'todo' and the
43 * consumers pick work items from the same array.
50 /* if type == WORK_SHA1, then 'identifier' is a SHA1,
51 * otherwise type == WORK_FILE, and 'identifier' is a NUL
52 * terminated filename.
59 /* In the range [todo_done, todo_start) in 'todo' we have work_items
60 * that have been or are processed by a consumer thread. We haven't
61 * written the result for these to stdout yet.
63 * The work_items in [todo_start, todo_end) are waiting to be picked
64 * up by a consumer thread.
66 * The ranges are modulo TODO_SIZE.
69 static struct work_item todo
[TODO_SIZE
];
70 static int todo_start
;
74 /* Has all work items been added? */
75 static int all_work_added
;
77 /* This lock protects all the variables above. */
78 static pthread_mutex_t grep_mutex
;
80 /* Used to serialize calls to read_sha1_file. */
81 static pthread_mutex_t read_sha1_mutex
;
83 #define grep_lock() pthread_mutex_lock(&grep_mutex)
84 #define grep_unlock() pthread_mutex_unlock(&grep_mutex)
85 #define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex)
86 #define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex)
88 /* Signalled when a new work_item is added to todo. */
89 static pthread_cond_t cond_add
;
91 /* Signalled when the result from one work_item is written to
94 static pthread_cond_t cond_write
;
96 /* Signalled when we are finished with everything. */
97 static pthread_cond_t cond_result
;
99 static int print_hunk_marks_between_files
;
100 static int printed_something
;
102 static void add_work(enum work_type type
, char *name
, void *id
)
106 while ((todo_end
+1) % ARRAY_SIZE(todo
) == todo_done
) {
107 pthread_cond_wait(&cond_write
, &grep_mutex
);
110 todo
[todo_end
].type
= type
;
111 todo
[todo_end
].name
= name
;
112 todo
[todo_end
].identifier
= id
;
113 todo
[todo_end
].done
= 0;
114 strbuf_reset(&todo
[todo_end
].out
);
115 todo_end
= (todo_end
+ 1) % ARRAY_SIZE(todo
);
117 pthread_cond_signal(&cond_add
);
121 static struct work_item
*get_work(void)
123 struct work_item
*ret
;
126 while (todo_start
== todo_end
&& !all_work_added
) {
127 pthread_cond_wait(&cond_add
, &grep_mutex
);
130 if (todo_start
== todo_end
&& all_work_added
) {
133 ret
= &todo
[todo_start
];
134 todo_start
= (todo_start
+ 1) % ARRAY_SIZE(todo
);
140 static void grep_sha1_async(struct grep_opt
*opt
, char *name
,
141 const unsigned char *sha1
)
146 add_work(WORK_SHA1
, name
, s
);
149 static void grep_file_async(struct grep_opt
*opt
, char *name
,
150 const char *filename
)
152 add_work(WORK_FILE
, name
, xstrdup(filename
));
155 static void work_done(struct work_item
*w
)
161 old_done
= todo_done
;
162 for(; todo
[todo_done
].done
&& todo_done
!= todo_start
;
163 todo_done
= (todo_done
+1) % ARRAY_SIZE(todo
)) {
164 w
= &todo
[todo_done
];
166 if (print_hunk_marks_between_files
&& printed_something
)
167 write_or_die(1, "--\n", 3);
168 write_or_die(1, w
->out
.buf
, w
->out
.len
);
169 printed_something
= 1;
175 if (old_done
!= todo_done
)
176 pthread_cond_signal(&cond_write
);
178 if (all_work_added
&& todo_done
== todo_end
)
179 pthread_cond_signal(&cond_result
);
184 static void *run(void *arg
)
187 struct grep_opt
*opt
= arg
;
190 struct work_item
*w
= get_work();
194 opt
->output_priv
= w
;
195 if (w
->type
== WORK_SHA1
) {
197 void* data
= load_sha1(w
->identifier
, &sz
, w
->name
);
200 hit
|= grep_buffer(opt
, w
->name
, data
, sz
);
203 } else if (w
->type
== WORK_FILE
) {
205 void* data
= load_file(w
->identifier
, &sz
);
207 hit
|= grep_buffer(opt
, w
->name
, data
, sz
);
216 free_grep_patterns(arg
);
219 return (void*) (intptr_t) hit
;
222 static void strbuf_out(struct grep_opt
*opt
, const void *buf
, size_t size
)
224 struct work_item
*w
= opt
->output_priv
;
225 strbuf_add(&w
->out
, buf
, size
);
228 static void start_threads(struct grep_opt
*opt
)
232 pthread_mutex_init(&grep_mutex
, NULL
);
233 pthread_mutex_init(&read_sha1_mutex
, NULL
);
234 pthread_cond_init(&cond_add
, NULL
);
235 pthread_cond_init(&cond_write
, NULL
);
236 pthread_cond_init(&cond_result
, NULL
);
238 for (i
= 0; i
< ARRAY_SIZE(todo
); i
++) {
239 strbuf_init(&todo
[i
].out
, 0);
242 for (i
= 0; i
< ARRAY_SIZE(threads
); i
++) {
244 struct grep_opt
*o
= grep_opt_dup(opt
);
245 o
->output
= strbuf_out
;
246 compile_grep_patterns(o
);
247 err
= pthread_create(&threads
[i
], NULL
, run
, o
);
250 die("grep: failed to create thread: %s",
255 static int wait_all(void)
263 /* Wait until all work is done. */
264 while (todo_done
!= todo_end
)
265 pthread_cond_wait(&cond_result
, &grep_mutex
);
267 /* Wake up all the consumer threads so they can see that there
268 * is no more work to do.
270 pthread_cond_broadcast(&cond_add
);
273 for (i
= 0; i
< ARRAY_SIZE(threads
); i
++) {
275 pthread_join(threads
[i
], &h
);
276 hit
|= (int) (intptr_t) h
;
279 pthread_mutex_destroy(&grep_mutex
);
280 pthread_mutex_destroy(&read_sha1_mutex
);
281 pthread_cond_destroy(&cond_add
);
282 pthread_cond_destroy(&cond_write
);
283 pthread_cond_destroy(&cond_result
);
287 #else /* !NO_PTHREADS */
288 #define read_sha1_lock()
289 #define read_sha1_unlock()
291 static int wait_all(void)
297 static int grep_config(const char *var
, const char *value
, void *cb
)
299 struct grep_opt
*opt
= cb
;
302 switch (userdiff_config(var
, value
)) {
308 if (!strcmp(var
, "color.grep"))
309 opt
->color
= git_config_colorbool(var
, value
, -1);
310 else if (!strcmp(var
, "color.grep.context"))
311 color
= opt
->color_context
;
312 else if (!strcmp(var
, "color.grep.filename"))
313 color
= opt
->color_filename
;
314 else if (!strcmp(var
, "color.grep.function"))
315 color
= opt
->color_function
;
316 else if (!strcmp(var
, "color.grep.linenumber"))
317 color
= opt
->color_lineno
;
318 else if (!strcmp(var
, "color.grep.match"))
319 color
= opt
->color_match
;
320 else if (!strcmp(var
, "color.grep.selected"))
321 color
= opt
->color_selected
;
322 else if (!strcmp(var
, "color.grep.separator"))
323 color
= opt
->color_sep
;
325 return git_color_default_config(var
, value
, cb
);
328 return config_error_nonbool(var
);
329 color_parse(value
, var
, color
);
335 * Return non-zero if max_depth is negative or path has no more then max_depth
338 static int accept_subdir(const char *path
, int max_depth
)
343 while ((path
= strchr(path
, '/')) != NULL
) {
353 * Return non-zero if name is a subdirectory of match and is not too deep.
355 static int is_subdir(const char *name
, int namelen
,
356 const char *match
, int matchlen
, int max_depth
)
358 if (matchlen
> namelen
|| strncmp(name
, match
, matchlen
))
361 if (name
[matchlen
] == '\0') /* exact match */
364 if (!matchlen
|| match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
365 return accept_subdir(name
+ matchlen
+ 1, max_depth
);
371 * git grep pathspecs are somewhat different from diff-tree pathspecs;
372 * pathname wildcards are allowed.
374 static int pathspec_matches(const char **paths
, const char *name
, int max_depth
)
377 if (!paths
|| !*paths
)
378 return accept_subdir(name
, max_depth
);
379 namelen
= strlen(name
);
380 for (i
= 0; paths
[i
]; i
++) {
381 const char *match
= paths
[i
];
382 int matchlen
= strlen(match
);
383 const char *cp
, *meta
;
385 if (is_subdir(name
, namelen
, match
, matchlen
, max_depth
))
387 if (!fnmatch(match
, name
, 0))
389 if (name
[namelen
-1] != '/')
392 /* We are being asked if the directory ("name") is worth
395 * Find the longest leading directory name that does
396 * not have metacharacter in the pathspec; the name
397 * we are looking at must overlap with that directory.
399 for (cp
= match
, meta
= NULL
; cp
- match
< matchlen
; cp
++) {
401 if (ch
== '*' || ch
== '[' || ch
== '?') {
407 meta
= cp
; /* fully literal */
409 if (namelen
<= meta
- match
) {
410 /* Looking at "Documentation/" and
411 * the pattern says "Documentation/howto/", or
412 * "Documentation/diff*.txt". The name we
413 * have should match prefix.
415 if (!memcmp(match
, name
, namelen
))
420 if (meta
- match
< namelen
) {
421 /* Looking at "Documentation/howto/" and
422 * the pattern says "Documentation/h*";
423 * match up to "Do.../h"; this avoids descending
424 * into "Documentation/technical/".
426 if (!memcmp(match
, name
, meta
- match
))
434 static void *lock_and_read_sha1_file(const unsigned char *sha1
, enum object_type
*type
, unsigned long *size
)
440 data
= read_sha1_file(sha1
, type
, size
);
443 data
= read_sha1_file(sha1
, type
, size
);
448 static void *load_sha1(const unsigned char *sha1
, unsigned long *size
,
451 enum object_type type
;
452 void *data
= lock_and_read_sha1_file(sha1
, &type
, size
);
455 error("'%s': unable to read %s", name
, sha1_to_hex(sha1
));
460 static int grep_sha1(struct grep_opt
*opt
, const unsigned char *sha1
,
461 const char *filename
, int tree_name_len
)
463 struct strbuf pathbuf
= STRBUF_INIT
;
466 if (opt
->relative
&& opt
->prefix_length
) {
467 quote_path_relative(filename
+ tree_name_len
, -1, &pathbuf
,
469 strbuf_insert(&pathbuf
, 0, filename
, tree_name_len
);
471 strbuf_addstr(&pathbuf
, filename
);
474 name
= strbuf_detach(&pathbuf
, NULL
);
478 grep_sha1_async(opt
, name
, sha1
);
485 void *data
= load_sha1(sha1
, &sz
, name
);
489 hit
= grep_buffer(opt
, name
, data
, sz
);
497 static void *load_file(const char *filename
, size_t *sz
)
503 if (lstat(filename
, &st
) < 0) {
506 error("'%s': %s", filename
, strerror(errno
));
509 if (!S_ISREG(st
.st_mode
))
511 *sz
= xsize_t(st
.st_size
);
512 i
= open(filename
, O_RDONLY
);
515 data
= xmalloc(*sz
+ 1);
516 if (st
.st_size
!= read_in_full(i
, data
, *sz
)) {
517 error("'%s': short read %s", filename
, strerror(errno
));
527 static int grep_file(struct grep_opt
*opt
, const char *filename
)
529 struct strbuf buf
= STRBUF_INIT
;
532 if (opt
->relative
&& opt
->prefix_length
)
533 quote_path_relative(filename
, -1, &buf
, opt
->prefix
);
535 strbuf_addstr(&buf
, filename
);
536 name
= strbuf_detach(&buf
, NULL
);
540 grep_file_async(opt
, name
, filename
);
547 void *data
= load_file(filename
, &sz
);
551 hit
= grep_buffer(opt
, name
, data
, sz
);
559 static int grep_cache(struct grep_opt
*opt
, const char **paths
, int cached
)
565 for (nr
= 0; nr
< active_nr
; nr
++) {
566 struct cache_entry
*ce
= active_cache
[nr
];
567 if (!S_ISREG(ce
->ce_mode
))
569 if (!pathspec_matches(paths
, ce
->name
, opt
->max_depth
))
572 * If CE_VALID is on, we assume worktree file and its cache entry
573 * are identical, even if worktree file has been modified, so use
574 * cache version instead
576 if (cached
|| (ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
)) {
579 hit
|= grep_sha1(opt
, ce
->sha1
, ce
->name
, 0);
582 hit
|= grep_file(opt
, ce
->name
);
586 } while (nr
< active_nr
&&
587 !strcmp(ce
->name
, active_cache
[nr
]->name
));
588 nr
--; /* compensate for loop control */
590 if (hit
&& opt
->status_only
)
596 static int grep_tree(struct grep_opt
*opt
, const char **paths
,
597 struct tree_desc
*tree
,
598 const char *tree_name
, const char *base
)
602 struct name_entry entry
;
604 int tn_len
= strlen(tree_name
);
605 struct strbuf pathbuf
;
607 strbuf_init(&pathbuf
, PATH_MAX
+ tn_len
);
610 strbuf_add(&pathbuf
, tree_name
, tn_len
);
611 strbuf_addch(&pathbuf
, ':');
612 tn_len
= pathbuf
.len
;
614 strbuf_addstr(&pathbuf
, base
);
617 while (tree_entry(tree
, &entry
)) {
618 int te_len
= tree_entry_len(entry
.path
, entry
.sha1
);
620 strbuf_add(&pathbuf
, entry
.path
, te_len
);
622 if (S_ISDIR(entry
.mode
))
623 /* Match "abc/" against pathspec to
624 * decide if we want to descend into "abc"
627 strbuf_addch(&pathbuf
, '/');
629 down
= pathbuf
.buf
+ tn_len
;
630 if (!pathspec_matches(paths
, down
, opt
->max_depth
))
632 else if (S_ISREG(entry
.mode
))
633 hit
|= grep_sha1(opt
, entry
.sha1
, pathbuf
.buf
, tn_len
);
634 else if (S_ISDIR(entry
.mode
)) {
635 enum object_type type
;
636 struct tree_desc sub
;
640 data
= lock_and_read_sha1_file(entry
.sha1
, &type
, &size
);
642 die("unable to read tree (%s)",
643 sha1_to_hex(entry
.sha1
));
644 init_tree_desc(&sub
, data
, size
);
645 hit
|= grep_tree(opt
, paths
, &sub
, tree_name
, down
);
648 if (hit
&& opt
->status_only
)
651 strbuf_release(&pathbuf
);
655 static int grep_object(struct grep_opt
*opt
, const char **paths
,
656 struct object
*obj
, const char *name
)
658 if (obj
->type
== OBJ_BLOB
)
659 return grep_sha1(opt
, obj
->sha1
, name
, 0);
660 if (obj
->type
== OBJ_COMMIT
|| obj
->type
== OBJ_TREE
) {
661 struct tree_desc tree
;
665 data
= read_object_with_reference(obj
->sha1
, tree_type
,
668 die("unable to read tree (%s)", sha1_to_hex(obj
->sha1
));
669 init_tree_desc(&tree
, data
, size
);
670 hit
= grep_tree(opt
, paths
, &tree
, name
, "");
674 die("unable to grep from object of type %s", typename(obj
->type
));
677 static int grep_objects(struct grep_opt
*opt
, const char **paths
,
678 const struct object_array
*list
)
682 const unsigned int nr
= list
->nr
;
684 for (i
= 0; i
< nr
; i
++) {
685 struct object
*real_obj
;
686 real_obj
= deref_tag(list
->objects
[i
].item
, NULL
, 0);
687 if (grep_object(opt
, paths
, real_obj
, list
->objects
[i
].name
)) {
689 if (opt
->status_only
)
696 static int grep_directory(struct grep_opt
*opt
, const char **paths
)
698 struct dir_struct dir
;
701 memset(&dir
, 0, sizeof(dir
));
702 setup_standard_excludes(&dir
);
704 fill_directory(&dir
, paths
);
705 for (i
= 0; i
< dir
.nr
; i
++) {
706 hit
|= grep_file(opt
, dir
.entries
[i
]->name
);
707 if (hit
&& opt
->status_only
)
713 static int context_callback(const struct option
*opt
, const char *arg
,
716 struct grep_opt
*grep_opt
= opt
->value
;
721 grep_opt
->pre_context
= grep_opt
->post_context
= 0;
724 value
= strtol(arg
, (char **)&endp
, 10);
726 return error("switch `%c' expects a numerical value",
729 grep_opt
->pre_context
= grep_opt
->post_context
= value
;
733 static int file_callback(const struct option
*opt
, const char *arg
, int unset
)
735 struct grep_opt
*grep_opt
= opt
->value
;
738 struct strbuf sb
= STRBUF_INIT
;
740 patterns
= fopen(arg
, "r");
742 die_errno("cannot open '%s'", arg
);
743 while (strbuf_getline(&sb
, patterns
, '\n') == 0) {
744 /* ignore empty line like grep does */
747 append_grep_pattern(grep_opt
, strbuf_detach(&sb
, NULL
), arg
,
748 ++lno
, GREP_PATTERN
);
755 static int not_callback(const struct option
*opt
, const char *arg
, int unset
)
757 struct grep_opt
*grep_opt
= opt
->value
;
758 append_grep_pattern(grep_opt
, "--not", "command line", 0, GREP_NOT
);
762 static int and_callback(const struct option
*opt
, const char *arg
, int unset
)
764 struct grep_opt
*grep_opt
= opt
->value
;
765 append_grep_pattern(grep_opt
, "--and", "command line", 0, GREP_AND
);
769 static int open_callback(const struct option
*opt
, const char *arg
, int unset
)
771 struct grep_opt
*grep_opt
= opt
->value
;
772 append_grep_pattern(grep_opt
, "(", "command line", 0, GREP_OPEN_PAREN
);
776 static int close_callback(const struct option
*opt
, const char *arg
, int unset
)
778 struct grep_opt
*grep_opt
= opt
->value
;
779 append_grep_pattern(grep_opt
, ")", "command line", 0, GREP_CLOSE_PAREN
);
783 static int pattern_callback(const struct option
*opt
, const char *arg
,
786 struct grep_opt
*grep_opt
= opt
->value
;
787 append_grep_pattern(grep_opt
, arg
, "-e option", 0, GREP_PATTERN
);
791 static int help_callback(const struct option
*opt
, const char *arg
, int unset
)
796 int cmd_grep(int argc
, const char **argv
, const char *prefix
)
800 int seen_dashdash
= 0;
801 int external_grep_allowed__ignored
;
803 struct object_array list
= { 0, 0, NULL
};
804 const char **paths
= NULL
;
807 int nongit
= 0, use_index
= 1;
808 struct option options
[] = {
809 OPT_BOOLEAN(0, "cached", &cached
,
810 "search in index instead of in the work tree"),
811 OPT_BOOLEAN(0, "index", &use_index
,
812 "--no-index finds in contents not managed by git"),
814 OPT_BOOLEAN('v', "invert-match", &opt
.invert
,
815 "show non-matching lines"),
816 OPT_BOOLEAN('i', "ignore-case", &opt
.ignore_case
,
817 "case insensitive matching"),
818 OPT_BOOLEAN('w', "word-regexp", &opt
.word_regexp
,
819 "match patterns only at word boundaries"),
820 OPT_SET_INT('a', "text", &opt
.binary
,
821 "process binary files as text", GREP_BINARY_TEXT
),
822 OPT_SET_INT('I', NULL
, &opt
.binary
,
823 "don't match patterns in binary files",
824 GREP_BINARY_NOMATCH
),
825 { OPTION_INTEGER
, 0, "max-depth", &opt
.max_depth
, "depth",
826 "descend at most <depth> levels", PARSE_OPT_NONEG
,
829 OPT_BIT('E', "extended-regexp", &opt
.regflags
,
830 "use extended POSIX regular expressions", REG_EXTENDED
),
831 OPT_NEGBIT('G', "basic-regexp", &opt
.regflags
,
832 "use basic POSIX regular expressions (default)",
834 OPT_BOOLEAN('F', "fixed-strings", &opt
.fixed
,
835 "interpret patterns as fixed strings"),
837 OPT_BOOLEAN('n', NULL
, &opt
.linenum
, "show line numbers"),
838 OPT_NEGBIT('h', NULL
, &opt
.pathname
, "don't show filenames", 1),
839 OPT_BIT('H', NULL
, &opt
.pathname
, "show filenames", 1),
840 OPT_NEGBIT(0, "full-name", &opt
.relative
,
841 "show filenames relative to top directory", 1),
842 OPT_BOOLEAN('l', "files-with-matches", &opt
.name_only
,
843 "show only filenames instead of matching lines"),
844 OPT_BOOLEAN(0, "name-only", &opt
.name_only
,
845 "synonym for --files-with-matches"),
846 OPT_BOOLEAN('L', "files-without-match",
847 &opt
.unmatch_name_only
,
848 "show only the names of files without match"),
849 OPT_BOOLEAN('z', "null", &opt
.null_following_name
,
850 "print NUL after filenames"),
851 OPT_BOOLEAN('c', "count", &opt
.count
,
852 "show the number of matches instead of matching lines"),
853 OPT__COLOR(&opt
.color
, "highlight matches"),
855 OPT_CALLBACK('C', NULL
, &opt
, "n",
856 "show <n> context lines before and after matches",
858 OPT_INTEGER('B', NULL
, &opt
.pre_context
,
859 "show <n> context lines before matches"),
860 OPT_INTEGER('A', NULL
, &opt
.post_context
,
861 "show <n> context lines after matches"),
862 OPT_NUMBER_CALLBACK(&opt
, "shortcut for -C NUM",
864 OPT_BOOLEAN('p', "show-function", &opt
.funcname
,
865 "show a line with the function name before matches"),
867 OPT_CALLBACK('f', NULL
, &opt
, "file",
868 "read patterns from file", file_callback
),
869 { OPTION_CALLBACK
, 'e', NULL
, &opt
, "pattern",
870 "match <pattern>", PARSE_OPT_NONEG
, pattern_callback
},
871 { OPTION_CALLBACK
, 0, "and", &opt
, NULL
,
872 "combine patterns specified with -e",
873 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, and_callback
},
874 OPT_BOOLEAN(0, "or", &dummy
, ""),
875 { OPTION_CALLBACK
, 0, "not", &opt
, NULL
, "",
876 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, not_callback
},
877 { OPTION_CALLBACK
, '(', NULL
, &opt
, NULL
, "",
878 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
880 { OPTION_CALLBACK
, ')', NULL
, &opt
, NULL
, "",
881 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
883 OPT_BOOLEAN('q', "quiet", &opt
.status_only
,
884 "indicate hit with exit status without output"),
885 OPT_BOOLEAN(0, "all-match", &opt
.all_match
,
886 "show only matches from files that match all patterns"),
888 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored
,
889 "allow calling of grep(1) (ignored by this build)"),
890 { OPTION_CALLBACK
, 0, "help-all", &options
, NULL
, "show usage",
891 PARSE_OPT_HIDDEN
| PARSE_OPT_NOARG
, help_callback
},
895 prefix
= setup_git_directory_gently(&nongit
);
898 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
899 * to show usage information and exit.
901 if (argc
== 2 && !strcmp(argv
[1], "-h"))
902 usage_with_options(grep_usage
, options
);
904 memset(&opt
, 0, sizeof(opt
));
906 opt
.prefix_length
= (prefix
&& *prefix
) ? strlen(prefix
) : 0;
909 opt
.pattern_tail
= &opt
.pattern_list
;
910 opt
.header_tail
= &opt
.header_list
;
911 opt
.regflags
= REG_NEWLINE
;
914 strcpy(opt
.color_context
, "");
915 strcpy(opt
.color_filename
, "");
916 strcpy(opt
.color_function
, "");
917 strcpy(opt
.color_lineno
, "");
918 strcpy(opt
.color_match
, GIT_COLOR_BOLD_RED
);
919 strcpy(opt
.color_selected
, "");
920 strcpy(opt
.color_sep
, GIT_COLOR_CYAN
);
922 git_config(grep_config
, &opt
);
924 opt
.color
= git_use_color_default
;
927 * If there is no -- then the paths must exist in the working
928 * tree. If there is no explicit pattern specified with -e or
929 * -f, we take the first unrecognized non option to be the
930 * pattern, but then what follows it must be zero or more
931 * valid refs up to the -- (if exists), and then existing
932 * paths. If there is an explicit pattern, then the first
933 * unrecognized non option is the beginning of the refs list
934 * that continues up to the -- (if exists), and then paths.
936 argc
= parse_options(argc
, argv
, prefix
, options
, grep_usage
,
937 PARSE_OPT_KEEP_DASHDASH
|
938 PARSE_OPT_STOP_AT_NON_OPTION
|
939 PARSE_OPT_NO_INTERNAL_HELP
);
941 if (use_index
&& nongit
)
942 /* die the same way as if we did it at the beginning */
943 setup_git_directory();
946 * skip a -- separator; we know it cannot be
947 * separating revisions from pathnames if
948 * we haven't even had any patterns yet
950 if (argc
> 0 && !opt
.pattern_list
&& !strcmp(argv
[0], "--")) {
955 /* First unrecognized non-option token */
956 if (argc
> 0 && !opt
.pattern_list
) {
957 append_grep_pattern(&opt
, argv
[0], "command line", 0,
963 if (!opt
.pattern_list
)
964 die("no pattern given.");
965 if (!opt
.fixed
&& opt
.ignore_case
)
966 opt
.regflags
|= REG_ICASE
;
967 if ((opt
.regflags
!= REG_NEWLINE
) && opt
.fixed
)
968 die("cannot mix --fixed-strings and regexp");
971 if (online_cpus() == 1 || !grep_threads_ok(&opt
))
975 if (opt
.pre_context
|| opt
.post_context
)
976 print_hunk_marks_between_files
= 1;
983 compile_grep_patterns(&opt
);
985 /* Check revs and then paths */
986 for (i
= 0; i
< argc
; i
++) {
987 const char *arg
= argv
[i
];
988 unsigned char sha1
[20];
990 if (!get_sha1(arg
, sha1
)) {
991 struct object
*object
= parse_object(sha1
);
993 die("bad object %s", arg
);
994 add_object_array(object
, arg
, &list
);
997 if (!strcmp(arg
, "--")) {
1004 /* The rest are paths */
1005 if (!seen_dashdash
) {
1007 for (j
= i
; j
< argc
; j
++)
1008 verify_filename(prefix
, argv
[j
]);
1012 paths
= get_pathspec(prefix
, argv
+ i
);
1014 paths
= xcalloc(2, sizeof(const char *));
1021 die("--cached cannot be used with --no-index.");
1023 die("--no-index cannot be used with revs.");
1024 hit
= grep_directory(&opt
, paths
);
1025 } else if (!list
.nr
) {
1029 hit
= grep_cache(&opt
, paths
, cached
);
1032 die("both --cached and trees are given.");
1033 hit
= grep_objects(&opt
, paths
, &list
);
1038 free_grep_patterns(&opt
);