@@ -19,27 +19,6 @@ static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1919 fwrite (buf , size , 1 , stdout );
2020}
2121
22- static struct grep_opt grep_defaults = {
23- .relative = 1 ,
24- .pathname = 1 ,
25- .max_depth = -1 ,
26- .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED ,
27- .colors = {
28- [GREP_COLOR_CONTEXT ] = "" ,
29- [GREP_COLOR_FILENAME ] = GIT_COLOR_MAGENTA ,
30- [GREP_COLOR_FUNCTION ] = "" ,
31- [GREP_COLOR_LINENO ] = GIT_COLOR_GREEN ,
32- [GREP_COLOR_COLUMNNO ] = GIT_COLOR_GREEN ,
33- [GREP_COLOR_MATCH_CONTEXT ] = GIT_COLOR_BOLD_RED ,
34- [GREP_COLOR_MATCH_SELECTED ] = GIT_COLOR_BOLD_RED ,
35- [GREP_COLOR_SELECTED ] = "" ,
36- [GREP_COLOR_SEP ] = GIT_COLOR_CYAN ,
37- },
38- .only_matching = 0 ,
39- .color = -1 ,
40- .output = std_output ,
41- };
42-
4322static const char * color_grep_slots [] = {
4423 [GREP_COLOR_CONTEXT ] = "context" ,
4524 [GREP_COLOR_FILENAME ] = "filename" ,
@@ -75,20 +54,12 @@ define_list_config_array_extra(color_grep_slots, {"match"});
7554 */
7655int grep_config (const char * var , const char * value , void * cb )
7756{
78- struct grep_opt * opt = & grep_defaults ;
57+ struct grep_opt * opt = cb ;
7958 const char * slot ;
8059
8160 if (userdiff_config (var , value ) < 0 )
8261 return -1 ;
8362
84- /*
85- * The instance of grep_opt that we set up here is copied by
86- * grep_init() to be used by each individual invocation.
87- * When populating a new field of this structure here, be
88- * sure to think about ownership -- e.g., you might need to
89- * override the shallow copy in grep_init() with a deep copy.
90- */
91-
9263 if (!strcmp (var , "grep.extendedregexp" )) {
9364 opt -> extended_regexp_option = git_config_bool (var , value );
9465 return 0 ;
@@ -134,78 +105,16 @@ int grep_config(const char *var, const char *value, void *cb)
134105 return 0 ;
135106}
136107
137- /*
138- * Initialize one instance of grep_opt and copy the
139- * default values from the template we read the configuration
140- * information in an earlier call to git_config(grep_config).
141- */
142- void grep_init (struct grep_opt * opt , struct repository * repo , const char * prefix )
108+ void grep_init (struct grep_opt * opt , struct repository * repo )
143109{
144- * opt = grep_defaults ;
110+ struct grep_opt blank = GREP_OPT_INIT ;
111+ memcpy (opt , & blank , sizeof (* opt ));
145112
146113 opt -> repo = repo ;
147- opt -> prefix = prefix ;
148- opt -> prefix_length = (prefix && * prefix ) ? strlen (prefix ) : 0 ;
149114 opt -> pattern_tail = & opt -> pattern_list ;
150115 opt -> header_tail = & opt -> header_list ;
151116}
152117
153- static void grep_set_pattern_type_option (enum grep_pattern_type pattern_type , struct grep_opt * opt )
154- {
155- /*
156- * When committing to the pattern type by setting the relevant
157- * fields in grep_opt it's generally not necessary to zero out
158- * the fields we're not choosing, since they won't have been
159- * set by anything. The extended_regexp_option field is the
160- * only exception to this.
161- *
162- * This is because in the process of parsing grep.patternType
163- * & grep.extendedRegexp we set opt->pattern_type_option and
164- * opt->extended_regexp_option, respectively. We then
165- * internally use opt->extended_regexp_option to see if we're
166- * compiling an ERE. It must be unset if that's not actually
167- * the case.
168- */
169- if (pattern_type != GREP_PATTERN_TYPE_ERE &&
170- opt -> extended_regexp_option )
171- opt -> extended_regexp_option = 0 ;
172-
173- switch (pattern_type ) {
174- case GREP_PATTERN_TYPE_UNSPECIFIED :
175- /* fall through */
176-
177- case GREP_PATTERN_TYPE_BRE :
178- break ;
179-
180- case GREP_PATTERN_TYPE_ERE :
181- opt -> extended_regexp_option = 1 ;
182- break ;
183-
184- case GREP_PATTERN_TYPE_FIXED :
185- opt -> fixed = 1 ;
186- break ;
187-
188- case GREP_PATTERN_TYPE_PCRE :
189- opt -> pcre2 = 1 ;
190- break ;
191- }
192- }
193-
194- void grep_commit_pattern_type (enum grep_pattern_type pattern_type , struct grep_opt * opt )
195- {
196- if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED )
197- grep_set_pattern_type_option (pattern_type , opt );
198- else if (opt -> pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED )
199- grep_set_pattern_type_option (opt -> pattern_type_option , opt );
200- else if (opt -> extended_regexp_option )
201- /*
202- * This branch *must* happen after setting from the
203- * opt->pattern_type_option above, we don't want
204- * grep.extendedRegexp to override grep.patternType!
205- */
206- grep_set_pattern_type_option (GREP_PATTERN_TYPE_ERE , opt );
207- }
208-
209118static struct grep_pat * create_grep_pat (const char * pat , size_t patlen ,
210119 const char * origin , int no ,
211120 enum grep_pat_token t ,
@@ -523,11 +432,17 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
523432 int err ;
524433 int regflags = REG_NEWLINE ;
525434
435+ if (opt -> pattern_type_option == GREP_PATTERN_TYPE_UNSPECIFIED )
436+ opt -> pattern_type_option = (opt -> extended_regexp_option
437+ ? GREP_PATTERN_TYPE_ERE
438+ : GREP_PATTERN_TYPE_BRE );
439+
526440 p -> word_regexp = opt -> word_regexp ;
527441 p -> ignore_case = opt -> ignore_case ;
528- p -> fixed = opt -> fixed ;
442+ p -> fixed = opt -> pattern_type_option == GREP_PATTERN_TYPE_FIXED ;
529443
530- if (memchr (p -> pattern , 0 , p -> patternlen ) && !opt -> pcre2 )
444+ if (opt -> pattern_type_option != GREP_PATTERN_TYPE_PCRE &&
445+ memchr (p -> pattern , 0 , p -> patternlen ))
531446 die (_ ("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2" ));
532447
533448 p -> is_fixed = is_fixed (p -> pattern , p -> patternlen );
@@ -578,14 +493,14 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
578493 return ;
579494 }
580495
581- if (opt -> pcre2 ) {
496+ if (opt -> pattern_type_option == GREP_PATTERN_TYPE_PCRE ) {
582497 compile_pcre2_pattern (p , opt );
583498 return ;
584499 }
585500
586501 if (p -> ignore_case )
587502 regflags |= REG_ICASE ;
588- if (opt -> extended_regexp_option )
503+ if (opt -> pattern_type_option == GREP_PATTERN_TYPE_ERE )
589504 regflags |= REG_EXTENDED ;
590505 err = regcomp (& p -> regexp , p -> pattern , regflags );
591506 if (err ) {
0 commit comments