Skip to content

Commit 445d2c5

Browse files
committed
Merge branch 'js/grep-patterntype-config'
"grep" learned to use a non-standard pattern type by default if a configuration variable tells it to. * js/grep-patterntype-config: grep: add a grep.patternType configuration setting
2 parents 2df9988 + 84befcd commit 445d2c5

File tree

5 files changed

+281
-42
lines changed

5 files changed

+281
-42
lines changed

Documentation/config.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,8 +1210,16 @@ gitweb.snapshot::
12101210
grep.lineNumber::
12111211
If set to true, enable '-n' option by default.
12121212

1213+
grep.patternType::
1214+
Set the default matching behavior. Using a value of 'basic', 'extended',
1215+
'fixed', or 'perl' will enable the '--basic-regexp', '--extended-regexp',
1216+
'--fixed-strings', or '--perl-regexp' option accordingly, while the
1217+
value 'default' will return to the default matching behavior.
1218+
12131219
grep.extendedRegexp::
1214-
If set to true, enable '--extended-regexp' option by default.
1220+
If set to true, enable '--extended-regexp' option by default. This
1221+
option is ignored when the 'grep.patternType' option is set to a value
1222+
other than 'default'.
12151223

12161224
gpg.program::
12171225
Use this custom program instead of "gpg" found on $PATH when

Documentation/git-grep.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ CONFIGURATION
4242
grep.lineNumber::
4343
If set to true, enable '-n' option by default.
4444

45+
grep.patternType::
46+
Set the default matching behavior. Using a value of 'basic', 'extended',
47+
'fixed', or 'perl' will enable the '--basic-regexp', '--extended-regexp',
48+
'--fixed-strings', or '--perl-regexp' option accordingly, while the
49+
value 'default' will return to the default matching behavior.
50+
4551
grep.extendedRegexp::
46-
If set to true, enable '--extended-regexp' option by default.
52+
If set to true, enable '--extended-regexp' option by default. This
53+
option is ignored when the 'grep.patternType' option is set to a value
54+
other than 'default'.
4755

4856

4957
OPTIONS

builtin/grep.c

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,53 @@ static int wait_all(void)
260260
}
261261
#endif
262262

