diff options
-rw-r--r-- | builtin/grep.c | 29 | ||||
-rwxr-xr-x | t/t7810-grep.sh | 33 |
2 files changed, 57 insertions, 5 deletions
diff --git a/builtin/grep.c b/builtin/grep.c index 461347adb0..e83b33bdaa 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 2c1f7373e6..a6011f9b1d 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 && |