git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Brandon Williams <bmwill@google.com>
To: git@vger.kernel.org
Cc: Brandon Williams <bmwill@google.com>,
	sbeller@google.com, jonathantanmy@google.com, gitster@pobox.com
Subject: [PATCH v3 5/6] grep: enable recurse-submodules to work on <tree> objects
Date: Fri, 11 Nov 2016 15:51:12 -0800	[thread overview]
Message-ID: <1478908273-190166-6-git-send-email-bmwill@google.com> (raw)
In-Reply-To: <1478908273-190166-1-git-send-email-bmwill@google.com>

Teach grep to recursively search in submodules when provided with a
<tree> object. This allows grep to search a submodule based on the state
of the submodule that is present in a commit of the super project.

When grep is provided with a <tree> object, the name of the object is
prefixed to all output.  In order to provide uniformity of output
between the parent and child processes the option `--parent-basename`
has been added so that the child can preface all of it's output with the
name of the parent's object instead of the name of the commit SHA1 of
the submodule. This changes output from the command
`git grep -e. -l --recurse-submodules HEAD` from:
HEAD:file
<commit sha1 of submodule>:sub/file

to:
HEAD:file
HEAD:sub/file

Signed-off-by: Brandon Williams <bmwill@google.com>
---
 Documentation/git-grep.txt         | 13 +++++-
 builtin/grep.c                     | 83 +++++++++++++++++++++++++++++++++++---
 t/t7814-grep-recurse-submodules.sh | 44 +++++++++++++++++++-
 3 files changed, 131 insertions(+), 9 deletions(-)

diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index 17aa1ba..71f32f3 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -26,7 +26,7 @@ SYNOPSIS
 	   [--threads <num>]
 	   [-f <file>] [-e] <pattern>
 	   [--and|--or|--not|(|)|-e <pattern>...]
