From: Chris Packham <judge.packham@gmail.com>
To: Jens.Lehmann@web.de
Cc: git@vger.kernel.org, Chris Packham <judge.packham@gmail.com>
Subject: [RFC PATCH 3/3] grep: add support for grepping in submodules
Date: Wed, 29 Sep 2010 13:28:54 -0700 [thread overview]
Message-ID: <1285792134-26339-4-git-send-email-judge.packham@gmail.com> (raw)
In-Reply-To: <1285792134-26339-1-git-send-email-judge.packham@gmail.com>
When the --recurse-submodules option is given git grep will search in
submodules as they are encountered.
Signed-off-by: Chris Packham <judge.packham@gmail.com>
---
builtin/grep.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
grep.h | 1 +
2 files changed, 73 insertions(+), 0 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 8315ff0..c9befdc 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -30,6 +30,9 @@ static char const * const grep_usage[] = {
static int use_threads = 1;
+static int saved_argc;
+static const char **saved_argv;
+
#ifndef NO_PTHREADS
#define THREADS 8
static pthread_t threads[THREADS];
@@ -585,6 +588,67 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
free(argv);
}
+static const char **create_sub_grep_argv(struct grep_opt *opt, const char *path)
+{
+ #define NUM_ARGS 10
+ struct strbuf buf = STRBUF_INIT;
+ const char **argv;
+ int i = 0;
+
+ argv = xcalloc(NUM_ARGS, sizeof(const char *));
+ argv[i++] = "grep";
+ strbuf_addf(&buf, "--submodule-prefix=\\\"%s\\\"", path);
+ //argv[i++] = buf.buf;
+
+ if (opt->linenum)
+ argv[i++] = "-n";
+ if (opt->invert)
+ argv[i++] = "-v";
+ if (opt->ignore_case)
+ argv[i++] = "-i";
+ if (opt->count)
+ argv[i++] = "-c";
+ if (opt->name_only)
+ argv[i++] = "-l";
+
+ argv[i++] = saved_argv[0];
+ argv[i++] = NULL;
+
+ strbuf_release(&buf);
+ return argv;
+}
+
+static int grep_submodule(struct grep_opt *opt, const char *path)
+{
+ struct strbuf buf = STRBUF_INIT;
+ struct child_process cp;
+ const char **argv = create_sub_grep_argv(opt, path);
+ const char *git_dir;
+ int hit = 0;
+
+ 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)) {
+ warning("submodule %s has not been initialized\n", path);
+ goto out_free;
+ }
+
+ memset(&cp, 0, sizeof(cp));
+ cp.argv = argv;
+ cp.env = local_repo_env;
+ cp.git_cmd = 1;
+ cp.no_stdin = 1;
+ cp.dir = path;
+ if (run_command(&cp) == 0)
+ hit = 1;
+out_free:
+ free(argv);
+ strbuf_release(&buf);
+ return hit;
+}
+
static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
{
int hit = 0;
@@ -593,6 +657,10 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
for (nr = 0; nr < active_nr; nr++) {
struct cache_entry *ce = active_cache[nr];
+ if (S_ISGITLINK(ce->ce_mode) && opt->recurse_submodules) {
+ hit |= grep_submodule(opt, ce->name);
+ continue;
+ }
if (!S_ISREG(ce->ce_mode))
continue;
if (!pathspec_matches(paths, ce->name, opt->max_depth))
@@ -929,9 +997,13 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
OPT_STRING(0, "submodule-prefix", &opt.submodule_prefix, "DIR",
"prepend this to submodule path output"),
+ OPT_BOOLEAN(0, "recurse-submodules", &opt.recurse_submodules,
+ "recurse into submodules"),
OPT_END()
};
+ saved_argc = argc;
+ saved_argv = argv;
/*
* 'git grep -h', unlike 'git grep -h <pattern>', is a request
* to show usage information and exit.
diff --git a/grep.h b/grep.h
index d918da4..e3199c9 100644
--- a/grep.h
+++ b/grep.h
@@ -102,6 +102,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.dirty
next prev parent reply other threads:[~2010-09-29 20:29 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-29 20:28 [RFC PATCH 0/3] grep: submodule support Chris Packham
2010-09-29 20:28 ` [RFC PATCH 1/3] add test for git grep --recursive Chris Packham
2010-09-29 20:35 ` Ævar Arnfjörð Bjarmason
2010-09-29 20:48 ` Chris Packham
2010-09-29 21:34 ` Kevin Ballard
2010-09-29 20:28 ` [RFC PATCH 2/3] grep: prepare grep for submodules Chris Packham
2010-09-30 1:10 ` Nguyen Thai Ngoc Duy
2010-09-30 18:34 ` Chris Packham
2010-10-01 14:37 ` Nguyen Thai Ngoc Duy
2010-10-01 16:26 ` Chris Packham
2010-09-29 20:28 ` Chris Packham [this message]
2010-09-29 22:21 ` [RFC PATCH 3/3] grep: add support for grepping in submodules Jens Lehmann
2010-09-29 22:59 ` Junio C Hamano
2010-09-29 23:47 ` Chris Packham
2010-09-30 11:09 ` Jens Lehmann
2010-09-30 11:28 ` Johannes Sixt
2010-09-30 15:07 ` Jens Lehmann
2010-09-29 23:02 ` Chris Packham
2010-09-30 11:24 ` Jens Lehmann
2010-09-30 16:48 ` Chris Packham
2010-09-30 18:59 ` Heiko Voigt
2010-09-30 19:48 ` Jens Lehmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1285792134-26339-4-git-send-email-judge.packham@gmail.com \
--to=judge.packham@gmail.com \
--cc=Jens.Lehmann@web.de \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).