Skip to content

Commit dba093d

Browse files
pcloudsgitster
authored andcommitted
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 f9ee2fc (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 38bbc2e (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>
1 parent ecbdaf0 commit dba093d

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

builtin/grep.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -393,18 +393,20 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
393393
exit(status);
394394
}
395395

396-
static int grep_cache(struct grep_opt *opt, struct repository *repo,
396+
static int grep_cache(struct grep_opt *opt,
397397
const struct pathspec *pathspec, int cached);
398398
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
399399
struct tree_desc *tree, struct strbuf *base, int tn_len,
400-
int check_attr, struct repository *repo);
400+
int check_attr);
401401

402-
static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
402+
static int grep_submodule(struct grep_opt *opt,
403403
const struct pathspec *pathspec,
404404
const struct object_id *oid,
405405
const char *filename, const char *path)
406406
{
407+
struct repository *superproject = opt->repo;
407408
struct repository submodule;
409+
struct grep_opt subopt;
408410
int hit;
409411

410412
/*
@@ -440,6 +442,9 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
440442
add_to_alternates_memory(submodule.objects->odb->path);
441443
grep_read_unlock();
442444

445+
memcpy(&subopt, opt, sizeof(subopt));
446+
subopt.repo = &submodule;
447+
443448
if (oid) {
444449
struct object *object;
445450
struct tree_desc tree;
@@ -461,21 +466,22 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
461466
strbuf_addch(&base, '/');
462467

463468
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);
466471
strbuf_release(&base);
467472
free(data);
468473
} else {
469-
hit = grep_cache(opt, &submodule, pathspec, 1);
474+
hit = grep_cache(&subopt, pathspec, 1);
470475
}
471476

472477
repo_clear(&submodule);
473478
return hit;
474479
}
475480

476-
static int grep_cache(struct grep_opt *opt, struct repository *repo,
481+
static int grep_cache(struct grep_opt *opt,
477482
const struct pathspec *pathspec, int cached)
478483
{
484+
struct repository *repo = opt->repo;
479485
int hit = 0;
480486
int nr;
481487
struct strbuf name = STRBUF_INIT;
@@ -513,7 +519,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
513519
}
514520
} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
515521
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);
517523
} else {
518524
continue;
519525
}
@@ -535,8 +541,9 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
535541

536542
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
537543
struct tree_desc *tree, struct strbuf *base, int tn_len,
538-
int check_attr, struct repository *repo)
544+
int check_attr)
539545
{
546+
struct repository *repo = opt->repo;
540547
int hit = 0;
541548
enum interesting match = entry_not_interesting;
542549
struct name_entry entry;
@@ -582,10 +589,10 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
582589
strbuf_addch(base, '/');
583590
init_tree_desc(&sub, data, size);
584591
hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
585-
check_attr, repo);
592+
check_attr);
586593
free(data);
587594
} 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,
589596
base->buf, base->buf + tn_len);
590597
}
591598

@@ -627,7 +634,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
627634
}
628635
init_tree_desc(&tree, data, size);
629636
hit = grep_tree(opt, pathspec, &tree, &base, base.len,
630-
obj->type == OBJ_COMMIT, the_repository);
637+
obj->type == OBJ_COMMIT);
631638
strbuf_release(&base);
632639
free(data);
633640
return hit;
@@ -644,12 +651,12 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
644651

645652
for (i = 0; i < nr; i++) {
646653
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,
648655
NULL, 0);
649656

650657
/* load the gitmodules file for this rev */
651658
if (recurse_submodules) {
652-
submodule_free(the_repository);
659+
submodule_free(opt->repo);
653660
gitmodules_config_oid(&real_obj->oid);
654661
}
655662
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,
674681
if (exc_std)
675682
setup_standard_excludes(&dir);
676683

677-
fill_directory(&dir, &the_index, pathspec);
684+
fill_directory(&dir, opt->repo->index, pathspec);
678685
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))
680687
continue;
681688
hit |= grep_file(opt, dir.entries[i]->name);
682689
if (hit && opt->status_only)
@@ -1117,7 +1124,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11171124
if (!cached)
11181125
setup_work_tree();
11191126

1120-
hit = grep_cache(&opt, the_repository, &pathspec, cached);
1127+
hit = grep_cache(&opt, &pathspec, cached);
11211128
} else {
11221129
if (cached)
11231130
die(_("both --cached and trees are given"));

0 commit comments

Comments
 (0)