Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/grep.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / grep.c` 1420 lines ยท 38565 bytes Raw Blame History 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 [] [-e] [...] [[--] ...]"), 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: when reading a submodule, the sparsity settings in the 468 * superproject are incorrectly forgotten or misused. For example: 469 * 470 * 1. "command_requires_full_index" 471 * When this setting is turned on for `grep`, only the superproject 472 * knows it. All the submodules are read with their own configs 473 * and get prepare_repo_settings()'d. Therefore, these submodules 474 * "forget" the sparse-index feature switch. As a result, the index 475 * of these submodules are expanded unexpectedly. 476 * 477 * 2. "config_values_private_.apply_sparse_checkout" 478 * When running `grep` in the superproject, this setting is 479 * populated using the superproject's configs. However, once 480 * initialized, this config is globally accessible and is read by 481 * prepare_repo_settings() for the submodules. For instance, if a 482 * submodule is using a sparse-checkout, however, the superproject 483 * is not, the result is that the config from the superproject will 484 * dictate the behavior for the submodule, making it "forget" its 485 * sparse-checkout state. 486 * 487 * 3. "core_sparse_checkout_cone" 488 * ditto. 489 * 490 * Note that this list is not exhaustive. 491 * 492 * NEEDSWORK: initializing the subrepository is not thread-safe, 493 * either, as it may cause us to race around `get_main_ref_store()`. We 494 * thus need to hold the object-read lock to serialize all readers with 495 * one another. 496 */ 497 obj_read_lock(); 498 repo_read_gitmodules(subrepo, 0); 499 obj_read_unlock(); 500 501 memcpy(&subopt, opt, sizeof(subopt)); 502 subopt.repo = subrepo; 503 504 if (oid) { 505 enum object_type object_type; 506 struct tree_desc tree; 507 void *data; 508 size_t size; 509 struct strbuf base = STRBUF_INIT; 510 511 obj_read_lock(); 512 object_type = odb_read_object_info(subrepo->objects, oid, NULL); 513 obj_read_unlock(); 514 data = odb_read_object_peeled(subrepo->objects, oid, OBJ_TREE, &size, NULL); 515 if (!data) 516 die(_("unable to read tree (%s)"), oid_to_hex(oid)); 517 518 strbuf_addstr(&base, filename); 519 strbuf_addch(&base, '/'); 520 521 init_tree_desc(&tree, oid, data, size); 522 hit = grep_tree(&subopt, pathspec, &tree, &base, base.len, 523 object_type == OBJ_COMMIT); 524 strbuf_release(&base); 525 free(data); 526 } else { 527 hit = grep_cache(&subopt, pathspec, cached); 528 } 529 530 return hit; 531 } 532 533 static int grep_cache(struct grep_opt *opt, 534 const struct pathspec *pathspec, int cached) 535 { 536 struct repository *repo = opt->repo; 537 int hit = 0; 538 int nr; 539 struct strbuf name = STRBUF_INIT; 540 int name_base_len = 0; 541 if (repo->submodule_prefix) { 542 name_base_len = strlen(repo->submodule_prefix); 543 strbuf_addstr(&name, repo->submodule_prefix); 544 } 545 546 if (repo_read_index(repo) < 0) 547 die(_("index file corrupt")); 548 549 for (nr = 0; nr < repo->index->cache_nr; nr++) { 550 const struct cache_entry *ce = repo->index->cache[nr]; 551 552 if (!cached && ce_skip_worktree(ce)) 553 continue; 554 555 strbuf_setlen(&name, name_base_len); 556 strbuf_addstr(&name, ce->name); 557 if (S_ISSPARSEDIR(ce->ce_mode)) { 558 enum object_type type; 559 struct tree_desc tree; 560 void *data; 561 size_t size; 562 563 data = odb_read_object(the_repository->objects, &ce->oid, 564 &type, &size); 565 if (!data) 566 die(_("unable to read tree %s"), oid_to_hex(&ce->oid)); 567 init_tree_desc(&tree, &ce->oid, data, size); 568 569 hit |= grep_tree(opt, pathspec, &tree, &name, 0, 0); 570 strbuf_setlen(&name, name_base_len); 571 strbuf_addstr(&name, ce->name); 572 free(data); 573 } else if (S_ISREG(ce->ce_mode) && 574 match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL, 575 S_ISDIR(ce->ce_mode) || 576 S_ISGITLINK(ce->ce_mode))) { 577 /* 578 * If CE_VALID is on, we assume worktree file and its 579 * cache entry are identical, even if worktree file has 580 * been modified, so use cache version instead 581 */ 582 if (cached || (ce->ce_flags & CE_VALID)) { 583 if (ce_stage(ce) || ce_intent_to_add(ce)) 584 continue; 585 hit |= grep_oid(opt, &ce->oid, name.buf, 586 0, name.buf); 587 } else { 588 hit |= grep_file(opt, name.buf); 589 } 590 } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && 591 submodule_path_match(repo->index, pathspec, name.buf, NULL)) { 592 hit |= grep_submodule(opt, pathspec, NULL, ce->name, 593 ce->name, cached); 594 } else { 595 continue; 596 } 597 598 if (ce_stage(ce)) { 599 do { 600 nr++; 601 } while (nr < repo->index->cache_nr && 602 !strcmp(ce->name, repo->index->cache[nr]->name)); 603 nr--; /* compensate for loop control */ 604 } 605 if (hit && opt->status_only) 606 break; 607 } 608 609 strbuf_release(&name); 610 return hit; 611 } 612 613 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, 614 struct tree_desc *tree, struct strbuf *base, int tn_len, 615 int check_attr) 616 { 617 struct repository *repo = opt->repo; 618 int hit = 0; 619 enum interesting match = entry_not_interesting; 620 struct name_entry entry; 621 int old_baselen = base->len; 622 struct strbuf name = STRBUF_INIT; 623 int name_base_len = 0; 624 if (repo->submodule_prefix) { 625 strbuf_addstr(&name, repo->submodule_prefix); 626 name_base_len = name.len; 627 } 628 629 while (tree_entry(tree, &entry)) { 630 int te_len = tree_entry_len(&entry); 631 632 if (match != all_entries_interesting) { 633 strbuf_addstr(&name, base->buf + tn_len); 634 match = tree_entry_interesting(repo->index, 635 &entry, &name, 636 pathspec); 637 strbuf_setlen(&name, name_base_len); 638 639 if (match == all_entries_not_interesting) 640 break; 641 if (match == entry_not_interesting) 642 continue; 643 } 644 645 strbuf_add(base, entry.path, te_len); 646 647 if (S_ISREG(entry.mode)) { 648 hit |= grep_oid(opt, &entry.oid, base->buf, tn_len, 649 check_attr ? base->buf + tn_len : NULL); 650 } else if (S_ISDIR(entry.mode)) { 651 enum object_type type; 652 struct tree_desc sub; 653 void *data; 654 size_t size; 655 656 data = odb_read_object(the_repository->objects, 657 &entry.oid, &type, &size); 658 if (!data) 659 die(_("unable to read tree (%s)"), 660 oid_to_hex(&entry.oid)); 661 662 strbuf_addch(base, '/'); 663 init_tree_desc(&sub, &entry.oid, data, size); 664 hit |= grep_tree(opt, pathspec, &sub, base, tn_len, 665 check_attr); 666 free(data); 667 } else if (recurse_submodules && S_ISGITLINK(entry.mode)) { 668 hit |= grep_submodule(opt, pathspec, &entry.oid, 669 base->buf, base->buf + tn_len, 670 1); /* ignored */ 671 } 672 673 strbuf_setlen(base, old_baselen); 674 675 if (hit && opt->status_only) 676 break; 677 } 678 679 strbuf_release(&name); 680 return hit; 681 } 682 683 static void collect_blob_oids_for_tree(struct repository *repo, 684 const struct pathspec *pathspec, 685 struct tree_desc *tree, 686 struct strbuf *base, 687 int tn_len, 688 struct oidset *blob_oids) 689 { 690 struct name_entry entry; 691 int old_baselen = base->len; 692 struct strbuf name = STRBUF_INIT; 693 enum interesting match = entry_not_interesting; 694 695 while (tree_entry(tree, &entry)) { 696 if (match != all_entries_interesting) { 697 strbuf_addstr(&name, base->buf + tn_len); 698 match = tree_entry_interesting(repo->index, 699 &entry, &name, 700 pathspec); 701 strbuf_reset(&name); 702 703 if (match == all_entries_not_interesting) 704 break; 705 if (match == entry_not_interesting) 706 continue; 707 } 708 709 strbuf_add(base, entry.path, tree_entry_len(&entry)); 710 711 if (S_ISREG(entry.mode)) { 712 if (!odb_has_object(repo->objects, &entry.oid, 0)) 713 oidset_insert(blob_oids, &entry.oid); 714 } else if (S_ISDIR(entry.mode)) { 715 enum object_type type; 716 struct tree_desc sub_tree; 717 void *data; 718 size_t size; 719 720 data = odb_read_object(repo->objects, &entry.oid, 721 &type, &size); 722 if (!data) 723 die(_("unable to read tree (%s)"), 724 oid_to_hex(&entry.oid)); 725 726 strbuf_addch(base, '/'); 727 init_tree_desc(&sub_tree, &entry.oid, data, size); 728 collect_blob_oids_for_tree(repo, pathspec, &sub_tree, 729 base, tn_len, blob_oids); 730 free(data); 731 } 732 /* 733 * ...no else clause for S_ISGITLINK: submodules have their 734 * own promisor configuration and would need separate fetches 735 * anyway. 736 */ 737 738 strbuf_setlen(base, old_baselen); 739 } 740 741 strbuf_release(&name); 742 } 743 744 static void collect_blob_oids_for_treeish(struct grep_opt *opt, 745 const struct pathspec *pathspec, 746 const struct object_id *tree_ish_oid, 747 const char *name, 748 struct oidset *blob_oids) 749 { 750 struct tree_desc tree; 751 void *data; 752 size_t size; 753 struct strbuf base = STRBUF_INIT; 754 int len; 755 756 data = odb_read_object_peeled(opt->repo->objects, tree_ish_oid, 757 OBJ_TREE, &size, NULL); 758 759 if (!data) 760 return; 761 762 len = name ? strlen(name) : 0; 763 if (len) { 764 strbuf_add(&base, name, len); 765 strbuf_addch(&base, ':'); 766 } 767 init_tree_desc(&tree, tree_ish_oid, data, size); 768 769 collect_blob_oids_for_tree(opt->repo, pathspec, &tree, 770 &base, base.len, blob_oids); 771 772 strbuf_release(&base); 773 free(data); 774 } 775 776 static void prefetch_grep_blobs(struct grep_opt *opt, 777 const struct pathspec *pathspec, 778 const struct object_array *list) 779 { 780 struct oidset blob_oids = OIDSET_INIT; 781 782 /* Exit if we're not in a partial clone */ 783 if (!repo_has_promisor_remote(opt->repo)) 784 return; 785 786 /* For each tree, gather the blobs in it */ 787 for (int i = 0; i < list->nr; i++) { 788 struct object *real_obj; 789 790 obj_read_lock(); 791 real_obj = deref_tag(opt->repo, list->objects[i].item, 792 NULL, 0); 793 obj_read_unlock(); 794 795 if (real_obj && 796 (real_obj->type == OBJ_COMMIT || 797 real_obj->type == OBJ_TREE)) 798 collect_blob_oids_for_treeish(opt, pathspec, 799 &real_obj->oid, 800 list->objects[i].name, 801 &blob_oids); 802 } 803 804 /* Prefetch the blobs we found */ 805 if (oidset_size(&blob_oids)) { 806 struct oid_array to_fetch = OID_ARRAY_INIT; 807 struct oidset_iter iter; 808 const struct object_id *oid; 809 810 oidset_iter_init(&blob_oids, &iter); 811 while ((oid = oidset_iter_next(&iter))) 812 oid_array_append(&to_fetch, oid); 813 814 promisor_remote_get_direct(opt->repo, to_fetch.oid, to_fetch.nr); 815 816 oid_array_clear(&to_fetch); 817 } 818 oidset_clear(&blob_oids); 819 } 820 821 static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec, 822 struct object *obj, const char *name, const char *path) 823 { 824 if (obj->type == OBJ_BLOB) 825 return grep_oid(opt, &obj->oid, name, 0, path); 826 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) { 827 struct tree_desc tree; 828 void *data; 829 size_t size; 830 struct strbuf base; 831 int hit, len; 832 833 data = odb_read_object_peeled(opt->repo->objects, &obj->oid, 834 OBJ_TREE, &size, NULL); 835 if (!data) 836 die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid)); 837 838 len = name ? strlen(name) : 0; 839 strbuf_init(&base, PATH_MAX + len + 1); 840 if (len) { 841 strbuf_add(&base, name, len); 842 strbuf_addch(&base, ':'); 843 } 844 init_tree_desc(&tree, &obj->oid, data, size); 845 hit = grep_tree(opt, pathspec, &tree, &base, base.len, 846 obj->type == OBJ_COMMIT); 847 strbuf_release(&base); 848 free(data); 849 return hit; 850 } 851 die(_("unable to grep from object of type %s"), type_name(obj->type)); 852 } 853 854 static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec, 855 const struct object_array *list) 856 { 857 unsigned int i; 858 int hit = 0; 859 const unsigned int nr = list->nr; 860 861 prefetch_grep_blobs(opt, pathspec, list); 862 863 for (i = 0; i < nr; i++) { 864 struct object *real_obj; 865 866 obj_read_lock(); 867 real_obj = deref_tag(opt->repo, list->objects[i].item, 868 NULL, 0); 869 obj_read_unlock(); 870 871 if (!real_obj) { 872 char hex[GIT_MAX_HEXSZ + 1]; 873 const char *name = list->objects[i].name; 874 875 if (!name) { 876 oid_to_hex_r(hex, &list->objects[i].item->oid); 877 name = hex; 878 } 879 die(_("invalid object '%s' given."), name); 880 } 881 882 /* load the gitmodules file for this rev */ 883 if (recurse_submodules) { 884 submodule_free(opt->repo); 885 obj_read_lock(); 886 gitmodules_config_oid(the_repository, &real_obj->oid); 887 obj_read_unlock(); 888 } 889 if (grep_object(opt, pathspec, real_obj, list->objects[i].name, 890 list->objects[i].path)) { 891 hit = 1; 892 if (opt->status_only) 893 break; 894 } 895 } 896 return hit; 897 } 898 899 static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec, 900 int exc_std, int use_index) 901 { 902 struct dir_struct dir = DIR_INIT; 903 int i, hit = 0; 904 905 if (!use_index) 906 dir.flags |= DIR_NO_GITLINKS; 907 if (exc_std) 908 setup_standard_excludes(&dir); 909 910 fill_directory(&dir, opt->repo->index, pathspec); 911 for (i = 0; i < dir.nr; i++) { 912 hit |= grep_file(opt, dir.entries[i]->name); 913 if (hit && opt->status_only) 914 break; 915 } 916 dir_clear(&dir); 917 return hit; 918 } 919 920 static int context_callback(const struct option *opt, const char *arg, 921 int unset) 922 { 923 struct grep_opt *grep_opt = opt->value; 924 int value; 925 const char *endp; 926 927 if (unset) { 928 grep_opt->pre_context = grep_opt->post_context = 0; 929 return 0; 930 } 931 value = strtol(arg, (char **)&endp, 10); 932 if (*endp) { 933 return error(_("switch `%c' expects a numerical value"), 934 opt->short_name); 935 } 936 grep_opt->pre_context = grep_opt->post_context = value; 937 return 0; 938 } 939 940 static int file_callback(const struct option *opt, const char *arg, int unset) 941 { 942 struct grep_opt *grep_opt = opt->value; 943 int from_stdin; 944 const char *filename = arg; 945 FILE *patterns; 946 int lno = 0; 947 struct strbuf sb = STRBUF_INIT; 948 949 BUG_ON_OPT_NEG(unset); 950 951 if (!*filename) 952 ; /* leave it as-is */ 953 else 954 filename = prefix_filename_except_for_dash(grep_prefix, filename); 955 956 from_stdin = !strcmp(filename, "-"); 957 patterns = from_stdin ? stdin : fopen(filename, "r"); 958 if (!patterns) 959 die_errno(_("cannot open '%s'"), arg); 960 while (strbuf_getline(&sb, patterns) == 0) { 961 /* ignore empty line like grep does */ 962 if (sb.len == 0) 963 continue; 964 965 append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno, 966 GREP_PATTERN); 967 } 968 if (!from_stdin) 969 fclose(patterns); 970 strbuf_release(&sb); 971 if (filename != arg) 972 free((void *)filename); 973 return 0; 974 } 975 976 static int not_callback(const struct option *opt, const char *arg, int unset) 977 { 978 struct grep_opt *grep_opt = opt->value; 979 BUG_ON_OPT_NEG(unset); 980 BUG_ON_OPT_ARG(arg); 981 append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT); 982 return 0; 983 } 984 985 static int and_callback(const struct option *opt, const char *arg, int unset) 986 { 987 struct grep_opt *grep_opt = opt->value; 988 BUG_ON_OPT_NEG(unset); 989 BUG_ON_OPT_ARG(arg); 990 append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND); 991 return 0; 992 } 993 994 static int open_callback(const struct option *opt, const char *arg, int unset) 995 { 996 struct grep_opt *grep_opt = opt->value; 997 BUG_ON_OPT_NEG(unset); 998 BUG_ON_OPT_ARG(arg); 999 append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN); 1000 return 0; 1001 } 1002 1003 static int close_callback(const struct option *opt, const char *arg, int unset) 1004 { 1005 struct grep_opt *grep_opt = opt->value; 1006 BUG_ON_OPT_NEG(unset); 1007 BUG_ON_OPT_ARG(arg); 1008 append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN); 1009 return 0; 1010 } 1011 1012 static int pattern_callback(const struct option *opt, const char *arg, 1013 int unset) 1014 { 1015 struct grep_opt *grep_opt = opt->value; 1016 BUG_ON_OPT_NEG(unset); 1017 append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN); 1018 return 0; 1019 } 1020 1021 int cmd_grep(int argc, 1022 const char **argv, 1023 const char *prefix, 1024 struct repository *repo UNUSED) 1025 { 1026 int hit = 0; 1027 int cached = 0, untracked = 0, opt_exclude = -1; 1028 int seen_dashdash = 0; 1029 int external_grep_allowed__ignored; 1030 const char *show_in_pager = NULL, *default_pager = "dummy"; 1031 struct grep_opt opt; 1032 struct object_array list = OBJECT_ARRAY_INIT; 1033 struct pathspec pathspec; 1034 struct string_list path_list = STRING_LIST_INIT_DUP; 1035 int i; 1036 int dummy; 1037 int use_index = 1; 1038 int allow_revs; 1039 int ret; 1040 1041 struct option options[] = { 1042 OPT_BOOL(0, "cached", &cached, 1043 N_("search in index instead of in the work tree")), 1044 OPT_NEGBIT(0, "no-index", &use_index, 1045 N_("find in contents not managed by git"), 1), 1046 OPT_BOOL(0, "untracked", &untracked, 1047 N_("search in both tracked and untracked files")), 1048 OPT_SET_INT(0, "exclude-standard", &opt_exclude, 1049 N_("ignore files specified via '.gitignore'"), 1), 1050 OPT_BOOL(0, "recurse-submodules", &recurse_submodules, 1051 N_("recursively search in each submodule")), 1052 OPT_GROUP(""), 1053 OPT_BOOL('v', "invert-match", &opt.invert, 1054 N_("show non-matching lines")), 1055 OPT_BOOL('i', "ignore-case", &opt.ignore_case, 1056 N_("case insensitive matching")), 1057 OPT_BOOL('w', "word-regexp", &opt.word_regexp, 1058 N_("match patterns only at word boundaries")), 1059 OPT_SET_INT('a', "text", &opt.binary, 1060 N_("process binary files as text"), GREP_BINARY_TEXT), 1061 OPT_SET_INT('I', NULL, &opt.binary, 1062 N_("don't match patterns in binary files"), 1063 GREP_BINARY_NOMATCH), 1064 OPT_BOOL(0, "textconv", &opt.allow_textconv, 1065 N_("process binary files with textconv filters")), 1066 OPT_SET_INT('r', "recursive", &opt.max_depth, 1067 N_("search in subdirectories (default)"), -1), 1068 OPT_INTEGER_F(0, "max-depth", &opt.max_depth, 1069 N_("descend at most levels"), PARSE_OPT_NONEG), 1070 OPT_GROUP(""), 1071 OPT_SET_INT('E', "extended-regexp", &opt.pattern_type_option, 1072 N_("use extended POSIX regular expressions"), 1073 GREP_PATTERN_TYPE_ERE), 1074 OPT_SET_INT('G', "basic-regexp", &opt.pattern_type_option, 1075 N_("use basic POSIX regular expressions (default)"), 1076 GREP_PATTERN_TYPE_BRE), 1077 OPT_SET_INT('F', "fixed-strings", &opt.pattern_type_option, 1078 N_("interpret patterns as fixed strings"), 1079 GREP_PATTERN_TYPE_FIXED), 1080 OPT_SET_INT('P', "perl-regexp", &opt.pattern_type_option, 1081 N_("use Perl-compatible regular expressions"), 1082 GREP_PATTERN_TYPE_PCRE), 1083 OPT_GROUP(""), 1084 OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")), 1085 OPT_BOOL(0, "column", &opt.columnnum, N_("show column number of first match")), 1086 OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1), 1087 OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1), 1088 OPT_NEGBIT(0, "full-name", &opt.relative, 1089 N_("show filenames relative to top directory"), 1), 1090 OPT_BOOL('l', "files-with-matches", &opt.name_only, 1091 N_("show only filenames instead of matching lines")), 1092 OPT_BOOL(0, "name-only", &opt.name_only, 1093 N_("synonym for --files-with-matches")), 1094 OPT_BOOL('L', "files-without-match", 1095 &opt.unmatch_name_only, 1096 N_("show only the names of files without match")), 1097 OPT_BOOL_F('z', "null", &opt.null_following_name, 1098 N_("print NUL after filenames"), 1099 PARSE_OPT_NOCOMPLETE), 1100 OPT_BOOL('o', "only-matching", &opt.only_matching, 1101 N_("show only matching parts of a line")), 1102 OPT_BOOL('c', "count", &opt.count, 1103 N_("show the number of matches instead of matching lines")), 1104 OPT__COLOR(&opt.color, N_("highlight matches")), 1105 OPT_BOOL(0, "break", &opt.file_break, 1106 N_("print empty line between matches from different files")), 1107 OPT_BOOL(0, "heading", &opt.heading, 1108 N_("show filename only once above matches from same file")), 1109 OPT_GROUP(""), 1110 OPT_CALLBACK('C', "context", &opt, N_("n"), 1111 N_("show context lines before and after matches"), 1112 context_callback), 1113 OPT_UNSIGNED('B', "before-context", &opt.pre_context, 1114 N_("show context lines before matches")), 1115 OPT_UNSIGNED('A', "after-context", &opt.post_context, 1116 N_("show context lines after matches")), 1117 OPT_INTEGER(0, "threads", &num_threads, 1118 N_("use worker threads")), 1119 OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"), 1120 context_callback), 1121 OPT_BOOL('p', "show-function", &opt.funcname, 1122 N_("show a line with the function name before matches")), 1123 OPT_BOOL('W', "function-context", &opt.funcbody, 1124 N_("show the surrounding function")), 1125 OPT_GROUP(""), 1126 OPT_CALLBACK('f', NULL, &opt, N_("file"), 1127 N_("read patterns from file"), file_callback), 1128 OPT_CALLBACK_F('e', NULL, &opt, N_("pattern"), 1129 N_("match "), PARSE_OPT_NONEG, pattern_callback), 1130 OPT_CALLBACK_F(0, "and", &opt, NULL, 1131 N_("combine patterns specified with -e"), 1132 PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback), 1133 OPT_BOOL_F(0, "or", &dummy, "", PARSE_OPT_NONEG), 1134 OPT_CALLBACK_F(0, "not", &opt, NULL, "", 1135 PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback), 1136 OPT_CALLBACK_F('(', NULL, &opt, NULL, "", 1137 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, 1138 open_callback), 1139 OPT_CALLBACK_F(')', NULL, &opt, NULL, "", 1140 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH, 1141 close_callback), 1142 OPT__QUIET(&opt.status_only, 1143 N_("indicate hit with exit status without output")), 1144 OPT_BOOL(0, "all-match", &opt.all_match, 1145 N_("show only matches from files that match all patterns")), 1146 OPT_GROUP(""), 1147 { 1148 .type = OPTION_STRING, 1149 .short_name = 'O', 1150 .long_name = "open-files-in-pager", 1151 .value = &show_in_pager, 1152 .argh = N_("pager"), 1153 .help = N_("show matching files in the pager"), 1154 .flags = PARSE_OPT_OPTARG | PARSE_OPT_NOCOMPLETE, 1155 .defval = (intptr_t)default_pager, 1156 }, 1157 OPT_BOOL_F(0, "ext-grep", &external_grep_allowed__ignored, 1158 N_("allow calling of grep(1) (ignored by this build)"), 1159 PARSE_OPT_NOCOMPLETE), 1160 OPT_INTEGER('m', "max-count", &opt.max_count, 1161 N_("maximum number of results per file")), 1162 OPT_END() 1163 }; 1164 grep_prefix = prefix; 1165 1166 grep_init(&opt, the_repository); 1167 repo_config(the_repository, grep_cmd_config, &opt); 1168 1169 /* 1170 * If there is no -- then the paths must exist in the working 1171 * tree. If there is no explicit pattern specified with -e or 1172 * -f, we take the first unrecognized non option to be the 1173 * pattern, but then what follows it must be zero or more 1174 * valid refs up to the -- (if exists), and then existing 1175 * paths. If there is an explicit pattern, then the first 1176 * unrecognized non option is the beginning of the refs list 1177 * that continues up to the -- (if exists), and then paths. 1178 */ 1179 argc = parse_options(argc, argv, prefix, options, grep_usage, 1180 PARSE_OPT_KEEP_DASHDASH | 1181 PARSE_OPT_STOP_AT_NON_OPTION); 1182 1183 if (the_repository->gitdir) { 1184 prepare_repo_settings(the_repository); 1185 the_repository->settings.command_requires_full_index = 0; 1186 } 1187 1188 if (use_index && !startup_info->have_repository) { 1189 int fallback = 0; 1190 repo_config_get_bool(the_repository, "grep.fallbacktonoindex", &fallback); 1191 if (fallback) 1192 use_index = 0; 1193 else 1194 /* die the same way as if we did it at the beginning */ 1195 setup_git_directory(the_repository); 1196 } 1197 /* Ignore --recurse-submodules if --no-index is given or implied */ 1198 if (!use_index) 1199 recurse_submodules = 0; 1200 1201 /* 1202 * skip a -- separator; we know it cannot be 1203 * separating revisions from pathnames if 1204 * we haven't even had any patterns yet 1205 */ 1206 if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) { 1207 argv++; 1208 argc--; 1209 } 1210 1211 /* First unrecognized non-option token */ 1212 if (argc > 0 && !opt.pattern_list) { 1213 append_grep_pattern(&opt, argv[0], "command line", 0, 1214 GREP_PATTERN); 1215 argv++; 1216 argc--; 1217 } 1218 1219 if (show_in_pager == default_pager) 1220 show_in_pager = git_pager(the_repository, 1); 1221 if (show_in_pager) { 1222 opt.color = GIT_COLOR_NEVER; 1223 opt.name_only = 1; 1224 opt.null_following_name = 1; 1225 opt.output_priv = &path_list; 1226 opt.output = append_path; 1227 string_list_append(&path_list, show_in_pager); 1228 } 1229 1230 if (!opt.pattern_list) 1231 die(_("no pattern given")); 1232 1233 /* --only-matching has no effect with --invert. */ 1234 if (opt.invert) 1235 opt.only_matching = 0; 1236 1237 /* 1238 * We have to find "--" in a separate pass, because its presence 1239 * influences how we will parse arguments that come before it. 1240 */ 1241 for (i = 0; i < argc; i++) { 1242 if (!strcmp(argv[i], "--")) { 1243 seen_dashdash = 1; 1244 break; 1245 } 1246 } 1247 1248 /* 1249 * Resolve any rev arguments. If we have a dashdash, then everything up 1250 * to it must resolve as a rev. If not, then we stop at the first 1251 * non-rev and assume everything else is a path. 1252 */ 1253 allow_revs = use_index && !untracked; 1254 for (i = 0; i < argc; i++) { 1255 const char *arg = argv[i]; 1256 struct object_id oid; 1257 struct object_context oc = {0}; 1258 struct object *object; 1259 1260 if (!strcmp(arg, "--")) { 1261 i++; 1262 break; 1263 } 1264 1265 if (!allow_revs) { 1266 if (seen_dashdash) 1267 die(_("--no-index or --untracked cannot be used with revs")); 1268 break; 1269 } 1270 1271 if (get_oid_with_context(the_repository, arg, 1272 GET_OID_RECORD_PATH, 1273 &oid, &oc)) { 1274 if (seen_dashdash) 1275 die(_("unable to resolve revision: %s"), arg); 1276 object_context_release(&oc); 1277 break; 1278 } 1279 1280 object = parse_object_or_die(the_repository, &oid, arg); 1281 if (!seen_dashdash) 1282 verify_non_filename(the_repository, prefix, arg); 1283 add_object_array_with_path(object, arg, &list, oc.mode, oc.path); 1284 object_context_release(&oc); 1285 } 1286 1287 /* 1288 * Anything left over is presumed to be a path. But in the non-dashdash 1289 * "do what I mean" case, we verify and complain when that isn't true. 1290 */ 1291 if (!seen_dashdash) { 1292 int j; 1293 for (j = i; j < argc; j++) 1294 verify_filename(the_repository, prefix, argv[j], j == i && allow_revs); 1295 } 1296 1297 parse_pathspec(&pathspec, 0, 1298 PATHSPEC_PREFER_CWD | 1299 (opt.max_depth != -1 ? PATHSPEC_MAXDEPTH_VALID : 0), 1300 prefix, argv + i); 1301 pathspec.max_depth = opt.max_depth; 1302 pathspec.recursive = 1; 1303 pathspec.recurse_submodules = !!recurse_submodules; 1304 1305 if (recurse_submodules && untracked) 1306 die(_("--untracked not supported with --recurse-submodules")); 1307 1308 /* 1309 * Optimize out the case where the amount of matches is limited to zero. 1310 * We do this to keep results consistent with GNU grep(1). 1311 */ 1312 if (opt.max_count == 0) { 1313 ret = 1; 1314 goto out; 1315 } 1316 1317 if (show_in_pager) { 1318 if (num_threads > 1) 1319 warning(_("invalid option combination, ignoring --threads")); 1320 num_threads = 1; 1321 } else if (!HAVE_THREADS && num_threads > 1) { 1322 warning(_("no threads support, ignoring --threads")); 1323 num_threads = 1; 1324 } else if (num_threads < 0) 1325 die(_("invalid number of threads specified (%d)"), num_threads); 1326 else if (num_threads == 0) 1327 num_threads = HAVE_THREADS ? online_cpus() : 1; 1328 1329 if (num_threads > 1) { 1330 if (!HAVE_THREADS) 1331 BUG("Somebody got num_threads calculation wrong!"); 1332 if (!(opt.name_only || opt.unmatch_name_only || opt.count) 1333 && (opt.pre_context || opt.post_context || 1334 opt.file_break || opt.funcbody)) 1335 skip_first_line = 1; 1336 1337 /* 1338 * Pre-read gitmodules (if not read already) and force eager 1339 * initialization of packed_git to prevent racy lazy 1340 * reading/initialization once worker threads are started. 1341 */ 1342 if (recurse_submodules) 1343 repo_read_gitmodules(the_repository, 1); 1344 1345 if (startup_info->have_repository) 1346 odb_prepare(the_repository->objects, 0); 1347 1348 start_threads(&opt); 1349 } else { 1350 /* 1351 * The compiled patterns on the main path are only 1352 * used when not using threading. Otherwise 1353 * start_threads() above calls compile_grep_patterns() 1354 * for each thread. 1355 */ 1356 compile_grep_patterns(&opt); 1357 } 1358 1359 if (show_in_pager && (cached || list.nr)) 1360 die(_("--open-files-in-pager only works on the worktree")); 1361 1362 if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) { 1363 const char *pager = path_list.items[0].string; 1364 int len = strlen(pager); 1365 1366 if (len > 4 && is_dir_sep(pager[len - 5])) 1367 pager += len - 4; 1368 1369 if (opt.ignore_case && !strcmp("less", pager)) 1370 string_list_append(&path_list, "-I"); 1371 1372 if (!strcmp("less", pager) || !strcmp("vi", pager)) { 1373 struct strbuf buf = STRBUF_INIT; 1374 strbuf_addf(&buf, "+/%s%s", 1375 strcmp("less", pager) ? "" : "*", 1376 opt.pattern_list->pattern); 1377 string_list_append_nodup(&path_list, 1378 strbuf_detach(&buf, NULL)); 1379 } 1380 } 1381 1382 if (!show_in_pager && !opt.status_only) 1383 setup_pager(the_repository); 1384 1385 die_for_incompatible_opt3(!use_index, "--no-index", 1386 untracked, "--untracked", 1387 cached, "--cached"); 1388 1389 if (!use_index || untracked) { 1390 int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude; 1391 hit = grep_directory(&opt, &pathspec, use_exclude, use_index); 1392 } else if (0 <= opt_exclude) { 1393 die(_("--[no-]exclude-standard cannot be used for tracked contents")); 1394 } else if (!list.nr) { 1395 if (!cached) 1396 setup_work_tree(the_repository); 1397 1398 hit = grep_cache(&opt, &pathspec, cached); 1399 } else { 1400 if (cached) 1401 die(_("both --cached and trees are given")); 1402 1403 hit = grep_objects(&opt, &pathspec, &list); 1404 } 1405 1406 if (num_threads > 1) 1407 hit |= wait_all(); 1408 if (hit && show_in_pager) 1409 run_pager(&opt, prefix); 1410 1411 ret = !hit; 1412 1413 out: 1414 clear_pathspec(&pathspec); 1415 string_list_clear(&path_list, 0); 1416 free_grep_patterns(&opt); 1417 object_array_clear(&list); 1418 free_repos(); 1419 return ret; 1420 }