@@ -259,7 +259,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
259
259
!cs ? "<unknown>" :
260
260
cs -> name ? cs -> name :
261
261
"the command line" );
262
- ret = git_config_from_file (git_config_include , path , inc );
262
+ ret = git_config_from_file_with_options (git_config_include , path , inc ,
263
+ current_config_scope (),
264
+ NULL );
263
265
inc -> depth -- ;
264
266
}
265
267
cleanup :
@@ -503,7 +505,7 @@ static int git_config_include(const char *var, const char *value,
503
505
* Pass along all values, including "include" directives; this makes it
504
506
* possible to query information on the includes themselves.
505
507
*/
506
- ret = inc -> fn (var , value , NULL , inc -> data );
508
+ ret = inc -> fn (var , value , ctx , inc -> data );
507
509
if (ret < 0 )
508
510
return ret ;
509
511
@@ -939,12 +941,15 @@ static char *parse_value(struct config_source *cs)
939
941
}
940
942
}
941
943
942
- static int get_value (struct config_source * cs , config_fn_t fn , void * data ,
943
- struct strbuf * name )
944
+ static int get_value (struct config_source * cs , struct key_value_info * kvi ,
945
+ config_fn_t fn , void * data , struct strbuf * name )
944
946
{
945
947
int c ;
946
948
char * value ;
947
949
int ret ;
950
+ struct config_context ctx = {
951
+ .kvi = kvi ,
952
+ };
948
953
949
954
/* Get the full name */
950
955
for (;;) {
@@ -973,7 +978,8 @@ static int get_value(struct config_source *cs, config_fn_t fn, void *data,
973
978
* accurate line number in error messages.
974
979
*/
975
980
cs -> linenr -- ;
976
- ret = fn (name -> buf , value , NULL , data );
981
+ kvi -> linenr = cs -> linenr ;
982
+ ret = fn (name -> buf , value , & ctx , data );
977
983
if (ret >= 0 )
978
984
cs -> linenr ++ ;
979
985
return ret ;
@@ -1072,8 +1078,19 @@ static int do_event(struct config_source *cs, enum config_event_t type,
1072
1078
return 0 ;
1073
1079
}
1074
1080
1081
+ static void kvi_from_source (struct config_source * cs ,
1082
+ enum config_scope scope ,
1083
+ struct key_value_info * out )
1084
+ {
1085
+ out -> filename = strintern (cs -> name );
1086
+ out -> origin_type = cs -> origin_type ;
1087
+ out -> linenr = cs -> linenr ;
1088
+ out -> scope = scope ;
1089
+ }
1090
+
1075
1091
static int git_parse_source (struct config_source * cs , config_fn_t fn ,
1076
- void * data , const struct config_options * opts )
1092
+ struct key_value_info * kvi , void * data ,
1093
+ const struct config_options * opts )
1077
1094
{
1078
1095
int comment = 0 ;
1079
1096
size_t baselen = 0 ;
@@ -1157,7 +1174,7 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn,
1157
1174
*/
1158
1175
strbuf_setlen (var , baselen );
1159
1176
strbuf_addch (var , tolower (c ));
1160
- if (get_value (cs , fn , data , var ) < 0 )
1177
+ if (get_value (cs , kvi , fn , data , var ) < 0 )
1161
1178
break ;
1162
1179
}
1163
1180
@@ -2010,9 +2027,11 @@ int git_default_config(const char *var, const char *value,
2010
2027
* this function.
2011
2028
*/
2012
2029
static int do_config_from (struct config_reader * reader ,
2013
- struct config_source * top , config_fn_t fn , void * data ,
2030
+ struct config_source * top , config_fn_t fn ,
2031
+ void * data , enum config_scope scope ,
2014
2032
const struct config_options * opts )
2015
2033
{
2034
+ struct key_value_info kvi = KVI_INIT ;
2016
2035
int ret ;
2017
2036
2018
2037
/* push config-file parsing state stack */
@@ -2022,8 +2041,9 @@ static int do_config_from(struct config_reader *reader,
2022
2041
strbuf_init (& top -> value , 1024 );
2023
2042
strbuf_init (& top -> var , 1024 );
2024
2043
config_reader_push_source (reader , top );
2044
+ kvi_from_source (top , scope , & kvi );
2025
2045
2026
- ret = git_parse_source (top , fn , data , opts );
2046
+ ret = git_parse_source (top , fn , & kvi , data , opts );
2027
2047
2028
2048
/* pop config-file parsing state stack */
2029
2049
strbuf_release (& top -> value );
@@ -2037,7 +2057,8 @@ static int do_config_from_file(struct config_reader *reader,
2037
2057
config_fn_t fn ,
2038
2058
const enum config_origin_type origin_type ,
2039
2059
const char * name , const char * path , FILE * f ,
2040
- void * data , const struct config_options * opts )
2060
+ void * data , enum config_scope scope ,
2061
+ const struct config_options * opts )
2041
2062
{
2042
2063
struct config_source top = CONFIG_SOURCE_INIT ;
2043
2064
int ret ;
@@ -2052,19 +2073,20 @@ static int do_config_from_file(struct config_reader *reader,
2052
2073
top .do_ftell = config_file_ftell ;
2053
2074
2054
2075
flockfile (f );
2055
- ret = do_config_from (reader , & top , fn , data , opts );
2076
+ ret = do_config_from (reader , & top , fn , data , scope , opts );
2056
2077
funlockfile (f );
2057
2078
return ret ;
2058
2079
}
2059
2080
2060
- static int git_config_from_stdin (config_fn_t fn , void * data )
2081
+ static int git_config_from_stdin (config_fn_t fn , void * data ,
2082
+ enum config_scope scope )
2061
2083
{
2062
2084
return do_config_from_file (& the_reader , fn , CONFIG_ORIGIN_STDIN , "" ,
2063
- NULL , stdin , data , NULL );
2085
+ NULL , stdin , data , scope , NULL );
2064
2086
}
2065
2087
2066
2088
int git_config_from_file_with_options (config_fn_t fn , const char * filename ,
2067
- void * data ,
2089
+ void * data , enum config_scope scope ,
2068
2090
const struct config_options * opts )
2069
2091
{
2070
2092
int ret = -1 ;
@@ -2075,21 +2097,24 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
2075
2097
f = fopen_or_warn (filename , "r" );
2076
2098
if (f ) {
2077
2099
ret = do_config_from_file (& the_reader , fn , CONFIG_ORIGIN_FILE ,
2078
- filename , filename , f , data , opts );
2100
+ filename , filename , f , data , scope ,
2101
+ opts );
2079
2102
fclose (f );
2080
2103
}
2081
2104
return ret ;
2082
2105
}
2083
2106
2084
2107
int git_config_from_file (config_fn_t fn , const char * filename , void * data )
2085
2108
{
2086
- return git_config_from_file_with_options (fn , filename , data , NULL );
2109
+ return git_config_from_file_with_options (fn , filename , data ,
2110
+ CONFIG_SCOPE_UNKNOWN , NULL );
2087
2111
}
2088
2112
2089
2113
int git_config_from_mem (config_fn_t fn ,
2090
2114
const enum config_origin_type origin_type ,
2091
2115
const char * name , const char * buf , size_t len ,
2092
- void * data , const struct config_options * opts )
2116
+ void * data , enum config_scope scope ,
2117
+ const struct config_options * opts )
2093
2118
{
2094
2119
struct config_source top = CONFIG_SOURCE_INIT ;
2095
2120
@@ -2104,14 +2129,15 @@ int git_config_from_mem(config_fn_t fn,
2104
2129
top .do_ungetc = config_buf_ungetc ;
2105
2130
top .do_ftell = config_buf_ftell ;
2106
2131
2107
- return do_config_from (& the_reader , & top , fn , data , opts );
2132
+ return do_config_from (& the_reader , & top , fn , data , scope , opts );
2108
2133
}
2109
2134
2110
2135
int git_config_from_blob_oid (config_fn_t fn ,
2111
2136
const char * name ,
2112
2137
struct repository * repo ,
2113
2138
const struct object_id * oid ,
2114
- void * data )
2139
+ void * data ,
2140
+ enum config_scope scope )
2115
2141
{
2116
2142
enum object_type type ;
2117
2143
char * buf ;
@@ -2127,7 +2153,7 @@ int git_config_from_blob_oid(config_fn_t fn,
2127
2153
}
2128
2154
2129
2155
ret = git_config_from_mem (fn , CONFIG_ORIGIN_BLOB , name , buf , size ,
2130
- data , NULL );
2156
+ data , scope , NULL );
2131
2157
free (buf );
2132
2158
2133
2159
return ret ;
@@ -2136,13 +2162,14 @@ int git_config_from_blob_oid(config_fn_t fn,
2136
2162
static int git_config_from_blob_ref (config_fn_t fn ,
2137
2163
struct repository * repo ,
2138
2164
const char * name ,
2139
- void * data )
2165
+ void * data ,
2166
+ enum config_scope scope )
2140
2167
{
2141
2168
struct object_id oid ;
2142
2169
2143
2170
if (repo_get_oid (repo , name , & oid ) < 0 )
2144
2171
return error (_ ("unable to resolve config blob '%s'" ), name );
2145
- return git_config_from_blob_oid (fn , name , repo , & oid , data );
2172
+ return git_config_from_blob_oid (fn , name , repo , & oid , data , scope );
2146
2173
}
2147
2174
2148
2175
char * git_system_config (void )
@@ -2228,27 +2255,34 @@ static int do_git_config_sequence(struct config_reader *reader,
2228
2255
if (git_config_system () && system_config &&
2229
2256
!access_or_die (system_config , R_OK ,
2230
2257
opts -> system_gently ? ACCESS_EACCES_OK : 0 ))
2231
- ret += git_config_from_file (fn , system_config , data );
2258
+ ret += git_config_from_file_with_options (fn , system_config ,
2259
+ data , CONFIG_SCOPE_SYSTEM ,
2260
+ NULL );
2232
2261
2233
2262
config_reader_set_scope (reader , CONFIG_SCOPE_GLOBAL );
2234
2263
git_global_config (& user_config , & xdg_config );
2235
2264
2236
2265
if (xdg_config && !access_or_die (xdg_config , R_OK , ACCESS_EACCES_OK ))
2237
- ret += git_config_from_file (fn , xdg_config , data );
2266
+ ret += git_config_from_file_with_options (fn , xdg_config , data ,
2267
+ CONFIG_SCOPE_GLOBAL , NULL );
2238
2268
2239
2269
if (user_config && !access_or_die (user_config , R_OK , ACCESS_EACCES_OK ))
2240
- ret += git_config_from_file (fn , user_config , data );
2270
+ ret += git_config_from_file_with_options (fn , user_config , data ,
2271
+ CONFIG_SCOPE_GLOBAL , NULL );
2241
2272
2242
2273
config_reader_set_scope (reader , CONFIG_SCOPE_LOCAL );
2243
2274
if (!opts -> ignore_repo && repo_config &&
2244
2275
!access_or_die (repo_config , R_OK , 0 ))
2245
- ret += git_config_from_file (fn , repo_config , data );
2276
+ ret += git_config_from_file_with_options (fn , repo_config , data ,
2277
+ CONFIG_SCOPE_LOCAL , NULL );
2246
2278
2247
2279
config_reader_set_scope (reader , CONFIG_SCOPE_WORKTREE );
2248
2280
if (!opts -> ignore_worktree && worktree_config &&
2249
2281
repo && repo -> repository_format_worktree_config &&
2250
2282
!access_or_die (worktree_config , R_OK , 0 )) {
2251
- ret += git_config_from_file (fn , worktree_config , data );
2283
+ ret += git_config_from_file_with_options (fn , worktree_config , data ,
2284
+ CONFIG_SCOPE_WORKTREE ,
2285
+ NULL );
2252
2286
}
2253
2287
2254
2288
config_reader_set_scope (reader , CONFIG_SCOPE_COMMAND );
@@ -2292,12 +2326,14 @@ int config_with_options(config_fn_t fn, void *data,
2292
2326
* regular lookup sequence.
2293
2327
*/
2294
2328
if (config_source && config_source -> use_stdin ) {
2295
- ret = git_config_from_stdin (fn , data );
2329
+ ret = git_config_from_stdin (fn , data , config_source -> scope );
2296
2330
} else if (config_source && config_source -> file ) {
2297
- ret = git_config_from_file (fn , config_source -> file , data );
2331
+ ret = git_config_from_file_with_options (fn , config_source -> file ,
2332
+ data , config_source -> scope ,
2333
+ NULL );
2298
2334
} else if (config_source && config_source -> blob ) {
2299
2335
ret = git_config_from_blob_ref (fn , repo , config_source -> blob ,
2300
- data );
2336
+ data , config_source -> scope );
2301
2337
} else {
2302
2338
ret = do_git_config_sequence (& the_reader , opts , repo , fn , data );
2303
2339
}
@@ -2440,16 +2476,14 @@ static int configset_add_value(struct config_reader *reader,
2440
2476
if (!reader -> source )
2441
2477
BUG ("configset_add_value has no source" );
2442
2478
if (reader -> source -> name ) {
2443
- kv_info -> filename = strintern (reader -> source -> name );
2444
- kv_info -> linenr = reader -> source -> linenr ;
2445
- kv_info -> origin_type = reader -> source -> origin_type ;
2479
+ kvi_from_source (reader -> source , current_config_scope (), kv_info );
2446
2480
} else {
2447
2481
/* for values read from `git_config_from_parameters()` */
2448
2482
kv_info -> filename = NULL ;
2449
2483
kv_info -> linenr = -1 ;
2450
2484
kv_info -> origin_type = CONFIG_ORIGIN_CMDLINE ;
2485
+ kv_info -> scope = reader -> parsing_scope ;
2451
2486
}
2452
- kv_info -> scope = reader -> parsing_scope ;
2453
2487
si -> util = kv_info ;
2454
2488
2455
2489
return 0 ;
@@ -3490,7 +3524,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
3490
3524
*/
3491
3525
if (git_config_from_file_with_options (store_aux ,
3492
3526
config_filename ,
3493
- & store , & opts )) {
3527
+ & store , CONFIG_SCOPE_UNKNOWN ,
3528
+ & opts )) {
3494
3529
error (_ ("invalid config file %s" ), config_filename );
3495
3530
ret = CONFIG_INVALID_FILE ;
3496
3531
goto out_free ;
0 commit comments