@@ -207,13 +207,22 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
207207 return prefix ;
208208}
209209
210- static int include_by_gitdir (const char * cond , size_t cond_len , int icase )
210+ static int include_by_gitdir (const struct config_options * opts ,
211+ const char * cond , size_t cond_len , int icase )
211212{
212213 struct strbuf text = STRBUF_INIT ;
213214 struct strbuf pattern = STRBUF_INIT ;
214215 int ret = 0 , prefix ;
216+ const char * git_dir ;
215217
216- strbuf_realpath (& text , get_git_dir (), 1 );
218+ if (opts -> git_dir )
219+ git_dir = opts -> git_dir ;
220+ else if (have_git_dir ())
221+ git_dir = get_git_dir ();
222+ else
223+ goto done ;
224+
225+ strbuf_realpath (& text , git_dir , 1 );
217226 strbuf_add (& pattern , cond , cond_len );
218227 prefix = prepare_include_condition_pattern (& pattern );
219228
@@ -242,13 +251,14 @@ static int include_by_gitdir(const char *cond, size_t cond_len, int icase)
242251 return ret ;
243252}
244253
245- static int include_condition_is_true (const char * cond , size_t cond_len )
254+ static int include_condition_is_true (const struct config_options * opts ,
255+ const char * cond , size_t cond_len )
246256{
247257
248258 if (skip_prefix_mem (cond , cond_len , "gitdir:" , & cond , & cond_len ))
249- return include_by_gitdir (cond , cond_len , 0 );
259+ return include_by_gitdir (opts , cond , cond_len , 0 );
250260 else if (skip_prefix_mem (cond , cond_len , "gitdir/i:" , & cond , & cond_len ))
251- return include_by_gitdir (cond , cond_len , 1 );
261+ return include_by_gitdir (opts , cond , cond_len , 1 );
252262
253263 /* unknown conditionals are always false */
254264 return 0 ;
@@ -273,7 +283,7 @@ int git_config_include(const char *var, const char *value, void *data)
273283 ret = handle_path_include (value , inc );
274284
275285 if (!parse_config_key (var , "includeif" , & cond , & cond_len , & key ) &&
276- (cond && include_condition_is_true (cond , cond_len )) &&
286+ (cond && include_condition_is_true (inc -> opts , cond , cond_len )) &&
277287 !strcmp (key , "path" ))
278288 ret = handle_path_include (value , inc );
279289
@@ -1511,12 +1521,20 @@ int git_config_system(void)
15111521 return !git_env_bool ("GIT_CONFIG_NOSYSTEM" , 0 );
15121522}
15131523
1514- static int do_git_config_sequence (config_fn_t fn , void * data )
1524+ static int do_git_config_sequence (const struct config_options * opts ,
1525+ config_fn_t fn , void * data )
15151526{
15161527 int ret = 0 ;
15171528 char * xdg_config = xdg_config_home ("config" );
15181529 char * user_config = expand_user_path ("~/.gitconfig" , 0 );
1519- char * repo_config = have_git_dir () ? git_pathdup ("config" ) : NULL ;
1530+ char * repo_config ;
1531+
1532+ if (opts -> git_dir )
1533+ repo_config = mkpathdup ("%s/config" , opts -> git_dir );
1534+ else if (have_git_dir ())
1535+ repo_config = git_pathdup ("config" );
1536+ else
1537+ repo_config = NULL ;
15201538
15211539 current_parsing_scope = CONFIG_SCOPE_SYSTEM ;
15221540 if (git_config_system () && !access_or_die (git_etc_gitconfig (), R_OK , 0 ))
@@ -1547,13 +1565,14 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
15471565
15481566int git_config_with_options (config_fn_t fn , void * data ,
15491567 struct git_config_source * config_source ,
1550- int respect_includes )
1568+ const struct config_options * opts )
15511569{
15521570 struct config_include_data inc = CONFIG_INCLUDE_INIT ;
15531571
1554- if (respect_includes ) {
1572+ if (opts -> respect_includes ) {
15551573 inc .fn = fn ;
15561574 inc .data = data ;
1575+ inc .opts = opts ;
15571576 fn = git_config_include ;
15581577 data = & inc ;
15591578 }
@@ -1569,12 +1588,15 @@ int git_config_with_options(config_fn_t fn, void *data,
15691588 else if (config_source && config_source -> blob )
15701589 return git_config_from_blob_ref (fn , config_source -> blob , data );
15711590
1572- return do_git_config_sequence (fn , data );
1591+ return do_git_config_sequence (opts , fn , data );
15731592}
15741593
15751594static void git_config_raw (config_fn_t fn , void * data )
15761595{
1577- if (git_config_with_options (fn , data , NULL , 1 ) < 0 )
1596+ struct config_options opts = {0 };
1597+
1598+ opts .respect_includes = 1 ;
1599+ if (git_config_with_options (fn , data , NULL , & opts ) < 0 )
15781600 /*
15791601 * git_config_with_options() normally returns only
15801602 * zero, as most errors are fatal, and
@@ -1614,10 +1636,13 @@ static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
16141636
16151637void read_early_config (config_fn_t cb , void * data )
16161638{
1639+ struct config_options opts = {0 };
16171640 struct strbuf buf = STRBUF_INIT ;
16181641
1619- git_config_with_options ( cb , data , NULL , 1 ) ;
1642+ opts . respect_includes = 1 ;
16201643
1644+ if (have_git_dir ())
1645+ opts .git_dir = get_git_dir ();
16211646 /*
16221647 * When setup_git_directory() was not yet asked to discover the
16231648 * GIT_DIR, we ask discover_git_directory() to figure out whether there
@@ -1626,14 +1651,11 @@ void read_early_config(config_fn_t cb, void *data)
16261651 * notably, the current working directory is still the same after the
16271652 * call).
16281653 */
1629- if (!have_git_dir () && discover_git_directory (& buf )) {
1630- struct git_config_source repo_config ;
1654+ else if (discover_git_directory (& buf ))
1655+ opts .git_dir = buf .buf ;
1656+
1657+ git_config_with_options (cb , data , NULL , & opts );
16311658
1632- memset (& repo_config , 0 , sizeof (repo_config ));
1633- strbuf_addstr (& buf , "/config" );
1634- repo_config .file = buf .buf ;
1635- git_config_with_options (cb , data , & repo_config , 1 );
1636- }
16371659 strbuf_release (& buf );
16381660}
16391661
0 commit comments