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