Skip to content

Commit dc8441f

Browse files
bmwillgitster
authored andcommitted
config: don't implicitly use gitdir or commondir
'git_config_with_options()' takes a 'config_options' struct which contains feilds for 'git_dir' and 'commondir'. If those feilds happen to be NULL the config machinery falls back to querying global repository state. Let's change this and instead use these fields in the 'config_options' struct explicilty all the time. Since the API is slightly changing to require these two fields to be set if callers want the config machinery to load the repository's config, let's change the name to 'config_with_optison()'. This allows the config machinery to not implicitly rely on any global repository state. Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a577fb5 commit dc8441f

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

builtin/config.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ static int get_value(const char *key_, const char *regex_)
243243
}
244244
}
245245

246-
git_config_with_options(collect_config, &values,
247-
&given_config_source, &config_options);
246+
config_with_options(collect_config, &values,
247+
&given_config_source, &config_options);
248248

249249
ret = !values.nr;
250250

@@ -321,8 +321,8 @@ static void get_color(const char *var, const char *def_color)
321321
get_color_slot = var;
322322
get_color_found = 0;
323323
parsed_color[0] = '\0';
324-
git_config_with_options(git_get_color_config, NULL,
325-
&given_config_source, &config_options);
324+
config_with_options(git_get_color_config, NULL,
325+
&given_config_source, &config_options);
326326

327327
if (!get_color_found && def_color) {
328328
if (color_parse(def_color, parsed_color) < 0)
@@ -353,8 +353,8 @@ static int get_colorbool(const char *var, int print)
353353
get_colorbool_found = -1;
354354
get_diff_color_found = -1;
355355
get_color_ui_found = -1;
356-
git_config_with_options(git_get_colorbool_config, NULL,
357-
&given_config_source, &config_options);
356+
config_with_options(git_get_colorbool_config, NULL,
357+
&given_config_source, &config_options);
358358

359359
if (get_colorbool_found < 0) {
360360
if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -442,8 +442,8 @@ static int get_urlmatch(const char *var, const char *url)
442442
show_keys = 1;
443443
}
444444

445-
git_config_with_options(urlmatch_config_entry, &config,
446-
&given_config_source, &config_options);
445+
config_with_options(urlmatch_config_entry, &config,
446+
&given_config_source, &config_options);
447447

448448
ret = !values.nr;
449449

@@ -536,6 +536,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
536536
config_options.respect_includes = !given_config_source.file;
537537
else
538538
config_options.respect_includes = respect_includes_opt;
539+
if (!nongit) {
540+
config_options.commondir = get_git_common_dir();
541+
config_options.git_dir = get_git_dir();
542+
}
539543

540544
if (end_null) {
541545
term = '\0';
@@ -580,9 +584,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
580584

581585
if (actions == ACTION_LIST) {
582586
check_argc(argc, 0, 0);
583-
if (git_config_with_options(show_all_config, NULL,
584-
&given_config_source,
585-
&config_options) < 0) {
587+
if (config_with_options(show_all_config, NULL,
588+
&given_config_source,
589+
&config_options) < 0) {
586590
if (given_config_source.file)
587591
die_errno("unable to read config file '%s'",
588592
given_config_source.file);

config.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ static int include_by_gitdir(const struct config_options *opts,
218218

219219
if (opts->git_dir)
220220
git_dir = opts->git_dir;
221-
else if (have_git_dir())
222-
git_dir = get_git_dir();
223221
else
224222
goto done;
225223

@@ -1533,8 +1531,6 @@ static int do_git_config_sequence(const struct config_options *opts,
15331531

15341532
if (opts->commondir)
15351533
repo_config = mkpathdup("%s/config", opts->commondir);
1536-
else if (have_git_dir())
1537-
repo_config = git_pathdup("config");
15381534
else
15391535
repo_config = NULL;
15401536

@@ -1565,9 +1561,9 @@ static int do_git_config_sequence(const struct config_options *opts,
15651561
return ret;
15661562
}
15671563

1568-
int git_config_with_options(config_fn_t fn, void *data,
1569-
struct git_config_source *config_source,
1570-
const struct config_options *opts)
1564+
int config_with_options(config_fn_t fn, void *data,
1565+
struct git_config_source *config_source,
1566+
const struct config_options *opts)
15711567
{
15721568
struct config_include_data inc = CONFIG_INCLUDE_INIT;
15731569

@@ -1598,9 +1594,14 @@ static void git_config_raw(config_fn_t fn, void *data)
15981594
struct config_options opts = {0};
15991595

16001596
opts.respect_includes = 1;
1601-
if (git_config_with_options(fn, data, NULL, &opts) < 0)
1597+
if (have_git_dir()) {
1598+
opts.commondir = get_git_common_dir();
1599+
opts.git_dir = get_git_dir();
1600+
}
1601+
1602+
if (config_with_options(fn, data, NULL, &opts) < 0)
16021603
/*
1603-
* git_config_with_options() normally returns only
1604+
* config_with_options() normally returns only
16041605
* zero, as most errors are fatal, and
16051606
* non-fatal potential errors are guarded by "if"
16061607
* statements that are entered only when no error is
@@ -1660,7 +1661,7 @@ void read_early_config(config_fn_t cb, void *data)
16601661
opts.git_dir = gitdir.buf;
16611662
}
16621663

1663-
git_config_with_options(cb, data, NULL, &opts);
1664+
config_with_options(cb, data, NULL, &opts);
16641665

16651666
strbuf_release(&commondir);
16661667
strbuf_release(&gitdir);

config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ extern void git_config_push_parameter(const char *text);
4545
extern int git_config_from_parameters(config_fn_t fn, void *data);
4646
extern void read_early_config(config_fn_t cb, void *data);
4747
extern void git_config(config_fn_t fn, void *);
48-
extern int git_config_with_options(config_fn_t fn, void *,
49-
struct git_config_source *config_source,
50-
const struct config_options *opts);
48+
extern int config_with_options(config_fn_t fn, void *,
49+
struct git_config_source *config_source,
50+
const struct config_options *opts);
5151
extern int git_parse_ulong(const char *, unsigned long *);
5252
extern int git_parse_maybe_bool(const char *);
5353
extern int git_config_int(const char *, const char *);

0 commit comments

Comments
 (0)