Skip to content

Commit c9672ba

Browse files
committed
Merge branch 'nd/conditional-config-in-early-config'
The recently introduced conditional inclusion of configuration did not work well when early-config mechanism was involved. * nd/conditional-config-in-early-config: config: correct file reading order in read_early_config() config: handle conditional include when $GIT_DIR is not set up config: prepare to pass more info in git_config_with_options()
2 parents 46bdfa3 + e145a0b commit c9672ba

File tree

5 files changed

+90
-30
lines changed

5 files changed

+90
-30
lines changed

builtin/config.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static int use_global_config, use_system_config, use_local_config;
2626
static struct git_config_source given_config_source;
2727
static int actions, types;
2828
static int end_null;
29-
static int respect_includes = -1;
29+
static int respect_includes_opt = -1;
30+
static struct config_options config_options;
3031
static int show_origin;
3132

3233
#define ACTION_GET (1<<0)
@@ -81,7 +82,7 @@ static struct option builtin_config_options[] = {
8182
OPT_GROUP(N_("Other")),
8283
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
8384
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
84-
OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
85+
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
8586
OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
8687
OPT_END(),
8788
};
@@ -242,7 +243,7 @@ static int get_value(const char *key_, const char *regex_)
242243
}
243244

244245
git_config_with_options(collect_config, &values,
245-
&given_config_source, respect_includes);
246+
&given_config_source, &config_options);
246247

247248
ret = !values.nr;
248249

@@ -320,7 +321,7 @@ static void get_color(const char *var, const char *def_color)
320321
get_color_found = 0;
321322
parsed_color[0] = '\0';
322323
git_config_with_options(git_get_color_config, NULL,
323-
&given_config_source, respect_includes);
324+
&given_config_source, &config_options);
324325

325326
if (!get_color_found && def_color) {
326327
if (color_parse(def_color, parsed_color) < 0)
@@ -352,7 +353,7 @@ static int get_colorbool(const char *var, int print)
352353
get_diff_color_found = -1;
353354
get_color_ui_found = -1;
354355
git_config_with_options(git_get_colorbool_config, NULL,
355-
&given_config_source, respect_includes);
356+
&given_config_source, &config_options);
356357

357358
if (get_colorbool_found < 0) {
358359
if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -441,7 +442,7 @@ static int get_urlmatch(const char *var, const char *url)
441442
}
442443

443444
git_config_with_options(urlmatch_config_entry, &config,
444-
&given_config_source, respect_includes);
445+
&given_config_source, &config_options);
445446

446447
ret = !values.nr;
447448

@@ -530,8 +531,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
530531
prefix_filename(prefix, given_config_source.file);
531532
}
532533

533-
if (respect_includes == -1)
534-
respect_includes = !given_config_source.file;
534+
if (respect_includes_opt == -1)
535+
config_options.respect_includes = !given_config_source.file;
536+
else
537+
config_options.respect_includes = respect_includes_opt;
535538

536539
if (end_null) {
537540
term = '\0';
@@ -578,7 +581,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
578581
check_argc(argc, 0, 0);
579582
if (git_config_with_options(show_all_config, NULL,
580583
&given_config_source,
581-
respect_includes) < 0) {
584+
&config_options) < 0) {
582585
if (given_config_source.file)
583586
die_errno("unable to read config file '%s'",
584587
given_config_source.file);

cache.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,11 @@ enum config_origin_type {
18911891
CONFIG_ORIGIN_CMDLINE
18921892
};
18931893

1894+
struct config_options {
1895+
unsigned int respect_includes : 1;
1896+
const char *git_dir;
1897+
};
1898+
18941899
typedef int (*config_fn_t)(const char *, const char *, void *);
18951900
extern int git_default_config(const char *, const char *, void *);
18961901
extern int git_config_from_file(config_fn_t fn, const char *, void *);
@@ -1904,7 +1909,7 @@ extern void read_early_config(config_fn_t cb, void *data);
19041909
extern void git_config(config_fn_t fn, void *);
19051910
extern int git_config_with_options(config_fn_t fn, void *,
19061911
struct git_config_source *config_source,
1907-
int respect_includes);
1912+
const struct config_options *opts);
19081913
extern int git_parse_ulong(const char *, unsigned long *);
19091914
extern int git_parse_maybe_bool(const char *);
19101915
extern int git_config_int(const char *, const char *);
@@ -1957,6 +1962,7 @@ struct config_include_data {
19571962
int depth;
19581963
config_fn_t fn;
19591964
void *data;
1965+
const struct config_options *opts;
19601966
};
19611967
#define CONFIG_INCLUDE_INIT { 0 }
19621968
extern int git_config_include(const char *name, const char *value, void *data);

config.c

Lines changed: 42 additions & 20 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_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

15481566
int 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

15751594
static 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

16151637
void 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

t/t1305-config-include.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,17 @@ test_expect_success 'conditional include, both unanchored, icase' '
218218
)
219219
'
220220

221+
test_expect_success 'conditional include, early config reading' '
222+
(
223+
cd foo &&
224+
echo "[includeIf \"gitdir:foo/\"]path=bar6" >>.git/config &&
225+
echo "[test]six=6" >.git/bar6 &&
226+
echo 6 >expect &&
227+
test-config read_early_config test.six >actual &&
228+
test_cmp expect actual
229+
)
230+
'
231+
221232
test_expect_success SYMLINKS 'conditional include, set up symlinked $HOME' '
222233
mkdir real-home &&
223234
ln -s real-home home &&

t/t1309-early-config.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ test_expect_success 'ceiling #2' '
4747
test xdg = "$(cat output)"
4848
'
4949

50+
cmdline_config="'test.source=cmdline'"
51+
test_expect_success 'read config file in right order' '
52+
echo "[test]source = home" >>.gitconfig &&
53+
git init foo &&
54+
(
55+
cd foo &&
56+
echo "[test]source = repo" >>.git/config &&
57+
GIT_CONFIG_PARAMETERS=$cmdline_config test-config \
58+
read_early_config test.source >actual &&
59+
cat >expected <<-\EOF &&
60+
home
61+
repo
62+
cmdline
63+
EOF
64+
test_cmp expected actual
65+
)
66+
'
67+
5068
test_with_config () {
5169
rm -rf throwaway &&
5270
git init throwaway &&

0 commit comments

Comments
 (0)