git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH for NEXT] grep: do not unnecessarily query repo for "--"
@ 2017-02-14  0:11 Jonathan Tan
  2017-02-14  1:20 ` Jeff King
  0 siblings, 1 reply; 22+ messages in thread
From: Jonathan Tan @ 2017-02-14  0:11 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan, peff, gitster

When running a command of the form

  git grep --no-index pattern -- path

in the absence of a Git repository, an error message will be printed:

  fatal: BUG: setup_git_env called without repository

This is because "git grep" tries to interpret "--" as a rev. "git grep"
has always tried to first interpret "--" as a rev for at least a few
years, but this issue was upgraded from a pessimization to a bug in
commit 59332d1 ("Resurrect "git grep --no-index"", 2010-02-06), which
calls get_sha1 regardless of whether --no-index was specified. This bug
appeared to be benign until commit b1ef400 ("setup_git_env: avoid blind
fall-back to ".git"", 2016-10-20) when Git was taught to die in this
situation.  (This "git grep" bug appears to be one of the bugs that
commit b1ef400 is meant to flush out.)

Therefore, always interpret "--" as signaling the end of options,
instead of trying to interpret it as a rev first.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---

There is probably a similar bug for commands of the form:

  git grep --no-index pattern foo

If there is a repo and "foo" is a rev, the "--no-index or --untracked
cannot be used with revs." error would occur. If there is a repo and
"foo" is not a rev, this command would proceed as usual. If there is no
repo, the "setup_git_env called without repository" error would occur.
(This is my understanding from reading the code - I haven't tested it
out.)

This patch does not fix this similar bug, but I decided to send it out
anyway because it still fixes a bug and unlocks the ability to
specify paths with "git grep --no-index".

 builtin/grep.c  |  9 +++++----
 t/t7810-grep.sh | 12 ++++++++++++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 2c727ef49..1b68d1638 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -1154,6 +1154,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		const char *arg = argv[i];
 		unsigned char sha1[20];
 		struct object_context oc;
+		if (!strcmp(arg, "--")) {
+			i++;
+			seen_dashdash = 1;
+			break;
+		}
 		/* Is it a rev? */
 		if (!get_sha1_with_context(arg, 0, sha1, &oc)) {
 			struct object *object = parse_object_or_die(sha1, arg);
@@ -1162,10 +1167,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
 			continue;
 		}
-		if (!strcmp(arg, "--")) {
-			i++;
-			seen_dashdash = 1;
-		}
 		break;
 	}
 
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 19f0108f8..29202f0e7 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -982,6 +982,18 @@ test_expect_success 'grep -e -- -- path' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep --no-index pattern -- path' '
+	rm -fr non &&
+	mkdir -p non/git &&
+	(
+		GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+		export GIT_CEILING_DIRECTORIES &&
+		cd non/git &&
+		echo hello >hello &&
+		git grep --no-index o -- .
+	)
+'
+
 cat >expected <<EOF
 hello.c:int main(int argc, const char **argv)
 hello.c:	printf("Hello world.\n");
-- 
2.11.0.483.g087da7b7c-goog


^ permalink raw reply related	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2017-02-14 21:59 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-14  0:11 [PATCH for NEXT] grep: do not unnecessarily query repo for "--" Jonathan Tan
2017-02-14  1:20 ` Jeff King
2017-02-14  6:00   ` [PATCH 0/7] grep rev/path parsing fixes Jeff King
2017-02-14  6:02     ` [PATCH 1/7] grep: move thread initialization a little lower Jeff King
2017-02-14 18:46       ` Brandon Williams
2017-02-14  6:03     ` [PATCH 2/7] grep: do not unnecessarily query repo for "--" Jeff King
2017-02-14  6:03     ` [PATCH 3/7] t7810: make "--no-index --" test more robust Jeff King
2017-02-14  6:04     ` [PATCH 4/7] grep: re-order rev-parsing loop Jeff King
2017-02-14 18:48       ` Brandon Williams
2017-02-14  6:05     ` [PATCH 5/7] grep: fix "--" rev/pathspec disambiguation Jeff King
2017-02-14 18:56       ` Brandon Williams
2017-02-14 19:51         ` Jeff King
2017-02-14 19:18       ` Junio C Hamano
2017-02-14  6:07     ` [PATCH 6/7] grep: avoid resolving revision names in --no-index case Jeff King
2017-02-14 16:53       ` Jonathan Tan
2017-02-14 18:04         ` Jeff King
2017-02-14 18:19           ` Jonathan Tan
2017-02-14 21:54             ` [PATCH 8/7] grep: treat revs the same for --untracked as for --no-index Jeff King
2017-02-14 21:58               ` Junio C Hamano
2017-02-14  6:08     ` [PATCH 7/7] grep: do not diagnose misspelt revs with --no-index Jeff King
2017-02-14  6:10     ` [PATCH 0/7] grep rev/path parsing fixes Jeff King
2017-02-14 16:58     ` Jonathan Tan

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