@@ -14,7 +14,31 @@ static int grep_source_load(struct grep_source *gs);
14
14
static int grep_source_is_binary (struct grep_source * gs ,
15
15
struct index_state * istate );
16
16
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
+ };
18
42
19
43
#ifdef USE_LIBPCRE2
20
44
static pcre2_general_context * pcre2_global_context ;
@@ -42,50 +66,6 @@ static const char *color_grep_slots[] = {
42
66
[GREP_COLOR_SEP ] = "separator" ,
43
67
};
44
68
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
-
89
69
static int parse_pattern_type_arg (const char * opt , const char * arg )
90
70
{
91
71
if (!strcmp (arg , "default" ))
@@ -115,6 +95,14 @@ int grep_config(const char *var, const char *value, void *cb)
115
95
if (userdiff_config (var , value ) < 0 )
116
96
return -1 ;
117
97
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
+
118
106
if (!strcmp (var , "grep.extendedregexp" )) {
119
107
opt -> extended_regexp_option = git_config_bool (var , value );
120
108
return 0 ;
@@ -172,9 +160,6 @@ int grep_config(const char *var, const char *value, void *cb)
172
160
*/
173
161
void grep_init (struct grep_opt * opt , struct repository * repo , const char * prefix )
174
162
{
175
- struct grep_opt * def = & grep_defaults ;
176
- int i ;
177
-
178
163
#if defined(USE_LIBPCRE2 )
179
164
if (!pcre2_global_context )
180
165
pcre2_global_context = pcre2_general_context_create (
@@ -186,26 +171,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
186
171
pcre_free = free ;
187
172
#endif
188
173
189
- memset (opt , 0 , sizeof (* opt ));
174
+ * opt = grep_defaults ;
175
+
190
176
opt -> repo = repo ;
191
177
opt -> prefix = prefix ;
192
178
opt -> prefix_length = (prefix && * prefix ) ? strlen (prefix ) : 0 ;
193
179
opt -> pattern_tail = & opt -> pattern_list ;
194
180
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 ]);
209
181
}
210
182
211
183
void grep_destroy (void )
0 commit comments