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 <gitster@pobox.com>
Junio C Hamano committed
May 9, 2011 at 18:48 UTC
cca2c172e0c37eff8e743b63016b6bc604b38a7d
1 file changed
+43
-13
builtin/grep.c
+43
-13
@@ -753,6 +753,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
753
int i;
754
int dummy;
755
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
+
765
struct option options[] = {
766
OPT_BOOLEAN(0, "cached", &cached,
767
"search in index instead of in the work tree"),
@@ -774,15 +783,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
783
"descend at most <depth> levels", PARSE_OPT_NONEG,
784
NULL, 1 },
785
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),
798
OPT_GROUP(""),
799
OPT_BOOLEAN('n', "line-number", &opt.linenum, "show line numbers"),
800
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)
900
PARSE_OPT_KEEP_DASHDASH |
901
PARSE_OPT_STOP_AT_NON_OPTION |
902
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
+ }
925
926
if (use_index && !startup_info->have_repository)
927
/* 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)
959
960
if (!opt.pattern_list)
961
die(_("no pattern given."));
928
- if (opt.regflags != REG_NEWLINE && opt.pcre)
929
- die(_("cannot mix --extended-regexp and --perl-regexp"));
962
if (!opt.fixed && opt.ignore_case)
963
opt.regflags |= REG_ICASE;
932
- if ((opt.regflags != REG_NEWLINE || opt.pcre) && opt.fixed)
933
- die(_("cannot mix --fixed-strings and regexp"));
964
965
#ifndef NO_PTHREADS
966
if (online_cpus() == 1 || !grep_threads_ok(&opt))