Skip to content

Commit eac97b4

Browse files
committed
Merge branch 'ab/grep-lose-opt-regflags'
Code cleanup. * ab/grep-lose-opt-regflags: grep: remove redundant REG_NEWLINE when compiling fixed regex grep: remove regflags from the public grep_opt API grep: remove redundant and verbose re-assignments to 0 grep: remove redundant "fixed" field re-assignment to 0 grep: adjust a redundant grep pattern type assignment grep: remove redundant double assignment to 0
2 parents 80145b1 + 1ceabab commit eac97b4

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

builtin/grep.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11701170

11711171
if (!opt.pattern_list)
11721172
die(_("no pattern given."));
1173-
if (!opt.fixed && opt.ignore_case)
1174-
opt.regflags |= REG_ICASE;
11751173

11761174
/*
11771175
* We have to find "--" in a separate pass, because its presence

grep.c

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ void init_grep_defaults(void)
3535
memset(opt, 0, sizeof(*opt));
3636
opt->relative = 1;
3737
opt->pathname = 1;
38-
opt->regflags = REG_NEWLINE;
3938
opt->max_depth = -1;
4039
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
41-
opt->extended_regexp_option = 0;
4240
color_set(opt->color_context, "");
4341
color_set(opt->color_filename, "");
4442
color_set(opt->color_function, "");
@@ -79,10 +77,7 @@ int grep_config(const char *var, const char *value, void *cb)
7977
return -1;
8078

8179
if (!strcmp(var, "grep.extendedregexp")) {
82-
if (git_config_bool(var, value))
83-
opt->extended_regexp_option = 1;
84-
else
85-
opt->extended_regexp_option = 0;
80+
opt->extended_regexp_option = git_config_bool(var, value);
8681
return 0;
8782
}
8883

@@ -157,7 +152,6 @@ void grep_init(struct grep_opt *opt, const char *prefix)
157152
opt->linenum = def->linenum;
158153
opt->max_depth = def->max_depth;
159154
opt->pathname = def->pathname;
160-
opt->regflags = def->regflags;
161155
opt->relative = def->relative;
162156
opt->output = def->output;
163157

@@ -173,33 +167,41 @@ void grep_init(struct grep_opt *opt, const char *prefix)
173167

174168
static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
175169
{
170+
/*
171+
* When committing to the pattern type by setting the relevant
172+
* fields in grep_opt it's generally not necessary to zero out
173+
* the fields we're not choosing, since they won't have been
174+
* set by anything. The extended_regexp_option field is the
175+
* only exception to this.
176+
*
177+
* This is because in the process of parsing grep.patternType
178+
* & grep.extendedRegexp we set opt->pattern_type_option and
179+
* opt->extended_regexp_option, respectively. We then
180+
* internally use opt->extended_regexp_option to see if we're
181+
* compiling an ERE. It must be unset if that's not actually
182+
* the case.
183+
*/
184+
if (pattern_type != GREP_PATTERN_TYPE_ERE &&
185+
opt->extended_regexp_option)
186+
opt->extended_regexp_option = 0;
187+
176188
switch (pattern_type) {
177189
case GREP_PATTERN_TYPE_UNSPECIFIED:
178190
/* fall through */
179191

180192
case GREP_PATTERN_TYPE_BRE:
181-
opt->fixed = 0;
182-
opt->pcre1 = 0;
183-
opt->pcre2 = 0;
184193
break;
185194

186195
case GREP_PATTERN_TYPE_ERE:
187-
opt->fixed = 0;
188-
opt->pcre1 = 0;
189-
opt->pcre2 = 0;
190-
opt->regflags |= REG_EXTENDED;
196+
opt->extended_regexp_option = 1;
191197
break;
192198

193199
case GREP_PATTERN_TYPE_FIXED:
194200
opt->fixed = 1;
195-
opt->pcre1 = 0;
196-
opt->pcre2 = 0;
197201
break;
198202

199203
case GREP_PATTERN_TYPE_PCRE:
200-
opt->fixed = 0;
201204
#ifdef USE_LIBPCRE2
202-
opt->pcre1 = 0;
203205
opt->pcre2 = 1;
204206
#else
205207
/*
@@ -209,7 +211,6 @@ static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, st
209211
* "cannot use Perl-compatible regexes[...]".
210212
*/
211213
opt->pcre1 = 1;
212-
opt->pcre2 = 0;
213214
#endif
214215
break;
215216
}
@@ -222,6 +223,11 @@ void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_o
222223
else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
223224
grep_set_pattern_type_option(opt->pattern_type_option, opt);
224225
else if (opt->extended_regexp_option)
226+
/*
227+
* This branch *must* happen after setting from the
228+
* opt->pattern_type_option above, we don't want
229+
* grep.extendedRegexp to override grep.patternType!
230+
*/
225231
grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
226232
}
227233

