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"
23 #include "thread-utils.h"
26 static char const * const grep_usage[] = {
27 "git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
31 static int use_threads = 1;
35 static pthread_t threads[THREADS];
37 static void *load_sha1(const unsigned char *sha1, unsigned long *size,
39 static void *load_file(const char *filename, size_t *sz);
41 enum work_type {WORK_SHA1, WORK_FILE};
43 /* We use one producer thread and THREADS consumer
44 * threads. The producer adds struct work_items to 'todo' and the
45 * consumers pick work items from the same array.
52 /* if type == WORK_SHA1, then 'identifier' is a SHA1,
53 * otherwise type == WORK_FILE, and 'identifier' is a NUL
54 * terminated filename.
61 /* In the range [todo_done, todo_start) in 'todo' we have work_items
62 * that have been or are processed by a consumer thread. We haven't
63 * written the result for these to stdout yet.
65 * The work_items in [todo_start, todo_end) are waiting to be picked
66 * up by a consumer thread.
68 * The ranges are modulo TODO_SIZE.
71 static struct work_item todo[TODO_SIZE];
72 static int todo_start;
76 /* Has all work items been added? */
77 static int all_work_added;
79 /* This lock protects all the variables above. */
80 static pthread_mutex_t grep_mutex;
82 /* Used to serialize calls to read_sha1_file. */
83 static pthread_mutex_t read_sha1_mutex;
85 #define grep_lock() pthread_mutex_lock(&grep_mutex)
86 #define grep_unlock() pthread_mutex_unlock(&grep_mutex)
87 #define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex)
88 #define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex)
90 /* Signalled when a new work_item is added to todo. */
91 static pthread_cond_t cond_add;
93 /* Signalled when the result from one work_item is written to
96 static pthread_cond_t cond_write;
98 /* Signalled when we are finished with everything. */
99 static pthread_cond_t cond_result;
101 static int print_hunk_marks_between_files;
102 static int printed_something;
104 static void add_work(enum work_type type, char *name, void *id)
108 while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) {
109 pthread_cond_wait(&cond_write, &grep_mutex);
112 todo[todo_end].type = type;
113 todo[todo_end].name = name;
114 todo[todo_end].identifier = id;
115 todo[todo_end].done = 0;
116 strbuf_reset(&todo[todo_end].out);
117 todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
119 pthread_cond_signal(&cond_add);
123 static struct work_item *get_work(void)
125 struct work_item *ret;
128 while (todo_start == todo_end && !all_work_added) {
129 pthread_cond_wait(&cond_add, &grep_mutex);
132 if (todo_start == todo_end && all_work_added) {
135 ret = &todo[todo_start];
136 todo_start = (todo_start + 1) % ARRAY_SIZE(todo);
142 static void grep_sha1_async(struct grep_opt *opt, char *name,
143 const unsigned char *sha1)
148 add_work(WORK_SHA1, name, s);
151 static void grep_file_async(struct grep_opt *opt, char *name,
152 const char *filename)
154 add_work(WORK_FILE, name, xstrdup(filename));
157 static void work_done(struct work_item *w)
163 old_done = todo_done;
164 for(; todo[todo_done].done && todo_done != todo_start;
165 todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
166 w = &todo[todo_done];
168 if (print_hunk_marks_between_files && printed_something)
169 write_or_die(1, "--\n", 3);
170 write_or_die(1, w->out.buf, w->out.len);
171 printed_something = 1;
177 if (old_done != todo_done)
178 pthread_cond_signal(&cond_write);
180 if (all_work_added && todo_done == todo_end)
181 pthread_cond_signal(&cond_result);
186 static void *run(void *arg)
189 struct grep_opt *opt = arg;
192 struct work_item *w = get_work();
196 opt->output_priv = w;
197 if (w->type == WORK_SHA1) {
199 void* data = load_sha1(w->identifier, &sz, w->name);
202 hit |= grep_buffer(opt, w->name, data, sz);
205 } else if (w->type == WORK_FILE) {
207 void* data = load_file(w->identifier, &sz);
209 hit |= grep_buffer(opt, w->name, data, sz);
218 free_grep_patterns(arg);
221 return (void*) (intptr_t) hit;
224 static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size)
226 struct work_item *w = opt->output_priv;
227 strbuf_add(&w->out, buf, size);
230 static void start_threads(struct grep_opt *opt)
234 pthread_mutex_init(&grep_mutex, NULL);
235 pthread_mutex_init(&read_sha1_mutex, NULL);
236 pthread_cond_init(&cond_add, NULL);
237 pthread_cond_init(&cond_write, NULL);
238 pthread_cond_init(&cond_result, NULL);
240 for (i = 0; i < ARRAY_SIZE(todo); i++) {
241 strbuf_init(&todo[i].out, 0);
244 for (i = 0; i < ARRAY_SIZE(threads); i++) {
246 struct grep_opt *o = grep_opt_dup(opt);
247 o->output = strbuf_out;
248 compile_grep_patterns(o);
249 err = pthread_create(&threads[i], NULL, run, o);
252 die("grep: failed to create thread: %s",
257 static int wait_all(void)
265 /* Wait until all work is done. */
266 while (todo_done != todo_end)
267 pthread_cond_wait(&cond_result, &grep_mutex);
269 /* Wake up all the consumer threads so they can see that there
270 * is no more work to do.
272 pthread_cond_broadcast(&cond_add);
275 for (i = 0; i < ARRAY_SIZE(threads); i++) {
277 pthread_join(threads[i], &h);
278 hit |= (int) (intptr_t) h;
281 pthread_mutex_destroy(&grep_mutex);
282 pthread_mutex_destroy(&read_sha1_mutex);
283 pthread_cond_destroy(&cond_add);
284 pthread_cond_destroy(&cond_write);
285 pthread_cond_destroy(&cond_result);
289 #else /* !NO_PTHREADS */
290 #define read_sha1_lock()
291 #define read_sha1_unlock()
293 static int wait_all(void)
299 static int grep_config(const char *var, const char *value, void *cb)
301 struct grep_opt *opt = cb;
304 switch (userdiff_config(var, value)) {
310 if (!strcmp(var, "color.grep"))
311 opt->color = git_config_colorbool(var, value, -1);
312 else if (!strcmp(var, "color.grep.context"))
313 color = opt->color_context;
314 else if (!strcmp(var, "color.grep.filename"))
315 color = opt->color_filename;
316 else if (!strcmp(var, "color.grep.function"))
317 color = opt->color_function;
318 else if (!strcmp(var, "color.grep.linenumber"))
319 color = opt->color_lineno;
320 else if (!strcmp(var, "color.grep.match"))
321 color = opt->color_match;
322 else if (!strcmp(var, "color.grep.selected"))
323 color = opt->color_selected;
324 else if (!strcmp(var, "color.grep.separator"))
325 color = opt->color_sep;
327 return git_color_default_config(var, value, cb);
330 return config_error_nonbool(var);
331 color_parse(value, var, color);
337 * Return non-zero if max_depth is negative or path has no more then max_depth
340 static int accept_subdir(const char *path, int max_depth)
345 while ((path = strchr(path, '/')) != NULL) {
355 * Return non-zero if name is a subdirectory of match and is not too deep.
357 static int is_subdir(const char *name, int namelen,
358 const char *match, int matchlen, int max_depth)
360 if (matchlen > namelen || strncmp(name, match, matchlen))
363 if (name[matchlen] == '\0') /* exact match */
366 if (!matchlen || match[matchlen-1] == '/' || name[matchlen] == '/')
367 return accept_subdir(name + matchlen + 1, max_depth);
373 * git grep pathspecs are somewhat different from diff-tree pathspecs;
374 * pathname wildcards are allowed.
376 static int pathspec_matches(const char **paths, const char *name, int max_depth)
379 if (!paths || !*paths)
380 return accept_subdir(name, max_depth);
381 namelen = strlen(name);
382 for (i = 0; paths[i]; i++) {
383 const char *match = paths[i];
384 int matchlen = strlen(match);
385 const char *cp, *meta;
387 if (is_subdir(name, namelen, match, matchlen, max_depth))
389 if (!fnmatch(match, name, 0))
391 if (name[namelen-1] != '/')
394 /* We are being asked if the directory ("name") is worth
397 * Find the longest leading directory name that does
398 * not have metacharacter in the pathspec; the name
399 * we are looking at must overlap with that directory.
401 for (cp = match, meta = NULL; cp - match < matchlen; cp++) {
403 if (ch == '*' || ch == '[' || ch == '?') {
409 meta = cp; /* fully literal */
411 if (namelen <= meta - match) {
412 /* Looking at "Documentation/" and
413 * the pattern says "Documentation/howto/", or
414 * "Documentation/diff*.txt". The name we
415 * have should match prefix.
417 if (!memcmp(match, name, namelen))
422 if (meta - match < namelen) {
423 /* Looking at "Documentation/howto/" and
424 * the pattern says "Documentation/h*";
425 * match up to "Do.../h"; this avoids descending
426 * into "Documentation/technical/".
428 if (!memcmp(match, name, meta - match))
436 static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
442 data = read_sha1_file(sha1, type, size);
445 data = read_sha1_file(sha1, type, size);
450 static void *load_sha1(const unsigned char *sha1, unsigned long *size,
453 enum object_type type;
454 void *data = lock_and_read_sha1_file(sha1, &type, size);
457 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
462 static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
463 const char *filename, int tree_name_len)
465 struct strbuf pathbuf = STRBUF_INIT;
468 if (opt->relative && opt->prefix_length) {
469 quote_path_relative(filename + tree_name_len, -1, &pathbuf,
471 strbuf_insert(&pathbuf, 0, filename, tree_name_len);
473 strbuf_addstr(&pathbuf, filename);
476 name = strbuf_detach(&pathbuf, NULL);
480 grep_sha1_async(opt, name, sha1);
487 void *data = load_sha1(sha1, &sz, name);
491 hit = grep_buffer(opt, name, data, sz);
499 static void *load_file(const char *filename, size_t *sz)
505 if (lstat(filename, &st) < 0) {
508 error("'%s': %s", filename, strerror(errno));
511 if (!S_ISREG(st.st_mode))
513 *sz = xsize_t(st.st_size);
514 i = open(filename, O_RDONLY);
517 data = xmalloc(*sz + 1);
518 if (st.st_size != read_in_full(i, data, *sz)) {
519 error("'%s': short read %s", filename, strerror(errno));
529 static int grep_file(struct grep_opt *opt, const char *filename)
531 struct strbuf buf = STRBUF_INIT;
534 if (opt->relative && opt->prefix_length)
535 quote_path_relative(filename, -1, &buf, opt->prefix);
537 strbuf_addstr(&buf, filename);
538 name = strbuf_detach(&buf, NULL);
542 grep_file_async(opt, name, filename);
549 void *data = load_file(filename, &sz);
553 hit = grep_buffer(opt, name, data, sz);
561 static void append_path(struct grep_opt *opt, const void *data, size_t len)
563 struct string_list *path_list = opt->output_priv;
565 if (len == 1 && *(const char *)data == '\0')
567 string_list_append(path_list, xstrndup(data, len));
570 static void run_pager(struct grep_opt *opt, const char *prefix)
572 struct string_list *path_list = opt->output_priv;
573 const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1));
576 for (i = 0; i < path_list->nr; i++)
577 argv[i] = path_list->items[i].string;
578 argv[path_list->nr] = NULL;
580 if (prefix && chdir(prefix))
581 die("Failed to chdir: %s", prefix);
582 status = run_command_v_opt(argv, RUN_USING_SHELL);
588 static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
594 for (nr = 0; nr < active_nr; nr++) {
595 struct cache_entry *ce = active_cache[nr];
596 if (!S_ISREG(ce->ce_mode))
598 if (!pathspec_matches(paths, ce->name, opt->max_depth))
601 * If CE_VALID is on, we assume worktree file and its cache entry
602 * are identical, even if worktree file has been modified, so use
603 * cache version instead
605 if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
608 hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
611 hit |= grep_file(opt, ce->name);
615 } while (nr < active_nr &&
616 !strcmp(ce->name, active_cache[nr]->name));
617 nr--; /* compensate for loop control */
619 if (hit && opt->status_only)
625 static int grep_tree(struct grep_opt *opt, const char **paths,
626 struct tree_desc *tree,
627 const char *tree_name, const char *base)
631 struct name_entry entry;
633 int tn_len = strlen(tree_name);
634 struct strbuf pathbuf;
636 strbuf_init(&pathbuf, PATH_MAX + tn_len);
639 strbuf_add(&pathbuf, tree_name, tn_len);
640 strbuf_addch(&pathbuf, ':');
641 tn_len = pathbuf.len;
643 strbuf_addstr(&pathbuf, base);
646 while (tree_entry(tree, &entry)) {
647 int te_len = tree_entry_len(entry.path, entry.sha1);
649 strbuf_add(&pathbuf, entry.path, te_len);
651 if (S_ISDIR(entry.mode))
652 /* Match "abc/" against pathspec to
653 * decide if we want to descend into "abc"
656 strbuf_addch(&pathbuf, '/');
658 down = pathbuf.buf + tn_len;
659 if (!pathspec_matches(paths, down, opt->max_depth))
661 else if (S_ISREG(entry.mode))
662 hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
663 else if (S_ISDIR(entry.mode)) {
664 enum object_type type;
665 struct tree_desc sub;
669 data = lock_and_read_sha1_file(entry.sha1, &type, &size);
671 die("unable to read tree (%s)",
672 sha1_to_hex(entry.sha1));
673 init_tree_desc(&sub, data, size);
674 hit |= grep_tree(opt, paths, &sub, tree_name, down);
677 if (hit && opt->status_only)
680 strbuf_release(&pathbuf);
684 static int grep_object(struct grep_opt *opt, const char **paths,
685 struct object *obj, const char *name)
687 if (obj->type == OBJ_BLOB)
688 return grep_sha1(opt, obj->sha1, name, 0);
689 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
690 struct tree_desc tree;
694 data = read_object_with_reference(obj->sha1, tree_type,
697 die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
698 init_tree_desc(&tree, data, size);
699 hit = grep_tree(opt, paths, &tree, name, "");
703 die("unable to grep from object of type %s", typename(obj->type));
706 static int grep_objects(struct grep_opt *opt, const char **paths,
707 const struct object_array *list)
711 const unsigned int nr = list->nr;
713 for (i = 0; i < nr; i++) {
714 struct object *real_obj;
715 real_obj = deref_tag(list->objects[i].item, NULL, 0);
716 if (grep_object(opt, paths, real_obj, list->objects[i].name)) {
718 if (opt->status_only)
725 static int grep_directory(struct grep_opt *opt, const char **paths)
727 struct dir_struct dir;
730 memset(&dir, 0, sizeof(dir));
731 setup_standard_excludes(&dir);
733 fill_directory(&dir, paths);
734 for (i = 0; i < dir.nr; i++) {
735 hit |= grep_file(opt, dir.entries[i]->name);
736 if (hit && opt->status_only)
742 static int context_callback(const struct option *opt, const char *arg,
745 struct grep_opt *grep_opt = opt->value;
750 grep_opt->pre_context = grep_opt->post_context = 0;
753 value = strtol(arg, (char **)&endp, 10);
755 return error("switch `%c' expects a numerical value",
758 grep_opt->pre_context = grep_opt->post_context = value;
762 static int file_callback(const struct option *opt, const char *arg, int unset)
764 struct grep_opt *grep_opt = opt->value;
767 struct strbuf sb = STRBUF_INIT;
769 patterns = fopen(arg, "r");
771 die_errno("cannot open '%s'", arg);
772 while (strbuf_getline(&sb, patterns, '\n') == 0) {
776 /* ignore empty line like grep does */
780 s = strbuf_detach(&sb, &len);
781 append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
788 static int not_callback(const struct option *opt, const char *arg, int unset)
790 struct grep_opt *grep_opt = opt->value;
791 append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
795 static int and_callback(const struct option *opt, const char *arg, int unset)
797 struct grep_opt *grep_opt = opt->value;
798 append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
802 static int open_callback(const struct option *opt, const char *arg, int unset)
804 struct grep_opt *grep_opt = opt->value;
805 append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
809 static int close_callback(const struct option *opt, const char *arg, int unset)
811 struct grep_opt *grep_opt = opt->value;
812 append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
816 static int pattern_callback(const struct option *opt, const char *arg,
819 struct grep_opt *grep_opt = opt->value;
820 append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
824 static int help_callback(const struct option *opt, const char *arg, int unset)
829 int cmd_grep(int argc, const char **argv, const char *prefix)
833 int seen_dashdash = 0;
834 int external_grep_allowed__ignored;
835 const char *show_in_pager = NULL, *default_pager = "dummy";
837 struct object_array list = OBJECT_ARRAY_INIT;
838 const char **paths = NULL;
839 struct string_list path_list = STRING_LIST_INIT_NODUP;
843 struct option options[] = {
844 OPT_BOOLEAN(0, "cached", &cached,
845 "search in index instead of in the work tree"),
846 OPT_BOOLEAN(0, "index", &use_index,
847 "--no-index finds in contents not managed by git"),
849 OPT_BOOLEAN('v', "invert-match", &opt.invert,
850 "show non-matching lines"),
851 OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case,
852 "case insensitive matching"),
853 OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
854 "match patterns only at word boundaries"),
855 OPT_SET_INT('a', "text", &opt.binary,
856 "process binary files as text", GREP_BINARY_TEXT),
857 OPT_SET_INT('I', NULL, &opt.binary,
858 "don't match patterns in binary files",
859 GREP_BINARY_NOMATCH),
860 { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, "depth",
861 "descend at most <depth> levels", PARSE_OPT_NONEG,
864 OPT_BIT('E', "extended-regexp", &opt.regflags,
865 "use extended POSIX regular expressions", REG_EXTENDED),
866 OPT_NEGBIT('G', "basic-regexp", &opt.regflags,
867 "use basic POSIX regular expressions (default)",
869 OPT_BOOLEAN('F', "fixed-strings", &opt.fixed,
870 "interpret patterns as fixed strings"),
872 OPT_BOOLEAN('n', NULL, &opt.linenum, "show line numbers"),
873 OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1),
874 OPT_BIT('H', NULL, &opt.pathname, "show filenames", 1),
875 OPT_NEGBIT(0, "full-name", &opt.relative,
876 "show filenames relative to top directory", 1),
877 OPT_BOOLEAN('l', "files-with-matches", &opt.name_only,
878 "show only filenames instead of matching lines"),
879 OPT_BOOLEAN(0, "name-only", &opt.name_only,
880 "synonym for --files-with-matches"),
881 OPT_BOOLEAN('L', "files-without-match",
882 &opt.unmatch_name_only,
883 "show only the names of files without match"),
884 OPT_BOOLEAN('z', "null", &opt.null_following_name,
885 "print NUL after filenames"),
886 OPT_BOOLEAN('c', "count", &opt.count,
887 "show the number of matches instead of matching lines"),
888 OPT__COLOR(&opt.color, "highlight matches"),
890 OPT_CALLBACK('C', NULL, &opt, "n",
891 "show <n> context lines before and after matches",
893 OPT_INTEGER('B', NULL, &opt.pre_context,
894 "show <n> context lines before matches"),
895 OPT_INTEGER('A', NULL, &opt.post_context,
896 "show <n> context lines after matches"),
897 OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM",
899 OPT_BOOLEAN('p', "show-function", &opt.funcname,
900 "show a line with the function name before matches"),
902 OPT_CALLBACK('f', NULL, &opt, "file",
903 "read patterns from file", file_callback),
904 { OPTION_CALLBACK, 'e', NULL, &opt, "pattern",
905 "match <pattern>", PARSE_OPT_NONEG, pattern_callback },
906 { OPTION_CALLBACK, 0, "and", &opt, NULL,
907 "combine patterns specified with -e",
908 PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
909 OPT_BOOLEAN(0, "or", &dummy, ""),
910 { OPTION_CALLBACK, 0, "not", &opt, NULL, "",
911 PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback },
912 { OPTION_CALLBACK, '(', NULL, &opt, NULL, "",
913 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
915 { OPTION_CALLBACK, ')', NULL, &opt, NULL, "",
916 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
918 OPT_BOOLEAN('q', "quiet", &opt.status_only,
919 "indicate hit with exit status without output"),
920 OPT_BOOLEAN(0, "all-match", &opt.all_match,
921 "show only matches from files that match all patterns"),
923 { OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager,
924 "pager", "show matching files in the pager",
925 PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
926 OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
927 "allow calling of grep(1) (ignored by this build)"),
928 { OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
929 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
934 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
935 * to show usage information and exit.
937 if (argc == 2 && !strcmp(argv[1], "-h"))
938 usage_with_options(grep_usage, options);
940 memset(&opt, 0, sizeof(opt));
942 opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
945 opt.pattern_tail = &opt.pattern_list;
946 opt.header_tail = &opt.header_list;
947 opt.regflags = REG_NEWLINE;
950 strcpy(opt.color_context, "");
951 strcpy(opt.color_filename, "");
952 strcpy(opt.color_function, "");
953 strcpy(opt.color_lineno, "");
954 strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
955 strcpy(opt.color_selected, "");
956 strcpy(opt.color_sep, GIT_COLOR_CYAN);
958 git_config(grep_config, &opt);
960 opt.color = git_use_color_default;
963 * If there is no -- then the paths must exist in the working
964 * tree. If there is no explicit pattern specified with -e or
965 * -f, we take the first unrecognized non option to be the
966 * pattern, but then what follows it must be zero or more
967 * valid refs up to the -- (if exists), and then existing
968 * paths. If there is an explicit pattern, then the first
969 * unrecognized non option is the beginning of the refs list
970 * that continues up to the -- (if exists), and then paths.
972 argc = parse_options(argc, argv, prefix, options, grep_usage,
973 PARSE_OPT_KEEP_DASHDASH |
974 PARSE_OPT_STOP_AT_NON_OPTION |
975 PARSE_OPT_NO_INTERNAL_HELP);
977 if (use_index && !startup_info->have_repository)
978 /* die the same way as if we did it at the beginning */
979 setup_git_directory();
982 * skip a -- separator; we know it cannot be
983 * separating revisions from pathnames if
984 * we haven't even had any patterns yet
986 if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) {
991 /* First unrecognized non-option token */
992 if (argc > 0 && !opt.pattern_list) {
993 append_grep_pattern(&opt, argv[0], "command line", 0,
999 if (show_in_pager == default_pager)
1000 show_in_pager = git_pager(1);
1001 if (show_in_pager) {
1004 opt.null_following_name = 1;
1005 opt.output_priv = &path_list;
1006 opt.output = append_path;
1007 string_list_append(&path_list, show_in_pager);
1011 if (!opt.pattern_list)
1012 die("no pattern given.");
1013 if (!opt.fixed && opt.ignore_case)
1014 opt.regflags |= REG_ICASE;
1015 if ((opt.regflags != REG_NEWLINE) && opt.fixed)
1016 die("cannot mix --fixed-strings and regexp");
1019 if (online_cpus() == 1 || !grep_threads_ok(&opt))
1023 if (opt.pre_context || opt.post_context)
1024 print_hunk_marks_between_files = 1;
1025 start_threads(&opt);
1031 compile_grep_patterns(&opt);
1033 /* Check revs and then paths */
1034 for (i = 0; i < argc; i++) {
1035 const char *arg = argv[i];
1036 unsigned char sha1[20];
1038 if (!get_sha1(arg, sha1)) {
1039 struct object *object = parse_object(sha1);
1041 die("bad object %s", arg);
1042 add_object_array(object, arg, &list);
1045 if (!strcmp(arg, "--")) {
1052 /* The rest are paths */
1053 if (!seen_dashdash) {
1055 for (j = i; j < argc; j++)
1056 verify_filename(prefix, argv[j]);
1060 paths = get_pathspec(prefix, argv + i);
1062 paths = xcalloc(2, sizeof(const char *));
1067 if (show_in_pager && (cached || list.nr))
1068 die("--open-files-in-pager only works on the worktree");
1070 if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
1071 const char *pager = path_list.items[0].string;
1072 int len = strlen(pager);
1074 if (len > 4 && is_dir_sep(pager[len - 5]))
1077 if (!strcmp("less", pager) || !strcmp("vi", pager)) {
1078 struct strbuf buf = STRBUF_INIT;
1079 strbuf_addf(&buf, "+/%s%s",
1080 strcmp("less", pager) ? "" : "*",
1081 opt.pattern_list->pattern);
1082 string_list_append(&path_list, buf.buf);
1083 strbuf_detach(&buf, NULL);
1093 die("--cached cannot be used with --no-index.");
1095 die("--no-index cannot be used with revs.");
1096 hit = grep_directory(&opt, paths);
1097 } else if (!list.nr) {
1101 hit = grep_cache(&opt, paths, cached);
1104 die("both --cached and trees are given.");
1105 hit = grep_objects(&opt, paths, &list);
1110 if (hit && show_in_pager)
1111 run_pager(&opt, prefix);
1112 free_grep_patterns(&opt);