grep: avoid resolving revision names in --no-index case
authorJeff King <peff@peff.net>
Tue, 14 Feb 2017 06:07:29 +0000 (01:07 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 Feb 2017 19:26:37 +0000 (11:26 -0800)
We disallow the use of revisions with --no-index, but we
don't actually check and complain until well after we've
parsed the revisions.

This is the cause of a few problems:

1. We shouldn't be calling get_sha1() at all when we aren't
in a repository, as it might access the ref or object
databases. For now, this should generally just return
failure, but eventually it will become a BUG().

2. When there's a "--" disambiguator and you're outside a
repository, we'll complain early with "unable to resolve
revision". But we can give a much more specific error.

3. When there isn't a "--" disambiguator, we still do the
normal rev/path checks. This is silly, as we know we
cannot have any revs with --no-index. Everything we see
must be a path.

Outside of a repository this doesn't matter (since we
know it won't resolve), but inside one, we may complain
unnecessarily if a filename happens to also match a
refname.

This patch skips the get_sha1() call entirely in the
no-index case, and behaves as if it failed (with the
exception of giving a better error message).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/grep.c
t/t7810-grep.sh
index e83b33bdaa784320211fe8f1e17f2cbdccfe7f25..c4c63259412730b26ab599f433b252f069d17571 100644 (file)
@@ -1176,6 +1176,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        break;
                }
 
+               if (!use_index) {
+                       if (seen_dashdash)
+                               die(_("--no-index cannot be used with revs"));
+                       break;
+               }
+
                if (get_sha1_with_context(arg, 0, sha1, &oc)) {
                        if (seen_dashdash)
                                die(_("unable to resolve revision: %s"), arg);
index a6011f9b1d60f36a2e7d998482a14c9b3df0adde..c051c7ee80801670dd33c564e57f9b44621468d6 100755 (executable)
@@ -1030,6 +1030,19 @@ test_expect_success 'grep --no-index pattern -- path' '
        )
 '
 
+test_expect_success 'grep --no-index complains of revs' '
+       test_must_fail git grep --no-index o master -- 2>err &&
+       test_i18ngrep "no-index cannot be used with revs" err
+'
+
+test_expect_success 'grep --no-index prefers paths to revs' '
+       test_when_finished "rm -f master" &&
+       echo content >master &&
+       echo master:content >expect &&
+       git grep --no-index o master >actual &&
+       test_cmp expect actual
+'
+
 cat >expected <<EOF
 hello.c:int main(int argc, const char **argv)
 hello.c:       printf("Hello world.\n");