All of lore.kernel.org
 help / color / mirror / 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 v4 0/6] recursively grep across submodules
Date: Fri, 18 Nov 2016 11:58:49 -0800	[thread overview]
Message-ID: <1479499135-64269-1-git-send-email-bmwill@google.com> (raw)
In-Reply-To: <1478908273-190166-1-git-send-email-bmwill@google.com>

This revision of this series should address all of the problems brought up with
v3.

* indent output example in patch 5/6.
* fix ':' in submodule names and add a test to verify.
* cleanup some comments.
* fixed tests to test the case where a submodule isn't at the root of a
  repository.
* always pass --threads=%d in order to limit threads to child proccess.

Brandon Williams (6):
  submodules: add helper functions to determine presence of submodules
  submodules: load gitmodules file from commit sha1
  grep: add submodules as a grep source type
  grep: optionally recurse into submodules
  grep: enable recurse-submodules to work on <tree> objects
  grep: search history of moved submodules

 Documentation/git-grep.txt         |  14 ++
 builtin/grep.c                     | 393 ++++++++++++++++++++++++++++++++++---
 cache.h                            |   2 +
 config.c                           |   8 +-
 git.c                              |   2 +-
 grep.c                             |  16 +-
 grep.h                             |   1 +
 submodule-config.c                 |   6 +-
 submodule-config.h                 |   3 +
 submodule.c                        |  50 +++++
 submodule.h                        |   3 +
 t/t7814-grep-recurse-submodules.sh | 213 ++++++++++++++++++++
 12 files changed, 679 insertions(+), 32 deletions(-)
 create mode 100755 t/t7814-grep-recurse-submodules.sh

-- interdiff based on 'bw/grep-recurse-submodules'

