Merge branch 'gc/config-context' into next

Reduce reliance on a global state in the config reading API.

* gc/config-context:
  config: pass source to config_parser_event_fn_t
  config: add kvi.path, use it to evaluate includes
  config.c: remove config_reader from configsets
  config: pass kvi to die_bad_number()
  trace2: plumb config kvi
  config.c: pass ctx with CLI config
  config: pass ctx with config files
  config.c: pass ctx in configsets
  config: add ctx arg to config_fn_t
  urlmatch.h: use config_fn_t type
  config: inline git_color_default_config
This commit is contained in:
Junio C Hamano
2023-06-28 23:04:42 -07:00
103 changed files with 960 additions and 632 deletions

View File

@ -195,37 +195,42 @@ static void check_argc(int argc, int min, int max)
usage_builtin_config();
}
static void show_config_origin(struct strbuf *buf)
static void show_config_origin(const struct key_value_info *kvi,
struct strbuf *buf)
{
const char term = end_nul ? '\0' : '\t';
strbuf_addstr(buf, current_config_origin_type());
strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
strbuf_addch(buf, ':');
if (end_nul)
strbuf_addstr(buf, current_config_name());
strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
else
quote_c_style(current_config_name(), buf, NULL, 0);
quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
strbuf_addch(buf, term);
}
static void show_config_scope(struct strbuf *buf)
static void show_config_scope(const struct key_value_info *kvi,
struct strbuf *buf)
{
const char term = end_nul ? '\0' : '\t';
const char *scope = config_scope_name(current_config_scope());
const char *scope = config_scope_name(kvi->scope);
strbuf_addstr(buf, N_(scope));
strbuf_addch(buf, term);
}
static int show_all_config(const char *key_, const char *value_,
const struct config_context *ctx,
void *cb UNUSED)
{
const struct key_value_info *kvi = ctx->kvi;
if (show_origin || show_scope) {
struct strbuf buf = STRBUF_INIT;
if (show_scope)
show_config_scope(&buf);
show_config_scope(kvi, &buf);
if (show_origin)
show_config_origin(&buf);
show_config_origin(kvi, &buf);
/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
fwrite(buf.buf, 1, buf.len, stdout);
strbuf_release(&buf);
@ -243,12 +248,13 @@ struct strbuf_list {
int alloc;
};
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
static int format_config(struct strbuf *buf, const char *key_,
const char *value_, const struct key_value_info *kvi)
{
if (show_scope)
show_config_scope(buf);
show_config_scope(kvi, buf);
if (show_origin)
show_config_origin(buf);
show_config_origin(kvi, buf);
if (show_keys)
strbuf_addstr(buf, key_);
if (!omit_values) {
@ -257,13 +263,14 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
if (type == TYPE_INT)
strbuf_addf(buf, "%"PRId64,
git_config_int64(key_, value_ ? value_ : ""));
git_config_int64(key_, value_ ? value_ : "", kvi));
else if (type == TYPE_BOOL)
strbuf_addstr(buf, git_config_bool(key_, value_) ?
"true" : "false");
else if (type == TYPE_BOOL_OR_INT) {
int is_bool, v;
v = git_config_bool_or_int(key_, value_, &is_bool);
v = git_config_bool_or_int(key_, value_, kvi,
&is_bool);
if (is_bool)
strbuf_addstr(buf, v ? "true" : "false");
else
@ -302,9 +309,11 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
return 0;
}
static int collect_config(const char *key_, const char *value_, void *cb)
static int collect_config(const char *key_, const char *value_,
const struct config_context *ctx, void *cb)
{
struct strbuf_list *values = cb;
const struct key_value_info *kvi = ctx->kvi;
if (!use_key_regexp && strcmp(key_, key))
return 0;
@ -319,7 +328,7 @@ static int collect_config(const char *key_, const char *value_, void *cb)
ALLOC_GROW(values->items, values->nr + 1, values->alloc);
strbuf_init(&values->items[values->nr], 0);
return format_config(&values->items[values->nr++], key_, value_);
return format_config(&values->items[values->nr++], key_, value_, kvi);
}
static int get_value(const char *key_, const char *regex_, unsigned flags)
@ -381,11 +390,14 @@ static int get_value(const char *key_, const char *regex_, unsigned flags)
&config_options);
if (!values.nr && default_value) {
struct key_value_info kvi = KVI_INIT;
struct strbuf *item;
kvi_from_param(&kvi);
ALLOC_GROW(values.items, values.nr + 1, values.alloc);
item = &values.items[values.nr++];
strbuf_init(item, 0);
if (format_config(item, key_, default_value) < 0)
if (format_config(item, key_, default_value, &kvi) < 0)
die(_("failed to format default config value: %s"),
default_value);
}
@ -414,7 +426,8 @@ free_strings:
return ret;
}
static char *normalize_value(const char *key, const char *value)
static char *normalize_value(const char *key, const char *value,
struct key_value_info *kvi)
{
if (!value)
return NULL;
@ -429,12 +442,12 @@ static char *normalize_value(const char *key, const char *value)
*/
return xstrdup(value);
if (type == TYPE_INT)
return xstrfmt("%"PRId64, git_config_int64(key, value));
return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
if (type == TYPE_BOOL)
return xstrdup(git_config_bool(key, value) ? "true" : "false");
if (type == TYPE_BOOL_OR_INT) {
int is_bool, v;
v = git_config_bool_or_int(key, value, &is_bool);
v = git_config_bool_or_int(key, value, kvi, &is_bool);
if (!is_bool)
return xstrfmt("%d", v);
else
@ -471,6 +484,7 @@ static const char *get_colorbool_slot;
static char parsed_color[COLOR_MAXLEN];
static int git_get_color_config(const char *var, const char *value,
const struct config_context *ctx UNUSED,
void *cb UNUSED)
{
if (!strcmp(var, get_color_slot)) {
@ -504,6 +518,7 @@ static int get_colorbool_found;
static int get_diff_color_found;
static int get_color_ui_found;
static int git_get_colorbool_config(const char *var, const char *value,
const struct config_context *ctx UNUSED,
void *data UNUSED)
{
if (!strcmp(var, get_colorbool_slot))
@ -560,13 +575,17 @@ static void check_write(void)
struct urlmatch_current_candidate_value {
char value_is_null;
struct strbuf value;
struct key_value_info kvi;
};
static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
static int urlmatch_collect_fn(const char *var, const char *value,
const struct config_context *ctx,
void *cb)
{
struct string_list *values = cb;
struct string_list_item *item = string_list_insert(values, var);
struct urlmatch_current_candidate_value *matched = item->util;
const struct key_value_info *kvi = ctx->kvi;
if (!matched) {
matched = xmalloc(sizeof(*matched));
@ -575,6 +594,7 @@ static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
} else {
strbuf_reset(&matched->value);
}
matched->kvi = *kvi;
if (value) {
strbuf_addstr(&matched->value, value);
@ -622,7 +642,8 @@ static int get_urlmatch(const char *var, const char *url)
struct strbuf buf = STRBUF_INIT;
format_config(&buf, item->string,
matched->value_is_null ? NULL : matched->value.buf);
matched->value_is_null ? NULL : matched->value.buf,
&matched->kvi);
fwrite(buf.buf, 1, buf.len, stdout);
strbuf_release(&buf);
@ -656,6 +677,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
char *value = NULL;
int flags = 0;
int ret = 0;
struct key_value_info default_kvi = KVI_INIT;
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
@ -873,7 +895,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
else if (actions == ACTION_SET) {
check_write();
check_argc(argc, 2, 2);
value = normalize_value(argv[0], argv[1]);
value = normalize_value(argv[0], argv[1], &default_kvi);
ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
if (ret == CONFIG_NOTHING_SET)
error(_("cannot overwrite multiple values with a single value\n"
@ -882,7 +904,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
else if (actions == ACTION_SET_ALL) {
check_write();
check_argc(argc, 2, 3);
value = normalize_value(argv[0], argv[1]);
value = normalize_value(argv[0], argv[1], &default_kvi);
ret = git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], value, argv[2],
flags);
@ -890,7 +912,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
else if (actions == ACTION_ADD) {
check_write();
check_argc(argc, 2, 2);
value = normalize_value(argv[0], argv[1]);
value = normalize_value(argv[0], argv[1], &default_kvi);
ret = git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], value,
CONFIG_REGEX_NONE,
@ -899,7 +921,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
else if (actions == ACTION_REPLACE_ALL) {
check_write();
check_argc(argc, 2, 3);
value = normalize_value(argv[0], argv[1]);
value = normalize_value(argv[0], argv[1], &default_kvi);
ret = git_config_set_multivar_in_file_gently(given_config_source.file,
argv[0], value, argv[2],
flags | CONFIG_FLAGS_MULTI_REPLACE);