Skip to content

Commit cca2c17

Browse files
committed
git-grep: do not die upon -F/-P when grep.extendedRegexp is set.
The previous one made "git grep -P" fail when grep.extendedRegexp is enabled. That is a no-starter. The option on the command line should just make the command ignore the configured default. The handling of "-F" in the existing code has the same problem. Instead of saying -G/-F/-E/-P incompatible with each other, just allow the last one win. That way, you can have "[alias] gr = grep -P" and use Pcre for everyday work e.g. "git gr ':i?foo'", and append -G to the aliased command line to override it e.g. "git gr -G '[Ff][Oo][Oo]'". Signed-off-by: Junio C Hamano <[email protected]>
1 parent 258a618 commit cca2c17

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

builtin/grep.c

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
753753
int i;
754754
int dummy;
755755
int use_index = 1;
756+
enum {
757+
pattern_type_unspecified = 0,
758+
pattern_type_bre,
759+
pattern_type_ere,
760+
pattern_type_fixed,
761+
pattern_type_pcre,
762+
};
763+
int pattern_type = pattern_type_unspecified;
764+
756765
struct option options[] = {
757766
OPT_BOOLEAN(0, "cached", &cached,
758767
"search in index instead of in the work tree"),
@@ -774,15 +783,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
774783
"descend at most <depth> levels", PARSE_OPT_NONEG,
775784
NULL, 1 },
776785
OPT_GROUP(""),
777-
OPT_BIT('E', "extended-regexp", &opt.regflags,
778-
"use extended POSIX regular expressions", REG_EXTENDED),
779-
OPT_NEGBIT('G', "basic-regexp", &opt.regflags,
780-
"use basic POSIX regular expressions (default)",
781-
REG_EXTENDED),
782-
OPT_BOOLEAN('F', "fixed-strings", &opt.fixed,
783-
"interpret patterns as fixed strings"),
784-
OPT_BOOLEAN('P', "perl-regexp", &opt.pcre,
785-
"use Perl-compatible regular expressions"),
786+
OPT_SET_INT('E', "extended-regexp", &pattern_type,
787+
"use extended POSIX regular expressions",
788+
pattern_type_ere),
789+
OPT_SET_INT('G', "basic-regexp", &pattern_type,
790+
"use basic POSIX regular expressions (default)",
791+
pattern_type_bre),
792+
OPT_SET_INT('F', "fixed-strings", &pattern_type,
793+
"interpret patterns as fixed strings",
794+
pattern_type_fixed),
795+
OPT_SET_INT('P', "perl-regexp", &pattern_type,
796+
"use Perl-compatible regular expressions",
797+
pattern_type_pcre),
786798
OPT_GROUP(""),
787799
OPT_BOOLEAN('n', "line-number", &opt.linenum, "show line numbers"),
788800
OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1),
@@ -888,6 +900,28 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
888900
PARSE_OPT_KEEP_DASHDASH |
889901
PARSE_OPT_STOP_AT_NON_OPTION |
890902
PARSE_OPT_NO_INTERNAL_HELP);
903+
switch (pattern_type) {
904+
case pattern_type_fixed:
905+
opt.fixed = 1;
906+
opt.pcre = 0;
907+
break;
908+
case pattern_type_bre:
909+
opt.fixed = 0;
910+
opt.pcre = 0;
911+
opt.regflags &= ~REG_EXTENDED;
912+
break;
913+
case pattern_type_ere:
914+
opt.fixed = 0;
915+
opt.pcre = 0;
916+
opt.regflags |= REG_EXTENDED;
917+
break;
918+
case pattern_type_pcre:
919+
opt.fixed = 0;
920+
opt.pcre = 1;
921+
break;
922+
default:
923+
break; /* nothing */
924+
}
891925

892926
if (use_index && !startup_info->have_repository)
893927
/* die the same way as if we did it at the beginning */
@@ -925,12 +959,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
925959

926960
if (!opt.pattern_list)
927961
die(_("no pattern given."));
928-
if (opt.regflags != REG_NEWLINE && opt.pcre)
929-
die(_("cannot mix --extended-regexp and --perl-regexp"));
930962
if (!opt.fixed && opt.ignore_case)
931963
opt.regflags |= REG_ICASE;
932-
if ((opt.regflags != REG_NEWLINE || opt.pcre) && opt.fixed)
933-
die(_("cannot mix --fixed-strings and regexp"));
934964

935965
#ifndef NO_PTHREADS
936966
if (online_cpus() == 1 || !grep_threads_ok(&opt))

0 commit comments

Comments
 (0)