diff --git a/builtin/grep.c b/builtin/grep.c
index 1cd2be9..747b0c3 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -518,9 +518,8 @@ static void compile_submodule_options(const struct grep_opt *opt,
 	 * This is to prevent potential fork-bomb behavior of git-grep as each
 	 * submodule process has its own thread pool.
 	 */
-	if (num_threads)
-		argv_array_pushf(&submodule_options, "--threads=%d",
-				 (num_threads + 1) / 2);
+	argv_array_pushf(&submodule_options, "--threads=%d",
+			 (num_threads + 1) / 2);
 
 	/* Add Pathspecs */
 	argv_array_push(&submodule_options, "--");
@@ -542,7 +541,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
 	struct work_item *w = opt->output_priv;
 
 	end_of_base = strchr(gs->name, ':');
-	if (end_of_base)
+	if (gs->identifier && end_of_base)
 		name = end_of_base + 1;
 	else
 		name = gs->name;
@@ -558,13 +557,13 @@ static int grep_submodule_launch(struct grep_opt *opt,
 
 	/*
 	 * 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
+	 * 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)
+	if (gs->identifier && end_of_base)
 		argv_array_pushf(&cp.args, "--parent-basename=%.*s",
 				 (int) (end_of_base - gs->name),
 				 gs->name);
@@ -572,7 +571,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
 	/* Add options */
 	for (i = 0; i < submodule_options.argc; i++) {
 		/*
-		 * If there is a <tree> identifier for the submodule, add the
+		 * 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
@@ -615,17 +614,20 @@ static int grep_submodule_launch(struct grep_opt *opt,
 static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
 			  const char *filename, const char *path)
 {
-	if (!(is_submodule_initialized(path) &&
-	      is_submodule_populated(path))) {
+	if (!is_submodule_initialized(path))
+		return 0;
+	if (!is_submodule_populated(path)) {
 		/*
 		 * If searching history, check for the presense of the
 		 * submodule's gitdir before skipping the submodule.
 		 */
 		if (sha1) {
-			path = git_path("modules/%s",
-					submodule_from_path(null_sha1, path)->name);
+			const struct submodule *sub =
+					submodule_from_path(null_sha1, path);
+			if (sub)
+				path = git_path("modules/%s", sub->name);
 
-			if (!(is_directory(path) && is_git_directory(path)))
+			if(!(is_directory(path) && is_git_directory(path)))
 				return 0;
 		} else {
 			return 0;
diff --git a/t/t7814-grep-recurse-submodules.sh b/t/t7814-grep-recurse-submodules.sh
index ee173ad..7d66716 100755
--- a/t/t7814-grep-recurse-submodules.sh
+++ b/t/t7814-grep-recurse-submodules.sh
@@ -60,7 +60,7 @@ test_expect_success 'grep and nested submodules' '
 	submodule/sub/a:foobar
 	EOF
 
-	git grep -e "bar" --recurse-submodules > actual &&
+	git grep -e "bar" --recurse-submodules >actual &&
 	test_cmp expect actual
 '
 
@@ -71,7 +71,7 @@ test_expect_success 'grep and multiple patterns' '
 	submodule/sub/a:foobar
 	EOF
 
-	git grep -e "bar" --and -e "foo" --recurse-submodules > actual &&
+	git grep -e "bar" --and -e "foo" --recurse-submodules >actual &&
 	test_cmp expect actual
 '
 
@@ -80,7 +80,7 @@ test_expect_success 'grep and multiple patterns' '
 	b/b:bar
 	EOF
 
-	git grep -e "bar" --and --not -e "foo" --recurse-submodules > actual &&
+	git grep -e "bar" --and --not -e "foo" --recurse-submodules >actual &&
 	test_cmp expect actual
 '
 
@@ -92,7 +92,7 @@ test_expect_success 'basic grep tree' '
 	HEAD:submodule/sub/a:foobar
 	EOF
 
-	git grep -e "bar" --recurse-submodules HEAD > actual &&
+	git grep -e "bar" --recurse-submodules HEAD >actual &&
 	test_cmp expect actual
 '
 
@@ -103,7 +103,7 @@ test_expect_success 'grep tree HEAD^' '
 	HEAD^:submodule/a:foobar
 	EOF
 
-	git grep -e "bar" --recurse-submodules HEAD^ > actual &&
+	git grep -e "bar" --recurse-submodules HEAD^ >actual &&
 	test_cmp expect actual
 '
 
@@ -113,7 +113,7 @@ test_expect_success 'grep tree HEAD^^' '
 	HEAD^^:b/b:bar
 	EOF
 
-	git grep -e "bar" --recurse-submodules HEAD^^ > actual &&
+	git grep -e "bar" --recurse-submodules HEAD^^ >actual &&
 	test_cmp expect actual
 '
 
@@ -123,49 +123,80 @@ test_expect_success 'grep tree and pathspecs' '
 	HEAD:submodule/sub/a:foobar
 	EOF
 
-	git grep -e "bar" --recurse-submodules HEAD -- submodule > actual &&
+	git grep -e "bar" --recurse-submodules HEAD -- submodule >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'grep recurse submodule colon in name' '
+	git init parent &&
+	test_when_finished "rm -rf parent" &&
+	echo "foobar" >"parent/fi:le" &&
+	git -C parent add "fi:le" &&
+	git -C parent commit -m "add fi:le" &&
+
+	git init "su:b" &&
+	test_when_finished "rm -rf su:b" &&
+	echo "foobar" >"su:b/fi:le" &&
+	git -C "su:b" add "fi:le" &&
+	git -C "su:b" commit -m "add fi:le" &&
+
+	git -C parent submodule add "../su:b" "su:b" &&
+	git -C parent commit -m "add submodule" &&
+
+	cat >expect <<-\EOF &&
+	fi:le:foobar
+	su:b/fi:le:foobar
+	EOF
+	git -C parent grep -e "foobar" --recurse-submodules >actual &&
+	test_cmp expect actual &&
+
+	cat >expect <<-\EOF &&
+	HEAD:fi:le:foobar
+	HEAD:su:b/fi:le:foobar
+	EOF
+	git -C parent grep -e "foobar" --recurse-submodules HEAD >actual &&
 	test_cmp expect actual
 '
 
 test_expect_success 'grep history with moved submoules' '
 	git init parent &&
+	test_when_finished "rm -rf parent" &&
 	echo "foobar" >parent/file &&
 	git -C parent add file &&
 	git -C parent commit -m "add file" &&
 
 	git init sub &&
+	test_when_finished "rm -rf sub" &&
 	echo "foobar" >sub/file &&
 	git -C sub add file &&
 	git -C sub commit -m "add file" &&
 
-	git -C parent submodule add ../sub &&
+	git -C parent submodule add ../sub dir/sub &&
 	git -C parent commit -m "add submodule" &&
 
 	cat >expect <<-\EOF &&
+	dir/sub/file:foobar
 	file:foobar
-	sub/file:foobar
 	EOF
-	git -C parent grep -e "foobar" --recurse-submodules > actual &&
+	git -C parent grep -e "foobar" --recurse-submodules >actual &&
 	test_cmp expect actual &&
 
-	git -C parent mv sub sub-moved &&
+	git -C parent mv dir/sub sub-moved &&
 	git -C parent commit -m "moved submodule" &&
 
 	cat >expect <<-\EOF &&
 	file:foobar
 	sub-moved/file:foobar
 	EOF
-	git -C parent grep -e "foobar" --recurse-submodules > actual &&
+	git -C parent grep -e "foobar" --recurse-submodules >actual &&
 	test_cmp expect actual &&
 
 	cat >expect <<-\EOF &&
+	HEAD^:dir/sub/file:foobar
 	HEAD^:file:foobar
-	HEAD^:sub/file:foobar
 	EOF
-	git -C parent grep -e "foobar" --recurse-submodules HEAD^ > actual &&
-	test_cmp expect actual &&
-
-	rm -rf parent sub
+	git -C parent grep -e "foobar" --recurse-submodules HEAD^ >actual &&
+	test_cmp expect actual
 '
 
 test_incompatible_with_recurse_submodules ()

-- 
2.8.0.rc3.226.g39d4020


  parent reply	other threads:[~2016-11-18 19:59 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     ` [PATCH v3 5/6] grep: enable recurse-submodules to work on <tree> objects Brandon Williams
2016-11-14 18:10       ` 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     ` Brandon Williams [this message]
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

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

  git send-email \
    --in-reply-to=1479499135-64269-1-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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.