grep: use grep_opt->repo instead of explict repo argument

This command is probably the first one that operates on a repository other than the_repository, in f9ee2fcdfa (grep: recurse in-process using 'struct repository' - 2017-08-02). An explicit 'struct repository *' was added in that commit to pass around the repository that we're supposed to grep from. Since 38bbc2ea39 (grep.c: remove implicit dependency on the_index - 2018-09-21). 'struct grep_opt *' carries in itself a repository parameter for grepping. We should now be able to reuse grep_opt to hold the submodule repo instead of a separate argument, which is just room for mistakes. While at there, use the right reference instead of the_repository and the_index in this code. I was a bit careless in my attempt to kick the_repository / the_index out of library code. It's normally safe to just stick the_repository / the_index in bultin/ code, but it's not the case for grep. Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 12, 2019 at 09:13 UTC dba093ddc0341a85f576bbd5227d393a820d930a
1 file changed +24 -17
builtin/grep.c
+24 -17
@@ -393,18 +393,20 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
393 exit(status);
394 }
395
396 -static int grep_cache(struct grep_opt *opt, struct repository *repo,
396 +static int grep_cache(struct grep_opt *opt,
397 const struct pathspec *pathspec, int cached);
398 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
399 struct tree_desc *tree, struct strbuf *base, int tn_len,
400 - int check_attr, struct repository *repo);
400 + int check_attr);
401
402 -static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
402 +static int grep_submodule(struct grep_opt *opt,
403 const struct pathspec *pathspec,
404 const struct object_id *oid,
405 const char *filename, const char *path)
406 {
407 + struct repository *superproject = opt->repo;
408 struct repository submodule;
409 + struct grep_opt subopt;
410 int hit;
411
412 /*
@@ -440,6 +442,9 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
442 add_to_alternates_memory(submodule.objects->odb->path);
443 grep_read_unlock();
444
445 + memcpy(&subopt, opt, sizeof(subopt));
446 + subopt.repo = &submodule;
447 +
448 if (oid) {
449 struct object *object;
450 struct tree_desc tree;
@@ -461,21 +466,22 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
466 strbuf_addch(&base, '/');
467
468 init_tree_desc(&tree, data, size);
464 - hit = grep_tree(opt, pathspec, &tree, &base, base.len,
465 - object->type == OBJ_COMMIT, &submodule);
469 + hit = grep_tree(&subopt, pathspec, &tree, &base, base.len,
470 + object->type == OBJ_COMMIT);
471 strbuf_release(&base);
472 free(data);
473 } else {
469 - hit = grep_cache(opt, &submodule, pathspec, 1);
474 + hit = grep_cache(&subopt, pathspec, 1);
475 }
476
477 repo_clear(&submodule);
478 return hit;
479 }
480
476 -static int grep_cache(struct grep_opt *opt, struct repository *repo,
481 +static int grep_cache(struct grep_opt *opt,
482 const struct pathspec *pathspec, int cached)
483 {
484 + struct repository *repo = opt->repo;
485 int hit = 0;
486 int nr;
487 struct strbuf name = STRBUF_INIT;
@@ -513,7 +519,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
519 }
520 } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
521 submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
516 - hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name);
522 + hit |= grep_submodule(opt, pathspec, NULL, ce->name, ce->name);
523 } else {
524 continue;
525 }
@@ -535,8 +541,9 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
541
542 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
543 struct tree_desc *tree, struct strbuf *base, int tn_len,
538 - int check_attr, struct repository *repo)
544 + int check_attr)
545 {
546 + struct repository *repo = opt->repo;
547 int hit = 0;
548 enum interesting match = entry_not_interesting;
549 struct name_entry entry;
@@ -582,10 +589,10 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
589 strbuf_addch(base, '/');
590 init_tree_desc(&sub, data, size);
591 hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
585 - check_attr, repo);
592 + check_attr);
593 free(data);
594 } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
588 - hit |= grep_submodule(opt, repo, pathspec, entry.oid,
595 + hit |= grep_submodule(opt, pathspec, entry.oid,
596 base->buf, base->buf + tn_len);
597 }
598
@@ -627,7 +634,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
634 }
635 init_tree_desc(&tree, data, size);
636 hit = grep_tree(opt, pathspec, &tree, &base, base.len,
630 - obj->type == OBJ_COMMIT, the_repository);
637 + obj->type == OBJ_COMMIT);
638 strbuf_release(&base);
639 free(data);
640 return hit;
@@ -644,12 +651,12 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
651
652 for (i = 0; i < nr; i++) {
653 struct object *real_obj;
647 - real_obj = deref_tag(the_repository, list->objects[i].item,
654 + real_obj = deref_tag(opt->repo, list->objects[i].item,
655 NULL, 0);
656
657 /* load the gitmodules file for this rev */
658 if (recurse_submodules) {
652 - submodule_free(the_repository);
659 + submodule_free(opt->repo);
660 gitmodules_config_oid(&real_obj->oid);
661 }
662 if (grep_object(opt, pathspec, real_obj, list->objects[i].name,
@@ -674,9 +681,9 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
681 if (exc_std)
682 setup_standard_excludes(&dir);
683
677 - fill_directory(&dir, &the_index, pathspec);
684 + fill_directory(&dir, opt->repo->index, pathspec);
685 for (i = 0; i < dir.nr; i++) {
679 - if (!dir_path_match(&the_index, dir.entries[i], pathspec, 0, NULL))
686 + if (!dir_path_match(opt->repo->index, dir.entries[i], pathspec, 0, NULL))
687 continue;
688 hit |= grep_file(opt, dir.entries[i]->name);
689 if (hit && opt->status_only)
@@ -1117,7 +1124,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1124 if (!cached)
1125 setup_work_tree();
1126
1120 - hit = grep_cache(&opt, the_repository, &pathspec, cached);
1127 + hit = grep_cache(&opt, &pathspec, cached);
1128 } else {
1129 if (cached)
1130 die(_("both --cached and trees are given"));