Skip to content

Commit 2185fde

Browse files
pcloudsgitster
authored andcommitted
config: handle conditional include when $GIT_DIR is not set up
If setup_git_directory() and friends have not been called, get_git_dir() (because of includeIf.gitdir:XXX) would lead to die("BUG: setup_git_env called without repository"); There are two cases when a config file could be read before $GIT_DIR is located. The first one is check_repository_format(), where we read just the one file $GIT_DIR/config to check if we could understand this repository. This case should be safe. We do not parse include directives, which can only be triggered from git_config_with_options, but setup code uses a lower-level function. The concerned variables should never be hidden away behind includes anyway. The second one is triggered in check_pager_config() when we're about to run an external git command. We might be able to find $GIT_DIR in this case, which is exactly what read_early_config() does (and also is what check_pager_config() uses). Conditional includes and get_git_dir() could be triggered by the first git_config_with_options() call there, before discover_git_directory() is used as a fallback $GIT_DIR detection. Detect this special "early reading" case, pass down the $GIT_DIR, either from previous setup or detected by discover_git_directory(), and make conditional include use it. Noticed-by: Bert Wesarg <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c48f4b3 commit 2185fde

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,7 @@ enum config_origin_type {
18871887

18881888
struct config_options {
18891889
unsigned int respect_includes : 1;
1890+
const char *git_dir;
18901891
};
18911892

18921893
typedef int (*config_fn_t)(const char *, const char *, void *);

config.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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_add_absolute_path(&text, get_git_dir());
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_add_absolute_path(&text, git_dir);
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

@@ -1603,10 +1613,12 @@ void read_early_config(config_fn_t cb, void *data)
16031613
{
16041614
struct config_options opts = {0};
16051615
struct strbuf buf = STRBUF_INIT;
1616+
char *to_free = NULL;
16061617

16071618
opts.respect_includes = 1;
1608-
git_config_with_options(cb, data, NULL, &opts);
16091619

1620+
if (have_git_dir())
1621+
opts.git_dir = get_git_dir();
16101622
/*
16111623
* When setup_git_directory() was not yet asked to discover the
16121624
* GIT_DIR, we ask discover_git_directory() to figure out whether there
@@ -1615,7 +1627,12 @@ void read_early_config(config_fn_t cb, void *data)
16151627
* notably, the current working directory is still the same after the
16161628
* call).
16171629
*/
1618-
if (!have_git_dir() && discover_git_directory(&buf)) {
1630+
else if (discover_git_directory(&buf))
1631+
opts.git_dir = to_free = xstrdup(buf.buf);
1632+
1633+
git_config_with_options(cb, data, NULL, &opts);
1634+
1635+
if (!have_git_dir() && opts.git_dir) {
16191636
struct git_config_source repo_config;
16201637

16211638
memset(&repo_config, 0, sizeof(repo_config));
@@ -1624,6 +1641,7 @@ void read_early_config(config_fn_t cb, void *data)
16241641
git_config_with_options(cb, data, &repo_config, &opts);
16251642
}
16261643
strbuf_release(&buf);
1644+
free(to_free);
16271645
}
16281646

16291647
static void git_config_check_init(void);

t/t1305-config-include.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ test_expect_success 'conditional include, both unanchored, icase' '
208208
)
209209
'
210210

211+
test_expect_success 'conditional include, early config reading' '
212+
(
213+
cd foo &&
214+
echo "[includeIf \"gitdir:foo/\"]path=bar6" >>.git/config &&
215+
echo "[test]six=6" >.git/bar6 &&
216+
echo 6 >expect &&
217+
test-config read_early_config test.six >actual &&
218+
test_cmp expect actual
219+
)
220+
'
221+
211222
test_expect_success 'include cycles are detected' '
212223
cat >.gitconfig <<-\EOF &&
213224
[test]value = gitconfig

0 commit comments

Comments
 (0)