Skip to content

Commit d2f4469

Browse files
committed
Merge branch 'jc/grep-pcre-loose-ends'
"git log -F -E --grep='<ere>'" failed to use the given <ere> pattern as extended regular expression, and instead looked for the string literally. The early part of this series is a fix for it; the latter part teaches log to respect the grep.* configuration. * jc/grep-pcre-loose-ends: log: honor grep.* configuration log --grep: accept --basic-regexp and --perl-regexp log --grep: use the same helper to set -E/-F options as "git grep" revisions: initialize revs->grep_filter using grep_init() grep: move pattern-type bits support to top-level grep.[ch] grep: move the configuration parsing logic to grep.[ch] builtin/grep.c: make configuration callback more reusable
2 parents fdb4d27 + 0657bcb commit d2f4469

File tree

7 files changed

+225
-129
lines changed

7 files changed

+225
-129
lines changed

Documentation/rev-list-options.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ if it is part of the log message.
7979

8080
Match the regexp limiting patterns without regard to letters case.
8181

82+
--basic-regexp::
83+
84+
Consider the limiting patterns to be basic regular expressions;
85+
this is the default.
86+
8287
-E::
8388
--extended-regexp::
8489

@@ -91,6 +96,11 @@ if it is part of the log message.
9196
Consider the limiting patterns to be fixed strings (don't interpret
9297
pattern as a regular expression).
9398

99+
--perl-regexp::
100+
101+
Consider the limiting patterns to be Perl-compatible regexp.
102+
Requires libpcre to be compiled in.
103+
94104
--remove-empty::
95105

96106
Stop when a given path disappears from the tree.

builtin/grep.c

Lines changed: 9 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -261,103 +261,12 @@ static int wait_all(void)
261261
}
262262
#endif
263263

264-
static int parse_pattern_type_arg(const char *opt, const char *arg)
264+
static int grep_cmd_config(const char *var, const char *value, void *cb)
265265
{
266-
if (!strcmp(arg, "default"))
267-
return GREP_PATTERN_TYPE_UNSPECIFIED;
268-
else if (!strcmp(arg, "basic"))
269-
return GREP_PATTERN_TYPE_BRE;
270-
else if (!strcmp(arg, "extended"))
271-
return GREP_PATTERN_TYPE_ERE;
272-
else if (!strcmp(arg, "fixed"))
273-
return GREP_PATTERN_TYPE_FIXED;
274-
else if (!strcmp(arg, "perl"))
275-
return GREP_PATTERN_TYPE_PCRE;
276-
die("bad %s argument: %s", opt, arg);
277-
}
278-
279-
static void grep_pattern_type_options(const int pattern_type, struct grep_opt *opt)
280-
{
281-
switch (pattern_type) {
282-
case GREP_PATTERN_TYPE_UNSPECIFIED:
283-
/* fall through */
284-
285-
case GREP_PATTERN_TYPE_BRE:
286-
opt->fixed = 0;
287-
opt->pcre = 0;
288-
opt->regflags &= ~REG_EXTENDED;
289-
break;
290-
291-
case GREP_PATTERN_TYPE_ERE:
292-
opt->fixed = 0;
293-
opt->pcre = 0;
294-
opt->regflags |= REG_EXTENDED;
295-
break;
296-
297-
case GREP_PATTERN_TYPE_FIXED:
298-
opt->fixed = 1;
299-
opt->pcre = 0;
300-
opt->regflags &= ~REG_EXTENDED;
301-
break;
302-
303-
case GREP_PATTERN_TYPE_PCRE:
304-
opt->fixed = 0;
305-
opt->pcre = 1;
306-
opt->regflags &= ~REG_EXTENDED;
307-
break;
308-
}
309-
}
310-
311-
static int grep_config(const char *var, const char *value, void *cb)
312-
{
313-
struct grep_opt *opt = cb;
314-
char *color = NULL;
315-
316-
if (userdiff_config(var, value) < 0)
317-
return -1;
318-
319-
if (!strcmp(var, "grep.extendedregexp")) {
320-
if (git_config_bool(var, value))
321-
opt->extended_regexp_option = 1;
322-
else
323-
opt->extended_regexp_option = 0;
324-
return 0;
325-
}
326-
327-
if (!strcmp(var, "grep.patterntype")) {
328-
opt->pattern_type_option = parse_pattern_type_arg(var, value);
329-
return 0;
330-
}
331-
332-
if (!strcmp(var, "grep.linenumber")) {
333-
opt->linenum = git_config_bool(var, value);
334-
return 0;
335-
}
336-
337-
if (!strcmp(var, "color.grep"))
338-
opt->color = git_config_colorbool(var, value);
339-
else if (!strcmp(var, "color.grep.context"))
340-
color = opt->color_context;
341-
else if (!strcmp(var, "color.grep.filename"))
342-
color = opt->color_filename;
343-
else if (!strcmp(var, "color.grep.function"))
344-
color = opt->color_function;
345-
else if (!strcmp(var, "color.grep.linenumber"))
346-
color = opt->color_lineno;
347-
else if (!strcmp(var, "color.grep.match"))
348-
color = opt->color_match;
349-
else if (!strcmp(var, "color.grep.selected"))
350-
color = opt->color_selected;
351-
else if (!strcmp(var, "color.grep.separator"))
352-
color = opt->color_sep;
353-
else
354-
return git_color_default_config(var, value, cb);
355-
if (color) {
356-
if (!value)
357-
return config_error_nonbool(var);
358-
color_parse(value, var, color);
359-
}
360-
return 0;
266+
int st = grep_config(var, value, cb);
267+
if (git_color_default_config(var, value, cb) < 0)
268+
st = -1;
269+
return st;
361270
}
362271

