@@ -207,13 +207,22 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
207
207
return prefix ;
208
208
}
209
209
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 )
211
212
{
212
213
struct strbuf text = STRBUF_INIT ;
213
214
struct strbuf pattern = STRBUF_INIT ;
214
215
int ret = 0 , prefix ;
216
+ const char * git_dir ;
215
217
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 );
217
226
strbuf_add (& pattern , cond , cond_len );
218
227
prefix = prepare_include_condition_pattern (& pattern );
219
228
@@ -242,13 +251,14 @@ static int include_by_gitdir(const char *cond, size_t cond_len, int icase)
242
251
return ret ;
243
252
}
244
253
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 )
246
256
{
247
257
248
258
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 );
250
260
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 );
252
262
253
263
/* unknown conditionals are always false */
254
264
return 0 ;
@@ -273,7 +283,7 @@ int git_config_include(const char *var, const char *value, void *data)
273
283
ret = handle_path_include (value , inc );
274
284
275
285
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 )) &&
277
287
!strcmp (key , "path" ))
278
288
ret = handle_path_include (value , inc );
279
289
@@ -1511,12 +1521,20 @@ int git_config_system(void)
1511
1521
return !git_env_bool ("GIT_CONFIG_NOSYSTEM" , 0 );
1512
1522
}
1513
1523
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 )
1515
1526
{
1516
1527
int ret = 0 ;
1517
1528
char * xdg_config = xdg_config_home ("config" );
1518
1529
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 ;
1520
1538
1521
1539
current_parsing_scope = CONFIG_SCOPE_SYSTEM ;
1522
1540
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)
1547
1565
1548
1566
int git_config_with_options (config_fn_t fn , void * data ,
1549
1567
struct git_config_source * config_source ,
1550
- int respect_includes )
1568
+ const struct config_options * opts )
1551
1569
{
1552
1570
struct config_include_data inc = CONFIG_INCLUDE_INIT ;
1553
1571
1554
- if (respect_includes ) {
1572
+ if (opts -> respect_includes ) {
1555
1573
inc .fn = fn ;
1556
1574
inc .data = data ;
1575
+ inc .opts = opts ;
1557
1576
fn = git_config_include ;
1558
1577
data = & inc ;
1559
1578
}
@@ -1569,12 +1588,15 @@ int git_config_with_options(config_fn_t fn, void *data,
1569
1588
else if (config_source && config_source -> blob )
1570
1589
return git_config_from_blob_ref (fn , config_source -> blob , data );
1571
1590
1572
- return do_git_config_sequence (fn , data );
1591
+ return do_git_config_sequence (opts , fn , data );
1573
1592
}
1574
1593
1575
1594
static void git_config_raw (config_fn_t fn , void * data )
1576
1595
{
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 )
1578
1600
/*
1579
1601
* git_config_with_options() normally returns only
1580
1602
* 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)
1614
1636
1615
1637
void read_early_config (config_fn_t cb , void * data )
1616
1638
{
1639
+ struct config_options opts = {0 };
1617
1640
struct strbuf buf = STRBUF_INIT ;
1618
1641
1619
- git_config_with_options ( cb , data , NULL , 1 ) ;
1642
+ opts . respect_includes = 1 ;
1620
1643
1644
+ if (have_git_dir ())
1645
+ opts .git_dir = get_git_dir ();
1621
1646
/*
1622
1647
* When setup_git_directory() was not yet asked to discover the
1623
1648
* 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)
1626
1651
* notably, the current working directory is still the same after the
1627
1652
* call).
1628
1653
*/
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 );
1631
1658
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
- }
1637
1659
strbuf_release (& buf );
1638
1660
}
1639
1661
0 commit comments