@@ -14,7 +14,31 @@ static int grep_source_load(struct grep_source *gs);
1414static int grep_source_is_binary (struct grep_source * gs ,
1515 struct index_state * istate );
1616
17- static struct grep_opt grep_defaults ;
17+ static void std_output (struct grep_opt * opt , const void * buf , size_t size )
18+ {
19+ fwrite (buf , size , 1 , stdout );
20+ }
21+
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 ] = "" ,
30+ [GREP_COLOR_FUNCTION ] = "" ,
31+ [GREP_COLOR_LINENO ] = "" ,
32+ [GREP_COLOR_COLUMNNO ] = "" ,
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+ };
1842
1943#ifdef USE_LIBPCRE2
2044static pcre2_general_context * pcre2_global_context ;
@@ -42,50 +66,6 @@ static const char *color_grep_slots[] = {
4266 [GREP_COLOR_SEP ] = "separator" ,
4367};
4468
45- static void std_output (struct grep_opt * opt , const void * buf , size_t size )
46- {
47- fwrite (buf , size , 1 , stdout );
48- }
49-
50- static void color_set (char * dst , const char * color_bytes )
51- {
52- xsnprintf (dst , COLOR_MAXLEN , "%s" , color_bytes );
53- }
54-
55- /*
56- * Initialize the grep_defaults template with hardcoded defaults.
57- * We could let the compiler do this, but without C99 initializers
58- * the code gets unwieldy and unreadable, so...
59- */
60- void init_grep_defaults (struct repository * repo )
61- {
62- struct grep_opt * opt = & grep_defaults ;
63- static int run_once ;
64-
65- if (run_once )
66- return ;
67- run_once ++ ;
68-
69- memset (opt , 0 , sizeof (* opt ));
70- opt -> repo = repo ;
71- opt -> relative = 1 ;
72- opt -> pathname = 1 ;
73- opt -> max_depth = -1 ;
74- opt -> pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED ;
75- color_set (opt -> colors [GREP_COLOR_CONTEXT ], "" );
76- color_set (opt -> colors [GREP_COLOR_FILENAME ], "" );
77- color_set (opt -> colors [GREP_COLOR_FUNCTION ], "" );
78- color_set (opt -> colors [GREP_COLOR_LINENO ], "" );
79- color_set (opt -> colors [GREP_COLOR_COLUMNNO ], "" );
80- color_set (opt -> colors [GREP_COLOR_MATCH_CONTEXT ], GIT_COLOR_BOLD_RED );
81- color_set (opt -> colors [GREP_COLOR_MATCH_SELECTED ], GIT_COLOR_BOLD_RED );
82- color_set (opt -> colors [GREP_COLOR_SELECTED ], "" );
83- color_set (opt -> colors [GREP_COLOR_SEP ], GIT_COLOR_CYAN );
84- opt -> only_matching = 0 ;
85- opt -> color = -1 ;
86- opt -> output = std_output ;
87- }
88-
8969static int parse_pattern_type_arg (const char * opt , const char * arg )
9070{
9171 if (!strcmp (arg , "default" ))
@@ -115,6 +95,14 @@ int grep_config(const char *var, const char *value, void *cb)
11595 if (userdiff_config (var , value ) < 0 )
11696 return -1 ;
11797
98+ /*
99+ * The instance of grep_opt that we set up here is copied by
100+ * grep_init() to be used by each individual invocation.
101+ * When populating a new field of this structure here, be
102+ * sure to think about ownership -- e.g., you might need to
103+ * override the shallow copy in grep_init() with a deep copy.
104+ */
105+
118106 if (!strcmp (var , "grep.extendedregexp" )) {
119107 opt -> extended_regexp_option = git_config_bool (var , value );
120108 return 0 ;
@@ -172,9 +160,6 @@ int grep_config(const char *var, const char *value, void *cb)
172160 */
173161void grep_init (struct grep_opt * opt , struct repository * repo , const char * prefix )
174162{
175- struct grep_opt * def = & grep_defaults ;
176- int i ;
177-
178163#if defined(USE_LIBPCRE2 )
179164 if (!pcre2_global_context )
180165 pcre2_global_context = pcre2_general_context_create (
@@ -186,26 +171,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
186171 pcre_free = free ;
187172#endif
188173
189- memset (opt , 0 , sizeof (* opt ));
174+ * opt = grep_defaults ;
175+
190176 opt -> repo = repo ;
191177 opt -> prefix = prefix ;
192178 opt -> prefix_length = (prefix && * prefix ) ? strlen (prefix ) : 0 ;
193179 opt -> pattern_tail = & opt -> pattern_list ;
194180 opt -> header_tail = & opt -> header_list ;
195-
196- opt -> only_matching = def -> only_matching ;
197- opt -> color = def -> color ;
198- opt -> extended_regexp_option = def -> extended_regexp_option ;
199- opt -> pattern_type_option = def -> pattern_type_option ;
200- opt -> linenum = def -> linenum ;
201- opt -> columnnum = def -> columnnum ;
202- opt -> max_depth = def -> max_depth ;
203- opt -> pathname = def -> pathname ;
204- opt -> relative = def -> relative ;
205- opt -> output = def -> output ;
206-
207- for (i = 0 ; i < NR_GREP_COLORS ; i ++ )
208- color_set (opt -> colors [i ], def -> colors [i ]);
209181}
210182
211183void grep_destroy (void )
0 commit comments