4 * Copyright (c) 2006 Junio C Hamano
11 #include "tree-walk.h"
13 #include "parse-options.h"
14 #include "string-list.h"
15 #include "run-command.h"
21 #include "submodule.h"
23 static char const * const grep_usage
[] = {
24 N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
28 static const char *super_prefix
;
29 static int recurse_submodules
;
30 static struct argv_array submodule_options
= ARGV_ARRAY_INIT
;
32 static int grep_submodule_launch(struct grep_opt
*opt
,
33 const struct grep_source
*gs
);
35 #define GREP_NUM_THREADS_DEFAULT 8
36 static int num_threads
;
39 static pthread_t
*threads
;
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.
46 struct grep_source source
;
51 /* In the range [todo_done, todo_start) in 'todo' we have work_items
52 * that have been or are processed by a consumer thread. We haven't
53 * written the result for these to stdout yet.
55 * The work_items in [todo_start, todo_end) are waiting to be picked
56 * up by a consumer thread.
58 * The ranges are modulo TODO_SIZE.
61 static struct work_item todo
[TODO_SIZE
];
62 static int todo_start
;
66 /* Has all work items been added? */
67 static int all_work_added
;
69 /* This lock protects all the variables above. */
70 static pthread_mutex_t grep_mutex
;
72 static inline void grep_lock(void)
75 pthread_mutex_lock(&grep_mutex
);
78 static inline void grep_unlock(void)
81 pthread_mutex_unlock(&grep_mutex
);
84 /* Signalled when a new work_item is added to todo. */
85 static pthread_cond_t cond_add
;
87 /* Signalled when the result from one work_item is written to
90 static pthread_cond_t cond_write
;
92 /* Signalled when we are finished with everything. */
93 static pthread_cond_t cond_result
;
95 static int skip_first_line
;
97 static void add_work(struct grep_opt
*opt
, enum grep_source_type type
,
98 const char *name
, const char *path
, const void *id
)
102 while ((todo_end
+1) % ARRAY_SIZE(todo
) == todo_done
) {
103 pthread_cond_wait(&cond_write
, &grep_mutex
);
106 grep_source_init(&todo
[todo_end
].source
, type
, name
, path
, id
);
107 if (opt
->binary
!= GREP_BINARY_TEXT
)
108 grep_source_load_driver(&todo
[todo_end
].source
);
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 work_done(struct work_item
*w
)
142 old_done
= todo_done
;
143 for(; todo
[todo_done
].done
&& todo_done
!= todo_start
;
144 todo_done
= (todo_done
+1) % ARRAY_SIZE(todo
)) {
145 w
= &todo
[todo_done
];
147 const char *p
= w
->out
.buf
;
148 size_t len
= w
->out
.len
;
150 /* Skip the leading hunk mark of the first file. */
151 if (skip_first_line
) {
160 write_or_die(1, p
, len
);
162 grep_source_clear(&w
->source
);
165 if (old_done
!= todo_done
)
166 pthread_cond_signal(&cond_write
);
168 if (all_work_added
&& todo_done
== todo_end
)
169 pthread_cond_signal(&cond_result
);
174 static void *run(void *arg
)
177 struct grep_opt
*opt
= arg
;
180 struct work_item
*w
= get_work();
184 opt
->output_priv
= w
;
185 if (w
->source
.type
== GREP_SOURCE_SUBMODULE
)
186 hit
|= grep_submodule_launch(opt
, &w
->source
);
188 hit
|= grep_source(opt
, &w
->source
);
189 grep_source_clear_data(&w
->source
);
192 free_grep_patterns(arg
);
195 return (void*) (intptr_t) hit
;
198 static void strbuf_out(struct grep_opt
*opt
, const void *buf
, size_t size
)
200 struct work_item
*w
= opt
->output_priv
;
201 strbuf_add(&w
->out
, buf
, size
);
204 static void start_threads(struct grep_opt
*opt
)
208 pthread_mutex_init(&grep_mutex
, NULL
);
209 pthread_mutex_init(&grep_read_mutex
, NULL
);
210 pthread_mutex_init(&grep_attr_mutex
, NULL
);
211 pthread_cond_init(&cond_add
, NULL
);
212 pthread_cond_init(&cond_write
, NULL
);
213 pthread_cond_init(&cond_result
, NULL
);
216 for (i
= 0; i
< ARRAY_SIZE(todo
); i
++) {
217 strbuf_init(&todo
[i
].out
, 0);
220 threads
= xcalloc(num_threads
, sizeof(*threads
));
221 for (i
= 0; i
< num_threads
; i
++) {
223 struct grep_opt
*o
= grep_opt_dup(opt
);
224 o
->output
= strbuf_out
;
226 compile_grep_patterns(o
);
227 err
= pthread_create(&threads
[i
], NULL
, run
, o
);
230 die(_("grep: failed to create thread: %s"),
235 static int wait_all(void)
243 /* Wait until all work is done. */
244 while (todo_done
!= todo_end
)
245 pthread_cond_wait(&cond_result
, &grep_mutex
);
247 /* Wake up all the consumer threads so they can see that there
248 * is no more work to do.
250 pthread_cond_broadcast(&cond_add
);
253 for (i
= 0; i
< num_threads
; i
++) {
255 pthread_join(threads
[i
], &h
);
256 hit
|= (int) (intptr_t) h
;
261 pthread_mutex_destroy(&grep_mutex
);
262 pthread_mutex_destroy(&grep_read_mutex
);
263 pthread_mutex_destroy(&grep_attr_mutex
);
264 pthread_cond_destroy(&cond_add
);
265 pthread_cond_destroy(&cond_write
);
266 pthread_cond_destroy(&cond_result
);
271 #else /* !NO_PTHREADS */
273 static int wait_all(void)
279 static int grep_cmd_config(const char *var
, const char *value
, void *cb
)
281 int st
= grep_config(var
, value
, cb
);
282 if (git_color_default_config(var
, value
, cb
) < 0)
285 if (!strcmp(var
, "grep.threads")) {
286 num_threads
= git_config_int(var
, value
);
288 die(_("invalid number of threads specified (%d) for %s"),
295 static void *lock_and_read_sha1_file(const unsigned char *sha1
, enum object_type
*type
, unsigned long *size
)
300 data
= read_sha1_file(sha1
, type
, size
);
305 static int grep_sha1(struct grep_opt
*opt
, const unsigned char *sha1
,
306 const char *filename
, int tree_name_len
,
309 struct strbuf pathbuf
= STRBUF_INIT
;
311 if (opt
->relative
&& opt
->prefix_length
) {
312 quote_path_relative(filename
+ tree_name_len
, opt
->prefix
, &pathbuf
);
313 strbuf_insert(&pathbuf
, 0, filename
, tree_name_len
);
314 } else if (super_prefix
) {
315 strbuf_add(&pathbuf
, filename
, tree_name_len
);
316 strbuf_addstr(&pathbuf
, super_prefix
);
317 strbuf_addstr(&pathbuf
, filename
+ tree_name_len
);
319 strbuf_addstr(&pathbuf
, filename
);
324 add_work(opt
, GREP_SOURCE_SHA1
, pathbuf
.buf
, path
, sha1
);
325 strbuf_release(&pathbuf
);
330 struct grep_source gs
;
333 grep_source_init(&gs
, GREP_SOURCE_SHA1
, pathbuf
.buf
, path
, sha1
);
334 strbuf_release(&pathbuf
);
335 hit
= grep_source(opt
, &gs
);
337 grep_source_clear(&gs
);
342 static int grep_file(struct grep_opt
*opt
, const char *filename
)
344 struct strbuf buf
= STRBUF_INIT
;
346 if (opt
->relative
&& opt
->prefix_length
) {
347 quote_path_relative(filename
, opt
->prefix
, &buf
);
350 strbuf_addstr(&buf
, super_prefix
);
351 strbuf_addstr(&buf
, filename
);
356 add_work(opt
, GREP_SOURCE_FILE
, buf
.buf
, filename
, filename
);
357 strbuf_release(&buf
);
362 struct grep_source gs
;
365 grep_source_init(&gs
, GREP_SOURCE_FILE
, buf
.buf
, filename
, filename
);
366 strbuf_release(&buf
);
367 hit
= grep_source(opt
, &gs
);
369 grep_source_clear(&gs
);
374 static void append_path(struct grep_opt
*opt
, const void *data
, size_t len
)
376 struct string_list
*path_list
= opt
->output_priv
;
378 if (len
== 1 && *(const char *)data
== '\0')
380 string_list_append(path_list
, xstrndup(data
, len
));
383 static void run_pager(struct grep_opt
*opt
, const char *prefix
)
385 struct string_list
*path_list
= opt
->output_priv
;
386 struct child_process child
= CHILD_PROCESS_INIT
;
389 for (i
= 0; i
< path_list
->nr
; i
++)
390 argv_array_push(&child
.args
, path_list
->items
[i
].string
);
394 status
= run_command(&child
);
399 static void compile_submodule_options(const struct grep_opt
*opt
,
400 const struct pathspec
*pathspec
,
401 int cached
, int untracked
,
402 int opt_exclude
, int use_index
,
403 int pattern_type_arg
)
405 struct grep_pat
*pattern
;
408 if (recurse_submodules
)
409 argv_array_push(&submodule_options
, "--recurse-submodules");
412 argv_array_push(&submodule_options
, "--cached");
414 argv_array_push(&submodule_options
, "--no-index");
416 argv_array_push(&submodule_options
, "--untracked");
418 argv_array_push(&submodule_options
, "--exclude-standard");
421 argv_array_push(&submodule_options
, "-v");
422 if (opt
->ignore_case
)
423 argv_array_push(&submodule_options
, "-i");
424 if (opt
->word_regexp
)
425 argv_array_push(&submodule_options
, "-w");
426 switch (opt
->binary
) {
427 case GREP_BINARY_NOMATCH
:
428 argv_array_push(&submodule_options
, "-I");
430 case GREP_BINARY_TEXT
:
431 argv_array_push(&submodule_options
, "-a");
436 if (opt
->allow_textconv
)
437 argv_array_push(&submodule_options
, "--textconv");
438 if (opt
->max_depth
!= -1)
439 argv_array_pushf(&submodule_options
, "--max-depth=%d",
442 argv_array_push(&submodule_options
, "-n");
444 argv_array_push(&submodule_options
, "-h");
446 argv_array_push(&submodule_options
, "--full-name");
448 argv_array_push(&submodule_options
, "-l");
449 if (opt
->unmatch_name_only
)
450 argv_array_push(&submodule_options
, "-L");
451 if (opt
->null_following_name
)
452 argv_array_push(&submodule_options
, "-z");
454 argv_array_push(&submodule_options
, "-c");
456 argv_array_push(&submodule_options
, "--break");
458 argv_array_push(&submodule_options
, "--heading");
459 if (opt
->pre_context
)
460 argv_array_pushf(&submodule_options
, "--before-context=%d",
462 if (opt
->post_context
)
463 argv_array_pushf(&submodule_options
, "--after-context=%d",
466 argv_array_push(&submodule_options
, "-p");
468 argv_array_push(&submodule_options
, "-W");
470 argv_array_push(&submodule_options
, "--all-match");
472 argv_array_push(&submodule_options
, "--debug");
473 if (opt
->status_only
)
474 argv_array_push(&submodule_options
, "-q");
476 switch (pattern_type_arg
) {
477 case GREP_PATTERN_TYPE_BRE
:
478 argv_array_push(&submodule_options
, "-G");
480 case GREP_PATTERN_TYPE_ERE
:
481 argv_array_push(&submodule_options
, "-E");
483 case GREP_PATTERN_TYPE_FIXED
:
484 argv_array_push(&submodule_options
, "-F");
486 case GREP_PATTERN_TYPE_PCRE
:
487 argv_array_push(&submodule_options
, "-P");
489 case GREP_PATTERN_TYPE_UNSPECIFIED
:
493 for (pattern
= opt
->pattern_list
; pattern
!= NULL
;
494 pattern
= pattern
->next
) {
495 switch (pattern
->token
) {
497 argv_array_pushf(&submodule_options
, "-e%s",
501 case GREP_OPEN_PAREN
:
502 case GREP_CLOSE_PAREN
:
505 argv_array_push(&submodule_options
, pattern
->pattern
);
507 /* BODY and HEAD are not used by git-grep */
508 case GREP_PATTERN_BODY
:
509 case GREP_PATTERN_HEAD
:
515 * Limit number of threads for child process to use.
516 * This is to prevent potential fork-bomb behavior of git-grep as each
517 * submodule process has its own thread pool.
519 argv_array_pushf(&submodule_options
, "--threads=%d",
520 (num_threads
+ 1) / 2);
523 argv_array_push(&submodule_options
, "--");
524 for (i
= 0; i
< pathspec
->nr
; i
++)
525 argv_array_push(&submodule_options
,
526 pathspec
->items
[i
].original
);
530 * Launch child process to grep contents of a submodule
532 static int grep_submodule_launch(struct grep_opt
*opt
,
533 const struct grep_source
*gs
)
535 struct child_process cp
= CHILD_PROCESS_INIT
;
537 struct work_item
*w
= opt
->output_priv
;
539 prepare_submodule_repo_env(&cp
.env_array
);
541 /* Add super prefix */
542 argv_array_pushf(&cp
.args
, "--super-prefix=%s%s/",
543 super_prefix
? super_prefix
: "",
545 argv_array_push(&cp
.args
, "grep");
548 for (i
= 0; i
< submodule_options
.argc
; i
++)
549 argv_array_push(&cp
.args
, submodule_options
.argv
[i
]);
555 * Capture output to output buffer and check the return code from the
556 * child process. A '0' indicates a hit, a '1' indicates no hit and
557 * anything else is an error.
559 status
= capture_command(&cp
, &w
->out
, 0);
560 if (status
&& (status
!= 1)) {
561 /* flush the buffer */
562 write_or_die(1, w
->out
.buf
, w
->out
.len
);
563 die("process for submodule '%s' failed with exit code: %d",
567 /* invert the return code to make a hit equal to 1 */
572 * Prep grep structures for a submodule grep
573 * sha1: the sha1 of the submodule or NULL if using the working tree
574 * filename: name of the submodule including tree name of parent
575 * path: location of the submodule
577 static int grep_submodule(struct grep_opt
*opt
, const unsigned char *sha1
,
578 const char *filename
, const char *path
)
580 if (!is_submodule_initialized(path
))
582 if (!is_submodule_populated(path
))
587 add_work(opt
, GREP_SOURCE_SUBMODULE
, filename
, path
, sha1
);
595 grep_source_init(&w
.source
, GREP_SOURCE_SUBMODULE
,
596 filename
, path
, sha1
);
597 strbuf_init(&w
.out
, 0);
598 opt
->output_priv
= &w
;
599 hit
= grep_submodule_launch(opt
, &w
.source
);
601 write_or_die(1, w
.out
.buf
, w
.out
.len
);
603 grep_source_clear(&w
.source
);
604 strbuf_release(&w
.out
);
609 static int grep_cache(struct grep_opt
*opt
, const struct pathspec
*pathspec
,
614 struct strbuf name
= STRBUF_INIT
;
615 int name_base_len
= 0;
617 name_base_len
= strlen(super_prefix
);
618 strbuf_addstr(&name
, super_prefix
);
623 for (nr
= 0; nr
< active_nr
; nr
++) {
624 const struct cache_entry
*ce
= active_cache
[nr
];
625 strbuf_setlen(&name
, name_base_len
);
626 strbuf_addstr(&name
, ce
->name
);
628 if (S_ISREG(ce
->ce_mode
) &&
629 match_pathspec(pathspec
, name
.buf
, name
.len
, 0, NULL
,
630 S_ISDIR(ce
->ce_mode
) ||
631 S_ISGITLINK(ce
->ce_mode
))) {
633 * If CE_VALID is on, we assume worktree file and its
634 * cache entry are identical, even if worktree file has
635 * been modified, so use cache version instead
637 if (cached
|| (ce
->ce_flags
& CE_VALID
) ||
638 ce_skip_worktree(ce
)) {
639 if (ce_stage(ce
) || ce_intent_to_add(ce
))
641 hit
|= grep_sha1(opt
, ce
->oid
.hash
, ce
->name
,
644 hit
|= grep_file(opt
, ce
->name
);
646 } else if (recurse_submodules
&& S_ISGITLINK(ce
->ce_mode
) &&
647 submodule_path_match(pathspec
, name
.buf
, NULL
)) {
648 hit
|= grep_submodule(opt
, NULL
, ce
->name
, ce
->name
);
656 } while (nr
< active_nr
&&
657 !strcmp(ce
->name
, active_cache
[nr
]->name
));
658 nr
--; /* compensate for loop control */
660 if (hit
&& opt
->status_only
)
664 strbuf_release(&name
);
668 static int grep_tree(struct grep_opt
*opt
, const struct pathspec
*pathspec
,
669 struct tree_desc
*tree
, struct strbuf
*base
, int tn_len
,
673 enum interesting match
= entry_not_interesting
;
674 struct name_entry entry
;
675 int old_baselen
= base
->len
;
677 while (tree_entry(tree
, &entry
)) {
678 int te_len
= tree_entry_len(&entry
);
680 if (match
!= all_entries_interesting
) {
681 match
= tree_entry_interesting(&entry
, base
, tn_len
, pathspec
);
682 if (match
== all_entries_not_interesting
)
684 if (match
== entry_not_interesting
)
688 strbuf_add(base
, entry
.path
, te_len
);
690 if (S_ISREG(entry
.mode
)) {
691 hit
|= grep_sha1(opt
, entry
.oid
->hash
, base
->buf
, tn_len
,
692 check_attr
? base
->buf
+ tn_len
: NULL
);
694 else if (S_ISDIR(entry
.mode
)) {
695 enum object_type type
;
696 struct tree_desc sub
;
700 data
= lock_and_read_sha1_file(entry
.oid
->hash
, &type
, &size
);
702 die(_("unable to read tree (%s)"),
703 oid_to_hex(entry
.oid
));
705 strbuf_addch(base
, '/');
706 init_tree_desc(&sub
, data
, size
);
707 hit
|= grep_tree(opt
, pathspec
, &sub
, base
, tn_len
,
711 strbuf_setlen(base
, old_baselen
);
713 if (hit
&& opt
->status_only
)
719 static int grep_object(struct grep_opt
*opt
, const struct pathspec
*pathspec
,
720 struct object
*obj
, const char *name
, const char *path
)
722 if (obj
->type
== OBJ_BLOB
)
723 return grep_sha1(opt
, obj
->oid
.hash
, name
, 0, path
);
724 if (obj
->type
== OBJ_COMMIT
|| obj
->type
== OBJ_TREE
) {
725 struct tree_desc tree
;
732 data
= read_object_with_reference(obj
->oid
.hash
, tree_type
,
737 die(_("unable to read tree (%s)"), oid_to_hex(&obj
->oid
));
739 len
= name
? strlen(name
) : 0;
740 strbuf_init(&base
, PATH_MAX
+ len
+ 1);
742 strbuf_add(&base
, name
, len
);
743 strbuf_addch(&base
, ':');
745 init_tree_desc(&tree
, data
, size
);
746 hit
= grep_tree(opt
, pathspec
, &tree
, &base
, base
.len
,
747 obj
->type
== OBJ_COMMIT
);
748 strbuf_release(&base
);
752 die(_("unable to grep from object of type %s"), typename(obj
->type
));
755 static int grep_objects(struct grep_opt
*opt
, const struct pathspec
*pathspec
,
756 const struct object_array
*list
)
760 const unsigned int nr
= list
->nr
;
762 for (i
= 0; i
< nr
; i
++) {
763 struct object
*real_obj
;
764 real_obj
= deref_tag(list
->objects
[i
].item
, NULL
, 0);
765 if (grep_object(opt
, pathspec
, real_obj
, list
->objects
[i
].name
, list
->objects
[i
].path
)) {
767 if (opt
->status_only
)
774 static int grep_directory(struct grep_opt
*opt
, const struct pathspec
*pathspec
,
775 int exc_std
, int use_index
)
777 struct dir_struct dir
;
780 memset(&dir
, 0, sizeof(dir
));
782 dir
.flags
|= DIR_NO_GITLINKS
;
784 setup_standard_excludes(&dir
);
786 fill_directory(&dir
, pathspec
);
787 for (i
= 0; i
< dir
.nr
; i
++) {
788 if (!dir_path_match(dir
.entries
[i
], pathspec
, 0, NULL
))
790 hit
|= grep_file(opt
, dir
.entries
[i
]->name
);
791 if (hit
&& opt
->status_only
)
797 static int context_callback(const struct option
*opt
, const char *arg
,
800 struct grep_opt
*grep_opt
= opt
->value
;
805 grep_opt
->pre_context
= grep_opt
->post_context
= 0;
808 value
= strtol(arg
, (char **)&endp
, 10);
810 return error(_("switch `%c' expects a numerical value"),
813 grep_opt
->pre_context
= grep_opt
->post_context
= value
;
817 static int file_callback(const struct option
*opt
, const char *arg
, int unset
)
819 struct grep_opt
*grep_opt
= opt
->value
;
820 int from_stdin
= !strcmp(arg
, "-");
823 struct strbuf sb
= STRBUF_INIT
;
825 patterns
= from_stdin
? stdin
: fopen(arg
, "r");
827 die_errno(_("cannot open '%s'"), arg
);
828 while (strbuf_getline(&sb
, patterns
) == 0) {
829 /* ignore empty line like grep does */
833 append_grep_pat(grep_opt
, sb
.buf
, sb
.len
, arg
, ++lno
,
842 static int not_callback(const struct option
*opt
, const char *arg
, int unset
)
844 struct grep_opt
*grep_opt
= opt
->value
;
845 append_grep_pattern(grep_opt
, "--not", "command line", 0, GREP_NOT
);
849 static int and_callback(const struct option
*opt
, const char *arg
, int unset
)
851 struct grep_opt
*grep_opt
= opt
->value
;
852 append_grep_pattern(grep_opt
, "--and", "command line", 0, GREP_AND
);
856 static int open_callback(const struct option
*opt
, const char *arg
, int unset
)
858 struct grep_opt
*grep_opt
= opt
->value
;
859 append_grep_pattern(grep_opt
, "(", "command line", 0, GREP_OPEN_PAREN
);
863 static int close_callback(const struct option
*opt
, const char *arg
, int unset
)
865 struct grep_opt
*grep_opt
= opt
->value
;
866 append_grep_pattern(grep_opt
, ")", "command line", 0, GREP_CLOSE_PAREN
);
870 static int pattern_callback(const struct option
*opt
, const char *arg
,
873 struct grep_opt
*grep_opt
= opt
->value
;
874 append_grep_pattern(grep_opt
, arg
, "-e option", 0, GREP_PATTERN
);
878 int cmd_grep(int argc
, const char **argv
, const char *prefix
)
881 int cached
= 0, untracked
= 0, opt_exclude
= -1;
882 int seen_dashdash
= 0;
883 int external_grep_allowed__ignored
;
884 const char *show_in_pager
= NULL
, *default_pager
= "dummy";
886 struct object_array list
= OBJECT_ARRAY_INIT
;
887 struct pathspec pathspec
;
888 struct string_list path_list
= STRING_LIST_INIT_NODUP
;
892 int pattern_type_arg
= GREP_PATTERN_TYPE_UNSPECIFIED
;
894 struct option options
[] = {
895 OPT_BOOL(0, "cached", &cached
,
896 N_("search in index instead of in the work tree")),
897 OPT_NEGBIT(0, "no-index", &use_index
,
898 N_("find in contents not managed by git"), 1),
899 OPT_BOOL(0, "untracked", &untracked
,
900 N_("search in both tracked and untracked files")),
901 OPT_SET_INT(0, "exclude-standard", &opt_exclude
,
902 N_("ignore files specified via '.gitignore'"), 1),
903 OPT_BOOL(0, "recurse-submodules", &recurse_submodules
,
904 N_("recursivley search in each submodule")),
906 OPT_BOOL('v', "invert-match", &opt
.invert
,
907 N_("show non-matching lines")),
908 OPT_BOOL('i', "ignore-case", &opt
.ignore_case
,
909 N_("case insensitive matching")),
910 OPT_BOOL('w', "word-regexp", &opt
.word_regexp
,
911 N_("match patterns only at word boundaries")),
912 OPT_SET_INT('a', "text", &opt
.binary
,
913 N_("process binary files as text"), GREP_BINARY_TEXT
),
914 OPT_SET_INT('I', NULL
, &opt
.binary
,
915 N_("don't match patterns in binary files"),
916 GREP_BINARY_NOMATCH
),
917 OPT_BOOL(0, "textconv", &opt
.allow_textconv
,
918 N_("process binary files with textconv filters")),
919 { OPTION_INTEGER
, 0, "max-depth", &opt
.max_depth
, N_("depth"),
920 N_("descend at most <depth> levels"), PARSE_OPT_NONEG
,
923 OPT_SET_INT('E', "extended-regexp", &pattern_type_arg
,
924 N_("use extended POSIX regular expressions"),
925 GREP_PATTERN_TYPE_ERE
),
926 OPT_SET_INT('G', "basic-regexp", &pattern_type_arg
,
927 N_("use basic POSIX regular expressions (default)"),
928 GREP_PATTERN_TYPE_BRE
),
929 OPT_SET_INT('F', "fixed-strings", &pattern_type_arg
,
930 N_("interpret patterns as fixed strings"),
931 GREP_PATTERN_TYPE_FIXED
),
932 OPT_SET_INT('P', "perl-regexp", &pattern_type_arg
,
933 N_("use Perl-compatible regular expressions"),
934 GREP_PATTERN_TYPE_PCRE
),
936 OPT_BOOL('n', "line-number", &opt
.linenum
, N_("show line numbers")),
937 OPT_NEGBIT('h', NULL
, &opt
.pathname
, N_("don't show filenames"), 1),
938 OPT_BIT('H', NULL
, &opt
.pathname
, N_("show filenames"), 1),
939 OPT_NEGBIT(0, "full-name", &opt
.relative
,
940 N_("show filenames relative to top directory"), 1),
941 OPT_BOOL('l', "files-with-matches", &opt
.name_only
,
942 N_("show only filenames instead of matching lines")),
943 OPT_BOOL(0, "name-only", &opt
.name_only
,
944 N_("synonym for --files-with-matches")),
945 OPT_BOOL('L', "files-without-match",
946 &opt
.unmatch_name_only
,
947 N_("show only the names of files without match")),
948 OPT_BOOL('z', "null", &opt
.null_following_name
,
949 N_("print NUL after filenames")),
950 OPT_BOOL('c', "count", &opt
.count
,
951 N_("show the number of matches instead of matching lines")),
952 OPT__COLOR(&opt
.color
, N_("highlight matches")),
953 OPT_BOOL(0, "break", &opt
.file_break
,
954 N_("print empty line between matches from different files")),
955 OPT_BOOL(0, "heading", &opt
.heading
,
956 N_("show filename only once above matches from same file")),
958 OPT_CALLBACK('C', "context", &opt
, N_("n"),
959 N_("show <n> context lines before and after matches"),
961 OPT_INTEGER('B', "before-context", &opt
.pre_context
,
962 N_("show <n> context lines before matches")),
963 OPT_INTEGER('A', "after-context", &opt
.post_context
,
964 N_("show <n> context lines after matches")),
965 OPT_INTEGER(0, "threads", &num_threads
,
966 N_("use <n> worker threads")),
967 OPT_NUMBER_CALLBACK(&opt
, N_("shortcut for -C NUM"),
969 OPT_BOOL('p', "show-function", &opt
.funcname
,
970 N_("show a line with the function name before matches")),
971 OPT_BOOL('W', "function-context", &opt
.funcbody
,
972 N_("show the surrounding function")),
974 OPT_CALLBACK('f', NULL
, &opt
, N_("file"),
975 N_("read patterns from file"), file_callback
),
976 { OPTION_CALLBACK
, 'e', NULL
, &opt
, N_("pattern"),
977 N_("match <pattern>"), PARSE_OPT_NONEG
, pattern_callback
},
978 { OPTION_CALLBACK
, 0, "and", &opt
, NULL
,
979 N_("combine patterns specified with -e"),
980 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, and_callback
},
981 OPT_BOOL(0, "or", &dummy
, ""),
982 { OPTION_CALLBACK
, 0, "not", &opt
, NULL
, "",
983 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, not_callback
},
984 { OPTION_CALLBACK
, '(', NULL
, &opt
, NULL
, "",
985 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
987 { OPTION_CALLBACK
, ')', NULL
, &opt
, NULL
, "",
988 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
,
990 OPT__QUIET(&opt
.status_only
,
991 N_("indicate hit with exit status without output")),
992 OPT_BOOL(0, "all-match", &opt
.all_match
,
993 N_("show only matches from files that match all patterns")),
994 { OPTION_SET_INT
, 0, "debug", &opt
.debug
, NULL
,
995 N_("show parse tree for grep expression"),
996 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
, NULL
, 1 },
998 { OPTION_STRING
, 'O', "open-files-in-pager", &show_in_pager
,
999 N_("pager"), N_("show matching files in the pager"),
1000 PARSE_OPT_OPTARG
, NULL
, (intptr_t)default_pager
},
1001 OPT_BOOL(0, "ext-grep", &external_grep_allowed__ignored
,
1002 N_("allow calling of grep(1) (ignored by this build)")),
1006 init_grep_defaults();
1007 git_config(grep_cmd_config
, NULL
);
1008 grep_init(&opt
, prefix
);
1009 super_prefix
= get_super_prefix();
1012 * If there is no -- then the paths must exist in the working
1013 * tree. If there is no explicit pattern specified with -e or
1014 * -f, we take the first unrecognized non option to be the
1015 * pattern, but then what follows it must be zero or more
1016 * valid refs up to the -- (if exists), and then existing
1017 * paths. If there is an explicit pattern, then the first
1018 * unrecognized non option is the beginning of the refs list
1019 * that continues up to the -- (if exists), and then paths.
1021 argc
= parse_options(argc
, argv
, prefix
, options
, grep_usage
,
1022 PARSE_OPT_KEEP_DASHDASH
|
1023 PARSE_OPT_STOP_AT_NON_OPTION
);
1024 grep_commit_pattern_type(pattern_type_arg
, &opt
);
1026 if (use_index
&& !startup_info
->have_repository
) {
1028 git_config_get_bool("grep.fallbacktonoindex", &fallback
);
1032 /* die the same way as if we did it at the beginning */
1033 setup_git_directory();
1037 * skip a -- separator; we know it cannot be
1038 * separating revisions from pathnames if
1039 * we haven't even had any patterns yet
1041 if (argc
> 0 && !opt
.pattern_list
&& !strcmp(argv
[0], "--")) {
1046 /* First unrecognized non-option token */
1047 if (argc
> 0 && !opt
.pattern_list
) {
1048 append_grep_pattern(&opt
, argv
[0], "command line", 0,
1054 if (show_in_pager
== default_pager
)
1055 show_in_pager
= git_pager(1);
1056 if (show_in_pager
) {
1059 opt
.null_following_name
= 1;
1060 opt
.output_priv
= &path_list
;
1061 opt
.output
= append_path
;
1062 string_list_append(&path_list
, show_in_pager
);
1065 if (!opt
.pattern_list
)
1066 die(_("no pattern given."));
1067 if (!opt
.fixed
&& opt
.ignore_case
)
1068 opt
.regflags
|= REG_ICASE
;
1070 compile_grep_patterns(&opt
);
1072 /* Check revs and then paths */
1073 for (i
= 0; i
< argc
; i
++) {
1074 const char *arg
= argv
[i
];
1075 unsigned char sha1
[20];
1076 struct object_context oc
;
1078 if (!get_sha1_with_context(arg
, 0, sha1
, &oc
)) {
1079 struct object
*object
= parse_object_or_die(sha1
, arg
);
1081 verify_non_filename(prefix
, arg
);
1082 add_object_array_with_path(object
, arg
, &list
, oc
.mode
, oc
.path
);
1085 if (!strcmp(arg
, "--")) {
1093 if (list
.nr
|| cached
|| show_in_pager
)
1095 else if (num_threads
== 0)
1096 num_threads
= GREP_NUM_THREADS_DEFAULT
;
1097 else if (num_threads
< 0)
1098 die(_("invalid number of threads specified (%d)"), num_threads
);
1105 if (!(opt
.name_only
|| opt
.unmatch_name_only
|| opt
.count
)
1106 && (opt
.pre_context
|| opt
.post_context
||
1107 opt
.file_break
|| opt
.funcbody
))
1108 skip_first_line
= 1;
1109 start_threads(&opt
);
1113 /* The rest are paths */
1114 if (!seen_dashdash
) {
1116 for (j
= i
; j
< argc
; j
++)
1117 verify_filename(prefix
, argv
[j
], j
== i
);
1120 parse_pathspec(&pathspec
, 0,
1121 PATHSPEC_PREFER_CWD
|
1122 (opt
.max_depth
!= -1 ? PATHSPEC_MAXDEPTH_VALID
: 0),
1124 pathspec
.max_depth
= opt
.max_depth
;
1125 pathspec
.recursive
= 1;
1127 if (recurse_submodules
) {
1128 gitmodules_config();
1129 compile_submodule_options(&opt
, &pathspec
, cached
, untracked
,
1130 opt_exclude
, use_index
,
1134 if (show_in_pager
&& (cached
|| list
.nr
))
1135 die(_("--open-files-in-pager only works on the worktree"));
1137 if (show_in_pager
&& opt
.pattern_list
&& !opt
.pattern_list
->next
) {
1138 const char *pager
= path_list
.items
[0].string
;
1139 int len
= strlen(pager
);
1141 if (len
> 4 && is_dir_sep(pager
[len
- 5]))
1144 if (opt
.ignore_case
&& !strcmp("less", pager
))
1145 string_list_append(&path_list
, "-I");
1147 if (!strcmp("less", pager
) || !strcmp("vi", pager
)) {
1148 struct strbuf buf
= STRBUF_INIT
;
1149 strbuf_addf(&buf
, "+/%s%s",
1150 strcmp("less", pager
) ? "" : "*",
1151 opt
.pattern_list
->pattern
);
1152 string_list_append(&path_list
, buf
.buf
);
1153 strbuf_detach(&buf
, NULL
);
1157 if (recurse_submodules
&& (!use_index
|| untracked
|| list
.nr
))
1158 die(_("option not supported with --recurse-submodules."));
1160 if (!show_in_pager
&& !opt
.status_only
)
1163 if (!use_index
&& (untracked
|| cached
))
1164 die(_("--cached or --untracked cannot be used with --no-index."));
1166 if (!use_index
|| untracked
) {
1167 int use_exclude
= (opt_exclude
< 0) ? use_index
: !!opt_exclude
;
1169 die(_("--no-index or --untracked cannot be used with revs."));
1170 hit
= grep_directory(&opt
, &pathspec
, use_exclude
, use_index
);
1171 } else if (0 <= opt_exclude
) {
1172 die(_("--[no-]exclude-standard cannot be used for tracked contents."));
1173 } else if (!list
.nr
) {
1177 hit
= grep_cache(&opt
, &pathspec
, cached
);
1180 die(_("both --cached and trees are given."));
1181 hit
= grep_objects(&opt
, &pathspec
, &list
);
1186 if (hit
&& show_in_pager
)
1187 run_pager(&opt
, prefix
);
1188 free_grep_patterns(&opt
);