Re: [PATCH] grep: use grep_opt->repo instead of explict repo argument

2018-11-26 Thread Stefan Beller
On Sun, Nov 18, 2018 at 8:38 AM Nguyễn Thái Ngọc Duy  wrote:
>
> 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.

That makes sense. Assuming we did not make a mistake yet, the
test suite would not need changes (further assuming we do test for it,
but we do as we have explicit submodule grep tests).

>
> 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 

> Signed-off-by: Nguyễn Thái Ngọc Duy 


[PATCH] grep: use grep_opt->repo instead of explict repo argument

2018-11-18 Thread Nguyễn Thái Ngọc Duy
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.

Signed-off-by: Nguyễn Thái Ngọc Duy 
---
 builtin/grep.c | 41 -
 1 file changed, 24 insertions(+), 17 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 56e4a11052..bdc49cd34e 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -408,18 +408,20 @@ static void run_pager(struct grep_opt *opt, const char 
*prefix)
exit(status);
 }
 
-static int grep_cache(struct grep_opt *opt, struct repository *repo,
+static int grep_cache(struct grep_opt *opt,
  const struct pathspec *pathspec, int cached);
 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 struct tree_desc *tree, struct strbuf *base, int tn_len,
-int check_attr, struct repository *repo);
+int check_attr);
 
-static int grep_submodule(struct grep_opt *opt, struct repository 
*superproject,
+static int grep_submodule(struct grep_opt *opt,
  const struct pathspec *pathspec,
  const struct object_id *oid,
  const char *filename, const char *path)
 {
+   struct repository *superproject = opt->repo;
struct repository submodule;
+   struct grep_opt subopt;
int hit;
 
/*
@@ -455,6 +457,9 @@ static int grep_submodule(struct grep_opt *opt, struct 
repository *superproject,
add_to_alternates_memory(submodule.objects->objectdir);
grep_read_unlock();
 
+   memcpy(, opt, sizeof(subopt));
+   subopt.repo = 
+
if (oid) {
struct object *object;
struct tree_desc tree;
@@ -476,21 +481,22 @@ static int grep_submodule(struct grep_opt *opt, struct 
repository *superproject,
strbuf_addch(, '/');
 
init_tree_desc(, data, size);
-   hit = grep_tree(opt, pathspec, , , base.len,
-   object->type == OBJ_COMMIT, );
+   hit = grep_tree(, pathspec, , , base.len,
+   object->type == OBJ_COMMIT);
strbuf_release();
free(data);
} else {
-   hit = grep_cache(opt, , pathspec, 1);
+   hit = grep_cache(, pathspec, 1);
}
 
repo_clear();
return hit;
 }
 
-static int grep_cache(struct grep_opt *opt, struct repository *repo,
+static int grep_cache(struct grep_opt *opt,
  const struct pathspec *pathspec, int cached)
 {
+   struct repository *repo = opt->repo;
int hit = 0;
int nr;
struct strbuf name = STRBUF_INIT;
@@ -528,7 +534,7 @@ static int grep_cache(struct grep_opt *opt, struct 
repository *repo,
}
} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
   submodule_path_match(repo->index, pathspec, 
name.buf, NULL)) {
-   hit |= grep_submodule(opt, repo, pathspec, NULL, 
ce->name, ce->name);
+   hit |= grep_submodule(opt, pathspec, NULL, ce->name, 
ce->name);
} else {
continue;
}
@@ -550,8 +556,9 @@ static int grep_cache(struct grep_opt *opt, struct 
repository *repo,
 
 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 struct tree_desc *tree, struct strbuf *base, int tn_len,
-int check_attr, struct repository *repo)
+int check_attr)
 {
+   struct repository *repo = opt->repo;
int hit = 0;
enum interesting match = entry_not_interesting;
struct name_entry entry;
@@ -597,10 +604,10 @@ static int grep_tree(struct grep_opt *opt, const struct 
pathspec *pathspec,
strbuf_addch(base, '/');
init_tree_desc(, data, size);
hit |= grep_tree(opt, pathspec, , base, tn_len,
-check_attr, repo);
+check_attr);