@@ -1203,32 +1203,34 @@ int git_config_system(void)
12031203 return !git_env_bool ("GIT_CONFIG_NOSYSTEM" , 0 );
12041204}
12051205
1206+ static inline void config_from_file_gently (config_fn_t fn , const char * filename ,
1207+ void * data , unsigned access_flags , int * ret , int * found ) {
1208+ if (!filename || access_or_die (filename , R_OK , access_flags ))
1209+ return ;
1210+
1211+ * ret += git_config_from_file (fn , filename , data );
1212+ (* found )++ ;
1213+ }
1214+
12061215int git_config_early (config_fn_t fn , void * data , const char * repo_config )
12071216{
12081217 int ret = 0 , found = 0 ;
12091218 char * xdg_config = xdg_config_home ("config" );
12101219 char * user_config = expand_user_path ("~/.gitconfig" );
12111220
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 );
12161226 }
12171227
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 );
12271232
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 );
12321234
12331235 switch (git_config_from_parameters (fn , data )) {
12341236 case -1 : /* error */
@@ -1953,6 +1955,20 @@ int git_config_key_is_valid(const char *key)
19531955 return !git_config_parse_key_1 (key , NULL , NULL , 1 );
19541956}
19551957
1958+ static int lock_config_file (const char * config_filename ,
1959+ struct lock_file * * result )
1960+ {
1961+ int fd ;
1962+
1963+ * result = xcalloc (1 , sizeof (struct lock_file ));
1964+ fd = hold_lock_file_for_update (* result , config_filename , 0 );
1965+ if (fd < 0 )
1966+ error ("could not lock config file %s: %s" , config_filename ,
1967+ strerror (errno ));
1968+
1969+ return fd ;
1970+ }
1971+
19561972/*
19571973 * If value==NULL, unset in (remove from) config,
19581974 * if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -2004,10 +2020,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
20042020 * The lock serves a purpose in addition to locking: the new
20052021 * contents of .git/config will be written into it.
20062022 */
2007- lock = xcalloc (1 , sizeof (struct lock_file ));
2008- fd = hold_lock_file_for_update (lock , config_filename , 0 );
2023+ fd = lock_config_file (config_filename , & lock );
20092024 if (fd < 0 ) {
2010- error ("could not lock config file %s: %s" , config_filename , strerror (errno ));
20112025 free (store .key );
20122026 ret = CONFIG_NO_LOCK ;
20132027 goto out_free ;
@@ -2306,12 +2320,9 @@ int git_config_rename_section_in_file(const char *config_filename,
23062320 if (!config_filename )
23072321 config_filename = filename_buf = git_pathdup ("config" );
23082322
2309- lock = xcalloc (1 , sizeof (struct lock_file ));
2310- out_fd = hold_lock_file_for_update (lock , config_filename , 0 );
2311- if (out_fd < 0 ) {
2312- ret = error ("could not lock config file %s" , config_filename );
2323+ out_fd = lock_config_file (config_filename , & lock );
2324+ if (out_fd < 0 )
23132325 goto out ;
2314- }
23152326
23162327 if (!(config_file = fopen (config_filename , "rb" ))) {
23172328 /* no config file means nothing to rename, no error */
0 commit comments