grep -I: do not bother to read known-binary files
[git/dscho.git] / builtin / grep.c
blob3589067dcc5dd3a7c52ea86c0fc2d9bf1bd01db9
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 "parse-options.h"
14 #include "string-list.h"
15 #include "run-command.h"
16 #include "userdiff.h"
17 #include "grep.h"
18 #include "quote.h"
19 #include "dir.h"
20 #include "attr.h"
22 static char const * const grep_usage[] = {
23 "git grep [options] [-e] <pattern> [<rev>...] [[--] <path>...]",
24 NULL
27 static int use_threads = 1;
29 #ifndef NO_PTHREADS
30 #define THREADS 8
31 static pthread_t threads[THREADS];
33 static void *load_sha1(const unsigned char *sha1, unsigned long *size,
34 const char *name);
35 static void *load_file(const char *filename, size_t *sz);
37 enum work_type {WORK_SHA1, WORK_FILE};
39 /* We use one producer thread and THREADS consumer
40 * threads. The producer adds struct work_items to 'todo' and the
41 * consumers pick work items from the same array.
43 struct work_item {
44 enum work_type type;
45 char *name;
47 /* if type == WORK_SHA1, then 'identifier' is a SHA1,
48 * otherwise type == WORK_FILE, and 'identifier' is a NUL
49 * terminated filename.
51 void *identifier;
52 char done;
53 struct strbuf out;
56 /* In the range [todo_done, todo_start) in 'todo' we have work_items
57 * that have been or are processed by a consumer thread. We haven't
58 * written the result for these to stdout yet.
60 * The work_items in [todo_start, todo_end) are waiting to be picked
61 * up by a consumer thread.
63 * The ranges are modulo TODO_SIZE.
65 #define TODO_SIZE 128
66 static struct work_item todo[TODO_SIZE];
67 static int todo_start;
68 static int todo_end;
69 static int todo_done;
71 /* Has all work items been added? */
72 static int all_work_added;
74 /* This lock protects all the variables above. */
75 static pthread_mutex_t grep_mutex;
77 static inline void grep_lock(void)
79 if (use_threads)
80 pthread_mutex_lock(&grep_mutex);
83 static inline void grep_unlock(void)
85 if (use_threads)
86 pthread_mutex_unlock(&grep_mutex);
89 /* Used to serialize calls to read_sha1_file. */
90 static pthread_mutex_t read_sha1_mutex;
92 static inline void read_sha1_lock(void)
94 if (use_threads)
95 pthread_mutex_lock(&read_sha1_mutex);
98 static inline void read_sha1_unlock(void)
100 if (use_threads)
101 pthread_mutex_unlock(&read_sha1_mutex);
104 /* Signalled when a new work_item is added to todo. */
105 static pthread_cond_t cond_add;
107 /* Signalled when the result from one work_item is written to
108 * stdout.
110 static pthread_cond_t cond_write;
112 /* Signalled when we are finished with everything. */
113 static pthread_cond_t cond_result;
115 static int skip_first_line;
117 static void add_work(enum work_type type, char *name, void *id)
119 grep_lock();
121 while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) {
122 pthread_cond_wait(&cond_write, &grep_mutex);
125 todo[todo_end].type = type;
126 todo[todo_end].name = name;
127 todo[todo_end].identifier = id;
128 todo[todo_end].done = 0;
129 strbuf_reset(&todo[todo_end].out);
130 todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
132 pthread_cond_signal(&cond_add);
133 grep_unlock();
136 static struct work_item *get_work(void)
138 struct work_item *ret;
140 grep_lock();
141 while (todo_start == todo_end && !all_work_added) {
142 pthread_cond_wait(&cond_add, &grep_mutex);
145 if (todo_start == todo_end && all_work_added) {
146 ret = NULL;
147 } else {
148 ret = &todo[todo_start];
149 todo_start = (todo_start + 1) % ARRAY_SIZE(todo);
151 grep_unlock();
152 return ret;
155 static void grep_sha1_async(struct grep_opt *opt, char *name,
156 const unsigned char *sha1)
158 unsigned char *s;
159 s = xmalloc(20);
160 memcpy(s, sha1, 20);
161 add_work(WORK_SHA1, name, s);
164 static void grep_file_async(struct grep_opt *opt, char *name,
165 const char *filename)
167 add_work(WORK_FILE, name, xstrdup(filename));
170 static void work_done(struct work_item *w)
172 int old_done;
174 grep_lock();
175 w->done = 1;
176 old_done = todo_done;
177 for(; todo[todo_done].done && todo_done != todo_start;
178 todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
179 w = &todo[todo_done];
180 if (w->out.len) {
181 const char *p = w->out.buf;
182 size_t len = w->out.len;
184 /* Skip the leading hunk mark of the first file. */
185 if (skip_first_line) {
186 while (len) {
187 len--;
188 if (*p++ == '\n')
189 break;
191 skip_first_line = 0;
194 write_or_die(1, p, len);
196 free(w->name);
197 free(w->identifier);
200 if (old_done != todo_done)
201 pthread_cond_signal(&cond_write);
203 if (all_work_added && todo_done == todo_end)
204 pthread_cond_signal(&cond_result);
206 grep_unlock();
209 static int skip_binary(struct grep_opt *opt, const char *filename)
211 if ((opt->binary & GREP_BINARY_NOMATCH)) {
212 static struct git_attr *attr_text;
213 struct git_attr_check check;
215 if (!attr_text)
216 attr_text = git_attr("text");
217 memset(&check, 0, sizeof(check));
218 check.attr = attr_text;
219 return !git_check_attr(filename, 1, &check) &&
220 ATTR_FALSE(check.value);
222 return 0;
225 static void *run(void *arg)
227 int hit = 0;
228 struct grep_opt *opt = arg;
230 while (1) {
231 struct work_item *w = get_work();
232 if (!w)
233 break;
235 if (skip_binary(opt, (const char *)w->identifier))
236 continue;
238 opt->output_priv = w;
239 if (w->type == WORK_SHA1) {
240 unsigned long sz;
241 void* data = load_sha1(w->identifier, &sz, w->name);
243 if (data) {
244 hit |= grep_buffer(opt, w->name, data, sz);
245 free(data);
247 } else if (w->type == WORK_FILE) {
248 size_t sz;
249 void* data = load_file(w->identifier, &sz);
250 if (data) {
251 hit |= grep_buffer(opt, w->name, data, sz);
252 free(data);
254 } else {
255 assert(0);
258 work_done(w);
260 free_grep_patterns(arg);
261 free(arg);
263 return (void*) (intptr_t) hit;
266 static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size)
268 struct work_item *w = opt->output_priv;
269 strbuf_add(&w->out, buf, size);
272 static void start_threads(struct grep_opt *opt)
274 int i;
276 pthread_mutex_init(&grep_mutex, NULL);
277 pthread_mutex_init(&read_sha1_mutex, NULL);
278 pthread_mutex_init(&grep_attr_mutex, NULL);
279 pthread_cond_init(&cond_add, NULL);
280 pthread_cond_init(&cond_write, NULL);
281 pthread_cond_init(&cond_result, NULL);
283 for (i = 0; i < ARRAY_SIZE(todo); i++) {
284 strbuf_init(&todo[i].out, 0);
287 for (i = 0; i < ARRAY_SIZE(threads); i++) {
288 int err;
289 struct grep_opt *o = grep_opt_dup(opt);
290 o->output = strbuf_out;
291 compile_grep_patterns(o);
292 err = pthread_create(&threads[i], NULL, run, o);
294 if (err)
295 die(_("grep: failed to create thread: %s"),
296 strerror(err));
300 static int wait_all(void)
302 int hit = 0;
303 int i;
305 grep_lock();
306 all_work_added = 1;
308 /* Wait until all work is done. */
309 while (todo_done != todo_end)
310 pthread_cond_wait(&cond_result, &grep_mutex);
312 /* Wake up all the consumer threads so they can see that there
313 * is no more work to do.
315 pthread_cond_broadcast(&cond_add);
316 grep_unlock();
318 for (i = 0; i < ARRAY_SIZE(threads); i++) {
319 void *h;
320 pthread_join(threads[i], &h);
321 hit |= (int) (intptr_t) h;
324 pthread_mutex_destroy(&grep_mutex);
325 pthread_mutex_destroy(&read_sha1_mutex);
326 pthread_mutex_destroy(&grep_attr_mutex);
327 pthread_cond_destroy(&cond_add);
328 pthread_cond_destroy(&cond_write);
329 pthread_cond_destroy(&cond_result);
331 return hit;
333 #else /* !NO_PTHREADS */
334 #define read_sha1_lock()
335 #define read_sha1_unlock()
337 static int wait_all(void)
339 return 0;
341 #endif
343 static int grep_config(const char *var, const char *value, void *cb)
345 struct grep_opt *opt = cb;
346 char *color = NULL;
348 switch (userdiff_config(var, value)) {
349 case 0: break;
350 case -1: return -1;
351 default: return 0;
354 if (!strcmp(var, "grep.extendedregexp")) {
355 if (git_config_bool(var, value))
356 opt->regflags |= REG_EXTENDED;
357 else
358 opt->regflags &= ~REG_EXTENDED;
359 return 0;
362 if (!strcmp(var, "grep.linenumber")) {
363 opt->linenum = git_config_bool(var, value);
364 return 0;
367 if (!strcmp(var, "color.grep"))
368 opt->color = git_config_colorbool(var, value);
369 else if (!strcmp(var, "color.grep.context"))
370 color = opt->color_context;
371 else if (!strcmp(var, "color.grep.filename"))
372 color = opt->color_filename;
373 else if (!strcmp(var, "color.grep.function"))
374 color = opt->color_function;
375 else if (!strcmp(var, "color.grep.linenumber"))
376 color = opt->color_lineno;
377 else if (!strcmp(var, "color.grep.match"))
378 color = opt->color_match;
379 else if (!strcmp(var, "color.grep.selected"))
380 color = opt->color_selected;
381 else if (!strcmp(var, "color.grep.separator"))
382 color = opt->color_sep;
383 else
384 return git_color_default_config(var, value, cb);
385 if (color) {
386 if (!value)
387 return config_error_nonbool(var);
388 color_parse(value, var, color);
390 return 0;
393 static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
395 void *data;
397 read_sha1_lock();
398 data = read_sha1_file(sha1, type, size);
399 read_sha1_unlock();
400 return data;
403 static void *load_sha1(const unsigned char *sha1, unsigned long *size,
404 const char *name)
406 enum object_type type;
407 void *data = lock_and_read_sha1_file(sha1, &type, size);
409 if (!data)
410 error(_("'%s': unable to read %s"), name, sha1_to_hex(sha1));
412 return data;
415 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
416 const char *filename, int tree_name_len)
418 struct strbuf pathbuf = STRBUF_INIT;
419 char *name;
421 if (opt->relative && opt->prefix_length) {
422 quote_path_relative(filename + tree_name_len, -1, &pathbuf,
423 opt->prefix);
424 strbuf_insert(&pathbuf, 0, filename, tree_name_len);
425 } else {
426 strbuf_addstr(&pathbuf, filename);
429 name = strbuf_detach(&pathbuf, NULL);
431 #ifndef NO_PTHREADS
432 if (use_threads) {
433 grep_sha1_async(opt, name, sha1);
434 return 0;
435 } else
436 #endif
438 int hit;
439 unsigned long sz;
440 void *data = load_sha1(sha1, &sz, name);
441 if (!data)
442 hit = 0;
443 else
444 hit = grep_buffer(opt, name, data, sz);
446 free(data);
447 free(name);
448 return hit;
452 static void *load_file(const char *filename, size_t *sz)
454 struct stat st;
455 char *data;
456 int i;
458 if (lstat(filename, &st) < 0) {
459 err_ret:
460 if (errno != ENOENT)
461 error(_("'%s': %s"), filename, strerror(errno));
462 return NULL;
464 if (!S_ISREG(st.st_mode))
465 return NULL;
466 *sz = xsize_t(st.st_size);
467 i = open(filename, O_RDONLY);
468 if (i < 0)
469 goto err_ret;
470 data = xmalloc(*sz + 1);
471 if (st.st_size != read_in_full(i, data, *sz)) {
472 error(_("'%s': short read %s"), filename, strerror(errno));
473 close(i);
474 free(data);
475 return NULL;
477 close(i);
478 data[*sz] = 0;
479 return data;
482 static int grep_file(struct grep_opt *opt, const char *filename)
484 struct strbuf buf = STRBUF_INIT;
485 char *name;
487 if (opt->relative && opt->prefix_length)
488 quote_path_relative(filename, -1, &buf, opt->prefix);
489 else
490 strbuf_addstr(&buf, filename);
491 name = strbuf_detach(&buf, NULL);
493 #ifndef NO_PTHREADS
494 if (use_threads) {
495 grep_file_async(opt, name, filename);
496 return 0;
497 } else
498 #endif
500 int hit;
501 size_t sz;
502 void *data = load_file(filename, &sz);
503 if (!data)
504 hit = 0;
505 else
506 hit = grep_buffer(opt, name, data, sz);
508 free(data);
509 free(name);
510 return hit;
514 static void append_path(struct grep_opt *opt, const void *data, size_t len)
516 struct string_list *path_list = opt->output_priv;
518 if (len == 1 && *(const char *)data == '\0')
519 return;
520 string_list_append(path_list, xstrndup(data, len));
523 static void run_pager(struct grep_opt *opt, const char *prefix)
525 struct string_list *path_list = opt->output_priv;
526 const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1));
527 int i, status;
529 for (i = 0; i < path_list->nr; i++)
530 argv[i] = path_list->items[i].string;
531 argv[path_list->nr] = NULL;
533 if (prefix && chdir(prefix))
534 die(_("Failed to chdir: %s"), prefix);
535 status = run_command_v_opt(argv, RUN_USING_SHELL);
536 if (status)
537 exit(status);
538 free(argv);
541 static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int cached)
543 int hit = 0;
544 int nr;
545 read_cache();
547 for (nr = 0; nr < active_nr; nr++) {
548 struct cache_entry *ce = active_cache[nr];
549 if (!S_ISREG(ce->ce_mode))
550 continue;
551 if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
552 continue;
553 if (skip_binary(opt, ce->name))
554 continue;
557 * If CE_VALID is on, we assume worktree file and its cache entry
558 * are identical, even if worktree file has been modified, so use
559 * cache version instead
561 if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
562 if (ce_stage(ce))
563 continue;
564 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
566 else
567 hit |= grep_file(opt, ce->name);
568 if (ce_stage(ce)) {
569 do {
570 nr++;
571 } while (nr < active_nr &&
572 !strcmp(ce->name, active_cache[nr]->name));
573 nr--; /* compensate for loop control */
575 if (hit && opt->status_only)
576 break;
578 return hit;
581 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
582 struct tree_desc *tree, struct strbuf *base, int tn_len)
584 int hit = 0;
585 enum interesting match = entry_not_interesting;
586 struct name_entry entry;
587 int old_baselen = base->len;
589 while (tree_entry(tree, &entry)) {
590 int te_len = tree_entry_len(&entry);
592 if (match != all_entries_interesting) {
593 match = tree_entry_interesting(&entry, base, tn_len, pathspec);
594 if (match == all_entries_not_interesting)
595 break;
596 if (match == entry_not_interesting)
597 continue;
600 strbuf_add(base, entry.path, te_len);
602 if (S_ISREG(entry.mode)) {
603 hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len);
605 else if (S_ISDIR(entry.mode)) {
606 enum object_type type;
607 struct tree_desc sub;
608 void *data;
609 unsigned long size;
611 data = lock_and_read_sha1_file(entry.sha1, &type, &size);
612 if (!data)
613 die(_("unable to read tree (%s)"),
614 sha1_to_hex(entry.sha1));
616 strbuf_addch(base, '/');
617 init_tree_desc(&sub, data, size);
618 hit |= grep_tree(opt, pathspec, &sub, base, tn_len);
619 free(data);
621 strbuf_setlen(base, old_baselen);
623 if (hit && opt->status_only)
624 break;
626 return hit;
629 static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
630 struct object *obj, const char *name)
632 if (obj->type == OBJ_BLOB)
633 return grep_sha1(opt, obj->sha1, name, 0);
634 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
635 struct tree_desc tree;
636 void *data;
637 unsigned long size;
638 struct strbuf base;
639 int hit, len;
641 read_sha1_lock();
642 data = read_object_with_reference(obj->sha1, tree_type,
643 &size, NULL);
644 read_sha1_unlock();
646 if (!data)
647 die(_("unable to read tree (%s)"), sha1_to_hex(obj->sha1));
649 len = name ? strlen(name) : 0;
650 strbuf_init(&base, PATH_MAX + len + 1);
651 if (len) {
652 strbuf_add(&base, name, len);
653 strbuf_addch(&base, ':');
655 init_tree_desc(&tree, data, size);
656 hit = grep_tree(opt, pathspec, &tree, &base, base.len);
657 strbuf_release(&base);
658 free(data);
659 return hit;
661 die(_("unable to grep from object of type %s"), typename(obj->type));
664 static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
665 const struct object_array *list)
667 unsigned int i;
668 int hit = 0;
669 const unsigned int nr = list->nr;
671 for (i = 0; i < nr; i++) {
672 struct object *real_obj;
673 real_obj = deref_tag(list->objects[i].item, NULL, 0);
674 if (grep_object(opt, pathspec, real_obj, list->objects[i].name)) {
675 hit = 1;
676 if (opt->status_only)
677 break;
680 return hit;
683 static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
684 int exc_std)
686 struct dir_struct dir;
687 int i, hit = 0;
689 memset(&dir, 0, sizeof(dir));
690 if (exc_std)
691 setup_standard_excludes(&dir);
693 fill_directory(&dir, pathspec->raw);
694 for (i = 0; i < dir.nr; i++) {
695 const char *name = dir.entries[i]->name;
696 int namelen = strlen(name);
697 if (!match_pathspec_depth(pathspec, name, namelen, 0, NULL))
698 continue;
699 hit |= grep_file(opt, dir.entries[i]->name);
700 if (hit && opt->status_only)
701 break;
703 return hit;
706 static int context_callback(const struct option *opt, const char *arg,
707 int unset)
709 struct grep_opt *grep_opt = opt->value;
710 int value;
711 const char *endp;
713 if (unset) {
714 grep_opt->pre_context = grep_opt->post_context = 0;
715 return 0;
717 value = strtol(arg, (char **)&endp, 10);
718 if (*endp) {
719 return error(_("switch `%c' expects a numerical value"),
720 opt->short_name);
722 grep_opt->pre_context = grep_opt->post_context = value;
723 return 0;
726 static int file_callback(const struct option *opt, const char *arg, int unset)
728 struct grep_opt *grep_opt = opt->value;
729 int from_stdin = !strcmp(arg, "-");
730 FILE *patterns;
731 int lno = 0;
732 struct strbuf sb = STRBUF_INIT;
734 patterns = from_stdin ? stdin : fopen(arg, "r");
735 if (!patterns)
736 die_errno(_("cannot open '%s'"), arg);
737 while (strbuf_getline(&sb, patterns, '\n') == 0) {
738 char *s;
739 size_t len;
741 /* ignore empty line like grep does */
742 if (sb.len == 0)
743 continue;
745 s = strbuf_detach(&sb, &len);
746 append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
748 if (!from_stdin)
749 fclose(patterns);
750 strbuf_release(&sb);
751 return 0;
754 static int not_callback(const struct option *opt, const char *arg, int unset)
756 struct grep_opt *grep_opt = opt->value;
757 append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
758 return 0;
761 static int and_callback(const struct option *opt, const char *arg, int unset)
763 struct grep_opt *grep_opt = opt->value;
764 append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
765 return 0;
768 static int open_callback(const struct option *opt, const char *arg, int unset)
770 struct grep_opt *grep_opt = opt->value;
771 append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
772 return 0;
775 static int close_callback(const struct option *opt, const char *arg, int unset)
777 struct grep_opt *grep_opt = opt->value;
778 append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
779 return 0;
782 static int pattern_callback(const struct option *opt, const char *arg,
783 int unset)
785 struct grep_opt *grep_opt = opt->value;
786 append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
787 return 0;
790 static int help_callback(const struct option *opt, const char *arg, int unset)
792 return -1;
795 int cmd_grep(int argc, const char **argv, const char *prefix)
797 int hit = 0;
798 int cached = 0, untracked = 0, opt_exclude = -1;
799 int seen_dashdash = 0;
800 int external_grep_allowed__ignored;
801 const char *show_in_pager = NULL, *default_pager = "dummy";
802 struct grep_opt opt;
803 struct object_array list = OBJECT_ARRAY_INIT;
804 const char **paths = NULL;
805 struct pathspec pathspec;
806 struct string_list path_list = STRING_LIST_INIT_NODUP;
807 int i;
808 int dummy;
809 int use_index = 1;
810 enum {
811 pattern_type_unspecified = 0,
812 pattern_type_bre,
813 pattern_type_ere,
814 pattern_type_fixed,
815 pattern_type_pcre,
817 int pattern_type = pattern_type_unspecified;
819 struct option options[] = {
820 OPT_BOOLEAN(0, "cached", &cached,
821 "search in index instead of in the work tree"),
822 { OPTION_BOOLEAN, 0, "index", &use_index, NULL,
823 "finds in contents not managed by git",
824 PARSE_OPT_NOARG | PARSE_OPT_NEGHELP },
825 OPT_BOOLEAN(0, "untracked", &untracked,
826 "search in both tracked and untracked files"),
827 OPT_SET_INT(0, "exclude-standard", &opt_exclude,
828 "search also in ignored files", 1),
829 OPT_GROUP(""),
830 OPT_BOOLEAN('v', "invert-match", &opt.invert,
831 "show non-matching lines"),
832 OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case,
833 "case insensitive matching"),
834 OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
835 "match patterns only at word boundaries"),
836 OPT_SET_INT('a', "text", &opt.binary,
837 "process binary files as text", GREP_BINARY_TEXT),
838 OPT_SET_INT('I', NULL, &opt.binary,
839 "don't match patterns in binary files",
840 GREP_BINARY_NOMATCH),
841 { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, "depth",
842 "descend at most <depth> levels", PARSE_OPT_NONEG,
843 NULL, 1 },
844 OPT_GROUP(""),
845 OPT_SET_INT('E', "extended-regexp", &pattern_type,
846 "use extended POSIX regular expressions",
847 pattern_type_ere),
848 OPT_SET_INT('G', "basic-regexp", &pattern_type,
849 "use basic POSIX regular expressions (default)",
850 pattern_type_bre),
851 OPT_SET_INT('F', "fixed-strings", &pattern_type,
852 "interpret patterns as fixed strings",
853 pattern_type_fixed),
854 OPT_SET_INT('P', "perl-regexp", &pattern_type,
855 "use Perl-compatible regular expressions",
856 pattern_type_pcre),
857 OPT_GROUP(""),
858 OPT_BOOLEAN('n', "line-number", &opt.linenum, "show line numbers"),
859 OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1),
860 OPT_BIT('H', NULL, &opt.pathname, "show filenames", 1),
861 OPT_NEGBIT(0, "full-name", &opt.relative,
862 "show filenames relative to top directory", 1),
863 OPT_BOOLEAN('l', "files-with-matches", &opt.name_only,
864 "show only filenames instead of matching lines"),
865 OPT_BOOLEAN(0, "name-only", &opt.name_only,
866 "synonym for --files-with-matches"),
867 OPT_BOOLEAN('L', "files-without-match",
868 &opt.unmatch_name_only,
869 "show only the names of files without match"),
870 OPT_BOOLEAN('z', "null", &opt.null_following_name,
871 "print NUL after filenames"),
872 OPT_BOOLEAN('c', "count", &opt.count,
873 "show the number of matches instead of matching lines"),
874 OPT__COLOR(&opt.color, "highlight matches"),
875 OPT_BOOLEAN(0, "break", &opt.file_break,
876 "print empty line between matches from different files"),
877 OPT_BOOLEAN(0, "heading", &opt.heading,
878 "show filename only once above matches from same file"),
879 OPT_GROUP(""),
880 OPT_CALLBACK('C', "context", &opt, "n",
881 "show <n> context lines before and after matches",
882 context_callback),
883 OPT_INTEGER('B', "before-context", &opt.pre_context,
884 "show <n> context lines before matches"),
885 OPT_INTEGER('A', "after-context", &opt.post_context,
886 "show <n> context lines after matches"),
887 OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM",
888 context_callback),
889 OPT_BOOLEAN('p', "show-function", &opt.funcname,
890 "show a line with the function name before matches"),
891 OPT_BOOLEAN('W', "function-context", &opt.funcbody,
892 "show the surrounding function"),
893 OPT_GROUP(""),
894 OPT_CALLBACK('f', NULL, &opt, "file",
895 "read patterns from file", file_callback),
896 { OPTION_CALLBACK, 'e', NULL, &opt, "pattern",
897 "match <pattern>", PARSE_OPT_NONEG, pattern_callback },
898 { OPTION_CALLBACK, 0, "and", &opt, NULL,
899 "combine patterns specified with -e",
900 PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
901 OPT_BOOLEAN(0, "or", &dummy, ""),
902 { OPTION_CALLBACK, 0, "not", &opt, NULL, "",
903 PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback },
904 { OPTION_CALLBACK, '(', NULL, &opt, NULL, "",
905 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
906 open_callback },
907 { OPTION_CALLBACK, ')', NULL, &opt, NULL, "",
908 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
909 close_callback },
910 OPT__QUIET(&opt.status_only,
911 "indicate hit with exit status without output"),
912 OPT_BOOLEAN(0, "all-match", &opt.all_match,
913 "show only matches from files that match all patterns"),
914 OPT_GROUP(""),
915 { OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager,
916 "pager", "show matching files in the pager",
917 PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
918 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
919 "allow calling of grep(1) (ignored by this build)"),
920 { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
921 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
922 OPT_END()
926 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
927 * to show usage information and exit.
929 if (argc == 2 && !strcmp(argv[1], "-h"))
930 usage_with_options(grep_usage, options);
932 memset(&opt, 0, sizeof(opt));
933 opt.prefix = prefix;
934 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
935 opt.relative = 1;
936 opt.pathname = 1;
937 opt.pattern_tail = &opt.pattern_list;
938 opt.header_tail = &opt.header_list;
939 opt.regflags = REG_NEWLINE;
940 opt.max_depth = -1;
942 strcpy(opt.color_context, "");
943 strcpy(opt.color_filename, "");
944 strcpy(opt.color_function, "");
945 strcpy(opt.color_lineno, "");
946 strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
947 strcpy(opt.color_selected, "");
948 strcpy(opt.color_sep, GIT_COLOR_CYAN);
949 opt.color = -1;
950 git_config(grep_config, &opt);
953 * If there is no -- then the paths must exist in the working
954 * tree. If there is no explicit pattern specified with -e or
955 * -f, we take the first unrecognized non option to be the
956 * pattern, but then what follows it must be zero or more
957 * valid refs up to the -- (if exists), and then existing
958 * paths. If there is an explicit pattern, then the first
959 * unrecognized non option is the beginning of the refs list
960 * that continues up to the -- (if exists), and then paths.
962 argc = parse_options(argc, argv, prefix, options, grep_usage,
963 PARSE_OPT_KEEP_DASHDASH |
964 PARSE_OPT_STOP_AT_NON_OPTION |
965 PARSE_OPT_NO_INTERNAL_HELP);
966 switch (pattern_type) {
967 case pattern_type_fixed:
968 opt.fixed = 1;
969 opt.pcre = 0;
970 break;
971 case pattern_type_bre:
972 opt.fixed = 0;
973 opt.pcre = 0;
974 opt.regflags &= ~REG_EXTENDED;
975 break;
976 case pattern_type_ere:
977 opt.fixed = 0;
978 opt.pcre = 0;
979 opt.regflags |= REG_EXTENDED;
980 break;
981 case pattern_type_pcre:
982 opt.fixed = 0;
983 opt.pcre = 1;
984 break;
985 default:
986 break; /* nothing */
989 if (use_index && !startup_info->have_repository)
990 /* die the same way as if we did it at the beginning */
991 setup_git_directory();
994 * skip a -- separator; we know it cannot be
995 * separating revisions from pathnames if
996 * we haven't even had any patterns yet
998 if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) {
999 argv++;
1000 argc--;
1003 /* First unrecognized non-option token */
1004 if (argc > 0 && !opt.pattern_list) {
1005 append_grep_pattern(&opt, argv[0], "command line", 0,
1006 GREP_PATTERN);
1007 argv++;
1008 argc--;
1011 if (show_in_pager == default_pager)
1012 show_in_pager = git_pager(1);
1013 if (show_in_pager) {
1014 opt.color = 0;
1015 opt.name_only = 1;
1016 opt.null_following_name = 1;
1017 opt.output_priv = &path_list;
1018 opt.output = append_path;
1019 string_list_append(&path_list, show_in_pager);
1020 use_threads = 0;
1022 if ((opt.binary & GREP_BINARY_NOMATCH))
1023 use_threads = 0;
1025 if (!opt.pattern_list)
1026 die(_("no pattern given."));
1027 if (!opt.fixed && opt.ignore_case)
1028 opt.regflags |= REG_ICASE;
1030 compile_grep_patterns(&opt);
1032 /* Check revs and then paths */
1033 for (i = 0; i < argc; i++) {
1034 const char *arg = argv[i];
1035 unsigned char sha1[20];
1036 /* Is it a rev? */
1037 if (!get_sha1(arg, sha1)) {
1038 struct object *object = parse_object(sha1);
1039 if (!object)
1040 die(_("bad object %s"), arg);
1041 add_object_array(object, arg, &list);
1042 continue;
1044 if (!strcmp(arg, "--")) {
1045 i++;
1046 seen_dashdash = 1;
1048 break;
1051 #ifndef NO_PTHREADS
1052 if (list.nr || cached || online_cpus() == 1)
1053 use_threads = 0;
1054 #else
1055 use_threads = 0;
1056 #endif
1058 opt.use_threads = use_threads;
1060 #ifndef NO_PTHREADS
1061 if (use_threads) {
1062 if (opt.pre_context || opt.post_context || opt.file_break ||
1063 opt.funcbody)
1064 skip_first_line = 1;
1065 start_threads(&opt);
1067 #endif
1069 /* The rest are paths */
1070 if (!seen_dashdash) {
1071 int j;
1072 for (j = i; j < argc; j++)
1073 verify_filename(prefix, argv[j]);
1076 paths = get_pathspec(prefix, argv + i);
1077 init_pathspec(&pathspec, paths);
1078 pathspec.max_depth = opt.max_depth;
1079 pathspec.recursive = 1;
1081 if (show_in_pager && (cached || list.nr))
1082 die(_("--open-files-in-pager only works on the worktree"));
1084 if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
1085 const char *pager = path_list.items[0].string;
1086 int len = strlen(pager);
1088 if (len > 4 && is_dir_sep(pager[len - 5]))
1089 pager += len - 4;
1091 if (!strcmp("less", pager) || !strcmp("vi", pager)) {
1092 struct strbuf buf = STRBUF_INIT;
1093 strbuf_addf(&buf, "+/%s%s",
1094 strcmp("less", pager) ? "" : "*",
1095 opt.pattern_list->pattern);
1096 string_list_append(&path_list, buf.buf);
1097 strbuf_detach(&buf, NULL);
1101 if (!show_in_pager)
1102 setup_pager();
1104 if (!use_index && (untracked || cached))
1105 die(_("--cached or --untracked cannot be used with --no-index."));
1107 if (!use_index || untracked) {
1108 int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
1109 if (list.nr)
1110 die(_("--no-index or --untracked cannot be used with revs."));
1111 hit = grep_directory(&opt, &pathspec, use_exclude);
1112 } else if (0 <= opt_exclude) {
1113 die(_("--[no-]exclude-standard cannot be used for tracked contents."));
1114 } else if (!list.nr) {
1115 if (!cached)
1116 setup_work_tree();
1118 hit = grep_cache(&opt, &pathspec, cached);
1119 } else {
1120 if (cached)
1121 die(_("both --cached and trees are given."));
1122 hit = grep_objects(&opt, &pathspec, &list);
1125 if (use_threads)
1126 hit |= wait_all();
1127 if (hit && show_in_pager)
1128 run_pager(&opt, prefix);
1129 free_grep_patterns(&opt);
1130 return !hit;