-	   [--recurse-submodules]
+	   [--recurse-submodules] [--parent-basename <basename>]
 	   [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
 	   [--] [<pathspec>...]
 
@@ -91,7 +91,16 @@ OPTIONS
 
 --recurse-submodules::
 	Recursively search in each submodule that has been initialized and
-	checked out in the repository.
+	checked out in the repository.  When used in combination with the
+	<tree> option the prefix of all submodule output will be the name of
+	the parent project's <tree> object.
+
+--parent-basename <basename>::
+	For internal use only.  In order to produce uniform output with the
+	--recurse-submodules option, this option can be used to provide the
+	basename of a parent's <tree> object to a submodule so the submodule
+	can prefix its output with the parent's name rather than the SHA1 of
+	the submodule.
 
 -a::
 --text::
diff --git a/builtin/grep.c b/builtin/grep.c
index 1fd292f..93e5405 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -19,6 +19,7 @@
 #include "dir.h"
 #include "pathspec.h"
 #include "submodule.h"
+#include "submodule-config.h"
 
 static char const * const grep_usage[] = {
 	N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
@@ -28,6 +29,7 @@ static char const * const grep_usage[] = {
 static const char *super_prefix;
 static int recurse_submodules;
 static struct argv_array submodule_options = ARGV_ARRAY_INIT;
+static const char *parent_basename;
 
 static int grep_submodule_launch(struct grep_opt *opt,
 				 const struct grep_source *gs);
@@ -535,19 +537,53 @@ static int grep_submodule_launch(struct grep_opt *opt,
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
 	int status, i;
+	const char *end_of_base;
+	const char *name;
 	struct work_item *w = opt->output_priv;
 
+	end_of_base = strchr(gs->name, ':');
+	if (end_of_base)
+		name = end_of_base + 1;
+	else
+		name = gs->name;
+
 	prepare_submodule_repo_env(&cp.env_array);
 
 	/* Add super prefix */
 	argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
 			 super_prefix ? super_prefix : "",
-			 gs->name);
+			 name);
 	argv_array_push(&cp.args, "grep");
 
+	/*
+	 * Add basename of parent project
+	 * When performing grep on a <tree> object the filename is prefixed
+	 * with the object's name: '<tree-name>:filename'.  In order to
+	 * provide uniformity of output we want to pass the name of the
+	 * parent project's object name to the submodule so the submodule can
+	 * prefix its output with the parent's name and not its own SHA1.
+	 */
+	if (end_of_base)
+		argv_array_pushf(&cp.args, "--parent-basename=%.*s",
+				 (int) (end_of_base - gs->name),
+				 gs->name);
+
 	/* Add options */
-	for (i = 0; i < submodule_options.argc; i++)
+	for (i = 0; i < submodule_options.argc; i++) {
+		/*
+		 * If there is a <tree> identifier for the submodule, add the
+		 * rev after adding the submodule options but before the
+		 * pathspecs.  To do this we listen for the '--' and insert the
+		 * sha1 before pushing the '--' onto the child process argv
+		 * array.
+		 */
+		if (gs->identifier &&
+		    !strcmp("--", submodule_options.argv[i])) {
+			argv_array_push(&cp.args, sha1_to_hex(gs->identifier));
+		}
+
 		argv_array_push(&cp.args, submodule_options.argv[i]);
+	}
 
 	cp.git_cmd = 1;
 	cp.dir = gs->path;
@@ -671,12 +707,29 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 	enum interesting match = entry_not_interesting;
 	struct name_entry entry;
 	int old_baselen = base->len;
+	struct strbuf name = STRBUF_INIT;
+	int name_base_len = 0;
+	if (super_prefix) {
+		strbuf_addstr(&name, super_prefix);
+		name_base_len = name.len;
+	}
 
 	while (tree_entry(tree, &entry)) {
 		int te_len = tree_entry_len(&entry);
 
 		if (match != all_entries_interesting) {
-			match = tree_entry_interesting(&entry, base, tn_len, pathspec);
+			strbuf_setlen(&name, name_base_len);
+			strbuf_addstr(&name, base->buf + tn_len);
+
+			if (recurse_submodules && S_ISGITLINK(entry.mode)) {
+				strbuf_addstr(&name, entry.path);
+				match = submodule_path_match(pathspec, name.buf,
+							     NULL);
+			} else {
+				match = tree_entry_interesting(&entry, &name,
+							       0, pathspec);
+			}
+
 			if (match == all_entries_not_interesting)
 				break;
 			if (match == entry_not_interesting)
@@ -688,8 +741,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 		if (S_ISREG(entry.mode)) {
 			hit |= grep_sha1(opt, entry.oid->hash, base->buf, tn_len,
 					 check_attr ? base->buf + tn_len : NULL);
-		}
-		else if (S_ISDIR(entry.mode)) {
+		} else if (S_ISDIR(entry.mode)) {
 			enum object_type type;
 			struct tree_desc sub;
 			void *data;
@@ -705,12 +757,18 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 			hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
 					 check_attr);
 			free(data);
+		} else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
+			hit |= grep_submodule(opt, entry.oid->hash, base->buf,
+					      base->buf + tn_len);
 		}
+
 		strbuf_setlen(base, old_baselen);
 
 		if (hit && opt->status_only)
 			break;
 	}
+
+	strbuf_release(&name);
 	return hit;
 }
 
@@ -734,6 +792,10 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
 		if (!data)
 			die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
 
+		/* Use parent's name as base when recursing submodules */
+		if (recurse_submodules && parent_basename)
+			name = parent_basename;
+
 		len = name ? strlen(name) : 0;
 		strbuf_init(&base, PATH_MAX + len + 1);
 		if (len) {
@@ -760,6 +822,12 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
 	for (i = 0; i < nr; i++) {
 		struct object *real_obj;
 		real_obj = deref_tag(list->objects[i].item, NULL, 0);
+
+		/* load the gitmodules file for this rev */
+		if (recurse_submodules) {
+			submodule_free();
+			gitmodules_config_sha1(real_obj->oid.hash);
+		}
 		if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path)) {
 			hit = 1;
 			if (opt->status_only)
@@ -900,6 +968,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			    N_("ignore files specified via '.gitignore'"), 1),
 		OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
 			 N_("recursivley search in each submodule")),
+		OPT_STRING(0, "parent-basename", &parent_basename,
+			   N_("basename"),
+			   N_("prepend parent project's basename to output")),
 		OPT_GROUP(""),
 		OPT_BOOL('v', "invert-match", &opt.invert,
 			N_("show non-matching lines")),
