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>
This commit is contained in:
Junio C Hamano 2011-05-09 18:48:36 -07:00
Родитель 258a618849
Коммит cca2c172e0
1 изменённых файлов: 43 добавлений и 13 удалений

Просмотреть файл

@ -753,6 +753,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
int i; int i;
int dummy; int dummy;
int use_index = 1; int use_index = 1;
enum {
pattern_type_unspecified = 0,
pattern_type_bre,
pattern_type_ere,
pattern_type_fixed,
pattern_type_pcre,
};
int pattern_type = pattern_type_unspecified;
struct option options[] = { struct option options[] = {
OPT_BOOLEAN(0, "cached", &cached, OPT_BOOLEAN(0, "cached", &cached,
"search in index instead of in the work tree"), "search in index instead of in the work tree"),
@ -774,15 +783,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
"descend at most <depth> levels", PARSE_OPT_NONEG, "descend at most <depth> levels", PARSE_OPT_NONEG,
NULL, 1 }, NULL, 1 },
OPT_GROUP(""), OPT_GROUP(""),
OPT_BIT('E', "extended-regexp", &opt.regflags, OPT_SET_INT('E', "extended-regexp", &pattern_type,
"use extended POSIX regular expressions", REG_EXTENDED), "use extended POSIX regular expressions",
OPT_NEGBIT('G', "basic-regexp", &opt.regflags, pattern_type_ere),
"use basic POSIX regular expressions (default)", OPT_SET_INT('G', "basic-regexp", &pattern_type,
REG_EXTENDED), "use basic POSIX regular expressions (default)",
OPT_BOOLEAN('F', "fixed-strings", &opt.fixed, pattern_type_bre),
"interpret patterns as fixed strings"), OPT_SET_INT('F', "fixed-strings", &pattern_type,
OPT_BOOLEAN('P', "perl-regexp", &opt.pcre, "interpret patterns as fixed strings",
"use Perl-compatible regular expressions"), pattern_type_fixed),
OPT_SET_INT('P', "perl-regexp", &pattern_type,
"use Perl-compatible regular expressions",
pattern_type_pcre),
OPT_GROUP(""), OPT_GROUP(""),
OPT_BOOLEAN('n', "line-number", &opt.linenum, "show line numbers"), OPT_BOOLEAN('n', "line-number", &opt.linenum, "show line numbers"),
OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1), 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)
PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_DASHDASH |
PARSE_OPT_STOP_AT_NON_OPTION | PARSE_OPT_STOP_AT_NON_OPTION |
PARSE_OPT_NO_INTERNAL_HELP); PARSE_OPT_NO_INTERNAL_HELP);
switch (pattern_type) {
case pattern_type_fixed:
opt.fixed = 1;
opt.pcre = 0;
break;
case pattern_type_bre:
opt.fixed = 0;
opt.pcre = 0;
opt.regflags &= ~REG_EXTENDED;
break;
case pattern_type_ere:
opt.fixed = 0;
opt.pcre = 0;
opt.regflags |= REG_EXTENDED;
break;
case pattern_type_pcre:
opt.fixed = 0;
opt.pcre = 1;
break;
default:
break; /* nothing */
}
if (use_index && !startup_info->have_repository) if (use_index && !startup_info->have_repository)
/* die the same way as if we did it at the beginning */ /* 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)
if (!opt.pattern_list) if (!opt.pattern_list)
die(_("no pattern given.")); die(_("no pattern given."));
if (opt.regflags != REG_NEWLINE && opt.pcre)
die(_("cannot mix --extended-regexp and --perl-regexp"));
if (!opt.fixed && opt.ignore_case) if (!opt.fixed && opt.ignore_case)
opt.regflags |= REG_ICASE; opt.regflags |= REG_ICASE;
if ((opt.regflags != REG_NEWLINE || opt.pcre) && opt.fixed)
die(_("cannot mix --fixed-strings and regexp"));
#ifndef NO_PTHREADS #ifndef NO_PTHREADS
if (online_cpus() == 1 || !grep_threads_ok(&opt)) if (online_cpus() == 1 || !grep_threads_ok(&opt))