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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 14, 2017 at 16:54 UTC 131f3c96d265d965d6c93280f9666b23b384802c
2 files changed +6 -6
builtin/grep.c
+5 -5
@@ -967,6 +967,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
967 int dummy;
968 int use_index = 1;
969 int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED;
970 + int allow_revs;
971
972 struct option options[] = {
973 OPT_BOOL(0, "cached", &cached,
@@ -1165,6 +1166,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1166 * to it must resolve as a rev. If not, then we stop at the first
1167 * non-rev and assume everything else is a path.
1168 */
1169 + allow_revs = use_index && !untracked;
1170 for (i = 0; i < argc; i++) {
1171 const char *arg = argv[i];
1172 unsigned char sha1[20];
@@ -1176,9 +1178,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1178 break;
1179 }
1180
1179 - if (!use_index) {
1181 + if (!allow_revs) {
1182 if (seen_dashdash)
1181 - die(_("--no-index cannot be used with revs"));
1183 + die(_("--no-index or --untracked cannot be used with revs"));
1184 break;
1185 }
1186
@@ -1201,7 +1203,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1203 if (!seen_dashdash) {
1204 int j;
1205 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);
1207 }
1208
1209 parse_pathspec(&pathspec, 0,
@@ -1273,8 +1275,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1275
1276 if (!use_index || untracked) {
1277 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."));
1278 hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
1279 } else if (0 <= opt_exclude) {
1280 die(_("--[no-]exclude-standard cannot be used for tracked contents."));
t/t7810-grep.sh
+1 -1
@@ -1032,7 +1032,7 @@ test_expect_success 'grep --no-index pattern -- path' '
1032
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
1035 + test_i18ngrep "cannot be used with revs" err
1036 '
1037
1038 test_expect_success 'grep --no-index prefers paths to revs' '