@@ -1152,7 +1223,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (recurse_submodules && (!use_index || untracked || list.nr))
+	if (recurse_submodules && (!use_index || untracked))
 		die(_("option not supported with --recurse-submodules."));
 
 	if (!show_in_pager && !opt.status_only)
diff --git a/t/t7814-grep-recurse-submodules.sh b/t/t7814-grep-recurse-submodules.sh
index b670c70..3d1892d 100755
--- a/t/t7814-grep-recurse-submodules.sh
+++ b/t/t7814-grep-recurse-submodules.sh
@@ -84,6 +84,49 @@ test_expect_success 'grep and multiple patterns' '
 	test_cmp expect actual
 '
 
+test_expect_success 'basic grep tree' '
+	cat >expect <<-\EOF &&
+	HEAD:a:foobar
+	HEAD:b/b:bar
+	HEAD:submodule/a:foobar
+	HEAD:submodule/sub/a:foobar
+	EOF
+
+	git grep -e "bar" --recurse-submodules HEAD > actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'grep tree HEAD^' '
+	cat >expect <<-\EOF &&
+	HEAD^:a:foobar
+	HEAD^:b/b:bar
+	HEAD^:submodule/a:foobar
+	EOF
+
+	git grep -e "bar" --recurse-submodules HEAD^ > actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'grep tree HEAD^^' '
+	cat >expect <<-\EOF &&
+	HEAD^^:a:foobar
+	HEAD^^:b/b:bar
+	EOF
+
+	git grep -e "bar" --recurse-submodules HEAD^^ > actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'grep tree and pathspecs' '
+	cat >expect <<-\EOF &&
+	HEAD:submodule/a:foobar
+	HEAD:submodule/sub/a:foobar
+	EOF
+
+	git grep -e "bar" --recurse-submodules HEAD -- submodule > actual &&
+	test_cmp expect actual
+'
+
 test_incompatible_with_recurse_submodules ()
 {
 	test_expect_success "--recurse-submodules and $1 are incompatible" "
@@ -94,6 +137,5 @@ test_incompatible_with_recurse_submodules ()
 
 test_incompatible_with_recurse_submodules --untracked
 test_incompatible_with_recurse_submodules --no-index
-test_incompatible_with_recurse_submodules HEAD
 
 test_done
-- 
2.8.0.rc3.226.g39d4020


  parent reply	other threads:[~2016-11-11 23:51 UTC|newest]

Thread overview: 126+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-27 22:38 [RFC PATCH 0/5] recursively grep across submodules Brandon Williams
2016-10-27 22:38 ` [PATCH 1/5] submodules: add helper functions to determine presence of submodules Brandon Williams
2016-10-27 22:38 ` [PATCH 2/5] submodules: load gitmodules file from commit sha1 Brandon Williams
2016-10-27 22:38 ` [PATCH 3/5] grep: add submodules as a grep source type Brandon Williams
2016-10-27 22:38 ` [PATCH 4/5] grep: optionally recurse into submodules Brandon Williams
2016-11-05  5:09   ` Jonathan Tan
2016-10-27 22:38 ` [PATCH 5/5] grep: enable recurse-submodules to work on <tree> objects Brandon Williams
2016-10-28 19:35   ` Brandon Williams
2016-10-27 23:26 ` [RFC PATCH 0/5] recursively grep across submodules Junio C Hamano
2016-10-28  0:59   ` Stefan Beller
2016-10-28  2:50     ` Junio C Hamano
2016-10-28  3:46       ` Stefan Beller
2016-10-28 15:06       ` Philip Oakley
2016-10-28 17:02   ` Brandon Williams
2016-10-28 17:21     ` Junio C Hamano
2016-10-31 22:38 ` [PATCH v2 0/6] " Brandon Williams
2016-10-31 22:38   ` [PATCH v2 1/6] submodules: add helper functions to determine presence of submodules Brandon Williams
2016-10-31 23:34     ` Stefan Beller
2016-11-01 17:20       ` Junio C Hamano
2016-11-01 17:24         ` Brandon Williams
2016-11-01 17:31         ` Stefan Beller
2016-11-06  7:42           ` Jacob Keller
2016-11-01 17:23       ` Brandon Williams
2016-11-05  2:34     ` Jonathan Tan
2016-10-31 22:38   ` [PATCH v2 2/6] submodules: load gitmodules file from commit sha1 Brandon Williams
2016-11-01 16:39     ` Stefan Beller
2016-10-31 22:38   ` [PATCH v2 3/6] grep: add submodules as a grep source type Brandon Williams
2016-11-01 16:53     ` Stefan Beller
2016-11-01 17:31     ` Junio C Hamano
2016-10-31 22:38   ` [PATCH v2 4/6] grep: optionally recurse into submodules Brandon Williams
2016-11-01 17:26     ` Stefan Beller
2016-11-01 20:25       ` Brandon Williams
2016-10-31 22:38   ` [PATCH v2 5/6] grep: enable recurse-submodules to work on <tree> objects Brandon Williams
2016-11-11 23:09     ` Jonathan Tan
2016-10-31 22:38   ` [PATCH v2 6/6] grep: search history of moved submodules Brandon Williams
2016-11-11 23:51   ` [PATCH v3 0/6] recursively grep across submodules Brandon Williams
2016-11-11 23:51     ` [PATCH v3 1/6] submodules: add helper functions to determine presence of submodules Brandon Williams
2016-11-15 23:49       ` Stefan Beller
2016-11-11 23:51     ` [PATCH v3 2/6] submodules: load gitmodules file from commit sha1 Brandon Williams
2016-11-12  0:22       ` Stefan Beller
2016-11-11 23:51     ` [PATCH v3 3/6] grep: add submodules as a grep source type Brandon Williams
2016-11-11 23:51     ` [PATCH v3 4/6] grep: optionally recurse into submodules Brandon Williams
2016-11-16  0:07       ` Stefan Beller
2016-11-17 22:13         ` Brandon Williams
2016-11-11 23:51     ` Brandon Williams [this message]
2016-11-14 18:10       ` [PATCH v3 5/6] grep: enable recurse-submodules to work on <tree> objects Junio C Hamano
2016-11-14 18:44         ` Jonathan Tan
2016-11-14 18:56           ` Junio C Hamano
2016-11-14 19:08             ` Jonathan Tan
2016-11-14 19:14               ` Brandon Williams
2016-11-16  1:09       ` Stefan Beller
2016-11-17 23:34         ` Brandon Williams
2016-11-11 23:51     ` [PATCH v3 6/6] grep: search history of moved submodules Brandon Williams
2016-11-12  0:30       ` Stefan Beller
2016-11-14 17:43         ` Brandon Williams
2016-11-15 17:42     ` [PATCH v3 0/6] recursively grep across submodules Stefan Beller
2016-11-18 19:58     ` [PATCH v4 " Brandon Williams
2016-11-18 19:58       ` [PATCH v4 1/6] submodules: add helper functions to determine presence of submodules Brandon Williams
2016-11-18 19:58       ` [PATCH v4 2/6] submodules: load gitmodules file from commit sha1 Brandon Williams
2016-11-18 19:58       ` [PATCH v4 3/6] grep: add submodules as a grep source type Brandon Williams
2016-11-18 21:37         ` Junio C Hamano
2016-11-18 22:56           ` Brandon Williams
2016-11-18 19:58       ` [PATCH v4 4/6] grep: optionally recurse into submodules Brandon Williams
2016-11-18 21:48         ` Junio C Hamano
2016-11-18 22:01         ` Junio C Hamano
2016-11-18 22:14         ` Junio C Hamano
2016-11-18 22:58           ` Brandon Williams
2016-11-18 19:58       ` [PATCH v4 5/6] grep: enable recurse-submodules to work on <tree> objects Brandon Williams
2016-11-18 22:19         ` Junio C Hamano
2016-11-18 22:52           ` Brandon Williams
2016-11-21 18:14             ` Brandon Williams
2016-11-18 19:58       ` [PATCH v4 6/6] grep: search history of moved submodules Brandon Williams
2016-11-18 20:10       ` [PATCH v4 0/6] recursively grep across submodules Stefan Beller
2016-11-22 18:46       ` [PATCH v5 " Brandon Williams
2016-11-22 18:46         ` [PATCH v5 1/6] submodules: add helper functions to determine presence of submodules Brandon Williams
2016-11-22 18:46         ` [PATCH v5 2/6] submodules: load gitmodules file from commit sha1 Brandon Williams
2016-11-22 18:46         ` [PATCH v5 3/6] grep: add submodules as a grep source type Brandon Williams
2016-11-22 18:46         ` [PATCH v5 4/6] grep: optionally recurse into submodules Brandon Williams
2016-11-22 18:46         ` [PATCH v5 5/6] grep: enable recurse-submodules to work on <tree> objects Brandon Williams
2016-11-22 22:59           ` Junio C Hamano
2016-11-22 23:21             ` Brandon Williams
2016-11-22 23:28               ` Brandon Williams
2016-11-22 23:37                 ` Junio C Hamano
2016-11-22 23:54                   ` Brandon Williams
2016-11-22 18:46         ` [PATCH v5 6/6] grep: search history of moved submodules Brandon Williams
2016-12-01  1:28         ` [PATCH v6 0/6] recursively grep across submodules Brandon Williams
2016-12-01  1:28           ` [PATCH v6 1/6] submodules: add helper functions to determine presence of submodules Brandon Williams
2016-12-01  4:29             ` Jeff King
2016-12-01 18:31               ` Stefan Beller
2016-12-01 18:46               ` Junio C Hamano
2016-12-01 19:09                 ` Jeff King
2016-12-01 19:16                   ` Brandon Williams
2016-12-01 20:54                     ` Brandon Williams
2016-12-01 20:59                       ` Jeff King
2016-12-01 21:56                         ` Stefan Beller
2016-12-01 21:59                           ` Jeff King
2016-12-02 18:36                             ` Brandon Williams
2016-12-02 18:44                               ` Jacob Keller
2016-12-02 18:49                                 ` Brandon Williams
2016-12-02 19:20                                   ` Jacob Keller
2016-12-02 19:28                                     ` Stefan Beller
2016-12-02 21:31                                       ` Jacob Keller
2016-12-02 21:46                                         ` Brandon Williams
2016-12-02 21:45                                       ` Jeff King
2016-12-03  0:16                                         ` Brandon Williams
2016-12-01  1:28           ` [PATCH v6 2/6] submodules: load gitmodules file from commit sha1 Brandon Williams
2016-12-01  1:28           ` [PATCH v6 3/6] grep: add submodules as a grep source type Brandon Williams
2016-12-01  1:28           ` [PATCH v6 4/6] grep: optionally recurse into submodules Brandon Williams
2016-12-01  1:28           ` [PATCH v6 5/6] grep: enable recurse-submodules to work on <tree> objects Brandon Williams
2016-12-01  7:25             ` Johannes Sixt
2016-12-01 17:51               ` Brandon Williams
2016-12-01 18:49                 ` Junio C Hamano
2016-12-01 18:52                 ` Jeff King
2016-12-01  1:28           ` [PATCH v6 6/6] grep: search history of moved submodules Brandon Williams
2016-12-01  4:22           ` [PATCH v6 0/6] recursively grep across submodules Jeff King
2016-12-01 17:45             ` Brandon Williams
2016-12-01 19:03               ` Jeff King
2016-12-16 19:03           ` [PATCH v7 0/7] " Brandon Williams
2016-12-16 19:03             ` [PATCH v7 1/7] submodules: add helper to determine if a submodule is populated Brandon Williams
2016-12-16 19:03             ` [PATCH v7 2/7] submodules: add helper to determine if a submodule is initialized Brandon Williams
2016-12-16 19:03             ` [PATCH v7 3/7] submodules: load gitmodules file from commit sha1 Brandon Williams
2016-12-16 19:03             ` [PATCH v7 4/7] grep: add submodules as a grep source type Brandon Williams
2016-12-16 19:03             ` [PATCH v7 5/7] grep: optionally recurse into submodules Brandon Williams
2016-12-16 19:03             ` [PATCH v7 6/7] grep: enable recurse-submodules to work on <tree> objects Brandon Williams
2016-12-16 19:03             ` [PATCH v7 7/7] grep: search history of moved submodules Brandon Williams
2016-12-16 21:42             ` [PATCH v7 0/7] recursively grep across submodules Junio C Hamano

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1478908273-190166-6-git-send-email-bmwill@google.com \
    --to=bmwill@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jonathantanmy@google.com \
    --cc=sbeller@google.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).