| 546 | } |
| 547 | |
| 548 | static int grep_cache(struct grep_opt *opt, |
| 549 | const struct pathspec *pathspec, int cached) |
| 550 | { |
| 551 | struct repository *repo = opt->repo; |
| 552 | int hit = 0; |
| 553 | int nr; |
| 554 | struct strbuf name = STRBUF_INIT; |
| 555 | int name_base_len = 0; |
| 556 | if (repo->submodule_prefix) { |
| 557 | name_base_len = strlen(repo->submodule_prefix); |
| 558 | strbuf_addstr(&name, repo->submodule_prefix); |
| 559 | } |
| 560 | |
| 561 | if (repo_read_index(repo) < 0) |
| 562 | die(_("index file corrupt")); |
| 563 | |
| 564 | for (nr = 0; nr < repo->index->cache_nr; nr++) { |
| 565 | const struct cache_entry *ce = repo->index->cache[nr]; |
| 566 | |
| 567 | if (!cached && ce_skip_worktree(ce)) |
| 568 | continue; |
| 569 | |
| 570 | strbuf_setlen(&name, name_base_len); |
| 571 | strbuf_addstr(&name, ce->name); |
| 572 | if (S_ISSPARSEDIR(ce->ce_mode)) { |
| 573 | enum object_type type; |
| 574 | struct tree_desc tree; |
| 575 | void *data; |
| 576 | size_t size; |
| 577 | |
| 578 | data = odb_read_object(the_repository->objects, &ce->oid, |
| 579 | &type, &size); |
| 580 | if (!data) |
| 581 | die(_("unable to read tree %s"), oid_to_hex(&ce->oid)); |
| 582 | init_tree_desc(&tree, &ce->oid, data, size); |
| 583 | |
| 584 | hit |= grep_tree(opt, pathspec, &tree, &name, 0, 0); |
| 585 | strbuf_setlen(&name, name_base_len); |
| 586 | strbuf_addstr(&name, ce->name); |
| 587 | free(data); |
| 588 | } else if (S_ISREG(ce->ce_mode) && |
| 589 | match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL, |
| 590 | S_ISDIR(ce->ce_mode) || |
| 591 | S_ISGITLINK(ce->ce_mode))) { |
| 592 | /* |
| 593 | * If CE_VALID is on, we assume worktree file and its |
| 594 | * cache entry are identical, even if worktree file has |
| 595 | * been modified, so use cache version instead |
| 596 | */ |
| 597 | if (cached || (ce->ce_flags & CE_VALID)) { |
| 598 | if (ce_stage(ce) || ce_intent_to_add(ce)) |
| 599 | continue; |
| 600 | hit |= grep_oid(opt, &ce->oid, name.buf, |
| 601 | 0, name.buf); |
| 602 | } else { |
| 603 | hit |= grep_file(opt, name.buf); |
| 604 | } |
| 605 | } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && |
no test coverage detected