363272
static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
@@ -839,27 +748,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
839748
if (argc == 2 && !strcmp(argv[1], "-h"))
840749
usage_with_options(grep_usage, options);
841750

842-
memset(&opt, 0, sizeof(opt));
843-
opt.prefix = prefix;
844-
opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
845-
opt.relative = 1;
846-
opt.pathname = 1;
847-
opt.pattern_tail = &opt.pattern_list;
848-
opt.header_tail = &opt.header_list;
849-
opt.regflags = REG_NEWLINE;
850-
opt.max_depth = -1;
851-
opt.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
852-
opt.extended_regexp_option = 0;
853-
854-
strcpy(opt.color_context, "");
855-
strcpy(opt.color_filename, "");
856-
strcpy(opt.color_function, "");
857-
strcpy(opt.color_lineno, "");
858-
strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
859-
strcpy(opt.color_selected, "");
860-
strcpy(opt.color_sep, GIT_COLOR_CYAN);
861-
opt.color = -1;
862-
git_config(grep_config, &opt);
751+
init_grep_defaults();
752+
git_config(grep_cmd_config, NULL);
753+
grep_init(&opt, prefix);
863754

864755
/*
865756
* If there is no -- then the paths must exist in the working
@@ -875,13 +766,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
875766
PARSE_OPT_KEEP_DASHDASH |
876767
PARSE_OPT_STOP_AT_NON_OPTION |
877768
PARSE_OPT_NO_INTERNAL_HELP);
878-
879-
if (pattern_type_arg != GREP_PATTERN_TYPE_UNSPECIFIED)
880-
grep_pattern_type_options(pattern_type_arg, &opt);
881-
else if (opt.pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
882-
grep_pattern_type_options(opt.pattern_type_option, &opt);
883-
else if (opt.extended_regexp_option)
884-
grep_pattern_type_options(GREP_PATTERN_TYPE_ERE, &opt);
769+
grep_commit_pattern_type(pattern_type_arg, &opt);
885770

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

builtin/log.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ static int git_log_config(const char *var, const char *value, void *cb)
351351
}
352352
if (!prefixcmp(var, "color.decorate."))
353353
return parse_decorate_color_config(var, 15, value);
354-
354+
if (grep_config(var, value, cb) < 0)
355+
return -1;
355356
return git_diff_ui_config(var, value, cb);
356357
}
357358

@@ -360,6 +361,7 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
360361
struct rev_info rev;
361362
struct setup_revision_opt opt;
362363

364+
init_grep_defaults();
363365
git_config(git_log_config, NULL);
364366

365367
init_revisions(&rev, prefix);
@@ -450,6 +452,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
450452
struct pathspec match_all;
451453
int i, count, ret = 0;
452454

455+
init_grep_defaults();
453456
git_config(git_log_config, NULL);
454457

455458
init_pathspec(&match_all, NULL);
@@ -530,6 +533,7 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
530533
struct rev_info rev;
531534
struct setup_revision_opt opt;
532535

536+
init_grep_defaults();
533537
git_config(git_log_config, NULL);
534538

535539
init_revisions(&rev, prefix);
@@ -552,6 +556,7 @@ int cmd_log(int argc, const char **argv, const char *prefix)
552556
struct rev_info rev;
553557
struct setup_revision_opt opt;
554558

559+
init_grep_defaults();
555560
git_config(git_log_config, NULL);
556561

557562
init_revisions(&rev, prefix);
@@ -1121,6 +1126,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
11211126
extra_hdr.strdup_strings = 1;
11221127
extra_to.strdup_strings = 1;
11231128
extra_cc.strdup_strings = 1;
1129+
init_grep_defaults();
11241130
git_config(git_format_config, NULL);
11251131
init_revisions(&rev, prefix);
11261132
rev.commit_format = CMIT_FMT_EMAIL;

0 commit comments

Comments
 (0)