Re: [RFC/PATCHv2 5/5] grep: add support for grepping in submodules

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Oct 16, 2010 at 6:26 AM, Chris Packham <judge.packham@xxxxxxxxx> wrote:
> +static int grep_submodule(struct grep_opt *opt, const char *path,
> + Â Â Â Â Â Â Â Â Â Â Â Â const char *sha1, const char *tree_name)
> +{
> + Â Â Â struct strbuf buf = STRBUF_INIT;
> + Â Â Â struct strbuf pre_buf = STRBUF_INIT;
> + Â Â Â struct child_process cp;
> + Â Â Â const char **argv = create_sub_grep_argv(opt, path, sha1, tree_name);

Can we just save sha1 in a env variable and drop this argv rewrite?

> + Â Â Â const char *git_dir;
> + Â Â Â int hit = 0;
> + Â Â Â memset(&cp, 0, sizeof(cp));
> +
> + Â Â Â strbuf_addf(&buf, "%s/.git", path);
> + Â Â Â git_dir = read_gitfile_gently(buf.buf);
> + Â Â Â if (!git_dir)
> + Â Â Â Â Â Â Â git_dir = buf.buf;
> + Â Â Â if (!is_directory(git_dir))
> + Â Â Â Â Â Â Â goto out_free;
> +
> + Â Â Â setenv("GIT_SUPER_REFNAME", tree_name, 1);
> + Â Â Â setenv(GIT_DIR_ENVIRONMENT, git_dir, 1);
> + Â Â Â setenv(GIT_WORK_TREE_ENVIRONMENT, path, 1);

cp.env can be used to set these variables

> + Â Â Â cp.argv = argv;
> + Â Â Â cp.git_cmd = 1;
> + Â Â Â cp.no_stdin = 1;

I think you stll need "cp.dir = path;" here because the setup routines
won't do that for you. But I need to check/test that.

> + Â Â Â if (run_command(&cp) == 0)
> + Â Â Â Â Â Â Â hit = 1;
> +out_free:
> + Â Â Â unsetenv("GIT_SUPER_REFNAME");
> + Â Â Â unsetenv(GIT_DIR_ENVIRONMENT);
> + Â Â Â unsetenv(GIT_WORK_TREE_ENVIRONMENT);
> + Â Â Â free(argv);
> + Â Â Â strbuf_release(&buf);
> + Â Â Â strbuf_release(&pre_buf);
> + Â Â Â return hit;
> +}
> +
> Âstatic int grep_cache(struct grep_opt *opt, const char **paths, int cached)
> Â{
> Â Â Â Âint hit = 0;
> @@ -597,6 +682,10 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
> Â Â Â Â Â Â Â Âstruct cache_entry *ce = active_cache[nr];
> Â Â Â Â Â Â Â Âif (!pathspec_matches(paths, ce->name, opt->max_depth))
> Â Â Â Â Â Â Â Â Â Â Â Âcontinue;
> + Â Â Â Â Â Â Â if (S_ISGITLINK(ce->ce_mode) && opt->recurse_submodules) {
> + Â Â Â Â Â Â Â Â Â Â Â hit |= grep_submodule(opt, ce->name, NULL, NULL);
> + Â Â Â Â Â Â Â Â Â Â Â continue;
> + Â Â Â Â Â Â Â }
> Â Â Â Â Â Â Â Âif (!S_ISREG(ce->ce_mode))
> Â Â Â Â Â Â Â Â Â Â Â Âcontinue;
> Â Â Â Â Â Â Â Â/*
> @@ -634,11 +723,16 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
> Â Â Â Âchar *down;
> Â Â Â Âint tn_len = strlen(tree_name);
> Â Â Â Âstruct strbuf pathbuf;
> + Â Â Â const char *refname = getenv("GIT_SUPER_REFNAME");
> + Â Â Â int rn_len = refname ? strlen(refname) : 0;
>
> - Â Â Â strbuf_init(&pathbuf, PATH_MAX + tn_len);
> + Â Â Â strbuf_init(&pathbuf, PATH_MAX + MAX(tn_len, rn_len));
>
> Â Â Â Âif (tn_len) {
> - Â Â Â Â Â Â Â strbuf_add(&pathbuf, tree_name, tn_len);
> + Â Â Â Â Â Â Â if (refname)
> + Â Â Â Â Â Â Â Â Â Â Â strbuf_add(&pathbuf, refname, rn_len);
> + Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â strbuf_add(&pathbuf, tree_name, tn_len);
> Â Â Â Â Â Â Â Âstrbuf_addch(&pathbuf, ':');
> Â Â Â Â Â Â Â Âtn_len = pathbuf.len;
> Â Â Â Â}
> @@ -664,6 +758,9 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
> Â Â Â Â Â Â Â Â Â Â Â Â;
> Â Â Â Â Â Â Â Âelse if (S_ISREG(entry.mode))
> Â Â Â Â Â Â Â Â Â Â Â Âhit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
> + Â Â Â Â Â Â Â else if (S_ISGITLINK(entry.mode) && opt->recurse_submodules)
> + Â Â Â Â Â Â Â Â Â Â Â hit |= grep_submodule(opt, entry.path,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sha1_to_hex(entry.sha1), tree_name);
> Â Â Â Â Â Â Â Âelse if (S_ISDIR(entry.mode)) {
> Â Â Â Â Â Â Â Â Â Â Â Âenum object_type type;
> Â Â Â Â Â Â Â Â Â Â Â Âstruct tree_desc sub;
> @@ -931,6 +1028,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â"allow calling of grep(1) (ignored by this build)"),
> Â Â Â Â Â Â Â Â{ OPTION_CALLBACK, 0, "help-all", &options, NULL, "show usage",
> Â Â Â Â Â Â Â Â ÂPARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
> + Â Â Â Â Â Â Â OPT_BOOLEAN(0, "recursive", &opt.recurse_submodules,
> + Â Â Â Â Â Â Â Â Â Â Â "recurse into submodules"),
> Â Â Â Â Â Â Â ÂOPT_END()
> Â Â Â Â};
>
> diff --git a/grep.h b/grep.h
> index 06621fe..d5e2e11 100644
> --- a/grep.h
> +++ b/grep.h
> @@ -101,6 +101,7 @@ struct grep_opt {
> Â Â Â Âunsigned post_context;
> Â Â Â Âunsigned last_shown;
> Â Â Â Âint show_hunk_mark;
> + Â Â Â int recurse_submodules;
> Â Â Â Âvoid *priv;
>
> Â Â Â Âvoid (*output)(struct grep_opt *opt, const void *data, size_t size);
> --
> 1.7.3.1
>
>



-- 
Duy
ÿô.nlj·Ÿ®‰­†+%ŠË±é¥Šwÿº{.nlj· ŠßžØn‡r¡öë¨è&£ûz¹Þúzf£¢·hšˆ§~†­†Ûÿÿïÿ‘ê_èæ+v‰¨þ)ßø

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]