@@ -15,7 +15,6 @@ static int show_keys;
15
15
static int use_key_regexp ;
16
16
static int do_all ;
17
17
static int do_not_match ;
18
- static int seen ;
19
18
static char delim = '=' ;
20
19
static char key_delim = ' ' ;
21
20
static char term = '\n' ;
@@ -95,12 +94,19 @@ static int show_all_config(const char *key_, const char *value_, void *cb)
95
94
return 0 ;
96
95
}
97
96
98
- static int show_config (const char * key_ , const char * value_ , void * cb )
97
+ struct strbuf_list {
98
+ struct strbuf * items ;
99
+ int nr ;
100
+ int alloc ;
101
+ };
102
+
103
+ static int collect_config (const char * key_ , const char * value_ , void * cb )
99
104
{
105
+ struct strbuf_list * values = cb ;
106
+ struct strbuf * buf ;
100
107
char value [256 ];
101
108
const char * vptr = value ;
102
109
int must_free_vptr = 0 ;
103
- int dup_error = 0 ;
104
110
int must_print_delim = 0 ;
105
111
106
112
if (!use_key_regexp && strcmp (key_ , key ))
@@ -111,12 +117,14 @@ static int show_config(const char *key_, const char *value_, void *cb)
111
117
(do_not_match ^ !!regexec (regexp , (value_ ?value_ :"" ), 0 , NULL , 0 )))
112
118
return 0 ;
113
119
120
+ ALLOC_GROW (values -> items , values -> nr + 1 , values -> alloc );
121
+ buf = & values -> items [values -> nr ++ ];
122
+ strbuf_init (buf , 0 );
123
+
114
124
if (show_keys ) {
115
- printf ( "%s" , key_ );
125
+ strbuf_addstr ( buf , key_ );
116
126
must_print_delim = 1 ;
117
127
}
118
- if (seen && !do_all )
119
- dup_error = 1 ;
120
128
if (types == TYPE_INT )
121
129
sprintf (value , "%d" , git_config_int (key_ , value_ ?value_ :"" ));
122
130
else if (types == TYPE_BOOL )
@@ -139,16 +147,12 @@ static int show_config(const char *key_, const char *value_, void *cb)
139
147
vptr = "" ;
140
148
must_print_delim = 0 ;
141
149
}
142
- seen ++ ;
143
- if (dup_error ) {
144
- error ("More than one value for the key %s: %s" ,
145
- key_ , vptr );
146
- }
147
- else {
148
- if (must_print_delim )
149
- printf ("%c" , key_delim );
150
- printf ("%s%c" , vptr , term );
151
- }
150
+
151
+ if (must_print_delim )
152
+ strbuf_addch (buf , key_delim );
153
+ strbuf_addstr (buf , vptr );
154
+ strbuf_addch (buf , term );
155
+
152
156
if (must_free_vptr )
153
157
/* If vptr must be freed, it's a pointer to a
154
158
* dynamically allocated buffer, it's safe to cast to
@@ -162,19 +166,8 @@ static int show_config(const char *key_, const char *value_, void *cb)
162
166
static int get_value (const char * key_ , const char * regex_ )
163
167
{
164
168
int ret = CONFIG_GENERIC_ERROR ;
165
- char * global = NULL , * xdg = NULL , * repo_config = NULL ;
166
- const char * system_wide = NULL , * local ;
167
- struct config_include_data inc = CONFIG_INCLUDE_INIT ;
168
- config_fn_t fn ;
169
- void * data ;
170
-
171
- local = given_config_file ;
172
- if (!local ) {
173
- local = repo_config = git_pathdup ("config" );
174
- if (git_config_system ())
175
- system_wide = git_etc_gitconfig ();
176
- home_config_paths (& global , & xdg , "config" );
177
- }
169
+ struct strbuf_list values = {NULL };
170
+ int i ;
178
171
179
172
if (use_key_regexp ) {
180
173
char * tl ;
@@ -196,7 +189,8 @@ static int get_value(const char *key_, const char *regex_)
196
189
key_regexp = (regex_t * )xmalloc (sizeof (regex_t ));
197
190
if (regcomp (key_regexp , key , REG_EXTENDED )) {
198
191
fprintf (stderr , "Invalid key pattern: %s\n" , key_ );
199
- free (key );
192
+ free (key_regexp );
193
+ key_regexp = NULL ;
200
194
ret = CONFIG_INVALID_PATTERN ;
201
195
goto free_strings ;
202
196
}
@@ -216,53 +210,37 @@ static int get_value(const char *key_, const char *regex_)
216
210
regexp = (regex_t * )xmalloc (sizeof (regex_t ));
217
211
if (regcomp (regexp , regex_ , REG_EXTENDED )) {
218
212
fprintf (stderr , "Invalid pattern: %s\n" , regex_ );
213
+ free (regexp );
214
+ regexp = NULL ;
219
215
ret = CONFIG_INVALID_PATTERN ;
220
216
goto free_strings ;
221
217
}
222
218
}
223
219
224
- fn = show_config ;
225
- data = NULL ;
226
- if (respect_includes ) {
227
- inc .fn = fn ;
228
- inc .data = data ;
229
- fn = git_config_include ;
230
- data = & inc ;
231
- }
232
-
233
- if (do_all && system_wide )
234
- git_config_from_file (fn , system_wide , data );
235
- if (do_all && xdg )
236
- git_config_from_file (fn , xdg , data );
237
- if (do_all && global )
238
- git_config_from_file (fn , global , data );
239
- if (do_all )
240
- git_config_from_file (fn , local , data );
241
- git_config_from_parameters (fn , data );
242
- if (!do_all && !seen )
243
- git_config_from_file (fn , local , data );
244
- if (!do_all && !seen && global )
245
- git_config_from_file (fn , global , data );
246
- if (!do_all && !seen && xdg )
247
- git_config_from_file (fn , xdg , data );
248
- if (!do_all && !seen && system_wide )
249
- git_config_from_file (fn , system_wide , data );
220
+ git_config_with_options (collect_config , & values ,
221
+ given_config_file , respect_includes );
222
+
223
+ ret = !values .nr ;
250
224
225
+ for (i = 0 ; i < values .nr ; i ++ ) {
226
+ struct strbuf * buf = values .items + i ;
227
+ if (do_all || i == values .nr - 1 )
228
+ fwrite (buf -> buf , 1 , buf -> len , stdout );
229
+ strbuf_release (buf );
230
+ }
231
+ free (values .items );
232
+
233
+ free_strings :
251
234
free (key );
235
+ if (key_regexp ) {
236
+ regfree (key_regexp );
237
+ free (key_regexp );
238
+ }
252
239
if (regexp ) {
253
240
regfree (regexp );
254
241
free (regexp );
255
242
}
256
243
257
- if (do_all )
258
- ret = !seen ;
259
- else
260
- ret = (seen == 1 ) ? 0 : seen > 1 ? 2 : 1 ;
261
-
262
- free_strings :
263
- free (repo_config );
264
- free (global );
265
- free (xdg );
266
244
return ret ;
267
245
}
268
246
0 commit comments