@@ -1553,11 +1553,27 @@ int git_config_system(void)
15531553 return !git_env_bool ("GIT_CONFIG_NOSYSTEM" , 0 );
15541554}
15551555
1556+ static void attempt_git_config_from_file_with_options (config_fn_t fn ,
1557+ const char * filename ,
1558+ void * data ,
1559+ enum config_scope scope ,
1560+ const struct config_options * opts ,
1561+ int * success_count ,
1562+ int * cumulative_ret )
1563+ {
1564+ int ret = git_config_from_file_with_options (fn , filename , data ,
1565+ scope , opts );
1566+ if (!ret )
1567+ (* success_count )++ ;
1568+ * cumulative_ret += ret ;
1569+ }
1570+
15561571static int do_git_config_sequence (const struct config_options * opts ,
1557- const struct repository * repo ,
1558- config_fn_t fn , void * data )
1572+ const struct repository * repo , config_fn_t fn ,
1573+ void * data , int require_successful_config )
15591574{
15601575 int ret = 0 ;
1576+ int success_count = 0 ;
15611577 char * system_config = git_system_config ();
15621578 char * xdg_config = NULL ;
15631579 char * user_config = NULL ;
@@ -1580,44 +1596,54 @@ static int do_git_config_sequence(const struct config_options *opts,
15801596 worktree_config = NULL ;
15811597 }
15821598
1583- if (git_config_system () && system_config &&
1599+ if (! opts -> ignore_system && git_config_system () && system_config &&
15841600 !access_or_die (system_config , R_OK ,
15851601 opts -> system_gently ? ACCESS_EACCES_OK : 0 ))
1586- ret += git_config_from_file_with_options (fn , system_config ,
1587- data , CONFIG_SCOPE_SYSTEM ,
1588- NULL );
1602+ attempt_git_config_from_file_with_options (fn , system_config , data ,
1603+ CONFIG_SCOPE_SYSTEM , NULL ,
1604+ & success_count , & ret );
15891605
1590- git_global_config_paths (& user_config , & xdg_config );
1606+ if (!opts -> ignore_global ) {
1607+ git_global_config_paths (& user_config , & xdg_config );
15911608
1592- if (xdg_config && !access_or_die (xdg_config , R_OK , ACCESS_EACCES_OK ))
1593- ret += git_config_from_file_with_options (fn , xdg_config , data ,
1594- CONFIG_SCOPE_GLOBAL , NULL );
1609+ if (xdg_config && !access_or_die (xdg_config , R_OK , ACCESS_EACCES_OK ))
1610+ attempt_git_config_from_file_with_options (fn , xdg_config ,
1611+ data ,
1612+ CONFIG_SCOPE_GLOBAL ,
1613+ NULL , & success_count , & ret );
15951614
1596- if (user_config && !access_or_die (user_config , R_OK , ACCESS_EACCES_OK ))
1597- ret += git_config_from_file_with_options (fn , user_config , data ,
1598- CONFIG_SCOPE_GLOBAL , NULL );
1615+ if (user_config && !access_or_die (user_config , R_OK , ACCESS_EACCES_OK ))
1616+ attempt_git_config_from_file_with_options (fn , user_config ,
1617+ data ,
1618+ CONFIG_SCOPE_GLOBAL ,
1619+ NULL , & success_count , & ret );
1620+
1621+ free (xdg_config );
1622+ free (user_config );
1623+ }
15991624
16001625 if (!opts -> ignore_repo && repo_config &&
16011626 !access_or_die (repo_config , R_OK , 0 ))
1602- ret += git_config_from_file_with_options (fn , repo_config , data ,
1603- CONFIG_SCOPE_LOCAL , NULL );
1627+ attempt_git_config_from_file_with_options (fn , repo_config , data ,
1628+ CONFIG_SCOPE_LOCAL , NULL , & success_count , & ret );
16041629
16051630 if (!opts -> ignore_worktree && worktree_config &&
16061631 repo && repo -> repository_format_worktree_config &&
1607- !access_or_die (worktree_config , R_OK , 0 )) {
1608- ret += git_config_from_file_with_options (fn , worktree_config , data ,
1609- CONFIG_SCOPE_WORKTREE ,
1610- NULL );
1611- }
1632+ !access_or_die (worktree_config , R_OK , 0 ))
1633+ attempt_git_config_from_file_with_options (fn , worktree_config , data ,
1634+ CONFIG_SCOPE_WORKTREE ,
1635+ NULL , & success_count , & ret );
16121636
16131637 if (!opts -> ignore_cmdline && git_config_from_parameters (fn , data ) < 0 )
16141638 die (_ ("unable to parse command-line config" ));
16151639
16161640 free (system_config );
1617- free (xdg_config );
1618- free (user_config );
16191641 free (repo_config );
16201642 free (worktree_config );
1643+
1644+ if (require_successful_config && !success_count && !ret )
1645+ ret = -1 ;
1646+
16211647 return ret ;
16221648}
16231649
@@ -1645,15 +1671,17 @@ int config_with_options(config_fn_t fn, void *data,
16451671 */
16461672 if (config_source && config_source -> use_stdin ) {
16471673 ret = git_config_from_stdin (fn , data , config_source -> scope );
1648- } else if (config_source && config_source -> file ) {
1674+ } else if (config_source && config_source -> file &&
1675+ config_source -> scope != CONFIG_SCOPE_GLOBAL ) {
16491676 ret = git_config_from_file_with_options (fn , config_source -> file ,
16501677 data , config_source -> scope ,
16511678 NULL );
16521679 } else if (config_source && config_source -> blob ) {
16531680 ret = git_config_from_blob_ref (fn , repo , config_source -> blob ,
16541681 data , config_source -> scope );
16551682 } else {
1656- ret = do_git_config_sequence (opts , repo , fn , data );
1683+ ret = do_git_config_sequence (opts , repo , fn , data ,
1684+ config_source && config_source -> scope == CONFIG_SCOPE_GLOBAL );
16571685 }
16581686
16591687 if (inc .remote_urls ) {
0 commit comments