Raw
1 /*
2 * Builtin "git grep"
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6
7 #define USE_THE_REPOSITORY_VARIABLE
8 #define DISABLE_SIGN_COMPARE_WARNINGS
9
10 #include "builtin.h"
11 #include "abspath.h"
12 #include "environment.h"
13 #include "gettext.h"
14 #include "hex.h"
15 #include "config.h"
16 #include "tag.h"
17 #include "tree-walk.h"
18 #include "parse-options.h"
19 #include "string-list.h"
20 #include "run-command.h"
21 #include "grep.h"
22 #include "quote.h"
23 #include "dir.h"
24 #include "pathspec.h"
25 #include "setup.h"
26 #include "submodule.h"
27 #include "submodule-config.h"
28 #include "object-name.h"
29 #include "odb.h"
30 #include "odb/source.h"
31 #include "oid-array.h"
32 #include "oidset.h"
33 #include "pager.h"
34 #include "path.h"
35 #include "promisor-remote.h"
36 #include "read-cache-ll.h"
37 #include "write-or-die.h"
38
39 static const char *grep_prefix;
40
41 static char const * const grep_usage[] = {
42 N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
43 NULL
44 };
45
46 static int recurse_submodules;
47
48 static int num_threads;
49
50 static pthread_t *threads;
51
52 /* We use one producer thread and THREADS consumer
53 * threads. The producer adds struct work_items to 'todo' and the
54 * consumers pick work items from the same array.
55 */
56 struct work_item {
57 struct grep_source source;
58 char done;
59 struct strbuf out;
60 };
61
62 /* In the range [todo_done, todo_start) in 'todo' we have work_items
63 * that have been or are processed by a consumer thread. We haven't
64 * written the result for these to stdout yet.
65 *
66 * The work_items in [todo_start, todo_end) are waiting to be picked
67 * up by a consumer thread.
68 *
69 * The ranges are modulo TODO_SIZE.
70 */
71 #define TODO_SIZE 128
72 static struct work_item todo[TODO_SIZE];
73 static int todo_start;
74 static int todo_end;
75 static int todo_done;
76
77 /* Has all work items been added? */
78 static int all_work_added;
79
80 static struct repository **repos_to_free;
81 static size_t repos_to_free_nr, repos_to_free_alloc;
82
83 /* This lock protects all the variables above. */
84 static pthread_mutex_t grep_mutex;
85
86 static inline void grep_lock(void)
87 {
88 pthread_mutex_lock(&grep_mutex);
89 }
90
91 static inline void grep_unlock(void)
92 {
93 pthread_mutex_unlock(&grep_mutex);
94 }
95
96 /* Signalled when a new work_item is added to todo. */
97 static pthread_cond_t cond_add;
98
99 /* Signalled when the result from one work_item is written to
100 * stdout.
101 */
102 static pthread_cond_t cond_write;
103
104 /* Signalled when we are finished with everything. */
105 static pthread_cond_t cond_result;
106
107 static int skip_first_line;
108
109 static void add_work(struct grep_opt *opt, struct grep_source *gs)
110 {
111 if (opt->binary != GREP_BINARY_TEXT)
112 grep_source_load_driver(gs, opt->repo->index);
113
114 grep_lock();
115
116 while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) {
117 pthread_cond_wait(&cond_write, &grep_mutex);
118 }
119
120 todo[todo_end].source = *gs;
121 todo[todo_end].done = 0;
122 strbuf_reset(&todo[todo_end].out);
123 todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
124
125 pthread_cond_signal(&cond_add);
126 grep_unlock();
127 }
128
129 static struct work_item *get_work(void)
130 {
131 struct work_item *ret;
132
133 grep_lock();
134 while (todo_start == todo_end && !all_work_added) {
135 pthread_cond_wait(&cond_add, &grep_mutex);
136 }
137
138 if (todo_start == todo_end && all_work_added) {
139 ret = NULL;
140 } else {
141 ret = &todo[todo_start];
142 todo_start = (todo_start + 1) % ARRAY_SIZE(todo);
143 }
144 grep_unlock();
145 return ret;
146 }
147
148 static void work_done(struct work_item *w)
149 {
150 int old_done;
151
152 grep_lock();
153 w->done = 1;
154 old_done = todo_done;
155 for(; todo[todo_done].done && todo_done != todo_start;
156 todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
157 w = &todo[todo_done];
158 if (w->out.len) {
159 const char *p = w->out.buf;
160 size_t len = w->out.len;
161
162 /* Skip the leading hunk mark of the first file. */
163 if (skip_first_line) {
164 while (len) {
165 len--;
166 if (*p++ == '\n')
167 break;
168 }
169 skip_first_line = 0;
170 }
171
172 write_or_die(1, p, len);
173 }
174 grep_source_clear(&w->source);
175 }
176
177 if (old_done != todo_done)
178 pthread_cond_signal(&cond_write);
179
180 if (all_work_added && todo_done == todo_end)
181 pthread_cond_signal(&cond_result);
182
183 grep_unlock();
184 }
185
186 static void free_repos(void)
187 {
188 int i;
189
190 for (i = 0; i < repos_to_free_nr; i++) {
191 repo_clear(repos_to_free[i]);
192 free(repos_to_free[i]);
193 }
194 FREE_AND_NULL(repos_to_free);
195 repos_to_free_nr = 0;
196 repos_to_free_alloc = 0;
197 }
198
199 static void *run(void *arg)
200 {
201 int hit = 0;
202 struct grep_opt *opt = arg;
203
204 while (1) {
205 struct work_item *w = get_work();
206 if (!w)
207 break;
208
209 opt->output_priv = w;
210 hit |= grep_source(opt, &w->source);
211 grep_source_clear_data(&w->source);
212 work_done(w);
213 }
214 free_grep_patterns(opt);
215 free(opt);
216
217 return (void*) (intptr_t) hit;
218 }
219
220 static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size)
221 {
222 struct work_item *w = opt->output_priv;
223 strbuf_add(&w->out, buf, size);
224 }
225
226 static void start_threads(struct grep_opt *opt)
227 {
228 int i;
229
230 pthread_mutex_init(&grep_mutex, NULL);
231 pthread_mutex_init(&grep_attr_mutex, NULL);
232 pthread_cond_init(&cond_add, NULL);
233 pthread_cond_init(&cond_write, NULL);
234 pthread_cond_init(&cond_result, NULL);
235 grep_use_locks = 1;
236 enable_obj_read_lock();
237
238 for (i = 0; i < ARRAY_SIZE(todo); i++) {
239 strbuf_init(&todo[i].out, 0);
240 }
241
242 CALLOC_ARRAY(threads, num_threads);
243 for (i = 0; i < num_threads; i++) {
244 int err;
245 struct grep_opt *o = grep_opt_dup(opt);
246 o->output = strbuf_out;
247 compile_grep_patterns(o);
248 err = pthread_create(&threads[i], NULL, run, o);
249
250 if (err)
251 die(_("grep: failed to create thread: %s"),
252 strerror(err));
253 }
254 }
255
256 static int wait_all(void)
257 {
258 int hit = 0;
259 int i;
260
261 if (!HAVE_THREADS)
262 BUG("Never call this function unless you have started threads");
263
264 grep_lock();
265 all_work_added = 1;
266
267 /* Wait until all work is done. */
268 while (todo_done != todo_end)
269 pthread_cond_wait(&cond_result, &grep_mutex);
270
271 /* Wake up all the consumer threads so they can see that there
272 * is no more work to do.
273 */
274 pthread_cond_broadcast(&cond_add);
275 grep_unlock();
276
277 for (i = 0; i < num_threads; i++) {
278 void *h;
279 pthread_join(threads[i], &h);
280 hit |= (int) (intptr_t) h;
281 }
282
283 free(threads);
284
285 pthread_mutex_destroy(&grep_mutex);
286 pthread_mutex_destroy(&grep_attr_mutex);
287 pthread_cond_destroy(&cond_add);
288 pthread_cond_destroy(&cond_write);
289 pthread_cond_destroy(&cond_result);
290 grep_use_locks = 0;
291 disable_obj_read_lock();
292
293 return hit;
294 }
295
296 static int grep_cmd_config(const char *var, const char *value,
297 const struct config_context *ctx, void *cb)
298 {
299 int st = grep_config(var, value, ctx, cb);
300
301 if (git_color_config(var, value, cb) < 0)
302 st = -1;
303 else if (git_default_config(var, value, ctx, cb) < 0)
304 st = -1;
305
306 if (!strcmp(var, "grep.threads")) {
307 num_threads = git_config_int(var, value, ctx->kvi);
308 if (num_threads < 0)
309 die(_("invalid number of threads specified (%d) for %s"),
310 num_threads, var);
311 else if (!HAVE_THREADS && num_threads > 1) {
312 /*
313 * TRANSLATORS: %s is the configuration
314 * variable for tweaking threads, currently
315 * grep.threads
316 */
317 warning(_("no threads support, ignoring %s"), var);
318 num_threads = 1;
319 }
320 }
321
322 if (!strcmp(var, "submodule.recurse"))
323 recurse_submodules = git_config_bool(var, value);
324
325 return st;
326 }
327
328 static void grep_source_name(struct grep_opt *opt, const char *filename,
329 int tree_name_len, struct strbuf *out)
330 {
331 strbuf_reset(out);
332
333 if (opt->null_following_name) {
334 if (opt->relative && grep_prefix) {
335 struct strbuf rel_buf = STRBUF_INIT;
336 const char *rel_name =
337 relative_path(filename + tree_name_len,
338 grep_prefix, &rel_buf);
339
340 if (tree_name_len)
341 strbuf_add(out, filename, tree_name_len);
342
343 strbuf_addstr(out, rel_name);
344 strbuf_release(&rel_buf);
345 } else {
346 strbuf_addstr(out, filename);
347 }
348 return;
349 }
350
351 if (opt->relative && grep_prefix)
352 quote_path(filename + tree_name_len, grep_prefix, out, 0);
353 else
354 quote_c_style(filename + tree_name_len, out, NULL, 0);
355
356 if (tree_name_len)
357 strbuf_insert(out, 0, filename, tree_name_len);
358 }
359
360 static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
361 const char *filename, int tree_name_len,
362 const char *path)
363 {
364 struct strbuf pathbuf = STRBUF_INIT;
365 struct grep_source gs;
366
367 grep_source_name(opt, filename, tree_name_len, &pathbuf);
368 grep_source_init_oid(&gs, pathbuf.buf, path, oid, opt->repo);
369 strbuf_release(&pathbuf);
370
371 if (num_threads > 1) {
372 /*
373 * add_work() copies gs and thus assumes ownership of
374 * its fields, so do not call grep_source_clear()
375 */
376 add_work(opt, &gs);
377 return 0;
378 } else {
379 int hit;
380
381 hit = grep_source(opt, &gs);
382
383 grep_source_clear(&gs);
384 return hit;
385 }
386 }
387
388 static int grep_file(struct grep_opt *opt, const char *filename)
389 {
390 struct strbuf buf = STRBUF_INIT;
391 struct grep_source gs;
392
393 grep_source_name(opt, filename, 0, &buf);
394 grep_source_init_file(&gs, buf.buf, filename);
395 strbuf_release(&buf);
396
397 if (num_threads > 1) {
398 /*
399 * add_work() copies gs and thus assumes ownership of
400 * its fields, so do not call grep_source_clear()
401 */
402 add_work(opt, &gs);
403 return 0;
404 } else {
405 int hit;
406
407 hit = grep_source(opt, &gs);
408
409 grep_source_clear(&gs);
410 return hit;
411 }
412 }
413
414 static void append_path(struct grep_opt *opt, const void *data, size_t len)
415 {
416 struct string_list *path_list = opt->output_priv;
417
418 if (len == 1 && *(const char *)data == '\0')
419 return;
420 string_list_append_nodup(path_list, xstrndup(data, len));
421 }
422
423 static void run_pager(struct grep_opt *opt, const char *prefix)
424 {
425 struct string_list *path_list = opt->output_priv;
426 struct child_process child = CHILD_PROCESS_INIT;
427 int i, status;
428
429 for (i = 0; i < path_list->nr; i++)
430 strvec_push(&child.args, path_list->items[i].string);
431 child.dir = prefix;
432 child.use_shell = 1;
433
434 status = run_command(&child);
435 if (status)
436 exit(status);
437 }
438
439 static int grep_cache(struct grep_opt *opt,
440 const struct pathspec *pathspec, int cached);
441 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
442 struct tree_desc *tree, struct strbuf *base, int tn_len,
443 int check_attr);
444
445 static int grep_submodule(struct grep_opt *opt,
446 const struct pathspec *pathspec,
447 const struct object_id *oid,
448 const char *filename, const char *path, int cached)
449 {
450 struct repository *subrepo;
451 struct repository *superproject = opt->repo;
452 struct grep_opt subopt;
453 int hit = 0;
454
455 if (!is_submodule_active(superproject, path))
456 return 0;
457
458 subrepo = xmalloc(sizeof(*subrepo));
459 if (repo_submodule_init(subrepo, superproject, path, null_oid(opt->repo->hash_algo))) {
460 free(subrepo);
461 return 0;
462 }
463 ALLOC_GROW(repos_to_free, repos_to_free_nr + 1, repos_to_free_alloc);
464 repos_to_free[repos_to_free_nr++] = subrepo;
465
466 /*
467 * NEEDSWORK: repo_read_gitmodules() might call
468 * odb_add_to_alternates_memory() via config_from_gitmodules(). This
469 * operation causes a race condition with concurrent object readings
470 * performed by the worker threads. That's why we need obj_read_lock()
471 * here. It should be removed once it's no longer necessary to add the
472 * subrepo's odbs to the in-memory alternates list.
473 */
474 obj_read_lock();
475
476 /*
477 * NEEDSWORK: when reading a submodule, the sparsity settings in the
478 * superproject are incorrectly forgotten or misused. For example:
479 *
480 * 1. "command_requires_full_index"
481 * When this setting is turned on for `grep`, only the superproject
482 * knows it. All the submodules are read with their own configs
483 * and get prepare_repo_settings()'d. Therefore, these submodules
484 * "forget" the sparse-index feature switch. As a result, the index
485 * of these submodules are expanded unexpectedly.
486 *
487 * 2. "config_values_private_.apply_sparse_checkout"
488 * When running `grep` in the superproject, this setting is
489 * populated using the superproject's configs. However, once
490 * initialized, this config is globally accessible and is read by
491 * prepare_repo_settings() for the submodules. For instance, if a
492 * submodule is using a sparse-checkout, however, the superproject
493 * is not, the result is that the config from the superproject will
494 * dictate the behavior for the submodule, making it "forget" its
495 * sparse-checkout state.
496 *
497 * 3. "core_sparse_checkout_cone"
498 * ditto.
499 *
500 * Note that this list is not exhaustive.
501 */
502 repo_read_gitmodules(subrepo, 0);
503
504 /*
505 * All code paths tested by test code no longer need submodule ODBs to
506 * be added as alternates, but add it to the list just in case.
507 * Submodule ODBs added through add_submodule_odb_by_path() will be
508 * lazily registered as alternates when needed (and except in an
509 * unexpected code interaction, it won't be needed).
510 */
511 odb_add_submodule_source_by_path(the_repository->objects,
512 subrepo->objects->sources->path);
513 obj_read_unlock();
514
515 memcpy(&subopt, opt, sizeof(subopt));
516 subopt.repo = subrepo;
517
518 if (oid) {
519 enum object_type object_type;
520 struct tree_desc tree;
521 void *data;
522 size_t size;
523 struct strbuf base = STRBUF_INIT;
524
525 obj_read_lock();
526 object_type = odb_read_object_info(subrepo->objects, oid, NULL);
527 obj_read_unlock();
528 data = odb_read_object_peeled(subrepo->objects, oid, OBJ_TREE, &size, NULL);
529 if (!data)
530 die(_("unable to read tree (%s)"), oid_to_hex(oid));
531
532 strbuf_addstr(&base, filename);
533 strbuf_addch(&base, '/');
534
535 init_tree_desc(&tree, oid, data, size);
536 hit = grep_tree(&subopt, pathspec, &tree, &base, base.len,
537 object_type == OBJ_COMMIT);
538 strbuf_release(&base);
539 free(data);
540 } else {
541 hit = grep_cache(&subopt, pathspec, cached);
542 }
543
544 return hit;
545 }
546
547 static int grep_cache(struct grep_opt *opt,
548 const struct pathspec *pathspec, int cached)
549 {
550 struct repository *repo = opt->repo;
551 int hit = 0;
552 int nr;
553 struct strbuf name = STRBUF_INIT;
554 int name_base_len = 0;
555 if (repo->submodule_prefix) {
556 name_base_len = strlen(repo->submodule_prefix);
557 strbuf_addstr(&name, repo->submodule_prefix);
558 }
559
560 if (repo_read_index(repo) < 0)
561 die(_("index file corrupt"));
562
563 for (nr = 0; nr < repo->index->cache_nr; nr++) {
564 const struct cache_entry *ce = repo->index->cache[nr];
565
566 if (!cached && ce_skip_worktree(ce))
567 continue;
568
569 strbuf_setlen(&name, name_base_len);
570 strbuf_addstr(&name, ce->name);
571 if (S_ISSPARSEDIR(ce->ce_mode)) {
572 enum object_type type;
573 struct tree_desc tree;
574 void *data;
575 size_t size;
576
577 data = odb_read_object(the_repository->objects, &ce->oid,
578 &type, &size);
579 if (!data)
580 die(_("unable to read tree %s"), oid_to_hex(&ce->oid));
581 init_tree_desc(&tree, &ce->oid, data, size);
582
583 hit |= grep_tree(opt, pathspec, &tree, &name, 0, 0);
584 strbuf_setlen(&name, name_base_len);
585 strbuf_addstr(&name, ce->name);
586 free(data);
587 } else if (S_ISREG(ce->ce_mode) &&
588 match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL,
589 S_ISDIR(ce->ce_mode) ||
590 S_ISGITLINK(ce->ce_mode))) {
591 /*
592 * If CE_VALID is on, we assume worktree file and its
593 * cache entry are identical, even if worktree file has
594 * been modified, so use cache version instead
595 */
596 if (cached || (ce->ce_flags & CE_VALID)) {
597 if (ce_stage(ce) || ce_intent_to_add(ce))
598 continue;
599 hit |= grep_oid(opt, &ce->oid, name.buf,
600 0, name.buf);
601 } else {
602 hit |= grep_file(opt, name.buf);
603 }
604 } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
605 submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
606 hit |= grep_submodule(opt, pathspec, NULL, ce->name,
607 ce->name, cached);
608 } else {
609 continue;
610 }
611
612 if (ce_stage(ce)) {
613 do {
614 nr++;
615 } while (nr < repo->index->cache_nr &&
616 !strcmp(ce->name, repo->index->cache[nr]->name));
617 nr--; /* compensate for loop control */
618 }
619 if (hit && opt->status_only)
620 break;
621 }
622
623 strbuf_release(&name);
624 return hit;
625 }
626
627 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
628 struct tree_desc *tree, struct strbuf *base, int tn_len,
629 int check_attr)
630 {
631 struct repository *repo = opt->repo;
632 int hit = 0;
633 enum interesting match = entry_not_interesting;
634 struct name_entry entry;
635 int old_baselen = base->len;
636 struct strbuf name = STRBUF_INIT;
637 int name_base_len = 0;
638 if (repo->submodule_prefix) {
639 strbuf_addstr(&name, repo->submodule_prefix);
640 name_base_len = name.len;
641 }
642
643 while (tree_entry(tree, &entry)) {
644 int te_len = tree_entry_len(&entry);
645
646 if (match != all_entries_interesting) {
647 strbuf_addstr(&name, base->buf + tn_len);
648 match = tree_entry_interesting(repo->index,
649 &entry, &name,
650 pathspec);
651 strbuf_setlen(&name, name_base_len);
652
653 if (match == all_entries_not_interesting)
654 break;
655 if (match == entry_not_interesting)
656 continue;
657 }
658
659 strbuf_add(base, entry.path, te_len);
660
661 if (S_ISREG(entry.mode)) {
662 hit |= grep_oid(opt, &entry.oid, base->buf, tn_len,
663 check_attr ? base->buf + tn_len : NULL);
664 } else if (S_ISDIR(entry.mode)) {
665 enum object_type type;
666 struct tree_desc sub;
667 void *data;
668 size_t size;
669
670 data = odb_read_object(the_repository->objects,
671 &entry.oid, &type, &size);
672 if (!data)
673 die(_("unable to read tree (%s)"),
674 oid_to_hex(&entry.oid));
675
676 strbuf_addch(base, '/');
677 init_tree_desc(&sub, &entry.oid, data, size);
678 hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
679 check_attr);
680 free(data);
681 } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
682 hit |= grep_submodule(opt, pathspec, &entry.oid,
683 base->buf, base->buf + tn_len,
684 1); /* ignored */
685 }
686
687 strbuf_setlen(base, old_baselen);
688
689 if (hit && opt->status_only)
690 break;
691 }
692
693 strbuf_release(&name);
694 return hit;
695 }
696
697 static void collect_blob_oids_for_tree(struct repository *repo,
698 const struct pathspec *pathspec,
699 struct tree_desc *tree,
700 struct strbuf *base,
701 int tn_len,
702 struct oidset *blob_oids)
703 {
704 struct name_entry entry;
705 int old_baselen = base->len;
706 struct strbuf name = STRBUF_INIT;
707 enum interesting match = entry_not_interesting;
708
709 while (tree_entry(tree, &entry)) {
710 if (match != all_entries_interesting) {
711 strbuf_addstr(&name, base->buf + tn_len);
712 match = tree_entry_interesting(repo->index,
713 &entry, &name,
714 pathspec);
715 strbuf_reset(&name);
716
717 if (match == all_entries_not_interesting)
718 break;
719 if (match == entry_not_interesting)
720 continue;
721 }
722
723 strbuf_add(base, entry.path, tree_entry_len(&entry));
724
725 if (S_ISREG(entry.mode)) {
726 if (!odb_has_object(repo->objects, &entry.oid, 0))
727 oidset_insert(blob_oids, &entry.oid);
728 } else if (S_ISDIR(entry.mode)) {
729 enum object_type type;
730 struct tree_desc sub_tree;
731 void *data;
732 size_t size;
733
734 data = odb_read_object(repo->objects, &entry.oid,
735 &type, &size);
736 if (!data)
737 die(_("unable to read tree (%s)"),
738 oid_to_hex(&entry.oid));
739
740 strbuf_addch(base, '/');
741 init_tree_desc(&sub_tree, &entry.oid, data, size);
742 collect_blob_oids_for_tree(repo, pathspec, &sub_tree,
743 base, tn_len, blob_oids);
744 free(data);
745 }
746 /*
747 * ...no else clause for S_ISGITLINK: submodules have their
748 * own promisor configuration and would need separate fetches
749 * anyway.
750 */
751
752 strbuf_setlen(base, old_baselen);
753 }
754
755 strbuf_release(&name);
756 }
757
758 static void collect_blob_oids_for_treeish(struct grep_opt *opt,
759 const struct pathspec *pathspec,
760 const struct object_id *tree_ish_oid,
761 const char *name,
762 struct oidset *blob_oids)
763 {
764 struct tree_desc tree;
765 void *data;
766 size_t size;
767 struct strbuf base = STRBUF_INIT;
768 int len;
769
770 data = odb_read_object_peeled(opt->repo->objects, tree_ish_oid,
771 OBJ_TREE, &size, NULL);
772
773 if (!data)
774 return;
775
776 len = name ? strlen(name) : 0;
777 if (len) {
778 strbuf_add(&base, name, len);
779 strbuf_addch(&base, ':');
780 }
781 init_tree_desc(&tree, tree_ish_oid, data, size);
782
783 collect_blob_oids_for_tree(opt->repo, pathspec, &tree,
784 &base, base.len, blob_oids);
785
786 strbuf_release(&base);
787 free(data);
788 }
789
790 static void prefetch_grep_blobs(struct grep_opt *opt,
791 const struct pathspec *pathspec,
792 const struct object_array *list)
793 {
794 struct oidset blob_oids = OIDSET_INIT;
795
796 /* Exit if we're not in a partial clone */
797 if (!repo_has_promisor_remote(opt->repo))
798 return;
799
800 /* For each tree, gather the blobs in it */
801 for (int i = 0; i < list->nr; i++) {
802 struct object *real_obj;
803
804 obj_read_lock();
805 real_obj = deref_tag(opt->repo, list->objects[i].item,
806 NULL, 0);
807 obj_read_unlock();
808
809 if (real_obj &&
810 (real_obj->type == OBJ_COMMIT ||
811 real_obj->type == OBJ_TREE))
812 collect_blob_oids_for_treeish(opt, pathspec,
813 &real_obj->oid,
814 list->objects[i].name,
815 &blob_oids);
816 }
817
818 /* Prefetch the blobs we found */
819 if (oidset_size(&blob_oids)) {
820 struct oid_array to_fetch = OID_ARRAY_INIT;
821 struct oidset_iter iter;
822 const struct object_id *oid;
823
824 oidset_iter_init(&blob_oids, &iter);
825 while ((oid = oidset_iter_next(&iter)))
826 oid_array_append(&to_fetch, oid);
827
828 promisor_remote_get_direct(opt->repo, to_fetch.oid, to_fetch.nr);
829
830 oid_array_clear(&to_fetch);
831 }
832 oidset_clear(&blob_oids);
833 }
834
835 static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
836 struct object *obj, const char *name, const char *path)
837 {
838 if (obj->type == OBJ_BLOB)
839 return grep_oid(opt, &obj->oid, name, 0, path);
840 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
841 struct tree_desc tree;
842 void *data;
843 size_t size;
844 struct strbuf base;
845 int hit, len;
846
847 data = odb_read_object_peeled(opt->repo->objects, &obj->oid,
848 OBJ_TREE, &size, NULL);
849 if (!data)
850 die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
851
852 len = name ? strlen(name) : 0;
853 strbuf_init(&base, PATH_MAX + len + 1);
854 if (len) {
855 strbuf_add(&base, name, len);
856 strbuf_addch(&base, ':');
857 }
858 init_tree_desc(&tree, &obj->oid, data, size);
859 hit = grep_tree(opt, pathspec, &tree, &base, base.len,
860 obj->type == OBJ_COMMIT);
861 strbuf_release(&base);
862 free(data);
863 return hit;
864 }
865 die(_("unable to grep from object of type %s"), type_name(obj->type));
866 }
867
868 static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
869 const struct object_array *list)
870 {
871 unsigned int i;
872 int hit = 0;
873 const unsigned int nr = list->nr;
874
875 prefetch_grep_blobs(opt, pathspec, list);
876
877 for (i = 0; i < nr; i++) {
878 struct object *real_obj;
879
880 obj_read_lock();
881 real_obj = deref_tag(opt->repo, list->objects[i].item,
882 NULL, 0);
883 obj_read_unlock();
884
885 if (!real_obj) {
886 char hex[GIT_MAX_HEXSZ + 1];
887 const char *name = list->objects[i].name;
888
889 if (!name) {
890 oid_to_hex_r(hex, &list->objects[i].item->oid);
891 name = hex;
892 }
893 die(_("invalid object '%s' given."), name);
894 }
895
896 /* load the gitmodules file for this rev */
897 if (recurse_submodules) {
898 submodule_free(opt->repo);
899 obj_read_lock();
900 gitmodules_config_oid(&real_obj->oid);
901 obj_read_unlock();
902 }
903 if (grep_object(opt, pathspec, real_obj, list->objects[i].name,
904 list->objects[i].path)) {
905 hit = 1;
906 if (opt->status_only)
907 break;
908 }
909 }
910 return hit;
911 }
912
913 static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
914 int exc_std, int use_index)
915 {
916 struct dir_struct dir = DIR_INIT;
917 int i, hit = 0;
918
919 if (!use_index)
920 dir.flags |= DIR_NO_GITLINKS;
921 if (exc_std)
922 setup_standard_excludes(&dir);
923
924 fill_directory(&dir, opt->repo->index, pathspec);
925 for (i = 0; i < dir.nr; i++) {
926 hit |= grep_file(opt, dir.entries[i]->name);
927 if (hit && opt->status_only)
928 break;
929 }
930 dir_clear(&dir);
931 return hit;
932 }
933
934 static int context_callback(const struct option *opt, const char *arg,
935 int unset)
936 {
937 struct grep_opt *grep_opt = opt->value;
938 int value;
939 const char *endp;
940
941 if (unset) {
942 grep_opt->pre_context = grep_opt->post_context = 0;
943 return 0;
944 }
945 value = strtol(arg, (char **)&endp, 10);
946 if (*endp) {
947 return error(_("switch `%c' expects a numerical value"),
948 opt->short_name);
949 }
950 grep_opt->pre_context = grep_opt->post_context = value;
951 return 0;
952 }
953
954 static int file_callback(const struct option *opt, const char *arg, int unset)
955 {
956 struct grep_opt *grep_opt = opt->value;
957 int from_stdin;
958 const char *filename = arg;
959 FILE *patterns;
960 int lno = 0;
961 struct strbuf sb = STRBUF_INIT;
962
963 BUG_ON_OPT_NEG(unset);
964
965 if (!*filename)
966 ; /* leave it as-is */
967 else
968 filename = prefix_filename_except_for_dash(grep_prefix, filename);
969
970 from_stdin = !strcmp(filename, "-");
971 patterns = from_stdin ? stdin : fopen(filename, "r");
972 if (!patterns)
973 die_errno(_("cannot open '%s'"), arg);
974 while (strbuf_getline(&sb, patterns) == 0) {
975 /* ignore empty line like grep does */
976 if (sb.len == 0)
977 continue;
978
979 append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno,
980 GREP_PATTERN);
981 }
982 if (!from_stdin)
983 fclose(patterns);
984 strbuf_release(&sb);
985 if (filename != arg)
986 free((void *)filename);
987 return 0;
988 }
989
990 static int not_callback(const struct option *opt, const char *arg, int unset)
991 {
992 struct grep_opt *grep_opt = opt->value;
993 BUG_ON_OPT_NEG(unset);
994 BUG_ON_OPT_ARG(arg);
995 append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
996 return 0;
997 }
998
999 static int and_callback(const struct option *opt, const char *arg, int unset)
1000 {
1001 struct grep_opt *grep_opt = opt->value;
1002 BUG_ON_OPT_NEG(unset);
1003 BUG_ON_OPT_ARG(arg);
1004 append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
1005 return 0;
1006 }
1007
1008 static int open_callback(const struct option *opt, const char *arg, int unset)
1009 {
1010 struct grep_opt *grep_opt = opt->value;
1011 BUG_ON_OPT_NEG(unset);
1012 BUG_ON_OPT_ARG(arg);
1013 append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
1014 return 0;
1015 }
1016
1017 static int close_callback(const struct option *opt, const char *arg, int unset)
1018 {
1019 struct grep_opt *grep_opt = opt->value;
1020 BUG_ON_OPT_NEG(unset);
1021 BUG_ON_OPT_ARG(arg);
1022 append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
1023 return 0;
1024 }
1025
1026 static int pattern_callback(const struct option *opt, const char *arg,
1027 int unset)
1028 {
1029 struct grep_opt *grep_opt = opt->value;
1030 BUG_ON_OPT_NEG(unset);
1031 append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
1032 return 0;
1033 }
1034
1035 int cmd_grep(int argc,
1036 const char **argv,
1037 const char *prefix,
1038 struct repository *repo UNUSED)
1039 {
1040 int hit = 0;
1041 int cached = 0, untracked = 0, opt_exclude = -1;
1042 int seen_dashdash = 0;
1043 int external_grep_allowed__ignored;
1044 const char *show_in_pager = NULL, *default_pager = "dummy";
1045 struct grep_opt opt;
1046 struct object_array list = OBJECT_ARRAY_INIT;
1047 struct pathspec pathspec;
1048 struct string_list path_list = STRING_LIST_INIT_DUP;
1049 int i;
1050 int dummy;
1051 int use_index = 1;
1052 int allow_revs;
1053 int ret;
1054
1055 struct option options[] = {
1056 OPT_BOOL(0, "cached", &cached,
1057 N_("search in index instead of in the work tree")),
1058 OPT_NEGBIT(0, "no-index", &use_index,
1059 N_("find in contents not managed by git"), 1),
1060 OPT_BOOL(0, "untracked", &untracked,
1061 N_("search in both tracked and untracked files")),
1062 OPT_SET_INT(0, "exclude-standard", &opt_exclude,
1063 N_("ignore files specified via '.gitignore'"), 1),
1064 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
1065 N_("recursively search in each submodule")),
1066 OPT_GROUP(""),
1067 OPT_BOOL('v', "invert-match", &opt.invert,
1068 N_("show non-matching lines")),
1069 OPT_BOOL('i', "ignore-case", &opt.ignore_case,
1070 N_("case insensitive matching")),
1071 OPT_BOOL('w', "word-regexp", &opt.word_regexp,
1072 N_("match patterns only at word boundaries")),
1073 OPT_SET_INT('a', "text", &opt.binary,
1074 N_("process binary files as text"), GREP_BINARY_TEXT),
1075 OPT_SET_INT('I', NULL, &opt.binary,
1076 N_("don't match patterns in binary files"),
1077 GREP_BINARY_NOMATCH),
1078 OPT_BOOL(0, "textconv", &opt.allow_textconv,
1079 N_("process binary files with textconv filters")),
1080 OPT_SET_INT('r', "recursive", &opt.max_depth,
1081 N_("search in subdirectories (default)"), -1),
1082 OPT_INTEGER_F(0, "max-depth", &opt.max_depth,
1083 N_("descend at most <n> levels"), PARSE_OPT_NONEG),
1084 OPT_GROUP(""),
1085 OPT_SET_INT('E', "extended-regexp", &opt.pattern_type_option,
1086 N_("use extended POSIX regular expressions"),
1087 GREP_PATTERN_TYPE_ERE),
1088 OPT_SET_INT('G', "basic-regexp", &opt.pattern_type_option,
1089 N_("use basic POSIX regular expressions (default)"),
1090 GREP_PATTERN_TYPE_BRE),
1091 OPT_SET_INT('F', "fixed-strings", &opt.pattern_type_option,
1092 N_("interpret patterns as fixed strings"),
1093 GREP_PATTERN_TYPE_FIXED),
1094 OPT_SET_INT('P', "perl-regexp", &opt.pattern_type_option,
1095 N_("use Perl-compatible regular expressions"),
1096 GREP_PATTERN_TYPE_PCRE),
1097 OPT_GROUP(""),
1098 OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")),
1099 OPT_BOOL(0, "column", &opt.columnnum, N_("show column number of first match")),
1100 OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1),
1101 OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1),
1102 OPT_NEGBIT(0, "full-name", &opt.relative,
1103 N_("show filenames relative to top directory"), 1),
1104 OPT_BOOL('l', "files-with-matches", &opt.name_only,
1105 N_("show only filenames instead of matching lines")),
1106 OPT_BOOL(0, "name-only", &opt.name_only,
1107 N_("synonym for --files-with-matches")),
1108 OPT_BOOL('L', "files-without-match",
1109 &opt.unmatch_name_only,
1110 N_("show only the names of files without match")),
1111 OPT_BOOL_F('z', "null", &opt.null_following_name,
1112 N_("print NUL after filenames"),
1113 PARSE_OPT_NOCOMPLETE),
1114 OPT_BOOL('o', "only-matching", &opt.only_matching,
1115 N_("show only matching parts of a line")),
1116 OPT_BOOL('c', "count", &opt.count,
1117 N_("show the number of matches instead of matching lines")),
1118 OPT__COLOR(&opt.color, N_("highlight matches")),
1119 OPT_BOOL(0, "break", &opt.file_break,
1120 N_("print empty line between matches from different files")),
1121 OPT_BOOL(0, "heading", &opt.heading,
1122 N_("show filename only once above matches from same file")),
1123 OPT_GROUP(""),
1124 OPT_CALLBACK('C', "context", &opt, N_("n"),
1125 N_("show <n> context lines before and after matches"),
1126 context_callback),
1127 OPT_UNSIGNED('B', "before-context", &opt.pre_context,
1128 N_("show <n> context lines before matches")),
1129 OPT_UNSIGNED('A', "after-context", &opt.post_context,
1130 N_("show <n> context lines after matches")),
1131 OPT_INTEGER(0, "threads", &num_threads,
1132 N_("use <n> worker threads")),
1133 OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"),
1134 context_callback),
1135 OPT_BOOL('p', "show-function", &opt.funcname,
1136 N_("show a line with the function name before matches")),
1137 OPT_BOOL('W', "function-context", &opt.funcbody,
1138 N_("show the surrounding function")),
1139 OPT_GROUP(""),
1140 OPT_CALLBACK('f', NULL, &opt, N_("file"),
1141 N_("read patterns from file"), file_callback),
1142 OPT_CALLBACK_F('e', NULL, &opt, N_("pattern"),
1143 N_("match <pattern>"), PARSE_OPT_NONEG, pattern_callback),
1144 OPT_CALLBACK_F(0, "and", &opt, NULL,
1145 N_("combine patterns specified with -e"),
1146 PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback),
1147 OPT_BOOL_F(0, "or", &dummy, "", PARSE_OPT_NONEG),
1148 OPT_CALLBACK_F(0, "not", &opt, NULL, "",
1149 PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback),
1150 OPT_CALLBACK_F('(', NULL, &opt, NULL, "",
1151 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
1152 open_callback),
1153 OPT_CALLBACK_F(')', NULL, &opt, NULL, "",
1154 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
1155 close_callback),
1156 OPT__QUIET(&opt.status_only,
1157 N_("indicate hit with exit status without output")),
1158 OPT_BOOL(0, "all-match", &opt.all_match,
1159 N_("show only matches from files that match all patterns")),
1160 OPT_GROUP(""),
1161 {
1162 .type = OPTION_STRING,
1163 .short_name = 'O',
1164 .long_name = "open-files-in-pager",
1165 .value = &show_in_pager,
1166 .argh = N_("pager"),
1167 .help = N_("show matching files in the pager"),
1168 .flags = PARSE_OPT_OPTARG | PARSE_OPT_NOCOMPLETE,
1169 .defval = (intptr_t)default_pager,
1170 },
1171 OPT_BOOL_F(0, "ext-grep", &external_grep_allowed__ignored,
1172 N_("allow calling of grep(1) (ignored by this build)"),
1173 PARSE_OPT_NOCOMPLETE),
1174 OPT_INTEGER('m', "max-count", &opt.max_count,
1175 N_("maximum number of results per file")),
1176 OPT_END()
1177 };
1178 grep_prefix = prefix;
1179
1180 grep_init(&opt, the_repository);
1181 repo_config(the_repository, grep_cmd_config, &opt);
1182
1183 /*
1184 * If there is no -- then the paths must exist in the working
1185 * tree. If there is no explicit pattern specified with -e or
1186 * -f, we take the first unrecognized non option to be the
1187 * pattern, but then what follows it must be zero or more
1188 * valid refs up to the -- (if exists), and then existing
1189 * paths. If there is an explicit pattern, then the first
1190 * unrecognized non option is the beginning of the refs list
1191 * that continues up to the -- (if exists), and then paths.
1192 */
1193 argc = parse_options(argc, argv, prefix, options, grep_usage,
1194 PARSE_OPT_KEEP_DASHDASH |
1195 PARSE_OPT_STOP_AT_NON_OPTION);
1196
1197 if (the_repository->gitdir) {
1198 prepare_repo_settings(the_repository);
1199 the_repository->settings.command_requires_full_index = 0;
1200 }
1201
1202 if (use_index && !startup_info->have_repository) {
1203 int fallback = 0;
1204 repo_config_get_bool(the_repository, "grep.fallbacktonoindex", &fallback);
1205 if (fallback)
1206 use_index = 0;
1207 else
1208 /* die the same way as if we did it at the beginning */
1209 setup_git_directory(the_repository);
1210 }
1211 /* Ignore --recurse-submodules if --no-index is given or implied */
1212 if (!use_index)
1213 recurse_submodules = 0;
1214
1215 /*
1216 * skip a -- separator; we know it cannot be
1217 * separating revisions from pathnames if
1218 * we haven't even had any patterns yet
1219 */
1220 if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) {
1221 argv++;
1222 argc--;
1223 }
1224
1225 /* First unrecognized non-option token */
1226 if (argc > 0 && !opt.pattern_list) {
1227 append_grep_pattern(&opt, argv[0], "command line", 0,
1228 GREP_PATTERN);
1229 argv++;
1230 argc--;
1231 }
1232
1233 if (show_in_pager == default_pager)
1234 show_in_pager = git_pager(the_repository, 1);
1235 if (show_in_pager) {
1236 opt.color = GIT_COLOR_NEVER;
1237 opt.name_only = 1;
1238 opt.null_following_name = 1;
1239 opt.output_priv = &path_list;
1240 opt.output = append_path;
1241 string_list_append(&path_list, show_in_pager);
1242 }
1243
1244 if (!opt.pattern_list)
1245 die(_("no pattern given"));
1246
1247 /* --only-matching has no effect with --invert. */
1248 if (opt.invert)
1249 opt.only_matching = 0;
1250
1251 /*
1252 * We have to find "--" in a separate pass, because its presence
1253 * influences how we will parse arguments that come before it.
1254 */
1255 for (i = 0; i < argc; i++) {
1256 if (!strcmp(argv[i], "--")) {
1257 seen_dashdash = 1;
1258 break;
1259 }
1260 }
1261
1262 /*
1263 * Resolve any rev arguments. If we have a dashdash, then everything up
1264 * to it must resolve as a rev. If not, then we stop at the first
1265 * non-rev and assume everything else is a path.
1266 */
1267 allow_revs = use_index && !untracked;
1268 for (i = 0; i < argc; i++) {
1269 const char *arg = argv[i];
1270 struct object_id oid;
1271 struct object_context oc = {0};
1272 struct object *object;
1273
1274 if (!strcmp(arg, "--")) {
1275 i++;
1276 break;
1277 }
1278
1279 if (!allow_revs) {
1280 if (seen_dashdash)
1281 die(_("--no-index or --untracked cannot be used with revs"));
1282 break;
1283 }
1284
1285 if (get_oid_with_context(the_repository, arg,
1286 GET_OID_RECORD_PATH,
1287 &oid, &oc)) {
1288 if (seen_dashdash)
1289 die(_("unable to resolve revision: %s"), arg);
1290 object_context_release(&oc);
1291 break;
1292 }
1293
1294 object = parse_object_or_die(the_repository, &oid, arg);
1295 if (!seen_dashdash)
1296 verify_non_filename(the_repository, prefix, arg);
1297 add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
1298 object_context_release(&oc);
1299 }
1300
1301 /*
1302 * Anything left over is presumed to be a path. But in the non-dashdash
1303 * "do what I mean" case, we verify and complain when that isn't true.
1304 */
1305 if (!seen_dashdash) {
1306 int j;
1307 for (j = i; j < argc; j++)
1308 verify_filename(the_repository, prefix, argv[j], j == i && allow_revs);
1309 }
1310
1311 parse_pathspec(&pathspec, 0,
1312 PATHSPEC_PREFER_CWD |
1313 (opt.max_depth != -1 ? PATHSPEC_MAXDEPTH_VALID : 0),
1314 prefix, argv + i);
1315 pathspec.max_depth = opt.max_depth;
1316 pathspec.recursive = 1;
1317 pathspec.recurse_submodules = !!recurse_submodules;
1318
1319 if (recurse_submodules && untracked)
1320 die(_("--untracked not supported with --recurse-submodules"));
1321
1322 /*
1323 * Optimize out the case where the amount of matches is limited to zero.
1324 * We do this to keep results consistent with GNU grep(1).
1325 */
1326 if (opt.max_count == 0) {
1327 ret = 1;
1328 goto out;
1329 }
1330
1331 if (show_in_pager) {
1332 if (num_threads > 1)
1333 warning(_("invalid option combination, ignoring --threads"));
1334 num_threads = 1;
1335 } else if (!HAVE_THREADS && num_threads > 1) {
1336 warning(_("no threads support, ignoring --threads"));
1337 num_threads = 1;
1338 } else if (num_threads < 0)
1339 die(_("invalid number of threads specified (%d)"), num_threads);
1340 else if (num_threads == 0)
1341 num_threads = HAVE_THREADS ? online_cpus() : 1;
1342
1343 if (num_threads > 1) {
1344 if (!HAVE_THREADS)
1345 BUG("Somebody got num_threads calculation wrong!");
1346 if (!(opt.name_only || opt.unmatch_name_only || opt.count)
1347 && (opt.pre_context || opt.post_context ||
1348 opt.file_break || opt.funcbody))
1349 skip_first_line = 1;
1350
1351 /*
1352 * Pre-read gitmodules (if not read already) and force eager
1353 * initialization of packed_git to prevent racy lazy
1354 * reading/initialization once worker threads are started.
1355 */
1356 if (recurse_submodules)
1357 repo_read_gitmodules(the_repository, 1);
1358
1359 if (startup_info->have_repository)
1360 odb_prepare(the_repository->objects, 0);
1361
1362 start_threads(&opt);
1363 } else {
1364 /*
1365 * The compiled patterns on the main path are only
1366 * used when not using threading. Otherwise
1367 * start_threads() above calls compile_grep_patterns()
1368 * for each thread.
1369 */
1370 compile_grep_patterns(&opt);
1371 }
1372
1373 if (show_in_pager && (cached || list.nr))
1374 die(_("--open-files-in-pager only works on the worktree"));
1375
1376 if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
1377 const char *pager = path_list.items[0].string;
1378 int len = strlen(pager);
1379
1380 if (len > 4 && is_dir_sep(pager[len - 5]))
1381 pager += len - 4;
1382
1383 if (opt.ignore_case && !strcmp("less", pager))
1384 string_list_append(&path_list, "-I");
1385
1386 if (!strcmp("less", pager) || !strcmp("vi", pager)) {
1387 struct strbuf buf = STRBUF_INIT;
1388 strbuf_addf(&buf, "+/%s%s",
1389 strcmp("less", pager) ? "" : "*",
1390 opt.pattern_list->pattern);
1391 string_list_append_nodup(&path_list,
1392 strbuf_detach(&buf, NULL));
1393 }
1394 }
1395
1396 if (!show_in_pager && !opt.status_only)
1397 setup_pager(the_repository);
1398
1399 die_for_incompatible_opt3(!use_index, "--no-index",
1400 untracked, "--untracked",
1401 cached, "--cached");
1402
1403 if (!use_index || untracked) {
1404 int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
1405 hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
1406 } else if (0 <= opt_exclude) {
1407 die(_("--[no-]exclude-standard cannot be used for tracked contents"));
1408 } else if (!list.nr) {
1409 if (!cached)
1410 setup_work_tree(the_repository);
1411
1412 hit = grep_cache(&opt, &pathspec, cached);
1413 } else {
1414 if (cached)
1415 die(_("both --cached and trees are given"));
1416
1417 hit = grep_objects(&opt, &pathspec, &list);
1418 }
1419
1420 if (num_threads > 1)
1421 hit |= wait_all();
1422 if (hit && show_in_pager)
1423 run_pager(&opt, prefix);
1424
1425 ret = !hit;
1426
1427 out:
1428 clear_pathspec(&pathspec);
1429 string_list_clear(&path_list, 0);
1430 free_grep_patterns(&opt);
1431 object_array_clear(&list);
1432 free_repos();
1433 return ret;
1434 }