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 <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jun 14, 2017 at 11:07 UTC dc8441fdb4598f54865a5c783d1f86c1e0bcb6dc
3 files changed +29 -24
builtin/config.c
+15 -11
@@ -243,8 +243,8 @@ static int get_value(const char *key_, const char *regex_)
243 }
244 }
245
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);
248
249 ret = !values.nr;
250
@@ -321,8 +321,8 @@ static void get_color(const char *var, const char *def_color)
321 get_color_slot = var;
322 get_color_found = 0;
323 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);
326
327 if (!get_color_found && def_color) {
328 if (color_parse(def_color, parsed_color) < 0)
@@ -353,8 +353,8 @@ static int get_colorbool(const char *var, int print)
353 get_colorbool_found = -1;
354 get_diff_color_found = -1;
355 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);
358
359 if (get_colorbool_found < 0) {
360 if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -442,8 +442,8 @@ static int get_urlmatch(const char *var, const char *url)
442 show_keys = 1;
443 }
444
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);
447
448 ret = !values.nr;
449
@@ -536,6 +536,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
536 config_options.respect_includes = !given_config_source.file;
537 else
538 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 + }
543
544 if (end_null) {
545 term = '\0';
@@ -580,9 +584,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
584
585 if (actions == ACTION_LIST) {
586 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) {
590 if (given_config_source.file)
591 die_errno("unable to read config file '%s'",
592 given_config_source.file);
config.c
+11 -10
@@ -218,8 +218,6 @@ static int include_by_gitdir(const struct config_options *opts,
218
219 if (opts->git_dir)
220 git_dir = opts->git_dir;
221 - else if (have_git_dir())
222 - git_dir = get_git_dir();
221 else
222 goto done;
223
@@ -1533,8 +1531,6 @@ static int do_git_config_sequence(const struct config_options *opts,
1531
1532 if (opts->commondir)
1533 repo_config = mkpathdup("%s/config", opts->commondir);
1536 - else if (have_git_dir())
1537 - repo_config = git_pathdup("config");
1534 else
1535 repo_config = NULL;
1536
@@ -1565,9 +1561,9 @@ static int do_git_config_sequence(const struct config_options *opts,
1561 return ret;
1562 }
1563
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)
1567 {
1568 struct config_include_data inc = CONFIG_INCLUDE_INIT;
1569
@@ -1598,9 +1594,14 @@ static void git_config_raw(config_fn_t fn, void *data)
1594 struct config_options opts = {0};
1595
1596 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)
1603 /*
1603 - * git_config_with_options() normally returns only
1604 + * config_with_options() normally returns only
1605 * zero, as most errors are fatal, and
1606 * non-fatal potential errors are guarded by "if"
1607 * statements that are entered only when no error is
@@ -1660,7 +1661,7 @@ void read_early_config(config_fn_t cb, void *data)
1661 opts.git_dir = gitdir.buf;
1662 }
1663
1663 - git_config_with_options(cb, data, NULL, &opts);
1664 + config_with_options(cb, data, NULL, &opts);
1665
1666 strbuf_release(&commondir);
1667 strbuf_release(&gitdir);
config.h
+3 -3
@@ -45,9 +45,9 @@ extern void git_config_push_parameter(const char *text);
45 extern int git_config_from_parameters(config_fn_t fn, void *data);
46 extern void read_early_config(config_fn_t cb, void *data);
47 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);
51 extern int git_parse_ulong(const char *, unsigned long *);
52 extern int git_parse_maybe_bool(const char *);
53 extern int git_config_int(const char *, const char *);