263+
static int parse_pattern_type_arg(const char *opt, const char *arg)
264+
{
265+
if (!strcmp(arg, "default"))
266+
return GREP_PATTERN_TYPE_UNSPECIFIED;
267+
else if (!strcmp(arg, "basic"))
268+
return GREP_PATTERN_TYPE_BRE;
269+
else if (!strcmp(arg, "extended"))
270+
return GREP_PATTERN_TYPE_ERE;
271+
else if (!strcmp(arg, "fixed"))
272+
return GREP_PATTERN_TYPE_FIXED;
273+
else if (!strcmp(arg, "perl"))
274+
return GREP_PATTERN_TYPE_PCRE;
275+
die("bad %s argument: %s", opt, arg);
276+
}
277+
278+
static void grep_pattern_type_options(const int pattern_type, struct grep_opt *opt)
279+
{
280+
switch (pattern_type) {
281+
case GREP_PATTERN_TYPE_UNSPECIFIED:
282+
/* fall through */
283+
284+
case GREP_PATTERN_TYPE_BRE:
285+
opt->fixed = 0;
286+
opt->pcre = 0;
287+
opt->regflags &= ~REG_EXTENDED;
288+
break;
289+
290+
case GREP_PATTERN_TYPE_ERE:
291+
opt->fixed = 0;
292+
opt->pcre = 0;
293+
opt->regflags |= REG_EXTENDED;
294+
break;
295+
296+
case GREP_PATTERN_TYPE_FIXED:
297+
opt->fixed = 1;
298+
opt->pcre = 0;
299+
opt->regflags &= ~REG_EXTENDED;
300+
break;
301+
302+
case GREP_PATTERN_TYPE_PCRE:
303+
opt->fixed = 0;
304+
opt->pcre = 1;
305+
opt->regflags &= ~REG_EXTENDED;
306+
break;
307+
}
308+
}
309+
263310
static int grep_config(const char *var, const char *value, void *cb)
264311
{
265312
struct grep_opt *opt = cb;
@@ -270,12 +317,17 @@ static int grep_config(const char *var, const char *value, void *cb)
270317

271318
if (!strcmp(var, "grep.extendedregexp")) {
272319
if (git_config_bool(var, value))
273-
opt->regflags |= REG_EXTENDED;
320+
opt->extended_regexp_option = 1;
274321
else
275-
opt->regflags &= ~REG_EXTENDED;
322+
opt->extended_regexp_option = 0;
276323
return 0;
277324
}
278325

326+
if (!strcmp(var, "grep.patterntype")) {
327+
opt->pattern_type_option = parse_pattern_type_arg(var, value);
328+
return 0;
329+
}
330+
279331
if (!strcmp(var, "grep.linenumber")) {
280332
opt->linenum = git_config_bool(var, value);
281333
return 0;
@@ -669,14 +721,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
669721
int i;
670722
int dummy;
671723
int use_index = 1;
672-
enum {
673-
pattern_type_unspecified = 0,
674-
pattern_type_bre,
675-
pattern_type_ere,
676-
pattern_type_fixed,
677-
pattern_type_pcre,
678-
};
679-
int pattern_type = pattern_type_unspecified;
724+
int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED;
680725

681726
struct option options[] = {
682727
OPT_BOOLEAN(0, "cached", &cached,
@@ -703,18 +748,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
703748
"descend at most <depth> levels", PARSE_OPT_NONEG,
704749
NULL, 1 },
705750
OPT_GROUP(""),
706-
OPT_SET_INT('E', "extended-regexp", &pattern_type,
751+
OPT_SET_INT('E', "extended-regexp", &pattern_type_arg,
707752
"use extended POSIX regular expressions",
708-
pattern_type_ere),
709-
OPT_SET_INT('G', "basic-regexp", &pattern_type,
753+
GREP_PATTERN_TYPE_ERE),
754+
OPT_SET_INT('G', "basic-regexp", &pattern_type_arg,
710755
"use basic POSIX regular expressions (default)",
711-
pattern_type_bre),
712-
OPT_SET_INT('F', "fixed-strings", &pattern_type,
756+
GREP_PATTERN_TYPE_BRE),
757+
OPT_SET_INT('F', "fixed-strings", &pattern_type_arg,
713758
"interpret patterns as fixed strings",
714-
pattern_type_fixed),
715-
OPT_SET_INT('P', "perl-regexp", &pattern_type,
759+
GREP_PATTERN_TYPE_FIXED),
760+
OPT_SET_INT('P', "perl-regexp", &pattern_type_arg,
716761
"use Perl-compatible regular expressions",
717-
pattern_type_pcre),
762+
GREP_PATTERN_TYPE_PCRE),
718763
OPT_GROUP(""),
719764
OPT_BOOLEAN('n', "line-number", &opt.linenum, "show line numbers"),
720765
OPT_NEGBIT('h', NULL, &opt.pathname, "don't show filenames", 1),
@@ -799,6 +844,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
799844
opt.header_tail = &opt.header_list;
800845
opt.regflags = REG_NEWLINE;
801846
opt.max_depth = -1;
847+
opt.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
848+
opt.extended_regexp_option = 0;
802849

803850
strcpy(opt.color_context, "");
804851
strcpy(opt.color_filename, "");
@@ -824,28 +871,13 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
824871
PARSE_OPT_KEEP_DASHDASH |
825872
PARSE_OPT_STOP_AT_NON_OPTION |
826873
PARSE_OPT_NO_INTERNAL_HELP);
827-
switch (pattern_type) {
828-
case pattern_type_fixed:
829-
opt.fixed = 1;
830-
opt.pcre = 0;
831-
break;
832-
case pattern_type_bre:
833-
opt.fixed = 0;
834-
opt.pcre = 0;
835-
opt.regflags &= ~REG_EXTENDED;
836-
break;
837-
case pattern_type_ere:
838-
opt.fixed = 0;
839-
opt.pcre = 0;
840-
opt.regflags |= REG_EXTENDED;
841-
break;
842-
case pattern_type_pcre:
843-
opt.fixed = 0;
844-
opt.pcre = 1;
845-
break;
846-
default:
847-
break; /* nothing */
848-
}
874+
875+
if (pattern_type_arg != GREP_PATTERN_TYPE_UNSPECIFIED)
876+
grep_pattern_type_options(pattern_type_arg, &opt);
877+
else if (opt.pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
878+
grep_pattern_type_options(opt.pattern_type_option, &opt);
879+
else if (opt.extended_regexp_option)
880+
grep_pattern_type_options(GREP_PATTERN_TYPE_ERE, &opt);
849881

850882
if (use_index && !startup_info->have_repository)
851883
/* die the same way as if we did it at the beginning */

grep.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ enum grep_expr_node {
5858
GREP_NODE_OR
5959
};
6060

61+
enum grep_pattern_type {
62+
GREP_PATTERN_TYPE_UNSPECIFIED = 0,
63+
GREP_PATTERN_TYPE_BRE,
64+
GREP_PATTERN_TYPE_ERE,
65+
GREP_PATTERN_TYPE_FIXED,
66+
GREP_PATTERN_TYPE_PCRE
67+
};
68+
6169
struct grep_expr {
6270
enum grep_expr_node node;
6371
unsigned hit;
@@ -103,6 +111,8 @@ struct grep_opt {
103111
int max_depth;
104112
int funcname;
105113
int funcbody;
114+
int extended_regexp_option;
115+
int pattern_type_option;
106116
char color_context[COLOR_MAXLEN];
107117
char color_filename[COLOR_MAXLEN];
108118
char color_function[COLOR_MAXLEN];

0 commit comments

Comments
 (0)