@@ -587,7 +593,7 @@ static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
587593
{
588594
struct strbuf sb = STRBUF_INIT;
589595
int err;
590-
int regflags = opt->regflags;
596+
int regflags = 0;
591597

592598
basic_regex_quote_buf(&sb, p->pattern);
593599
if (opt->ignore_case)
@@ -606,12 +612,12 @@ static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
606612

607613
static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
608614
{
609-
int icase, ascii_only;
615+
int ascii_only;
610616
int err;
617+
int regflags = REG_NEWLINE;
611618

612619
p->word_regexp = opt->word_regexp;
613620
p->ignore_case = opt->ignore_case;
614-
icase = opt->regflags & REG_ICASE || p->ignore_case;
615621
ascii_only = !has_non_ascii(p->pattern);
616622

617623
/*
@@ -629,12 +635,10 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
629635
if (opt->fixed ||
630636
has_null(p->pattern, p->patternlen) ||
631637
is_fixed(p->pattern, p->patternlen))
632-
p->fixed = !icase || ascii_only;
633-
else
634-
p->fixed = 0;
638+
p->fixed = !p->ignore_case || ascii_only;
635639

636640
if (p->fixed) {
637-
p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
641+
p->kws = kwsalloc(p->ignore_case ? tolower_trans_tbl : NULL);
638642
kwsincr(p->kws, p->pattern, p->patternlen);
639643
kwsprep(p->kws);
640644
return;
@@ -658,7 +662,11 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
658662
return;
659663
}
660664

661-
err = regcomp(&p->regexp, p->pattern, opt->regflags);
665+
if (p->ignore_case)
666+
regflags |= REG_ICASE;
667+
if (opt->extended_regexp_option)
668+
regflags |= REG_EXTENDED;
669+
err = regcomp(&p->regexp, p->pattern, regflags);
662670
if (err) {
663671
char errbuf[1024];
664672
regerror(err, &p->regexp, errbuf, 1024);

grep.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ struct grep_opt {
162162
char color_match_selected[COLOR_MAXLEN];
163163
char color_selected[COLOR_MAXLEN];
164164
char color_sep[COLOR_MAXLEN];
165-
int regflags;
166165
unsigned pre_context;
167166
unsigned post_context;
168167
unsigned last_shown;

revision.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,6 @@ void init_revisions(struct rev_info *revs, const char *prefix)
13621362
init_grep_defaults();
13631363
grep_init(&revs->grep_filter, prefix);
13641364
revs->grep_filter.status_only = 1;
1365-
revs->grep_filter.regflags = REG_NEWLINE;
13661365

13671366
diff_setup(&revs->diffopt);
13681367
if (prefix && !revs->diffopt.prefix) {
@@ -2022,7 +2021,6 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
20222021
revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
20232022
} else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
20242023
revs->grep_filter.ignore_case = 1;
2025-
revs->grep_filter.regflags |= REG_ICASE;
20262024
DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
20272025
} else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
20282026
revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;

0 commit comments

Comments
 (0)