Skip to content

Commit d0ffc06

Browse files
peffgitster
authored andcommitted
grep: avoid resolving revision names in --no-index case
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b5b8113 commit d0ffc06

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

builtin/grep.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11761176
break;
11771177
}
11781178

1179+
if (!use_index) {
1180+
if (seen_dashdash)
1181+
die(_("--no-index cannot be used with revs"));
1182+
break;
1183+
}
1184+
11791185
if (get_sha1_with_context(arg, 0, sha1, &oc)) {
11801186
if (seen_dashdash)
11811187
die(_("unable to resolve revision: %s"), arg);

t/t7810-grep.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,19 @@ test_expect_success 'grep --no-index pattern -- path' '
10301030
)
10311031
'
10321032

1033+
test_expect_success 'grep --no-index complains of revs' '
1034+
test_must_fail git grep --no-index o master -- 2>err &&
1035+
test_i18ngrep "no-index cannot be used with revs" err
1036+
'
1037+
1038+
test_expect_success 'grep --no-index prefers paths to revs' '
1039+
test_when_finished "rm -f master" &&
1040+
echo content >master &&
1041+
echo master:content >expect &&
1042+
git grep --no-index o master >actual &&
1043+
test_cmp expect actual
1044+
'
1045+
10331046
cat >expected <<EOF
10341047
hello.c:int main(int argc, const char **argv)
10351048
hello.c: printf("Hello world.\n");

0 commit comments

Comments
 (0)