@@ -35,10 +35,8 @@ void init_grep_defaults(void)
35
35
memset (opt , 0 , sizeof (* opt ));
36
36
opt -> relative = 1 ;
37
37
opt -> pathname = 1 ;
38
- opt -> regflags = REG_NEWLINE ;
39
38
opt -> max_depth = -1 ;
40
39
opt -> pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED ;
41
- opt -> extended_regexp_option = 0 ;
42
40
color_set (opt -> color_context , "" );
43
41
color_set (opt -> color_filename , "" );
44
42
color_set (opt -> color_function , "" );
@@ -79,10 +77,7 @@ int grep_config(const char *var, const char *value, void *cb)
79
77
return -1 ;
80
78
81
79
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 );
86
81
return 0 ;
87
82
}
88
83
@@ -157,7 +152,6 @@ void grep_init(struct grep_opt *opt, const char *prefix)
157
152
opt -> linenum = def -> linenum ;
158
153
opt -> max_depth = def -> max_depth ;
159
154
opt -> pathname = def -> pathname ;
160
- opt -> regflags = def -> regflags ;
161
155
opt -> relative = def -> relative ;
162
156
opt -> output = def -> output ;
163
157
@@ -173,33 +167,41 @@ void grep_init(struct grep_opt *opt, const char *prefix)
173
167
174
168
static void grep_set_pattern_type_option (enum grep_pattern_type pattern_type , struct grep_opt * opt )
175
169
{
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
+
176
188
switch (pattern_type ) {
177
189
case GREP_PATTERN_TYPE_UNSPECIFIED :
178
190
/* fall through */
179
191
180
192
case GREP_PATTERN_TYPE_BRE :
181
- opt -> fixed = 0 ;
182
- opt -> pcre1 = 0 ;
183
- opt -> pcre2 = 0 ;
184
193
break ;
185
194
186
195
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 ;
191
197
break ;
192
198
193
199
case GREP_PATTERN_TYPE_FIXED :
194
200
opt -> fixed = 1 ;
195
- opt -> pcre1 = 0 ;
196
- opt -> pcre2 = 0 ;
197
201
break ;
198
202
199
203
case GREP_PATTERN_TYPE_PCRE :
200
- opt -> fixed = 0 ;
201
204
#ifdef USE_LIBPCRE2
202
- opt -> pcre1 = 0 ;
203
205
opt -> pcre2 = 1 ;
204
206
#else
205
207
/*
@@ -209,7 +211,6 @@ static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, st
209
211
* "cannot use Perl-compatible regexes[...]".
210
212
*/
211
213
opt -> pcre1 = 1 ;
212
- opt -> pcre2 = 0 ;
213
214
#endif
214
215
break ;
215
216
}
@@ -222,6 +223,11 @@ void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_o
222
223
else if (opt -> pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED )
223
224
grep_set_pattern_type_option (opt -> pattern_type_option , opt );
224
225
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
+ */
225
231
grep_set_pattern_type_option (GREP_PATTERN_TYPE_ERE , opt );
226
232
}
227
233
@@ -587,7 +593,7 @@ static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
587
593
{
588
594
struct strbuf sb = STRBUF_INIT ;
589
595
int err ;
590
- int regflags = opt -> regflags ;
596
+ int regflags = 0 ;
591
597
592
598
basic_regex_quote_buf (& sb , p -> pattern );
593
599
if (opt -> ignore_case )
@@ -606,12 +612,12 @@ static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
606
612
607
613
static void compile_regexp (struct grep_pat * p , struct grep_opt * opt )
608
614
{
609
- int icase , ascii_only ;
615
+ int ascii_only ;
610
616
int err ;
617
+ int regflags = REG_NEWLINE ;
611
618
612
619
p -> word_regexp = opt -> word_regexp ;
613
620
p -> ignore_case = opt -> ignore_case ;
614
- icase = opt -> regflags & REG_ICASE || p -> ignore_case ;
615
621
ascii_only = !has_non_ascii (p -> pattern );
616
622
617
623
/*
@@ -629,12 +635,10 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
629
635
if (opt -> fixed ||
630
636
has_null (p -> pattern , p -> patternlen ) ||
631
637
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 ;
635
639
636
640
if (p -> fixed ) {
637
- p -> kws = kwsalloc (icase ? tolower_trans_tbl : NULL );
641
+ p -> kws = kwsalloc (p -> ignore_case ? tolower_trans_tbl : NULL );
638
642
kwsincr (p -> kws , p -> pattern , p -> patternlen );
639
643
kwsprep (p -> kws );
640
644
return ;
@@ -658,7 +662,11 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
658
662
return ;
659
663
}
660
664
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 );
662
670
if (err ) {
663
671
char errbuf [1024 ];
664
672
regerror (err , & p -> regexp , errbuf , 1024 );
0 commit comments