@@ -1201,6 +1201,15 @@ int git_config_system(void)
1201
1201
return !git_env_bool ("GIT_CONFIG_NOSYSTEM" , 0 );
1202
1202
}
1203
1203
1204
+ static inline void config_from_file_gently (config_fn_t fn , const char * filename ,
1205
+ void * data , unsigned access_flags , int * ret , int * found ) {
1206
+ if (!filename || access_or_die (filename , R_OK , access_flags ))
1207
+ return ;
1208
+
1209
+ * ret += git_config_from_file (fn , filename , data );
1210
+ (* found )++ ;
1211
+ }
1212
+
1204
1213
int git_config_early (config_fn_t fn , void * data , const char * repo_config )
1205
1214
{
1206
1215
int ret = 0 , found = 0 ;
@@ -1209,26 +1218,19 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
1209
1218
1210
1219
home_config_paths (& user_config , & xdg_config , "config" );
1211
1220
1212
- if (git_config_system () && !access_or_die (git_etc_gitconfig (), R_OK , 0 )) {
1213
- ret += git_config_from_file (fn , git_etc_gitconfig (),
1214
- data );
1215
- found += 1 ;
1221
+ if (git_config_system ()) {
1222
+ config_from_file_gently (fn , git_program_data_config (), data ,
1223
+ 0 , & ret , & found );
1224
+ config_from_file_gently (fn , git_etc_gitconfig (), data , 0 ,
1225
+ & ret , & found );
1216
1226
}
1217
1227
1218
- if (xdg_config && !access_or_die (xdg_config , R_OK , ACCESS_EACCES_OK )) {
1219
- ret += git_config_from_file (fn , xdg_config , data );
1220
- found += 1 ;
1221
- }
1222
-
1223
- if (user_config && !access_or_die (user_config , R_OK , ACCESS_EACCES_OK )) {
1224
- ret += git_config_from_file (fn , user_config , data );
1225
- found += 1 ;
1226
- }
1228
+ config_from_file_gently (fn , xdg_config , data , ACCESS_EACCES_OK ,
1229
+ & ret , & found );
1230
+ config_from_file_gently (fn , user_config , data , ACCESS_EACCES_OK ,
1231
+ & ret , & found );
1227
1232
1228
- if (repo_config && !access_or_die (repo_config , R_OK , 0 )) {
1229
- ret += git_config_from_file (fn , repo_config , data );
1230
- found += 1 ;
1231
- }
1233
+ config_from_file_gently (fn , repo_config , data , 0 , & ret , & found );
1232
1234
1233
1235
switch (git_config_from_parameters (fn , data )) {
1234
1236
case -1 : /* error */
@@ -1925,6 +1927,24 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
1925
1927
return - CONFIG_INVALID_KEY ;
1926
1928
}
1927
1929
1930
+ static int lock_config_file (const char * config_filename ,
1931
+ struct lock_file * * result )
1932
+ {
1933
+ int fd ;
1934
+
1935
+ /* make sure the parent directory exists */
1936
+ if (safe_create_leading_directories_const (config_filename ))
1937
+ return error ("could not create parent directory of %s" ,
1938
+ config_filename );
1939
+ * result = xcalloc (1 , sizeof (struct lock_file ));
1940
+ fd = hold_lock_file_for_update (* result , config_filename , 0 );
1941
+ if (fd < 0 )
1942
+ error ("could not lock config file %s: %s" , config_filename ,
1943
+ strerror (errno ));
1944
+
1945
+ return fd ;
1946
+ }
1947
+
1928
1948
/*
1929
1949
* If value==NULL, unset in (remove from) config,
1930
1950
* if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -1973,10 +1993,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
1973
1993
* The lock serves a purpose in addition to locking: the new
1974
1994
* contents of .git/config will be written into it.
1975
1995
*/
1976
- lock = xcalloc (1 , sizeof (struct lock_file ));
1977
- fd = hold_lock_file_for_update (lock , config_filename , 0 );
1996
+ fd = lock_config_file (config_filename , & lock );
1978
1997
if (fd < 0 ) {
1979
- error ("could not lock config file %s: %s" , config_filename , strerror (errno ));
1980
1998
free (store .key );
1981
1999
ret = CONFIG_NO_LOCK ;
1982
2000
goto out_free ;
@@ -2244,12 +2262,9 @@ int git_config_rename_section_in_file(const char *config_filename,
2244
2262
if (!config_filename )
2245
2263
config_filename = filename_buf = git_pathdup ("config" );
2246
2264
2247
- lock = xcalloc (1 , sizeof (struct lock_file ));
2248
- out_fd = hold_lock_file_for_update (lock , config_filename , 0 );
2249
- if (out_fd < 0 ) {
2250
- ret = error ("could not lock config file %s" , config_filename );
2265
+ out_fd = lock_config_file (config_filename , & lock );
2266
+ if (out_fd < 0 )
2251
2267
goto out ;
2252
- }
2253
2268
2254
2269
if (!(config_file = fopen (config_filename , "rb" ))) {
2255
2270
/* no config file means nothing to rename, no error */
0 commit comments