git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/7] grep: fix "--" rev/pathspec disambiguation

From
Jeff King <peff@peff.net>
Date
Feb 14, 2017, 06:05 UTC
Message-ID
<20170214060555.yzh6hhi2t7pkeqvi@sigill.intra.peff.net>
In-Reply-To
<20170214060021.einv7372exbxa23z@sigill.intra.peff.net>

If we see "git grep pattern rev -- file" then we apply the usual rev/pathspec disambiguation rules: any "rev" before the "--" must be a revision, and we do not need to apply the verify_non_filename() check.

But there are two bugs here:
  1. We keep a seen_dashdash flag to handle this case, but
     we set it in the same left-to-right pass over the
     arguments in which we parse "rev".
     So when we see "rev", we do not yet know that there is
     a "--", and we mistakenly complain if there is a
     matching file.
     We can fix this by making a preliminary pass over the
     arguments to find the "--", and only then checking the rev
     arguments.
  2. If we can't resolve "rev" but there isn't a dashdash,
     that's OK. We treat it like a path, and complain later
     if it doesn't exist.
     But if there _is_ a dashdash, then we know it must be a
     rev, and should treat it as such, complaining if it
     does not resolve. The current code instead ignores it
     and tries to treat it like a path.

This patch fixes both bugs, and tries to comment the parsing flow a bit better.

It adds tests that cover the two bugs, but also some related situations (which already worked, but this confirms that our fixes did not break anything).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/grep.c  | 29 ++++++++++++++++++++++++-----
 t/t7810-grep.sh | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 5 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 461347adb..e83b33bda 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -1149,7 +1149,22 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 
 	compile_grep_patterns(&opt);
 
-	/* Check revs and then paths */
+	/*
+	 * We have to find "--" in a separate pass, because its presence
+	 * influences how we will parse arguments that come before it.
+	 */
+	for (i = 0; i < argc; i++) {
+		if (!strcmp(argv[i], "--")) {
+			seen_dashdash = 1;
+			break;
+		}
+	}
+
+	/*
+	 * Resolve any rev arguments. If we have a dashdash, then everything up
+	 * to it must resolve as a rev. If not, then we stop at the first
+	 * non-rev and assume everything else is a path.
+	 */
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
 		unsigned char sha1[20];
@@ -1158,13 +1173,14 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 
 		if (!strcmp(arg, "--")) {
 			i++;
-			seen_dashdash = 1;
 			break;
 		}
 
-		/* Stop at the first non-rev */
-		if (get_sha1_with_context(arg, 0, sha1, &oc))
+		if (get_sha1_with_context(arg, 0, sha1, &oc)) {
+			if (seen_dashdash)
+				die(_("unable to resolve revision: %s"), arg);
 			break;
+		}
 
 		object = parse_object_or_die(sha1, arg);
 		if (!seen_dashdash)
@@ -1172,7 +1188,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
 	}
 
-	/* The rest are paths */
+	/*
+	 * Anything left over is presumed to be a path. But in the non-dashdash
+	 * "do what I mean" case, we verify and complain when that isn't true.
+	 */
 	if (!seen_dashdash) {
 		int j;
 		for (j = i; j < argc; j++)
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 2c1f7373e..a6011f9b1 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -982,6 +982,39 @@ test_expect_success 'grep -e -- -- path' '
 	test_cmp expected actual
 '
 
+test_expect_success 'dashdash disambiguates rev as rev' '
+	test_when_finished "rm -f master" &&
+	echo content >master &&
+	echo master:hello.c >expect &&
+	git grep -l o master -- hello.c >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'dashdash disambiguates pathspec as pathspec' '
+	test_when_finished "git rm -f master" &&
+	echo content >master &&
+	git add master &&
+	echo master:content >expect &&
+	git grep o -- master >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'report bogus arg without dashdash' '
+	test_must_fail git grep o does-not-exist
+'
+
+test_expect_success 'report bogus rev with dashdash' '
+	test_must_fail git grep o hello.c --
+'
+
+test_expect_success 'allow non-existent path with dashdash' '
+	# We need a real match so grep exits with success.
+	tree=$(git ls-tree HEAD |
+	       sed s/hello.c/not-in-working-tree/ |
+	       git mktree) &&
+	git grep o "$tree" -- not-in-working-tree
+'
+
 test_expect_success 'grep --no-index pattern -- path' '
 	rm -fr non &&
 	mkdir -p non/git &&
-- 
2.12.0.rc1.471.ga79ec8999
Previous: Brandon WilliamsNext: Brandon Williams
Message 10 of 22 in “grep: do not unnecessarily query repo for "--"”
  1. grep: do not unnecessarily query repo for "--"Jonathan Tan, Feb 14, 2017
  2. Jeff KingFeb 14, 2017
  3. 0/7 grep rev/path parsing fixesJeff King, Feb 14, 2017
  4. 1/7 grep: move thread initialization a little lowerJeff King, Feb 14, 2017
  5. Brandon WilliamsFeb 14, 2017
  6. 2/7 grep: do not unnecessarily query repo for "--"Jeff King, Feb 14, 2017
  7. 3/7 t7810: make "--no-index --" test more robustJeff King, Feb 14, 2017
  8. 4/7 grep: re-order rev-parsing loopJeff King, Feb 14, 2017
  9. Brandon WilliamsFeb 14, 2017
  10. 5/7 grep: fix "--" rev/pathspec disambiguationJeff King, Feb 14, 2017
  11. Brandon WilliamsFeb 14, 2017
  12. Jeff KingFeb 14, 2017
  13. Junio C HamanoFeb 14, 2017
  14. 6/7 grep: avoid resolving revision names in --no-index caseJeff King, Feb 14, 2017
  15. Jonathan TanFeb 14, 2017
  16. Jeff KingFeb 14, 2017
  17. Jonathan TanFeb 14, 2017
  18. 8/7 grep: treat revs the same for --untracked as for --no-indexJeff King, Feb 14, 2017
  19. Junio C HamanoFeb 14, 2017
  20. 7/7 grep: do not diagnose misspelt revs with --no-indexJeff King, Feb 14, 2017
  21. Jeff KingFeb 14, 2017
  22. Jonathan TanFeb 14, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.