@@ -2288,7 +2288,7 @@ void git_die_config(const char *key, const char *err, ...)
2288
2288
* Find all the stuff for git_config_set() below.
2289
2289
*/
2290
2290
2291
- static struct {
2291
+ struct config_store_data {
2292
2292
int baselen ;
2293
2293
char * key ;
2294
2294
int do_not_match ;
@@ -2298,83 +2298,86 @@ static struct {
2298
2298
unsigned int offset_alloc ;
2299
2299
enum { START , SECTION_SEEN , SECTION_END_SEEN , KEY_SEEN } state ;
2300
2300
unsigned int seen ;
2301
- } store ;
2301
+ };
2302
2302
2303
- static int matches (const char * key , const char * value )
2303
+ static int matches (const char * key , const char * value ,
2304
+ const struct config_store_data * store )
2304
2305
{
2305
- if (strcmp (key , store . key ))
2306
+ if (strcmp (key , store -> key ))
2306
2307
return 0 ; /* not ours */
2307
- if (!store . value_regex )
2308
+ if (!store -> value_regex )
2308
2309
return 1 ; /* always matches */
2309
- if (store . value_regex == CONFIG_REGEX_NONE )
2310
+ if (store -> value_regex == CONFIG_REGEX_NONE )
2310
2311
return 0 ; /* never matches */
2311
2312
2312
- return store . do_not_match ^
2313
- (value && !regexec (store . value_regex , value , 0 , NULL , 0 ));
2313
+ return store -> do_not_match ^
2314
+ (value && !regexec (store -> value_regex , value , 0 , NULL , 0 ));
2314
2315
}
2315
2316
2316
2317
static int store_aux (const char * key , const char * value , void * cb )
2317
2318
{
2318
2319
const char * ep ;
2319
2320
size_t section_len ;
2321
+ struct config_store_data * store = cb ;
2320
2322
2321
- switch (store . state ) {
2323
+ switch (store -> state ) {
2322
2324
case KEY_SEEN :
2323
- if (matches (key , value )) {
2324
- if (store . seen == 1 && store . multi_replace == 0 ) {
2325
+ if (matches (key , value , store )) {
2326
+ if (store -> seen == 1 && store -> multi_replace == 0 ) {
2325
2327
warning (_ ("%s has multiple values" ), key );
2326
2328
}
2327
2329
2328
- ALLOC_GROW (store . offset , store . seen + 1 ,
2329
- store . offset_alloc );
2330
+ ALLOC_GROW (store -> offset , store -> seen + 1 ,
2331
+ store -> offset_alloc );
2330
2332
2331
- store . offset [store . seen ] = cf -> do_ftell (cf );
2332
- store . seen ++ ;
2333
+ store -> offset [store -> seen ] = cf -> do_ftell (cf );
2334
+ store -> seen ++ ;
2333
2335
}
2334
2336
break ;
2335
2337
case SECTION_SEEN :
2336
2338
/*
2337
- * What we are looking for is in store. key (both
2339
+ * What we are looking for is in store-> key (both
2338
2340
* section and var), and its section part is baselen
2339
2341
* long. We found key (again, both section and var).
2340
2342
* We would want to know if this key is in the same
2341
2343
* section as what we are looking for. We already
2342
2344
* know we are in the same section as what should
2343
- * hold store. key.
2345
+ * hold store-> key.
2344
2346
*/
2345
2347
ep = strrchr (key , '.' );
2346
2348
section_len = ep - key ;
2347
2349
2348
- if ((section_len != store . baselen ) ||
2349
- memcmp (key , store . key , section_len + 1 )) {
2350
- store . state = SECTION_END_SEEN ;
2350
+ if ((section_len != store -> baselen ) ||
2351
+ memcmp (key , store -> key , section_len + 1 )) {
2352
+ store -> state = SECTION_END_SEEN ;
2351
2353
break ;
2352
2354
}
2353
2355
2354
2356
/*
2355
2357
* Do not increment matches: this is no match, but we
2356
2358
* just made sure we are in the desired section.
2357
2359
*/
2358
- ALLOC_GROW (store . offset , store . seen + 1 ,
2359
- store . offset_alloc );
2360
- store . offset [store . seen ] = cf -> do_ftell (cf );
2360
+ ALLOC_GROW (store -> offset , store -> seen + 1 ,
2361
+ store -> offset_alloc );
2362
+ store -> offset [store -> seen ] = cf -> do_ftell (cf );
2361
2363
/* fallthru */
2362
2364
case SECTION_END_SEEN :
2363
2365
case START :
2364
- if (matches (key , value )) {
2365
- ALLOC_GROW (store . offset , store . seen + 1 ,
2366
- store . offset_alloc );
2367
- store . offset [store . seen ] = cf -> do_ftell (cf );
2368
- store . state = KEY_SEEN ;
2369
- store . seen ++ ;
2366
+ if (matches (key , value , store )) {
2367
+ ALLOC_GROW (store -> offset , store -> seen + 1 ,
2368
+ store -> offset_alloc );
2369
+ store -> offset [store -> seen ] = cf -> do_ftell (cf );
2370
+ store -> state = KEY_SEEN ;
2371
+ store -> seen ++ ;
2370
2372
} else {
2371
- if (strrchr (key , '.' ) - key == store .baselen &&
2372
- !strncmp (key , store .key , store .baselen )) {
2373
- store .state = SECTION_SEEN ;
2374
- ALLOC_GROW (store .offset ,
2375
- store .seen + 1 ,
2376
- store .offset_alloc );
2377
- store .offset [store .seen ] = cf -> do_ftell (cf );
2373
+ if (strrchr (key , '.' ) - key == store -> baselen &&
2374
+ !strncmp (key , store -> key , store -> baselen )) {
2375
+ store -> state = SECTION_SEEN ;
2376
+ ALLOC_GROW (store -> offset ,
2377
+ store -> seen + 1 ,
2378
+ store -> offset_alloc );
2379
+ store -> offset [store -> seen ] =
2380
+ cf -> do_ftell (cf );
2378
2381
}
2379
2382
}
2380
2383
}
@@ -2389,31 +2392,33 @@ static int write_error(const char *filename)
2389
2392
return 4 ;
2390
2393
}
2391
2394
2392
- static struct strbuf store_create_section (const char * key )
2395
+ static struct strbuf store_create_section (const char * key ,
2396
+ const struct config_store_data * store )
2393
2397
{
2394
2398
const char * dot ;
2395
2399
int i ;
2396
2400
struct strbuf sb = STRBUF_INIT ;
2397
2401
2398
- dot = memchr (key , '.' , store . baselen );
2402
+ dot = memchr (key , '.' , store -> baselen );
2399
2403
if (dot ) {
2400
2404
strbuf_addf (& sb , "[%.*s \"" , (int )(dot - key ), key );
2401
- for (i = dot - key + 1 ; i < store . baselen ; i ++ ) {
2405
+ for (i = dot - key + 1 ; i < store -> baselen ; i ++ ) {
2402
2406
if (key [i ] == '"' || key [i ] == '\\' )
2403
2407
strbuf_addch (& sb , '\\' );
2404
2408
strbuf_addch (& sb , key [i ]);
2405
2409
}
2406
2410
strbuf_addstr (& sb , "\"]\n" );
2407
2411
} else {
2408
- strbuf_addf (& sb , "[%.*s]\n" , store . baselen , key );
2412
+ strbuf_addf (& sb , "[%.*s]\n" , store -> baselen , key );
2409
2413
}
2410
2414
2411
2415
return sb ;
2412
2416
}
2413
2417
2414
- static ssize_t write_section (int fd , const char * key )
2418
+ static ssize_t write_section (int fd , const char * key ,
2419
+ const struct config_store_data * store )
2415
2420
{
2416
- struct strbuf sb = store_create_section (key );
2421
+ struct strbuf sb = store_create_section (key , store );
2417
2422
ssize_t ret ;
2418
2423
2419
2424
ret = write_in_full (fd , sb .buf , sb .len );
@@ -2422,11 +2427,12 @@ static ssize_t write_section(int fd, const char *key)
2422
2427
return ret ;
2423
2428
}
2424
2429
2425
- static ssize_t write_pair (int fd , const char * key , const char * value )
2430
+ static ssize_t write_pair (int fd , const char * key , const char * value ,
2431
+ const struct config_store_data * store )
2426
2432
{
2427
2433
int i ;
2428
2434
ssize_t ret ;
2429
- int length = strlen (key + store . baselen + 1 );
2435
+ int length = strlen (key + store -> baselen + 1 );
2430
2436
const char * quote = "" ;
2431
2437
struct strbuf sb = STRBUF_INIT ;
2432
2438
@@ -2446,7 +2452,7 @@ static ssize_t write_pair(int fd, const char *key, const char *value)
2446
2452
quote = "\"" ;
2447
2453
2448
2454
strbuf_addf (& sb , "\t%.*s = %s" ,
2449
- length , key + store . baselen + 1 , quote );
2455
+ length , key + store -> baselen + 1 , quote );
2450
2456
2451
2457
for (i = 0 ; value [i ]; i ++ )
2452
2458
switch (value [i ]) {
@@ -2556,6 +2562,9 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2556
2562
char * filename_buf = NULL ;
2557
2563
char * contents = NULL ;
2558
2564
size_t contents_sz ;
2565
+ struct config_store_data store ;
2566
+
2567
+ memset (& store , 0 , sizeof (store ));
2559
2568
2560
2569
/* parse-key returns negative; flip the sign to feed exit(3) */
2561
2570
ret = 0 - git_config_parse_key (key , & store .key , & store .baselen );
@@ -2598,8 +2607,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2598
2607
}
2599
2608
2600
2609
store .key = (char * )key ;
2601
- if (write_section (fd , key ) < 0 ||
2602
- write_pair (fd , key , value ) < 0 )
2610
+ if (write_section (fd , key , & store ) < 0 ||
2611
+ write_pair (fd , key , value , & store ) < 0 )
2603
2612
goto write_err_out ;
2604
2613
} else {
2605
2614
struct stat st ;
@@ -2638,7 +2647,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2638
2647
* As a side effect, we make sure to transform only a valid
2639
2648
* existing config file.
2640
2649
*/
2641
- if (git_config_from_file (store_aux , config_filename , NULL )) {
2650
+ if (git_config_from_file (store_aux , config_filename , & store )) {
2642
2651
error ("invalid config file %s" , config_filename );
2643
2652
free (store .key );
2644
2653
if (store .value_regex != NULL &&
@@ -2722,10 +2731,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2722
2731
/* write the pair (value == NULL means unset) */
2723
2732
if (value != NULL ) {
2724
2733
if (store .state == START ) {
2725
- if (write_section (fd , key ) < 0 )
2734
+ if (write_section (fd , key , & store ) < 0 )
2726
2735
goto write_err_out ;
2727
2736
}
2728
- if (write_pair (fd , key , value ) < 0 )
2737
+ if (write_pair (fd , key , value , & store ) < 0 )
2729
2738
goto write_err_out ;
2730
2739
}
2731
2740
@@ -2849,7 +2858,8 @@ static int section_name_is_ok(const char *name)
2849
2858
2850
2859
/* if new_name == NULL, the section is removed instead */
2851
2860
static int git_config_copy_or_rename_section_in_file (const char * config_filename ,
2852
- const char * old_name , const char * new_name , int copy )
2861
+ const char * old_name ,
2862
+ const char * new_name , int copy )
2853
2863
{
2854
2864
int ret = 0 , remove = 0 ;
2855
2865
char * filename_buf = NULL ;
@@ -2859,6 +2869,9 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
2859
2869
FILE * config_file = NULL ;
2860
2870
struct stat st ;
2861
2871
struct strbuf copystr = STRBUF_INIT ;
2872
+ struct config_store_data store ;
2873
+
2874
+ memset (& store , 0 , sizeof (store ));
2862
2875
2863
2876
if (new_name && !section_name_is_ok (new_name )) {
2864
2877
ret = error ("invalid section name: %s" , new_name );
@@ -2928,7 +2941,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
2928
2941
}
2929
2942
store .baselen = strlen (new_name );
2930
2943
if (!copy ) {
2931
- if (write_section (out_fd , new_name ) < 0 ) {
2944
+ if (write_section (out_fd , new_name , & store ) < 0 ) {
2932
2945
ret = write_error (get_lock_file_path (& lock ));
2933
2946
goto out ;
2934
2947
}
@@ -2949,7 +2962,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
2949
2962
output [0 ] = '\t' ;
2950
2963
}
2951
2964
} else {
2952
- copystr = store_create_section (new_name );
2965
+ copystr = store_create_section (new_name , & store );
2953
2966
}
2954
2967
}
2955
2968
remove = 0 ;
0 commit comments