Skip to content

Commit 131f3c9

Browse files
peffgitster
authored andcommitted
grep: treat revs the same for --untracked as for --no-index
git-grep has always disallowed grepping in a tree (as opposed to the working directory) with both --untracked and --no-index. But we traditionally did so by first collecting the revs, and then complaining when any were provided. The --no-index option recently learned to detect revs much earlier. This has two user-visible effects: - we don't bother to resolve revision names at all. So when there's a rev/path ambiguity, we always choose to treat it as a path. - likewise, when you do specify a revision without "--", the error you get is "no such path" and not "--untracked cannot be used with revs". The rationale for doing this with --no-index is that it is meant to be used outside a repository, and so parsing revs at all does not make sense. This patch gives --untracked the same treatment. While it _is_ meant to be used in a repository, it is explicitly about grepping the non-repository contents. Telling the user "we found a rev, but you are not allowed to use revs" is not really helpful compared to "we treated your argument as a path, and could not find it". Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73fc7b6 commit 131f3c9

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

builtin/grep.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
967967
int dummy;
968968
int use_index = 1;
969969
int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED;
970+
int allow_revs;
970971

971972
struct option options[] = {
972973
OPT_BOOL(0, "cached", &cached,
@@ -1165,6 +1166,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11651166
* to it must resolve as a rev. If not, then we stop at the first
11661167
* non-rev and assume everything else is a path.
11671168
*/
1169+
allow_revs = use_index && !untracked;
11681170
for (i = 0; i < argc; i++) {
11691171
const char *arg = argv[i];
11701172
unsigned char sha1[20];
@@ -1176,9 +1178,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11761178
break;
11771179
}
11781180

1179-
if (!use_index) {
1181+
if (!allow_revs) {
11801182
if (seen_dashdash)
1181-
die(_("--no-index cannot be used with revs"));
1183+
die(_("--no-index or --untracked cannot be used with revs"));
11821184
break;
11831185
}
11841186

@@ -1201,7 +1203,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
12011203
if (!seen_dashdash) {
12021204
int j;
12031205
for (j = i; j < argc; j++)
1204-
verify_filename(prefix, argv[j], j == i && use_index);
1206+
verify_filename(prefix, argv[j], j == i && allow_revs);
12051207
}
12061208

12071209
parse_pathspec(&pathspec, 0,
@@ -1273,8 +1275,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
12731275

12741276
if (!use_index || untracked) {
12751277
int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
1276-
if (list.nr)
1277-
die(_("--no-index or --untracked cannot be used with revs."));
12781278
hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
12791279
} else if (0 <= opt_exclude) {
12801280
die(_("--[no-]exclude-standard cannot be used for tracked contents."));

t/t7810-grep.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ test_expect_success 'grep --no-index pattern -- path' '
10321032

10331033
test_expect_success 'grep --no-index complains of revs' '
10341034
test_must_fail git grep --no-index o master -- 2>err &&
1035-
test_i18ngrep "no-index cannot be used with revs" err
1035+
test_i18ngrep "cannot be used with revs" err
10361036
'
10371037

10381038
test_expect_success 'grep --no-index prefers paths to revs' '

0 commit comments

Comments
 (0)