Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2023-07-06 21:54:48 +0300
committerJunio C Hamano <gitster@pobox.com>2023-07-06 21:54:48 +0300
commitb3d1c85d4833aef546f11e4d37516a1ececaefc3 (patch)
treeb5e799c3279f98f5172ce18cfd0e9b317e9822dd /config.c
parent1d76e69212102c3373b552186590b76d6ad8d84c (diff)
parent6e8e7981eb86e2c19401825dbdb55d5860d11313 (diff)
Merge branch 'gc/config-context'
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
Diffstat (limited to 'config.c')
-rw-r--r--config.c546
1 files changed, 220 insertions, 326 deletions
diff --git a/config.c b/config.c
index 08a782e6a7..85c5f35132 100644
--- a/config.c
+++ b/config.c
@@ -69,72 +69,6 @@ struct config_source {
};
#define CONFIG_SOURCE_INIT { 0 }
-struct config_reader {
- /*
- * These members record the "current" config source, which can be
- * accessed by parsing callbacks.
- *
- * The "source" variable will be non-NULL only when we are actually
- * parsing a real config source (file, blob, cmdline, etc).
- *
- * The "config_kvi" variable will be non-NULL only when we are feeding
- * cached config from a configset into a callback.
- *
- * They cannot be non-NULL at the same time. If they are both NULL, then
- * we aren't parsing anything (and depending on the function looking at
- * the variables, it's either a bug for it to be called in the first
- * place, or it's a function which can be reused for non-config
- * purposes, and should fall back to some sane behavior).
- */
- struct config_source *source;
- struct key_value_info *config_kvi;
- /*
- * The "scope" of the current config source being parsed (repo, global,
- * etc). Like "source", this is only set when parsing a config source.
- * It's not part of "source" because it transcends a single file (i.e.,
- * a file included from .git/config is still in "repo" scope).
- *
- * When iterating through a configset, the equivalent value is
- * "config_kvi.scope" (see above).
- */
- enum config_scope parsing_scope;
-};
-/*
- * Where possible, prefer to accept "struct config_reader" as an arg than to use
- * "the_reader". "the_reader" should only be used if that is infeasible, e.g. in
- * a public function.
- */
-static struct config_reader the_reader;
-
-static inline void config_reader_push_source(struct config_reader *reader,
- struct config_source *top)
-{
- top->prev = reader->source;
- reader->source = top;
-}
-
-static inline struct config_source *config_reader_pop_source(struct config_reader *reader)
-{
- struct config_source *ret;
- if (!reader->source)
- BUG("tried to pop config source, but we weren't reading config");
- ret = reader->source;
- reader->source = reader->source->prev;
- return ret;
-}
-
-static inline void config_reader_set_kvi(struct config_reader *reader,
- struct key_value_info *kvi)
-{
- reader->config_kvi = kvi;
-}
-
-static inline void config_reader_set_scope(struct config_reader *reader,
- enum config_scope scope)
-{
- reader->parsing_scope = scope;
-}
-
static int pack_compression_seen;
static int zlib_compression_seen;
@@ -197,7 +131,6 @@ struct config_include_data {
const struct config_options *opts;
struct git_config_source *config_source;
struct repository *repo;
- struct config_reader *config_reader;
/*
* All remote URLs discovered when reading all config files.
@@ -206,7 +139,8 @@ struct config_include_data {
};
#define CONFIG_INCLUDE_INIT { 0 }
-static int git_config_include(const char *var, const char *value, void *data);
+static int git_config_include(const char *var, const char *value,
+ const struct config_context *ctx, void *data);
#define MAX_INCLUDE_DEPTH 10
static const char include_depth_advice[] = N_(
@@ -215,7 +149,8 @@ static const char include_depth_advice[] = N_(
"from\n"
" %s\n"
"This might be due to circular includes.");
-static int handle_path_include(struct config_source *cs, const char *path,
+static int handle_path_include(const struct key_value_info *kvi,
+ const char *path,
struct config_include_data *inc)
{
int ret = 0;
@@ -237,14 +172,14 @@ static int handle_path_include(struct config_source *cs, const char *path,
if (!is_absolute_path(path)) {
char *slash;
- if (!cs || !cs->path) {
+ if (!kvi || !kvi->path) {
ret = error(_("relative config includes must come from files"));
goto cleanup;
}
- slash = find_last_dir_sep(cs->path);
+ slash = find_last_dir_sep(kvi->path);
if (slash)
- strbuf_add(&buf, cs->path, slash - cs->path + 1);
+ strbuf_add(&buf, kvi->path, slash - kvi->path + 1);
strbuf_addstr(&buf, path);
path = buf.buf;
}
@@ -252,10 +187,11 @@ static int handle_path_include(struct config_source *cs, const char *path,
if (!access_or_die(path, R_OK, 0)) {
if (++inc->depth > MAX_INCLUDE_DEPTH)
die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path,
- !cs ? "<unknown>" :
- cs->name ? cs->name :
+ !kvi ? "<unknown>" :
+ kvi->filename ? kvi->filename :
"the command line");
- ret = git_config_from_file(git_config_include, path, inc);
+ ret = git_config_from_file_with_options(git_config_include, path, inc,
+ kvi->scope, NULL);
inc->depth--;
}
cleanup:
@@ -270,7 +206,7 @@ static void add_trailing_starstar_for_dir(struct strbuf *pat)
strbuf_addstr(pat, "**");
}
-static int prepare_include_condition_pattern(struct config_source *cs,
+static int prepare_include_condition_pattern(const struct key_value_info *kvi,
struct strbuf *pat)
{
struct strbuf path = STRBUF_INIT;
@@ -287,11 +223,11 @@ static int prepare_include_condition_pattern(struct config_source *cs,
if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) {
const char *slash;
- if (!cs || !cs->path)
+ if (!kvi || !kvi->path)
return error(_("relative config include "
"conditionals must come from files"));
- strbuf_realpath(&path, cs->path, 1);
+ strbuf_realpath(&path, kvi->path, 1);
slash = find_last_dir_sep(path.buf);
if (!slash)
BUG("how is this possible?");
@@ -306,7 +242,7 @@ static int prepare_include_condition_pattern(struct config_source *cs,
return prefix;
}
-static int include_by_gitdir(struct config_source *cs,
+static int include_by_gitdir(const struct key_value_info *kvi,
const struct config_options *opts,
const char *cond, size_t cond_len, int icase)
{
@@ -323,7 +259,7 @@ static int include_by_gitdir(struct config_source *cs,
strbuf_realpath(&text, git_dir, 1);
strbuf_add(&pattern, cond, cond_len);
- prefix = prepare_include_condition_pattern(cs, &pattern);
+ prefix = prepare_include_condition_pattern(kvi, &pattern);
again:
if (prefix < 0)
@@ -385,7 +321,8 @@ static int include_by_branch(const char *cond, size_t cond_len)
return ret;
}
-static int add_remote_url(const char *var, const char *value, void *data)
+static int add_remote_url(const char *var, const char *value,
+ const struct config_context *ctx UNUSED, void *data)
{
struct string_list *remote_urls = data;
const char *remote_name;
@@ -404,22 +341,17 @@ static void populate_remote_urls(struct config_include_data *inc)
{
struct config_options opts;
- enum config_scope store_scope = inc->config_reader->parsing_scope;
-
opts = *inc->opts;
opts.unconditional_remote_url = 1;
- config_reader_set_scope(inc->config_reader, 0);
-
inc->remote_urls = xmalloc(sizeof(*inc->remote_urls));
string_list_init_dup(inc->remote_urls);
config_with_options(add_remote_url, inc->remote_urls,
inc->config_source, inc->repo, &opts);
-
- config_reader_set_scope(inc->config_reader, store_scope);
}
static int forbid_remote_url(const char *var, const char *value UNUSED,
+ const struct config_context *ctx UNUSED,
void *data UNUSED)
{
const char *remote_name;
@@ -463,16 +395,16 @@ static int include_by_remote_url(struct config_include_data *inc,
inc->remote_urls);
}
-static int include_condition_is_true(struct config_source *cs,
+static int include_condition_is_true(const struct key_value_info *kvi,
struct config_include_data *inc,
const char *cond, size_t cond_len)
{
const struct config_options *opts = inc->opts;
if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
- return include_by_gitdir(cs, opts, cond, cond_len, 0);
+ return include_by_gitdir(kvi, opts, cond, cond_len, 0);
else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
- return include_by_gitdir(cs, opts, cond, cond_len, 1);
+ return include_by_gitdir(kvi, opts, cond, cond_len, 1);
else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
return include_by_branch(cond, cond_len);
else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
@@ -483,10 +415,11 @@ static int include_condition_is_true(struct config_source *cs,
return 0;
}
-static int git_config_include(const char *var, const char *value, void *data)
+static int git_config_include(const char *var, const char *value,
+ const struct config_context *ctx,
+ void *data)
{
struct config_include_data *inc = data;
- struct config_source *cs = inc->config_reader->source;
const char *cond, *key;
size_t cond_len;
int ret;
@@ -495,21 +428,21 @@ static int git_config_include(const char *var, const char *value, void *data)
* Pass along all values, including "include" directives; this makes it
* possible to query information on the includes themselves.
*/
- ret = inc->fn(var, value, inc->data);
+ ret = inc->fn(var, value, ctx, inc->data);
if (ret < 0)
return ret;
if (!strcmp(var, "include.path"))
- ret = handle_path_include(cs, value, inc);
+ ret = handle_path_include(ctx->kvi, value, inc);
if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
- cond && include_condition_is_true(cs, inc, cond, cond_len) &&
+ cond && include_condition_is_true(ctx->kvi, inc, cond, cond_len) &&
!strcmp(key, "path")) {
config_fn_t old_fn = inc->fn;
if (inc->opts->unconditional_remote_url)
inc->fn = forbid_remote_url;
- ret = handle_path_include(cs, value, inc);
+ ret = handle_path_include(ctx->kvi, value, inc);
inc->fn = old_fn;
}
@@ -667,27 +600,45 @@ out_free_ret_1:
}
static int config_parse_pair(const char *key, const char *value,
- config_fn_t fn, void *data)
+ struct key_value_info *kvi,
+ config_fn_t fn, void *data)
{
char *canonical_name;
int ret;
+ struct config_context ctx = {
+ .kvi = kvi,
+ };
if (!strlen(key))
return error(_("empty config key"));
if (git_config_parse_key(key, &canonical_name, NULL))
return -1;
- ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
+ ret = (fn(canonical_name, value, &ctx, data) < 0) ? -1 : 0;
free(canonical_name);
return ret;
}
+
+/* for values read from `git_config_from_parameters()` */
+void kvi_from_param(struct key_value_info *out)
+{
+ out->filename = NULL;
+ out->linenr = -1;
+ out->origin_type = CONFIG_ORIGIN_CMDLINE;
+ out->scope = CONFIG_SCOPE_COMMAND;
+ out->path = NULL;
+}
+
int git_config_parse_parameter(const char *text,
config_fn_t fn, void *data)
{
const char *value;
struct strbuf **pair;
int ret;
+ struct key_value_info kvi = KVI_INIT;
+
+ kvi_from_param(&kvi);
pair = strbuf_split_str(text, '=', 2);
if (!pair[0])
@@ -706,12 +657,13 @@ int git_config_parse_parameter(const char *text,
return error(_("bogus config parameter: %s"), text);
}
- ret = config_parse_pair(pair[0]->buf, value, fn, data);
+ ret = config_parse_pair(pair[0]->buf, value, &kvi, fn, data);
strbuf_list_free(pair);
return ret;
}
-static int parse_config_env_list(char *env, config_fn_t fn, void *data)
+static int parse_config_env_list(char *env, struct key_value_info *kvi,
+ config_fn_t fn, void *data)
{
char *cur = env;
while (cur && *cur) {
@@ -745,7 +697,7 @@ static int parse_config_env_list(char *env, config_fn_t fn, void *data)
CONFIG_DATA_ENVIRONMENT);
}
- if (config_parse_pair(key, value, fn, data) < 0)
+ if (config_parse_pair(key, value, kvi, fn, data) < 0)
return -1;
}
else {
@@ -769,11 +721,9 @@ int git_config_from_parameters(config_fn_t fn, void *data)
struct strvec to_free = STRVEC_INIT;
int ret = 0;
char *envw = NULL;
- struct config_source source = CONFIG_SOURCE_INIT;
-
- source.origin_type = CONFIG_ORIGIN_CMDLINE;
- config_reader_push_source(&the_reader, &source);
+ struct key_value_info kvi = KVI_INIT;
+ kvi_from_param(&kvi);
env = getenv(CONFIG_COUNT_ENVIRONMENT);
if (env) {
unsigned long count;
@@ -809,7 +759,7 @@ int git_config_from_parameters(config_fn_t fn, void *data)
}
strbuf_reset(&envvar);
- if (config_parse_pair(key, value, fn, data) < 0) {
+ if (config_parse_pair(key, value, &kvi, fn, data) < 0) {
ret = -1;
goto out;
}
@@ -820,7 +770,7 @@ int git_config_from_parameters(config_fn_t fn, void *data)
if (env) {
/* sq_dequote will write over it */
envw = xstrdup(env);
- if (parse_config_env_list(envw, fn, data) < 0) {
+ if (parse_config_env_list(envw, &kvi, fn, data) < 0) {
ret = -1;
goto out;
}
@@ -830,7 +780,6 @@ out:
strbuf_release(&envvar);
strvec_clear(&to_free);
free(envw);
- config_reader_pop_source(&the_reader);
return ret;
}
@@ -931,12 +880,15 @@ static char *parse_value(struct config_source *cs)
}
}
-static int get_value(struct config_source *cs, config_fn_t fn, void *data,
- struct strbuf *name)
+static int get_value(struct config_source *cs, struct key_value_info *kvi,
+ config_fn_t fn, void *data, struct strbuf *name)
{
int c;
char *value;
int ret;
+ struct config_context ctx = {
+ .kvi = kvi,
+ };
/* Get the full name */
for (;;) {
@@ -965,7 +917,8 @@ static int get_value(struct config_source *cs, config_fn_t fn, void *data,
* accurate line number in error messages.
*/
cs->linenr--;
- ret = fn(name->buf, value, data);
+ kvi->linenr = cs->linenr;
+ ret = fn(name->buf, value, &ctx, data);
if (ret >= 0)
cs->linenr++;
return ret;
@@ -1055,7 +1008,7 @@ static int do_event(struct config_source *cs, enum config_event_t type,
if (data->previous_type != CONFIG_EVENT_EOF &&
data->opts->event_fn(data->previous_type, data->previous_offset,
- offset, data->opts->event_fn_data) < 0)
+ offset, cs, data->opts->event_fn_data) < 0)
return -1;
data->previous_type = type;
@@ -1064,8 +1017,20 @@ static int do_event(struct config_source *cs, enum config_event_t type,
return 0;
}
+static void kvi_from_source(struct config_source *cs,
+ enum config_scope scope,
+ struct key_value_info *out)
+{
+ out->filename = strintern(cs->name);
+ out->origin_type = cs->origin_type;
+ out->linenr = cs->linenr;
+ out->scope = scope;
+ out->path = cs->path;
+}
+
static int git_parse_source(struct config_source *cs, config_fn_t fn,
- void *data, const struct config_options *opts)
+ struct key_value_info *kvi, void *data,
+ const struct config_options *opts)
{
int comment = 0;
size_t baselen = 0;
@@ -1149,7 +1114,7 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn,
*/
strbuf_setlen(var, baselen);
strbuf_addch(var, tolower(c));
- if (get_value(cs, fn, data, var) < 0)
+ if (get_value(cs, kvi, fn, data, var) < 0)
break;
}
@@ -1325,80 +1290,78 @@ int git_parse_ssize_t(const char *value, ssize_t *ret)
return 1;
}
-static int reader_config_name(struct config_reader *reader, const char **out);
-static int reader_origin_type(struct config_reader *reader,
- enum config_origin_type *type);
NORETURN
-static void die_bad_number(struct config_reader *reader, const char *name,
- const char *value)
+static void die_bad_number(const char *name, const char *value,
+ const struct key_value_info *kvi)
{
const char *error_type = (errno == ERANGE) ?
N_("out of range") : N_("invalid unit");
const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s");
- const char *config_name = NULL;
- enum config_origin_type config_origin = CONFIG_ORIGIN_UNKNOWN;
+
+ if (!kvi)
+ BUG("kvi should not be NULL");
if (!value)
value = "";
- /* Ignoring the return value is okay since we handle missing values. */
- reader_config_name(reader, &config_name);
- reader_origin_type(reader, &config_origin);
-
- if (!config_name)
+ if (!kvi->filename)
die(_(bad_numeric), value, name, _(error_type));
- switch (config_origin) {
+ switch (kvi->origin_type) {
case CONFIG_ORIGIN_BLOB:
die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
- value, name, config_name, _(error_type));
+ value, name, kvi->filename, _(error_type));
case CONFIG_ORIGIN_FILE:
die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
- value, name, config_name, _(error_type));
+ value, name, kvi->filename, _(error_type));
case CONFIG_ORIGIN_STDIN:
die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
value, name, _(error_type));
case CONFIG_ORIGIN_SUBMODULE_BLOB:
die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
- value, name, config_name, _(error_type));
+ value, name, kvi->filename, _(error_type));
case CONFIG_ORIGIN_CMDLINE:
die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
- value, name, config_name, _(error_type));
+ value, name, kvi->filename, _(error_type));
default:
die(_("bad numeric config value '%s' for '%s' in %s: %s"),
- value, name, config_name, _(error_type));
+ value, name, kvi->filename, _(error_type));
}
}
-int git_config_int(const char *name, const char *value)
+int git_config_int(const char *name, const char *value,
+ const struct key_value_info *kvi)
{
int ret;
if (!git_parse_int(value, &ret))
- die_bad_number(&the_reader, name, value);
+ die_bad_number(name, value, kvi);
return ret;
}
-int64_t git_config_int64(const char *name, const char *value)
+int64_t git_config_int64(const char *name, const char *value,
+ const struct key_value_info *kvi)
{
int64_t ret;
if (!git_parse_int64(value, &ret))
- die_bad_number(&the_reader, name, value);
+ die_bad_number(name, value, kvi);
return ret;
}
-unsigned long git_config_ulong(const char *name, const char *value)
+unsigned long git_config_ulong(const char *name, const char *value,
+ const struct key_value_info *kvi)
{
unsigned long ret;
if (!git_parse_ulong(value, &ret))
- die_bad_number(&the_reader, name, value);
+ die_bad_number(name, value, kvi);
return ret;
}
-ssize_t git_config_ssize_t(const char *name, const char *value)
+ssize_t git_config_ssize_t(const char *name, const char *value,
+ const struct key_value_info *kvi)
{
ssize_t ret;
if (!git_parse_ssize_t(value, &ret))
- die_bad_number(&the_reader, name, value);
+ die_bad_number(name, value, kvi);
return ret;
}
@@ -1503,7 +1466,8 @@ int git_parse_maybe_bool(const char *value)
return -1;
}
-int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
+int git_config_bool_or_int(const char *name, const char *value,
+ const struct key_value_info *kvi, int *is_bool)
{
int v = git_parse_maybe_bool_text(value);
if (0 <= v) {
@@ -1511,7 +1475,7 @@ int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
return v;
}
*is_bool = 0;
- return git_config_int(name, value);
+ return git_config_int(name, value, kvi);
}
int git_config_bool(const char *name, const char *value)
@@ -1559,7 +1523,8 @@ int git_config_color(char *dest, const char *var, const char *value)
return 0;
}
-static int git_default_core_config(const char *var, const char *value, void *cb)
+static int git_default_core_config(const char *var, const char *value,
+ const struct config_context *ctx, void *cb)
{
/* This needs a better name */
if (!strcmp(var, "core.filemode")) {
@@ -1636,7 +1601,7 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
else if (!git_parse_maybe_bool_text(value))
default_abbrev = the_hash_algo->hexsz;
else {
- int abbrev = git_config_int(var, value);
+ int abbrev = git_config_int(var, value, ctx->kvi);
if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz)
return error(_("abbrev length out of range: %d"), abbrev);
default_abbrev = abbrev;
@@ -1648,7 +1613,7 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
return set_disambiguate_hint_config(var, value);
if (!strcmp(var, "core.loosecompression")) {
- int level = git_config_int(var, value);
+ int level = git_config_int(var, value, ctx->kvi);
if (level == -1)
level = Z_DEFAULT_COMPRESSION;
else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -1659,7 +1624,7 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
}
if (!strcmp(var, "core.compression")) {
- int level = git_config_int(var, value);
+ int level = git_config_int(var, value, ctx->kvi);
if (level == -1)
level = Z_DEFAULT_COMPRESSION;
else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -1673,7 +1638,7 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
if (!strcmp(var, "core.packedgitwindowsize")) {
int pgsz_x2 = getpagesize() * 2;
- packed_git_window_size = git_config_ulong(var, value);
+ packed_git_window_size = git_config_ulong(var, value, ctx->kvi);
/* This value must be multiple of (pagesize * 2) */
packed_git_window_size /= pgsz_x2;
@@ -1684,17 +1649,17 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
}
if (!strcmp(var, "core.bigfilethreshold")) {
- big_file_threshold = git_config_ulong(var, value);
+ big_file_threshold = git_config_ulong(var, value, ctx->kvi);
return 0;
}
if (!strcmp(var, "core.packedgitlimit")) {
- packed_git_limit = git_config_ulong(var, value);
+ packed_git_limit = git_config_ulong(var, value, ctx->kvi);
return 0;
}
if (!strcmp(var, "core.deltabasecachelimit")) {
- delta_base_cache_limit = git_config_ulong(var, value);
+ delta_base_cache_limit = git_config_ulong(var, value, ctx->kvi);
return 0;
}
@@ -1839,7 +1804,7 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
}
/* Add other config variables here and to Documentation/config.txt. */
- return platform_core_config(var, value, cb);
+ return platform_core_config(var, value, ctx, cb);
}
static int git_default_sparse_config(const char *var, const char *value)
@@ -1941,15 +1906,16 @@ static int git_default_mailmap_config(const char *var, const char *value)
return 0;
}
-int git_default_config(const char *var, const char *value, void *cb)
+int git_default_config(const char *var, const char *value,
+ const struct config_context *ctx, void *cb)
{
if (starts_with(var, "core."))
- return git_default_core_config(var, value, cb);
+ return git_default_core_config(var, value, ctx, cb);
if (starts_with(var, "user.") ||
starts_with(var, "author.") ||
starts_with(var, "committer."))
- return git_ident_config(var, value, cb);
+ return git_ident_config(var, value, ctx, cb);
if (starts_with(var, "i18n."))
return git_default_i18n_config(var, value);
@@ -1972,12 +1938,12 @@ int git_default_config(const char *var, const char *value, void *cb)
}
if (!strcmp(var, "pack.packsizelimit")) {
- pack_size_limit_cfg = git_config_ulong(var, value);
+ pack_size_limit_cfg = git_config_ulong(var, value, ctx->kvi);
return 0;
}
if (!strcmp(var, "pack.compression")) {
- int level = git_config_int(var, value);
+ int level = git_config_int(var, value, ctx->kvi);
if (level == -1)
level = Z_DEFAULT_COMPRESSION;
else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -1999,10 +1965,11 @@ int git_default_config(const char *var, const char *value, void *cb)
* fgetc, ungetc, ftell of top need to be initialized before calling
* this function.
*/
-static int do_config_from(struct config_reader *reader,
- struct config_source *top, config_fn_t fn, void *data,
+static int do_config_from(struct config_source *top, config_fn_t fn,
+ void *data, enum config_scope scope,
const struct config_options *opts)
{
+ struct key_value_info kvi = KVI_INIT;
int ret;
/* push config-file parsing state stack */
@@ -2011,23 +1978,21 @@ static int do_config_from(struct config_reader *reader,
top->total_len = 0;
strbuf_init(&top->value, 1024);
strbuf_init(&top->var, 1024);
- config_reader_push_source(reader, top);
+ kvi_from_source(top, scope, &kvi);
- ret = git_parse_source(top, fn, data, opts);
+ ret = git_parse_source(top, fn, &kvi, data, opts);
- /* pop config-file parsing state stack */
strbuf_release(&top->value);
strbuf_release(&top->var);
- config_reader_pop_source(reader);
return ret;
}
-static int do_config_from_file(struct config_reader *reader,
- config_fn_t fn,
+static int do_config_from_file(config_fn_t fn,
const enum config_origin_type origin_type,
const char *name, const char *path, FILE *f,
- void *data, const struct config_options *opts)
+ void *data, enum config_scope scope,
+ const struct config_options *opts)
{
struct config_source top = CONFIG_SOURCE_INIT;
int ret;
@@ -2042,19 +2007,20 @@ static int do_config_from_file(struct config_reader *reader,
top.do_ftell = config_file_ftell;
flockfile(f);
- ret = do_config_from(reader, &top, fn, data, opts);
+ ret = do_config_from(&top, fn, data, scope, opts);
funlockfile(f);
return ret;
}
-static int git_config_from_stdin(config_fn_t fn, void *data)
+static int git_config_from_stdin(config_fn_t fn, void *data,
+ enum config_scope scope)
{
- return do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_STDIN, "",
- NULL, stdin, data, NULL);
+ return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin,
+ data, scope, NULL);
}
int git_config_from_file_with_options(config_fn_t fn, const char *filename,
- void *data,
+ void *data, enum config_scope scope,
const struct config_options *opts)
{
int ret = -1;
@@ -2064,8 +2030,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
BUG("filename cannot be NULL");
f = fopen_or_warn(filename, "r");
if (f) {
- ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
- filename, filename, f, data, opts);
+ ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
+ filename, f, data, scope, opts);
fclose(f);
}
return ret;
@@ -2073,13 +2039,15 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
int git_config_from_file(config_fn_t fn, const char *filename, void *data)
{
- return git_config_from_file_with_options(fn, filename, data, NULL);
+ return git_config_from_file_with_options(fn, filename, data,
+ CONFIG_SCOPE_UNKNOWN, NULL);
}
int git_config_from_mem(config_fn_t fn,
const enum config_origin_type origin_type,
const char *name, const char *buf, size_t len,
- void *data, const struct config_options *opts)
+ void *data, enum config_scope scope,
+ const struct config_options *opts)
{
struct config_source top = CONFIG_SOURCE_INIT;
@@ -2094,14 +2062,15 @@ int git_config_from_mem(config_fn_t fn,
top.do_ungetc = config_buf_ungetc;
top.do_ftell = config_buf_ftell;
- return do_config_from(&the_reader, &top, fn, data, opts);
+ return do_config_from(&top, fn, data, scope, opts);
}
int git_config_from_blob_oid(config_fn_t fn,
const char *name,
struct repository *repo,
const struct object_id *oid,
- void *data)
+ void *data,
+ enum config_scope scope)
{
enum object_type type;
char *buf;
@@ -2117,7 +2086,7 @@ int git_config_from_blob_oid(config_fn_t fn,
}
ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size,
- data, NULL);
+ data, scope, NULL);
free(buf);
return ret;
@@ -2126,13 +2095,14 @@ int git_config_from_blob_oid(config_fn_t fn,
static int git_config_from_blob_ref(config_fn_t fn,
struct repository *repo,
const char *name,
- void *data)
+ void *data,
+ enum config_scope scope)
{
struct object_id oid;
if (repo_get_oid(repo, name, &oid) < 0)
return error(_("unable to resolve config blob '%s'"), name);
- return git_config_from_blob_oid(fn, name, repo, &oid, data);
+ return git_config_from_blob_oid(fn, name, repo, &oid, data, scope);
}
char *git_system_config(void)
@@ -2185,8 +2155,7 @@ int git_config_system(void)
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
}
-static int do_git_config_sequence(struct config_reader *reader,
- const struct config_options *opts,
+static int do_git_config_sequence(const struct config_options *opts,
const struct repository *repo,
config_fn_t fn, void *data)
{
@@ -2196,7 +2165,6 @@ static int do_git_config_sequence(struct config_reader *reader,
char *user_config = NULL;
char *repo_config;
char *worktree_config;
- enum config_scope prev_parsing_scope = reader->parsing_scope;
/*
* Ensure that either:
@@ -2214,38 +2182,39 @@ static int do_git_config_sequence(struct config_reader *reader,
worktree_config = NULL;
}
- config_reader_set_scope(reader, CONFIG_SCOPE_SYSTEM);
if (git_config_system() && system_config &&
!access_or_die(system_config, R_OK,
opts->system_gently ? ACCESS_EACCES_OK : 0))
- ret += git_config_from_file(fn, system_config, data);
+ ret += git_config_from_file_with_options(fn, system_config,
+ data, CONFIG_SCOPE_SYSTEM,
+ NULL);
- config_reader_set_scope(reader, CONFIG_SCOPE_GLOBAL);
git_global_config(&user_config, &xdg_config);
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
- ret += git_config_from_file(fn, xdg_config, data);
+ ret += git_config_from_file_with_options(fn, xdg_config, data,
+ CONFIG_SCOPE_GLOBAL, NULL);
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
- ret += git_config_from_file(fn, user_config, data);
+ ret += git_config_from_file_with_options(fn, user_config, data,
+ CONFIG_SCOPE_GLOBAL, NULL);
- config_reader_set_scope(reader, CONFIG_SCOPE_LOCAL);
if (!opts->ignore_repo && repo_config &&
!access_or_die(repo_config, R_OK, 0))
- ret += git_config_from_file(fn, repo_config, data);
+ ret += git_config_from_file_with_options(fn, repo_config, data,
+ CONFIG_SCOPE_LOCAL, NULL);
- config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
if (!opts->ignore_worktree && worktree_config &&
repo && repo->repository_format_worktree_config &&
!access_or_die(worktree_config, R_OK, 0)) {
- ret += git_config_from_file(fn, worktree_config, data);
+ ret += git_config_from_file_with_options(fn, worktree_config, data,
+ CONFIG_SCOPE_WORKTREE,
+ NULL);
}
- config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
die(_("unable to parse command-line config"));
- config_reader_set_scope(reader, prev_parsing_scope);
free(system_config);
free(xdg_config);
free(user_config);
@@ -2260,7 +2229,6 @@ int config_with_options(config_fn_t fn, void *data,
const struct config_options *opts)
{
struct config_include_data inc = CONFIG_INCLUDE_INIT;
- enum config_scope prev_scope = the_reader.parsing_scope;
int ret;
if (opts->respect_includes) {
@@ -2269,58 +2237,52 @@ int config_with_options(config_fn_t fn, void *data,
inc.opts = opts;
inc.repo = repo;
inc.config_source = config_source;
- inc.config_reader = &the_reader;
fn = git_config_include;
data = &inc;
}
- if (config_source)
- config_reader_set_scope(&the_reader, config_source->scope);
-
/*
* If we have a specific filename, use it. Otherwise, follow the
* regular lookup sequence.
*/
if (config_source && config_source->use_stdin) {
- ret = git_config_from_stdin(fn, data);
+ ret = git_config_from_stdin(fn, data, config_source->scope);
} else if (config_source && config_source->file) {
- ret = git_config_from_file(fn, config_source->file, data);
+ ret = git_config_from_file_with_options(fn, config_source->file,
+ data, config_source->scope,
+ NULL);
} else if (config_source && config_source->blob) {
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
- data);
+ data, config_source->scope);
} else {
- ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
+ ret = do_git_config_sequence(opts, repo, fn, data);
}
if (inc.remote_urls) {
string_list_clear(inc.remote_urls, 0);
FREE_AND_NULL(inc.remote_urls);
}
- config_reader_set_scope(&the_reader, prev_scope);
return ret;
}
-static void configset_iter(struct config_reader *reader, struct config_set *set,
- config_fn_t fn, void *data)
+static void configset_iter(struct config_set *set, config_fn_t fn, void *data)
{
int i, value_index;
struct string_list *values;
struct config_set_element *entry;
struct configset_list *list = &set->list;
+ struct config_context ctx = CONFIG_CONTEXT_INIT;
for (i = 0; i < list->nr; i++) {
entry = list->items[i].e;
value_index = list->items[i].value_index;
values = &entry->value_list;
- config_reader_set_kvi(reader, values->items[value_index].util);
-
- if (fn(entry->key, values->items[value_index].string, data) < 0)
+ ctx.kvi = values->items[value_index].util;
+ if (fn(entry->key, values->items[value_index].string, &ctx, data) < 0)
git_die_config_linenr(entry->key,
- reader->config_kvi->filename,
- reader->config_kvi->linenr);
-
- config_reader_set_kvi(reader, NULL);
+ ctx.kvi->filename,
+ ctx.kvi->linenr);
}
}
@@ -2396,7 +2358,7 @@ static int configset_find_element(struct config_set *set, const char *key,
return 0;
}
-static int configset_add_value(struct config_reader *reader,
+static int configset_add_value(const struct key_value_info *kvi_p,
struct config_set *set, const char *key,
const char *value)
{
@@ -2427,19 +2389,7 @@ static int configset_add_value(struct config_reader *reader,
l_item->e = e;
l_item->value_index = e->value_list.nr - 1;
- if (!reader->source)
- BUG("configset_add_value has no source");
- if (reader->source->name) {
- kv_info->filename = strintern(reader->source->name);
- kv_info->linenr = reader->source->linenr;
- kv_info->origin_type = reader->source->origin_type;
- } else {
- /* for values read from `git_config_from_parameters()` */
- kv_info->filename = NULL;
- kv_info->linenr = -1;
- kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
- }
- kv_info->scope = reader->parsing_scope;
+ *kv_info = *kvi_p;
si->util = kv_info;
return 0;
@@ -2487,32 +2437,26 @@ void git_configset_clear(struct config_set *set)
set->list.items = NULL;
}
-struct configset_add_data {
- struct config_set *config_set;
- struct config_reader *config_reader;
-};
-#define CONFIGSET_ADD_INIT { 0 }
-
-static int config_set_callback(const char *key, const char *value, void *cb)
+static int config_set_callback(const char *key, const char *value,
+ const struct config_context *ctx,
+ void *cb)
{
- struct configset_add_data *data = cb;
- configset_add_value(data->config_reader, data->config_set, key, value);
+ struct config_set *set = cb;
+ configset_add_value(ctx->kvi, set, key, value);
return 0;
}
int git_configset_add_file(struct config_set *set, const char *filename)
{
- struct configset_add_data data = CONFIGSET_ADD_INIT;
- data.config_reader = &the_reader;
- data.config_set = set;
- return git_config_from_file(config_set_callback, filename, &data);
+ return git_config_from_file(config_set_callback, filename, set);
}
-int git_configset_get_value(struct config_set *set, const char *key, const char **value)
+int git_configset_get_value(struct config_set *set, const char *key,
+ const char **value, struct key_value_info *kvi)
{
const struct string_list *values = NULL;
int ret;
-
+ struct string_list_item item;
/*
* Follows "last one wins" semantic, i.e., if there are multiple matches for the
* queried key in the files of the configset, the value returned will be the last
@@ -2522,7 +2466,10 @@ int git_configset_get_value(struct config_set *set, const char *key, const char
return ret;
assert(values->nr > 0);
- *value = values->items[values->nr - 1].string;
+ item = values->items[values->nr - 1];
+ *value = item.string;
+ if (kvi)
+ *kvi = *((struct key_value_info *)item.util);
return 0;
}
@@ -2575,7 +2522,7 @@ int git_configset_get(struct config_set *set, const char *key)
int git_configset_get_string(struct config_set *set, const char *key, char **dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value))
+ if (!git_configset_get_value(set, key, &value, NULL))
return git_config_string((const char **)dest, key, value);
else
return 1;
@@ -2585,7 +2532,7 @@ static int git_configset_get_string_tmp(struct config_set *set, const char *key,
const char **dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value)) {
+ if (!git_configset_get_value(set, key, &value, NULL)) {
if (!value)
return config_error_nonbool(key);
*dest = value;
@@ -2598,8 +2545,10 @@ static int git_configset_get_string_tmp(struct config_set *set, const char *key,
int git_configset_get_int(struct config_set *set, const char *key, int *dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value)) {
- *dest = git_config_int(key, value);
+ struct key_value_info kvi;
+
+ if (!git_configset_get_value(set, key, &value, &kvi)) {
+ *dest = git_config_int(key, value, &kvi);
return 0;
} else
return 1;
@@ -2608,8 +2557,10 @@ int git_configset_get_int(struct config_set *set, const char *key, int *dest)
int git_configset_get_ulong(struct config_set *set, const char *key, unsigned long *dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value)) {
- *dest = git_config_ulong(key, value);
+ struct key_value_info kvi;
+
+ if (!git_configset_get_value(set, key, &value, &kvi)) {
+ *dest = git_config_ulong(key, value, &kvi);
return 0;
} else
return 1;
@@ -2618,7 +2569,7 @@ int git_configset_get_ulong(struct config_set *set, const char *key, unsigned lo
int git_configset_get_bool(struct config_set *set, const char *key, int *dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value)) {
+ if (!git_configset_get_value(set, key, &value, NULL)) {
*dest = git_config_bool(key, value);
return 0;
} else
@@ -2629,8 +2580,10 @@ int git_configset_get_bool_or_int(struct config_set *set, const char *key,
int *is_bool, int *dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value)) {
- *dest = git_config_bool_or_int(key, value, is_bool);
+ struct key_value_info kvi;
+
+ if (!git_configset_get_value(set, key, &value, &kvi)) {
+ *dest = git_config_bool_or_int(key, value, &kvi, is_bool);
return 0;
} else
return 1;
@@ -2639,7 +2592,7 @@ int git_configset_get_bool_or_int(struct config_set *set, const char *key,
int git_configset_get_maybe_bool(struct config_set *set, const char *key, int *dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value)) {
+ if (!git_configset_get_value(set, key, &value, NULL)) {
*dest = git_parse_maybe_bool(value);
if (*dest == -1)
return -1;
@@ -2651,7 +2604,7 @@ int git_configset_get_maybe_bool(struct config_set *set, const char *key, int *d
int git_configset_get_pathname(struct config_set *set, const char *key, const char **dest)
{
const char *value;
- if (!git_configset_get_value(set, key, &value))
+ if (!git_configset_get_value(set, key, &value, NULL))
return git_config_pathname(dest, key, value);
else
return 1;
@@ -2661,7 +2614,6 @@ int git_configset_get_pathname(struct config_set *set, const char *key, const ch
static void repo_read_config(struct repository *repo)
{
struct config_options opts = { 0 };
- struct configset_add_data data = CONFIGSET_ADD_INIT;
opts.respect_includes = 1;
opts.commondir = repo->commondir;
@@ -2673,10 +2625,8 @@ static void repo_read_config(struct repository *repo)
git_configset_clear(repo->config);
git_configset_init(repo->config);
- data.config_set = repo->config;
- data.config_reader = &the_reader;
-
- if (config_with_options(config_set_callback, &data, NULL, repo, &opts) < 0)
+ if (config_with_options(config_set_callback, repo->config, NULL,
+ repo, &opts) < 0)
/*
* config_with_options() normally returns only
* zero, as most errors are fatal, and
@@ -2708,7 +2658,7 @@ static void repo_config_clear(struct repository *repo)
void repo_config(struct repository *repo, config_fn_t fn, void *data)
{
git_config_check_init(repo);
- configset_iter(&the_reader, repo->config, fn, data);
+ configset_iter(repo->config, fn, data);
}
int repo_config_get(struct repository *repo, const char *key)
@@ -2721,7 +2671,7 @@ int repo_config_get_value(struct repository *repo,
const char *key, const char **value)
{
git_config_check_init(repo);
- return git_configset_get_value(repo->config, key, value);
+ return git_configset_get_value(repo->config, key, value, NULL);
}
int repo_config_get_value_multi(struct repository *repo, const char *key,
@@ -2815,19 +2765,17 @@ static void read_protected_config(void)
.ignore_worktree = 1,
.system_gently = 1,
};
- struct configset_add_data data = CONFIGSET_ADD_INIT;
git_configset_init(&protected_config);
- data.config_set = &protected_config;
- data.config_reader = &the_reader;
- config_with_options(config_set_callback, &data, NULL, NULL, &opts);
+ config_with_options(config_set_callback, &protected_config, NULL,
+ NULL, &opts);
}
void git_protected_config(config_fn_t fn, void *data)
{
if (!protected_config.hash_initialized)
read_protected_config();
- configset_iter(&the_reader, &protected_config, fn, data);
+ configset_iter(&protected_config, fn, data);
}
/* Functions used historically to read configuration from 'the_repository' */
@@ -3017,7 +2965,6 @@ void git_die_config(const char *key, const char *err, ...)
*/
struct config_store_data {
- struct config_reader *config_reader;
size_t baselen;
char *key;
int do_not_match;
@@ -3063,11 +3010,10 @@ static int matches(const char *key, const char *value,
(value && !regexec(store->value_pattern, value, 0, NULL, 0));
}
-static int store_aux_event(enum config_event_t type,
- size_t begin, size_t end, void *data)
+static int store_aux_event(enum config_event_t type, size_t begin, size_t end,
+ struct config_source *cs, void *data)
{
struct config_store_data *store = data;
- struct config_source *cs = store->config_reader->source;
ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
store->parsed[store->parsed_nr].begin = begin;
@@ -3103,7 +3049,8 @@ static int store_aux_event(enum config_event_t type,
return 0;
}
-static int store_aux(const char *key, const char *value, void *cb)
+static int store_aux(const char *key, const char *value,
+ const struct config_context *ctx UNUSED, void *cb)
{
struct config_store_data *store = cb;
@@ -3387,8 +3334,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
size_t contents_sz;
struct config_store_data store = CONFIG_STORE_INIT;
- store.config_reader = &the_reader;
-
/* parse-key returns negative; flip the sign to feed exit(3) */
ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
if (ret)
@@ -3477,7 +3422,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
*/
if (git_config_from_file_with_options(store_aux,
config_filename,
- &store, &opts)) {
+ &store, CONFIG_SCOPE_UNKNOWN,
+ &opts)) {
error(_("invalid config file %s"), config_filename);
ret = CONFIG_INVALID_FILE;
goto out_free;
@@ -3959,25 +3905,8 @@ int parse_config_key(const char *var,
return 0;
}
-static int reader_origin_type(struct config_reader *reader,
- enum config_origin_type *type)
-{
- if (the_reader.config_kvi)
- *type = reader->config_kvi->origin_type;
- else if(the_reader.source)
- *type = reader->source->origin_type;
- else
- return 1;
- return 0;
-}
-
-const char *current_config_origin_type(void)
+const char *config_origin_type_name(enum config_origin_type type)
{
- enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
-
- if (reader_origin_type(&the_reader, &type))
- BUG("current_config_origin_type called outside config callback");
-
switch (type) {
case CONFIG_ORIGIN_BLOB:
return "blob";
@@ -4014,41 +3943,6 @@ const char *config_scope_name(enum config_scope scope)
}
}
-static int reader_config_name(struct config_reader *reader, const char **out)
-{
- if (the_reader.config_kvi)
- *out = reader->config_kvi->filename;
- else if (the_reader.source)
- *out = reader->source->name;
- else
- return 1;
- return 0;
-}
-
-const char *current_config_name(void)
-{
- const char *name;
- if (reader_config_name(&the_reader, &name))
- BUG("current_config_name called outside config callback");
- return name ? name : "";
-}
-
-enum config_scope current_config_scope(void)
-{
- if (the_reader.config_kvi)
- return the_reader.config_kvi->scope;
- else
- return the_reader.parsing_scope;
-}
-
-int current_config_line(void)
-{
- if (the_reader.config_kvi)
- return the_reader.config_kvi->linenr;
- else
- return the_reader.source->linenr;
-}
-
int lookup_config(const char **mapping, int nr_mapping, const char *var)
{
int i;