Git Mailing List Archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/14] [RFC] config: remove global state from config iteration
@ 2023-04-21 19:13 Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 01/14] config.c: introduce kvi_fn(), use it for configsets Glen Choo via GitGitGadget
                   ` (14 more replies)
  0 siblings, 15 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

= Description

This series removes all global state from config iteration, i.e. parsing
config and iterating configsets, by passing config metadata as a "struct
key_value_info" arg to the "config_fn_t" callbacks. This allows us to get
rid of:

 * "the_reader" (formerly "cf", "current_parsing_scope",
   "current_config_kvi"), and the config.c machinery that maintained it.
   This only needed to be global because it was read by...
 * The "current_*" functions that read metadata about the 'current' config
   value ("current_config_scope", "current_config_name", etc), which are
   replaced by reading values from the "struct key_value_info" passed to the
   callbacks.

As a result:

 * Config iterating code can be moved to into its own library with few
   modifications. C.f. libification efforts [1].
 * The config iterating code can be safely called in parallel.
 * We expose and fix instances where the "current_*" functions were being
   called outside of config callbacks.

We've had this idea of doing this "config_fn_t" refactor for a long time
[2], but we've never attempted it because we wanted to avoid churn. After
attempting it, though, I'm quite convinced that this is the right way
forward for config, since the lack of global state makes things much easier
to reason about. The churn is also quite manageable:

 * The vast majority of changes can be handled with cocci.
 * The few cases that aren't covered by cocci have obvious fixes.
 * The change is simple enough for in-flight topics to perform and conflicts
   will be caught at build time anyway.
 * Anecdotally, we don't change config functions often anyway. This merges
   cleanly with 'seen' and the result passes CI.

= Patch overview

I've arranged the patches in a way that makes them readable, but is
unsuitable for merging, hence the RFC tag. The "Post-RFC cleanups" section
describes all the changes I'll make prior to submitting this as non-RFC.

1-5/14 converts the config.c machinery to set and make use of
"config_reader.config_kvi" instead of "config_reader.source" and
"config_reader.scope", which makes it easier to pass "struct key_value_info"
to config callbacks. To minimize churn, I opted to 'de-plumb' "struct
config_reader" here instead of plumbing it and removing it later.

6-10/14 actually changes "config_fn_t"'s signature. The earlier patches are
minor refactors that make the cocci application + adjustment easier. Post
RFC, cocci application + adjustment will be a single patch, but for ease of
reading I've kept them separate.

11-14/14 teach the config callbacks to use the "kvi" parameter, replace the
now-obsolete "current_*" functions, and remove the now-obsolete "struct
config_reader". This requires plumbing "kvi" through some more config.c
machinery and fixing a sneaky config.c-like code path in trace2/.

= Post-RFC cleanups

 * To make future refactors easier, I'm strongly considering replacing the
   "struct key_value_info" arg with something more extensible so that we can
   modify that without changing 100+ function signatures. An alternative
   would be to replace all of the args to config_fn_t with a single struct,
   but that would very significantly increase churn because we'd need to
   touch all the config_fn_t function bodies too.

 * Rearrange the cocci patches to avoid breaking CI in the middle of the
   series.

 * Fix style issues (e.g. spacing around "kvi", line wrapping, "{ 0 }"
   instead of dedicated initializer)

= Alternatives considered

Ævar suggested in [3] that we might be able to do the refactor incrementally
by having both the old "config_fn_t" and the new "config_fn_t with kvi",
which lets us convert some of config iterating (e.g. configsets) without
touching the others (e.g. config parsing). I experimented with that for a
bit, and it turned out that doing it all at once is actually less work
because we don't have to worry about the case where the same "config_fn_t"
is used in both git_config() and git_config_from_file().

Jonathan Tan suggested in [4] that to reduce churn, we might be able to
convert many of the config_fn_t-s to the config set API before attempting
this refactor. But (hopefully), these patches show that the churn is
manageable even without this preparatory step.

[1]
https://lore.kernel.org/git/CAJoAoZ=Cig_kLocxKGax31sU7Xe4==BGzC__Bg2_pr7krNq6MA@mail.gmail.com/
[2]
https://lore.kernel.org/git/CAPc5daV6bdUKS-ExHmpT4Ppy2S832NXoyPw7aOLP7fG=WrBPgg@mail.gmail.com/
[3]
https://lore.kernel.org/git/RFC-patch-5.5-2b80d293c83-20230317T042408Z-avarab@gmail.com
[4]
https://lore.kernel.org/git/20230306195756.3399115-1-jonathantanmy@google.com/

Glen Choo (14):
  config.c: introduce kvi_fn(), use it for configsets
  config.c: use kvi for CLI config
  config: use kvi for config files
  config: add kvi.path, use it to evaluate includes
  config: pass source to config_parser_event_fn_t
  config: inline git_color_default_config
  urlmatch.h: use config_fn_t type
  (RFC-only) config: add kvi arg to config_fn_t
  (RFC-only) config: apply cocci to config_fn_t implementations
  (RFC-only) config: finish config_fn_t refactor
  config: remove current_config_(line|name|origin_type)
  config: remove current_config_scope()
  config: pass kvi to die_bad_number()
  config: remove config_reader from configset_add_value

 alias.c                                       |   3 +-
 archive-tar.c                                 |   5 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   5 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   8 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |   7 +-
 builtin/clean.c                               |   9 +-
 builtin/clone.c                               |  10 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   3 +-
 builtin/commit.c                              |  20 +-
 builtin/config.c                              |  59 +-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |  12 +-
 builtin/fsmonitor--daemon.c                   |  11 +-
 builtin/grep.c                                |  12 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   9 +-
 builtin/log.c                                 |  12 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |  19 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |  15 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |  12 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   8 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   3 +-
 builtin/tag.c                                 |   9 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   9 +-
 color.c                                       |   8 -
 color.h                                       |   6 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      | 540 +++++++-----------
 config.h                                      |  56 +-
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_kvi.pending.cocci    | 146 +++++
 contrib/coccinelle/git_config_number.cocci    |  27 +
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   3 +-
 diff.c                                        |  19 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   7 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |  11 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   6 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |  10 +-
 http.c                                        |  15 +-
 ident.c                                       |   3 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   7 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   2 +-
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   3 +-
 notes.c                                       |   3 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   7 +-
 revision.c                                    |   3 +-
 scalar.c                                      |   3 +-
 sequencer.c                                   |  28 +-
 setup.c                                       |  17 +-
 submodule-config.c                            |  31 +-
 submodule-config.h                            |   3 +-
 t/helper/test-config.c                        |  21 +-
 t/helper/test-userdiff.c                      |   4 +-
 trace2.c                                      |   4 +-
 trace2.h                                      |   3 +-
 trace2/tr2_cfg.c                              |  12 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trace2/tr2_tgt.h                              |   4 +-
 trace2/tr2_tgt_event.c                        |   4 +-
 trace2/tr2_tgt_normal.c                       |   4 +-
 trace2/tr2_tgt_perf.c                         |   4 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |  18 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   8 +-
 worktree.c                                    |   2 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   5 +-
 102 files changed, 855 insertions(+), 641 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_kvi.pending.cocci
 create mode 100644 contrib/coccinelle/git_config_number.cocci


base-commit: 9857273be005833c71e2d16ba48e193113e12276
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1497%2Fchooglen%2Fconfig%2Fno-global-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1497/chooglen/config/no-global-v1
Pull-Request: https://github.com/git/git/pull/1497
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH 01/14] config.c: introduce kvi_fn(), use it for configsets
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 02/14] config.c: use kvi for CLI config Glen Choo via GitGitGadget
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

In order to pass "struct key_value_info" to config callbacks in a future
commit, we want to make sure that a "struct key_value_info" is available
in all code paths that invoke a config callback.

Enforce this by introducing a helper function, "kvi_fn()", that sets
"the_reader.config_kvi" before invoking the config callback, and
trivially use it in "configset_iter()". This closely simulates passing
an extra "struct key_value_info" such that making the switch will be
trivial.

This breaks our rule that no underlying machinery uses "the_reader", but
"struct config_reader" will be gone by the end of the series so there's
no point plumbing it as an arg.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/config.c b/config.c
index 493f47df8ae..daad892e4fd 100644
--- a/config.c
+++ b/config.c
@@ -660,6 +660,17 @@ out_free_ret_1:
 	return -CONFIG_INVALID_KEY;
 }
 
+static int kvi_fn(config_fn_t fn, const char *key, const char *value,
+		  struct key_value_info *kvi,
+		  void *data)
+{
+	int ret;
+	config_reader_set_kvi(&the_reader, kvi);
+	ret = fn(key, value, data);
+	config_reader_set_kvi(&the_reader, NULL);
+	return ret;
+}
+
 static int config_parse_pair(const char *key, const char *value,
 			  config_fn_t fn, void *data)
 {
@@ -2288,27 +2299,24 @@ int config_with_options(config_fn_t fn, void *data,
 	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 key_value_info *kvi;
 
 	for (i = 0; i < list->nr; i++) {
 		entry = list->items[i].e;
 		value_index = list->items[i].value_index;
 		values = &entry->value_list;
+		kvi = values->items[value_index].util;
 
-		config_reader_set_kvi(reader, values->items[value_index].util);
-
-		if (fn(entry->key, values->items[value_index].string, data) < 0)
-			git_die_config_linenr(entry->key,
-					      reader->config_kvi->filename,
-					      reader->config_kvi->linenr);
-
-		config_reader_set_kvi(reader, NULL);
+		if (kvi_fn(fn, entry->key, values->items[value_index].string,
+			   kvi, data) < 0)
+			git_die_config_linenr(entry->key, kvi->filename,
+					      kvi->linenr);
 	}
 }
 
@@ -2696,7 +2704,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)
@@ -2815,7 +2823,7 @@ 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' */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 02/14] config.c: use kvi for CLI config
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 01/14] config.c: introduce kvi_fn(), use it for configsets Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-05-01 11:06   ` Ævar Arnfjörð Bjarmason
  2023-04-21 19:13 ` [PATCH 03/14] config: use kvi for config files Glen Choo via GitGitGadget
                   ` (12 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Plumb "struct key_value_info" and use "kvi_fn()" when parsing CLI
config. Do this by refactoring out and reusing the logic that sets the
"struct key_value_info" members when caching CLI config in a configset.

This lets us get rid of the fake "struct config_source" in
"git_config_from_parameters()", so we now only have to maintain one
implementation. Additionally, this plumbing also reveals that
"git_config_parse_parameter()" hasn't been setting either
"the_reader.source" or "the_reader.config_kvi", so any calls to
"current_*" would either BUG() or return *_UNKNOWN values.

Also, get rid of the BUG() checks that forbid setting ".config_kvi" and
".source" at the same time, since we will run afoul of that check. They
will soon be unnecessary when we remove ".source".

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 58 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 29 deletions(-)

diff --git a/config.c b/config.c
index daad892e4fd..4e9e8e7abb9 100644
--- a/config.c
+++ b/config.c
@@ -99,8 +99,6 @@ static struct config_reader the_reader;
 static inline void config_reader_push_source(struct config_reader *reader,
 					     struct config_source *top)
 {
-	if (reader->config_kvi)
-		BUG("source should not be set while iterating a config set");
 	top->prev = reader->source;
 	reader->source = top;
 }
@@ -118,16 +116,12 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 static inline void config_reader_set_kvi(struct config_reader *reader,
 					 struct key_value_info *kvi)
 {
-	if (kvi && (reader->source || reader->parsing_scope))
-		BUG("kvi should not be set while parsing a config source");
 	reader->config_kvi = kvi;
 }
 
 static inline void config_reader_set_scope(struct config_reader *reader,
 					   enum config_scope scope)
 {
-	if (scope && reader->config_kvi)
-		BUG("scope should only be set when iterating through a config source");
 	reader->parsing_scope = scope;
 }
 
@@ -672,7 +666,8 @@ static int kvi_fn(config_fn_t fn, const char *key, const char *value,
 }
 
 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;
@@ -682,17 +677,30 @@ static int config_parse_pair(const char *key, const char *value,
 	if (git_config_parse_key(key, &canonical_name, NULL))
 		return -1;
 
-	ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
+	ret = (kvi_fn(fn, canonical_name, value, kvi, data) < 0) ? -1 : 0;
 	free(canonical_name);
 	return ret;
 }
 
+
+/* for values read from `git_config_from_parameters()` */
+static 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;
+}
+
 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 = { 0 };
+
+	kvi_from_param(&kvi);
 
 	pair = strbuf_split_str(text, '=', 2);
 	if (!pair[0])
@@ -711,12 +719,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) {
@@ -750,7 +759,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 {
@@ -774,11 +783,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 = { 0 };
 
+	kvi_from_param(&kvi);
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -814,7 +821,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;
 			}
@@ -825,7 +832,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;
 		}
@@ -835,7 +842,6 @@ out:
 	strbuf_release(&envvar);
 	strvec_clear(&to_free);
 	free(envw);
-	config_reader_pop_source(&the_reader);
 	return ret;
 }
 
@@ -2241,7 +2247,7 @@ static int do_git_config_sequence(struct config_reader *reader,
 		free(path);
 	}
 
-	config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
+	config_reader_set_scope(reader, 0);
 	if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
 		die(_("unable to parse command-line config"));
 
@@ -2423,19 +2429,13 @@ 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) {
+	if (reader->source && 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->scope = reader->parsing_scope;
+	} else
+		kvi_from_param(kv_info);
 	si->util = kv_info;
 
 	return 0;
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 03/14] config: use kvi for config files
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 01/14] config.c: introduce kvi_fn(), use it for configsets Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 02/14] config.c: use kvi for CLI config Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 04/14] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Plumb "struct key_value_info" and use "kvi_fn()" when parsing config
files. As a result, "config_reader.kvi" is now always set correctly, so
we can remove "config_reader.scope" (but not the ".source" member since
that's still needed by some non-parsing machinery). This requires
plumbing an additional "enum config_scope" arg through
"git_config_from_file_with_options()" and the underlying machinery to
make up for the fact that "struct key_value_info" has a ".scope" member,
but "struct config_source" does not.

To handle "include" directives correctly, use push/pop semantics for
"config_reader.config_kvi" (instead of "set" semantics) like we do for
"config_reader.source". Otherwise, "config_reader.config_kvi" won't be
set correctly when we finish parsing an included config file and we want
to "pop" it to resume parsing the original file. This distinction only
matters while there is a global "kvi", i.e. it will be obsolete at the
end of the series.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 bundle-uri.c       |   1 +
 config.c           | 171 ++++++++++++++++++++++++---------------------
 config.h           |   9 ++-
 fsck.c             |   3 +-
 submodule-config.c |   5 +-
 5 files changed, 104 insertions(+), 85 deletions(-)

diff --git a/bundle-uri.c b/bundle-uri.c
index e2b267cc02b..8c4e2b70b89 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -252,6 +252,7 @@ int bundle_uri_parse_config_format(const char *uri,
 	}
 	result = git_config_from_file_with_options(config_to_bundle_list,
 						   filename, list,
+						   CONFIG_SCOPE_UNKNOWN,
 						   &opts);
 
 	if (!result && list->mode == BUNDLE_MODE_NONE) {
diff --git a/config.c b/config.c
index 4e9e8e7abb9..3369b32d065 100644
--- a/config.c
+++ b/config.c
@@ -78,16 +78,6 @@ struct config_reader {
 	 */
 	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
@@ -113,16 +103,21 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 	return ret;
 }
 
-static inline void config_reader_set_kvi(struct config_reader *reader,
-					 struct key_value_info *kvi)
+static inline void config_reader_push_kvi(struct config_reader *reader,
+					  struct key_value_info *kvi)
 {
+	kvi->prev = reader->config_kvi;
 	reader->config_kvi = kvi;
 }
 
-static inline void config_reader_set_scope(struct config_reader *reader,
-					   enum config_scope scope)
+static inline struct key_value_info *config_reader_pop_kvi(struct config_reader *reader)
 {
-	reader->parsing_scope = scope;
+	struct key_value_info *ret;
+	if (!reader->config_kvi)
+		BUG("tried to pop config_kvi, but we weren't reading config");
+	ret = reader->config_kvi;
+	reader->config_kvi = reader->config_kvi->prev;
+	return ret;
 }
 
 static int pack_compression_seen;
@@ -244,7 +239,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    !cs ? "<unknown>" :
 			    cs->name ? cs->name :
 			    "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,
+							current_config_scope(),
+							NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -393,18 +390,12 @@ 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, &opts);
-
-	config_reader_set_scope(inc->config_reader, store_scope);
 }
 
 static int forbid_remote_url(const char *var, const char *value UNUSED,
@@ -471,10 +462,13 @@ static int include_condition_is_true(struct config_source *cs,
 	return 0;
 }
 
+static int kvi_fn(config_fn_t fn, const char *key, const char *value,
+		  struct key_value_info *kvi, void *data);
 static int git_config_include(const char *var, const char *value, void *data)
 {
 	struct config_include_data *inc = data;
 	struct config_source *cs = inc->config_reader->source;
+	struct key_value_info *kvi = inc->config_reader->config_kvi;
 	const char *cond, *key;
 	size_t cond_len;
 	int ret;
@@ -483,7 +477,7 @@ 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 = kvi_fn(inc->fn, var, value, kvi, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -655,13 +649,12 @@ out_free_ret_1:
 }
 
 static int kvi_fn(config_fn_t fn, const char *key, const char *value,
-		  struct key_value_info *kvi,
-		  void *data)
+		  struct key_value_info *kvi, void *data)
 {
 	int ret;
-	config_reader_set_kvi(&the_reader, kvi);
+	config_reader_push_kvi(&the_reader, kvi);
 	ret = fn(key, value, data);
-	config_reader_set_kvi(&the_reader, NULL);
+	config_reader_pop_kvi(&the_reader);
 	return ret;
 }
 
@@ -942,8 +935,8 @@ 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;
@@ -976,7 +969,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 = kvi_fn(fn, name->buf, value, kvi, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1075,8 +1069,19 @@ 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;
+}
+
 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;
@@ -1160,7 +1165,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;
 	}
 
@@ -2016,9 +2021,11 @@ int git_default_config(const char *var, const char *value, void *cb)
  * this function.
  */
 static int do_config_from(struct config_reader *reader,
-			  struct config_source *top, config_fn_t fn, void *data,
+			  struct config_source *top, config_fn_t fn,
+			  void *data, enum config_scope scope,
 			  const struct config_options *opts)
 {
+	struct key_value_info kvi = { 0 };
 	int ret;
 
 	/* push config-file parsing state stack */
@@ -2028,8 +2035,9 @@ static int do_config_from(struct config_reader *reader,
 	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);
@@ -2043,7 +2051,8 @@ static int do_config_from_file(struct config_reader *reader,
 			       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;
@@ -2058,19 +2067,21 @@ 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(reader, &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);
+				   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;
@@ -2081,7 +2092,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
 	f = fopen_or_warn(filename, "r");
 	if (f) {
 		ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
-					  filename, filename, f, data, opts);
+					  filename, filename, f, data, scope,
+					  opts);
 		fclose(f);
 	}
 	return ret;
@@ -2089,13 +2101,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;
 
@@ -2110,14 +2124,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(&the_reader, &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;
@@ -2133,7 +2148,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;
@@ -2142,13 +2157,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)
@@ -2201,8 +2217,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,
 				  config_fn_t fn, void *data)
 {
 	int ret = 0;
@@ -2210,7 +2225,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 	char *xdg_config = NULL;
 	char *user_config = NULL;
 	char *repo_config;
-	enum config_scope prev_parsing_scope = reader->parsing_scope;
 
 	if (opts->commondir)
 		repo_config = mkpathdup("%s/config", opts->commondir);
@@ -2219,39 +2233,40 @@ static int do_git_config_sequence(struct config_reader *reader,
 	else
 		repo_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 && repository_format_worktree_config) {
 		char *path = git_pathdup("config.worktree");
 		if (!access_or_die(path, R_OK, 0))
-			ret += git_config_from_file(fn, path, data);
+			ret += git_config_from_file_with_options(fn, path, data,
+								 CONFIG_SCOPE_WORKTREE,
+								 NULL);
 		free(path);
 	}
 
-	config_reader_set_scope(reader, 0);
 	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);
@@ -2264,7 +2279,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) {
@@ -2277,31 +2291,29 @@ int config_with_options(config_fn_t fn, void *data,
 		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) {
 		struct repository *repo = config_source->repo ?
 			config_source->repo : the_repository;
 		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, fn, data);
+		ret = do_git_config_sequence(opts, 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;
 }
 
@@ -2429,13 +2441,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 && reader->source->name) {
-		kv_info->filename = strintern(reader->source->name);
-		kv_info->linenr = reader->source->linenr;
-		kv_info->origin_type = reader->source->origin_type;
-		kv_info->scope = reader->parsing_scope;
-	} else
-		kvi_from_param(kv_info);
+	memcpy(kv_info, reader->config_kvi, sizeof(struct key_value_info));
 	si->util = kv_info;
 
 	return 0;
@@ -3473,7 +3479,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;
@@ -4018,7 +4025,13 @@ enum config_scope current_config_scope(void)
 	if (the_reader.config_kvi)
 		return the_reader.config_kvi->scope;
 	else
-		return the_reader.parsing_scope;
+		/*
+		 * FIXME This should be a BUG, but tr2_list_env_vars_fl is
+		 * calling this outside of a config callback. This will be
+		 * easier to fix when we plumb kvi through the config callbacks,
+		 * so leave this untouched for now.
+		 */
+		return CONFIG_SCOPE_UNKNOWN;
 }
 
 int current_config_line(void)
diff --git a/config.h b/config.h
index 247b572b37b..27d1718ac7b 100644
--- a/config.h
+++ b/config.h
@@ -142,16 +142,18 @@ int git_default_config(const char *, const char *, void *);
 int git_config_from_file(config_fn_t fn, const char *, void *);
 
 int git_config_from_file_with_options(config_fn_t fn, const char *,
-				      void *,
+				      void *, enum config_scope,
 				      const struct config_options *);
 int git_config_from_mem(config_fn_t fn,
 			const enum config_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);
 int git_config_from_blob_oid(config_fn_t fn, const char *name,
 			     struct repository *repo,
-			     const struct object_id *oid, void *data);
+			     const struct object_id *oid, void *data,
+			     enum config_scope scope);
 void git_config_push_parameter(const char *text);
 void git_config_push_env(const char *spec);
 int git_config_from_parameters(config_fn_t fn, void *data);
@@ -672,6 +674,7 @@ struct key_value_info {
 	int linenr;
 	enum config_origin_type origin_type;
 	enum config_scope scope;
+	struct key_value_info *prev;
 };
 
 /**
diff --git a/fsck.c b/fsck.c
index 8ef1b022346..4238344ed82 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1235,7 +1235,8 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
 		data.ret = 0;
 		config_opts.error_action = CONFIG_ERROR_SILENT;
 		if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
-					".gitmodules", buf, size, &data, &config_opts))
+					".gitmodules", buf, size, &data,
+					CONFIG_SCOPE_UNKNOWN, &config_opts))
 			data.ret |= report(options, oid, OBJ_BLOB,
 					FSCK_MSG_GITMODULES_PARSE,
 					"could not parse gitmodules blob");
diff --git a/submodule-config.c b/submodule-config.c
index ecf0fcf0074..c2f71f0b2e3 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -603,7 +603,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 	parameter.gitmodules_oid = &oid;
 	parameter.overwrite = 0;
 	git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
-			config, config_size, &parameter, NULL);
+			    config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
 	strbuf_release(&rev);
 	free(config);
 
@@ -711,7 +711,8 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
 
 	if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
 		git_config_from_blob_oid(gitmodules_cb, rev.buf,
-					 the_repository, &oid, the_repository);
+					 the_repository, &oid, the_repository,
+					 CONFIG_SCOPE_UNKNOWN);
 	}
 	strbuf_release(&rev);
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 04/14] config: add kvi.path, use it to evaluate includes
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (2 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 03/14] config: use kvi for config files Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 05/14] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Include directives are evaluated using the path of the config file. To
reduce the dependence on "config_reader.source", add a new
"key_value_info.path" member and use that instead of
"config_source.path".

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 40 ++++++++++++++++++++--------------------
 config.h |  1 +
 2 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/config.c b/config.c
index 3369b32d065..b952a4b118d 100644
--- a/config.c
+++ b/config.c
@@ -199,7 +199,7 @@ 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(struct key_value_info *kvi, const char *path,
 			       struct config_include_data *inc)
 {
 	int ret = 0;
@@ -221,14 +221,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;
 	}
@@ -236,12 +236,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_with_options(git_config_include, path, inc,
-							current_config_scope(),
-							NULL);
+							kvi->scope, NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -256,7 +255,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(struct key_value_info *kvi,
 					     struct strbuf *pat)
 {
 	struct strbuf path = STRBUF_INIT;
@@ -273,11 +272,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?");
@@ -292,7 +291,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(struct key_value_info *kvi,
 			     const struct config_options *opts,
 			     const char *cond, size_t cond_len, int icase)
 {
@@ -309,7 +308,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)
@@ -442,16 +441,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(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,
@@ -467,7 +466,6 @@ static int kvi_fn(config_fn_t fn, const char *key, const char *value,
 static int git_config_include(const char *var, const char *value, void *data)
 {
 	struct config_include_data *inc = data;
-	struct config_source *cs = inc->config_reader->source;
 	struct key_value_info *kvi = inc->config_reader->config_kvi;
 	const char *cond, *key;
 	size_t cond_len;
@@ -482,16 +480,16 @@ static int git_config_include(const char *var, const char *value, void *data)
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(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(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(kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -683,6 +681,7 @@ static void kvi_from_param(struct key_value_info *out)
 	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,
@@ -1077,6 +1076,7 @@ static void kvi_from_source(struct config_source *cs,
 	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,
diff --git a/config.h b/config.h
index 27d1718ac7b..f8bab9fdef4 100644
--- a/config.h
+++ b/config.h
@@ -674,6 +674,7 @@ struct key_value_info {
 	int linenr;
 	enum config_origin_type origin_type;
 	enum config_scope scope;
+	const char *path;
 	struct key_value_info *prev;
 };
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 05/14] config: pass source to config_parser_event_fn_t
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (3 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 04/14] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 06/14] config: inline git_color_default_config Glen Choo via GitGitGadget
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

..so that the callback can use a "struct config_source" parameter
instead of "config_reader.source". "struct config_source" is internal to
config.c, but this refactor is okay because this function has only ever
been (and probably ever will be) used internally by config.c.

This removes the last user of "config_reader.source", so remove it too.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 72 ++++++++++----------------------------------------------
 config.h |  6 +++++
 2 files changed, 18 insertions(+), 60 deletions(-)

diff --git a/config.c b/config.c
index b952a4b118d..147e422a27b 100644
--- a/config.c
+++ b/config.c
@@ -60,23 +60,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;
 };
 /*
@@ -86,23 +69,6 @@ struct config_reader {
  */
 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_push_kvi(struct config_reader *reader,
 					  struct key_value_info *kvi)
 {
@@ -1059,7 +1025,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;
@@ -2020,8 +1986,7 @@ 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,
+static int do_config_from(struct config_source *top, config_fn_t fn,
 			  void *data, enum config_scope scope,
 			  const struct config_options *opts)
 {
@@ -2034,21 +1999,17 @@ 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, &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, enum config_scope scope,
@@ -2067,7 +2028,7 @@ 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, scope, opts);
+	ret = do_config_from(&top, fn, data, scope, opts);
 	funlockfile(f);
 
 	return ret;
@@ -2076,8 +2037,8 @@ static int do_config_from_file(struct config_reader *reader,
 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, scope, 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,
@@ -2091,9 +2052,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, scope,
-					  opts);
+		ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
+					  filename, f, data, scope, opts);
 		fclose(f);
 	}
 	return ret;
@@ -2124,7 +2084,7 @@ 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, scope, opts);
+	return do_config_from(&top, fn, data, scope, opts);
 }
 
 int git_config_from_blob_oid(config_fn_t fn,
@@ -3019,7 +2979,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;
@@ -3065,11 +3024,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;
@@ -3389,8 +3347,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)
@@ -3951,8 +3907,6 @@ static int reader_origin_type(struct config_reader *reader,
 {
 	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;
@@ -4005,8 +3959,6 @@ 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;
@@ -4039,7 +3991,7 @@ int current_config_line(void)
 	if (the_reader.config_kvi)
 		return the_reader.config_kvi->linenr;
 	else
-		return the_reader.source->linenr;
+		BUG("current_config_line called outside config callback");
 }
 
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
diff --git a/config.h b/config.h
index f8bab9fdef4..525fc0d5e03 100644
--- a/config.h
+++ b/config.h
@@ -73,6 +73,7 @@ enum config_event_t {
 	CONFIG_EVENT_ERROR
 };
 
+struct config_source;
 /*
  * The parser event function (if not NULL) is called with the event type and
  * the begin/end offsets of the parsed elements.
@@ -82,6 +83,7 @@ enum config_event_t {
  */
 typedef int (*config_parser_event_fn_t)(enum config_event_t type,
 					size_t begin_offset, size_t end_offset,
+					struct config_source *cs,
 					void *event_fn_data);
 
 struct config_options {
@@ -101,6 +103,10 @@ struct config_options {
 
 	const char *commondir;
 	const char *git_dir;
+	/*
+	 * event_fn and event_fn_data are for internal use only. Handles events
+	 * emitted by the config parser.
+	 */
 	config_parser_event_fn_t event_fn;
 	void *event_fn_data;
 	enum config_error_action {
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 06/14] config: inline git_color_default_config
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (4 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 05/14] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 07/14] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

git_color_default_config() is a shorthand for calling two other config
callbacks. There are no other non-static functions that do this and it
will complicate our refactoring of config_fn_t so inline it instead.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/branch.c      | 5 ++++-
 builtin/clean.c       | 6 ++++--
 builtin/grep.c        | 5 ++++-
 builtin/show-branch.c | 5 ++++-
 builtin/tag.c         | 6 +++++-
 color.c               | 8 --------
 color.h               | 6 +-----
 7 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 6413a016c57..c6982181fd5 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -114,7 +114,10 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/clean.c b/builtin/clean.c
index 14c0d555eac..a06df48a269 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -129,8 +129,10 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	/* inspect the color.ui config variable and others */
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/grep.c b/builtin/grep.c
index a1b68d90bdb..c880c9538d6 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -290,7 +290,10 @@ static int wait_all(void)
 static int grep_cmd_config(const char *var, const char *value, void *cb)
 {
 	int st = grep_config(var, value, cb);
-	if (git_color_default_config(var, value, NULL) < 0)
+
+	if (git_color_config(var, value, cb) < 0)
+		st = -1;
+	else if (git_default_config(var, value, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 463a8d11c31..82ae2a7e475 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -576,7 +576,10 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/tag.c b/builtin/tag.c
index 782bb3aa2ff..7245a4d30e6 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -204,7 +204,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 
 	if (starts_with(var, "column."))
 		return git_column_config(var, value, "tag", &colopts);
-	return git_color_default_config(var, value, cb);
+
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/color.c b/color.c
index 672dcbb73a6..9bdbffe928d 100644
--- a/color.c
+++ b/color.c
@@ -427,14 +427,6 @@ int git_color_config(const char *var, const char *value, void *cb UNUSED)
 	return 0;
 }
 
-int git_color_default_config(const char *var, const char *value, void *cb)
-{
-	if (git_color_config(var, value, cb) < 0)
-		return -1;
-
-	return git_default_config(var, value, cb);
-}
-
 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
 {
 	if (*color)
diff --git a/color.h b/color.h
index cfc8f841b23..bb28343be21 100644
--- a/color.h
+++ b/color.h
@@ -88,12 +88,8 @@ extern const int column_colors_ansi_max;
  */
 extern int color_stdout_is_tty;
 
-/*
- * Use the first one if you need only color config; the second is a convenience
- * if you are just going to change to git_default_config, too.
- */
+/* Parse color config. */
 int git_color_config(const char *var, const char *value, void *cb);
-int git_color_default_config(const char *var, const char *value, void *cb);
 
 /*
  * Parse a config option, which can be a boolean or one of
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 07/14] urlmatch.h: use config_fn_t type
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (5 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 06/14] config: inline git_color_default_config Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 08/14] (RFC-only) config: add kvi arg to config_fn_t Glen Choo via GitGitGadget
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

These are actually used as config callbacks, so use the typedef-ed type
and make future refactors easier.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 urlmatch.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/urlmatch.h b/urlmatch.h
index 9f40b00bfb8..bee374a642c 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -2,6 +2,7 @@
 #define URL_MATCH_H
 
 #include "string-list.h"
+#include "config.h"
 
 struct url_info {
 	/* normalized url on success, must be freed, otherwise NULL */
@@ -48,8 +49,8 @@ struct urlmatch_config {
 	const char *key;
 
 	void *cb;
-	int (*collect_fn)(const char *var, const char *value, void *cb);
-	int (*cascade_fn)(const char *var, const char *value, void *cb);
+	config_fn_t collect_fn;
+	config_fn_t cascade_fn;
 	/*
 	 * Compare the two matches, the one just discovered and the existing
 	 * best match and return a negative value if the found item is to be
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 08/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (6 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 07/14] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 09/14] (RFC-only) config: apply cocci to config_fn_t implementations Glen Choo via GitGitGadget
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

..without actually changing any of its implementations. This commit does
not build - I've split this out for readability, but post-RFC I will
squash this with the rest of the refactor + cocci changes.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c                                      |   2 +-
 config.h                                      |  20 +--
 .../coccinelle/config_fn_kvi.pending.cocci    | 146 ++++++++++++++++++
 3 files changed, 157 insertions(+), 11 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_kvi.pending.cocci

diff --git a/config.c b/config.c
index 147e422a27b..758d6a5cc3b 100644
--- a/config.c
+++ b/config.c
@@ -617,7 +617,7 @@ static int kvi_fn(config_fn_t fn, const char *key, const char *value,
 {
 	int ret;
 	config_reader_push_kvi(&the_reader, kvi);
-	ret = fn(key, value, data);
+	ret = fn(key, value, kvi, data);
 	config_reader_pop_kvi(&the_reader);
 	return ret;
 }
diff --git a/config.h b/config.h
index 525fc0d5e03..2c7b7399691 100644
--- a/config.h
+++ b/config.h
@@ -117,6 +117,15 @@ struct config_options {
 	} error_action;
 };
 
+struct key_value_info {
+	const char *filename;
+	int linenr;
+	enum config_origin_type origin_type;
+	enum config_scope scope;
+	const char *path;
+	struct key_value_info *prev;
+};
+
 /**
  * A config callback function takes three parameters:
  *
@@ -135,7 +144,7 @@ struct config_options {
  * A config callback should return 0 for success, or -1 if the variable
  * could not be parsed properly.
  */
-typedef int (*config_fn_t)(const char *, const char *, void *);
+typedef int (*config_fn_t)(const char *, const char *, struct key_value_info *, void *);
 
 int git_default_config(const char *, const char *, void *);
 
@@ -675,15 +684,6 @@ int git_config_get_expiry(const char *key, const char **output);
 /* parse either "this many days" integer, or "5.days.ago" approxidate */
 int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
 
-struct key_value_info {
-	const char *filename;
-	int linenr;
-	enum config_origin_type origin_type;
-	enum config_scope scope;
-	const char *path;
-	struct key_value_info *prev;
-};
-
 /**
  * First prints the error message specified by the caller in `err` and then
  * dies printing the line number and the file name of the highest priority
diff --git a/contrib/coccinelle/config_fn_kvi.pending.cocci b/contrib/coccinelle/config_fn_kvi.pending.cocci
new file mode 100644
index 00000000000..d4c84599afa
--- /dev/null
+++ b/contrib/coccinelle/config_fn_kvi.pending.cocci
@@ -0,0 +1,146 @@
+// These are safe to apply to *.c *.h builtin/*.c
+
+@ get_fn @
+identifier fn, R;
+@@
+(
+(
+git_config_from_file
+|
+git_config_from_file_with_options
+|
+git_config_from_mem
+|
+git_config_from_blob_oid
+|
+read_early_config
+|
+read_very_early_config
+|
+config_with_options
+|
+git_config
+|
+git_protected_config
+|
+config_from_gitmodules
+)
+  (fn, ...)
+|
+repo_config(R, fn, ...)
+)
+
+@ extends get_fn @
+identifier C1, C2, D;
+@@
+int fn(const char *C1, const char *C2,
++  struct key_value_info *kvi,
+  void *D);
+
+@ extends get_fn @
+@@
+int fn(const char *, const char *,
++  struct key_value_info *,
+  void *);
+
+@ extends get_fn @
+// Don't change fns that look like callback fns but aren't
+identifier fn2 != tar_filter_config && != git_diff_heuristic_config &&
+  != git_default_submodule_config && != git_color_config &&
+  != bundle_list_update && != parse_object_filter_config;
+identifier C1, C2, D1, D2, S;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++  struct key_value_info *kvi,
+  void *D1) {
+<+...
+(
+fn2(C1, C2,
++ kvi,
+D2);
+|
+if(fn2(C1, C2,
++ kvi,
+D2) < 0) { ... }
+|
+return fn2(C1, C2,
++ kvi,
+D2);
+|
+S = fn2(C1, C2,
++ kvi,
+D2);
+)
+...+>
+  }
+
+@ extends get_fn@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++  struct key_value_info *kvi UNUSED,
+  void *D) {...}
+
+
+// The previous rules don't catch all callbacks, especially if they're defined
+// in a separate file from the git_config() call. Fix these manually.
+@@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int
+(
+git_ident_config
+|
+urlmatch_collect_fn
+|
+write_one_config
+|
+forbid_remote_url
+|
+credential_config_callback
+)
+  (const char *C1, const char *C2,
++  struct key_value_info *kvi UNUSED,
+  void *D) {...}
+
+@@
+identifier C1, C2, D, D2, S, fn2;
+@@
+int
+(
+http_options
+|
+git_status_config
+|
+git_commit_config
+|
+git_default_core_config
+|
+grep_config
+)
+  (const char *C1, const char *C2,
++  struct key_value_info *kvi,
+  void *D) {
+<+...
+(
+fn2(C1, C2,
++ kvi,
+D2);
+|
+if(fn2(C1, C2,
++ kvi,
+D2) < 0) { ... }
+|
+return fn2(C1, C2,
++ kvi,
+D2);
+|
+S = fn2(C1, C2,
++ kvi,
+D2);
+)
+...+>
+  }
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 09/14] (RFC-only) config: apply cocci to config_fn_t implementations
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (7 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 08/14] (RFC-only) config: add kvi arg to config_fn_t Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass "struct key_value_info" to *most* functions that are invoked as
"config_fn_t" callbacks by applying
contrib/coccinelle/config_fn_kvi.pending.cocci. None of the functions
actually use the "kvi" arg yet (besides propagating it to a function
that now expects "kvi"), but this will be addressed in a later commit.
When deciding whether or not to propagate "kvi" to an inner function,
only propagate the "kvi" arg if the inner function is actually invoked
elsewhere as a config callback; it does not matter whether the function
happens have the same signature as config_fn_t.

This commit does not build and has several style issues (e.g. a lack of
spacing around the "kvi" arg), but I've split this out for the RFC so
that it's more obvious which changes are automatic vs manual. Post-RFC I
will squash this with the rest of the refactor + cocci changes.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 alias.c                     |  3 ++-
 archive-tar.c               |  3 ++-
 archive-zip.c               |  1 +
 builtin/add.c               |  5 +++--
 builtin/blame.c             |  5 +++--
 builtin/branch.c            |  5 +++--
 builtin/cat-file.c          |  5 +++--
 builtin/checkout.c          |  5 +++--
 builtin/clean.c             |  5 +++--
 builtin/clone.c             |  8 +++++---
 builtin/column.c            |  3 ++-
 builtin/commit-graph.c      |  1 +
 builtin/commit.c            | 10 ++++++----
 builtin/config.c            | 10 +++++++---
 builtin/difftool.c          |  5 +++--
 builtin/fetch.c             |  8 +++++---
 builtin/fsmonitor--daemon.c |  5 +++--
 builtin/grep.c              |  5 +++--
 builtin/help.c              |  5 +++--
 builtin/index-pack.c        |  5 +++--
 builtin/log.c               | 10 ++++++----
 builtin/merge.c             |  7 ++++---
 builtin/multi-pack-index.c  |  1 +
 builtin/pack-objects.c      |  5 +++--
 builtin/patch-id.c          |  5 +++--
 builtin/pull.c              |  5 +++--
 builtin/push.c              |  5 +++--
 builtin/read-tree.c         |  5 +++--
 builtin/rebase.c            |  5 +++--
 builtin/receive-pack.c      |  5 +++--
 builtin/reflog.c            |  7 ++++---
 builtin/remote.c            |  6 ++++--
 builtin/repack.c            |  5 +++--
 builtin/reset.c             |  5 +++--
 builtin/send-pack.c         |  5 +++--
 builtin/show-branch.c       |  5 +++--
 builtin/stash.c             |  5 +++--
 builtin/submodule--helper.c |  1 +
 builtin/tag.c               |  5 +++--
 builtin/var.c               |  5 +++--
 builtin/worktree.c          |  5 +++--
 bundle-uri.c                |  8 ++++++--
 config.c                    | 28 ++++++++++++++++++----------
 config.h                    |  3 ++-
 connect.c                   |  4 ++--
 convert.c                   |  4 +++-
 credential.c                |  1 +
 delta-islands.c             |  3 ++-
 diff.c                      | 10 ++++++----
 diff.h                      |  6 ++++--
 fetch-pack.c                |  5 +++--
 fmt-merge-msg.c             |  5 +++--
 fmt-merge-msg.h             |  3 ++-
 fsck.c                      |  8 +++++---
 fsck.h                      |  3 ++-
 gpg-interface.c             |  6 ++++--
 grep.c                      |  3 ++-
 help.c                      |  7 +++++--
 ident.c                     |  3 ++-
 ident.h                     |  3 ++-
 imap-send.c                 |  5 +++--
 ll-merge.c                  |  1 +
 ls-refs.c                   |  2 +-
 mailinfo.c                  |  5 +++--
 notes-utils.c               |  3 ++-
 notes.c                     |  3 ++-
 pager.c                     |  5 ++++-
 pretty.c                    |  1 +
 promisor-remote.c           |  4 +++-
 remote.c                    |  3 ++-
 revision.c                  |  3 ++-
 scalar.c                    |  3 ++-
 sequencer.c                 |  8 +++++---
 setup.c                     | 15 ++++++++++-----
 submodule-config.c          | 15 +++++++++++----
 t/helper/test-config.c      |  9 ++++++---
 t/helper/test-userdiff.c    |  4 +++-
 trace2/tr2_cfg.c            |  3 ++-
 trace2/tr2_sysenv.c         |  3 ++-
 trailer.c                   |  2 ++
 upload-pack.c               |  8 ++++++--
 urlmatch.c                  |  3 ++-
 urlmatch.h                  |  3 ++-
 xdiff-interface.c           |  5 +++--
 xdiff-interface.h           |  3 ++-
 85 files changed, 285 insertions(+), 156 deletions(-)

diff --git a/alias.c b/alias.c
index e814948ced3..38c51038a13 100644
--- a/alias.c
+++ b/alias.c
@@ -11,7 +11,8 @@ struct config_alias_data {
 	struct string_list *list;
 };
 
-static int config_alias_cb(const char *key, const char *value, void *d)
+static int config_alias_cb(const char *key, const char *value,
+			   struct key_value_info *kvi UNUSED, void *d)
 {
 	struct config_alias_data *data = d;
 	const char *p;
diff --git a/archive-tar.c b/archive-tar.c
index 497dad0b3af..dcfbce5225a 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -410,7 +410,8 @@ static int tar_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int git_tar_config(const char *var, const char *value, void *cb)
+static int git_tar_config(const char *var, const char *value,
+			  struct key_value_info *kvi UNUSED, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
diff --git a/archive-zip.c b/archive-zip.c
index e6f5c10a14f..0b028246689 100644
--- a/archive-zip.c
+++ b/archive-zip.c
@@ -616,6 +616,7 @@ static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
 }
 
 static int archive_zip_config(const char *var, const char *value,
+			      struct key_value_info *kvi UNUSED,
 			      void *data UNUSED)
 {
 	return userdiff_config(var, value);
diff --git a/builtin/add.c b/builtin/add.c
index f12054d9be1..f8e42e05b07 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -355,7 +355,8 @@ static struct option builtin_add_options[] = {
 	OPT_END(),
 };
 
-static int add_config(const char *var, const char *value, void *cb)
+static int add_config(const char *var, const char *value,
+		      struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "add.ignoreerrors") ||
 	    !strcmp(var, "add.ignore-errors")) {
@@ -363,7 +364,7 @@ static int add_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static const char embedded_advice[] = N_(
diff --git a/builtin/blame.c b/builtin/blame.c
index a8d2114adc9..0aafc8172e2 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -692,7 +692,8 @@ static const char *add_prefix(const char *prefix, const char *path)
 	return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 }
 
-static int git_blame_config(const char *var, const char *value, void *cb)
+static int git_blame_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "blame.showroot")) {
 		show_root = git_config_bool(var, value);
@@ -765,7 +766,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
diff --git a/builtin/branch.c b/builtin/branch.c
index c6982181fd5..26091a036d2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -80,7 +80,8 @@ static unsigned int colopts;
 
 define_list_config_array(color_branch_slots);
 
-static int git_branch_config(const char *var, const char *value, void *cb)
+static int git_branch_config(const char *var, const char *value,
+			     struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 
@@ -117,7 +118,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 04d4bb6c777..b1e0e95d631 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -882,12 +882,13 @@ static int batch_objects(struct batch_options *opt)
 	return retval;
 }
 
-static int git_cat_file_config(const char *var, const char *value, void *cb)
+static int git_cat_file_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int batch_option_callback(const struct option *opt,
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 38a8cd6a965..92017ba6696 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1182,7 +1182,8 @@ static int switch_branches(const struct checkout_opts *opts,
 	return ret || writeout_error;
 }
 
-static int git_checkout_config(const char *var, const char *value, void *cb)
+static int git_checkout_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	struct checkout_opts *opts = cb;
 
@@ -1198,7 +1199,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
 
-	return git_xmerge_config(var, value, NULL);
+	return git_xmerge_config(var, value,kvi, NULL);
 }
 
 static void setup_new_branch_info_and_source_tree(
diff --git a/builtin/clean.c b/builtin/clean.c
index a06df48a269..1c648276ebf 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -102,7 +102,8 @@ struct menu_stuff {
 
 define_list_config_array(color_interactive_slots);
 
-static int git_clean_config(const char *var, const char *value, void *cb)
+static int git_clean_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 
@@ -132,7 +133,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/clone.c b/builtin/clone.c
index 6dc89f1058b..1e1cf104194 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -775,7 +775,8 @@ static int checkout(int submodule_progress, int filter_submodules)
 	return err;
 }
 
-static int git_clone_config(const char *k, const char *v, void *cb)
+static int git_clone_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "clone.defaultremotename")) {
 		free(remote_name);
@@ -786,10 +787,11 @@ static int git_clone_config(const char *k, const char *v, void *cb)
 	if (!strcmp(k, "clone.filtersubmodules"))
 		config_filter_submodules = git_config_bool(k, v);
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
-static int write_one_config(const char *key, const char *value, void *data)
+static int write_one_config(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *data)
 {
 	/*
 	 * give git_clone_config a chance to write config values back to the
diff --git a/builtin/column.c b/builtin/column.c
index de623a16c2d..30cfbed62ec 100644
--- a/builtin/column.c
+++ b/builtin/column.c
@@ -13,7 +13,8 @@ static const char * const builtin_column_usage[] = {
 };
 static unsigned int colopts;
 
-static int column_config(const char *var, const char *value, void *cb)
+static int column_config(const char *var, const char *value,
+			 struct key_value_info *kvi UNUSED, void *cb)
 {
 	return git_column_config(var, value, cb, &colopts);
 }
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 90114269761..e811866b5dd 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -185,6 +185,7 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
+					 struct key_value_info *kvi UNUSED,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
diff --git a/builtin/commit.c b/builtin/commit.c
index 9d8e1ea91a3..ec468e87039 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1402,7 +1402,8 @@ static int parse_status_slot(const char *slot)
 	return LOOKUP_CONFIG(color_status_slots, slot);
 }
 
-static int git_status_config(const char *k, const char *v, void *cb)
+static int git_status_config(const char *k, const char *v,
+			     struct key_value_info *kvi, void *cb)
 {
 	struct wt_status *s = cb;
 	const char *slot_name;
@@ -1487,7 +1488,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
 		s->detect_rename = git_config_rename(k, v);
 		return 0;
 	}
-	return git_diff_ui_config(k, v, NULL);
+	return git_diff_ui_config(k, v,kvi, NULL);
 }
 
 int cmd_status(int argc, const char **argv, const char *prefix)
@@ -1602,7 +1603,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int git_commit_config(const char *k, const char *v, void *cb)
+static int git_commit_config(const char *k, const char *v,
+			     struct key_value_info *kvi, void *cb)
 {
 	struct wt_status *s = cb;
 
@@ -1624,7 +1626,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_status_config(k, v, s);
+	return git_status_config(k, v,kvi, s);
 }
 
 int cmd_commit(int argc, const char **argv, const char *prefix)
diff --git a/builtin/config.c b/builtin/config.c
index fe79fb60c43..b2ad7351d0a 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -214,7 +214,7 @@ static void show_config_scope(struct strbuf *buf)
 }
 
 static int show_all_config(const char *key_, const char *value_,
-			   void *cb UNUSED)
+			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
 {
 	if (show_origin || show_scope) {
 		struct strbuf buf = STRBUF_INIT;
@@ -298,7 +298,8 @@ 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_,
+			  struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct strbuf_list *values = cb;
 
@@ -466,6 +467,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,
+				struct key_value_info *kvi UNUSED,
 				void *cb UNUSED)
 {
 	if (!strcmp(var, get_color_slot)) {
@@ -498,6 +500,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,
+				    struct key_value_info *kvi UNUSED,
 				    void *data UNUSED)
 {
 	if (!strcmp(var, get_colorbool_slot))
@@ -555,7 +558,8 @@ struct urlmatch_current_candidate_value {
 	struct strbuf value;
 };
 
-static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
+static int urlmatch_collect_fn(const char *var, const char *value,
+			       struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct string_list *values = cb;
 	struct string_list_item *item = string_list_insert(values, var);
diff --git a/builtin/difftool.c b/builtin/difftool.c
index e010a21bfbc..d4d149bcf6b 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -38,14 +38,15 @@ static const char *const builtin_difftool_usage[] = {
 	NULL
 };
 
-static int difftool_config(const char *var, const char *value, void *cb)
+static int difftool_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "difftool.trustexitcode")) {
 		trust_exit_code = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int print_tool_help(void)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 85bd2801036..aa688291613 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -100,7 +100,8 @@ static int fetch_write_commit_graph = -1;
 static int stdin_refspecs = 0;
 static int negotiate_only;
 
-static int git_fetch_config(const char *k, const char *v, void *cb)
+static int git_fetch_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "fetch.prune")) {
 		fetch_prune_config = git_config_bool(k, v);
@@ -140,7 +141,7 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
@@ -1828,7 +1829,8 @@ struct remote_group_data {
 	struct string_list *list;
 };
 
-static int get_remote_group(const char *key, const char *value, void *priv)
+static int get_remote_group(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *priv)
 {
 	struct remote_group_data *g = priv;
 
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 42af6a2cc7e..a7375d61d02 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -36,7 +36,8 @@ static int fsmonitor__start_timeout_sec = 60;
 #define FSMONITOR__ANNOUNCE_STARTUP "fsmonitor.announcestartup"
 static int fsmonitor__announce_startup = 0;
 
-static int fsmonitor_config(const char *var, const char *value, void *cb)
+static int fsmonitor_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
 		int i = git_config_int(var, value);
@@ -66,7 +67,7 @@ static int fsmonitor_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /*
diff --git a/builtin/grep.c b/builtin/grep.c
index c880c9538d6..177befc3ed4 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -287,13 +287,14 @@ static int wait_all(void)
 	return hit;
 }
 
-static int grep_cmd_config(const char *var, const char *value, void *cb)
+static int grep_cmd_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	int st = grep_config(var, value, cb);
 
 	if (git_color_config(var, value, cb) < 0)
 		st = -1;
-	else if (git_default_config(var, value, cb) < 0)
+	else if (git_default_config(var, value,kvi, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/help.c b/builtin/help.c
index 87333a02ec4..5d4a86c4b41 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -396,7 +396,8 @@ static int add_man_viewer_info(const char *var, const char *value)
 	return 0;
 }
 
-static int git_help_config(const char *var, const char *value, void *cb)
+static int git_help_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "help.format")) {
 		if (!value)
@@ -419,7 +420,7 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index b17e79cd40f..4450510ddfc 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1578,7 +1578,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
 	strbuf_release(&pack_name);
 }
 
-static int git_index_pack_config(const char *k, const char *v, void *cb)
+static int git_index_pack_config(const char *k, const char *v,
+				 struct key_value_info *kvi, void *cb)
 {
 	struct pack_idx_option *opts = cb;
 
@@ -1605,7 +1606,7 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
 		else
 			opts->flags &= ~WRITE_REV;
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 static int cmp_uint32(const void *a_, const void *b_)
diff --git a/builtin/log.c b/builtin/log.c
index 7d195789633..f8e61330491 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -559,7 +559,8 @@ static int cmd_log_walk(struct rev_info *rev)
 	return retval;
 }
 
-static int git_log_config(const char *var, const char *value, void *cb)
+static int git_log_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 
@@ -608,7 +609,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_diff_ui_config(var, value, cb);
+	return git_diff_ui_config(var, value,kvi, cb);
 }
 
 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
@@ -974,7 +975,8 @@ static enum cover_from_description parse_cover_from_description(const char *arg)
 		die(_("%s: invalid cover from description mode"), arg);
 }
 
-static int git_format_config(const char *var, const char *value, void *cb)
+static int git_format_config(const char *var, const char *value,
+			     struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "format.headers")) {
 		if (!value)
@@ -1103,7 +1105,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "diff.noprefix"))
 		return 0;
 
-	return git_log_config(var, value, cb);
+	return git_log_config(var, value,kvi, cb);
 }
 
 static const char *output_directory = NULL;
diff --git a/builtin/merge.c b/builtin/merge.c
index a99be9610e9..492a83a900c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -620,7 +620,8 @@ static void parse_branch_merge_options(char *bmo)
 	free(argv);
 }
 
-static int git_merge_config(const char *k, const char *v, void *cb)
+static int git_merge_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	int status;
 	const char *str;
@@ -665,10 +666,10 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	status = fmt_merge_msg_config(k, v, cb);
+	status = fmt_merge_msg_config(k, v,kvi, cb);
 	if (status)
 		return status;
-	return git_diff_ui_config(k, v, cb);
+	return git_diff_ui_config(k, v,kvi, cb);
 }
 
 static int read_tree_trivial(struct object_id *common, struct object_id *head,
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 1b5083f8b26..c3cd7163c84 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -82,6 +82,7 @@ static struct option *add_common_options(struct option *prev)
 }
 
 static int git_multi_pack_index_write_config(const char *var, const char *value,
+					     struct key_value_info *kvi UNUSED,
 					     void *cb UNUSED)
 {
 	if (!strcmp(var, "pack.writebitmaphashcache")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 77d88f85b04..ca023000cc0 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3134,7 +3134,8 @@ static void prepare_pack(int window, int depth)
 	free(delta_list);
 }
 
-static int git_pack_config(const char *k, const char *v, void *cb)
+static int git_pack_config(const char *k, const char *v,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
 		window = git_config_int(k, v);
@@ -3226,7 +3227,7 @@ static int git_pack_config(const char *k, const char *v, void *cb)
 		ex->uri = xstrdup(pack_end + 1);
 		oidmap_put(&configured_exclusions, ex);
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 /* Counters for trace2 output when in --stdin-packs mode. */
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 9d5585d3a72..9b4f5a71b87 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -196,7 +196,8 @@ struct patch_id_opts {
 	int verbatim;
 };
 
-static int git_patch_id_config(const char *var, const char *value, void *cb)
+static int git_patch_id_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	struct patch_id_opts *opts = cb;
 
@@ -209,7 +210,7 @@ static int git_patch_id_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_patch_id(int argc, const char **argv, const char *prefix)
diff --git a/builtin/pull.c b/builtin/pull.c
index 5405d09f22f..1b244eee67c 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -359,7 +359,8 @@ static enum rebase_type config_get_rebase(int *rebase_unspecified)
 /**
  * Read config variables.
  */
-static int git_pull_config(const char *var, const char *value, void *cb)
+static int git_pull_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "rebase.autostash")) {
 		config_autostash = git_config_bool(var, value);
@@ -372,7 +373,7 @@ static int git_pull_config(const char *var, const char *value, void *cb)
 		check_trust_level = 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /**
diff --git a/builtin/push.c b/builtin/push.c
index fa550b8f80a..65b79378b92 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -506,7 +506,8 @@ static void set_push_cert_flags(int *flags, int v)
 }
 
 
-static int git_push_config(const char *k, const char *v, void *cb)
+static int git_push_config(const char *k, const char *v,
+			   struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 	int *flags = cb;
@@ -573,7 +574,7 @@ static int git_push_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_default_config(k, v, NULL);
+	return git_default_config(k, v,kvi, NULL);
 }
 
 int cmd_push(int argc, const char **argv, const char *prefix)
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 600d4f748fc..e9859b6157e 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -100,12 +100,13 @@ static int debug_merge(const struct cache_entry * const *stages,
 	return 0;
 }
 
-static int git_read_tree_config(const char *var, const char *value, void *cb)
+static int git_read_tree_config(const char *var, const char *value,
+				struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 680fe3c1453..1fd05d708b8 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -791,7 +791,8 @@ static void parse_rebase_merges_value(struct rebase_options *options, const char
 		die(_("Unknown rebase-merges mode: %s"), value);
 }
 
-static int rebase_config(const char *var, const char *value, void *data)
+static int rebase_config(const char *var, const char *value,
+			 struct key_value_info *kvi, void *data)
 {
 	struct rebase_options *opts = data;
 
@@ -850,7 +851,7 @@ static int rebase_config(const char *var, const char *value, void *data)
 		return git_config_string(&opts->default_backend, var, value);
 	}
 
-	return git_default_config(var, value, data);
+	return git_default_config(var, value,kvi, data);
 }
 
 static int checkout_up_to_date(struct rebase_options *options)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 9109552533d..2f5fd2abbc3 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -135,7 +135,8 @@ static enum deny_action parse_deny_action(const char *var, const char *value)
 	return DENY_IGNORE;
 }
 
-static int receive_pack_config(const char *var, const char *value, void *cb)
+static int receive_pack_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
 
@@ -262,7 +263,7 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static void show_ref(const char *path, const struct object_id *oid)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index a1fa0c855f4..ecf21ac9c6e 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -108,7 +108,8 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
 #define EXPIRE_TOTAL   01
 #define EXPIRE_UNREACH 02
 
-static int reflog_expire_config(const char *var, const char *value, void *cb)
+static int reflog_expire_config(const char *var, const char *value,
+				struct key_value_info *kvi, void *cb)
 {
 	const char *pattern, *key;
 	size_t pattern_len;
@@ -117,7 +118,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 	struct reflog_expire_cfg *ent;
 
 	if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value,kvi, cb);
 
 	if (!strcmp(key, "reflogexpire")) {
 		slot = EXPIRE_TOTAL;
@@ -128,7 +129,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 		if (git_config_expiry_date(&expire, var, value))
 			return -1;
 	} else
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value,kvi, cb);
 
 	if (!pattern) {
 		switch (slot) {
diff --git a/builtin/remote.c b/builtin/remote.c
index 1e0b137d977..edb4a9ddd7f 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -268,6 +268,7 @@ static const char *abbrev_ref(const char *name, const char *prefix)
 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
 
 static int config_read_branches(const char *key, const char *value,
+				struct key_value_info *kvi UNUSED,
 				void *data UNUSED)
 {
 	const char *orig_key = key;
@@ -645,7 +646,7 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	void *cb)
+	struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
@@ -1494,7 +1495,8 @@ static int prune(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
+static int get_remote_default(const char *key, const char *value UNUSED,
+			      struct key_value_info *kvi UNUSED, void *priv)
 {
 	if (strcmp(key, "remotes.default") == 0) {
 		int *found = priv;
diff --git a/builtin/repack.c b/builtin/repack.c
index df4d8e0f0ba..7c8401b2227 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -58,7 +58,8 @@ struct pack_objects_args {
 	int local;
 };
 
-static int repack_config(const char *var, const char *value, void *cb)
+static int repack_config(const char *var, const char *value,
+			 struct key_value_info *kvi, void *cb)
 {
 	struct pack_objects_args *cruft_po_args = cb;
 	if (!strcmp(var, "repack.usedeltabaseoffset")) {
@@ -90,7 +91,7 @@ static int repack_config(const char *var, const char *value, void *cb)
 		return git_config_string(&cruft_po_args->depth, var, value);
 	if (!strcmp(var, "repack.cruftthreads"))
 		return git_config_string(&cruft_po_args->threads, var, value);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /*
diff --git a/builtin/reset.c b/builtin/reset.c
index 0ed329236c8..a04d46d7fdd 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -308,12 +308,13 @@ static int reset_refs(const char *rev, const struct object_id *oid)
 	return update_ref_status;
 }
 
-static int git_reset_config(const char *var, const char *value, void *cb)
+static int git_reset_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_reset(int argc, const char **argv, const char *prefix)
diff --git a/builtin/send-pack.c b/builtin/send-pack.c
index 4784143004d..b0c90e549b8 100644
--- a/builtin/send-pack.c
+++ b/builtin/send-pack.c
@@ -131,7 +131,8 @@ static void print_helper_status(struct ref *ref)
 	strbuf_release(&buf);
 }
 
-static int send_pack_config(const char *k, const char *v, void *cb)
+static int send_pack_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "push.gpgsign")) {
 		const char *value;
@@ -151,7 +152,7 @@ static int send_pack_config(const char *k, const char *v, void *cb)
 			}
 		}
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 int cmd_send_pack(int argc, const char **argv, const char *prefix)
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 82ae2a7e475..ad8a391904e 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -556,7 +556,8 @@ static void append_one_rev(const char *av)
 	die("bad sha1 reference %s", av);
 }
 
-static int git_show_branch_config(const char *var, const char *value, void *cb)
+static int git_show_branch_config(const char *var, const char *value,
+				  struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "showbranch.default")) {
 		if (!value)
@@ -579,7 +580,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/stash.c b/builtin/stash.c
index 735d27039e1..689087d6240 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -836,7 +836,8 @@ static int show_stat = 1;
 static int show_patch;
 static int show_include_untracked;
 
-static int git_stash_config(const char *var, const char *value, void *cb)
+static int git_stash_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "stash.showstat")) {
 		show_stat = git_config_bool(var, value);
@@ -850,7 +851,7 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 		show_include_untracked = git_config_bool(var, value);
 		return 0;
 	}
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value,kvi, cb);
 }
 
 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 569068e6a2c..8570effbf0d 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2187,6 +2187,7 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
+				   struct key_value_info *kvi UNUSED,
 				   void *cb)
 {
 	int *max_jobs = cb;
diff --git a/builtin/tag.c b/builtin/tag.c
index 7245a4d30e6..626fe8c641e 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -183,7 +183,8 @@ static const char tag_template_nocleanup[] =
 	"Lines starting with '%c' will be kept; you may remove them"
 	" yourself if you want to.\n");
 
-static int git_tag_config(const char *var, const char *value, void *cb)
+static int git_tag_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "tag.gpgsign")) {
 		config_sign_tag = git_config_bool(var, value);
@@ -208,7 +209,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/builtin/var.c b/builtin/var.c
index acb988d2d56..440add19bb5 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -69,13 +69,14 @@ static const struct git_var *get_git_var(const char *var)
 	return NULL;
 }
 
-static int show_config(const char *var, const char *value, void *cb)
+static int show_config(const char *var, const char *value,
+		       struct key_value_info *kvi, void *cb)
 {
 	if (value)
 		printf("%s=%s\n", var, value);
 	else
 		printf("%s\n", var);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 39e9e5c9ce8..d5ccd741e87 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -103,14 +103,15 @@ static int verbose;
 static int guess_remote;
 static timestamp_t expire;
 
-static int git_worktree_config(const char *var, const char *value, void *cb)
+static int git_worktree_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "worktree.guessremote")) {
 		guess_remote = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int delete_git_dir(const char *id)
diff --git a/bundle-uri.c b/bundle-uri.c
index 8c4e2b70b89..bb88ccbca4b 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -223,7 +223,9 @@ static int bundle_list_update(const char *key, const char *value,
 	return 0;
 }
 
-static int config_to_bundle_list(const char *key, const char *value, void *data)
+static int config_to_bundle_list(const char *key, const char *value,
+				 struct key_value_info *kvi UNUSED,
+				 void *data)
 {
 	struct bundle_list *list = data;
 	return bundle_list_update(key, value, list);
@@ -871,7 +873,9 @@ cached:
 	return advertise_bundle_uri;
 }
 
-static int config_to_packet_line(const char *key, const char *value, void *data)
+static int config_to_packet_line(const char *key, const char *value,
+				 struct key_value_info *kvi UNUSED,
+				 void *data)
 {
 	struct packet_reader *writer = data;
 
diff --git a/config.c b/config.c
index 758d6a5cc3b..60f8c0c666b 100644
--- a/config.c
+++ b/config.c
@@ -156,7 +156,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,
+			      struct key_value_info *kvi, void *data);
 
 #define MAX_INCLUDE_DEPTH 10
 static const char include_depth_advice[] = N_(
@@ -336,7 +337,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,
+			  struct key_value_info *kvi UNUSED, void *data)
 {
 	struct string_list *remote_urls = data;
 	const char *remote_name;
@@ -364,6 +366,7 @@ static void populate_remote_urls(struct config_include_data *inc)
 }
 
 static int forbid_remote_url(const char *var, const char *value UNUSED,
+			     struct key_value_info *kvi UNUSED,
 			     void *data UNUSED)
 {
 	const char *remote_name;
@@ -429,7 +432,8 @@ static int include_condition_is_true(struct key_value_info *kvi,
 
 static int kvi_fn(config_fn_t fn, const char *key, const char *value,
 		  struct key_value_info *kvi, void *data);
-static int git_config_include(const char *var, const char *value, void *data)
+static int git_config_include(const char *var, const char *value,
+			      struct key_value_info *kvi UNUSED, void *data)
 {
 	struct config_include_data *inc = data;
 	struct key_value_info *kvi = inc->config_reader->config_kvi;
@@ -1541,7 +1545,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,
+				   struct key_value_info *kvi, void *cb)
 {
 	/* This needs a better name */
 	if (!strcmp(var, "core.filemode")) {
@@ -1826,7 +1831,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,kvi, cb);
 }
 
 static int git_default_sparse_config(const char *var, const char *value)
@@ -1928,15 +1933,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,
+		       struct key_value_info *kvi, void *cb)
 {
 	if (starts_with(var, "core."))
-		return git_default_core_config(var, value, cb);
+		return git_default_core_config(var, value,kvi, 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,kvi, cb);
 
 	if (starts_with(var, "i18n."))
 		return git_default_i18n_config(var, value);
@@ -2455,7 +2461,8 @@ struct configset_add_data {
 };
 #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,
+			       struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct configset_add_data *data = cb;
 	configset_add_value(data->config_reader, data->config_set, key, value);
@@ -3063,7 +3070,8 @@ static int store_aux_event(enum config_event_t type, size_t begin, size_t end,
 	return 0;
 }
 
-static int store_aux(const char *key, const char *value, void *cb)
+static int store_aux(const char *key, const char *value,
+		     struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct config_store_data *store = cb;
 
diff --git a/config.h b/config.h
index 2c7b7399691..e4893e237e1 100644
--- a/config.h
+++ b/config.h
@@ -146,7 +146,8 @@ struct key_value_info {
  */
 typedef int (*config_fn_t)(const char *, const char *, struct key_value_info *, void *);
 
-int git_default_config(const char *, const char *, void *);
+int git_default_config(const char *, const char *, struct key_value_info *,
+		       void *);
 
 /**
  * Read a specific file in git-config format.
diff --git a/connect.c b/connect.c
index c0c8a38178c..72aad67a288 100644
--- a/connect.c
+++ b/connect.c
@@ -962,7 +962,7 @@ static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
 static char *git_proxy_command;
 
 static int git_proxy_command_options(const char *var, const char *value,
-		void *cb)
+		struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "core.gitproxy")) {
 		const char *for_pos;
@@ -1008,7 +1008,7 @@ static int git_proxy_command_options(const char *var, const char *value,
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int git_use_proxy(const char *host)
diff --git a/convert.c b/convert.c
index da06e2f51cb..a563380e7e7 100644
--- a/convert.c
+++ b/convert.c
@@ -1011,7 +1011,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
 	return 0;
 }
 
-static int read_convert_config(const char *var, const char *value, void *cb UNUSED)
+static int read_convert_config(const char *var, const char *value,
+			       struct key_value_info *kvi UNUSED,
+			       void *cb UNUSED)
 {
 	const char *key, *name;
 	size_t namelen;
diff --git a/credential.c b/credential.c
index e6417bf8804..6b9b8bfbb79 100644
--- a/credential.c
+++ b/credential.c
@@ -46,6 +46,7 @@ static int credential_from_potentially_partial_url(struct credential *c,
 						   const char *url);
 
 static int credential_config_callback(const char *var, const char *value,
+				      struct key_value_info *kvi UNUSED,
 				      void *data)
 {
 	struct credential *c = data;
diff --git a/delta-islands.c b/delta-islands.c
index 40f2ccfb550..404f9c07a42 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -341,7 +341,8 @@ static void free_remote_islands(kh_str_t *remote_islands)
 	kh_destroy_str(remote_islands);
 }
 
-static int island_config_callback(const char *k, const char *v, void *cb)
+static int island_config_callback(const char *k, const char *v,
+				  struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct island_load_data *ild = cb;
 
diff --git a/diff.c b/diff.c
index 78b0fdd8caa..d7ed2dc900b 100644
--- a/diff.c
+++ b/diff.c
@@ -350,7 +350,8 @@ static unsigned parse_color_moved_ws(const char *arg)
 	return ret;
 }
 
-int git_diff_ui_config(const char *var, const char *value, void *cb)
+int git_diff_ui_config(const char *var, const char *value,
+		       struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 		diff_use_color_default = git_config_colorbool(var, value);
@@ -433,10 +434,11 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value,kvi, cb);
 }
 
-int git_diff_basic_config(const char *var, const char *value, void *cb)
+int git_diff_basic_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb)
 {
 	const char *name;
 
@@ -488,7 +490,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
 	if (git_diff_heuristic_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static char *quote_two(const char *one, const char *two)
diff --git a/diff.h b/diff.h
index 6a0737b9c34..6a3efa63753 100644
--- a/diff.h
+++ b/diff.h
@@ -532,10 +532,12 @@ void free_diffstat_info(struct diffstat_t *diffstat);
 int parse_long_opt(const char *opt, const char **argv,
 		   const char **optarg);
 
-int git_diff_basic_config(const char *var, const char *value, void *cb);
+int git_diff_basic_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb);
 int git_diff_heuristic_config(const char *var, const char *value, void *cb);
 void init_diff_ui_defaults(void);
-int git_diff_ui_config(const char *var, const char *value, void *cb);
+int git_diff_ui_config(const char *var, const char *value,
+		       struct key_value_info *kvi, void *cb);
 void repo_diff_setup(struct repository *, struct diff_options *);
 struct option *add_diff_options(const struct option *, struct diff_options *);
 int diff_opt_parse(struct diff_options *, const char **, int, const char *);
diff --git a/fetch-pack.c b/fetch-pack.c
index 368f2ed25a1..c1072aa6cac 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1858,7 +1858,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	return ref;
 }
 
-static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
+static int fetch_pack_config_cb(const char *var, const char *value,
+				struct key_value_info *kvi, void *cb)
 {
 	if (strcmp(var, "fetch.fsck.skiplist") == 0) {
 		const char *path;
@@ -1880,7 +1881,7 @@ static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static void fetch_pack_config(void)
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 1886c92ddb9..97358034fa0 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -19,7 +19,8 @@ static int use_branch_desc;
 static int suppress_dest_pattern_seen;
 static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP;
 
-int fmt_merge_msg_config(const char *key, const char *value, void *cb)
+int fmt_merge_msg_config(const char *key, const char *value,
+			 struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
@@ -39,7 +40,7 @@ int fmt_merge_msg_config(const char *key, const char *value, void *cb)
 			string_list_append(&suppress_dest_patterns, value);
 		suppress_dest_pattern_seen = 1;
 	} else {
-		return git_default_config(key, value, cb);
+		return git_default_config(key, value,kvi, cb);
 	}
 	return 0;
 }
diff --git a/fmt-merge-msg.h b/fmt-merge-msg.h
index 99054042dc5..5262c39268a 100644
--- a/fmt-merge-msg.h
+++ b/fmt-merge-msg.h
@@ -13,7 +13,8 @@ struct fmt_merge_msg_opts {
 };
 
 extern int merge_log_config;
-int fmt_merge_msg_config(const char *key, const char *value, void *cb);
+int fmt_merge_msg_config(const char *key, const char *value,
+			 struct key_value_info *kvi, void *cb);
 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 		  struct fmt_merge_msg_opts *);
 
diff --git a/fsck.c b/fsck.c
index 4238344ed82..ec26857c79d 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1162,7 +1162,8 @@ struct fsck_gitmodules_data {
 	int ret;
 };
 
-static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
+static int fsck_gitmodules_fn(const char *var, const char *value,
+			      struct key_value_info *kvi UNUSED, void *vdata)
 {
 	struct fsck_gitmodules_data *data = vdata;
 	const char *subsection, *key;
@@ -1373,7 +1374,8 @@ int fsck_finish(struct fsck_options *options)
 	return ret;
 }
 
-int git_fsck_config(const char *var, const char *value, void *cb)
+int git_fsck_config(const char *var, const char *value,
+		    struct key_value_info *kvi, void *cb)
 {
 	struct fsck_options *options = cb;
 	if (strcmp(var, "fsck.skiplist") == 0) {
@@ -1394,7 +1396,7 @@ int git_fsck_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /*
diff --git a/fsck.h b/fsck.h
index e17730e9da9..a06f202576c 100644
--- a/fsck.h
+++ b/fsck.h
@@ -237,6 +237,7 @@ const char *fsck_describe_object(struct fsck_options *options,
  * git_config() callback for use by fsck-y tools that want to support
  * fsck.<msg> fsck.skipList etc.
  */
-int git_fsck_config(const char *var, const char *value, void *cb);
+int git_fsck_config(const char *var, const char *value,
+		    struct key_value_info *kvi, void *cb);
 
 #endif
diff --git a/gpg-interface.c b/gpg-interface.c
index aceeb083367..5ed89ef69ed 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -12,7 +12,8 @@
 #include "alias.h"
 #include "wrapper.h"
 
-static int git_gpg_config(const char *, const char *, void *);
+static int git_gpg_config(const char *, const char *, struct key_value_info *,
+			  void *);
 
 static void gpg_interface_lazy_init(void)
 {
@@ -718,7 +719,8 @@ void set_signing_key(const char *key)
 	configured_signing_key = xstrdup(key);
 }
 
-static int git_gpg_config(const char *var, const char *value, void *cb UNUSED)
+static int git_gpg_config(const char *var, const char *value,
+			  struct key_value_info *kvi UNUSED, void *cb UNUSED)
 {
 	struct gpg_format *fmt = NULL;
 	char *fmtname = NULL;
diff --git a/grep.c b/grep.c
index b86462a12a9..1516b0689d0 100644
--- a/grep.c
+++ b/grep.c
@@ -54,7 +54,8 @@ define_list_config_array_extra(color_grep_slots, {"match"});
  * Read the configuration file once and store it in
  * the grep_defaults template.
  */
-int grep_config(const char *var, const char *value, void *cb)
+int grep_config(const char *var, const char *value,
+		struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct grep_opt *opt = cb;
 	const char *slot;
diff --git a/help.c b/help.c
index 5d7637dce92..43d1eb702cd 100644
--- a/help.c
+++ b/help.c
@@ -309,7 +309,8 @@ void load_command_list(const char *prefix,
 	exclude_cmds(other_cmds, main_cmds);
 }
 
-static int get_colopts(const char *var, const char *value, void *data)
+static int get_colopts(const char *var, const char *value,
+		       struct key_value_info *kvi UNUSED, void *data)
 {
 	unsigned int *colopts = data;
 
@@ -459,7 +460,8 @@ void list_developer_interfaces_help(void)
 	putchar('\n');
 }
 
-static int get_alias(const char *var, const char *value, void *data)
+static int get_alias(const char *var, const char *value,
+		     struct key_value_info *kvi UNUSED, void *data)
 {
 	struct string_list *list = data;
 
@@ -543,6 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
+				  struct key_value_info *kvi UNUSED,
 				  void *cb UNUSED)
 {
 	const char *p;
diff --git a/ident.c b/ident.c
index 8fad92d7007..21b7b3ff35b 100644
--- a/ident.c
+++ b/ident.c
@@ -671,7 +671,8 @@ static int set_ident(const char *var, const char *value)
 	return 0;
 }
 
-int git_ident_config(const char *var, const char *value, void *data UNUSED)
+int git_ident_config(const char *var, const char *value,
+		     struct key_value_info *kvi UNUSED, void *data UNUSED)
 {
 	if (!strcmp(var, "user.useconfigonly")) {
 		ident_use_config_only = git_config_bool(var, value);
diff --git a/ident.h b/ident.h
index 96a64896a01..f55db41ff99 100644
--- a/ident.h
+++ b/ident.h
@@ -62,6 +62,7 @@ const char *fmt_name(enum want_ident);
 int committer_ident_sufficiently_given(void);
 int author_ident_sufficiently_given(void);
 
-int git_ident_config(const char *, const char *, void *);
+int git_ident_config(const char *, const char *,
+		     struct key_value_info *UNUSED, void *);
 
 #endif
diff --git a/imap-send.c b/imap-send.c
index a62424e90a4..3cc98f1a0a5 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1322,7 +1322,8 @@ static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
 	return 1;
 }
 
-static int git_imap_config(const char *var, const char *val, void *cb)
+static int git_imap_config(const char *var, const char *val,
+			   struct key_value_info *kvi, void *cb)
 {
 
 	if (!strcmp("imap.sslverify", var))
@@ -1356,7 +1357,7 @@ static int git_imap_config(const char *var, const char *val, void *cb)
 			server.host = xstrdup(val);
 		}
 	} else
-		return git_default_config(var, val, cb);
+		return git_default_config(var, val,kvi, cb);
 
 	return 0;
 }
diff --git a/ll-merge.c b/ll-merge.c
index 8be38d3bd41..2d240609ddf 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -252,6 +252,7 @@ static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
 static const char *default_ll_merge;
 
 static int read_merge_config(const char *var, const char *value,
+			     struct key_value_info *kvi UNUSED,
 			     void *cb UNUSED)
 {
 	struct ll_merge_driver *fn;
diff --git a/ls-refs.c b/ls-refs.c
index b9f3e08ec3d..90b902c84f8 100644
--- a/ls-refs.c
+++ b/ls-refs.c
@@ -136,7 +136,7 @@ static void send_possibly_unborn_head(struct ls_refs_data *data)
 }
 
 static int ls_refs_config(const char *var, const char *value,
-			  void *cb_data)
+			  struct key_value_info *kvi UNUSED, void *cb_data)
 {
 	struct ls_refs_data *data = cb_data;
 	/*
diff --git a/mailinfo.c b/mailinfo.c
index 2aeb20e5e62..e2f469c96f7 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -1241,12 +1241,13 @@ int mailinfo_parse_quoted_cr_action(const char *actionstr, int *action)
 	return 0;
 }
 
-static int git_mailinfo_config(const char *var, const char *value, void *mi_)
+static int git_mailinfo_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *mi_)
 {
 	struct mailinfo *mi = mi_;
 
 	if (!starts_with(var, "mailinfo."))
-		return git_default_config(var, value, NULL);
+		return git_default_config(var, value,kvi, NULL);
 	if (!strcmp(var, "mailinfo.scissors")) {
 		mi->use_scissors = git_config_bool(var, value);
 		return 0;
diff --git a/notes-utils.c b/notes-utils.c
index cb88171b7bb..f7a0ec60731 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -93,7 +93,8 @@ static combine_notes_fn parse_combine_notes_fn(const char *v)
 		return NULL;
 }
 
-static int notes_rewrite_config(const char *k, const char *v, void *cb)
+static int notes_rewrite_config(const char *k, const char *v,
+				struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct notes_rewrite_cfg *c = cb;
 	if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
diff --git a/notes.c b/notes.c
index 45fb7f22d1d..89b84ea0869 100644
--- a/notes.c
+++ b/notes.c
@@ -973,7 +973,8 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
 	free(globs_copy);
 }
 
-static int notes_display_config(const char *k, const char *v, void *cb)
+static int notes_display_config(const char *k, const char *v,
+				struct key_value_info *kvi UNUSED, void *cb)
 {
 	int *load_refs = cb;
 
diff --git a/pager.c b/pager.c
index b66bbff2785..4b77e0d2e57 100644
--- a/pager.c
+++ b/pager.c
@@ -39,6 +39,7 @@ static void wait_for_pager_signal(int signo)
 }
 
 static int core_pager_config(const char *var, const char *value,
+			     struct key_value_info *kvi UNUSED,
 			     void *data UNUSED)
 {
 	if (!strcmp(var, "core.pager"))
@@ -224,7 +225,9 @@ struct pager_command_config_data {
 	char *value;
 };
 
-static int pager_command_config(const char *var, const char *value, void *vdata)
+static int pager_command_config(const char *var, const char *value,
+				struct key_value_info *kvi UNUSED,
+				void *vdata)
 {
 	struct pager_command_config_data *data = vdata;
 	const char *cmd;
diff --git a/pretty.c b/pretty.c
index 76fc4f61e40..7f57379c44e 100644
--- a/pretty.c
+++ b/pretty.c
@@ -55,6 +55,7 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
 }
 
 static int git_pretty_formats_config(const char *var, const char *value,
+				     struct key_value_info *kvi UNUSED,
 				     void *cb UNUSED)
 {
 	struct cmt_fmt_map *commit_format = NULL;
diff --git a/promisor-remote.c b/promisor-remote.c
index a8dbb788e8f..ea68df91209 100644
--- a/promisor-remote.c
+++ b/promisor-remote.c
@@ -99,7 +99,9 @@ static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
 	config->promisors_tail = &r->next;
 }
 
-static int promisor_remote_config(const char *var, const char *value, void *data)
+static int promisor_remote_config(const char *var, const char *value,
+				  struct key_value_info *kvi UNUSED,
+				  void *data)
 {
 	struct promisor_remote_config *config = data;
 	const char *name;
diff --git a/remote.c b/remote.c
index 3a831cb5304..10868a963f2 100644
--- a/remote.c
+++ b/remote.c
@@ -348,7 +348,8 @@ static void read_branches_file(struct remote_state *remote_state,
 	remote->fetch_tags = 1; /* always auto-follow */
 }
 
-static int handle_config(const char *key, const char *value, void *cb)
+static int handle_config(const char *key, const char *value,
+			 struct key_value_info *kvi UNUSED, void *cb)
 {
 	const char *name;
 	size_t namelen;
diff --git a/revision.c b/revision.c
index 106ca1ce6c9..6de0132d719 100644
--- a/revision.c
+++ b/revision.c
@@ -1569,7 +1569,8 @@ struct exclude_hidden_refs_cb {
 	const char *section;
 };
 
-static int hide_refs_config(const char *var, const char *value, void *cb_data)
+static int hide_refs_config(const char *var, const char *value,
+			    struct key_value_info *kvi UNUSED, void *cb_data)
 {
 	struct exclude_hidden_refs_cb *cb = cb_data;
 	cb->exclusions->hidden_refs_configured = 1;
diff --git a/scalar.c b/scalar.c
index de07c37d210..1c44df2ec7a 100644
--- a/scalar.c
+++ b/scalar.c
@@ -593,7 +593,8 @@ static int cmd_register(int argc, const char **argv)
 	return register_dir();
 }
 
-static int get_scalar_repos(const char *key, const char *value, void *data)
+static int get_scalar_repos(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *data)
 {
 	struct string_list *list = data;
 
diff --git a/sequencer.c b/sequencer.c
index 6985ca526ae..171561c2cdb 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -214,7 +214,8 @@ static struct update_ref_record *init_update_ref_record(const char *ref)
 	return rec;
 }
 
-static int git_sequencer_config(const char *k, const char *v, void *cb)
+static int git_sequencer_config(const char *k, const char *v,
+				struct key_value_info *kvi, void *cb)
 {
 	struct replay_opts *opts = cb;
 	int status;
@@ -269,7 +270,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
 	if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
 		opts->commit_use_reference = git_config_bool(k, v);
 
-	return git_diff_basic_config(k, v, NULL);
+	return git_diff_basic_config(k, v,kvi, NULL);
 }
 
 void sequencer_init_config(struct replay_opts *opts)
@@ -2876,7 +2877,8 @@ static int git_config_string_dup(char **dest,
 	return 0;
 }
 
-static int populate_opts_cb(const char *key, const char *value, void *data)
+static int populate_opts_cb(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *data)
 {
 	struct replay_opts *opts = data;
 	int error_flag = 1;
diff --git a/setup.c b/setup.c
index 6c5b85e96c1..a461dd15233 100644
--- a/setup.c
+++ b/setup.c
@@ -514,7 +514,9 @@ no_prevention_needed:
 	startup_info->original_cwd = NULL;
 }
 
-static int read_worktree_config(const char *var, const char *value, void *vdata)
+static int read_worktree_config(const char *var, const char *value,
+				struct key_value_info *kvi UNUSED,
+				void *vdata)
 {
 	struct repository_format *data = vdata;
 
@@ -585,7 +587,8 @@ static enum extension_result handle_extension(const char *var,
 	return EXTENSION_UNKNOWN;
 }
 
-static int check_repo_format(const char *var, const char *value, void *vdata)
+static int check_repo_format(const char *var, const char *value,
+			     struct key_value_info *kvi, void *vdata)
 {
 	struct repository_format *data = vdata;
 	const char *ext;
@@ -614,7 +617,7 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
 		}
 	}
 
-	return read_worktree_config(var, value, vdata);
+	return read_worktree_config(var, value,kvi, vdata);
 }
 
 static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
@@ -1113,7 +1116,8 @@ struct safe_directory_data {
 	int is_safe;
 };
 
-static int safe_directory_cb(const char *key, const char *value, void *d)
+static int safe_directory_cb(const char *key, const char *value,
+			     struct key_value_info *kvi UNUSED, void *d)
 {
 	struct safe_directory_data *data = d;
 
@@ -1169,7 +1173,8 @@ static int ensure_valid_ownership(const char *gitfile,
 	return data.is_safe;
 }
 
-static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
+static int allowed_bare_repo_cb(const char *key, const char *value,
+				struct key_value_info *kvi UNUSED, void *d)
 {
 	enum allowed_bare_repo *allowed_bare_repo = d;
 
diff --git a/submodule-config.c b/submodule-config.c
index c2f71f0b2e3..522c9cd3213 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -424,7 +424,8 @@ struct parse_config_parameter {
  * config store (.git/config, etc).  Callers are responsible for
  * checking for overrides in the main config store when appropriate.
  */
-static int parse_config(const char *var, const char *value, void *data)
+static int parse_config(const char *var, const char *value,
+			struct key_value_info *kvi UNUSED, void *data)
 {
 	struct parse_config_parameter *me = data;
 	struct submodule *submodule;
@@ -673,7 +674,8 @@ out:
 	}
 }
 
-static int gitmodules_cb(const char *var, const char *value, void *data)
+static int gitmodules_cb(const char *var, const char *value,
+			 struct key_value_info *kvi UNUSED, void *data)
 {
 	struct repository *repo = data;
 	struct parse_config_parameter parameter;
@@ -801,7 +803,9 @@ void submodule_free(struct repository *r)
 		submodule_cache_clear(r->submodule_cache);
 }
 
-static int config_print_callback(const char *var, const char *value, void *cb_data)
+static int config_print_callback(const char *var, const char *value,
+				 struct key_value_info *kvi UNUSED,
+				 void *cb_data)
 {
 	char *wanted_key = cb_data;
 
@@ -843,7 +847,9 @@ struct fetch_config {
 	int *recurse_submodules;
 };
 
-static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
+static int gitmodules_fetch_config(const char *var, const char *value,
+				   struct key_value_info *kvi UNUSED,
+				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
@@ -871,6 +877,7 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
+					  struct key_value_info *kvi UNUSED,
 					  void *cb)
 {
 	int *max_jobs = cb;
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index ad78fc17683..00cd49e5145 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -42,7 +42,8 @@
  *
  */
 
-static int iterate_cb(const char *var, const char *value, void *data UNUSED)
+static int iterate_cb(const char *var, const char *value,
+		      struct key_value_info *kvi UNUSED, void *data UNUSED)
 {
 	static int nr;
 
@@ -59,7 +60,8 @@ static int iterate_cb(const char *var, const char *value, void *data UNUSED)
 	return 0;
 }
 
-static int parse_int_cb(const char *var, const char *value, void *data)
+static int parse_int_cb(const char *var, const char *value,
+			struct key_value_info *kvi UNUSED, void *data)
 {
 	const char *key_to_match = data;
 
@@ -70,7 +72,8 @@ static int parse_int_cb(const char *var, const char *value, void *data)
 	return 0;
 }
 
-static int early_config_cb(const char *var, const char *value, void *vdata)
+static int early_config_cb(const char *var, const char *value,
+			   struct key_value_info *kvi UNUSED, void *vdata)
 {
 	const char *key = vdata;
 
diff --git a/t/helper/test-userdiff.c b/t/helper/test-userdiff.c
index 680124a6760..33dd3a65241 100644
--- a/t/helper/test-userdiff.c
+++ b/t/helper/test-userdiff.c
@@ -12,7 +12,9 @@ static int driver_cb(struct userdiff_driver *driver,
 	return 0;
 }
 
-static int cmd__userdiff_config(const char *var, const char *value, void *cb UNUSED)
+static int cmd__userdiff_config(const char *var, const char *value,
+				struct key_value_info *kvi UNUSED,
+				void *cb UNUSED)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 78cfc15d52d..6871258d468 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -99,7 +99,8 @@ struct tr2_cfg_data {
 /*
  * See if the given config key matches any of our patterns of interest.
  */
-static int tr2_cfg_cb(const char *key, const char *value, void *d)
+static int tr2_cfg_cb(const char *key, const char *value,
+		      struct key_value_info *kvi UNUSED, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
diff --git a/trace2/tr2_sysenv.c b/trace2/tr2_sysenv.c
index 069786cb927..d17a08b0f50 100644
--- a/trace2/tr2_sysenv.c
+++ b/trace2/tr2_sysenv.c
@@ -57,7 +57,8 @@ static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
 };
 /* clang-format on */
 
-static int tr2_sysenv_cb(const char *key, const char *value, void *d)
+static int tr2_sysenv_cb(const char *key, const char *value,
+			 struct key_value_info *kvi UNUSED, void *d)
 {
 	int k;
 
diff --git a/trailer.c b/trailer.c
index a2c3ed6f28c..fac7e2cfe6e 100644
--- a/trailer.c
+++ b/trailer.c
@@ -482,6 +482,7 @@ static struct {
 };
 
 static int git_trailer_default_config(const char *conf_key, const char *value,
+				      struct key_value_info *kvi UNUSED,
 				      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
@@ -514,6 +515,7 @@ static int git_trailer_default_config(const char *conf_key, const char *value,
 }
 
 static int git_trailer_config(const char *conf_key, const char *value,
+			      struct key_value_info *kvi UNUSED,
 			      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
diff --git a/upload-pack.c b/upload-pack.c
index e23f16dfdd2..5f8232ff078 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1298,7 +1298,9 @@ static int parse_object_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int upload_pack_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_config(const char *var, const char *value,
+			      struct key_value_info *kvi UNUSED,
+			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
@@ -1339,7 +1341,9 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
 }
 
-static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_protected_config(const char *var, const char *value,
+					struct key_value_info *kvi UNUSED,
+					void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
diff --git a/urlmatch.c b/urlmatch.c
index eba0bdd77fe..47683974d8c 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -551,7 +551,8 @@ static int cmp_matches(const struct urlmatch_item *a,
 	return 0;
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb)
+int urlmatch_config_entry(const char *var, const char *value,
+			  struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct string_list_item *item;
 	struct urlmatch_config *collect = cb;
diff --git a/urlmatch.h b/urlmatch.h
index bee374a642c..f6eac4af9ea 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -71,7 +71,8 @@ struct urlmatch_config {
 	.vars = STRING_LIST_INIT_DUP, \
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb);
+int urlmatch_config_entry(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb);
 void urlmatch_config_release(struct urlmatch_config *config);
 
 #endif /* URL_MATCH_H */
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 0460e03f5ed..f1aac104285 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -307,7 +307,8 @@ int xdiff_compare_lines(const char *l1, long s1,
 
 int git_xmerge_style = -1;
 
-int git_xmerge_config(const char *var, const char *value, void *cb)
+int git_xmerge_config(const char *var, const char *value,
+		      struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "merge.conflictstyle")) {
 		if (!value)
@@ -327,5 +328,5 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
 			    value, var);
 		return 0;
 	}
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
diff --git a/xdiff-interface.h b/xdiff-interface.h
index 3750794afe9..c1676b11702 100644
--- a/xdiff-interface.h
+++ b/xdiff-interface.h
@@ -50,7 +50,8 @@ int buffer_is_binary(const char *ptr, unsigned long size);
 
 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
 void xdiff_clear_find_func(xdemitconf_t *xecfg);
-int git_xmerge_config(const char *var, const char *value, void *cb);
+int git_xmerge_config(const char *var, const char *value,
+		      struct key_value_info *kvi, void *cb);
 extern int git_xmerge_style;
 
 /*
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (8 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 09/14] (RFC-only) config: apply cocci to config_fn_t implementations Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-05-01 11:19   ` Ævar Arnfjörð Bjarmason
  2023-04-21 19:13 ` [PATCH 11/14] config: remove current_config_(line|name|origin_type) Glen Choo via GitGitGadget
                   ` (4 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Here's an exhaustive list of all of the changes:

* Cases that need a judgement call

  - trace2/tr2_cfg.c:tr2_cfg_set_fl()

    This function needs to account for tr2_cfg_cb() now using "kvi".
    Since this is only called (indirectly) by git_config_set(), config
    source information has never been available here, so just pass NULL.
    It will be tr2_cfg_cb()'s responsibility to not use "kvi".

  - builtin/checkout.c:checkout_main()

    This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
    "kvi" doesn't apply, so just pass NULL. This might be worth
    refactoring away, since git_xmerge_config() can call
    git_default_config().

  - config.c:git_config_include()

    Replace the local "kvi" variable with the "kvi" parameter. This
    makes config_include_data.config_reader obsolete, so remove it.

* Hard for cocci to catch

  - urlmatch.c

    Manually refactor the custom config callbacks in "struct
    urlmatch_config".

  - diff.h, fsck.h, grep.h, ident.h, xdiff-interface.h

    "struct key_value_info" hasn't been defined yet, so forward declare
    it. Alternatively, maybe these files should "#include config.h".

* Likely deficiencies in .cocci patch

  - submodule-config.c:gitmodules_cb()

    Manually refactor a parse_config() call that gets missed because it
    uses a different "*data" arg.

  - grep.h, various

    Manually refactor grep_config() calls. Not sure why these don't get
    picked up.

  - config.c:git_config_include(), http.c:http_options()

    Manually add "kvi" where it was missed. Not sure why they get missed.

  - builtin/clone.c:write_one_config()

    Manually refactor a git_clone_config() call. Probably got missed
    because I didn't include git_config_parse_parameter().

  - ident.h

    Remove the UNUSED attribute. Not sure why this is the only instance
    of this.

  - git-compat-util.h, compat/mingw.[h|c]

    Manually refactor noop_core_config(), platform_core_config() and
    mingw_core_config(). I can probably add these as "manual fixups" in
    cocci.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/checkout.c | 2 +-
 builtin/clone.c    | 4 ++--
 builtin/grep.c     | 2 +-
 compat/mingw.c     | 3 ++-
 compat/mingw.h     | 4 +++-
 config.c           | 5 +----
 diff.h             | 1 +
 fsck.h             | 1 +
 git-compat-util.h  | 2 ++
 grep.c             | 6 +++---
 grep.h             | 4 +++-
 http.c             | 5 +++--
 ident.h            | 3 ++-
 submodule-config.c | 4 ++--
 trace2/tr2_cfg.c   | 2 +-
 urlmatch.c         | 6 +++---
 xdiff-interface.h  | 2 ++
 17 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 92017ba6696..9641423dc2f 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1687,7 +1687,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 
 	if (opts->conflict_style) {
 		opts->merge = 1; /* implied */
-		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
+		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL, NULL);
 	}
 	if (opts->force) {
 		opts->discard_changes = 1;
diff --git a/builtin/clone.c b/builtin/clone.c
index 1e1cf104194..e654757c45d 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -791,14 +791,14 @@ static int git_clone_config(const char *k, const char *v,
 }
 
 static int write_one_config(const char *key, const char *value,
-			    struct key_value_info *kvi UNUSED, void *data)
+			    struct key_value_info *kvi, void *data)
 {
 	/*
 	 * give git_clone_config a chance to write config values back to the
 	 * environment, since git_config_set_multivar_gently only deals with
 	 * config-file writes
 	 */
-	int apply_failed = git_clone_config(key, value, data);
+	int apply_failed = git_clone_config(key, value, kvi, data);
 	if (apply_failed)
 		return apply_failed;
 
diff --git a/builtin/grep.c b/builtin/grep.c
index 177befc3ed4..6e795f9f3a2 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -290,7 +290,7 @@ static int wait_all(void)
 static int grep_cmd_config(const char *var, const char *value,
 			   struct key_value_info *kvi, void *cb)
 {
-	int st = grep_config(var, value, cb);
+	int st = grep_config(var, value, kvi, cb);
 
 	if (git_color_config(var, value, cb) < 0)
 		st = -1;
diff --git a/compat/mingw.c b/compat/mingw.c
index 94c5a1daa40..c8181469a2f 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -242,7 +242,8 @@ static int core_restrict_inherited_handles = -1;
 static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
 static char *unset_environment_variables;
 
-int mingw_core_config(const char *var, const char *value, void *cb)
+int mingw_core_config(const char *var, const char *value,
+		      struct key_value_info *kvi UNUSED, void *cb)
 {
 	if (!strcmp(var, "core.hidedotfiles")) {
 		if (value && !strcasecmp(value, "dotgitonly"))
diff --git a/compat/mingw.h b/compat/mingw.h
index 209cf7cebad..4f2b489b883 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -11,7 +11,9 @@ typedef _sigset_t sigset_t;
 #undef _POSIX_THREAD_SAFE_FUNCTIONS
 #endif
 
-int mingw_core_config(const char *var, const char *value, void *cb);
+struct key_value_info;
+int mingw_core_config(const char *var, const char *value,
+		      struct key_value_info *, void *cb);
 #define platform_core_config mingw_core_config
 
 /*
diff --git a/config.c b/config.c
index 60f8c0c666b..aa183f6f244 100644
--- a/config.c
+++ b/config.c
@@ -147,7 +147,6 @@ struct config_include_data {
 	void *data;
 	const struct config_options *opts;
 	struct git_config_source *config_source;
-	struct config_reader *config_reader;
 
 	/*
 	 * All remote URLs discovered when reading all config files.
@@ -433,10 +432,9 @@ static int include_condition_is_true(struct key_value_info *kvi,
 static int kvi_fn(config_fn_t fn, const char *key, const char *value,
 		  struct key_value_info *kvi, void *data);
 static int git_config_include(const char *var, const char *value,
-			      struct key_value_info *kvi UNUSED, void *data)
+			      struct key_value_info *kvi, void *data)
 {
 	struct config_include_data *inc = data;
-	struct key_value_info *kvi = inc->config_reader->config_kvi;
 	const char *cond, *key;
 	size_t cond_len;
 	int ret;
@@ -2252,7 +2250,6 @@ int config_with_options(config_fn_t fn, void *data,
 		inc.data = data;
 		inc.opts = opts;
 		inc.config_source = config_source;
-		inc.config_reader = &the_reader;
 		fn = git_config_include;
 		data = &inc;
 	}
diff --git a/diff.h b/diff.h
index 6a3efa63753..2ceb0fd2d66 100644
--- a/diff.h
+++ b/diff.h
@@ -532,6 +532,7 @@ void free_diffstat_info(struct diffstat_t *diffstat);
 int parse_long_opt(const char *opt, const char **argv,
 		   const char **optarg);
 
+struct key_value_info;
 int git_diff_basic_config(const char *var, const char *value,
 			  struct key_value_info *kvi, void *cb);
 int git_diff_heuristic_config(const char *var, const char *value, void *cb);
diff --git a/fsck.h b/fsck.h
index a06f202576c..914e67a067d 100644
--- a/fsck.h
+++ b/fsck.h
@@ -233,6 +233,7 @@ void fsck_put_object_name(struct fsck_options *options,
 const char *fsck_describe_object(struct fsck_options *options,
 				 const struct object_id *oid);
 
+struct key_value_info;
 /*
  * git_config() callback for use by fsck-y tools that want to support
  * fsck.<msg> fsck.skipList etc.
diff --git a/git-compat-util.h b/git-compat-util.h
index 4a200a9fb41..6812b592c15 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -440,8 +440,10 @@ typedef uintmax_t timestamp_t;
 #endif
 
 #ifndef platform_core_config
+struct key_value_info;
 static inline int noop_core_config(const char *var UNUSED,
 				   const char *value UNUSED,
+				   struct key_value_info *kvi UNUSED,
 				   void *cb UNUSED)
 {
 	return 0;
diff --git a/grep.c b/grep.c
index 1516b0689d0..2d3b9bf5d92 100644
--- a/grep.c
+++ b/grep.c
@@ -55,7 +55,7 @@ define_list_config_array_extra(color_grep_slots, {"match"});
  * the grep_defaults template.
  */
 int grep_config(const char *var, const char *value,
-		struct key_value_info *kvi UNUSED, void *cb)
+		struct key_value_info *kvi, void *cb)
 {
 	struct grep_opt *opt = cb;
 	const char *slot;
@@ -90,9 +90,9 @@ int grep_config(const char *var, const char *value,
 	if (!strcmp(var, "color.grep"))
 		opt->color = git_config_colorbool(var, value);
 	if (!strcmp(var, "color.grep.match")) {
-		if (grep_config("color.grep.matchcontext", value, cb) < 0)
+		if (grep_config("color.grep.matchcontext", value, kvi, cb) < 0)
 			return -1;
-		if (grep_config("color.grep.matchselected", value, cb) < 0)
+		if (grep_config("color.grep.matchselected", value, kvi, cb) < 0)
 			return -1;
 	} else if (skip_prefix(var, "color.grep.", &slot)) {
 		int i = LOOKUP_CONFIG(color_grep_slots, slot);
diff --git a/grep.h b/grep.h
index c59592e3bdb..6d2fb0ada54 100644
--- a/grep.h
+++ b/grep.h
@@ -202,7 +202,9 @@ struct grep_opt {
 	.output = std_output, \
 }
 
-int grep_config(const char *var, const char *value, void *);
+struct key_value_info;
+int grep_config(const char *var, const char *value, struct key_value_info *kvi,
+		void *);
 void grep_init(struct grep_opt *, struct repository *repo);
 
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
diff --git a/http.c b/http.c
index d5d82c5230f..3d4292eba6a 100644
--- a/http.c
+++ b/http.c
@@ -361,7 +361,8 @@ static void process_curl_messages(void)
 	}
 }
 
-static int http_options(const char *var, const char *value, void *cb)
+static int http_options(const char *var, const char *value,
+			struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp("http.version", var)) {
 		return git_config_string(&curl_http_version, var, value);
@@ -532,7 +533,7 @@ static int http_options(const char *var, const char *value, void *cb)
 	}
 
 	/* Fall back on the default ones */
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, kvi, cb);
 }
 
 static int curl_empty_auth_enabled(void)
diff --git a/ident.h b/ident.h
index f55db41ff99..4e3bd347c52 100644
--- a/ident.h
+++ b/ident.h
@@ -62,7 +62,8 @@ const char *fmt_name(enum want_ident);
 int committer_ident_sufficiently_given(void);
 int author_ident_sufficiently_given(void);
 
+struct key_value_info;
 int git_ident_config(const char *, const char *,
-		     struct key_value_info *UNUSED, void *);
+		     struct key_value_info *, void *);
 
 #endif
diff --git a/submodule-config.c b/submodule-config.c
index 522c9cd3213..7d773f33621 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -675,7 +675,7 @@ out:
 }
 
 static int gitmodules_cb(const char *var, const char *value,
-			 struct key_value_info *kvi UNUSED, void *data)
+			 struct key_value_info *kvi, void *data)
 {
 	struct repository *repo = data;
 	struct parse_config_parameter parameter;
@@ -685,7 +685,7 @@ static int gitmodules_cb(const char *var, const char *value,
 	parameter.gitmodules_oid = null_oid();
 	parameter.overwrite = 1;
 
-	return parse_config(var, value, &parameter);
+	return parse_config(var, value, kvi, &parameter);
 }
 
 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 6871258d468..8ed139c69f4 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -146,5 +146,5 @@ void tr2_cfg_set_fl(const char *file, int line, const char *key,
 	struct tr2_cfg_data data = { file, line };
 
 	if (tr2_cfg_load_patterns() > 0)
-		tr2_cfg_cb(key, value, &data);
+		tr2_cfg_cb(key, value, NULL, &data);
 }
diff --git a/urlmatch.c b/urlmatch.c
index 47683974d8c..c94500b61b3 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -552,7 +552,7 @@ static int cmp_matches(const struct urlmatch_item *a,
 }
 
 int urlmatch_config_entry(const char *var, const char *value,
-			  struct key_value_info *kvi UNUSED, void *cb)
+			  struct key_value_info *kvi, void *cb)
 {
 	struct string_list_item *item;
 	struct urlmatch_config *collect = cb;
@@ -566,7 +566,7 @@ int urlmatch_config_entry(const char *var, const char *value,
 
 	if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
 		if (collect->cascade_fn)
-			return collect->cascade_fn(var, value, cb);
+			return collect->cascade_fn(var, value, kvi, cb);
 		return 0; /* not interested */
 	}
 	dot = strrchr(key, '.');
@@ -610,7 +610,7 @@ int urlmatch_config_entry(const char *var, const char *value,
 	strbuf_addstr(&synthkey, collect->section);
 	strbuf_addch(&synthkey, '.');
 	strbuf_addstr(&synthkey, key);
-	retval = collect->collect_fn(synthkey.buf, value, collect->cb);
+	retval = collect->collect_fn(synthkey.buf, value, kvi, collect->cb);
 
 	strbuf_release(&synthkey);
 	return retval;
diff --git a/xdiff-interface.h b/xdiff-interface.h
index c1676b11702..749cdf77579 100644
--- a/xdiff-interface.h
+++ b/xdiff-interface.h
@@ -50,6 +50,8 @@ int buffer_is_binary(const char *ptr, unsigned long size);
 
 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
 void xdiff_clear_find_func(xdemitconf_t *xecfg);
+
+struct key_value_info;
 int git_xmerge_config(const char *var, const char *value,
 		      struct key_value_info *kvi, void *cb);
 extern int git_xmerge_style;
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 11/14] config: remove current_config_(line|name|origin_type)
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (9 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 12/14] config: remove current_config_scope() Glen Choo via GitGitGadget
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Trivially replace current_config_(line|name|origin_type) by reading the
corresponding values from "struct key_value_info". This includes some
light "kvi" plumbing for builtin/config.c, and for *origin_type,
splitting out a function that turns "enum config_origin_type" into the
human-readable string that callbacks actually want.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/config.c       | 25 +++++++++++++------------
 builtin/remote.c       |  6 +++---
 config.c               | 23 +----------------------
 config.h               |  4 +---
 t/helper/test-config.c |  8 ++++----
 5 files changed, 22 insertions(+), 44 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index b2ad7351d0a..2ffa25139c6 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -191,16 +191,16 @@ 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(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);
 }
 
@@ -214,14 +214,14 @@ static void show_config_scope(struct strbuf *buf)
 }
 
 static int show_all_config(const char *key_, const char *value_,
-			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
+			   struct key_value_info *kvi, void *cb UNUSED)
 {
 	if (show_origin || show_scope) {
 		struct strbuf buf = STRBUF_INIT;
 		if (show_scope)
 			show_config_scope(&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);
@@ -239,12 +239,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_,
+			 struct key_value_info *kvi)
 {
 	if (show_scope)
 		show_config_scope(buf);
 	if (show_origin)
-		show_config_origin(buf);
+		show_config_origin(kvi, buf);
 	if (show_keys)
 		strbuf_addstr(buf, key_);
 	if (!omit_values) {
@@ -299,7 +300,7 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
 }
 
 static int collect_config(const char *key_, const char *value_,
-			  struct key_value_info *kvi UNUSED, void *cb)
+			  struct key_value_info *kvi, void *cb)
 {
 	struct strbuf_list *values = cb;
 
@@ -316,7 +317,7 @@ static int collect_config(const char *key_, const char *value_,
 	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,7 +382,7 @@ static int get_value(const char *key_, const char *regex_, unsigned flags)
 		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, NULL) < 0)
 			die(_("failed to format default config value: %s"),
 				default_value);
 	}
@@ -618,7 +619,7 @@ 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, NULL);
 		fwrite(buf.buf, 1, buf.len, stdout);
 		strbuf_release(&buf);
 
diff --git a/builtin/remote.c b/builtin/remote.c
index edb4a9ddd7f..fc4ea993ebb 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -646,7 +646,7 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	struct key_value_info *kvi UNUSED, void *cb)
+	struct key_value_info *kvi, void *cb)
 {
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
@@ -655,8 +655,8 @@ static int config_read_push_default(const char *key, const char *value,
 
 	info->scope = current_config_scope();
 	strbuf_reset(&info->origin);
-	strbuf_addstr(&info->origin, current_config_name());
-	info->linenr = current_config_line();
+	strbuf_addstr(&info->origin, kvi->filename);
+	info->linenr = kvi->linenr;
 
 	return 0;
 }
diff --git a/config.c b/config.c
index aa183f6f244..0a5443243dc 100644
--- a/config.c
+++ b/config.c
@@ -3917,13 +3917,8 @@ static int reader_origin_type(struct config_reader *reader,
 	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";
@@ -3969,14 +3964,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	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)
@@ -3991,14 +3978,6 @@ enum config_scope current_config_scope(void)
 		return CONFIG_SCOPE_UNKNOWN;
 }
 
-int current_config_line(void)
-{
-	if (the_reader.config_kvi)
-		return the_reader.config_kvi->linenr;
-	else
-		BUG("current_config_line called outside config callback");
-}
-
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index e4893e237e1..fb16c4aca77 100644
--- a/config.h
+++ b/config.h
@@ -375,9 +375,7 @@ void git_global_config(char **user, char **xdg);
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
 enum config_scope current_config_scope(void);
-const char *current_config_origin_type(void);
-const char *current_config_name(void);
-int current_config_line(void);
+const char *config_origin_type_name(enum config_origin_type type);
 
 /*
  * Match and parse a config key of the form:
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 00cd49e5145..337587df41d 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -43,7 +43,7 @@
  */
 
 static int iterate_cb(const char *var, const char *value,
-		      struct key_value_info *kvi UNUSED, void *data UNUSED)
+		      struct key_value_info *kvi, void *data UNUSED)
 {
 	static int nr;
 
@@ -52,9 +52,9 @@ static int iterate_cb(const char *var, const char *value,
 
 	printf("key=%s\n", var);
 	printf("value=%s\n", value ? value : "(null)");
-	printf("origin=%s\n", current_config_origin_type());
-	printf("name=%s\n", current_config_name());
-	printf("lno=%d\n", current_config_line());
+	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
+	printf("name=%s\n", kvi->filename ? kvi->filename : "");
+	printf("lno=%d\n", kvi->linenr);
 	printf("scope=%s\n", config_scope_name(current_config_scope()));
 
 	return 0;
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 12/14] config: remove current_config_scope()
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (10 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 11/14] config: remove current_config_(line|name|origin_type) Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 13/14] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Replace current_config_scope() by reading the corresponding value off
"struct key_value_info".

Most instances of this are trivial, except for the trace2/* files. There
is a code path starting from trace2_def_param_fl() that eventually calls
current_config_scope(), and thus it needs to have "kvi" plumbed through
it. Additional plumbing is also needed to get "kvi" to
trace2_def_param_fl(), which gets called by two code paths:

- Through tr2_cfg_cb(), which is a config callback, so it trivially
  receives "kvi".

- Through tr2_list_env_vars_fl(), which is a high level function that
  lists environment variables for tracing. This has been secretly
  behaving like git_config_from_parameters() (in that it parses config
  from environment variables/the CLI), but does not set config source
  information.

  Teach tr2_list_env_vars_fl() to be well-behaved by using
  kvi_from_param(), which is already used internally by config.c for
  CLI/environment variable-based config.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/config.c        |  8 ++++----
 builtin/remote.c        |  2 +-
 config.c                | 16 +---------------
 config.h                |  3 ++-
 remote.c                |  6 +++---
 t/helper/test-config.c  |  2 +-
 trace2.c                |  4 ++--
 trace2.h                |  3 ++-
 trace2/tr2_cfg.c        |  9 ++++++---
 trace2/tr2_tgt.h        |  4 +++-
 trace2/tr2_tgt_event.c  |  4 ++--
 trace2/tr2_tgt_normal.c |  4 ++--
 trace2/tr2_tgt_perf.c   |  4 ++--
 13 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index 2ffa25139c6..7899add68fb 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -204,10 +204,10 @@ static void show_config_origin(struct key_value_info *kvi, struct strbuf *buf)
 	strbuf_addch(buf, term);
 }
 
-static void show_config_scope(struct strbuf *buf)
+static void show_config_scope(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);
@@ -219,7 +219,7 @@ static int show_all_config(const char *key_, const char *value_,
 	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(kvi, &buf);
 		/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
@@ -243,7 +243,7 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
 			 struct key_value_info *kvi)
 {
 	if (show_scope)
-		show_config_scope(buf);
+		show_config_scope(kvi, buf);
 	if (show_origin)
 		show_config_origin(kvi, buf);
 	if (show_keys)
diff --git a/builtin/remote.c b/builtin/remote.c
index fc4ea993ebb..034998a1205 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -653,7 +653,7 @@ static int config_read_push_default(const char *key, const char *value,
 	    !value || strcmp(value, info->old_name))
 		return 0;
 
-	info->scope = current_config_scope();
+	info->scope = kvi->scope;
 	strbuf_reset(&info->origin);
 	strbuf_addstr(&info->origin, kvi->filename);
 	info->linenr = kvi->linenr;
diff --git a/config.c b/config.c
index 0a5443243dc..68c9b507a4d 100644
--- a/config.c
+++ b/config.c
@@ -643,7 +643,7 @@ static int config_parse_pair(const char *key, const char *value,
 
 
 /* for values read from `git_config_from_parameters()` */
-static void kvi_from_param(struct key_value_info *out)
+void kvi_from_param(struct key_value_info *out)
 {
 	out->filename = NULL;
 	out->linenr = -1;
@@ -3964,20 +3964,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	return 0;
 }
 
-enum config_scope current_config_scope(void)
-{
-	if (the_reader.config_kvi)
-		return the_reader.config_kvi->scope;
-	else
-		/*
-		 * FIXME This should be a BUG, but tr2_list_env_vars_fl is
-		 * calling this outside of a config callback. This will be
-		 * easier to fix when we plumb kvi through the config callbacks,
-		 * so leave this untouched for now.
-		 */
-		return CONFIG_SCOPE_UNKNOWN;
-}
-
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index fb16c4aca77..f3b1a8c38a6 100644
--- a/config.h
+++ b/config.h
@@ -374,7 +374,6 @@ void git_global_config(char **user, char **xdg);
 
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
-enum config_scope current_config_scope(void);
 const char *config_origin_type_name(enum config_origin_type type);
 
 /*
@@ -702,4 +701,6 @@ NORETURN void git_die_config_linenr(const char *key, const char *filename, int l
 	lookup_config(mapping, ARRAY_SIZE(mapping), var)
 int lookup_config(const char **mapping, int nr_mapping, const char *var);
 
+void kvi_from_param(struct key_value_info *out);
+
 #endif /* CONFIG_H */
diff --git a/remote.c b/remote.c
index 10868a963f2..266601157e3 100644
--- a/remote.c
+++ b/remote.c
@@ -349,7 +349,7 @@ static void read_branches_file(struct remote_state *remote_state,
 }
 
 static int handle_config(const char *key, const char *value,
-			 struct key_value_info *kvi UNUSED, void *cb)
+			 struct key_value_info *kvi, void *cb)
 {
 	const char *name;
 	size_t namelen;
@@ -414,8 +414,8 @@ static int handle_config(const char *key, const char *value,
 	}
 	remote = make_remote(remote_state, name, namelen);
 	remote->origin = REMOTE_CONFIG;
-	if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
-	    current_config_scope() == CONFIG_SCOPE_WORKTREE)
+	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
+	    kvi->scope == CONFIG_SCOPE_WORKTREE)
 		remote->configured_in_repo = 1;
 	if (!strcmp(subkey, "mirror"))
 		remote->mirror = git_config_bool(key, value);
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 337587df41d..7027ffa187f 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -55,7 +55,7 @@ static int iterate_cb(const char *var, const char *value,
 	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
 	printf("name=%s\n", kvi->filename ? kvi->filename : "");
 	printf("lno=%d\n", kvi->linenr);
-	printf("scope=%s\n", config_scope_name(current_config_scope()));
+	printf("scope=%s\n", config_scope_name(kvi->scope));
 
 	return 0;
 }
diff --git a/trace2.c b/trace2.c
index e8ba62c0c3d..f519a3514b6 100644
--- a/trace2.c
+++ b/trace2.c
@@ -632,7 +632,7 @@ void trace2_thread_exit_fl(const char *file, int line)
 }
 
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value)
+			 const char *value, struct key_value_info *kvi)
 {
 	struct tr2_tgt *tgt_j;
 	int j;
@@ -642,7 +642,7 @@ void trace2_def_param_fl(const char *file, int line, const char *param,
 
 	for_each_wanted_builtin (j, tgt_j)
 		if (tgt_j->pfn_param_fl)
-			tgt_j->pfn_param_fl(file, line, param, value);
+			tgt_j->pfn_param_fl(file, line, param, value, kvi);
 }
 
 void trace2_def_repo_fl(const char *file, int line, struct repository *repo)
diff --git a/trace2.h b/trace2.h
index 4ced30c0db3..af06f66739e 100644
--- a/trace2.h
+++ b/trace2.h
@@ -325,6 +325,7 @@ void trace2_thread_exit_fl(const char *file, int line);
 
 #define trace2_thread_exit() trace2_thread_exit_fl(__FILE__, __LINE__)
 
+struct key_value_info;
 /*
  * Emits a "def_param" message containing a key/value pair.
  *
@@ -334,7 +335,7 @@ void trace2_thread_exit_fl(const char *file, int line);
  * `core.abbrev`, `status.showUntrackedFiles`, or `--no-ahead-behind`.
  */
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value);
+			 const char *value, struct key_value_info *kvi);
 
 #define trace2_def_param(param, value) \
 	trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 8ed139c69f4..1450c9bec71 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -100,7 +100,7 @@ struct tr2_cfg_data {
  * See if the given config key matches any of our patterns of interest.
  */
 static int tr2_cfg_cb(const char *key, const char *value,
-		      struct key_value_info *kvi UNUSED, void *d)
+		      struct key_value_info *kvi, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
@@ -109,7 +109,8 @@ static int tr2_cfg_cb(const char *key, const char *value,
 		struct strbuf *buf = *s;
 		int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
 		if (wm == WM_MATCH) {
-			trace2_def_param_fl(data->file, data->line, key, value);
+			trace2_def_param_fl(data->file, data->line, key, value,
+					    kvi);
 			return 0;
 		}
 	}
@@ -127,8 +128,10 @@ void tr2_cfg_list_config_fl(const char *file, int line)
 
 void tr2_list_env_vars_fl(const char *file, int line)
 {
+	struct key_value_info kvi = { 0 };
 	struct strbuf **s;
 
+	kvi_from_param(&kvi);
 	if (tr2_load_env_vars() <= 0)
 		return;
 
@@ -136,7 +139,7 @@ void tr2_list_env_vars_fl(const char *file, int line)
 		struct strbuf *buf = *s;
 		const char *val = getenv(buf->buf);
 		if (val && *val)
-			trace2_def_param_fl(file, line, buf->buf, val);
+			trace2_def_param_fl(file, line, buf->buf, val, &kvi);
 	}
 }
 
diff --git a/trace2/tr2_tgt.h b/trace2/tr2_tgt.h
index bf8745c4f05..9c88ca9beed 100644
--- a/trace2/tr2_tgt.h
+++ b/trace2/tr2_tgt.h
@@ -69,8 +69,10 @@ typedef void(tr2_tgt_evt_exec_result_fl_t)(const char *file, int line,
 					   uint64_t us_elapsed_absolute,
 					   int exec_id, int code);
 
+struct key_value_info;
 typedef void(tr2_tgt_evt_param_fl_t)(const char *file, int line,
-				     const char *param, const char *value);
+				     const char *param, const char *value,
+				     struct key_value_info *kvi);
 
 typedef void(tr2_tgt_evt_repo_fl_t)(const char *file, int line,
 				    const struct repository *repo);
diff --git a/trace2/tr2_tgt_event.c b/trace2/tr2_tgt_event.c
index 9e7aab6d510..83db3c755bd 100644
--- a/trace2/tr2_tgt_event.c
+++ b/trace2/tr2_tgt_event.c
@@ -476,11 +476,11 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct json_writer jw = JSON_WRITER_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	jw_object_begin(&jw, 0);
diff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c
index 8672c2c2d04..65e9be9c5a4 100644
--- a/trace2/tr2_tgt_normal.c
+++ b/trace2/tr2_tgt_normal.c
@@ -296,10 +296,10 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, struct key_value_info *kvi)
 {
 	struct strbuf buf_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
diff --git a/trace2/tr2_tgt_perf.c b/trace2/tr2_tgt_perf.c
index 3f2b2d53118..f402f6e3813 100644
--- a/trace2/tr2_tgt_perf.c
+++ b/trace2/tr2_tgt_perf.c
@@ -438,12 +438,12 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct strbuf buf_payload = STRBUF_INIT;
 	struct strbuf scope_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "%s:%s", param, value);
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 13/14] config: pass kvi to die_bad_number()
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (11 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 12/14] config: remove current_config_scope() Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-04-21 19:13 ` [PATCH 14/14] config: remove config_reader from configset_add_value Glen Choo via GitGitGadget
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Plumb "struct key_value_info" through all code paths that end in
die_bad_number(), which lets us remove the helper functions that read
analogous values from "struct config_reader".

In config.c, this requires changing the signature of
git_configset_get_value() to 'return' "kvi" in an out parameter so that
git_configset_get_<type>() can pass it to git_config_<type>().

Outside of config.c, config callbacks now need to pass "kvi" to any of
the git_config_<type>() functions that parse a config string into a
number type. Included is a .cocci patch to make that refactor. In cases
where "kvi" would never be used, pass NULL, e.g.:

- In config.c, when we are parsing a boolean instead of a number
- In builtin/config.c, when calling normalize_value() before setting
  config to something the user gave us.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 archive-tar.c                              |   4 +-
 builtin/commit-graph.c                     |   4 +-
 builtin/commit.c                           |  10 +-
 builtin/config.c                           |  20 ++--
 builtin/fetch.c                            |   4 +-
 builtin/fsmonitor--daemon.c                |   6 +-
 builtin/grep.c                             |   2 +-
 builtin/index-pack.c                       |   4 +-
 builtin/log.c                              |   2 +-
 builtin/pack-objects.c                     |  14 +--
 builtin/receive-pack.c                     |  10 +-
 builtin/submodule--helper.c                |   4 +-
 config.c                                   | 128 +++++++++------------
 config.h                                   |  14 ++-
 contrib/coccinelle/git_config_number.cocci |  27 +++++
 diff.c                                     |   9 +-
 fmt-merge-msg.c                            |   2 +-
 help.c                                     |   5 +-
 http.c                                     |  10 +-
 imap-send.c                                |   2 +-
 sequencer.c                                |  22 ++--
 setup.c                                    |   2 +-
 submodule-config.c                         |  15 ++-
 submodule-config.h                         |   3 +-
 t/helper/test-config.c                     |   6 +-
 upload-pack.c                              |  12 +-
 worktree.c                                 |   2 +-
 27 files changed, 182 insertions(+), 161 deletions(-)
 create mode 100644 contrib/coccinelle/git_config_number.cocci

diff --git a/archive-tar.c b/archive-tar.c
index dcfbce5225a..1cd6d72d21e 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -411,14 +411,14 @@ static int tar_filter_config(const char *var, const char *value,
 }
 
 static int git_tar_config(const char *var, const char *value,
-			  struct key_value_info *kvi UNUSED, void *cb)
+			  struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
 			tar_umask = umask(0);
 			umask(tar_umask);
 		} else {
-			tar_umask = git_config_int(var, value);
+			tar_umask = git_config_int(var, value, kvi);
 		}
 		return 0;
 	}
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index e811866b5dd..c99804abc7e 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -185,11 +185,11 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
-					 struct key_value_info *kvi UNUSED,
+					 struct key_value_info *kvi,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
-		write_opts.max_new_filters = git_config_int(var, value);
+		write_opts.max_new_filters = git_config_int(var, value, kvi);
 	/*
 	 * No need to fall-back to 'git_default_config', since this was already
 	 * called in 'cmd_commit_graph()'.
diff --git a/builtin/commit.c b/builtin/commit.c
index ec468e87039..e846355ec39 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1412,7 +1412,8 @@ static int git_status_config(const char *k, const char *v,
 		return git_column_config(k, v, "status", &s->colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
-		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
+		s->submodule_summary = git_config_bool_or_int(k, v, kvi,
+							      &is_bool);
 		if (is_bool && s->submodule_summary)
 			s->submodule_summary = -1;
 		return 0;
@@ -1472,11 +1473,11 @@ static int git_status_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "diff.renamelimit")) {
 		if (s->rename_limit == -1)
-			s->rename_limit = git_config_int(k, v);
+			s->rename_limit = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "status.renamelimit")) {
-		s->rename_limit = git_config_int(k, v);
+		s->rename_limit = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "diff.renames")) {
@@ -1622,7 +1623,8 @@ static int git_commit_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "commit.verbose")) {
 		int is_bool;
-		config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
+		config_commit_verbose = git_config_bool_or_int(k, v, kvi,
+							       &is_bool);
 		return 0;
 	}
 
diff --git a/builtin/config.c b/builtin/config.c
index 7899add68fb..fcfceb8e156 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -254,13 +254,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
@@ -411,7 +412,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;
@@ -426,12 +428,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
@@ -870,7 +872,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], NULL);
 		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"
@@ -879,7 +881,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], NULL);
 		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
 							     argv[0], value, argv[2],
 							     flags);
@@ -887,7 +889,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], NULL);
 		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
 							     argv[0], value,
 							     CONFIG_REGEX_NONE,
@@ -896,7 +898,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], NULL);
 		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
 							     argv[0], value, argv[2],
 							     flags | CONFIG_FLAGS_MULTI_REPLACE);
diff --git a/builtin/fetch.c b/builtin/fetch.c
index aa688291613..04cf5518d2c 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -125,7 +125,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "submodule.fetchjobs")) {
-		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
+		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v, kvi);
 		return 0;
 	} else if (!strcmp(k, "fetch.recursesubmodules")) {
 		recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
@@ -133,7 +133,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "fetch.parallel")) {
-		fetch_parallel_config = git_config_int(k, v);
+		fetch_parallel_config = git_config_int(k, v, kvi);
 		if (fetch_parallel_config < 0)
 			die(_("fetch.parallel cannot be negative"));
 		if (!fetch_parallel_config)
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index a7375d61d02..cde4a575836 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -40,7 +40,7 @@ static int fsmonitor_config(const char *var, const char *value,
 			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, kvi);
 		if (i < 1)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__IPC_THREADS, i);
@@ -49,7 +49,7 @@ static int fsmonitor_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, FSMONITOR__START_TIMEOUT)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, kvi);
 		if (i < 0)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__START_TIMEOUT, i);
@@ -59,7 +59,7 @@ static int fsmonitor_config(const char *var, const char *value,
 
 	if (!strcmp(var, FSMONITOR__ANNOUNCE_STARTUP)) {
 		int is_bool;
-		int i = git_config_bool_or_int(var, value, &is_bool);
+		int i = git_config_bool_or_int(var, value, kvi, &is_bool);
 		if (i < 0)
 			return error(_("value of '%s' not bool or int: %d"),
 				     var, i);
diff --git a/builtin/grep.c b/builtin/grep.c
index 6e795f9f3a2..edb57f048ef 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -298,7 +298,7 @@ static int grep_cmd_config(const char *var, const char *value,
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
-		num_threads = git_config_int(var, value);
+		num_threads = git_config_int(var, value, kvi);
 		if (num_threads < 0)
 			die(_("invalid number of threads specified (%d) for %s"),
 			    num_threads, var);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 4450510ddfc..e7685fa9a6f 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1584,13 +1584,13 @@ static int git_index_pack_config(const char *k, const char *v,
 	struct pack_idx_option *opts = cb;
 
 	if (!strcmp(k, "pack.indexversion")) {
-		opts->version = git_config_int(k, v);
+		opts->version = git_config_int(k, v, kvi);
 		if (opts->version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32), opts->version);
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		nr_threads = git_config_int(k, v);
+		nr_threads = git_config_int(k, v, kvi);
 		if (nr_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    nr_threads);
diff --git a/builtin/log.c b/builtin/log.c
index f8e61330491..805320a1abf 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -569,7 +569,7 @@ static int git_log_config(const char *var, const char *value,
 	if (!strcmp(var, "format.subjectprefix"))
 		return git_config_string(&fmt_patch_subject_prefix, var, value);
 	if (!strcmp(var, "format.filenamemaxlength")) {
-		fmt_patch_name_max = git_config_int(var, value);
+		fmt_patch_name_max = git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp(var, "format.encodeemailheaders")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index ca023000cc0..cde11f83f81 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3138,23 +3138,23 @@ static int git_pack_config(const char *k, const char *v,
 			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
-		window = git_config_int(k, v);
+		window = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.windowmemory")) {
-		window_memory_limit = git_config_ulong(k, v);
+		window_memory_limit = git_config_ulong(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.depth")) {
-		depth = git_config_int(k, v);
+		depth = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachesize")) {
-		max_delta_cache_size = git_config_int(k, v);
+		max_delta_cache_size = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachelimit")) {
-		cache_max_small_delta_size = git_config_int(k, v);
+		cache_max_small_delta_size = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.writebitmaphashcache")) {
@@ -3180,7 +3180,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		delta_search_threads = git_config_int(k, v);
+		delta_search_threads = git_config_int(k, v, kvi);
 		if (delta_search_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    delta_search_threads);
@@ -3191,7 +3191,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.indexversion")) {
-		pack_idx_opts.version = git_config_int(k, v);
+		pack_idx_opts.version = git_config_int(k, v, kvi);
 		if (pack_idx_opts.version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32),
 			    pack_idx_opts.version);
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 2f5fd2abbc3..d2bc0fead9f 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -154,12 +154,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.unpacklimit") == 0) {
-		receive_unpack_limit = git_config_int(var, value);
+		receive_unpack_limit = git_config_int(var, value, kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "transfer.unpacklimit") == 0) {
-		transfer_unpack_limit = git_config_int(var, value);
+		transfer_unpack_limit = git_config_int(var, value, kvi);
 		return 0;
 	}
 
@@ -227,7 +227,7 @@ static int receive_pack_config(const char *var, const char *value,
 		return git_config_string(&cert_nonce_seed, var, value);
 
 	if (strcmp(var, "receive.certnonceslop") == 0) {
-		nonce_stamp_slop_limit = git_config_ulong(var, value);
+		nonce_stamp_slop_limit = git_config_ulong(var, value, kvi);
 		return 0;
 	}
 
@@ -242,12 +242,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.keepalive") == 0) {
-		keepalive_in_sec = git_config_int(var, value);
+		keepalive_in_sec = git_config_int(var, value, kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "receive.maxinputsize") == 0) {
-		max_input_size = git_config_int64(var, value);
+		max_input_size = git_config_int64(var, value, kvi);
 		return 0;
 	}
 
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 8570effbf0d..bda10764db5 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2187,13 +2187,13 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
-				   struct key_value_info *kvi UNUSED,
+				   struct key_value_info *kvi,
 				   void *cb)
 {
 	int *max_jobs = cb;
 
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, kvi);
 	return 0;
 }
 
diff --git a/config.c b/config.c
index 68c9b507a4d..e78de67c452 100644
--- a/config.c
+++ b/config.c
@@ -1309,80 +1309,74 @@ 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,
+			   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 (!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 || !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,
+		   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, 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,
+			       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,
+			   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;
 }
 
@@ -1487,7 +1481,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,
+			   struct key_value_info *kvi, int *is_bool)
 {
 	int v = git_parse_maybe_bool_text(value);
 	if (0 <= v) {
@@ -1495,7 +1490,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)
@@ -1621,7 +1616,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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, kvi);
 			if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz)
 				return error(_("abbrev length out of range: %d"), abbrev);
 			default_abbrev = abbrev;
@@ -1633,7 +1628,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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, kvi);
 		if (level == -1)
 			level = Z_DEFAULT_COMPRESSION;
 		else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -1644,7 +1639,7 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.compression")) {
-		int level = git_config_int(var, value);
+		int level = git_config_int(var, value, kvi);
 		if (level == -1)
 			level = Z_DEFAULT_COMPRESSION;
 		else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -1658,7 +1653,7 @@ static int git_default_core_config(const char *var, const char *value,
 
 	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, kvi);
 
 		/* This value must be multiple of (pagesize * 2) */
 		packed_git_window_size /= pgsz_x2;
@@ -1669,17 +1664,17 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.bigfilethreshold")) {
-		big_file_threshold = git_config_ulong(var, value);
+		big_file_threshold = git_config_ulong(var, value, kvi);
 		return 0;
 	}
 
 	if (!strcmp(var, "core.packedgitlimit")) {
-		packed_git_limit = git_config_ulong(var, value);
+		packed_git_limit = git_config_ulong(var, value, 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, kvi);
 		return 0;
 	}
 
@@ -1963,12 +1958,12 @@ int git_default_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "pack.packsizelimit")) {
-		pack_size_limit_cfg = git_config_ulong(var, value);
+		pack_size_limit_cfg = git_config_ulong(var, value, kvi);
 		return 0;
 	}
 
 	if (!strcmp(var, "pack.compression")) {
-		int level = git_config_int(var, value);
+		int level = git_config_int(var, value, kvi);
 		if (level == -1)
 			level = Z_DEFAULT_COMPRESSION;
 		else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -2474,11 +2469,12 @@ int git_configset_add_file(struct config_set *set, const char *filename)
 	return git_config_from_file(config_set_callback, filename, &data);
 }
 
-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
@@ -2488,7 +2484,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;
 }
 
@@ -2541,7 +2540,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;
@@ -2551,7 +2550,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;
@@ -2564,8 +2563,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;
@@ -2574,8 +2575,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;
@@ -2584,7 +2587,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
@@ -2595,8 +2598,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;
@@ -2605,7 +2610,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;
@@ -2617,7 +2622,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;
@@ -2687,7 +2692,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,
@@ -3907,16 +3912,6 @@ 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
-		return 1;
-	return 0;
-}
-
 const char *config_origin_type_name(enum config_origin_type type)
 {
 	switch (type) {
@@ -3955,15 +3950,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
-		return 1;
-	return 0;
-}
-
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index f3b1a8c38a6..ffb2d76823c 100644
--- a/config.h
+++ b/config.h
@@ -237,22 +237,23 @@ int git_parse_maybe_bool(const char *);
  * Parse the string to an integer, including unit factors. Dies on error;
  * otherwise, returns the parsed result.
  */
-int git_config_int(const char *, const char *);
+int git_config_int(const char *, const char *, struct key_value_info *);
 
-int64_t git_config_int64(const char *, const char *);
+int64_t git_config_int64(const char *, const char *, struct key_value_info *);
 
 /**
  * Identical to `git_config_int`, but for unsigned longs.
  */
-unsigned long git_config_ulong(const char *, const char *);
+unsigned long git_config_ulong(const char *, const char *, struct key_value_info *);
 
-ssize_t git_config_ssize_t(const char *, const char *);
+ssize_t git_config_ssize_t(const char *, const char *, struct key_value_info *);
 
 /**
  * Same as `git_config_bool`, except that integers are returned as-is, and
  * an `is_bool` flag is unset.
  */
-int git_config_bool_or_int(const char *, const char *, int *);
+int git_config_bool_or_int(const char *, const char *, struct key_value_info *,
+			   int *);
 
 /**
  * Parse a string into a boolean value, respecting keywords like "true" and
@@ -516,7 +517,8 @@ int git_configset_get(struct config_set *cs, const char *key);
  * touching `value`. The caller should not free or modify `value`, as it
  * is owned by the cache.
  */
-int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
+int git_configset_get_value(struct config_set *cs, const char *key,
+			    const char **dest, struct key_value_info *kvi);
 
 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
diff --git a/contrib/coccinelle/git_config_number.cocci b/contrib/coccinelle/git_config_number.cocci
new file mode 100644
index 00000000000..f46c74dd23c
--- /dev/null
+++ b/contrib/coccinelle/git_config_number.cocci
@@ -0,0 +1,27 @@
+@@
+identifier C1, C2, C3;
+@@
+(
+(
+git_config_int
+|
+git_config_int64
+|
+git_config_ulong
+|
+git_config_ssize_t
+)
+  (C1, C2
++ , kvi
+  )
+|
+(
+git_configset_get_value
+|
+git_config_bool_or_int
+)
+  (C1, C2,
++ kvi,
+  C3
+  )
+)
diff --git a/diff.c b/diff.c
index d7ed2dc900b..da7cd353a6d 100644
--- a/diff.c
+++ b/diff.c
@@ -372,13 +372,14 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.context")) {
-		diff_context_default = git_config_int(var, value);
+		diff_context_default = git_config_int(var, value, kvi);
 		if (diff_context_default < 0)
 			return -1;
 		return 0;
 	}
 	if (!strcmp(var, "diff.interhunkcontext")) {
-		diff_interhunk_context_default = git_config_int(var, value);
+		diff_interhunk_context_default = git_config_int(var, value,
+								kvi);
 		if (diff_interhunk_context_default < 0)
 			return -1;
 		return 0;
@@ -404,7 +405,7 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.statgraphwidth")) {
-		diff_stat_graph_width = git_config_int(var, value);
+		diff_stat_graph_width = git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp(var, "diff.external"))
@@ -443,7 +444,7 @@ int git_diff_basic_config(const char *var, const char *value,
 	const char *name;
 
 	if (!strcmp(var, "diff.renamelimit")) {
-		diff_rename_limit_default = git_config_int(var, value);
+		diff_rename_limit_default = git_config_int(var, value, kvi);
 		return 0;
 	}
 
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 97358034fa0..d1b59af44bb 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -24,7 +24,7 @@ int fmt_merge_msg_config(const char *key, const char *value,
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
-		merge_log_config = git_config_bool_or_int(key, value, &is_bool);
+		merge_log_config = git_config_bool_or_int(key, value, kvi, &is_bool);
 		if (!is_bool && merge_log_config < 0)
 			return error("%s: negative length %s", key, value);
 		if (is_bool && merge_log_config)
diff --git a/help.c b/help.c
index 43d1eb702cd..08f0b953736 100644
--- a/help.c
+++ b/help.c
@@ -545,8 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
-				  struct key_value_info *kvi UNUSED,
-				  void *cb UNUSED)
+				  struct key_value_info *kvi, void *cb UNUSED)
 {
 	const char *p;
 
@@ -560,7 +559,7 @@ static int git_unknown_cmd_config(const char *var, const char *value,
 		} else if (!strcmp(value, "prompt")) {
 			autocorrect = AUTOCORRECT_PROMPT;
 		} else {
-			int v = git_config_int(var, value);
+			int v = git_config_int(var, value, kvi);
 			autocorrect = (v < 0)
 				? AUTOCORRECT_IMMEDIATELY : v;
 		}
diff --git a/http.c b/http.c
index 3d4292eba6a..a26c3dff827 100644
--- a/http.c
+++ b/http.c
@@ -412,21 +412,21 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.minsessions", var)) {
-		min_curl_sessions = git_config_int(var, value);
+		min_curl_sessions = git_config_int(var, value, kvi);
 		if (min_curl_sessions > 1)
 			min_curl_sessions = 1;
 		return 0;
 	}
 	if (!strcmp("http.maxrequests", var)) {
-		max_requests = git_config_int(var, value);
+		max_requests = git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedlimit", var)) {
-		curl_low_speed_limit = (long)git_config_int(var, value);
+		curl_low_speed_limit = (long)git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedtime", var)) {
-		curl_low_speed_time = (long)git_config_int(var, value);
+		curl_low_speed_time = (long)git_config_int(var, value, kvi);
 		return 0;
 	}
 
@@ -462,7 +462,7 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.postbuffer", var)) {
-		http_post_buffer = git_config_ssize_t(var, value);
+		http_post_buffer = git_config_ssize_t(var, value, kvi);
 		if (http_post_buffer < 0)
 			warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
 		if (http_post_buffer < LARGE_PACKET_MAX)
diff --git a/imap-send.c b/imap-send.c
index 3cc98f1a0a5..3c391a52c5a 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1341,7 +1341,7 @@ static int git_imap_config(const char *var, const char *val,
 	else if (!strcmp("imap.authmethod", var))
 		return git_config_string(&server.auth_method, var, val);
 	else if (!strcmp("imap.port", var))
-		server.port = git_config_int(var, val);
+		server.port = git_config_int(var, val, kvi);
 	else if (!strcmp("imap.host", var)) {
 		if (!val) {
 			git_die_config("imap.host", "Missing value for 'imap.host'");
diff --git a/sequencer.c b/sequencer.c
index 171561c2cdb..76b4750b4bd 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2878,7 +2878,7 @@ static int git_config_string_dup(char **dest,
 }
 
 static int populate_opts_cb(const char *key, const char *value,
-			    struct key_value_info *kvi UNUSED, void *data)
+			    struct key_value_info *kvi, void *data)
 {
 	struct replay_opts *opts = data;
 	int error_flag = 1;
@@ -2886,26 +2886,26 @@ static int populate_opts_cb(const char *key, const char *value,
 	if (!value)
 		error_flag = 0;
 	else if (!strcmp(key, "options.no-commit"))
-		opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
+		opts->no_commit = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.edit"))
-		opts->edit = git_config_bool_or_int(key, value, &error_flag);
+		opts->edit = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty"))
 		opts->allow_empty =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty-message"))
 		opts->allow_empty_message =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.keep-redundant-commits"))
 		opts->keep_redundant_commits =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.signoff"))
-		opts->signoff = git_config_bool_or_int(key, value, &error_flag);
+		opts->signoff = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.record-origin"))
-		opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
+		opts->record_origin = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-ff"))
-		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
+		opts->allow_ff = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
-		opts->mainline = git_config_int(key, value);
+		opts->mainline = git_config_int(key, value, kvi);
 	else if (!strcmp(key, "options.strategy"))
 		git_config_string_dup(&opts->strategy, key, value);
 	else if (!strcmp(key, "options.gpg-sign"))
@@ -2915,7 +2915,7 @@ static int populate_opts_cb(const char *key, const char *value,
 		opts->xopts[opts->xopts_nr++] = xstrdup(value);
 	} else if (!strcmp(key, "options.allow-rerere-auto"))
 		opts->allow_rerere_auto =
-			git_config_bool_or_int(key, value, &error_flag) ?
+			git_config_bool_or_int(key, value, kvi, &error_flag) ?
 				RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
 	else if (!strcmp(key, "options.default-msg-cleanup")) {
 		opts->explicit_cleanup = 1;
diff --git a/setup.c b/setup.c
index a461dd15233..75934d7438f 100644
--- a/setup.c
+++ b/setup.c
@@ -594,7 +594,7 @@ static int check_repo_format(const char *var, const char *value,
 	const char *ext;
 
 	if (strcmp(var, "core.repositoryformatversion") == 0)
-		data->version = git_config_int(var, value);
+		data->version = git_config_int(var, value, kvi);
 	else if (skip_prefix(var, "extensions.", &ext)) {
 		switch (handle_extension_v0(var, value, ext, data)) {
 		case EXTENSION_ERROR:
diff --git a/submodule-config.c b/submodule-config.c
index 7d773f33621..b86547fd1ee 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -302,9 +302,10 @@ static int parse_fetch_recurse(const char *opt, const char *arg,
 	}
 }
 
-int parse_submodule_fetchjobs(const char *var, const char *value)
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      struct key_value_info *kvi)
 {
-	int fetchjobs = git_config_int(var, value);
+	int fetchjobs = git_config_int(var, value, kvi);
 	if (fetchjobs < 0)
 		die(_("negative values not allowed for submodule.fetchJobs"));
 	if (!fetchjobs)
@@ -848,14 +849,13 @@ struct fetch_config {
 };
 
 static int gitmodules_fetch_config(const char *var, const char *value,
-				   struct key_value_info *kvi UNUSED,
-				   void *cb)
+				   struct key_value_info *kvi, void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
 		if (config->max_children)
 			*(config->max_children) =
-				parse_submodule_fetchjobs(var, value);
+				parse_submodule_fetchjobs(var, value, kvi);
 		return 0;
 	} else if (!strcmp(var, "fetch.recursesubmodules")) {
 		if (config->recurse_submodules)
@@ -877,12 +877,11 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
-					  struct key_value_info *kvi UNUSED,
-					  void *cb)
+					  struct key_value_info *kvi, void *cb)
 {
 	int *max_jobs = cb;
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, kvi);
 	return 0;
 }
 
diff --git a/submodule-config.h b/submodule-config.h
index c2045875bbb..944cae75cc9 100644
--- a/submodule-config.h
+++ b/submodule-config.h
@@ -50,7 +50,8 @@ struct repository;
 
 void submodule_cache_free(struct submodule_cache *cache);
 
-int parse_submodule_fetchjobs(const char *var, const char *value);
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      struct key_value_info *kvi);
 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 struct option;
 int option_fetch_parse_recurse_submodules(const struct option *opt,
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 7027ffa187f..737505583d4 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -61,12 +61,12 @@ static int iterate_cb(const char *var, const char *value,
 }
 
 static int parse_int_cb(const char *var, const char *value,
-			struct key_value_info *kvi UNUSED, void *data)
+			struct key_value_info *kvi, void *data)
 {
 	const char *key_to_match = data;
 
 	if (!strcmp(key_to_match, var)) {
-		int parsed = git_config_int(value, value);
+		int parsed = git_config_int(value, value, kvi);
 		printf("%d\n", parsed);
 	}
 	return 0;
@@ -179,7 +179,7 @@ int cmd__config(int argc, const char **argv)
 				goto exit2;
 			}
 		}
-		if (!git_configset_get_value(&cs, argv[2], &v)) {
+		if (!git_configset_get_value(&cs, argv[2], &v, NULL)) {
 			if (!v)
 				printf("(NULL)\n");
 			else
diff --git a/upload-pack.c b/upload-pack.c
index 5f8232ff078..7cf776cde91 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1264,7 +1264,8 @@ static int find_symref(const char *refname,
 }
 
 static int parse_object_filter_config(const char *var, const char *value,
-				       struct upload_pack_data *data)
+				      struct key_value_info *kvi,
+				      struct upload_pack_data *data)
 {
 	struct strbuf buf = STRBUF_INIT;
 	const char *sub, *key;
@@ -1291,7 +1292,8 @@ static int parse_object_filter_config(const char *var, const char *value,
 		}
 		string_list_insert(&data->allowed_filters, buf.buf)->util =
 			(void *)(intptr_t)1;
-		data->tree_filter_max_depth = git_config_ulong(var, value);
+		data->tree_filter_max_depth = git_config_ulong(var, value,
+							       kvi);
 	}
 
 	strbuf_release(&buf);
@@ -1299,7 +1301,7 @@ static int parse_object_filter_config(const char *var, const char *value,
 }
 
 static int upload_pack_config(const char *var, const char *value,
-			      struct key_value_info *kvi UNUSED,
+			      struct key_value_info *kvi,
 			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
@@ -1320,7 +1322,7 @@ static int upload_pack_config(const char *var, const char *value,
 		else
 			data->allow_uor &= ~ALLOW_ANY_SHA1;
 	} else if (!strcmp("uploadpack.keepalive", var)) {
-		data->keepalive = git_config_int(var, value);
+		data->keepalive = git_config_int(var, value, kvi);
 		if (!data->keepalive)
 			data->keepalive = -1;
 	} else if (!strcmp("uploadpack.allowfilter", var)) {
@@ -1335,7 +1337,7 @@ static int upload_pack_config(const char *var, const char *value,
 		data->advertise_sid = git_config_bool(var, value);
 	}
 
-	if (parse_object_filter_config(var, value, data) < 0)
+	if (parse_object_filter_config(var, value, kvi, data) < 0)
 		return -1;
 
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
diff --git a/worktree.c b/worktree.c
index b5ee71c5ebd..1fbdbd745fb 100644
--- a/worktree.c
+++ b/worktree.c
@@ -835,7 +835,7 @@ int init_worktree_config(struct repository *r)
 	 * Relocate that value to avoid breaking all worktrees with this
 	 * upgrade to worktree config.
 	 */
-	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
+	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
 		if ((res = move_config_setting("core.worktree", core_worktree,
 					       common_config_file,
 					       main_worktree_file)))
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH 14/14] config: remove config_reader from configset_add_value
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (12 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 13/14] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
@ 2023-04-21 19:13 ` Glen Choo via GitGitGadget
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-04-21 19:13 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Since we now get "kvi" from the config callback, we can stop passing it
via "*data". Now "struct config_reader" has no more references, so get
rid of it.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 81 ++++++++++----------------------------------------------
 1 file changed, 14 insertions(+), 67 deletions(-)

diff --git a/config.c b/config.c
index e78de67c452..fbb4aab46cf 100644
--- a/config.c
+++ b/config.c
@@ -59,33 +59,6 @@ struct config_source {
 };
 #define CONFIG_SOURCE_INIT { 0 }
 
-struct config_reader {
-	struct key_value_info *config_kvi;
-};
-/*
- * 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_kvi(struct config_reader *reader,
-					  struct key_value_info *kvi)
-{
-	kvi->prev = reader->config_kvi;
-	reader->config_kvi = kvi;
-}
-
-static inline struct key_value_info *config_reader_pop_kvi(struct config_reader *reader)
-{
-	struct key_value_info *ret;
-	if (!reader->config_kvi)
-		BUG("tried to pop config_kvi, but we weren't reading config");
-	ret = reader->config_kvi;
-	reader->config_kvi = reader->config_kvi->prev;
-	return ret;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -429,8 +402,6 @@ static int include_condition_is_true(struct key_value_info *kvi,
 	return 0;
 }
 
-static int kvi_fn(config_fn_t fn, const char *key, const char *value,
-		  struct key_value_info *kvi, void *data);
 static int git_config_include(const char *var, const char *value,
 			      struct key_value_info *kvi, void *data)
 {
@@ -443,7 +414,7 @@ static int git_config_include(const char *var, const char *value,
 	 * Pass along all values, including "include" directives; this makes it
 	 * possible to query information on the includes themselves.
 	 */
-	ret = kvi_fn(inc->fn, var, value, kvi, inc->data);
+	ret = inc->fn(var, value, kvi, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -614,16 +585,6 @@ out_free_ret_1:
 	return -CONFIG_INVALID_KEY;
 }
 
-static int kvi_fn(config_fn_t fn, const char *key, const char *value,
-		  struct key_value_info *kvi, void *data)
-{
-	int ret;
-	config_reader_push_kvi(&the_reader, kvi);
-	ret = fn(key, value, kvi, data);
-	config_reader_pop_kvi(&the_reader);
-	return ret;
-}
-
 static int config_parse_pair(const char *key, const char *value,
 			     struct key_value_info *kvi,
 			     config_fn_t fn, void *data)
@@ -636,7 +597,7 @@ static int config_parse_pair(const char *key, const char *value,
 	if (git_config_parse_key(key, &canonical_name, NULL))
 		return -1;
 
-	ret = (kvi_fn(fn, canonical_name, value, kvi, data) < 0) ? -1 : 0;
+	ret = (fn(canonical_name, value, kvi, data) < 0) ? -1 : 0;
 	free(canonical_name);
 	return ret;
 }
@@ -937,7 +898,7 @@ static int get_value(struct config_source *cs, struct key_value_info *kvi,
 	 */
 	cs->linenr--;
 	kvi->linenr = cs->linenr;
-	ret = kvi_fn(fn, name->buf, value, kvi, data);
+	ret = fn(name->buf, value, kvi, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -2289,8 +2250,8 @@ static void configset_iter(struct config_set *set, config_fn_t fn, void *data)
 		values = &entry->value_list;
 		kvi = values->items[value_index].util;
 
-		if (kvi_fn(fn, entry->key, values->items[value_index].string,
-			   kvi, data) < 0)
+		if (fn(entry->key, values->items[value_index].string, kvi,
+		       data) < 0)
 			git_die_config_linenr(entry->key, kvi->filename,
 					      kvi->linenr);
 	}
@@ -2368,9 +2329,8 @@ static int configset_find_element(struct config_set *set, const char *key,
 	return 0;
 }
 
-static int configset_add_value(struct config_reader *reader,
-			       struct config_set *set, const char *key,
-			       const char *value)
+static int configset_add_value(struct config_set *set, const char *key,
+			       const char *value, struct key_value_info *kvi_p)
 {
 	struct config_set_element *e;
 	struct string_list_item *si;
@@ -2399,7 +2359,7 @@ static int configset_add_value(struct config_reader *reader,
 	l_item->e = e;
 	l_item->value_index = e->value_list.nr - 1;
 
-	memcpy(kv_info, reader->config_kvi, sizeof(struct key_value_info));
+	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
 	si->util = kv_info;
 
 	return 0;
@@ -2447,26 +2407,19 @@ 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,
-			       struct key_value_info *kvi UNUSED, void *cb)
+			       struct key_value_info *kvi, 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(set, key, value, kvi);
 	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,
@@ -2632,7 +2585,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;
@@ -2644,10 +2596,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, &opts) < 0)
+	if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
 		/*
 		 * config_with_options() normally returns only
 		 * zero, as most errors are fatal, and
@@ -2786,12 +2736,9 @@ 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, &opts);
+	config_with_options(config_set_callback, &protected_config, NULL, &opts);
 }
 
 void git_protected_config(config_fn_t fn, void *data)
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH 02/14] config.c: use kvi for CLI config
  2023-04-21 19:13 ` [PATCH 02/14] config.c: use kvi for CLI config Glen Choo via GitGitGadget
@ 2023-05-01 11:06   ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 115+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2023-05-01 11:06 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget; +Cc: git, Jonathan Tan, Emily Shaffer, Glen Choo


On Fri, Apr 21 2023, Glen Choo via GitGitGadget wrote:

> @@ -682,17 +677,30 @@ static int config_parse_pair(const char *key, const char *value,
>  	if (git_config_parse_key(key, &canonical_name, NULL))
>  		return -1;
>  
> -	ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
> +	ret = (kvi_fn(fn, canonical_name, value, kvi, data) < 0) ? -1 : 0;
>  	free(canonical_name);
>  	return ret;
>  }

This is pre-existing, but I'd much prefer as we're doing more
formalization of this interface if this were just:

	ret = kvi_fn(...);
	...;
	return ret;

I.e. a look at the current code shows us that the API users of
git_config_parse_parameter() are already doing this coercion themselves,
i.e. they only care about "ret < 0".

So let's just hand them the actual return value, rather than doing the
needless coercion.

> @@ -2423,19 +2429,13 @@ 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) {
> +	if (reader->source && 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->scope = reader->parsing_scope;
> +	} else
> +		kvi_from_param(kv_info);

Missing a {} here.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor
  2023-04-21 19:13 ` [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
@ 2023-05-01 11:19   ` Ævar Arnfjörð Bjarmason
  2023-05-05 21:07     ` Jonathan Tan
  2023-05-08 21:00     ` Glen Choo
  0 siblings, 2 replies; 115+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2023-05-01 11:19 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget; +Cc: git, Jonathan Tan, Emily Shaffer, Glen Choo


On Fri, Apr 21 2023, Glen Choo via GitGitGadget wrote:

> From: Glen Choo <chooglen@google.com>

I like the general goal of this series, i.e. to get rid of the "reader"
callback, and pass stuff more explicitly.

But as I pointed out in
https://lore.kernel.org/git/RFC-cover-0.5-00000000000-20230317T042408Z-avarab@gmail.com/
I think this part (and the preceding two commits) are really taking is
down the wrong path.

To demonstrate why, here's a patch-on-top of this topic as a whole where
I renamed the "kvi" struct members. I've excluded config.c itself (as
its internals aren't interesting for the purposes of this discussion):
	
	diff --git a/builtin/config.c b/builtin/config.c
	index c9e80a4b500..00cf8ffd791 100644
	--- a/builtin/config.c
	+++ b/builtin/config.c
	@@ -198,8 +198,8 @@ static void show_config_origin(struct key_value_info *kvi, struct strbuf *buf)
	 
	-	strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
	+	strbuf_addstr(buf, config_origin_type_name(kvi->origin_type2));
	 	strbuf_addch(buf, ':');
	 	if (end_nul)
	-		strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
	+		strbuf_addstr(buf, kvi->filename2 ? kvi->filename2 : "");
	 	else
	-		quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
	+		quote_c_style(kvi->filename2 ? kvi->filename2 : "", buf, NULL, 0);
	 	strbuf_addch(buf, term);
	@@ -210,3 +210,3 @@ static void show_config_scope(struct key_value_info *kvi, struct strbuf *buf)
	 	const char term = end_nul ? '\0' : '\t';
	-	const char *scope = config_scope_name(kvi->scope);
	+	const char *scope = config_scope_name(kvi->scope2);
	 
	diff --git a/builtin/remote.c b/builtin/remote.c
	index 034998a1205..81922af3f58 100644
	--- a/builtin/remote.c
	+++ b/builtin/remote.c
	@@ -655,6 +655,6 @@ static int config_read_push_default(const char *key, const char *value,
	 
	-	info->scope = kvi->scope;
	+	info->scope = kvi->scope2;
	 	strbuf_reset(&info->origin);
	-	strbuf_addstr(&info->origin, kvi->filename);
	-	info->linenr = kvi->linenr;
	+	strbuf_addstr(&info->origin, kvi->filename2);
	+	info->linenr = kvi->linenr2;
	 
	diff --git a/config.h b/config.h
	index ffb2d76823c..d9b0470e7b7 100644
	--- a/config.h
	+++ b/config.h
	@@ -120,8 +120,8 @@ struct config_options {
	 struct key_value_info {
	-	const char *filename;
	-	int linenr;
	-	enum config_origin_type origin_type;
	-	enum config_scope scope;
	-	const char *path;
	-	struct key_value_info *prev;
	+	const char *filename2;
	+	int linenr2;
	+	enum config_origin_type origin_type2;
	+	enum config_scope scope2;
	+	const char *path2;
	+	struct key_value_info *prev2;
	 };
	diff --git a/remote.c b/remote.c
	index 5239dfeab55..1cb465c6c17 100644
	--- a/remote.c
	+++ b/remote.c
	@@ -417,4 +417,4 @@ static int handle_config(const char *key, const char *value,
	 	remote->origin = REMOTE_CONFIG;
	-	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
	-	    kvi->scope == CONFIG_SCOPE_WORKTREE)
	+	if (kvi->scope2 == CONFIG_SCOPE_LOCAL ||
	+	    kvi->scope2 == CONFIG_SCOPE_WORKTREE)
	 		remote->configured_in_repo = 1;
	diff --git a/t/helper/test-config.c b/t/helper/test-config.c
	index 737505583d4..fa89cdd084c 100644
	--- a/t/helper/test-config.c
	+++ b/t/helper/test-config.c
	@@ -54,6 +54,6 @@ static int iterate_cb(const char *var, const char *value,
	 	printf("value=%s\n", value ? value : "(null)");
	-	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
	-	printf("name=%s\n", kvi->filename ? kvi->filename : "");
	-	printf("lno=%d\n", kvi->linenr);
	-	printf("scope=%s\n", config_scope_name(kvi->scope));
	+	printf("origin=%s\n", config_origin_type_name(kvi->origin_type2));
	+	printf("name=%s\n", kvi->filename2 ? kvi->filename2 : "");
	+	printf("lno=%d\n", kvi->linenr2);
	+	printf("scope=%s\n", config_scope_name(kvi->scope2));
	 
	diff --git a/trace2/tr2_tgt_event.c b/trace2/tr2_tgt_event.c
	index 83db3c755bd..338c5a58bd9 100644
	--- a/trace2/tr2_tgt_event.c
	+++ b/trace2/tr2_tgt_event.c
	@@ -482,3 +482,3 @@ static void fn_param_fl(const char *file, int line, const char *param,
	 	struct json_writer jw = JSON_WRITER_INIT;
	-	enum config_scope scope = kvi->scope;
	+	enum config_scope scope = kvi->scope2;
	 	const char *scope_name = config_scope_name(scope);
	diff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c
	index 65e9be9c5a4..5a06042e7c3 100644
	--- a/trace2/tr2_tgt_normal.c
	+++ b/trace2/tr2_tgt_normal.c
	@@ -301,3 +301,3 @@ static void fn_param_fl(const char *file, int line, const char *param,
	 	struct strbuf buf_payload = STRBUF_INIT;
	-	enum config_scope scope = kvi->scope;
	+	enum config_scope scope = kvi->scope2;
	 	const char *scope_name = config_scope_name(scope);
	diff --git a/trace2/tr2_tgt_perf.c b/trace2/tr2_tgt_perf.c
	index f402f6e3813..96fa359183d 100644
	--- a/trace2/tr2_tgt_perf.c
	+++ b/trace2/tr2_tgt_perf.c
	@@ -445,3 +445,3 @@ static void fn_param_fl(const char *file, int line, const char *param,
	 	struct strbuf scope_payload = STRBUF_INIT;
	-	enum config_scope scope = kvi->scope;
	+	enum config_scope scope = kvi->scope2;
	 	const char *scope_name = config_scope_name(scope);

So, as this shows us your 08/14 has gone through the effort of passing
this "kvi" info to every single callback, but it's only this handful
that actually needs this information.

So, even if we *can* get this to work I don't think it's worth it,
especially as this would preclude giving these config callbacks some
"lighter" API that doesn't take the trouble of recording and ferrying
this information to them.

Of course *now* we need to always prepare this information anyway, as
anyone could access it via a global, but as the work you've done here
shows we're always doing that, but only need it for these few cases.

So I really think we could leave the vast majority of the current
callbacks alone, and just supply a new "kvi" callback. My
https://lore.kernel.org/git/RFC-patch-5.5-2b80d293c83-20230317T042408Z-avarab@gmail.com/
showed one way forward with that.

I think this should also neatly answer some of your outstanding
questions. Especially as the above shows that the only non-test caller
that needs "linenr" is the builtin/config.c caller that my proposed RFC
(linked above) tackled directly. Most of these callbacks just need the
more basic "scope".

So, in particular:

> Here's an exhaustive list of all of the changes:
>
> * Cases that need a judgement call
>
>   - trace2/tr2_cfg.c:tr2_cfg_set_fl()
>
>     This function needs to account for tr2_cfg_cb() now using "kvi".
>     Since this is only called (indirectly) by git_config_set(), config
>     source information has never been available here, so just pass NULL.
>     It will be tr2_cfg_cb()'s responsibility to not use "kvi".

Just adding a "CONFIG_SCOPE_IN_PROCESS", "CONFIG_SCOPE_SET" or whatever
you'd want to call it seems to make much more sense here, no? 

>   - builtin/checkout.c:checkout_main()
>
>     This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
>     "kvi" doesn't apply, so just pass NULL. This might be worth
>     refactoring away, since git_xmerge_config() can call
>     git_default_config().

Another example of a caller which never actually cares about this data,
so if it doesn't need to have it passed to it, it doesn't need to fake
it up either.

>   - config.c:git_config_include()
>
>     Replace the local "kvi" variable with the "kvi" parameter. This
>     makes config_include_data.config_reader obsolete, so remove it.

No comment (internal to config.c, as noted above).

> * Hard for cocci to catch
>
>   - urlmatch.c
>
>     Manually refactor the custom config callbacks in "struct
>     urlmatch_config".
>
>   - diff.h, fsck.h, grep.h, ident.h, xdiff-interface.h
>
>     "struct key_value_info" hasn't been defined yet, so forward declare
>     it. Alternatively, maybe these files should "#include config.h".

All of these problems go away if you don't insist on changing every
single caller, you'll just have a step where you remove the current
global in favor of some "config callback with kvi" info, and "make" will
spot those callers that aren't converted yet.

Those changes will be trivial enough (just the callers I noted above) to
not require the tricky cocci patch in 08/14.

> * Likely deficiencies in .cocci patch
>
>   - submodule-config.c:gitmodules_cb()
>
>     Manually refactor a parse_config() call that gets missed because it
>     uses a different "*data" arg.
>
>   - grep.h, various
>
>     Manually refactor grep_config() calls. Not sure why these don't get
>     picked up.
>
>   - config.c:git_config_include(), http.c:http_options()
>
>     Manually add "kvi" where it was missed. Not sure why they get missed.
>
>   - builtin/clone.c:write_one_config()
>
>     Manually refactor a git_clone_config() call. Probably got missed
>     because I didn't include git_config_parse_parameter().
>
>   - ident.h
>
>     Remove the UNUSED attribute. Not sure why this is the only instance
>     of this.
>
>   - git-compat-util.h, compat/mingw.[h|c]
>
>     Manually refactor noop_core_config(), platform_core_config() and
>     mingw_core_config(). I can probably add these as "manual fixups" in
>     cocci.

ditto.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor
  2023-05-01 11:19   ` Ævar Arnfjörð Bjarmason
@ 2023-05-05 21:07     ` Jonathan Tan
  2023-05-09 22:46       ` Glen Choo
  2023-05-08 21:00     ` Glen Choo
  1 sibling, 1 reply; 115+ messages in thread
From: Jonathan Tan @ 2023-05-05 21:07 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Jonathan Tan, Glen Choo via GitGitGadget, git, Emily Shaffer,
	Glen Choo

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
> But as I pointed out in
> https://lore.kernel.org/git/RFC-cover-0.5-00000000000-20230317T042408Z-avarab@gmail.com/
> I think this part (and the preceding two commits) are really taking is
> down the wrong path.
> 
> To demonstrate why, here's a patch-on-top of this topic as a whole where
> I renamed the "kvi" struct members. I've excluded config.c itself (as
> its internals aren't interesting for the purposes of this discussion):

[snip patch showing where kvi is used]

Ah, thanks for this patch. One of my objections to your proposal in the
aforementioned thread is that we would end up with a lot of callback-
taking config functions that have 2 variants, one for the kvi callback
and one for the non-kvi callback, but here your patch shows that it
won't be the case.

Your approach does look like a reasonable one.

As for my own opinions, (before Ævar sent this email) I took a look
at these patches myself and had some issues with at least the first
2: in patch 1, kvi_fn() replaces fn() for some but not all invocations
of fn() (in patch 2, you can see one such invocation that was not
changed), and I was having difficulty thinking of what kind of bugs
I should watch out for since not all invocations were changed; and
in patch 2, the safeguard of not setting kvi and source together was
removed and likewise I was having difficulty thinking of what kind of
bugs could occur from both being set at once inadvertently. I was going
to suggest reordering the patches such that the large-scale refactoring
(and any supporting patches like [PATCH 06/14] config: inline
git_color_default_config) should occur first (or waiting for a reviewer
who is convinced that patches 1 and 2 are OK, I guess), but having now
seen that sidestepping a large part of this makes sense, sidestepping
seems like a good idea to me.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor
  2023-05-01 11:19   ` Ævar Arnfjörð Bjarmason
  2023-05-05 21:07     ` Jonathan Tan
@ 2023-05-08 21:00     ` Glen Choo
  1 sibling, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-05-08 21:00 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason,
	Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Emily Shaffer

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> On Fri, Apr 21 2023, Glen Choo via GitGitGadget wrote:
>
>> From: Glen Choo <chooglen@google.com>
>
> I like the general goal of this series, i.e. to get rid of the "reader"
> callback, and pass stuff more explicitly.
>
> But as I pointed out in
> https://lore.kernel.org/git/RFC-cover-0.5-00000000000-20230317T042408Z-avarab@gmail.com/
> I think this part (and the preceding two commits) are really taking is
> down the wrong path.
>
> To demonstrate why, here's a patch-on-top of this topic as a whole where
> I renamed the "kvi" struct members. I've excluded config.c itself (as
> its internals aren't interesting for the purposes of this discussion):
>
> [Patch showing used kvi members...]
>
> So, as this shows us your 08/14 has gone through the effort of passing
> this "kvi" info to every single callback, but it's only this handful
> that actually needs this information.

The attached patch misses the majority of "kvi" users, which use it in
die_bad_number(). This change happens in Patch 13/14, where the whole
"kvi" is passed into config.c machinery. It's still a much smaller
number of config callbacks than the ones changed in these few patches,
but it's more than a handful, I'd think.

> So, even if we *can* get this to work I don't think it's worth it,
> especially as this would preclude giving these config callbacks some
> "lighter" API that doesn't take the trouble of recording and ferrying
> this information to them.

I assume the "lighter API" refers to the current API?

If I'm reading this correctly, your concern isn't primarily about
reducing churn, it's YAGNI - since most callers don't need this info,
they should be able to reap the benefits of an API that doesn't provide
that info. Thus, you're proposing to having both the current API and a
new kvi-based one?

If so, I don't think that's a good route to take:

- Having both *_kvi() and "old" variants will add bloat to an already
  bloated config API. Even without further changes, we'd need to add
  *_kvi() to at least config_with_options() and repo_config(), as well
  as all of the functions that config_with_options() uses under the
  hood (some of which are public).

  To some extent, this can be managed by shrinking the config API (e.g.
  like your suggestion to get rid of git_config_from_file() [1]), but
  IMO that needs more discussion.

- What benefit is the old API giving us vs the new one? It won't be any
  faster since the machinery will need to support the new API, and even
  if it were, the benefit would be tiny. In some cases it might be nice
  to use the config callback in a non-config setting, but this patch
  shows that those are rare and can be easily worked around.

- I strongly suspect that we'd already like "kvi" in more places, and if
  we made it readily available, we would add these additional callers.
  E.g. we already use it to give additional diagnostics when failing to
  parse a number, and there's no reason why we shouldn't do this when
  doing other kinds of parsing (e.g. date in git_config_expiry_date() or
  color in color_parse_mem()).

If your concern really is primarily about churn and we actually want to
move everything to the new API eventually, wouldn't it be better to bite
the bullet and take a one time, well-scoped churn cost instead of a
longer migration?

[1] https://lore.kernel.org/git/230307.86wn3szrzu.gmgdl@evledraar.gmail.com/

> I think this should also neatly answer some of your outstanding
> questions. Especially as the above shows that the only non-test caller
> that needs "linenr" is the builtin/config.c caller that my proposed RFC
> (linked above) tackled directly. Most of these callbacks just need the
> more basic "scope".

I didn't see where builtin/config.c was handled in the above link.
Perhaps you meant a different RFC,
https://github.com/avar/git/commit/0233297a359bbda43a902dd0213aacdca82faa34?

> So, in particular:
>
>> Here's an exhaustive list of all of the changes:
>>
>> * Cases that need a judgement call
>>
>>   - trace2/tr2_cfg.c:tr2_cfg_set_fl()
>>
>>     This function needs to account for tr2_cfg_cb() now using "kvi".
>>     Since this is only called (indirectly) by git_config_set(), config
>>     source information has never been available here, so just pass NULL.
>>     It will be tr2_cfg_cb()'s responsibility to not use "kvi".
>
> Just adding a "CONFIG_SCOPE_IN_PROCESS", "CONFIG_SCOPE_SET" or whatever
> you'd want to call it seems to make much more sense here, no? 

CONFIG_SCOPE_SET makes sense.

>>   - builtin/checkout.c:checkout_main()
>>
>>     This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
>>     "kvi" doesn't apply, so just pass NULL. This might be worth
>>     refactoring away, since git_xmerge_config() can call
>>     git_default_config().
>
> Another example of a caller which never actually cares about this data,
> so if it doesn't need to have it passed to it, it doesn't need to fake
> it up either.

Here's a case of YAGNI I mentioned above and I agree it would be nice to
not have to fake a "kvi". However, git_xmerge_config() can call
git_defualt_config() so this seems more like it's abusing the config
callback to parse a CLI arg. A better resolution would be to have a
function dedicated to parsing "merge.conflictstyle".

>> * Hard for cocci to catch
>>
>>   - urlmatch.c
>>
>>     Manually refactor the custom config callbacks in "struct
>>     urlmatch_config".
>>
>>   - diff.h, fsck.h, grep.h, ident.h, xdiff-interface.h
>>
>>     "struct key_value_info" hasn't been defined yet, so forward declare
>>     it. Alternatively, maybe these files should "#include config.h".
>
> All of these problems go away if you don't insist on changing every
> single caller, you'll just have a step where you remove the current
> global in favor of some "config callback with kvi" info, and "make" will
> spot those callers that aren't converted yet.
>
> Those changes will be trivial enough (just the callers I noted above) to
> not require the tricky cocci patch in 08/14.
>
>> * Likely deficiencies in .cocci patch
>>
>>   - submodule-config.c:gitmodules_cb()
>>
>>     Manually refactor a parse_config() call that gets missed because it
>>     uses a different "*data" arg.
>>
>>   - grep.h, various
>>
>>     Manually refactor grep_config() calls. Not sure why these don't get
>>     picked up.
>>
>>   - config.c:git_config_include(), http.c:http_options()
>>
>>     Manually add "kvi" where it was missed. Not sure why they get missed.
>>
>>   - builtin/clone.c:write_one_config()
>>
>>     Manually refactor a git_clone_config() call. Probably got missed
>>     because I didn't include git_config_parse_parameter().
>>
>>   - ident.h
>>
>>     Remove the UNUSED attribute. Not sure why this is the only instance
>>     of this.
>>
>>   - git-compat-util.h, compat/mingw.[h|c]
>>
>>     Manually refactor noop_core_config(), platform_core_config() and
>>     mingw_core_config(). I can probably add these as "manual fixups" in
>>     cocci.
>
> ditto.

Yeah, there was definitely more cocci trickery than I'd like. Btw, if
you comments on the .cocci file itself, feel free to share. I obviously
just hacked together something based on a very rudimentary understanding
of cocci and I'd love suggestions on how to improve.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor
  2023-05-05 21:07     ` Jonathan Tan
@ 2023-05-09 22:46       ` Glen Choo
  2023-05-11 16:21         ` Jonathan Tan
  0 siblings, 1 reply; 115+ messages in thread
From: Glen Choo @ 2023-05-09 22:46 UTC (permalink / raw)
  To: Jonathan Tan, Ævar Arnfjörð Bjarmason
  Cc: Jonathan Tan, Glen Choo via GitGitGadget, git, Emily Shaffer

I've covered most your response to Ævar upthread, so I'll omit that.

Jonathan Tan <jonathantanmy@google.com> writes:

> As for my own opinions, (before Ævar sent this email) I took a look
> at these patches myself and had some issues with at least the first
> 2: in patch 1, kvi_fn() replaces fn() for some but not all invocations
> of fn() (in patch 2, you can see one such invocation that was not
> changed), and I was having difficulty thinking of what kind of bugs
> I should watch out for since not all invocations were changed; and
> in patch 2, the safeguard of not setting kvi and source together was
> removed and likewise I was having difficulty thinking of what kind of
> bugs could occur from both being set at once inadvertently. I was going
> to suggest reordering the patches such that the large-scale refactoring
> (and any supporting patches like [PATCH 06/14] config: inline
> git_color_default_config) should occur first (or waiting for a reviewer
> who is convinced that patches 1 and 2 are OK, I guess), but having now
> seen that sidestepping a large part of this makes sense, sidestepping
> seems like a good idea to me.

In an off-list discussion, we described some plausible ways to organize
the refactor that would make it easier for a reviewer to confirm safety.

I haven't tried that yet because it sounds like you'd prefer the
sidestepping approach. Do you prefer that primarily for safety reasons,
or is it largely motivated by other concerns too (e.g. reducing churn or
sidestepping produces a better API)? If the primary concern is just
safety, I'm somewhat confident that we can find some way to organize
this that makes it easier to review and I should just do it.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor
  2023-05-09 22:46       ` Glen Choo
@ 2023-05-11 16:21         ` Jonathan Tan
  0 siblings, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-05-11 16:21 UTC (permalink / raw)
  To: Glen Choo
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Glen Choo via GitGitGadget, git, Emily Shaffer

Glen Choo <chooglen@google.com> writes:
> I've covered most your response to Ævar upthread, so I'll omit that.

Thanks. Indeed, I missed the situation in which a caller used kvi not
by accessing its fields directly but by passing kvi to a function in
config.c.

> In an off-list discussion, we described some plausible ways to organize
> the refactor that would make it easier for a reviewer to confirm safety.
> 
> I haven't tried that yet because it sounds like you'd prefer the
> sidestepping approach. Do you prefer that primarily for safety reasons,
> or is it largely motivated by other concerns too (e.g. reducing churn or
> sidestepping produces a better API)? If the primary concern is just
> safety, I'm somewhat confident that we can find some way to organize
> this that makes it easier to review and I should just do it.

My preference for the sidestepping approach was to reduce churn, but as
you have pointed out, it doesn't actually reduce churn. So now I think
that the patches should be reordered (but am open to being convinced
otherwise, of course).

As for safety and better API, I think both approaches (bulk modification
of all functions to take the new config_fn_t and two sets of functions
each taking a different function type) are equally safe, and it is
bulk modification that results in a better API (as you've demonstrated,
having kvi information is needed for good error messages, and I expect
that to be more and more needed).

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v2 00/14] [RFC] config: remove global state from config iteration
  2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                   ` (13 preceding siblings ...)
  2023-04-21 19:13 ` [PATCH 14/14] config: remove config_reader from configset_add_value Glen Choo via GitGitGadget
@ 2023-05-30 18:41 ` Glen Choo via GitGitGadget
  2023-05-30 18:41   ` [PATCH v2 01/14] config: inline git_color_default_config Glen Choo via GitGitGadget
                     ` (14 more replies)
  14 siblings, 15 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:41 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

Thanks for the review on the previous round!

This v2 is mostly a reorganization of v1, largely based off Jonathan's
feedback in [1]. As such, nearly all of v1's cover letter still applies
(except for the "Patch Overview" section [2]) so this cover letter omits
those bits that have already been covered. The first non-RFC version will
get rerolled with full cover letter.

Since a lot of this series removes the config_reader stuff from
gc/config-parsing-cleanup, it might be useful to diff this with "master"
right before that:

git diff 0a8c337394 HEAD -- config.c

= Changes since v1

 * Reorganize patches in a (hopefully) easier-to-review way.

 * Squash bugs in builtin/config.c that became apparent during the refactor.
   These could have been fixed in v1, but they dropped off my radar.

= Patch overview

 * 1-5/14 add the "kvi" parameter to the config_fn_t signature. These are
   mostly unchanged from v1.

 * 6-11/14 converts the config.c machinery off "config_reader.config_kvi"
   and "config_reader.source" and onto the new "kvi" arg. Most of the
   changes from v1 are here.
   
   In v1, we converted all of the config.c machinery first, making the "kvi"
   arg available everywhere before the refactor. Thus we could convert all
   callers of the current_*() API to the "kvi" arg in a single step. However
   (as Jonathan rightfully pointed out), some of the changes to the
   machinery are non-trivial, and it's quite difficult to spot bugs in the
   intermediate patches.
   
   In v2, we convert the config.c machinery from "config_reader" to "kvi"
   one-by-one: configsets, then files, then CLI. To exercise the "kvi" arg
   as soon as possible, we convert from current_*() to "kvi" as soon as it
   is available. For example, in 6/14 "kvi" is available only in configsets,
   so we convert the current_*() call sites that are only reached via
   configsets and leave the others untouched. This means that we have a mix
   of current_*() and "kvi" in the middle, but auditing the changes is
   relatively easy (compared to v1's machinery changes), since you only need
   to verify that a callback isn't relying on the "kvi" arg before it is
   available, and that current_*() and "kvi" give the same value.
   
   * 8-9/14 squashes some bugs where builtin/config.c was calling the
     current_*() API outside of config callbacks. The "kvi" plumbing doesn't
     just make the bugs apparent, it also provides an obvious way to fix the
     bugs (by injecting "kvi" into the right places in builtin/config.c).
     These would have been nontrivial to fix if we were still using global
     state.

 * 12-14/14 remove config_reader by taking advantage of the "kvi" parameter
   and doing some other light plumbing.

[1]
https://lore.kernel.org/git/20230505210702.3359841-1-jonathantanmy@google.com
[2]
https://lore.kernel.org/pull.1497.git.git.1682104398.gitgitgadget@gmail.com

Glen Choo (14):
  config: inline git_color_default_config
  urlmatch.h: use config_fn_t type
  (RFC-only) config: add kvi arg to config_fn_t
  (RFC-only) config: apply cocci to config_fn_t implementations
  (RFC-only) config: finish config_fn_t refactor
  config.c: pass kvi in configsets
  config: provide kvi with config files
  builtin/config.c: test misuse of format_config()
  config.c: provide kvi with CLI config
  trace2: plumb config kvi
  config: pass kvi to die_bad_number()
  config.c: remove config_reader from configsets
  config: add kvi.path, use it to evaluate includes
  config: pass source to config_parser_event_fn_t

 alias.c                                       |   3 +-
 archive-tar.c                                 |   5 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   5 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   8 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |   7 +-
 builtin/clean.c                               |   9 +-
 builtin/clone.c                               |  10 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   3 +-
 builtin/commit.c                              |  20 +-
 builtin/config.c                              |  65 ++-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |  12 +-
 builtin/fsmonitor--daemon.c                   |  11 +-
 builtin/grep.c                                |  12 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   9 +-
 builtin/log.c                                 |  12 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |  19 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |  15 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |  12 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   8 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   3 +-
 builtin/tag.c                                 |   9 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   9 +-
 color.c                                       |   8 -
 color.h                                       |   6 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      | 538 +++++++-----------
 config.h                                      |  54 +-
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_kvi.pending.cocci    | 146 +++++
 contrib/coccinelle/git_config_number.cocci    |  27 +
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   3 +-
 diff.c                                        |  19 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   7 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |  11 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   6 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |  10 +-
 http.c                                        |  15 +-
 ident.c                                       |   3 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   7 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   2 +-
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   3 +-
 notes.c                                       |   3 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   7 +-
 revision.c                                    |   3 +-
 scalar.c                                      |   3 +-
 sequencer.c                                   |  28 +-
 setup.c                                       |  17 +-
 submodule-config.c                            |  31 +-
 submodule-config.h                            |   3 +-
 t/helper/test-config.c                        |  21 +-
 t/helper/test-userdiff.c                      |   4 +-
 t/t1300-config.sh                             |  27 +
 trace2.c                                      |   4 +-
 trace2.h                                      |   3 +-
 trace2/tr2_cfg.c                              |  12 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trace2/tr2_tgt.h                              |   4 +-
 trace2/tr2_tgt_event.c                        |   4 +-
 trace2/tr2_tgt_normal.c                       |   4 +-
 trace2/tr2_tgt_perf.c                         |   4 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |  18 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   8 +-
 worktree.c                                    |   2 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   5 +-
 103 files changed, 883 insertions(+), 642 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_kvi.pending.cocci
 create mode 100644 contrib/coccinelle/git_config_number.cocci


base-commit: 9857273be005833c71e2d16ba48e193113e12276
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1497%2Fchooglen%2Fconfig%2Fno-global-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1497/chooglen/config/no-global-v2
Pull-Request: https://github.com/git/git/pull/1497

Range-diff vs v1:

  1:  49bc2f6eedc <  -:  ----------- config.c: introduce kvi_fn(), use it for configsets
  6:  cb021810688 =  1:  d5edf7e3fdd config: inline git_color_default_config
  7:  e0f43eafa07 =  2:  821f0b90580 urlmatch.h: use config_fn_t type
  8:  961d06f89cb !  3:  6834e37066e (RFC-only) config: add kvi arg to config_fn_t
     @@ Commit message
          Signed-off-by: Glen Choo <chooglen@google.com>
      
       ## config.c ##
     -@@ config.c: static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     - {
     - 	int ret;
     - 	config_reader_push_kvi(&the_reader, kvi);
     --	ret = fn(key, value, data);
     -+	ret = fn(key, value, kvi, data);
     - 	config_reader_pop_kvi(&the_reader);
     +@@ config.c: 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, NULL, inc->data);
     + 	if (ret < 0)
     + 		return ret;
     + 
     +@@ config.c: static int config_parse_pair(const char *key, const char *value,
     + 	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, NULL, data) < 0) ? -1 : 0;
     + 	free(canonical_name);
       	return ret;
       }
     +@@ config.c: 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);
     ++	ret = fn(name->buf, value, NULL, data);
     + 	if (ret >= 0)
     + 		cs->linenr++;
     + 	return ret;
     +@@ config.c: static void configset_iter(struct config_reader *reader, struct config_set *set,
     + 
     + 		config_reader_set_kvi(reader, values->items[value_index].util);
     + 
     +-		if (fn(entry->key, values->items[value_index].string, data) < 0)
     ++		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
     + 			git_die_config_linenr(entry->key,
     + 					      reader->config_kvi->filename,
     + 					      reader->config_kvi->linenr);
      
       ## config.h ##
      @@ config.h: struct config_options {
     @@ config.h: struct config_options {
      +	int linenr;
      +	enum config_origin_type origin_type;
      +	enum config_scope scope;
     -+	const char *path;
     -+	struct key_value_info *prev;
      +};
      +
       /**
     @@ config.h: int git_config_get_expiry(const char *key, const char **output);
      -	int linenr;
      -	enum config_origin_type origin_type;
      -	enum config_scope scope;
     --	const char *path;
     --	struct key_value_info *prev;
      -};
      -
       /**
  9:  5eb3874b494 !  4:  bd52c6232ec (RFC-only) config: apply cocci to config_fn_t implementations
     @@ config.c: static void populate_remote_urls(struct config_include_data *inc)
       			     void *data UNUSED)
       {
       	const char *remote_name;
     -@@ config.c: static int include_condition_is_true(struct key_value_info *kvi,
     +@@ config.c: static int include_condition_is_true(struct config_source *cs,
     + 	return 0;
     + }
       
     - static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     - 		  struct key_value_info *kvi, void *data);
      -static int git_config_include(const char *var, const char *value, void *data)
      +static int git_config_include(const char *var, const char *value,
      +			      struct key_value_info *kvi UNUSED, void *data)
       {
       	struct config_include_data *inc = data;
     - 	struct key_value_info *kvi = inc->config_reader->config_kvi;
     + 	struct config_source *cs = inc->config_reader->source;
      @@ config.c: int git_config_color(char *dest, const char *var, const char *value)
       	return 0;
       }
     @@ config.c: struct configset_add_data {
       {
       	struct configset_add_data *data = cb;
       	configset_add_value(data->config_reader, data->config_set, key, value);
     -@@ config.c: static int store_aux_event(enum config_event_t type, size_t begin, size_t end,
     +@@ config.c: static int store_aux_event(enum config_event_t type,
       	return 0;
       }
       
 10:  1071e70c928 !  5:  f363b160259 (RFC-only) config: finish config_fn_t refactor
     @@ Commit message
              refactoring away, since git_xmerge_config() can call
              git_default_config().
      
     -      - config.c:git_config_include()
     -
     -        Replace the local "kvi" variable with the "kvi" parameter. This
     -        makes config_include_data.config_reader obsolete, so remove it.
     -
          * Hard for cocci to catch
      
            - urlmatch.c
     @@ compat/mingw.h: typedef _sigset_t sigset_t;
       
       /*
      
     - ## config.c ##
     -@@ config.c: struct config_include_data {
     - 	void *data;
     - 	const struct config_options *opts;
     - 	struct git_config_source *config_source;
     --	struct config_reader *config_reader;
     - 
     - 	/*
     - 	 * All remote URLs discovered when reading all config files.
     -@@ config.c: static int include_condition_is_true(struct key_value_info *kvi,
     - static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     - 		  struct key_value_info *kvi, void *data);
     - static int git_config_include(const char *var, const char *value,
     --			      struct key_value_info *kvi UNUSED, void *data)
     -+			      struct key_value_info *kvi, void *data)
     - {
     - 	struct config_include_data *inc = data;
     --	struct key_value_info *kvi = inc->config_reader->config_kvi;
     - 	const char *cond, *key;
     - 	size_t cond_len;
     - 	int ret;
     -@@ config.c: int config_with_options(config_fn_t fn, void *data,
     - 		inc.data = data;
     - 		inc.opts = opts;
     - 		inc.config_source = config_source;
     --		inc.config_reader = &the_reader;
     - 		fn = git_config_include;
     - 		data = &inc;
     - 	}
     -
       ## diff.h ##
      @@ diff.h: void free_diffstat_info(struct diffstat_t *diffstat);
       int parse_long_opt(const char *opt, const char **argv,
 11:  b38653477c7 !  6:  f57c1007cad config: remove current_config_(line|name|origin_type)
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config: remove current_config_(line|name|origin_type)
     +    config.c: pass kvi in configsets
      
     -    Trivially replace current_config_(line|name|origin_type) by reading the
     -    corresponding values from "struct key_value_info". This includes some
     -    light "kvi" plumbing for builtin/config.c, and for *origin_type,
     -    splitting out a function that turns "enum config_origin_type" into the
     -    human-readable string that callbacks actually want.
     +    Trivially pass "struct key_value_info" to config callbacks in
     +    configset_iter(). Then, in config callbacks that are only used with
     +    configsets, use the "kvi" arg to replace calls to current_config_*(),
     +    and delete current_config_line() because it has no remaining callers.
      
     -    Signed-off-by: Glen Choo <chooglen@google.com>
     +    This leaves builtin/config.c and config.c as the only remaining users of
     +    current_config_*().
      
     - ## builtin/config.c ##
     -@@ builtin/config.c: 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(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);
     - }
     - 
     -@@ builtin/config.c: static void show_config_scope(struct strbuf *buf)
     - }
     - 
     - static int show_all_config(const char *key_, const char *value_,
     --			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
     -+			   struct key_value_info *kvi, void *cb UNUSED)
     - {
     - 	if (show_origin || show_scope) {
     - 		struct strbuf buf = STRBUF_INIT;
     - 		if (show_scope)
     - 			show_config_scope(&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);
     -@@ builtin/config.c: 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_,
     -+			 struct key_value_info *kvi)
     - {
     - 	if (show_scope)
     - 		show_config_scope(buf);
     - 	if (show_origin)
     --		show_config_origin(buf);
     -+		show_config_origin(kvi, buf);
     - 	if (show_keys)
     - 		strbuf_addstr(buf, key_);
     - 	if (!omit_values) {
     -@@ builtin/config.c: static int format_config(struct strbuf *buf, const char *key_, const char *value
     - }
     - 
     - static int collect_config(const char *key_, const char *value_,
     --			  struct key_value_info *kvi UNUSED, void *cb)
     -+			  struct key_value_info *kvi, void *cb)
     - {
     - 	struct strbuf_list *values = cb;
     - 
     -@@ builtin/config.c: static int collect_config(const char *key_, const char *value_,
     - 	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)
     -@@ builtin/config.c: static int get_value(const char *key_, const char *regex_, unsigned flags)
     - 		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, NULL) < 0)
     - 			die(_("failed to format default config value: %s"),
     - 				default_value);
     - 	}
     -@@ builtin/config.c: 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, NULL);
     - 		fwrite(buf.buf, 1, buf.len, stdout);
     - 		strbuf_release(&buf);
     - 
     +    Signed-off-by: Glen Choo <chooglen@google.com>
      
       ## builtin/remote.c ##
      @@ builtin/remote.c: struct push_default_info
     @@ builtin/remote.c: struct push_default_info
       {
       	struct push_default_info* info = cb;
       	if (strcmp(key, "remote.pushdefault") ||
     -@@ builtin/remote.c: static int config_read_push_default(const char *key, const char *value,
     + 	    !value || strcmp(value, info->old_name))
     + 		return 0;
       
     - 	info->scope = current_config_scope();
     +-	info->scope = current_config_scope();
     ++	info->scope = kvi->scope;
       	strbuf_reset(&info->origin);
      -	strbuf_addstr(&info->origin, current_config_name());
      -	info->linenr = current_config_line();
     -+	strbuf_addstr(&info->origin, kvi->filename);
     ++	strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
      +	info->linenr = kvi->linenr;
       
       	return 0;
       }
      
       ## config.c ##
     +@@ config.c: static void configset_iter(struct config_reader *reader, struct config_set *set,
     + 	struct string_list *values;
     + 	struct config_set_element *entry;
     + 	struct configset_list *list = &set->list;
     ++	struct key_value_info *kvi;
     + 
     + 	for (i = 0; i < list->nr; i++) {
     + 		entry = list->items[i].e;
     + 		value_index = list->items[i].value_index;
     + 		values = &entry->value_list;
     ++		kvi = values->items[value_index].util;
     + 
     + 		config_reader_set_kvi(reader, values->items[value_index].util);
     + 
     +-		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
     +-			git_die_config_linenr(entry->key,
     +-					      reader->config_kvi->filename,
     +-					      reader->config_kvi->linenr);
     +-
     ++		if (fn(entry->key, values->items[value_index].string, kvi, data) < 0)
     ++			git_die_config_linenr(entry->key, kvi->filename, kvi->linenr);
     + 		config_reader_set_kvi(reader, NULL);
     + 	}
     + }
      @@ config.c: static int reader_origin_type(struct config_reader *reader,
       	return 0;
       }
     @@ config.c: static int reader_origin_type(struct config_reader *reader,
       	switch (type) {
       	case CONFIG_ORIGIN_BLOB:
       		return "blob";
     -@@ config.c: static int reader_config_name(struct config_reader *reader, const char **out)
     - 	return 0;
     +@@ config.c: const char *current_config_origin_type(void)
     + 	}
       }
       
     --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)
     ++const char *current_config_origin_type(void)
     ++{
     ++	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
     ++
     ++	if (reader_origin_type(&the_reader, &type))
     ++		BUG("current_config_origin_type called outside config callback");
     ++
     ++	return config_origin_type_name(type);
     ++}
     ++
     + const char *config_scope_name(enum config_scope scope)
       {
     - 	if (the_reader.config_kvi)
     + 	switch (scope) {
      @@ config.c: enum config_scope current_config_scope(void)
     - 		return CONFIG_SCOPE_UNKNOWN;
     + 		return the_reader.parsing_scope;
       }
       
      -int current_config_line(void)
     @@ config.c: enum config_scope current_config_scope(void)
      -	if (the_reader.config_kvi)
      -		return the_reader.config_kvi->linenr;
      -	else
     --		BUG("current_config_line called outside config callback");
     +-		return the_reader.source->linenr;
      -}
      -
       int lookup_config(const char **mapping, int nr_mapping, const char *var)
     @@ config.c: enum config_scope current_config_scope(void)
       	int i;
      
       ## config.h ##
     -@@ config.h: void git_global_config(char **user, char **xdg);
     - int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
     - 
     +@@ config.h: int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
       enum config_scope current_config_scope(void);
     --const char *current_config_origin_type(void);
     --const char *current_config_name(void);
     + const char *current_config_origin_type(void);
     + const char *current_config_name(void);
      -int current_config_line(void);
      +const char *config_origin_type_name(enum config_origin_type type);
       
       /*
        * Match and parse a config key of the form:
      
     + ## remote.c ##
     +@@ remote.c: static void read_branches_file(struct remote_state *remote_state,
     + }
     + 
     + static int handle_config(const char *key, const char *value,
     +-			 struct key_value_info *kvi UNUSED, void *cb)
     ++			 struct key_value_info *kvi, void *cb)
     + {
     + 	const char *name;
     + 	size_t namelen;
     +@@ remote.c: static int handle_config(const char *key, const char *value,
     + 	}
     + 	remote = make_remote(remote_state, name, namelen);
     + 	remote->origin = REMOTE_CONFIG;
     +-	if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
     +-	    current_config_scope() == CONFIG_SCOPE_WORKTREE)
     ++	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
     ++	    kvi->scope == CONFIG_SCOPE_WORKTREE)
     + 		remote->configured_in_repo = 1;
     + 	if (!strcmp(subkey, "mirror"))
     + 		remote->mirror = git_config_bool(key, value);
     +
       ## t/helper/test-config.c ##
      @@
        */
     @@ t/helper/test-config.c: static int iterate_cb(const char *var, const char *value
      -	printf("origin=%s\n", current_config_origin_type());
      -	printf("name=%s\n", current_config_name());
      -	printf("lno=%d\n", current_config_line());
     +-	printf("scope=%s\n", config_scope_name(current_config_scope()));
      +	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
      +	printf("name=%s\n", kvi->filename ? kvi->filename : "");
      +	printf("lno=%d\n", kvi->linenr);
     - 	printf("scope=%s\n", config_scope_name(current_config_scope()));
     ++	printf("scope=%s\n", config_scope_name(kvi->scope));
       
       	return 0;
     + }
  3:  8c7a84137c8 !  7:  641a56f0b40 config: use kvi for config files
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config: use kvi for config files
     +    config: provide kvi with config files
      
     -    Plumb "struct key_value_info" and use "kvi_fn()" when parsing config
     -    files. As a result, "config_reader.kvi" is now always set correctly, so
     -    we can remove "config_reader.scope" (but not the ".source" member since
     -    that's still needed by some non-parsing machinery). This requires
     -    plumbing an additional "enum config_scope" arg through
     -    "git_config_from_file_with_options()" and the underlying machinery to
     -    make up for the fact that "struct key_value_info" has a ".scope" member,
     -    but "struct config_source" does not.
     +    Refactor out the configset logic that caches "struct config_source" and
     +    "enum config_scope" as a "struct key_value_info", and use it to pass the
     +    "kvi" arg to config callbacks when parsing config files. Get the "enum
     +    config_scope" value by plumbing an additional arg through
     +    git_config_from_file_with_options() and the underlying machinery.
      
     -    To handle "include" directives correctly, use push/pop semantics for
     -    "config_reader.config_kvi" (instead of "set" semantics) like we do for
     -    "config_reader.source". Otherwise, "config_reader.config_kvi" won't be
     -    set correctly when we finish parsing an included config file and we want
     -    to "pop" it to resume parsing the original file. This distinction only
     -    matters while there is a global "kvi", i.e. it will be obsolete at the
     -    end of the series.
     +    We do not exercise the "kvi" arg yet because the remaining
     +    current_config_*() callers may be used with config_with_options(), which
     +    may read config from parameters, but parameters don't pass "kvi" yet.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     @@ bundle-uri.c: int bundle_uri_parse_config_format(const char *uri,
       	if (!result && list->mode == BUNDLE_MODE_NONE) {
      
       ## config.c ##
     -@@ config.c: struct config_reader {
     - 	 */
     - 	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
     -@@ config.c: static inline struct config_source *config_reader_pop_source(struct config_reade
     - 	return ret;
     - }
     - 
     --static inline void config_reader_set_kvi(struct config_reader *reader,
     --					 struct key_value_info *kvi)
     -+static inline void config_reader_push_kvi(struct config_reader *reader,
     -+					  struct key_value_info *kvi)
     - {
     -+	kvi->prev = reader->config_kvi;
     - 	reader->config_kvi = kvi;
     - }
     - 
     --static inline void config_reader_set_scope(struct config_reader *reader,
     --					   enum config_scope scope)
     -+static inline struct key_value_info *config_reader_pop_kvi(struct config_reader *reader)
     - {
     --	reader->parsing_scope = scope;
     -+	struct key_value_info *ret;
     -+	if (!reader->config_kvi)
     -+		BUG("tried to pop config_kvi, but we weren't reading config");
     -+	ret = reader->config_kvi;
     -+	reader->config_kvi = reader->config_kvi->prev;
     -+	return ret;
     - }
     - 
     - static int pack_compression_seen;
      @@ config.c: static int handle_path_include(struct config_source *cs, const char *path,
       			    !cs ? "<unknown>" :
       			    cs->name ? cs->name :
     @@ config.c: static int handle_path_include(struct config_source *cs, const char *p
       		inc->depth--;
       	}
       cleanup:
     -@@ config.c: 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, &opts);
     --
     --	config_reader_set_scope(inc->config_reader, store_scope);
     - }
     - 
     - static int forbid_remote_url(const char *var, const char *value UNUSED,
      @@ config.c: static int include_condition_is_true(struct config_source *cs,
     - 	return 0;
       }
       
     -+static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     -+		  struct key_value_info *kvi, void *data);
     - static int git_config_include(const char *var, const char *value, void *data)
     + static int git_config_include(const char *var, const char *value,
     +-			      struct key_value_info *kvi UNUSED, void *data)
     ++			      struct key_value_info *kvi, void *data)
       {
       	struct config_include_data *inc = data;
       	struct config_source *cs = inc->config_reader->source;
     -+	struct key_value_info *kvi = inc->config_reader->config_kvi;
     - 	const char *cond, *key;
     - 	size_t cond_len;
     - 	int ret;
     -@@ config.c: static int git_config_include(const char *var, const char *value, void *data)
     +@@ config.c: static int git_config_include(const char *var, const char *value,
       	 * 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 = kvi_fn(inc->fn, var, value, kvi, inc->data);
     +-	ret = inc->fn(var, value, NULL, inc->data);
     ++	ret = inc->fn(var, value, kvi, inc->data);
       	if (ret < 0)
       		return ret;
       
     -@@ config.c: out_free_ret_1:
     - }
     - 
     - static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     --		  struct key_value_info *kvi,
     --		  void *data)
     -+		  struct key_value_info *kvi, void *data)
     - {
     - 	int ret;
     --	config_reader_set_kvi(&the_reader, kvi);
     -+	config_reader_push_kvi(&the_reader, kvi);
     - 	ret = fn(key, value, data);
     --	config_reader_set_kvi(&the_reader, NULL);
     -+	config_reader_pop_kvi(&the_reader);
     - 	return ret;
     - }
     - 
      @@ config.c: static char *parse_value(struct config_source *cs)
       	}
       }
     @@ config.c: static int get_value(struct config_source *cs, config_fn_t fn, void *d
       	 * accurate line number in error messages.
       	 */
       	cs->linenr--;
     --	ret = fn(name->buf, value, data);
     +-	ret = fn(name->buf, value, NULL, data);
      +	kvi->linenr = cs->linenr;
     -+	ret = kvi_fn(fn, name->buf, value, kvi, data);
     ++	ret = fn(name->buf, value, kvi, data);
       	if (ret >= 0)
       		cs->linenr++;
       	return ret;
     @@ config.c: static int git_parse_source(struct config_source *cs, config_fn_t fn,
       			break;
       	}
       
     -@@ config.c: int git_default_config(const char *var, const char *value, void *cb)
     +@@ config.c: int git_default_config(const char *var, const char *value,
        * this function.
        */
       static int do_config_from(struct config_reader *reader,
     @@ config.c: static int do_config_from_file(struct config_reader *reader,
      -	ret = do_config_from(reader, &top, fn, data, opts);
      +	ret = do_config_from(reader, &top, fn, data, scope, opts);
       	funlockfile(f);
     -+
       	return ret;
       }
       
     @@ config.c: int git_config_from_blob_oid(config_fn_t fn,
       }
       
       char *git_system_config(void)
     -@@ config.c: 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,
     - 				  config_fn_t fn, void *data)
     - {
     - 	int ret = 0;
      @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     - 	char *xdg_config = NULL;
     - 	char *user_config = NULL;
     - 	char *repo_config;
     --	enum config_scope prev_parsing_scope = reader->parsing_scope;
     - 
     - 	if (opts->commondir)
     - 		repo_config = mkpathdup("%s/config", opts->commondir);
     -@@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     - 	else
     - 		repo_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))
     @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
      +							 data, CONFIG_SCOPE_SYSTEM,
      +							 NULL);
       
     --	config_reader_set_scope(reader, CONFIG_SCOPE_GLOBAL);
     + 	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))
     @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
      +		ret += git_config_from_file_with_options(fn, user_config, data,
      +							 CONFIG_SCOPE_GLOBAL, NULL);
       
     --	config_reader_set_scope(reader, CONFIG_SCOPE_LOCAL);
     + 	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);
     + 	config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
       	if (!opts->ignore_worktree && repository_format_worktree_config) {
       		char *path = git_pathdup("config.worktree");
       		if (!access_or_die(path, R_OK, 0))
     @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
       		free(path);
       	}
       
     --	config_reader_set_scope(reader, 0);
     - 	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);
     -@@ config.c: 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) {
      @@ config.c: int config_with_options(config_fn_t fn, void *data,
     - 		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) {
     @@ config.c: int config_with_options(config_fn_t fn, void *data,
      -						data);
      +					       data, config_source->scope);
       	} else {
     --		ret = do_git_config_sequence(&the_reader, opts, fn, data);
     -+		ret = do_git_config_sequence(opts, fn, data);
     - 	}
     - 
     - 	if (inc.remote_urls) {
     - 		string_list_clear(inc.remote_urls, 0);
     - 		FREE_AND_NULL(inc.remote_urls);
     + 		ret = do_git_config_sequence(&the_reader, opts, fn, data);
       	}
     --	config_reader_set_scope(&the_reader, prev_scope);
     - 	return ret;
     - }
     - 
      @@ config.c: 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 && reader->source->name) {
     + 	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;
     --		kv_info->scope = reader->parsing_scope;
     --	} else
     --		kvi_from_param(kv_info);
     -+	memcpy(kv_info, reader->config_kvi, sizeof(struct key_value_info));
     ++		kvi_from_source(reader->source, current_config_scope(), kv_info);
     + 	} 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->scope = reader->parsing_scope;
       	si->util = kv_info;
       
       	return 0;
     @@ config.c: int git_config_set_multivar_in_file_gently(const char *config_filename
       			error(_("invalid config file %s"), config_filename);
       			ret = CONFIG_INVALID_FILE;
       			goto out_free;
     -@@ config.c: enum config_scope current_config_scope(void)
     - 	if (the_reader.config_kvi)
     - 		return the_reader.config_kvi->scope;
     - 	else
     --		return the_reader.parsing_scope;
     -+		/*
     -+		 * FIXME This should be a BUG, but tr2_list_env_vars_fl is
     -+		 * calling this outside of a config callback. This will be
     -+		 * easier to fix when we plumb kvi through the config callbacks,
     -+		 * so leave this untouched for now.
     -+		 */
     -+		return CONFIG_SCOPE_UNKNOWN;
     - }
     - 
     - int current_config_line(void)
      
       ## config.h ##
     -@@ config.h: int git_default_config(const char *, const char *, void *);
     +@@ config.h: int git_default_config(const char *, const char *, struct key_value_info *,
       int git_config_from_file(config_fn_t fn, const char *, void *);
       
       int git_config_from_file_with_options(config_fn_t fn, const char *,
     @@ config.h: int git_default_config(const char *, const char *, void *);
       void git_config_push_parameter(const char *text);
       void git_config_push_env(const char *spec);
       int git_config_from_parameters(config_fn_t fn, void *data);
     -@@ config.h: struct key_value_info {
     - 	int linenr;
     - 	enum config_origin_type origin_type;
     - 	enum config_scope scope;
     -+	struct key_value_info *prev;
     - };
     - 
     - /**
      
       ## fsck.c ##
      @@ fsck.c: static int fsck_blob(const struct object_id *oid, const char *buf,
  -:  ----------- >  8:  74f43fc727e builtin/config.c: test misuse of format_config()
  2:  a682612cff2 !  9:  3760015d2c0 config.c: use kvi for CLI config
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config.c: use kvi for CLI config
     +    config.c: provide kvi with CLI config
      
     -    Plumb "struct key_value_info" and use "kvi_fn()" when parsing CLI
     -    config. Do this by refactoring out and reusing the logic that sets the
     -    "struct key_value_info" members when caching CLI config in a configset.
     +    Refactor out kvi_from_param() from the logic that caches CLI config in
     +    configsets, and use it to pass the "kvi" arg to config callbacks when
     +    parsing CLI config. Now that "kvi" is always present when config
     +    machinery calls config callbacks, plumb "kvi" so that we can replace
     +    nearly all calls to current_config_*(). (The exception is an edge case
     +    where trace2/*.c calls current_config_scope(). That will be handled in a
     +    later commit.) Note that this results in "kvi" containing a different,
     +    more complete set of information than the mocked up "struct
     +    config_source" in git_config_from_parameters().
      
     -    This lets us get rid of the fake "struct config_source" in
     -    "git_config_from_parameters()", so we now only have to maintain one
     -    implementation. Additionally, this plumbing also reveals that
     -    "git_config_parse_parameter()" hasn't been setting either
     -    "the_reader.source" or "the_reader.config_kvi", so any calls to
     -    "current_*" would either BUG() or return *_UNKNOWN values.
     +    Plumbing "kvi" reveals a few places where we've been doing the wrong
     +    thing:
      
     -    Also, get rid of the BUG() checks that forbid setting ".config_kvi" and
     -    ".source" at the same time, since we will run afoul of that check. They
     -    will soon be unnecessary when we remove ".source".
     +    * git_config_parse_parameter() hasn't been setting config source
     +      information, so plumb "kvi" there too.
     +
     +    * "git config --get-urlmatch --show-scope" iterates config to collect
     +      values, but then attempts to display the scope after config iteration.
     +      Fix this by copying the "kvi" arg in the collection phase so that it
     +      can be read back later. This means that we can now support "git config
     +      --get-urlmatch --show-origin" (we don't allow this combination of args
     +      because of this bug), but that is left unchanged for now.
     +
     +    * "git config --default" doesn't have config source metadata when
     +      displaying the default value. Fix this by treating the default value
     +      as if it came from the command line (e.g. like we do with "git -c" or
     +      "git config --file"), using kvi_from_param().
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     - ## config.c ##
     -@@ config.c: static struct config_reader the_reader;
     - static inline void config_reader_push_source(struct config_reader *reader,
     - 					     struct config_source *top)
     + ## builtin/config.c ##
     +@@ builtin/config.c: 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(struct key_value_info *kvi, struct strbuf *buf)
       {
     --	if (reader->config_kvi)
     --		BUG("source should not be set while iterating a config set");
     - 	top->prev = reader->source;
     - 	reader->source = top;
     + 	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);
       }
     -@@ config.c: static inline struct config_source *config_reader_pop_source(struct config_reade
     - static inline void config_reader_set_kvi(struct config_reader *reader,
     - 					 struct key_value_info *kvi)
     + 
     +-static void show_config_scope(struct strbuf *buf)
     ++static void show_config_scope(struct key_value_info *kvi, struct strbuf *buf)
       {
     --	if (kvi && (reader->source || reader->parsing_scope))
     --		BUG("kvi should not be set while parsing a config source");
     - 	reader->config_kvi = kvi;
     + 	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 inline void config_reader_set_scope(struct config_reader *reader,
     - 					   enum config_scope scope)
     + static int show_all_config(const char *key_, const char *value_,
     +-			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
     ++			   struct key_value_info *kvi, void *cb UNUSED)
     + {
     + 	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);
     +@@ builtin/config.c: 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_, struct key_value_info *kvi)
       {
     --	if (scope && reader->config_kvi)
     --		BUG("scope should only be set when iterating through a config source");
     - 	reader->parsing_scope = scope;
     + 	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) {
     +@@ builtin/config.c: static int format_config(struct strbuf *buf, const char *key_, const char *value
       }
       
     -@@ config.c: static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     + static int collect_config(const char *key_, const char *value_,
     +-			  struct key_value_info *kvi UNUSED, void *cb)
     ++			  struct key_value_info *kvi, void *cb)
     + {
     + 	struct strbuf_list *values = cb;
     + 
     +@@ builtin/config.c: static int collect_config(const char *key_, const char *value_,
     + 	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)
     +@@ builtin/config.c: static int get_value(const char *key_, const char *regex_, unsigned flags)
     + 			    &given_config_source, &config_options);
     + 
     + 	if (!values.nr && default_value) {
     ++		struct key_value_info kvi = { 0 };
     + 		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);
     + 	}
     +@@ builtin/config.c: 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,
     +-			       struct key_value_info *kvi UNUSED, void *cb)
     ++			       struct key_value_info *kvi, void *cb)
     + {
     + 	struct string_list *values = cb;
     + 	struct string_list_item *item = string_list_insert(values, var);
     +@@ builtin/config.c: static int urlmatch_collect_fn(const char *var, const char *value,
     + 	} else {
     + 		strbuf_reset(&matched->value);
     + 	}
     ++	memcpy(&matched->kvi, kvi, sizeof(struct key_value_info));
     + 
     + 	if (value) {
     + 		strbuf_addstr(&matched->value, value);
     +@@ builtin/config.c: 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);
     + 
     +
     + ## config.c ##
     +@@ config.c: 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(struct config_source *cs,
     ++			       struct key_value_info *kvi,
     ++			       const char *path,
     + 			       struct config_include_data *inc)
     + {
     + 	int ret = 0;
     +@@ config.c: static int handle_path_include(struct config_source *cs, const char *path,
     + 			    cs->name ? cs->name :
     + 			    "the command line");
     + 		ret = git_config_from_file_with_options(git_config_include, path, inc,
     +-							current_config_scope(),
     +-							NULL);
     ++							kvi->scope, NULL);
     + 		inc->depth--;
     + 	}
     + cleanup:
     +@@ config.c: static int git_config_include(const char *var, const char *value,
     + 		return ret;
     + 
     + 	if (!strcmp(var, "include.path"))
     +-		ret = handle_path_include(cs, value, inc);
     ++		ret = handle_path_include(cs, kvi, value, inc);
     + 
     + 	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
     + 	    cond && include_condition_is_true(cs, inc, cond, cond_len) &&
     +@@ config.c: static int git_config_include(const char *var, const char *value,
     + 
     + 		if (inc->opts->unconditional_remote_url)
     + 			inc->fn = forbid_remote_url;
     +-		ret = handle_path_include(cs, value, inc);
     ++		ret = handle_path_include(cs, kvi, value, inc);
     + 		inc->fn = old_fn;
     + 	}
     + 
     +@@ config.c: out_free_ret_1:
       }
       
       static int config_parse_pair(const char *key, const char *value,
     @@ config.c: static int config_parse_pair(const char *key, const char *value,
       	if (git_config_parse_key(key, &canonical_name, NULL))
       		return -1;
       
     --	ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
     -+	ret = (kvi_fn(fn, canonical_name, value, kvi, data) < 0) ? -1 : 0;
     +-	ret = (fn(canonical_name, value, NULL, data) < 0) ? -1 : 0;
     ++	ret = (fn(canonical_name, value, kvi, data) < 0) ? -1 : 0;
       	free(canonical_name);
       	return ret;
       }
       
      +
      +/* for values read from `git_config_from_parameters()` */
     -+static void kvi_from_param(struct key_value_info *out)
     ++void kvi_from_param(struct key_value_info *out)
      +{
      +	out->filename = NULL;
      +	out->linenr = -1;
     @@ config.c: static int parse_config_env_list(char *env, config_fn_t fn, void *data
       		}
       		else {
      @@ config.c: 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 config_source source = CONFIG_SOURCE_INIT;
      +	struct key_value_info kvi = { 0 };
       
     + 	source.origin_type = CONFIG_ORIGIN_CMDLINE;
     + 	config_reader_push_source(&the_reader, &source);
     + 
      +	kvi_from_param(&kvi);
     ++
       	env = getenv(CONFIG_COUNT_ENVIRONMENT);
       	if (env) {
       		unsigned long count;
     @@ config.c: int git_config_from_parameters(config_fn_t fn, void *data)
       			ret = -1;
       			goto out;
       		}
     -@@ config.c: out:
     - 	strbuf_release(&envvar);
     - 	strvec_clear(&to_free);
     - 	free(envw);
     --	config_reader_pop_source(&the_reader);
     - 	return ret;
     +@@ config.c: static int configset_find_element(struct config_set *set, const char *key,
     + 	return 0;
       }
       
     -@@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     - 		free(path);
     - 	}
     - 
     --	config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
     -+	config_reader_set_scope(reader, 0);
     - 	if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
     - 		die(_("unable to parse command-line config"));
     - 
     +-static int configset_add_value(struct config_reader *reader,
     ++static int configset_add_value(struct key_value_info *kvi_p,
     ++			       struct config_reader *reader,
     + 			       struct config_set *set, const char *key,
     + 			       const char *value)
     + {
      @@ config.c: 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) {
     -+	if (reader->source && 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 {
     + 	if (reader->source->name) {
     +-		kvi_from_source(reader->source, current_config_scope(), kv_info);
     ++		kvi_from_source(reader->source, kvi_p->scope, kv_info);
     + 	} 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->scope = reader->parsing_scope;
     -+	} else
     +-		kv_info->scope = reader->parsing_scope;
      +		kvi_from_param(kv_info);
     + 	}
       	si->util = kv_info;
       
     +@@ config.c: struct configset_add_data {
     + #define CONFIGSET_ADD_INIT { 0 }
     + 
     + static int config_set_callback(const char *key, const char *value,
     +-			       struct key_value_info *kvi UNUSED, void *cb)
     ++			       struct key_value_info *kvi, void *cb)
     + {
     + 	struct configset_add_data *data = cb;
     +-	configset_add_value(data->config_reader, data->config_set, key, value);
     ++	configset_add_value(kvi, data->config_reader, data->config_set, key, value);
     + 	return 0;
     + }
     + 
     +@@ config.c: const char *config_origin_type_name(enum config_origin_type type)
     + 	}
     + }
     + 
     +-const char *current_config_origin_type(void)
     +-{
     +-	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
     +-
     +-	if (reader_origin_type(&the_reader, &type))
     +-		BUG("current_config_origin_type called outside config callback");
     +-
     +-	return config_origin_type_name(type);
     +-}
     +-
     + const char *config_scope_name(enum config_scope scope)
     + {
     + 	switch (scope) {
     +@@ config.c: static int reader_config_name(struct config_reader *reader, const char **out)
       	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)
     +
     + ## config.h ##
     +@@ config.h: void git_global_config(char **user, char **xdg);
     + int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
     + 
     + enum config_scope current_config_scope(void);
     +-const char *current_config_origin_type(void);
     +-const char *current_config_name(void);
     + const char *config_origin_type_name(enum config_origin_type type);
     ++void kvi_from_param(struct key_value_info *out);
     + 
     + /*
     +  * Match and parse a config key of the form:
     +
     + ## t/t1300-config.sh ##
     +@@ t/t1300-config.sh: test_expect_success 'urlmatch with --show-scope' '
     + 	EOF
     + 
     + 	cat >expect <<-EOF &&
     +-	unknown	http.cookiefile /tmp/cookie.txt
     +-	unknown	http.sslverify false
     ++	local	http.cookiefile /tmp/cookie.txt
     ++	local	http.sslverify false
     + 	EOF
     + 	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
     + 	test_cmp expect actual
     +@@ t/t1300-config.sh: test_expect_success '--show-origin blob ref' '
     + '
     + 
     + test_expect_success '--show-origin with --default' '
     +-	test_must_fail git config --show-origin --default foo some.key
     ++	git config --show-origin --default foo some.key >actual &&
     ++	echo "command line:	foo" >expect &&
     ++	test_cmp expect actual
     + '
     + 
     + test_expect_success '--show-scope with --list' '
     +@@ t/t1300-config.sh: test_expect_success '--show-scope with --show-origin' '
     + 
     + test_expect_success '--show-scope with --default' '
     + 	git config --show-scope --default foo some.key >actual &&
     +-	echo "unknown	foo" >expect &&
     ++	echo "command	foo" >expect &&
     + 	test_cmp expect actual
     + '
     + 
 12:  4723ae3bde6 ! 10:  7dc0c46b864 config: remove current_config_scope()
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config: remove current_config_scope()
     +    trace2: plumb config kvi
      
     -    Replace current_config_scope() by reading the corresponding value off
     -    "struct key_value_info".
     -
     -    Most instances of this are trivial, except for the trace2/* files. There
     -    is a code path starting from trace2_def_param_fl() that eventually calls
     -    current_config_scope(), and thus it needs to have "kvi" plumbed through
     -    it. Additional plumbing is also needed to get "kvi" to
     +    There is a code path starting from trace2_def_param_fl() that eventually
     +    calls current_config_scope(), and thus it needs to have "kvi" plumbed
     +    through it. Additional plumbing is also needed to get "kvi" to
          trace2_def_param_fl(), which gets called by two code paths:
      
          - Through tr2_cfg_cb(), which is a config callback, so it trivially
     @@ Commit message
            information.
      
            Teach tr2_list_env_vars_fl() to be well-behaved by using
     -      kvi_from_param(), which is already used internally by config.c for
     -      CLI/environment variable-based config.
     +      kvi_from_param(), which is used elsewhere for CLI/environment
     +      variable-based config.
     +
     +    As a result, current_config_scope() has no more callers, so remove it.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     - ## builtin/config.c ##
     -@@ builtin/config.c: static void show_config_origin(struct key_value_info *kvi, struct strbuf *buf)
     - 	strbuf_addch(buf, term);
     + ## config.c ##
     +@@ config.c: struct config_reader {
     + 	 */
     + 	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
     +@@ config.c: static inline struct config_source *config_reader_pop_source(struct config_reade
     + static inline void config_reader_set_kvi(struct config_reader *reader,
     + 					 struct key_value_info *kvi)
     + {
     +-	if (kvi && (reader->source || reader->parsing_scope))
     +-		BUG("kvi should not be set while parsing a config source");
     + 	reader->config_kvi = kvi;
       }
       
     --static void show_config_scope(struct strbuf *buf)
     -+static void show_config_scope(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);
     -@@ builtin/config.c: static int show_all_config(const char *key_, const char *value_,
     - 	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(kvi, &buf);
     - 		/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
     -@@ builtin/config.c: static int format_config(struct strbuf *buf, const char *key_, const char *value
     - 			 struct key_value_info *kvi)
     +-static inline void config_reader_set_scope(struct config_reader *reader,
     +-					   enum config_scope scope)
     +-{
     +-	if (scope && reader->config_kvi)
     +-		BUG("scope should only be set when iterating through a config source");
     +-	reader->parsing_scope = scope;
     +-}
     +-
     + static int pack_compression_seen;
     + static int zlib_compression_seen;
     + 
     +@@ config.c: static void populate_remote_urls(struct config_include_data *inc)
       {
     - 	if (show_scope)
     --		show_config_scope(buf);
     -+		show_config_scope(kvi, buf);
     - 	if (show_origin)
     - 		show_config_origin(kvi, buf);
     - 	if (show_keys)
     -
     - ## builtin/remote.c ##
     -@@ builtin/remote.c: static int config_read_push_default(const char *key, const char *value,
     - 	    !value || strcmp(value, info->old_name))
     - 		return 0;
     - 
     --	info->scope = current_config_scope();
     -+	info->scope = kvi->scope;
     - 	strbuf_reset(&info->origin);
     - 	strbuf_addstr(&info->origin, kvi->filename);
     - 	info->linenr = kvi->linenr;
     -
     - ## config.c ##
     -@@ config.c: static int config_parse_pair(const char *key, const char *value,
     + 	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, &opts);
     +-
     +-	config_reader_set_scope(inc->config_reader, store_scope);
     + }
     + 
     + static int forbid_remote_url(const char *var, const char *value UNUSED,
     +@@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     + 	char *xdg_config = NULL;
     + 	char *user_config = NULL;
     + 	char *repo_config;
     +-	enum config_scope prev_parsing_scope = reader->parsing_scope;
     + 
     + 	if (opts->commondir)
     + 		repo_config = mkpathdup("%s/config", opts->commondir);
     +@@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     + 	else
     + 		repo_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))
     +@@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     + 							 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))
     +@@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     + 		ret += git_config_from_file_with_options(fn, user_config, data,
     + 							 CONFIG_SCOPE_GLOBAL, NULL);
       
     - /* for values read from `git_config_from_parameters()` */
     --static void kvi_from_param(struct key_value_info *out)
     -+void kvi_from_param(struct key_value_info *out)
     +-	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_with_options(fn, repo_config, data,
     + 							 CONFIG_SCOPE_LOCAL, NULL);
     + 
     +-	config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
     + 	if (!opts->ignore_worktree && repository_format_worktree_config) {
     + 		char *path = git_pathdup("config.worktree");
     + 		if (!access_or_die(path, R_OK, 0))
     +@@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     + 		free(path);
     + 	}
     + 
     +-	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);
     +@@ config.c: int config_with_options(config_fn_t fn, void *data,
     + 			const struct config_options *opts)
       {
     - 	out->filename = NULL;
     - 	out->linenr = -1;
     + 	struct config_include_data inc = CONFIG_INCLUDE_INIT;
     +-	enum config_scope prev_scope = the_reader.parsing_scope;
     + 	int ret;
     + 
     + 	if (opts->respect_includes) {
     +@@ config.c: int config_with_options(config_fn_t fn, void *data,
     + 		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.
     +@@ config.c: int config_with_options(config_fn_t fn, void *data,
     + 		string_list_clear(inc.remote_urls, 0);
     + 		FREE_AND_NULL(inc.remote_urls);
     + 	}
     +-	config_reader_set_scope(&the_reader, prev_scope);
     + 	return ret;
     + }
     + 
      @@ config.c: static int reader_config_name(struct config_reader *reader, const char **out)
       	return 0;
       }
     @@ config.c: static int reader_config_name(struct config_reader *reader, const char
      -	if (the_reader.config_kvi)
      -		return the_reader.config_kvi->scope;
      -	else
     --		/*
     --		 * FIXME This should be a BUG, but tr2_list_env_vars_fl is
     --		 * calling this outside of a config callback. This will be
     --		 * easier to fix when we plumb kvi through the config callbacks,
     --		 * so leave this untouched for now.
     --		 */
     --		return CONFIG_SCOPE_UNKNOWN;
     +-		return the_reader.parsing_scope;
      -}
      -
       int lookup_config(const char **mapping, int nr_mapping, const char *var)
     @@ config.h: void git_global_config(char **user, char **xdg);
       
      -enum config_scope current_config_scope(void);
       const char *config_origin_type_name(enum config_origin_type type);
     + void kvi_from_param(struct key_value_info *out);
       
     - /*
     -@@ config.h: NORETURN void git_die_config_linenr(const char *key, const char *filename, int l
     - 	lookup_config(mapping, ARRAY_SIZE(mapping), var)
     - int lookup_config(const char **mapping, int nr_mapping, const char *var);
     - 
     -+void kvi_from_param(struct key_value_info *out);
     -+
     - #endif /* CONFIG_H */
     -
     - ## remote.c ##
     -@@ remote.c: static void read_branches_file(struct remote_state *remote_state,
     - }
     - 
     - static int handle_config(const char *key, const char *value,
     --			 struct key_value_info *kvi UNUSED, void *cb)
     -+			 struct key_value_info *kvi, void *cb)
     - {
     - 	const char *name;
     - 	size_t namelen;
     -@@ remote.c: static int handle_config(const char *key, const char *value,
     - 	}
     - 	remote = make_remote(remote_state, name, namelen);
     - 	remote->origin = REMOTE_CONFIG;
     --	if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
     --	    current_config_scope() == CONFIG_SCOPE_WORKTREE)
     -+	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
     -+	    kvi->scope == CONFIG_SCOPE_WORKTREE)
     - 		remote->configured_in_repo = 1;
     - 	if (!strcmp(subkey, "mirror"))
     - 		remote->mirror = git_config_bool(key, value);
     -
     - ## t/helper/test-config.c ##
     -@@ t/helper/test-config.c: static int iterate_cb(const char *var, const char *value,
     - 	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
     - 	printf("name=%s\n", kvi->filename ? kvi->filename : "");
     - 	printf("lno=%d\n", kvi->linenr);
     --	printf("scope=%s\n", config_scope_name(current_config_scope()));
     -+	printf("scope=%s\n", config_scope_name(kvi->scope));
     - 
     - 	return 0;
     - }
      
       ## trace2.c ##
      @@ trace2.c: void trace2_thread_exit_fl(const char *file, int line)
 13:  11a32c86203 ! 11:  504eb206b5a config: pass kvi to die_bad_number()
     @@ Commit message
      
          Plumb "struct key_value_info" through all code paths that end in
          die_bad_number(), which lets us remove the helper functions that read
     -    analogous values from "struct config_reader".
     +    analogous values from "struct config_reader". As a result, nothing reads
     +    config_reader.config_kvi any more, so remove that too.
      
          In config.c, this requires changing the signature of
          git_configset_get_value() to 'return' "kvi" in an out parameter so that
     @@ builtin/commit.c: static int git_commit_config(const char *k, const char *v,
       
      
       ## builtin/config.c ##
     -@@ builtin/config.c: static int format_config(struct strbuf *buf, const char *key_, const char *value
     +@@ builtin/config.c: static int format_config(struct strbuf *buf, const char *key_,
       
       		if (type == TYPE_INT)
       			strbuf_addf(buf, "%"PRId64,
     @@ builtin/submodule--helper.c: static int update_clone_task_finished(int result,
       
      
       ## config.c ##
     +@@ config.c: struct config_reader {
     + 	 *
     + 	 * 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;
     + };
     + /*
     +  * Where possible, prefer to accept "struct config_reader" as an arg than to use
     +@@ config.c: static struct config_reader the_reader;
     + static inline void config_reader_push_source(struct config_reader *reader,
     + 					     struct config_source *top)
     + {
     +-	if (reader->config_kvi)
     +-		BUG("source should not be set while iterating a config set");
     + 	top->prev = reader->source;
     + 	reader->source = top;
     + }
     +@@ config.c: static inline struct config_source *config_reader_pop_source(struct config_reade
     + 	return ret;
     + }
     + 
     +-static inline void config_reader_set_kvi(struct config_reader *reader,
     +-					 struct key_value_info *kvi)
     +-{
     +-	reader->config_kvi = kvi;
     +-}
     +-
     + static int pack_compression_seen;
     + static int zlib_compression_seen;
     + 
      @@ config.c: int git_parse_ssize_t(const char *value, ssize_t *ret)
       	return 1;
       }
     @@ config.c: int git_default_config(const char *var, const char *value,
       		if (level == -1)
       			level = Z_DEFAULT_COMPRESSION;
       		else if (level < 0 || level > Z_BEST_COMPRESSION)
     +@@ config.c: static void configset_iter(struct config_reader *reader, struct config_set *set,
     + 		values = &entry->value_list;
     + 		kvi = values->items[value_index].util;
     + 
     +-		config_reader_set_kvi(reader, values->items[value_index].util);
     +-
     + 		if (fn(entry->key, values->items[value_index].string, kvi, data) < 0)
     + 			git_die_config_linenr(entry->key, kvi->filename, kvi->linenr);
     +-		config_reader_set_kvi(reader, NULL);
     + 	}
     + }
     + 
      @@ config.c: int git_configset_add_file(struct config_set *set, const char *filename)
       	return git_config_from_file(config_set_callback, filename, &data);
       }
     @@ config.c: int parse_config_key(const char *var,
      -{
      -	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;
     @@ config.c: const char *config_scope_name(enum config_scope scope)
      -{
      -	if (the_reader.config_kvi)
      -		*out = reader->config_kvi->filename;
     +-	else if (the_reader.source)
     +-		*out = reader->source->name;
      -	else
      -		return 1;
      -	return 0;
 14:  33e59152293 ! 12:  52db0d3be82 config: remove config_reader from configset_add_value
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config: remove config_reader from configset_add_value
     +    config.c: remove config_reader from configsets
      
     -    Since we now get "kvi" from the config callback, we can stop passing it
     -    via "*data". Now "struct config_reader" has no more references, so get
     -    rid of it.
     +    Remove the last usage of "struct config_reader" from configsets by
     +    copying the "kvi" arg instead of recomputing "kvi" from
     +    config_reader.source. Since we no longer need to pass both "struct
     +    config_reader" and "struct config_set" in a single "void *cb", remove
     +    "struct configset_add_data" too.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
       ## config.c ##
     -@@ config.c: struct config_source {
     - };
     - #define CONFIG_SOURCE_INIT { 0 }
     - 
     --struct config_reader {
     --	struct key_value_info *config_kvi;
     --};
     --/*
     -- * 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_kvi(struct config_reader *reader,
     --					  struct key_value_info *kvi)
     --{
     --	kvi->prev = reader->config_kvi;
     --	reader->config_kvi = kvi;
     --}
     --
     --static inline struct key_value_info *config_reader_pop_kvi(struct config_reader *reader)
     --{
     --	struct key_value_info *ret;
     --	if (!reader->config_kvi)
     --		BUG("tried to pop config_kvi, but we weren't reading config");
     --	ret = reader->config_kvi;
     --	reader->config_kvi = reader->config_kvi->prev;
     --	return ret;
     --}
     --
     - static int pack_compression_seen;
     - static int zlib_compression_seen;
     - 
     -@@ config.c: static int include_condition_is_true(struct key_value_info *kvi,
     - 	return 0;
     - }
     - 
     --static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     --		  struct key_value_info *kvi, void *data);
     - static int git_config_include(const char *var, const char *value,
     - 			      struct key_value_info *kvi, void *data)
     - {
     -@@ config.c: static int git_config_include(const char *var, const char *value,
     - 	 * Pass along all values, including "include" directives; this makes it
     - 	 * possible to query information on the includes themselves.
     - 	 */
     --	ret = kvi_fn(inc->fn, var, value, kvi, inc->data);
     -+	ret = inc->fn(var, value, kvi, inc->data);
     - 	if (ret < 0)
     - 		return ret;
     - 
     -@@ config.c: out_free_ret_1:
     - 	return -CONFIG_INVALID_KEY;
     - }
     - 
     --static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     --		  struct key_value_info *kvi, void *data)
     --{
     --	int ret;
     --	config_reader_push_kvi(&the_reader, kvi);
     --	ret = fn(key, value, kvi, data);
     --	config_reader_pop_kvi(&the_reader);
     --	return ret;
     --}
     --
     - static int config_parse_pair(const char *key, const char *value,
     - 			     struct key_value_info *kvi,
     - 			     config_fn_t fn, void *data)
     -@@ config.c: static int config_parse_pair(const char *key, const char *value,
     - 	if (git_config_parse_key(key, &canonical_name, NULL))
     - 		return -1;
     - 
     --	ret = (kvi_fn(fn, canonical_name, value, kvi, data) < 0) ? -1 : 0;
     -+	ret = (fn(canonical_name, value, kvi, data) < 0) ? -1 : 0;
     - 	free(canonical_name);
     +@@ config.c: int config_with_options(config_fn_t fn, void *data,
       	return ret;
       }
     -@@ config.c: static int get_value(struct config_source *cs, struct key_value_info *kvi,
     - 	 */
     - 	cs->linenr--;
     - 	kvi->linenr = cs->linenr;
     --	ret = kvi_fn(fn, name->buf, value, kvi, data);
     -+	ret = fn(name->buf, value, kvi, data);
     - 	if (ret >= 0)
     - 		cs->linenr++;
     - 	return ret;
     -@@ config.c: static void configset_iter(struct config_set *set, config_fn_t fn, void *data)
     - 		values = &entry->value_list;
     - 		kvi = values->items[value_index].util;
       
     --		if (kvi_fn(fn, entry->key, values->items[value_index].string,
     --			   kvi, data) < 0)
     -+		if (fn(entry->key, values->items[value_index].string, kvi,
     -+		       data) < 0)
     - 			git_die_config_linenr(entry->key, kvi->filename,
     - 					      kvi->linenr);
     - 	}
     +-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;
      @@ config.c: static int configset_find_element(struct config_set *set, const char *key,
     - 	return 0;
       }
       
     --static int configset_add_value(struct config_reader *reader,
     --			       struct config_set *set, const char *key,
     --			       const char *value)
     -+static int configset_add_value(struct config_set *set, const char *key,
     -+			       const char *value, struct key_value_info *kvi_p)
     + static int configset_add_value(struct key_value_info *kvi_p,
     +-			       struct config_reader *reader,
     + 			       struct config_set *set, const char *key,
     + 			       const char *value)
       {
     - 	struct config_set_element *e;
     - 	struct string_list_item *si;
     -@@ config.c: static int configset_add_value(struct config_reader *reader,
     +@@ config.c: static int configset_add_value(struct key_value_info *kvi_p,
       	l_item->e = e;
       	l_item->value_index = e->value_list.nr - 1;
       
     --	memcpy(kv_info, reader->config_kvi, sizeof(struct key_value_info));
     +-	if (reader->source->name) {
     +-		kvi_from_source(reader->source, kvi_p->scope, kv_info);
     +-	} else {
     +-		kvi_from_param(kv_info);
     +-	}
      +	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
       	si->util = kv_info;
       
     @@ config.c: void git_configset_clear(struct config_set *set)
      -	struct config_set *config_set;
      -	struct config_reader *config_reader;
      -};
     - #define CONFIGSET_ADD_INIT { 0 }
     - 
     +-#define CONFIGSET_ADD_INIT { 0 }
     +-
       static int config_set_callback(const char *key, const char *value,
     --			       struct key_value_info *kvi UNUSED, void *cb)
     -+			       struct key_value_info *kvi, void *cb)
     + 			       struct key_value_info *kvi, void *cb)
       {
      -	struct configset_add_data *data = cb;
     --	configset_add_value(data->config_reader, data->config_set, key, value);
     +-	configset_add_value(kvi, data->config_reader, data->config_set, key, value);
      +	struct config_set *set = cb;
     -+	configset_add_value(set, key, value, kvi);
     ++	configset_add_value(kvi, set, key, value);
       	return 0;
       }
       
     @@ config.c: static void repo_read_config(struct repository *repo)
       	git_configset_init(repo->config);
      -	data.config_set = repo->config;
      -	data.config_reader = &the_reader;
     - 
     +-
      -	if (config_with_options(config_set_callback, &data, NULL, &opts) < 0)
     -+	if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
     ++	if (config_with_options(config_set_callback, repo->config, NULL,
     ++				&opts) < 0)
       		/*
       		 * config_with_options() normally returns only
       		 * zero, as most errors are fatal, and
     +@@ config.c: 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)
      @@ config.c: static void read_protected_config(void)
       		.ignore_worktree = 1,
       		.system_gently = 1,
     @@ config.c: static void read_protected_config(void)
       }
       
       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' */
  4:  b407fe1ed1d ! 13:  7d9b9eefc78 config: add kvi.path, use it to evaluate includes
     @@ Commit message
          Signed-off-by: Glen Choo <chooglen@google.com>
      
       ## config.c ##
     +@@ config.c: struct config_include_data {
     + 	void *data;
     + 	const struct config_options *opts;
     + 	struct git_config_source *config_source;
     +-	struct config_reader *config_reader;
     + 
     + 	/*
     + 	 * All remote URLs discovered when reading all config files.
      @@ config.c: 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(struct config_source *cs,
     +-			       struct key_value_info *kvi,
     +-			       const char *path,
      +static int handle_path_include(struct key_value_info *kvi, const char *path,
       			       struct config_include_data *inc)
       {
       	int ret = 0;
     -@@ config.c: static int handle_path_include(struct config_source *cs, const char *path,
     +@@ config.c: static int handle_path_include(struct config_source *cs,
       	if (!is_absolute_path(path)) {
       		char *slash;
       
     @@ config.c: static int handle_path_include(struct config_source *cs, const char *p
       		strbuf_addstr(&buf, path);
       		path = buf.buf;
       	}
     -@@ config.c: static int handle_path_include(struct config_source *cs, const char *path,
     +@@ config.c: static int handle_path_include(struct config_source *cs,
       	if (!access_or_die(path, R_OK, 0)) {
       		if (++inc->depth > MAX_INCLUDE_DEPTH)
       			die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path,
     @@ config.c: static int handle_path_include(struct config_source *cs, const char *p
      +			    kvi->filename ? kvi->filename :
       			    "the command line");
       		ret = git_config_from_file_with_options(git_config_include, path, inc,
     --							current_config_scope(),
     --							NULL);
     -+							kvi->scope, NULL);
     - 		inc->depth--;
     - 	}
     - cleanup:
     + 							kvi->scope, NULL);
      @@ config.c: static void add_trailing_starstar_for_dir(struct strbuf *pat)
       		strbuf_addstr(pat, "**");
       }
     @@ config.c: static int include_by_remote_url(struct config_include_data *inc,
       	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,
     -@@ config.c: static int kvi_fn(config_fn_t fn, const char *key, const char *value,
     - static int git_config_include(const char *var, const char *value, void *data)
     +@@ config.c: static int git_config_include(const char *var, const char *value,
     + 			      struct key_value_info *kvi, void *data)
       {
       	struct config_include_data *inc = data;
      -	struct config_source *cs = inc->config_reader->source;
     - 	struct key_value_info *kvi = inc->config_reader->config_kvi;
       	const char *cond, *key;
       	size_t cond_len;
     -@@ config.c: static int git_config_include(const char *var, const char *value, void *data)
     + 	int ret;
     +@@ config.c: static int git_config_include(const char *var, const char *value,
       		return ret;
       
       	if (!strcmp(var, "include.path"))
     --		ret = handle_path_include(cs, value, inc);
     +-		ret = handle_path_include(cs, kvi, value, inc);
      +		ret = handle_path_include(kvi, value, inc);
       
       	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
     @@ config.c: static int git_config_include(const char *var, const char *value, void
       
       		if (inc->opts->unconditional_remote_url)
       			inc->fn = forbid_remote_url;
     --		ret = handle_path_include(cs, value, inc);
     +-		ret = handle_path_include(cs, kvi, value, inc);
      +		ret = handle_path_include(kvi, value, inc);
       		inc->fn = old_fn;
       	}
       
     -@@ config.c: static void kvi_from_param(struct key_value_info *out)
     +@@ config.c: void kvi_from_param(struct key_value_info *out)
       	out->linenr = -1;
       	out->origin_type = CONFIG_ORIGIN_CMDLINE;
       	out->scope = CONFIG_SCOPE_COMMAND;
     @@ config.c: static void kvi_from_source(struct config_source *cs,
       }
       
       static int git_parse_source(struct config_source *cs, config_fn_t fn,
     +@@ config.c: int config_with_options(config_fn_t fn, void *data,
     + 		inc.data = data;
     + 		inc.opts = opts;
     + 		inc.config_source = config_source;
     +-		inc.config_reader = &the_reader;
     + 		fn = git_config_include;
     + 		data = &inc;
     + 	}
      
       ## config.h ##
      @@ config.h: struct key_value_info {
     @@ config.h: struct key_value_info {
       	enum config_origin_type origin_type;
       	enum config_scope scope;
      +	const char *path;
     - 	struct key_value_info *prev;
       };
       
     + /**
  5:  e80d1b5f483 ! 14:  9e35b5b1f4d config: pass source to config_parser_event_fn_t
     @@ Commit message
          config.c, but this refactor is okay because this function has only ever
          been (and probably ever will be) used internally by config.c.
      
     -    This removes the last user of "config_reader.source", so remove it too.
     +    As a result, the_reader isn't used anywhere, so "struct config_reader"
     +    is obsolete (it was only intended to be used with the_reader). Remove
     +    them.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
       ## config.c ##
      @@ config.c: struct config_source {
     + };
       #define CONFIG_SOURCE_INIT { 0 }
       
     - struct config_reader {
     +-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;
     - };
     - /*
     -@@ config.c: struct config_reader {
     -  */
     - static struct config_reader the_reader;
     - 
     +-};
     +-/*
     +- * 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)
      -{
     @@ config.c: struct config_reader {
      -	return ret;
      -}
      -
     - static inline void config_reader_push_kvi(struct config_reader *reader,
     - 					  struct key_value_info *kvi)
     - {
     + static int pack_compression_seen;
     + static int zlib_compression_seen;
     + 
     +@@ config.c: 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;
     + 	struct key_value_info kvi = { 0 };
     + 
     +-	source.origin_type = CONFIG_ORIGIN_CMDLINE;
     +-	config_reader_push_source(&the_reader, &source);
     +-
     + 	kvi_from_param(&kvi);
     +-
     + 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
     + 	if (env) {
     + 		unsigned long count;
     +@@ config.c: out:
     + 	strbuf_release(&envvar);
     + 	strvec_clear(&to_free);
     + 	free(envw);
     +-	config_reader_pop_source(&the_reader);
     + 	return ret;
     + }
     + 
      @@ config.c: static int do_event(struct config_source *cs, enum config_event_t type,
       
       	if (data->previous_type != CONFIG_EVENT_EOF &&
     @@ config.c: static int do_event(struct config_source *cs, enum config_event_t type
       		return -1;
       
       	data->previous_type = type;
     -@@ config.c: int git_default_config(const char *var, const char *value, void *cb)
     +@@ config.c: int git_default_config(const char *var, const char *value,
        * fgetc, ungetc, ftell of top need to be initialized before calling
        * this function.
        */
     @@ config.c: static int do_config_from_file(struct config_reader *reader,
      -	ret = do_config_from(reader, &top, fn, data, scope, opts);
      +	ret = do_config_from(&top, fn, data, scope, opts);
       	funlockfile(f);
     - 
       	return ret;
     + }
      @@ config.c: static int do_config_from_file(struct config_reader *reader,
       static int git_config_from_stdin(config_fn_t fn, void *data,
       				 enum config_scope scope)
     @@ config.c: int git_config_from_mem(config_fn_t fn,
       }
       
       int git_config_from_blob_oid(config_fn_t fn,
     +@@ config.c: 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,
     + 				  config_fn_t fn, void *data)
     + {
     + 	int ret = 0;
     +@@ config.c: int config_with_options(config_fn_t fn, void *data,
     + 		ret = git_config_from_blob_ref(fn, repo, config_source->blob,
     + 					       data, config_source->scope);
     + 	} else {
     +-		ret = do_git_config_sequence(&the_reader, opts, fn, data);
     ++		ret = do_git_config_sequence(opts, fn, data);
     + 	}
     + 
     + 	if (inc.remote_urls) {
      @@ config.c: void git_die_config(const char *key, const char *err, ...)
        */
       
     @@ config.c: int git_config_set_multivar_in_file_gently(const char *config_filename
       	/* parse-key returns negative; flip the sign to feed exit(3) */
       	ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
       	if (ret)
     -@@ config.c: static int reader_origin_type(struct config_reader *reader,
     - {
     - 	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;
     -@@ config.c: 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;
     -@@ config.c: int current_config_line(void)
     - 	if (the_reader.config_kvi)
     - 		return the_reader.config_kvi->linenr;
     - 	else
     --		return the_reader.source->linenr;
     -+		BUG("current_config_line called outside config callback");
     - }
     - 
     - int lookup_config(const char **mapping, int nr_mapping, const char *var)
      
       ## config.h ##
      @@ config.h: enum config_event_t {

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v2 01/14] config: inline git_color_default_config
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
@ 2023-05-30 18:41   ` Glen Choo via GitGitGadget
  2023-05-30 18:42   ` [PATCH v2 02/14] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
                     ` (13 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:41 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

git_color_default_config() is a shorthand for calling two other config
callbacks. There are no other non-static functions that do this and it
will complicate our refactoring of config_fn_t so inline it instead.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/branch.c      | 5 ++++-
 builtin/clean.c       | 6 ++++--
 builtin/grep.c        | 5 ++++-
 builtin/show-branch.c | 5 ++++-
 builtin/tag.c         | 6 +++++-
 color.c               | 8 --------
 color.h               | 6 +-----
 7 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 6413a016c57..c6982181fd5 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -114,7 +114,10 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/clean.c b/builtin/clean.c
index 14c0d555eac..a06df48a269 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -129,8 +129,10 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	/* inspect the color.ui config variable and others */
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/grep.c b/builtin/grep.c
index a1b68d90bdb..c880c9538d6 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -290,7 +290,10 @@ static int wait_all(void)
 static int grep_cmd_config(const char *var, const char *value, void *cb)
 {
 	int st = grep_config(var, value, cb);
-	if (git_color_default_config(var, value, NULL) < 0)
+
+	if (git_color_config(var, value, cb) < 0)
+		st = -1;
+	else if (git_default_config(var, value, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 463a8d11c31..82ae2a7e475 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -576,7 +576,10 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/tag.c b/builtin/tag.c
index 782bb3aa2ff..7245a4d30e6 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -204,7 +204,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 
 	if (starts_with(var, "column."))
 		return git_column_config(var, value, "tag", &colopts);
-	return git_color_default_config(var, value, cb);
+
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/color.c b/color.c
index 672dcbb73a6..9bdbffe928d 100644
--- a/color.c
+++ b/color.c
@@ -427,14 +427,6 @@ int git_color_config(const char *var, const char *value, void *cb UNUSED)
 	return 0;
 }
 
-int git_color_default_config(const char *var, const char *value, void *cb)
-{
-	if (git_color_config(var, value, cb) < 0)
-		return -1;
-
-	return git_default_config(var, value, cb);
-}
-
 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
 {
 	if (*color)
diff --git a/color.h b/color.h
index cfc8f841b23..bb28343be21 100644
--- a/color.h
+++ b/color.h
@@ -88,12 +88,8 @@ extern const int column_colors_ansi_max;
  */
 extern int color_stdout_is_tty;
 
-/*
- * Use the first one if you need only color config; the second is a convenience
- * if you are just going to change to git_default_config, too.
- */
+/* Parse color config. */
 int git_color_config(const char *var, const char *value, void *cb);
-int git_color_default_config(const char *var, const char *value, void *cb);
 
 /*
  * Parse a config option, which can be a boolean or one of
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 02/14] urlmatch.h: use config_fn_t type
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
  2023-05-30 18:41   ` [PATCH v2 01/14] config: inline git_color_default_config Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-05-30 18:42   ` [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t Glen Choo via GitGitGadget
                     ` (12 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

These are actually used as config callbacks, so use the typedef-ed type
and make future refactors easier.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 urlmatch.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/urlmatch.h b/urlmatch.h
index 9f40b00bfb8..bee374a642c 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -2,6 +2,7 @@
 #define URL_MATCH_H
 
 #include "string-list.h"
+#include "config.h"
 
 struct url_info {
 	/* normalized url on success, must be freed, otherwise NULL */
@@ -48,8 +49,8 @@ struct urlmatch_config {
 	const char *key;
 
 	void *cb;
-	int (*collect_fn)(const char *var, const char *value, void *cb);
-	int (*cascade_fn)(const char *var, const char *value, void *cb);
+	config_fn_t collect_fn;
+	config_fn_t cascade_fn;
 	/*
 	 * Compare the two matches, the one just discovered and the existing
 	 * best match and return a negative value if the found item is to be
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
  2023-05-30 18:41   ` [PATCH v2 01/14] config: inline git_color_default_config Glen Choo via GitGitGadget
  2023-05-30 18:42   ` [PATCH v2 02/14] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-01  9:50     ` Phillip Wood
  2023-05-30 18:42   ` [PATCH v2 04/14] (RFC-only) config: apply cocci to config_fn_t implementations Glen Choo via GitGitGadget
                     ` (11 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

..without actually changing any of its implementations. This commit does
not build - I've split this out for readability, but post-RFC I will
squash this with the rest of the refactor + cocci changes.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c                                      |   8 +-
 config.h                                      |  16 +-
 .../coccinelle/config_fn_kvi.pending.cocci    | 146 ++++++++++++++++++
 3 files changed, 158 insertions(+), 12 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_kvi.pending.cocci

diff --git a/config.c b/config.c
index 493f47df8ae..945f4f3b77e 100644
--- a/config.c
+++ b/config.c
@@ -489,7 +489,7 @@ 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, NULL, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -671,7 +671,7 @@ static int config_parse_pair(const char *key, const char *value,
 	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, NULL, data) < 0) ? -1 : 0;
 	free(canonical_name);
 	return ret;
 }
@@ -959,7 +959,7 @@ 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);
+	ret = fn(name->buf, value, NULL, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -2303,7 +2303,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
 
-		if (fn(entry->key, values->items[value_index].string, data) < 0)
+		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
 			git_die_config_linenr(entry->key,
 					      reader->config_kvi->filename,
 					      reader->config_kvi->linenr);
diff --git a/config.h b/config.h
index 247b572b37b..9d052c52c3c 100644
--- a/config.h
+++ b/config.h
@@ -111,6 +111,13 @@ struct config_options {
 	} error_action;
 };
 
+struct key_value_info {
+	const char *filename;
+	int linenr;
+	enum config_origin_type origin_type;
+	enum config_scope scope;
+};
+
 /**
  * A config callback function takes three parameters:
  *
@@ -129,7 +136,7 @@ struct config_options {
  * A config callback should return 0 for success, or -1 if the variable
  * could not be parsed properly.
  */
-typedef int (*config_fn_t)(const char *, const char *, void *);
+typedef int (*config_fn_t)(const char *, const char *, struct key_value_info *, void *);
 
 int git_default_config(const char *, const char *, void *);
 
@@ -667,13 +674,6 @@ int git_config_get_expiry(const char *key, const char **output);
 /* parse either "this many days" integer, or "5.days.ago" approxidate */
 int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
 
-struct key_value_info {
-	const char *filename;
-	int linenr;
-	enum config_origin_type origin_type;
-	enum config_scope scope;
-};
-
 /**
  * First prints the error message specified by the caller in `err` and then
  * dies printing the line number and the file name of the highest priority
diff --git a/contrib/coccinelle/config_fn_kvi.pending.cocci b/contrib/coccinelle/config_fn_kvi.pending.cocci
new file mode 100644
index 00000000000..d4c84599afa
--- /dev/null
+++ b/contrib/coccinelle/config_fn_kvi.pending.cocci
@@ -0,0 +1,146 @@
+// These are safe to apply to *.c *.h builtin/*.c
+
+@ get_fn @
+identifier fn, R;
+@@
+(
+(
+git_config_from_file
+|
+git_config_from_file_with_options
+|
+git_config_from_mem
+|
+git_config_from_blob_oid
+|
+read_early_config
+|
+read_very_early_config
+|
+config_with_options
+|
+git_config
+|
+git_protected_config
+|
+config_from_gitmodules
+)
+  (fn, ...)
+|
+repo_config(R, fn, ...)
+)
+
+@ extends get_fn @
+identifier C1, C2, D;
+@@
+int fn(const char *C1, const char *C2,
++  struct key_value_info *kvi,
+  void *D);
+
+@ extends get_fn @
+@@
+int fn(const char *, const char *,
++  struct key_value_info *,
+  void *);
+
+@ extends get_fn @
+// Don't change fns that look like callback fns but aren't
+identifier fn2 != tar_filter_config && != git_diff_heuristic_config &&
+  != git_default_submodule_config && != git_color_config &&
+  != bundle_list_update && != parse_object_filter_config;
+identifier C1, C2, D1, D2, S;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++  struct key_value_info *kvi,
+  void *D1) {
+<+...
+(
+fn2(C1, C2,
++ kvi,
+D2);
+|
+if(fn2(C1, C2,
++ kvi,
+D2) < 0) { ... }
+|
+return fn2(C1, C2,
++ kvi,
+D2);
+|
+S = fn2(C1, C2,
++ kvi,
+D2);
+)
+...+>
+  }
+
+@ extends get_fn@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++  struct key_value_info *kvi UNUSED,
+  void *D) {...}
+
+
+// The previous rules don't catch all callbacks, especially if they're defined
+// in a separate file from the git_config() call. Fix these manually.
+@@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int
+(
+git_ident_config
+|
+urlmatch_collect_fn
+|
+write_one_config
+|
+forbid_remote_url
+|
+credential_config_callback
+)
+  (const char *C1, const char *C2,
++  struct key_value_info *kvi UNUSED,
+  void *D) {...}
+
+@@
+identifier C1, C2, D, D2, S, fn2;
+@@
+int
+(
+http_options
+|
+git_status_config
+|
+git_commit_config
+|
+git_default_core_config
+|
+grep_config
+)
+  (const char *C1, const char *C2,
++  struct key_value_info *kvi,
+  void *D) {
+<+...
+(
+fn2(C1, C2,
++ kvi,
+D2);
+|
+if(fn2(C1, C2,
++ kvi,
+D2) < 0) { ... }
+|
+return fn2(C1, C2,
++ kvi,
+D2);
+|
+S = fn2(C1, C2,
++ kvi,
+D2);
+)
+...+>
+  }
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 04/14] (RFC-only) config: apply cocci to config_fn_t implementations
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (2 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-05-30 18:42   ` [PATCH v2 05/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
                     ` (10 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass "struct key_value_info" to *most* functions that are invoked as
"config_fn_t" callbacks by applying
contrib/coccinelle/config_fn_kvi.pending.cocci. None of the functions
actually use the "kvi" arg yet (besides propagating it to a function
that now expects "kvi"), but this will be addressed in a later commit.
When deciding whether or not to propagate "kvi" to an inner function,
only propagate the "kvi" arg if the inner function is actually invoked
elsewhere as a config callback; it does not matter whether the function
happens have the same signature as config_fn_t.

This commit does not build and has several style issues (e.g. a lack of
spacing around the "kvi" arg), but I've split this out for the RFC so
that it's more obvious which changes are automatic vs manual. Post-RFC I
will squash this with the rest of the refactor + cocci changes.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 alias.c                     |  3 ++-
 archive-tar.c               |  3 ++-
 archive-zip.c               |  1 +
 builtin/add.c               |  5 +++--
 builtin/blame.c             |  5 +++--
 builtin/branch.c            |  5 +++--
 builtin/cat-file.c          |  5 +++--
 builtin/checkout.c          |  5 +++--
 builtin/clean.c             |  5 +++--
 builtin/clone.c             |  8 +++++---
 builtin/column.c            |  3 ++-
 builtin/commit-graph.c      |  1 +
 builtin/commit.c            | 10 ++++++----
 builtin/config.c            | 10 +++++++---
 builtin/difftool.c          |  5 +++--
 builtin/fetch.c             |  8 +++++---
 builtin/fsmonitor--daemon.c |  5 +++--
 builtin/grep.c              |  5 +++--
 builtin/help.c              |  5 +++--
 builtin/index-pack.c        |  5 +++--
 builtin/log.c               | 10 ++++++----
 builtin/merge.c             |  7 ++++---
 builtin/multi-pack-index.c  |  1 +
 builtin/pack-objects.c      |  5 +++--
 builtin/patch-id.c          |  5 +++--
 builtin/pull.c              |  5 +++--
 builtin/push.c              |  5 +++--
 builtin/read-tree.c         |  5 +++--
 builtin/rebase.c            |  5 +++--
 builtin/receive-pack.c      |  5 +++--
 builtin/reflog.c            |  7 ++++---
 builtin/remote.c            |  6 ++++--
 builtin/repack.c            |  5 +++--
 builtin/reset.c             |  5 +++--
 builtin/send-pack.c         |  5 +++--
 builtin/show-branch.c       |  5 +++--
 builtin/stash.c             |  5 +++--
 builtin/submodule--helper.c |  1 +
 builtin/tag.c               |  5 +++--
 builtin/var.c               |  5 +++--
 builtin/worktree.c          |  5 +++--
 bundle-uri.c                |  8 ++++++--
 config.c                    | 28 ++++++++++++++++++----------
 config.h                    |  3 ++-
 connect.c                   |  4 ++--
 convert.c                   |  4 +++-
 credential.c                |  1 +
 delta-islands.c             |  3 ++-
 diff.c                      | 10 ++++++----
 diff.h                      |  6 ++++--
 fetch-pack.c                |  5 +++--
 fmt-merge-msg.c             |  5 +++--
 fmt-merge-msg.h             |  3 ++-
 fsck.c                      |  8 +++++---
 fsck.h                      |  3 ++-
 gpg-interface.c             |  6 ++++--
 grep.c                      |  3 ++-
 help.c                      |  7 +++++--
 ident.c                     |  3 ++-
 ident.h                     |  3 ++-
 imap-send.c                 |  5 +++--
 ll-merge.c                  |  1 +
 ls-refs.c                   |  2 +-
 mailinfo.c                  |  5 +++--
 notes-utils.c               |  3 ++-
 notes.c                     |  3 ++-
 pager.c                     |  5 ++++-
 pretty.c                    |  1 +
 promisor-remote.c           |  4 +++-
 remote.c                    |  3 ++-
 revision.c                  |  3 ++-
 scalar.c                    |  3 ++-
 sequencer.c                 |  8 +++++---
 setup.c                     | 15 ++++++++++-----
 submodule-config.c          | 15 +++++++++++----
 t/helper/test-config.c      |  9 ++++++---
 t/helper/test-userdiff.c    |  4 +++-
 trace2/tr2_cfg.c            |  3 ++-
 trace2/tr2_sysenv.c         |  3 ++-
 trailer.c                   |  2 ++
 upload-pack.c               |  8 ++++++--
 urlmatch.c                  |  3 ++-
 urlmatch.h                  |  3 ++-
 xdiff-interface.c           |  5 +++--
 xdiff-interface.h           |  3 ++-
 85 files changed, 285 insertions(+), 156 deletions(-)

diff --git a/alias.c b/alias.c
index e814948ced3..38c51038a13 100644
--- a/alias.c
+++ b/alias.c
@@ -11,7 +11,8 @@ struct config_alias_data {
 	struct string_list *list;
 };
 
-static int config_alias_cb(const char *key, const char *value, void *d)
+static int config_alias_cb(const char *key, const char *value,
+			   struct key_value_info *kvi UNUSED, void *d)
 {
 	struct config_alias_data *data = d;
 	const char *p;
diff --git a/archive-tar.c b/archive-tar.c
index 497dad0b3af..dcfbce5225a 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -410,7 +410,8 @@ static int tar_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int git_tar_config(const char *var, const char *value, void *cb)
+static int git_tar_config(const char *var, const char *value,
+			  struct key_value_info *kvi UNUSED, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
diff --git a/archive-zip.c b/archive-zip.c
index e6f5c10a14f..0b028246689 100644
--- a/archive-zip.c
+++ b/archive-zip.c
@@ -616,6 +616,7 @@ static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
 }
 
 static int archive_zip_config(const char *var, const char *value,
+			      struct key_value_info *kvi UNUSED,
 			      void *data UNUSED)
 {
 	return userdiff_config(var, value);
diff --git a/builtin/add.c b/builtin/add.c
index f12054d9be1..f8e42e05b07 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -355,7 +355,8 @@ static struct option builtin_add_options[] = {
 	OPT_END(),
 };
 
-static int add_config(const char *var, const char *value, void *cb)
+static int add_config(const char *var, const char *value,
+		      struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "add.ignoreerrors") ||
 	    !strcmp(var, "add.ignore-errors")) {
@@ -363,7 +364,7 @@ static int add_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static const char embedded_advice[] = N_(
diff --git a/builtin/blame.c b/builtin/blame.c
index a8d2114adc9..0aafc8172e2 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -692,7 +692,8 @@ static const char *add_prefix(const char *prefix, const char *path)
 	return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 }
 
-static int git_blame_config(const char *var, const char *value, void *cb)
+static int git_blame_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "blame.showroot")) {
 		show_root = git_config_bool(var, value);
@@ -765,7 +766,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
diff --git a/builtin/branch.c b/builtin/branch.c
index c6982181fd5..26091a036d2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -80,7 +80,8 @@ static unsigned int colopts;
 
 define_list_config_array(color_branch_slots);
 
-static int git_branch_config(const char *var, const char *value, void *cb)
+static int git_branch_config(const char *var, const char *value,
+			     struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 
@@ -117,7 +118,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 04d4bb6c777..b1e0e95d631 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -882,12 +882,13 @@ static int batch_objects(struct batch_options *opt)
 	return retval;
 }
 
-static int git_cat_file_config(const char *var, const char *value, void *cb)
+static int git_cat_file_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int batch_option_callback(const struct option *opt,
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 38a8cd6a965..92017ba6696 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1182,7 +1182,8 @@ static int switch_branches(const struct checkout_opts *opts,
 	return ret || writeout_error;
 }
 
-static int git_checkout_config(const char *var, const char *value, void *cb)
+static int git_checkout_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	struct checkout_opts *opts = cb;
 
@@ -1198,7 +1199,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
 
-	return git_xmerge_config(var, value, NULL);
+	return git_xmerge_config(var, value,kvi, NULL);
 }
 
 static void setup_new_branch_info_and_source_tree(
diff --git a/builtin/clean.c b/builtin/clean.c
index a06df48a269..1c648276ebf 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -102,7 +102,8 @@ struct menu_stuff {
 
 define_list_config_array(color_interactive_slots);
 
-static int git_clean_config(const char *var, const char *value, void *cb)
+static int git_clean_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 
@@ -132,7 +133,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/clone.c b/builtin/clone.c
index 6dc89f1058b..1e1cf104194 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -775,7 +775,8 @@ static int checkout(int submodule_progress, int filter_submodules)
 	return err;
 }
 
-static int git_clone_config(const char *k, const char *v, void *cb)
+static int git_clone_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "clone.defaultremotename")) {
 		free(remote_name);
@@ -786,10 +787,11 @@ static int git_clone_config(const char *k, const char *v, void *cb)
 	if (!strcmp(k, "clone.filtersubmodules"))
 		config_filter_submodules = git_config_bool(k, v);
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
-static int write_one_config(const char *key, const char *value, void *data)
+static int write_one_config(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *data)
 {
 	/*
 	 * give git_clone_config a chance to write config values back to the
diff --git a/builtin/column.c b/builtin/column.c
index de623a16c2d..30cfbed62ec 100644
--- a/builtin/column.c
+++ b/builtin/column.c
@@ -13,7 +13,8 @@ static const char * const builtin_column_usage[] = {
 };
 static unsigned int colopts;
 
-static int column_config(const char *var, const char *value, void *cb)
+static int column_config(const char *var, const char *value,
+			 struct key_value_info *kvi UNUSED, void *cb)
 {
 	return git_column_config(var, value, cb, &colopts);
 }
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 90114269761..e811866b5dd 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -185,6 +185,7 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
+					 struct key_value_info *kvi UNUSED,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
diff --git a/builtin/commit.c b/builtin/commit.c
index 9d8e1ea91a3..ec468e87039 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1402,7 +1402,8 @@ static int parse_status_slot(const char *slot)
 	return LOOKUP_CONFIG(color_status_slots, slot);
 }
 
-static int git_status_config(const char *k, const char *v, void *cb)
+static int git_status_config(const char *k, const char *v,
+			     struct key_value_info *kvi, void *cb)
 {
 	struct wt_status *s = cb;
 	const char *slot_name;
@@ -1487,7 +1488,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
 		s->detect_rename = git_config_rename(k, v);
 		return 0;
 	}
-	return git_diff_ui_config(k, v, NULL);
+	return git_diff_ui_config(k, v,kvi, NULL);
 }
 
 int cmd_status(int argc, const char **argv, const char *prefix)
@@ -1602,7 +1603,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int git_commit_config(const char *k, const char *v, void *cb)
+static int git_commit_config(const char *k, const char *v,
+			     struct key_value_info *kvi, void *cb)
 {
 	struct wt_status *s = cb;
 
@@ -1624,7 +1626,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_status_config(k, v, s);
+	return git_status_config(k, v,kvi, s);
 }
 
 int cmd_commit(int argc, const char **argv, const char *prefix)
diff --git a/builtin/config.c b/builtin/config.c
index fe79fb60c43..b2ad7351d0a 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -214,7 +214,7 @@ static void show_config_scope(struct strbuf *buf)
 }
 
 static int show_all_config(const char *key_, const char *value_,
-			   void *cb UNUSED)
+			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
 {
 	if (show_origin || show_scope) {
 		struct strbuf buf = STRBUF_INIT;
@@ -298,7 +298,8 @@ 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_,
+			  struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct strbuf_list *values = cb;
 
@@ -466,6 +467,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,
+				struct key_value_info *kvi UNUSED,
 				void *cb UNUSED)
 {
 	if (!strcmp(var, get_color_slot)) {
@@ -498,6 +500,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,
+				    struct key_value_info *kvi UNUSED,
 				    void *data UNUSED)
 {
 	if (!strcmp(var, get_colorbool_slot))
@@ -555,7 +558,8 @@ struct urlmatch_current_candidate_value {
 	struct strbuf value;
 };
 
-static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
+static int urlmatch_collect_fn(const char *var, const char *value,
+			       struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct string_list *values = cb;
 	struct string_list_item *item = string_list_insert(values, var);
diff --git a/builtin/difftool.c b/builtin/difftool.c
index e010a21bfbc..d4d149bcf6b 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -38,14 +38,15 @@ static const char *const builtin_difftool_usage[] = {
 	NULL
 };
 
-static int difftool_config(const char *var, const char *value, void *cb)
+static int difftool_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "difftool.trustexitcode")) {
 		trust_exit_code = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int print_tool_help(void)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 85bd2801036..aa688291613 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -100,7 +100,8 @@ static int fetch_write_commit_graph = -1;
 static int stdin_refspecs = 0;
 static int negotiate_only;
 
-static int git_fetch_config(const char *k, const char *v, void *cb)
+static int git_fetch_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "fetch.prune")) {
 		fetch_prune_config = git_config_bool(k, v);
@@ -140,7 +141,7 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
@@ -1828,7 +1829,8 @@ struct remote_group_data {
 	struct string_list *list;
 };
 
-static int get_remote_group(const char *key, const char *value, void *priv)
+static int get_remote_group(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *priv)
 {
 	struct remote_group_data *g = priv;
 
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 42af6a2cc7e..a7375d61d02 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -36,7 +36,8 @@ static int fsmonitor__start_timeout_sec = 60;
 #define FSMONITOR__ANNOUNCE_STARTUP "fsmonitor.announcestartup"
 static int fsmonitor__announce_startup = 0;
 
-static int fsmonitor_config(const char *var, const char *value, void *cb)
+static int fsmonitor_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
 		int i = git_config_int(var, value);
@@ -66,7 +67,7 @@ static int fsmonitor_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /*
diff --git a/builtin/grep.c b/builtin/grep.c
index c880c9538d6..177befc3ed4 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -287,13 +287,14 @@ static int wait_all(void)
 	return hit;
 }
 
-static int grep_cmd_config(const char *var, const char *value, void *cb)
+static int grep_cmd_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	int st = grep_config(var, value, cb);
 
 	if (git_color_config(var, value, cb) < 0)
 		st = -1;
-	else if (git_default_config(var, value, cb) < 0)
+	else if (git_default_config(var, value,kvi, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/help.c b/builtin/help.c
index 87333a02ec4..5d4a86c4b41 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -396,7 +396,8 @@ static int add_man_viewer_info(const char *var, const char *value)
 	return 0;
 }
 
-static int git_help_config(const char *var, const char *value, void *cb)
+static int git_help_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "help.format")) {
 		if (!value)
@@ -419,7 +420,7 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index b17e79cd40f..4450510ddfc 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1578,7 +1578,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
 	strbuf_release(&pack_name);
 }
 
-static int git_index_pack_config(const char *k, const char *v, void *cb)
+static int git_index_pack_config(const char *k, const char *v,
+				 struct key_value_info *kvi, void *cb)
 {
 	struct pack_idx_option *opts = cb;
 
@@ -1605,7 +1606,7 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
 		else
 			opts->flags &= ~WRITE_REV;
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 static int cmp_uint32(const void *a_, const void *b_)
diff --git a/builtin/log.c b/builtin/log.c
index 7d195789633..f8e61330491 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -559,7 +559,8 @@ static int cmd_log_walk(struct rev_info *rev)
 	return retval;
 }
 
-static int git_log_config(const char *var, const char *value, void *cb)
+static int git_log_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 
@@ -608,7 +609,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_diff_ui_config(var, value, cb);
+	return git_diff_ui_config(var, value,kvi, cb);
 }
 
 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
@@ -974,7 +975,8 @@ static enum cover_from_description parse_cover_from_description(const char *arg)
 		die(_("%s: invalid cover from description mode"), arg);
 }
 
-static int git_format_config(const char *var, const char *value, void *cb)
+static int git_format_config(const char *var, const char *value,
+			     struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "format.headers")) {
 		if (!value)
@@ -1103,7 +1105,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "diff.noprefix"))
 		return 0;
 
-	return git_log_config(var, value, cb);
+	return git_log_config(var, value,kvi, cb);
 }
 
 static const char *output_directory = NULL;
diff --git a/builtin/merge.c b/builtin/merge.c
index a99be9610e9..492a83a900c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -620,7 +620,8 @@ static void parse_branch_merge_options(char *bmo)
 	free(argv);
 }
 
-static int git_merge_config(const char *k, const char *v, void *cb)
+static int git_merge_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	int status;
 	const char *str;
@@ -665,10 +666,10 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	status = fmt_merge_msg_config(k, v, cb);
+	status = fmt_merge_msg_config(k, v,kvi, cb);
 	if (status)
 		return status;
-	return git_diff_ui_config(k, v, cb);
+	return git_diff_ui_config(k, v,kvi, cb);
 }
 
 static int read_tree_trivial(struct object_id *common, struct object_id *head,
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 1b5083f8b26..c3cd7163c84 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -82,6 +82,7 @@ static struct option *add_common_options(struct option *prev)
 }
 
 static int git_multi_pack_index_write_config(const char *var, const char *value,
+					     struct key_value_info *kvi UNUSED,
 					     void *cb UNUSED)
 {
 	if (!strcmp(var, "pack.writebitmaphashcache")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 77d88f85b04..ca023000cc0 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3134,7 +3134,8 @@ static void prepare_pack(int window, int depth)
 	free(delta_list);
 }
 
-static int git_pack_config(const char *k, const char *v, void *cb)
+static int git_pack_config(const char *k, const char *v,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
 		window = git_config_int(k, v);
@@ -3226,7 +3227,7 @@ static int git_pack_config(const char *k, const char *v, void *cb)
 		ex->uri = xstrdup(pack_end + 1);
 		oidmap_put(&configured_exclusions, ex);
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 /* Counters for trace2 output when in --stdin-packs mode. */
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 9d5585d3a72..9b4f5a71b87 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -196,7 +196,8 @@ struct patch_id_opts {
 	int verbatim;
 };
 
-static int git_patch_id_config(const char *var, const char *value, void *cb)
+static int git_patch_id_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	struct patch_id_opts *opts = cb;
 
@@ -209,7 +210,7 @@ static int git_patch_id_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_patch_id(int argc, const char **argv, const char *prefix)
diff --git a/builtin/pull.c b/builtin/pull.c
index 5405d09f22f..1b244eee67c 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -359,7 +359,8 @@ static enum rebase_type config_get_rebase(int *rebase_unspecified)
 /**
  * Read config variables.
  */
-static int git_pull_config(const char *var, const char *value, void *cb)
+static int git_pull_config(const char *var, const char *value,
+			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "rebase.autostash")) {
 		config_autostash = git_config_bool(var, value);
@@ -372,7 +373,7 @@ static int git_pull_config(const char *var, const char *value, void *cb)
 		check_trust_level = 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /**
diff --git a/builtin/push.c b/builtin/push.c
index fa550b8f80a..65b79378b92 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -506,7 +506,8 @@ static void set_push_cert_flags(int *flags, int v)
 }
 
 
-static int git_push_config(const char *k, const char *v, void *cb)
+static int git_push_config(const char *k, const char *v,
+			   struct key_value_info *kvi, void *cb)
 {
 	const char *slot_name;
 	int *flags = cb;
@@ -573,7 +574,7 @@ static int git_push_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_default_config(k, v, NULL);
+	return git_default_config(k, v,kvi, NULL);
 }
 
 int cmd_push(int argc, const char **argv, const char *prefix)
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 600d4f748fc..e9859b6157e 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -100,12 +100,13 @@ static int debug_merge(const struct cache_entry * const *stages,
 	return 0;
 }
 
-static int git_read_tree_config(const char *var, const char *value, void *cb)
+static int git_read_tree_config(const char *var, const char *value,
+				struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 680fe3c1453..1fd05d708b8 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -791,7 +791,8 @@ static void parse_rebase_merges_value(struct rebase_options *options, const char
 		die(_("Unknown rebase-merges mode: %s"), value);
 }
 
-static int rebase_config(const char *var, const char *value, void *data)
+static int rebase_config(const char *var, const char *value,
+			 struct key_value_info *kvi, void *data)
 {
 	struct rebase_options *opts = data;
 
@@ -850,7 +851,7 @@ static int rebase_config(const char *var, const char *value, void *data)
 		return git_config_string(&opts->default_backend, var, value);
 	}
 
-	return git_default_config(var, value, data);
+	return git_default_config(var, value,kvi, data);
 }
 
 static int checkout_up_to_date(struct rebase_options *options)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 9109552533d..2f5fd2abbc3 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -135,7 +135,8 @@ static enum deny_action parse_deny_action(const char *var, const char *value)
 	return DENY_IGNORE;
 }
 
-static int receive_pack_config(const char *var, const char *value, void *cb)
+static int receive_pack_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
 
@@ -262,7 +263,7 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static void show_ref(const char *path, const struct object_id *oid)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index a1fa0c855f4..ecf21ac9c6e 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -108,7 +108,8 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
 #define EXPIRE_TOTAL   01
 #define EXPIRE_UNREACH 02
 
-static int reflog_expire_config(const char *var, const char *value, void *cb)
+static int reflog_expire_config(const char *var, const char *value,
+				struct key_value_info *kvi, void *cb)
 {
 	const char *pattern, *key;
 	size_t pattern_len;
@@ -117,7 +118,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 	struct reflog_expire_cfg *ent;
 
 	if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value,kvi, cb);
 
 	if (!strcmp(key, "reflogexpire")) {
 		slot = EXPIRE_TOTAL;
@@ -128,7 +129,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 		if (git_config_expiry_date(&expire, var, value))
 			return -1;
 	} else
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value,kvi, cb);
 
 	if (!pattern) {
 		switch (slot) {
diff --git a/builtin/remote.c b/builtin/remote.c
index 1e0b137d977..edb4a9ddd7f 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -268,6 +268,7 @@ static const char *abbrev_ref(const char *name, const char *prefix)
 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
 
 static int config_read_branches(const char *key, const char *value,
+				struct key_value_info *kvi UNUSED,
 				void *data UNUSED)
 {
 	const char *orig_key = key;
@@ -645,7 +646,7 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	void *cb)
+	struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
@@ -1494,7 +1495,8 @@ static int prune(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
+static int get_remote_default(const char *key, const char *value UNUSED,
+			      struct key_value_info *kvi UNUSED, void *priv)
 {
 	if (strcmp(key, "remotes.default") == 0) {
 		int *found = priv;
diff --git a/builtin/repack.c b/builtin/repack.c
index df4d8e0f0ba..7c8401b2227 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -58,7 +58,8 @@ struct pack_objects_args {
 	int local;
 };
 
-static int repack_config(const char *var, const char *value, void *cb)
+static int repack_config(const char *var, const char *value,
+			 struct key_value_info *kvi, void *cb)
 {
 	struct pack_objects_args *cruft_po_args = cb;
 	if (!strcmp(var, "repack.usedeltabaseoffset")) {
@@ -90,7 +91,7 @@ static int repack_config(const char *var, const char *value, void *cb)
 		return git_config_string(&cruft_po_args->depth, var, value);
 	if (!strcmp(var, "repack.cruftthreads"))
 		return git_config_string(&cruft_po_args->threads, var, value);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /*
diff --git a/builtin/reset.c b/builtin/reset.c
index 0ed329236c8..a04d46d7fdd 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -308,12 +308,13 @@ static int reset_refs(const char *rev, const struct object_id *oid)
 	return update_ref_status;
 }
 
-static int git_reset_config(const char *var, const char *value, void *cb)
+static int git_reset_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_reset(int argc, const char **argv, const char *prefix)
diff --git a/builtin/send-pack.c b/builtin/send-pack.c
index 4784143004d..b0c90e549b8 100644
--- a/builtin/send-pack.c
+++ b/builtin/send-pack.c
@@ -131,7 +131,8 @@ static void print_helper_status(struct ref *ref)
 	strbuf_release(&buf);
 }
 
-static int send_pack_config(const char *k, const char *v, void *cb)
+static int send_pack_config(const char *k, const char *v,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "push.gpgsign")) {
 		const char *value;
@@ -151,7 +152,7 @@ static int send_pack_config(const char *k, const char *v, void *cb)
 			}
 		}
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v,kvi, cb);
 }
 
 int cmd_send_pack(int argc, const char **argv, const char *prefix)
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 82ae2a7e475..ad8a391904e 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -556,7 +556,8 @@ static void append_one_rev(const char *av)
 	die("bad sha1 reference %s", av);
 }
 
-static int git_show_branch_config(const char *var, const char *value, void *cb)
+static int git_show_branch_config(const char *var, const char *value,
+				  struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "showbranch.default")) {
 		if (!value)
@@ -579,7 +580,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/stash.c b/builtin/stash.c
index 735d27039e1..689087d6240 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -836,7 +836,8 @@ static int show_stat = 1;
 static int show_patch;
 static int show_include_untracked;
 
-static int git_stash_config(const char *var, const char *value, void *cb)
+static int git_stash_config(const char *var, const char *value,
+			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "stash.showstat")) {
 		show_stat = git_config_bool(var, value);
@@ -850,7 +851,7 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 		show_include_untracked = git_config_bool(var, value);
 		return 0;
 	}
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value,kvi, cb);
 }
 
 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 569068e6a2c..8570effbf0d 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2187,6 +2187,7 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
+				   struct key_value_info *kvi UNUSED,
 				   void *cb)
 {
 	int *max_jobs = cb;
diff --git a/builtin/tag.c b/builtin/tag.c
index 7245a4d30e6..626fe8c641e 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -183,7 +183,8 @@ static const char tag_template_nocleanup[] =
 	"Lines starting with '%c' will be kept; you may remove them"
 	" yourself if you want to.\n");
 
-static int git_tag_config(const char *var, const char *value, void *cb)
+static int git_tag_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "tag.gpgsign")) {
 		config_sign_tag = git_config_bool(var, value);
@@ -208,7 +209,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/builtin/var.c b/builtin/var.c
index acb988d2d56..440add19bb5 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -69,13 +69,14 @@ static const struct git_var *get_git_var(const char *var)
 	return NULL;
 }
 
-static int show_config(const char *var, const char *value, void *cb)
+static int show_config(const char *var, const char *value,
+		       struct key_value_info *kvi, void *cb)
 {
 	if (value)
 		printf("%s=%s\n", var, value);
 	else
 		printf("%s\n", var);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 39e9e5c9ce8..d5ccd741e87 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -103,14 +103,15 @@ static int verbose;
 static int guess_remote;
 static timestamp_t expire;
 
-static int git_worktree_config(const char *var, const char *value, void *cb)
+static int git_worktree_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "worktree.guessremote")) {
 		guess_remote = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int delete_git_dir(const char *id)
diff --git a/bundle-uri.c b/bundle-uri.c
index e2b267cc02b..7f01fbbff74 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -223,7 +223,9 @@ static int bundle_list_update(const char *key, const char *value,
 	return 0;
 }
 
-static int config_to_bundle_list(const char *key, const char *value, void *data)
+static int config_to_bundle_list(const char *key, const char *value,
+				 struct key_value_info *kvi UNUSED,
+				 void *data)
 {
 	struct bundle_list *list = data;
 	return bundle_list_update(key, value, list);
@@ -870,7 +872,9 @@ cached:
 	return advertise_bundle_uri;
 }
 
-static int config_to_packet_line(const char *key, const char *value, void *data)
+static int config_to_packet_line(const char *key, const char *value,
+				 struct key_value_info *kvi UNUSED,
+				 void *data)
 {
 	struct packet_reader *writer = data;
 
diff --git a/config.c b/config.c
index 945f4f3b77e..1bc2e35a3e0 100644
--- a/config.c
+++ b/config.c
@@ -201,7 +201,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,
+			      struct key_value_info *kvi, void *data);
 
 #define MAX_INCLUDE_DEPTH 10
 static const char include_depth_advice[] = N_(
@@ -380,7 +381,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,
+			  struct key_value_info *kvi UNUSED, void *data)
 {
 	struct string_list *remote_urls = data;
 	const char *remote_name;
@@ -414,6 +416,7 @@ static void populate_remote_urls(struct config_include_data *inc)
 }
 
 static int forbid_remote_url(const char *var, const char *value UNUSED,
+			     struct key_value_info *kvi UNUSED,
 			     void *data UNUSED)
 {
 	const char *remote_name;
@@ -477,7 +480,8 @@ 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,
+			      struct key_value_info *kvi UNUSED, void *data)
 {
 	struct config_include_data *inc = data;
 	struct config_source *cs = inc->config_reader->source;
@@ -1553,7 +1557,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,
+				   struct key_value_info *kvi, void *cb)
 {
 	/* This needs a better name */
 	if (!strcmp(var, "core.filemode")) {
@@ -1838,7 +1843,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,kvi, cb);
 }
 
 static int git_default_sparse_config(const char *var, const char *value)
@@ -1940,15 +1945,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,
+		       struct key_value_info *kvi, void *cb)
 {
 	if (starts_with(var, "core."))
-		return git_default_core_config(var, value, cb);
+		return git_default_core_config(var, value,kvi, 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,kvi, cb);
 
 	if (starts_with(var, "i18n."))
 		return git_default_i18n_config(var, value);
@@ -2481,7 +2487,8 @@ struct configset_add_data {
 };
 #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,
+			       struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct configset_add_data *data = cb;
 	configset_add_value(data->config_reader, data->config_set, key, value);
@@ -3091,7 +3098,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,
+		     struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct config_store_data *store = cb;
 
diff --git a/config.h b/config.h
index 9d052c52c3c..9e0c1f12165 100644
--- a/config.h
+++ b/config.h
@@ -138,7 +138,8 @@ struct key_value_info {
  */
 typedef int (*config_fn_t)(const char *, const char *, struct key_value_info *, void *);
 
-int git_default_config(const char *, const char *, void *);
+int git_default_config(const char *, const char *, struct key_value_info *,
+		       void *);
 
 /**
  * Read a specific file in git-config format.
diff --git a/connect.c b/connect.c
index c0c8a38178c..72aad67a288 100644
--- a/connect.c
+++ b/connect.c
@@ -962,7 +962,7 @@ static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
 static char *git_proxy_command;
 
 static int git_proxy_command_options(const char *var, const char *value,
-		void *cb)
+		struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "core.gitproxy")) {
 		const char *for_pos;
@@ -1008,7 +1008,7 @@ static int git_proxy_command_options(const char *var, const char *value,
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static int git_use_proxy(const char *host)
diff --git a/convert.c b/convert.c
index da06e2f51cb..a563380e7e7 100644
--- a/convert.c
+++ b/convert.c
@@ -1011,7 +1011,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
 	return 0;
 }
 
-static int read_convert_config(const char *var, const char *value, void *cb UNUSED)
+static int read_convert_config(const char *var, const char *value,
+			       struct key_value_info *kvi UNUSED,
+			       void *cb UNUSED)
 {
 	const char *key, *name;
 	size_t namelen;
diff --git a/credential.c b/credential.c
index e6417bf8804..6b9b8bfbb79 100644
--- a/credential.c
+++ b/credential.c
@@ -46,6 +46,7 @@ static int credential_from_potentially_partial_url(struct credential *c,
 						   const char *url);
 
 static int credential_config_callback(const char *var, const char *value,
+				      struct key_value_info *kvi UNUSED,
 				      void *data)
 {
 	struct credential *c = data;
diff --git a/delta-islands.c b/delta-islands.c
index 40f2ccfb550..404f9c07a42 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -341,7 +341,8 @@ static void free_remote_islands(kh_str_t *remote_islands)
 	kh_destroy_str(remote_islands);
 }
 
-static int island_config_callback(const char *k, const char *v, void *cb)
+static int island_config_callback(const char *k, const char *v,
+				  struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct island_load_data *ild = cb;
 
diff --git a/diff.c b/diff.c
index 78b0fdd8caa..d7ed2dc900b 100644
--- a/diff.c
+++ b/diff.c
@@ -350,7 +350,8 @@ static unsigned parse_color_moved_ws(const char *arg)
 	return ret;
 }
 
-int git_diff_ui_config(const char *var, const char *value, void *cb)
+int git_diff_ui_config(const char *var, const char *value,
+		       struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 		diff_use_color_default = git_config_colorbool(var, value);
@@ -433,10 +434,11 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value,kvi, cb);
 }
 
-int git_diff_basic_config(const char *var, const char *value, void *cb)
+int git_diff_basic_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb)
 {
 	const char *name;
 
@@ -488,7 +490,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
 	if (git_diff_heuristic_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static char *quote_two(const char *one, const char *two)
diff --git a/diff.h b/diff.h
index 6a0737b9c34..6a3efa63753 100644
--- a/diff.h
+++ b/diff.h
@@ -532,10 +532,12 @@ void free_diffstat_info(struct diffstat_t *diffstat);
 int parse_long_opt(const char *opt, const char **argv,
 		   const char **optarg);
 
-int git_diff_basic_config(const char *var, const char *value, void *cb);
+int git_diff_basic_config(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb);
 int git_diff_heuristic_config(const char *var, const char *value, void *cb);
 void init_diff_ui_defaults(void);
-int git_diff_ui_config(const char *var, const char *value, void *cb);
+int git_diff_ui_config(const char *var, const char *value,
+		       struct key_value_info *kvi, void *cb);
 void repo_diff_setup(struct repository *, struct diff_options *);
 struct option *add_diff_options(const struct option *, struct diff_options *);
 int diff_opt_parse(struct diff_options *, const char **, int, const char *);
diff --git a/fetch-pack.c b/fetch-pack.c
index 368f2ed25a1..c1072aa6cac 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1858,7 +1858,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	return ref;
 }
 
-static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
+static int fetch_pack_config_cb(const char *var, const char *value,
+				struct key_value_info *kvi, void *cb)
 {
 	if (strcmp(var, "fetch.fsck.skiplist") == 0) {
 		const char *path;
@@ -1880,7 +1881,7 @@ static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 static void fetch_pack_config(void)
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 1886c92ddb9..97358034fa0 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -19,7 +19,8 @@ static int use_branch_desc;
 static int suppress_dest_pattern_seen;
 static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP;
 
-int fmt_merge_msg_config(const char *key, const char *value, void *cb)
+int fmt_merge_msg_config(const char *key, const char *value,
+			 struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
@@ -39,7 +40,7 @@ int fmt_merge_msg_config(const char *key, const char *value, void *cb)
 			string_list_append(&suppress_dest_patterns, value);
 		suppress_dest_pattern_seen = 1;
 	} else {
-		return git_default_config(key, value, cb);
+		return git_default_config(key, value,kvi, cb);
 	}
 	return 0;
 }
diff --git a/fmt-merge-msg.h b/fmt-merge-msg.h
index 99054042dc5..5262c39268a 100644
--- a/fmt-merge-msg.h
+++ b/fmt-merge-msg.h
@@ -13,7 +13,8 @@ struct fmt_merge_msg_opts {
 };
 
 extern int merge_log_config;
-int fmt_merge_msg_config(const char *key, const char *value, void *cb);
+int fmt_merge_msg_config(const char *key, const char *value,
+			 struct key_value_info *kvi, void *cb);
 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 		  struct fmt_merge_msg_opts *);
 
diff --git a/fsck.c b/fsck.c
index 8ef1b022346..1569e64f011 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1162,7 +1162,8 @@ struct fsck_gitmodules_data {
 	int ret;
 };
 
-static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
+static int fsck_gitmodules_fn(const char *var, const char *value,
+			      struct key_value_info *kvi UNUSED, void *vdata)
 {
 	struct fsck_gitmodules_data *data = vdata;
 	const char *subsection, *key;
@@ -1372,7 +1373,8 @@ int fsck_finish(struct fsck_options *options)
 	return ret;
 }
 
-int git_fsck_config(const char *var, const char *value, void *cb)
+int git_fsck_config(const char *var, const char *value,
+		    struct key_value_info *kvi, void *cb)
 {
 	struct fsck_options *options = cb;
 	if (strcmp(var, "fsck.skiplist") == 0) {
@@ -1393,7 +1395,7 @@ int git_fsck_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
 
 /*
diff --git a/fsck.h b/fsck.h
index e17730e9da9..a06f202576c 100644
--- a/fsck.h
+++ b/fsck.h
@@ -237,6 +237,7 @@ const char *fsck_describe_object(struct fsck_options *options,
  * git_config() callback for use by fsck-y tools that want to support
  * fsck.<msg> fsck.skipList etc.
  */
-int git_fsck_config(const char *var, const char *value, void *cb);
+int git_fsck_config(const char *var, const char *value,
+		    struct key_value_info *kvi, void *cb);
 
 #endif
diff --git a/gpg-interface.c b/gpg-interface.c
index aceeb083367..5ed89ef69ed 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -12,7 +12,8 @@
 #include "alias.h"
 #include "wrapper.h"
 
-static int git_gpg_config(const char *, const char *, void *);
+static int git_gpg_config(const char *, const char *, struct key_value_info *,
+			  void *);
 
 static void gpg_interface_lazy_init(void)
 {
@@ -718,7 +719,8 @@ void set_signing_key(const char *key)
 	configured_signing_key = xstrdup(key);
 }
 
-static int git_gpg_config(const char *var, const char *value, void *cb UNUSED)
+static int git_gpg_config(const char *var, const char *value,
+			  struct key_value_info *kvi UNUSED, void *cb UNUSED)
 {
 	struct gpg_format *fmt = NULL;
 	char *fmtname = NULL;
diff --git a/grep.c b/grep.c
index b86462a12a9..1516b0689d0 100644
--- a/grep.c
+++ b/grep.c
@@ -54,7 +54,8 @@ define_list_config_array_extra(color_grep_slots, {"match"});
  * Read the configuration file once and store it in
  * the grep_defaults template.
  */
-int grep_config(const char *var, const char *value, void *cb)
+int grep_config(const char *var, const char *value,
+		struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct grep_opt *opt = cb;
 	const char *slot;
diff --git a/help.c b/help.c
index 5d7637dce92..43d1eb702cd 100644
--- a/help.c
+++ b/help.c
@@ -309,7 +309,8 @@ void load_command_list(const char *prefix,
 	exclude_cmds(other_cmds, main_cmds);
 }
 
-static int get_colopts(const char *var, const char *value, void *data)
+static int get_colopts(const char *var, const char *value,
+		       struct key_value_info *kvi UNUSED, void *data)
 {
 	unsigned int *colopts = data;
 
@@ -459,7 +460,8 @@ void list_developer_interfaces_help(void)
 	putchar('\n');
 }
 
-static int get_alias(const char *var, const char *value, void *data)
+static int get_alias(const char *var, const char *value,
+		     struct key_value_info *kvi UNUSED, void *data)
 {
 	struct string_list *list = data;
 
@@ -543,6 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
+				  struct key_value_info *kvi UNUSED,
 				  void *cb UNUSED)
 {
 	const char *p;
diff --git a/ident.c b/ident.c
index 8fad92d7007..21b7b3ff35b 100644
--- a/ident.c
+++ b/ident.c
@@ -671,7 +671,8 @@ static int set_ident(const char *var, const char *value)
 	return 0;
 }
 
-int git_ident_config(const char *var, const char *value, void *data UNUSED)
+int git_ident_config(const char *var, const char *value,
+		     struct key_value_info *kvi UNUSED, void *data UNUSED)
 {
 	if (!strcmp(var, "user.useconfigonly")) {
 		ident_use_config_only = git_config_bool(var, value);
diff --git a/ident.h b/ident.h
index 96a64896a01..f55db41ff99 100644
--- a/ident.h
+++ b/ident.h
@@ -62,6 +62,7 @@ const char *fmt_name(enum want_ident);
 int committer_ident_sufficiently_given(void);
 int author_ident_sufficiently_given(void);
 
-int git_ident_config(const char *, const char *, void *);
+int git_ident_config(const char *, const char *,
+		     struct key_value_info *UNUSED, void *);
 
 #endif
diff --git a/imap-send.c b/imap-send.c
index a62424e90a4..3cc98f1a0a5 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1322,7 +1322,8 @@ static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
 	return 1;
 }
 
-static int git_imap_config(const char *var, const char *val, void *cb)
+static int git_imap_config(const char *var, const char *val,
+			   struct key_value_info *kvi, void *cb)
 {
 
 	if (!strcmp("imap.sslverify", var))
@@ -1356,7 +1357,7 @@ static int git_imap_config(const char *var, const char *val, void *cb)
 			server.host = xstrdup(val);
 		}
 	} else
-		return git_default_config(var, val, cb);
+		return git_default_config(var, val,kvi, cb);
 
 	return 0;
 }
diff --git a/ll-merge.c b/ll-merge.c
index 8be38d3bd41..2d240609ddf 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -252,6 +252,7 @@ static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
 static const char *default_ll_merge;
 
 static int read_merge_config(const char *var, const char *value,
+			     struct key_value_info *kvi UNUSED,
 			     void *cb UNUSED)
 {
 	struct ll_merge_driver *fn;
diff --git a/ls-refs.c b/ls-refs.c
index b9f3e08ec3d..90b902c84f8 100644
--- a/ls-refs.c
+++ b/ls-refs.c
@@ -136,7 +136,7 @@ static void send_possibly_unborn_head(struct ls_refs_data *data)
 }
 
 static int ls_refs_config(const char *var, const char *value,
-			  void *cb_data)
+			  struct key_value_info *kvi UNUSED, void *cb_data)
 {
 	struct ls_refs_data *data = cb_data;
 	/*
diff --git a/mailinfo.c b/mailinfo.c
index 2aeb20e5e62..e2f469c96f7 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -1241,12 +1241,13 @@ int mailinfo_parse_quoted_cr_action(const char *actionstr, int *action)
 	return 0;
 }
 
-static int git_mailinfo_config(const char *var, const char *value, void *mi_)
+static int git_mailinfo_config(const char *var, const char *value,
+			       struct key_value_info *kvi, void *mi_)
 {
 	struct mailinfo *mi = mi_;
 
 	if (!starts_with(var, "mailinfo."))
-		return git_default_config(var, value, NULL);
+		return git_default_config(var, value,kvi, NULL);
 	if (!strcmp(var, "mailinfo.scissors")) {
 		mi->use_scissors = git_config_bool(var, value);
 		return 0;
diff --git a/notes-utils.c b/notes-utils.c
index cb88171b7bb..f7a0ec60731 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -93,7 +93,8 @@ static combine_notes_fn parse_combine_notes_fn(const char *v)
 		return NULL;
 }
 
-static int notes_rewrite_config(const char *k, const char *v, void *cb)
+static int notes_rewrite_config(const char *k, const char *v,
+				struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct notes_rewrite_cfg *c = cb;
 	if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
diff --git a/notes.c b/notes.c
index 45fb7f22d1d..89b84ea0869 100644
--- a/notes.c
+++ b/notes.c
@@ -973,7 +973,8 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
 	free(globs_copy);
 }
 
-static int notes_display_config(const char *k, const char *v, void *cb)
+static int notes_display_config(const char *k, const char *v,
+				struct key_value_info *kvi UNUSED, void *cb)
 {
 	int *load_refs = cb;
 
diff --git a/pager.c b/pager.c
index b66bbff2785..4b77e0d2e57 100644
--- a/pager.c
+++ b/pager.c
@@ -39,6 +39,7 @@ static void wait_for_pager_signal(int signo)
 }
 
 static int core_pager_config(const char *var, const char *value,
+			     struct key_value_info *kvi UNUSED,
 			     void *data UNUSED)
 {
 	if (!strcmp(var, "core.pager"))
@@ -224,7 +225,9 @@ struct pager_command_config_data {
 	char *value;
 };
 
-static int pager_command_config(const char *var, const char *value, void *vdata)
+static int pager_command_config(const char *var, const char *value,
+				struct key_value_info *kvi UNUSED,
+				void *vdata)
 {
 	struct pager_command_config_data *data = vdata;
 	const char *cmd;
diff --git a/pretty.c b/pretty.c
index 76fc4f61e40..7f57379c44e 100644
--- a/pretty.c
+++ b/pretty.c
@@ -55,6 +55,7 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
 }
 
 static int git_pretty_formats_config(const char *var, const char *value,
+				     struct key_value_info *kvi UNUSED,
 				     void *cb UNUSED)
 {
 	struct cmt_fmt_map *commit_format = NULL;
diff --git a/promisor-remote.c b/promisor-remote.c
index a8dbb788e8f..ea68df91209 100644
--- a/promisor-remote.c
+++ b/promisor-remote.c
@@ -99,7 +99,9 @@ static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
 	config->promisors_tail = &r->next;
 }
 
-static int promisor_remote_config(const char *var, const char *value, void *data)
+static int promisor_remote_config(const char *var, const char *value,
+				  struct key_value_info *kvi UNUSED,
+				  void *data)
 {
 	struct promisor_remote_config *config = data;
 	const char *name;
diff --git a/remote.c b/remote.c
index 3a831cb5304..10868a963f2 100644
--- a/remote.c
+++ b/remote.c
@@ -348,7 +348,8 @@ static void read_branches_file(struct remote_state *remote_state,
 	remote->fetch_tags = 1; /* always auto-follow */
 }
 
-static int handle_config(const char *key, const char *value, void *cb)
+static int handle_config(const char *key, const char *value,
+			 struct key_value_info *kvi UNUSED, void *cb)
 {
 	const char *name;
 	size_t namelen;
diff --git a/revision.c b/revision.c
index 106ca1ce6c9..6de0132d719 100644
--- a/revision.c
+++ b/revision.c
@@ -1569,7 +1569,8 @@ struct exclude_hidden_refs_cb {
 	const char *section;
 };
 
-static int hide_refs_config(const char *var, const char *value, void *cb_data)
+static int hide_refs_config(const char *var, const char *value,
+			    struct key_value_info *kvi UNUSED, void *cb_data)
 {
 	struct exclude_hidden_refs_cb *cb = cb_data;
 	cb->exclusions->hidden_refs_configured = 1;
diff --git a/scalar.c b/scalar.c
index de07c37d210..1c44df2ec7a 100644
--- a/scalar.c
+++ b/scalar.c
@@ -593,7 +593,8 @@ static int cmd_register(int argc, const char **argv)
 	return register_dir();
 }
 
-static int get_scalar_repos(const char *key, const char *value, void *data)
+static int get_scalar_repos(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *data)
 {
 	struct string_list *list = data;
 
diff --git a/sequencer.c b/sequencer.c
index 6985ca526ae..171561c2cdb 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -214,7 +214,8 @@ static struct update_ref_record *init_update_ref_record(const char *ref)
 	return rec;
 }
 
-static int git_sequencer_config(const char *k, const char *v, void *cb)
+static int git_sequencer_config(const char *k, const char *v,
+				struct key_value_info *kvi, void *cb)
 {
 	struct replay_opts *opts = cb;
 	int status;
@@ -269,7 +270,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
 	if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
 		opts->commit_use_reference = git_config_bool(k, v);
 
-	return git_diff_basic_config(k, v, NULL);
+	return git_diff_basic_config(k, v,kvi, NULL);
 }
 
 void sequencer_init_config(struct replay_opts *opts)
@@ -2876,7 +2877,8 @@ static int git_config_string_dup(char **dest,
 	return 0;
 }
 
-static int populate_opts_cb(const char *key, const char *value, void *data)
+static int populate_opts_cb(const char *key, const char *value,
+			    struct key_value_info *kvi UNUSED, void *data)
 {
 	struct replay_opts *opts = data;
 	int error_flag = 1;
diff --git a/setup.c b/setup.c
index 6c5b85e96c1..a461dd15233 100644
--- a/setup.c
+++ b/setup.c
@@ -514,7 +514,9 @@ no_prevention_needed:
 	startup_info->original_cwd = NULL;
 }
 
-static int read_worktree_config(const char *var, const char *value, void *vdata)
+static int read_worktree_config(const char *var, const char *value,
+				struct key_value_info *kvi UNUSED,
+				void *vdata)
 {
 	struct repository_format *data = vdata;
 
@@ -585,7 +587,8 @@ static enum extension_result handle_extension(const char *var,
 	return EXTENSION_UNKNOWN;
 }
 
-static int check_repo_format(const char *var, const char *value, void *vdata)
+static int check_repo_format(const char *var, const char *value,
+			     struct key_value_info *kvi, void *vdata)
 {
 	struct repository_format *data = vdata;
 	const char *ext;
@@ -614,7 +617,7 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
 		}
 	}
 
-	return read_worktree_config(var, value, vdata);
+	return read_worktree_config(var, value,kvi, vdata);
 }
 
 static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
@@ -1113,7 +1116,8 @@ struct safe_directory_data {
 	int is_safe;
 };
 
-static int safe_directory_cb(const char *key, const char *value, void *d)
+static int safe_directory_cb(const char *key, const char *value,
+			     struct key_value_info *kvi UNUSED, void *d)
 {
 	struct safe_directory_data *data = d;
 
@@ -1169,7 +1173,8 @@ static int ensure_valid_ownership(const char *gitfile,
 	return data.is_safe;
 }
 
-static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
+static int allowed_bare_repo_cb(const char *key, const char *value,
+				struct key_value_info *kvi UNUSED, void *d)
 {
 	enum allowed_bare_repo *allowed_bare_repo = d;
 
diff --git a/submodule-config.c b/submodule-config.c
index ecf0fcf0074..fb12d8040e8 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -424,7 +424,8 @@ struct parse_config_parameter {
  * config store (.git/config, etc).  Callers are responsible for
  * checking for overrides in the main config store when appropriate.
  */
-static int parse_config(const char *var, const char *value, void *data)
+static int parse_config(const char *var, const char *value,
+			struct key_value_info *kvi UNUSED, void *data)
 {
 	struct parse_config_parameter *me = data;
 	struct submodule *submodule;
@@ -673,7 +674,8 @@ out:
 	}
 }
 
-static int gitmodules_cb(const char *var, const char *value, void *data)
+static int gitmodules_cb(const char *var, const char *value,
+			 struct key_value_info *kvi UNUSED, void *data)
 {
 	struct repository *repo = data;
 	struct parse_config_parameter parameter;
@@ -800,7 +802,9 @@ void submodule_free(struct repository *r)
 		submodule_cache_clear(r->submodule_cache);
 }
 
-static int config_print_callback(const char *var, const char *value, void *cb_data)
+static int config_print_callback(const char *var, const char *value,
+				 struct key_value_info *kvi UNUSED,
+				 void *cb_data)
 {
 	char *wanted_key = cb_data;
 
@@ -842,7 +846,9 @@ struct fetch_config {
 	int *recurse_submodules;
 };
 
-static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
+static int gitmodules_fetch_config(const char *var, const char *value,
+				   struct key_value_info *kvi UNUSED,
+				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
@@ -870,6 +876,7 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
+					  struct key_value_info *kvi UNUSED,
 					  void *cb)
 {
 	int *max_jobs = cb;
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index ad78fc17683..00cd49e5145 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -42,7 +42,8 @@
  *
  */
 
-static int iterate_cb(const char *var, const char *value, void *data UNUSED)
+static int iterate_cb(const char *var, const char *value,
+		      struct key_value_info *kvi UNUSED, void *data UNUSED)
 {
 	static int nr;
 
@@ -59,7 +60,8 @@ static int iterate_cb(const char *var, const char *value, void *data UNUSED)
 	return 0;
 }
 
-static int parse_int_cb(const char *var, const char *value, void *data)
+static int parse_int_cb(const char *var, const char *value,
+			struct key_value_info *kvi UNUSED, void *data)
 {
 	const char *key_to_match = data;
 
@@ -70,7 +72,8 @@ static int parse_int_cb(const char *var, const char *value, void *data)
 	return 0;
 }
 
-static int early_config_cb(const char *var, const char *value, void *vdata)
+static int early_config_cb(const char *var, const char *value,
+			   struct key_value_info *kvi UNUSED, void *vdata)
 {
 	const char *key = vdata;
 
diff --git a/t/helper/test-userdiff.c b/t/helper/test-userdiff.c
index 680124a6760..33dd3a65241 100644
--- a/t/helper/test-userdiff.c
+++ b/t/helper/test-userdiff.c
@@ -12,7 +12,9 @@ static int driver_cb(struct userdiff_driver *driver,
 	return 0;
 }
 
-static int cmd__userdiff_config(const char *var, const char *value, void *cb UNUSED)
+static int cmd__userdiff_config(const char *var, const char *value,
+				struct key_value_info *kvi UNUSED,
+				void *cb UNUSED)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 78cfc15d52d..6871258d468 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -99,7 +99,8 @@ struct tr2_cfg_data {
 /*
  * See if the given config key matches any of our patterns of interest.
  */
-static int tr2_cfg_cb(const char *key, const char *value, void *d)
+static int tr2_cfg_cb(const char *key, const char *value,
+		      struct key_value_info *kvi UNUSED, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
diff --git a/trace2/tr2_sysenv.c b/trace2/tr2_sysenv.c
index 069786cb927..d17a08b0f50 100644
--- a/trace2/tr2_sysenv.c
+++ b/trace2/tr2_sysenv.c
@@ -57,7 +57,8 @@ static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
 };
 /* clang-format on */
 
-static int tr2_sysenv_cb(const char *key, const char *value, void *d)
+static int tr2_sysenv_cb(const char *key, const char *value,
+			 struct key_value_info *kvi UNUSED, void *d)
 {
 	int k;
 
diff --git a/trailer.c b/trailer.c
index a2c3ed6f28c..fac7e2cfe6e 100644
--- a/trailer.c
+++ b/trailer.c
@@ -482,6 +482,7 @@ static struct {
 };
 
 static int git_trailer_default_config(const char *conf_key, const char *value,
+				      struct key_value_info *kvi UNUSED,
 				      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
@@ -514,6 +515,7 @@ static int git_trailer_default_config(const char *conf_key, const char *value,
 }
 
 static int git_trailer_config(const char *conf_key, const char *value,
+			      struct key_value_info *kvi UNUSED,
 			      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
diff --git a/upload-pack.c b/upload-pack.c
index e23f16dfdd2..5f8232ff078 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1298,7 +1298,9 @@ static int parse_object_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int upload_pack_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_config(const char *var, const char *value,
+			      struct key_value_info *kvi UNUSED,
+			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
@@ -1339,7 +1341,9 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
 }
 
-static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_protected_config(const char *var, const char *value,
+					struct key_value_info *kvi UNUSED,
+					void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
diff --git a/urlmatch.c b/urlmatch.c
index eba0bdd77fe..47683974d8c 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -551,7 +551,8 @@ static int cmp_matches(const struct urlmatch_item *a,
 	return 0;
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb)
+int urlmatch_config_entry(const char *var, const char *value,
+			  struct key_value_info *kvi UNUSED, void *cb)
 {
 	struct string_list_item *item;
 	struct urlmatch_config *collect = cb;
diff --git a/urlmatch.h b/urlmatch.h
index bee374a642c..f6eac4af9ea 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -71,7 +71,8 @@ struct urlmatch_config {
 	.vars = STRING_LIST_INIT_DUP, \
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb);
+int urlmatch_config_entry(const char *var, const char *value,
+			  struct key_value_info *kvi, void *cb);
 void urlmatch_config_release(struct urlmatch_config *config);
 
 #endif /* URL_MATCH_H */
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 0460e03f5ed..f1aac104285 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -307,7 +307,8 @@ int xdiff_compare_lines(const char *l1, long s1,
 
 int git_xmerge_style = -1;
 
-int git_xmerge_config(const char *var, const char *value, void *cb)
+int git_xmerge_config(const char *var, const char *value,
+		      struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "merge.conflictstyle")) {
 		if (!value)
@@ -327,5 +328,5 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
 			    value, var);
 		return 0;
 	}
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value,kvi, cb);
 }
diff --git a/xdiff-interface.h b/xdiff-interface.h
index 3750794afe9..c1676b11702 100644
--- a/xdiff-interface.h
+++ b/xdiff-interface.h
@@ -50,7 +50,8 @@ int buffer_is_binary(const char *ptr, unsigned long size);
 
 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
 void xdiff_clear_find_func(xdemitconf_t *xecfg);
-int git_xmerge_config(const char *var, const char *value, void *cb);
+int git_xmerge_config(const char *var, const char *value,
+		      struct key_value_info *kvi, void *cb);
 extern int git_xmerge_style;
 
 /*
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 05/14] (RFC-only) config: finish config_fn_t refactor
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (3 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 04/14] (RFC-only) config: apply cocci to config_fn_t implementations Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-01 22:17     ` Jonathan Tan
  2023-05-30 18:42   ` [PATCH v2 06/14] config.c: pass kvi in configsets Glen Choo via GitGitGadget
                     ` (9 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Here's an exhaustive list of all of the changes:

* Cases that need a judgement call

  - trace2/tr2_cfg.c:tr2_cfg_set_fl()

    This function needs to account for tr2_cfg_cb() now using "kvi".
    Since this is only called (indirectly) by git_config_set(), config
    source information has never been available here, so just pass NULL.
    It will be tr2_cfg_cb()'s responsibility to not use "kvi".

  - builtin/checkout.c:checkout_main()

    This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
    "kvi" doesn't apply, so just pass NULL. This might be worth
    refactoring away, since git_xmerge_config() can call
    git_default_config().

* Hard for cocci to catch

  - urlmatch.c

    Manually refactor the custom config callbacks in "struct
    urlmatch_config".

  - diff.h, fsck.h, grep.h, ident.h, xdiff-interface.h

    "struct key_value_info" hasn't been defined yet, so forward declare
    it. Alternatively, maybe these files should "#include config.h".

* Likely deficiencies in .cocci patch

  - submodule-config.c:gitmodules_cb()

    Manually refactor a parse_config() call that gets missed because it
    uses a different "*data" arg.

  - grep.h, various

    Manually refactor grep_config() calls. Not sure why these don't get
    picked up.

  - config.c:git_config_include(), http.c:http_options()

    Manually add "kvi" where it was missed. Not sure why they get missed.

  - builtin/clone.c:write_one_config()

    Manually refactor a git_clone_config() call. Probably got missed
    because I didn't include git_config_parse_parameter().

  - ident.h

    Remove the UNUSED attribute. Not sure why this is the only instance
    of this.

  - git-compat-util.h, compat/mingw.[h|c]

    Manually refactor noop_core_config(), platform_core_config() and
    mingw_core_config(). I can probably add these as "manual fixups" in
    cocci.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/checkout.c | 2 +-
 builtin/clone.c    | 4 ++--
 builtin/grep.c     | 2 +-
 compat/mingw.c     | 3 ++-
 compat/mingw.h     | 4 +++-
 diff.h             | 1 +
 fsck.h             | 1 +
 git-compat-util.h  | 2 ++
 grep.c             | 6 +++---
 grep.h             | 4 +++-
 http.c             | 5 +++--
 ident.h            | 3 ++-
 submodule-config.c | 4 ++--
 trace2/tr2_cfg.c   | 2 +-
 urlmatch.c         | 6 +++---
 xdiff-interface.h  | 2 ++
 16 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 92017ba6696..9641423dc2f 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1687,7 +1687,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 
 	if (opts->conflict_style) {
 		opts->merge = 1; /* implied */
-		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
+		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL, NULL);
 	}
 	if (opts->force) {
 		opts->discard_changes = 1;
diff --git a/builtin/clone.c b/builtin/clone.c
index 1e1cf104194..e654757c45d 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -791,14 +791,14 @@ static int git_clone_config(const char *k, const char *v,
 }
 
 static int write_one_config(const char *key, const char *value,
-			    struct key_value_info *kvi UNUSED, void *data)
+			    struct key_value_info *kvi, void *data)
 {
 	/*
 	 * give git_clone_config a chance to write config values back to the
 	 * environment, since git_config_set_multivar_gently only deals with
 	 * config-file writes
 	 */
-	int apply_failed = git_clone_config(key, value, data);
+	int apply_failed = git_clone_config(key, value, kvi, data);
 	if (apply_failed)
 		return apply_failed;
 
diff --git a/builtin/grep.c b/builtin/grep.c
index 177befc3ed4..6e795f9f3a2 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -290,7 +290,7 @@ static int wait_all(void)
 static int grep_cmd_config(const char *var, const char *value,
 			   struct key_value_info *kvi, void *cb)
 {
-	int st = grep_config(var, value, cb);
+	int st = grep_config(var, value, kvi, cb);
 
 	if (git_color_config(var, value, cb) < 0)
 		st = -1;
diff --git a/compat/mingw.c b/compat/mingw.c
index 94c5a1daa40..c8181469a2f 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -242,7 +242,8 @@ static int core_restrict_inherited_handles = -1;
 static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
 static char *unset_environment_variables;
 
-int mingw_core_config(const char *var, const char *value, void *cb)
+int mingw_core_config(const char *var, const char *value,
+		      struct key_value_info *kvi UNUSED, void *cb)
 {
 	if (!strcmp(var, "core.hidedotfiles")) {
 		if (value && !strcasecmp(value, "dotgitonly"))
diff --git a/compat/mingw.h b/compat/mingw.h
index 209cf7cebad..4f2b489b883 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -11,7 +11,9 @@ typedef _sigset_t sigset_t;
 #undef _POSIX_THREAD_SAFE_FUNCTIONS
 #endif
 
-int mingw_core_config(const char *var, const char *value, void *cb);
+struct key_value_info;
+int mingw_core_config(const char *var, const char *value,
+		      struct key_value_info *, void *cb);
 #define platform_core_config mingw_core_config
 
 /*
diff --git a/diff.h b/diff.h
index 6a3efa63753..2ceb0fd2d66 100644
--- a/diff.h
+++ b/diff.h
@@ -532,6 +532,7 @@ void free_diffstat_info(struct diffstat_t *diffstat);
 int parse_long_opt(const char *opt, const char **argv,
 		   const char **optarg);
 
+struct key_value_info;
 int git_diff_basic_config(const char *var, const char *value,
 			  struct key_value_info *kvi, void *cb);
 int git_diff_heuristic_config(const char *var, const char *value, void *cb);
diff --git a/fsck.h b/fsck.h
index a06f202576c..914e67a067d 100644
--- a/fsck.h
+++ b/fsck.h
@@ -233,6 +233,7 @@ void fsck_put_object_name(struct fsck_options *options,
 const char *fsck_describe_object(struct fsck_options *options,
 				 const struct object_id *oid);
 
+struct key_value_info;
 /*
  * git_config() callback for use by fsck-y tools that want to support
  * fsck.<msg> fsck.skipList etc.
diff --git a/git-compat-util.h b/git-compat-util.h
index 4a200a9fb41..6812b592c15 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -440,8 +440,10 @@ typedef uintmax_t timestamp_t;
 #endif
 
 #ifndef platform_core_config
+struct key_value_info;
 static inline int noop_core_config(const char *var UNUSED,
 				   const char *value UNUSED,
+				   struct key_value_info *kvi UNUSED,
 				   void *cb UNUSED)
 {
 	return 0;
diff --git a/grep.c b/grep.c
index 1516b0689d0..2d3b9bf5d92 100644
--- a/grep.c
+++ b/grep.c
@@ -55,7 +55,7 @@ define_list_config_array_extra(color_grep_slots, {"match"});
  * the grep_defaults template.
  */
 int grep_config(const char *var, const char *value,
-		struct key_value_info *kvi UNUSED, void *cb)
+		struct key_value_info *kvi, void *cb)
 {
 	struct grep_opt *opt = cb;
 	const char *slot;
@@ -90,9 +90,9 @@ int grep_config(const char *var, const char *value,
 	if (!strcmp(var, "color.grep"))
 		opt->color = git_config_colorbool(var, value);
 	if (!strcmp(var, "color.grep.match")) {
-		if (grep_config("color.grep.matchcontext", value, cb) < 0)
+		if (grep_config("color.grep.matchcontext", value, kvi, cb) < 0)
 			return -1;
-		if (grep_config("color.grep.matchselected", value, cb) < 0)
+		if (grep_config("color.grep.matchselected", value, kvi, cb) < 0)
 			return -1;
 	} else if (skip_prefix(var, "color.grep.", &slot)) {
 		int i = LOOKUP_CONFIG(color_grep_slots, slot);
diff --git a/grep.h b/grep.h
index c59592e3bdb..6d2fb0ada54 100644
--- a/grep.h
+++ b/grep.h
@@ -202,7 +202,9 @@ struct grep_opt {
 	.output = std_output, \
 }
 
-int grep_config(const char *var, const char *value, void *);
+struct key_value_info;
+int grep_config(const char *var, const char *value, struct key_value_info *kvi,
+		void *);
 void grep_init(struct grep_opt *, struct repository *repo);
 
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
diff --git a/http.c b/http.c
index d5d82c5230f..3d4292eba6a 100644
--- a/http.c
+++ b/http.c
@@ -361,7 +361,8 @@ static void process_curl_messages(void)
 	}
 }
 
-static int http_options(const char *var, const char *value, void *cb)
+static int http_options(const char *var, const char *value,
+			struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp("http.version", var)) {
 		return git_config_string(&curl_http_version, var, value);
@@ -532,7 +533,7 @@ static int http_options(const char *var, const char *value, void *cb)
 	}
 
 	/* Fall back on the default ones */
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, kvi, cb);
 }
 
 static int curl_empty_auth_enabled(void)
diff --git a/ident.h b/ident.h
index f55db41ff99..4e3bd347c52 100644
--- a/ident.h
+++ b/ident.h
@@ -62,7 +62,8 @@ const char *fmt_name(enum want_ident);
 int committer_ident_sufficiently_given(void);
 int author_ident_sufficiently_given(void);
 
+struct key_value_info;
 int git_ident_config(const char *, const char *,
-		     struct key_value_info *UNUSED, void *);
+		     struct key_value_info *, void *);
 
 #endif
diff --git a/submodule-config.c b/submodule-config.c
index fb12d8040e8..0b794b6f32e 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -675,7 +675,7 @@ out:
 }
 
 static int gitmodules_cb(const char *var, const char *value,
-			 struct key_value_info *kvi UNUSED, void *data)
+			 struct key_value_info *kvi, void *data)
 {
 	struct repository *repo = data;
 	struct parse_config_parameter parameter;
@@ -685,7 +685,7 @@ static int gitmodules_cb(const char *var, const char *value,
 	parameter.gitmodules_oid = null_oid();
 	parameter.overwrite = 1;
 
-	return parse_config(var, value, &parameter);
+	return parse_config(var, value, kvi, &parameter);
 }
 
 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 6871258d468..8ed139c69f4 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -146,5 +146,5 @@ void tr2_cfg_set_fl(const char *file, int line, const char *key,
 	struct tr2_cfg_data data = { file, line };
 
 	if (tr2_cfg_load_patterns() > 0)
-		tr2_cfg_cb(key, value, &data);
+		tr2_cfg_cb(key, value, NULL, &data);
 }
diff --git a/urlmatch.c b/urlmatch.c
index 47683974d8c..c94500b61b3 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -552,7 +552,7 @@ static int cmp_matches(const struct urlmatch_item *a,
 }
 
 int urlmatch_config_entry(const char *var, const char *value,
-			  struct key_value_info *kvi UNUSED, void *cb)
+			  struct key_value_info *kvi, void *cb)
 {
 	struct string_list_item *item;
 	struct urlmatch_config *collect = cb;
@@ -566,7 +566,7 @@ int urlmatch_config_entry(const char *var, const char *value,
 
 	if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
 		if (collect->cascade_fn)
-			return collect->cascade_fn(var, value, cb);
+			return collect->cascade_fn(var, value, kvi, cb);
 		return 0; /* not interested */
 	}
 	dot = strrchr(key, '.');
@@ -610,7 +610,7 @@ int urlmatch_config_entry(const char *var, const char *value,
 	strbuf_addstr(&synthkey, collect->section);
 	strbuf_addch(&synthkey, '.');
 	strbuf_addstr(&synthkey, key);
-	retval = collect->collect_fn(synthkey.buf, value, collect->cb);
+	retval = collect->collect_fn(synthkey.buf, value, kvi, collect->cb);
 
 	strbuf_release(&synthkey);
 	return retval;
diff --git a/xdiff-interface.h b/xdiff-interface.h
index c1676b11702..749cdf77579 100644
--- a/xdiff-interface.h
+++ b/xdiff-interface.h
@@ -50,6 +50,8 @@ int buffer_is_binary(const char *ptr, unsigned long size);
 
 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
 void xdiff_clear_find_func(xdemitconf_t *xecfg);
+
+struct key_value_info;
 int git_xmerge_config(const char *var, const char *value,
 		      struct key_value_info *kvi, void *cb);
 extern int git_xmerge_style;
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 06/14] config.c: pass kvi in configsets
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (4 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 05/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-01 22:21     ` Jonathan Tan
  2023-05-30 18:42   ` [PATCH v2 07/14] config: provide kvi with config files Glen Choo via GitGitGadget
                     ` (8 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Trivially pass "struct key_value_info" to config callbacks in
configset_iter(). Then, in config callbacks that are only used with
configsets, use the "kvi" arg to replace calls to current_config_*(),
and delete current_config_line() because it has no remaining callers.

This leaves builtin/config.c and config.c as the only remaining users of
current_config_*().

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/remote.c       |  8 ++++----
 config.c               | 34 +++++++++++++++-------------------
 config.h               |  2 +-
 remote.c               |  6 +++---
 t/helper/test-config.c | 10 +++++-----
 5 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index edb4a9ddd7f..0a18c49f1f8 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -646,17 +646,17 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	struct key_value_info *kvi UNUSED, void *cb)
+	struct key_value_info *kvi, void *cb)
 {
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
 	    !value || strcmp(value, info->old_name))
 		return 0;
 
-	info->scope = current_config_scope();
+	info->scope = kvi->scope;
 	strbuf_reset(&info->origin);
-	strbuf_addstr(&info->origin, current_config_name());
-	info->linenr = current_config_line();
+	strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
+	info->linenr = kvi->linenr;
 
 	return 0;
 }
diff --git a/config.c b/config.c
index 1bc2e35a3e0..66078b22eef 100644
--- a/config.c
+++ b/config.c
@@ -2301,19 +2301,18 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 	struct string_list *values;
 	struct config_set_element *entry;
 	struct configset_list *list = &set->list;
+	struct key_value_info *kvi;
 
 	for (i = 0; i < list->nr; i++) {
 		entry = list->items[i].e;
 		value_index = list->items[i].value_index;
 		values = &entry->value_list;
+		kvi = values->items[value_index].util;
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
 
-		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
-			git_die_config_linenr(entry->key,
-					      reader->config_kvi->filename,
-					      reader->config_kvi->linenr);
-
+		if (fn(entry->key, values->items[value_index].string, kvi, data) < 0)
+			git_die_config_linenr(entry->key, kvi->filename, kvi->linenr);
 		config_reader_set_kvi(reader, NULL);
 	}
 }
@@ -3951,13 +3950,8 @@ static int reader_origin_type(struct config_reader *reader,
 	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";
@@ -3974,6 +3968,16 @@ const char *current_config_origin_type(void)
 	}
 }
 
+const char *current_config_origin_type(void)
+{
+	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
+
+	if (reader_origin_type(&the_reader, &type))
+		BUG("current_config_origin_type called outside config callback");
+
+	return config_origin_type_name(type);
+}
+
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4021,14 +4025,6 @@ enum config_scope current_config_scope(void)
 		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;
diff --git a/config.h b/config.h
index 9e0c1f12165..ea4ffe37a55 100644
--- a/config.h
+++ b/config.h
@@ -367,7 +367,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 enum config_scope current_config_scope(void);
 const char *current_config_origin_type(void);
 const char *current_config_name(void);
-int current_config_line(void);
+const char *config_origin_type_name(enum config_origin_type type);
 
 /*
  * Match and parse a config key of the form:
diff --git a/remote.c b/remote.c
index 10868a963f2..266601157e3 100644
--- a/remote.c
+++ b/remote.c
@@ -349,7 +349,7 @@ static void read_branches_file(struct remote_state *remote_state,
 }
 
 static int handle_config(const char *key, const char *value,
-			 struct key_value_info *kvi UNUSED, void *cb)
+			 struct key_value_info *kvi, void *cb)
 {
 	const char *name;
 	size_t namelen;
@@ -414,8 +414,8 @@ static int handle_config(const char *key, const char *value,
 	}
 	remote = make_remote(remote_state, name, namelen);
 	remote->origin = REMOTE_CONFIG;
-	if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
-	    current_config_scope() == CONFIG_SCOPE_WORKTREE)
+	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
+	    kvi->scope == CONFIG_SCOPE_WORKTREE)
 		remote->configured_in_repo = 1;
 	if (!strcmp(subkey, "mirror"))
 		remote->mirror = git_config_bool(key, value);
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 00cd49e5145..7027ffa187f 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -43,7 +43,7 @@
  */
 
 static int iterate_cb(const char *var, const char *value,
-		      struct key_value_info *kvi UNUSED, void *data UNUSED)
+		      struct key_value_info *kvi, void *data UNUSED)
 {
 	static int nr;
 
@@ -52,10 +52,10 @@ static int iterate_cb(const char *var, const char *value,
 
 	printf("key=%s\n", var);
 	printf("value=%s\n", value ? value : "(null)");
-	printf("origin=%s\n", current_config_origin_type());
-	printf("name=%s\n", current_config_name());
-	printf("lno=%d\n", current_config_line());
-	printf("scope=%s\n", config_scope_name(current_config_scope()));
+	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
+	printf("name=%s\n", kvi->filename ? kvi->filename : "");
+	printf("lno=%d\n", kvi->linenr);
+	printf("scope=%s\n", config_scope_name(kvi->scope));
 
 	return 0;
 }
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 07/14] config: provide kvi with config files
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (5 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 06/14] config.c: pass kvi in configsets Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-01 22:41     ` Jonathan Tan
  2023-06-01 23:54     ` Jonathan Tan
  2023-05-30 18:42   ` [PATCH v2 08/14] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
                     ` (7 subsequent siblings)
  14 siblings, 2 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Refactor out the configset logic that caches "struct config_source" and
"enum config_scope" as a "struct key_value_info", and use it to pass the
"kvi" arg to config callbacks when parsing config files. Get the "enum
config_scope" value by plumbing an additional arg through
git_config_from_file_with_options() and the underlying machinery.

We do not exercise the "kvi" arg yet because the remaining
current_config_*() callers may be used with config_with_options(), which
may read config from parameters, but parameters don't pass "kvi" yet.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 bundle-uri.c       |   1 +
 config.c           | 104 +++++++++++++++++++++++++++++----------------
 config.h           |   8 ++--
 fsck.c             |   3 +-
 submodule-config.c |   5 ++-
 5 files changed, 79 insertions(+), 42 deletions(-)

diff --git a/bundle-uri.c b/bundle-uri.c
index 7f01fbbff74..bb88ccbca4b 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -254,6 +254,7 @@ int bundle_uri_parse_config_format(const char *uri,
 	}
 	result = git_config_from_file_with_options(config_to_bundle_list,
 						   filename, list,
+						   CONFIG_SCOPE_UNKNOWN,
 						   &opts);
 
 	if (!result && list->mode == BUNDLE_MODE_NONE) {
diff --git a/config.c b/config.c
index 66078b22eef..4b6830fcc79 100644
--- a/config.c
+++ b/config.c
@@ -251,7 +251,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    !cs ? "<unknown>" :
 			    cs->name ? cs->name :
 			    "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,
+							current_config_scope(),
+							NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -481,7 +483,7 @@ static int include_condition_is_true(struct config_source *cs,
 }
 
 static int git_config_include(const char *var, const char *value,
-			      struct key_value_info *kvi UNUSED, void *data)
+			      struct key_value_info *kvi, void *data)
 {
 	struct config_include_data *inc = data;
 	struct config_source *cs = inc->config_reader->source;
@@ -493,7 +495,7 @@ static int git_config_include(const char *var, const char *value,
 	 * Pass along all values, including "include" directives; this makes it
 	 * possible to query information on the includes themselves.
 	 */
-	ret = inc->fn(var, value, NULL, inc->data);
+	ret = inc->fn(var, value, kvi, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -929,8 +931,8 @@ 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;
@@ -963,7 +965,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, NULL, data);
+	kvi->linenr = cs->linenr;
+	ret = fn(name->buf, value, kvi, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1062,8 +1065,19 @@ 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;
+}
+
 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;
@@ -1147,7 +1161,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;
 	}
 
@@ -2005,9 +2019,11 @@ int git_default_config(const char *var, const char *value,
  * this function.
  */
 static int do_config_from(struct config_reader *reader,
-			  struct config_source *top, config_fn_t fn, void *data,
+			  struct config_source *top, config_fn_t fn,
+			  void *data, enum config_scope scope,
 			  const struct config_options *opts)
 {
+	struct key_value_info kvi = { 0 };
 	int ret;
 
 	/* push config-file parsing state stack */
@@ -2017,8 +2033,9 @@ static int do_config_from(struct config_reader *reader,
 	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);
@@ -2032,7 +2049,8 @@ static int do_config_from_file(struct config_reader *reader,
 			       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;
@@ -2047,19 +2065,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(reader, &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);
+				   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;
@@ -2070,7 +2089,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
 	f = fopen_or_warn(filename, "r");
 	if (f) {
 		ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
-					  filename, filename, f, data, opts);
+					  filename, filename, f, data, scope,
+					  opts);
 		fclose(f);
 	}
 	return ret;
@@ -2078,13 +2098,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;
 
@@ -2099,14 +2121,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(&the_reader, &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;
@@ -2122,7 +2145,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;
@@ -2131,13 +2154,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)
@@ -2212,27 +2236,34 @@ static int do_git_config_sequence(struct config_reader *reader,
 	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 && repository_format_worktree_config) {
 		char *path = git_pathdup("config.worktree");
 		if (!access_or_die(path, R_OK, 0))
-			ret += git_config_from_file(fn, path, data);
+			ret += git_config_from_file_with_options(fn, path, data,
+								 CONFIG_SCOPE_WORKTREE,
+								 NULL);
 		free(path);
 	}
 
@@ -2274,14 +2305,16 @@ int config_with_options(config_fn_t fn, void *data,
 	 * 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) {
 		struct repository *repo = config_source->repo ?
 			config_source->repo : the_repository;
 		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, fn, data);
 	}
@@ -2423,16 +2456,14 @@ static int configset_add_value(struct config_reader *reader,
 	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;
+		kvi_from_source(reader->source, current_config_scope(), kv_info);
 	} 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->scope = reader->parsing_scope;
 	si->util = kv_info;
 
 	return 0;
@@ -3472,7 +3503,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;
diff --git a/config.h b/config.h
index ea4ffe37a55..ca5aac62980 100644
--- a/config.h
+++ b/config.h
@@ -150,16 +150,18 @@ int git_default_config(const char *, const char *, struct key_value_info *,
 int git_config_from_file(config_fn_t fn, const char *, void *);
 
 int git_config_from_file_with_options(config_fn_t fn, const char *,
-				      void *,
+				      void *, enum config_scope,
 				      const struct config_options *);
 int git_config_from_mem(config_fn_t fn,
 			const enum config_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);
 int git_config_from_blob_oid(config_fn_t fn, const char *name,
 			     struct repository *repo,
-			     const struct object_id *oid, void *data);
+			     const struct object_id *oid, void *data,
+			     enum config_scope scope);
 void git_config_push_parameter(const char *text);
 void git_config_push_env(const char *spec);
 int git_config_from_parameters(config_fn_t fn, void *data);
diff --git a/fsck.c b/fsck.c
index 1569e64f011..ec26857c79d 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1236,7 +1236,8 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
 		data.ret = 0;
 		config_opts.error_action = CONFIG_ERROR_SILENT;
 		if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
-					".gitmodules", buf, size, &data, &config_opts))
+					".gitmodules", buf, size, &data,
+					CONFIG_SCOPE_UNKNOWN, &config_opts))
 			data.ret |= report(options, oid, OBJ_BLOB,
 					FSCK_MSG_GITMODULES_PARSE,
 					"could not parse gitmodules blob");
diff --git a/submodule-config.c b/submodule-config.c
index 0b794b6f32e..7d773f33621 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -604,7 +604,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 	parameter.gitmodules_oid = &oid;
 	parameter.overwrite = 0;
 	git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
-			config, config_size, &parameter, NULL);
+			    config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
 	strbuf_release(&rev);
 	free(config);
 
@@ -713,7 +713,8 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
 
 	if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
 		git_config_from_blob_oid(gitmodules_cb, rev.buf,
-					 the_repository, &oid, the_repository);
+					 the_repository, &oid, the_repository,
+					 CONFIG_SCOPE_UNKNOWN);
 	}
 	strbuf_release(&rev);
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 08/14] builtin/config.c: test misuse of format_config()
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (6 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 07/14] config: provide kvi with config files Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-05-30 18:42   ` [PATCH v2 09/14] config.c: provide kvi with CLI config Glen Choo via GitGitGadget
                     ` (6 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

current_config_*() functions aren't meant to be called outside of
config callbacks because they read state that is only set when iterating
through config. However, several sites in builtin/config.c are
indirectly calling these functions outside of config callbacks thanks to
the format_config() helper. Show the current, bad behavior via tests
so that the fixes in a subsequent commit will be clearer.

The misbehaving cases are:

* "git config --get-urlmatch --show-scope" results in an "unknown"
   scope, where it arguably should show the config file's scope. It's
   clear that this wasn't intended, though: we knew that
   "--get-urlmatch" couldn't show config source metadata, which is why
   "--show-origin" was marked incompatible with "--get-urlmatch" when
   it was introduced [1]. It was most likely a mistake that we allowed
   "--show-scope" to sneak through.

* Similarly, "git config --default" doesn't set config source metadata ,
  so "--show-scope" also results in "unknown", and "--show-origin"
  results in a BUG().

[1] https://lore.kernel.org/git/20160205112001.GA13397@sigill.intra.peff.net/

Signed-off-by: Glen Choo <chooglen@google.com>
---
 t/t1300-config.sh | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 2575279ab84..57fe250b78f 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1627,6 +1627,21 @@ test_expect_success 'urlmatch' '
 	test_cmp expect actual
 '
 
+test_expect_success 'urlmatch with --show-scope' '
+	cat >.git/config <<-\EOF &&
+	[http "https://weak.example.com"]
+		sslVerify = false
+		cookieFile = /tmp/cookie.txt
+	EOF
+
+	cat >expect <<-EOF &&
+	unknown	http.cookiefile /tmp/cookie.txt
+	unknown	http.sslverify false
+	EOF
+	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'urlmatch favors more specific URLs' '
 	cat >.git/config <<-\EOF &&
 	[http "https://example.com/"]
@@ -2014,6 +2029,10 @@ test_expect_success '--show-origin blob ref' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-origin with --default' '
+	test_must_fail git config --show-origin --default foo some.key
+'
+
 test_expect_success '--show-scope with --list' '
 	cat >expect <<-EOF &&
 	global	user.global=true
@@ -2082,6 +2101,12 @@ test_expect_success '--show-scope with --show-origin' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-scope with --default' '
+	git config --show-scope --default foo some.key >actual &&
+	echo "unknown	foo" >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'override global and system config' '
 	test_when_finished rm -f \"\$HOME\"/.gitconfig &&
 	cat >"$HOME"/.gitconfig <<-EOF &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 09/14] config.c: provide kvi with CLI config
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (7 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 08/14] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-01 23:35     ` Jonathan Tan
  2023-05-30 18:42   ` [PATCH v2 10/14] trace2: plumb config kvi Glen Choo via GitGitGadget
                     ` (5 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Refactor out kvi_from_param() from the logic that caches CLI config in
configsets, and use it to pass the "kvi" arg to config callbacks when
parsing CLI config. Now that "kvi" is always present when config
machinery calls config callbacks, plumb "kvi" so that we can replace
nearly all calls to current_config_*(). (The exception is an edge case
where trace2/*.c calls current_config_scope(). That will be handled in a
later commit.) Note that this results in "kvi" containing a different,
more complete set of information than the mocked up "struct
config_source" in git_config_from_parameters().

Plumbing "kvi" reveals a few places where we've been doing the wrong
thing:

* git_config_parse_parameter() hasn't been setting config source
  information, so plumb "kvi" there too.

* "git config --get-urlmatch --show-scope" iterates config to collect
  values, but then attempts to display the scope after config iteration.
  Fix this by copying the "kvi" arg in the collection phase so that it
  can be read back later. This means that we can now support "git config
  --get-urlmatch --show-origin" (we don't allow this combination of args
  because of this bug), but that is left unchanged for now.

* "git config --default" doesn't have config source metadata when
  displaying the default value. Fix this by treating the default value
  as if it came from the command line (e.g. like we do with "git -c" or
  "git config --file"), using kvi_from_param().

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/config.c  | 41 ++++++++++++++-----------
 config.c          | 78 ++++++++++++++++++++++-------------------------
 config.h          |  3 +-
 t/t1300-config.sh | 10 +++---
 4 files changed, 68 insertions(+), 64 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index b2ad7351d0a..2fcad601a7b 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -191,37 +191,37 @@ 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(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(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_,
-			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
+			   struct key_value_info *kvi, void *cb UNUSED)
 {
 	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);
@@ -239,12 +239,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_, 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) {
@@ -299,7 +300,7 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
 }
 
 static int collect_config(const char *key_, const char *value_,
-			  struct key_value_info *kvi UNUSED, void *cb)
+			  struct key_value_info *kvi, void *cb)
 {
 	struct strbuf_list *values = cb;
 
@@ -316,7 +317,7 @@ static int collect_config(const char *key_, const char *value_,
 	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)
@@ -377,11 +378,14 @@ static int get_value(const char *key_, const char *regex_, unsigned flags)
 			    &given_config_source, &config_options);
 
 	if (!values.nr && default_value) {
+		struct key_value_info kvi = { 0 };
 		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);
 	}
@@ -556,10 +560,11 @@ 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,
-			       struct key_value_info *kvi UNUSED, void *cb)
+			       struct key_value_info *kvi, void *cb)
 {
 	struct string_list *values = cb;
 	struct string_list_item *item = string_list_insert(values, var);
@@ -572,6 +577,7 @@ static int urlmatch_collect_fn(const char *var, const char *value,
 	} else {
 		strbuf_reset(&matched->value);
 	}
+	memcpy(&matched->kvi, kvi, sizeof(struct key_value_info));
 
 	if (value) {
 		strbuf_addstr(&matched->value, value);
@@ -618,7 +624,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);
 
diff --git a/config.c b/config.c
index 4b6830fcc79..1779fe62f89 100644
--- a/config.c
+++ b/config.c
@@ -211,7 +211,9 @@ 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(struct config_source *cs,
+			       struct key_value_info *kvi,
+			       const char *path,
 			       struct config_include_data *inc)
 {
 	int ret = 0;
@@ -252,8 +254,7 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    cs->name ? cs->name :
 			    "the command line");
 		ret = git_config_from_file_with_options(git_config_include, path, inc,
-							current_config_scope(),
-							NULL);
+							kvi->scope, NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -500,7 +501,7 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, kvi, value, inc);
 
 	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
 	    cond && include_condition_is_true(cs, inc, cond, cond_len) &&
@@ -509,7 +510,7 @@ static int git_config_include(const char *var, const char *value,
 
 		if (inc->opts->unconditional_remote_url)
 			inc->fn = forbid_remote_url;
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -667,7 +668,8 @@ 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;
@@ -677,17 +679,30 @@ static int config_parse_pair(const char *key, const char *value,
 	if (git_config_parse_key(key, &canonical_name, NULL))
 		return -1;
 
-	ret = (fn(canonical_name, value, NULL, data) < 0) ? -1 : 0;
+	ret = (fn(canonical_name, value, kvi, 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;
+}
+
 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 = { 0 };
+
+	kvi_from_param(&kvi);
 
 	pair = strbuf_split_str(text, '=', 2);
 	if (!pair[0])
@@ -706,12 +721,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 +761,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 {
@@ -770,10 +786,13 @@ int git_config_from_parameters(config_fn_t fn, void *data)
 	int ret = 0;
 	char *envw = NULL;
 	struct config_source source = CONFIG_SOURCE_INIT;
+	struct key_value_info kvi = { 0 };
 
 	source.origin_type = CONFIG_ORIGIN_CMDLINE;
 	config_reader_push_source(&the_reader, &source);
 
+	kvi_from_param(&kvi);
+
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -809,7 +828,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 +839,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;
 		}
@@ -2422,7 +2441,8 @@ 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(struct key_value_info *kvi_p,
+			       struct config_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2453,16 +2473,10 @@ 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) {
-		kvi_from_source(reader->source, current_config_scope(), kv_info);
+		kvi_from_source(reader->source, kvi_p->scope, kv_info);
 	} 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;
+		kvi_from_param(kv_info);
 	}
 	si->util = kv_info;
 
@@ -2518,10 +2532,10 @@ struct configset_add_data {
 #define CONFIGSET_ADD_INIT { 0 }
 
 static int config_set_callback(const char *key, const char *value,
-			       struct key_value_info *kvi UNUSED, void *cb)
+			       struct key_value_info *kvi, void *cb)
 {
 	struct configset_add_data *data = cb;
-	configset_add_value(data->config_reader, data->config_set, key, value);
+	configset_add_value(kvi, data->config_reader, data->config_set, key, value);
 	return 0;
 }
 
@@ -4000,16 +4014,6 @@ const char *config_origin_type_name(enum config_origin_type type)
 	}
 }
 
-const char *current_config_origin_type(void)
-{
-	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
-
-	if (reader_origin_type(&the_reader, &type))
-		BUG("current_config_origin_type called outside config callback");
-
-	return config_origin_type_name(type);
-}
-
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4041,14 +4045,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	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)
diff --git a/config.h b/config.h
index ca5aac62980..fe7c1b79f80 100644
--- a/config.h
+++ b/config.h
@@ -367,9 +367,8 @@ void git_global_config(char **user, char **xdg);
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
 enum config_scope current_config_scope(void);
-const char *current_config_origin_type(void);
-const char *current_config_name(void);
 const char *config_origin_type_name(enum config_origin_type type);
+void kvi_from_param(struct key_value_info *out);
 
 /*
  * Match and parse a config key of the form:
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 57fe250b78f..028998dadc4 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1635,8 +1635,8 @@ test_expect_success 'urlmatch with --show-scope' '
 	EOF
 
 	cat >expect <<-EOF &&
-	unknown	http.cookiefile /tmp/cookie.txt
-	unknown	http.sslverify false
+	local	http.cookiefile /tmp/cookie.txt
+	local	http.sslverify false
 	EOF
 	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
 	test_cmp expect actual
@@ -2030,7 +2030,9 @@ test_expect_success '--show-origin blob ref' '
 '
 
 test_expect_success '--show-origin with --default' '
-	test_must_fail git config --show-origin --default foo some.key
+	git config --show-origin --default foo some.key >actual &&
+	echo "command line:	foo" >expect &&
+	test_cmp expect actual
 '
 
 test_expect_success '--show-scope with --list' '
@@ -2103,7 +2105,7 @@ test_expect_success '--show-scope with --show-origin' '
 
 test_expect_success '--show-scope with --default' '
 	git config --show-scope --default foo some.key >actual &&
-	echo "unknown	foo" >expect &&
+	echo "command	foo" >expect &&
 	test_cmp expect actual
 '
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 10/14] trace2: plumb config kvi
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (8 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 09/14] config.c: provide kvi with CLI config Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-01 23:38     ` Jonathan Tan
  2023-05-30 18:42   ` [PATCH v2 11/14] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
                     ` (4 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

There is a code path starting from trace2_def_param_fl() that eventually
calls current_config_scope(), and thus it needs to have "kvi" plumbed
through it. Additional plumbing is also needed to get "kvi" to
trace2_def_param_fl(), which gets called by two code paths:

- Through tr2_cfg_cb(), which is a config callback, so it trivially
  receives "kvi".

- Through tr2_list_env_vars_fl(), which is a high level function that
  lists environment variables for tracing. This has been secretly
  behaving like git_config_from_parameters() (in that it parses config
  from environment variables/the CLI), but does not set config source
  information.

  Teach tr2_list_env_vars_fl() to be well-behaved by using
  kvi_from_param(), which is used elsewhere for CLI/environment
  variable-based config.

As a result, current_config_scope() has no more callers, so remove it.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c                | 46 -----------------------------------------
 config.h                |  1 -
 trace2.c                |  4 ++--
 trace2.h                |  3 ++-
 trace2/tr2_cfg.c        |  9 +++++---
 trace2/tr2_tgt.h        |  4 +++-
 trace2/tr2_tgt_event.c  |  4 ++--
 trace2/tr2_tgt_normal.c |  4 ++--
 trace2/tr2_tgt_perf.c   |  4 ++--
 9 files changed, 19 insertions(+), 60 deletions(-)

diff --git a/config.c b/config.c
index 1779fe62f89..819e9e5966c 100644
--- a/config.c
+++ b/config.c
@@ -78,16 +78,6 @@ struct config_reader {
 	 */
 	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
@@ -118,19 +108,9 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 static inline void config_reader_set_kvi(struct config_reader *reader,
 					 struct key_value_info *kvi)
 {
-	if (kvi && (reader->source || reader->parsing_scope))
-		BUG("kvi should not be set while parsing a config source");
 	reader->config_kvi = kvi;
 }
 
-static inline void config_reader_set_scope(struct config_reader *reader,
-					   enum config_scope scope)
-{
-	if (scope && reader->config_kvi)
-		BUG("scope should only be set when iterating through a config source");
-	reader->parsing_scope = scope;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -404,18 +384,12 @@ 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, &opts);
-
-	config_reader_set_scope(inc->config_reader, store_scope);
 }
 
 static int forbid_remote_url(const char *var, const char *value UNUSED,
@@ -2242,7 +2216,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 	char *xdg_config = NULL;
 	char *user_config = NULL;
 	char *repo_config;
-	enum config_scope prev_parsing_scope = reader->parsing_scope;
 
 	if (opts->commondir)
 		repo_config = mkpathdup("%s/config", opts->commondir);
@@ -2251,7 +2224,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 	else
 		repo_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))
@@ -2259,7 +2231,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 							 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))
@@ -2270,13 +2241,11 @@ static int do_git_config_sequence(struct config_reader *reader,
 		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_with_options(fn, repo_config, data,
 							 CONFIG_SCOPE_LOCAL, NULL);
 
-	config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
 	if (!opts->ignore_worktree && repository_format_worktree_config) {
 		char *path = git_pathdup("config.worktree");
 		if (!access_or_die(path, R_OK, 0))
@@ -2286,11 +2255,9 @@ static int do_git_config_sequence(struct config_reader *reader,
 		free(path);
 	}
 
-	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);
@@ -2303,7 +2270,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) {
@@ -2316,9 +2282,6 @@ int config_with_options(config_fn_t fn, void *data,
 		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.
@@ -2342,7 +2305,6 @@ int config_with_options(config_fn_t fn, void *data,
 		string_list_clear(inc.remote_urls, 0);
 		FREE_AND_NULL(inc.remote_urls);
 	}
-	config_reader_set_scope(&the_reader, prev_scope);
 	return ret;
 }
 
@@ -4045,14 +4007,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	return 0;
 }
 
-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 lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index fe7c1b79f80..9e3c2bdb429 100644
--- a/config.h
+++ b/config.h
@@ -366,7 +366,6 @@ void git_global_config(char **user, char **xdg);
 
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
-enum config_scope current_config_scope(void);
 const char *config_origin_type_name(enum config_origin_type type);
 void kvi_from_param(struct key_value_info *out);
 
diff --git a/trace2.c b/trace2.c
index e8ba62c0c3d..f519a3514b6 100644
--- a/trace2.c
+++ b/trace2.c
@@ -632,7 +632,7 @@ void trace2_thread_exit_fl(const char *file, int line)
 }
 
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value)
+			 const char *value, struct key_value_info *kvi)
 {
 	struct tr2_tgt *tgt_j;
 	int j;
@@ -642,7 +642,7 @@ void trace2_def_param_fl(const char *file, int line, const char *param,
 
 	for_each_wanted_builtin (j, tgt_j)
 		if (tgt_j->pfn_param_fl)
-			tgt_j->pfn_param_fl(file, line, param, value);
+			tgt_j->pfn_param_fl(file, line, param, value, kvi);
 }
 
 void trace2_def_repo_fl(const char *file, int line, struct repository *repo)
diff --git a/trace2.h b/trace2.h
index 4ced30c0db3..af06f66739e 100644
--- a/trace2.h
+++ b/trace2.h
@@ -325,6 +325,7 @@ void trace2_thread_exit_fl(const char *file, int line);
 
 #define trace2_thread_exit() trace2_thread_exit_fl(__FILE__, __LINE__)
 
+struct key_value_info;
 /*
  * Emits a "def_param" message containing a key/value pair.
  *
@@ -334,7 +335,7 @@ void trace2_thread_exit_fl(const char *file, int line);
  * `core.abbrev`, `status.showUntrackedFiles`, or `--no-ahead-behind`.
  */
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value);
+			 const char *value, struct key_value_info *kvi);
 
 #define trace2_def_param(param, value) \
 	trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 8ed139c69f4..1450c9bec71 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -100,7 +100,7 @@ struct tr2_cfg_data {
  * See if the given config key matches any of our patterns of interest.
  */
 static int tr2_cfg_cb(const char *key, const char *value,
-		      struct key_value_info *kvi UNUSED, void *d)
+		      struct key_value_info *kvi, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
@@ -109,7 +109,8 @@ static int tr2_cfg_cb(const char *key, const char *value,
 		struct strbuf *buf = *s;
 		int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
 		if (wm == WM_MATCH) {
-			trace2_def_param_fl(data->file, data->line, key, value);
+			trace2_def_param_fl(data->file, data->line, key, value,
+					    kvi);
 			return 0;
 		}
 	}
@@ -127,8 +128,10 @@ void tr2_cfg_list_config_fl(const char *file, int line)
 
 void tr2_list_env_vars_fl(const char *file, int line)
 {
+	struct key_value_info kvi = { 0 };
 	struct strbuf **s;
 
+	kvi_from_param(&kvi);
 	if (tr2_load_env_vars() <= 0)
 		return;
 
@@ -136,7 +139,7 @@ void tr2_list_env_vars_fl(const char *file, int line)
 		struct strbuf *buf = *s;
 		const char *val = getenv(buf->buf);
 		if (val && *val)
-			trace2_def_param_fl(file, line, buf->buf, val);
+			trace2_def_param_fl(file, line, buf->buf, val, &kvi);
 	}
 }
 
diff --git a/trace2/tr2_tgt.h b/trace2/tr2_tgt.h
index bf8745c4f05..9c88ca9beed 100644
--- a/trace2/tr2_tgt.h
+++ b/trace2/tr2_tgt.h
@@ -69,8 +69,10 @@ typedef void(tr2_tgt_evt_exec_result_fl_t)(const char *file, int line,
 					   uint64_t us_elapsed_absolute,
 					   int exec_id, int code);
 
+struct key_value_info;
 typedef void(tr2_tgt_evt_param_fl_t)(const char *file, int line,
-				     const char *param, const char *value);
+				     const char *param, const char *value,
+				     struct key_value_info *kvi);
 
 typedef void(tr2_tgt_evt_repo_fl_t)(const char *file, int line,
 				    const struct repository *repo);
diff --git a/trace2/tr2_tgt_event.c b/trace2/tr2_tgt_event.c
index 9e7aab6d510..83db3c755bd 100644
--- a/trace2/tr2_tgt_event.c
+++ b/trace2/tr2_tgt_event.c
@@ -476,11 +476,11 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct json_writer jw = JSON_WRITER_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	jw_object_begin(&jw, 0);
diff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c
index 8672c2c2d04..65e9be9c5a4 100644
--- a/trace2/tr2_tgt_normal.c
+++ b/trace2/tr2_tgt_normal.c
@@ -296,10 +296,10 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, struct key_value_info *kvi)
 {
 	struct strbuf buf_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
diff --git a/trace2/tr2_tgt_perf.c b/trace2/tr2_tgt_perf.c
index 3f2b2d53118..f402f6e3813 100644
--- a/trace2/tr2_tgt_perf.c
+++ b/trace2/tr2_tgt_perf.c
@@ -438,12 +438,12 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct strbuf buf_payload = STRBUF_INIT;
 	struct strbuf scope_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "%s:%s", param, value);
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 11/14] config: pass kvi to die_bad_number()
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (9 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 10/14] trace2: plumb config kvi Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-01 23:48     ` Jonathan Tan
  2023-05-30 18:42   ` [PATCH v2 12/14] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
                     ` (3 subsequent siblings)
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Plumb "struct key_value_info" through all code paths that end in
die_bad_number(), which lets us remove the helper functions that read
analogous values from "struct config_reader". As a result, nothing reads
config_reader.config_kvi any more, so remove that too.

In config.c, this requires changing the signature of
git_configset_get_value() to 'return' "kvi" in an out parameter so that
git_configset_get_<type>() can pass it to git_config_<type>().

Outside of config.c, config callbacks now need to pass "kvi" to any of
the git_config_<type>() functions that parse a config string into a
number type. Included is a .cocci patch to make that refactor. In cases
where "kvi" would never be used, pass NULL, e.g.:

- In config.c, when we are parsing a boolean instead of a number
- In builtin/config.c, when calling normalize_value() before setting
  config to something the user gave us.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 archive-tar.c                              |   4 +-
 builtin/commit-graph.c                     |   4 +-
 builtin/commit.c                           |  10 +-
 builtin/config.c                           |  20 +--
 builtin/fetch.c                            |   4 +-
 builtin/fsmonitor--daemon.c                |   6 +-
 builtin/grep.c                             |   2 +-
 builtin/index-pack.c                       |   4 +-
 builtin/log.c                              |   2 +-
 builtin/pack-objects.c                     |  14 +-
 builtin/receive-pack.c                     |  10 +-
 builtin/submodule--helper.c                |   4 +-
 config.c                                   | 153 ++++++++-------------
 config.h                                   |  14 +-
 contrib/coccinelle/git_config_number.cocci |  27 ++++
 diff.c                                     |   9 +-
 fmt-merge-msg.c                            |   2 +-
 help.c                                     |   5 +-
 http.c                                     |  10 +-
 imap-send.c                                |   2 +-
 sequencer.c                                |  22 +--
 setup.c                                    |   2 +-
 submodule-config.c                         |  15 +-
 submodule-config.h                         |   3 +-
 t/helper/test-config.c                     |   6 +-
 upload-pack.c                              |  12 +-
 worktree.c                                 |   2 +-
 27 files changed, 182 insertions(+), 186 deletions(-)
 create mode 100644 contrib/coccinelle/git_config_number.cocci

diff --git a/archive-tar.c b/archive-tar.c
index dcfbce5225a..1cd6d72d21e 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -411,14 +411,14 @@ static int tar_filter_config(const char *var, const char *value,
 }
 
 static int git_tar_config(const char *var, const char *value,
-			  struct key_value_info *kvi UNUSED, void *cb)
+			  struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
 			tar_umask = umask(0);
 			umask(tar_umask);
 		} else {
-			tar_umask = git_config_int(var, value);
+			tar_umask = git_config_int(var, value, kvi);
 		}
 		return 0;
 	}
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index e811866b5dd..c99804abc7e 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -185,11 +185,11 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
-					 struct key_value_info *kvi UNUSED,
+					 struct key_value_info *kvi,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
-		write_opts.max_new_filters = git_config_int(var, value);
+		write_opts.max_new_filters = git_config_int(var, value, kvi);
 	/*
 	 * No need to fall-back to 'git_default_config', since this was already
 	 * called in 'cmd_commit_graph()'.
diff --git a/builtin/commit.c b/builtin/commit.c
index ec468e87039..e846355ec39 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1412,7 +1412,8 @@ static int git_status_config(const char *k, const char *v,
 		return git_column_config(k, v, "status", &s->colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
-		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
+		s->submodule_summary = git_config_bool_or_int(k, v, kvi,
+							      &is_bool);
 		if (is_bool && s->submodule_summary)
 			s->submodule_summary = -1;
 		return 0;
@@ -1472,11 +1473,11 @@ static int git_status_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "diff.renamelimit")) {
 		if (s->rename_limit == -1)
-			s->rename_limit = git_config_int(k, v);
+			s->rename_limit = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "status.renamelimit")) {
-		s->rename_limit = git_config_int(k, v);
+		s->rename_limit = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "diff.renames")) {
@@ -1622,7 +1623,8 @@ static int git_commit_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "commit.verbose")) {
 		int is_bool;
-		config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
+		config_commit_verbose = git_config_bool_or_int(k, v, kvi,
+							       &is_bool);
 		return 0;
 	}
 
diff --git a/builtin/config.c b/builtin/config.c
index 2fcad601a7b..0a33bea26c0 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -254,13 +254,14 @@ static int format_config(struct strbuf *buf, const char *key_,
 
 		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
@@ -414,7 +415,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 +431,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
@@ -876,7 +878,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], NULL);
 		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"
@@ -885,7 +887,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], NULL);
 		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
 							     argv[0], value, argv[2],
 							     flags);
@@ -893,7 +895,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], NULL);
 		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
 							     argv[0], value,
 							     CONFIG_REGEX_NONE,
@@ -902,7 +904,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], NULL);
 		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
 							     argv[0], value, argv[2],
 							     flags | CONFIG_FLAGS_MULTI_REPLACE);
diff --git a/builtin/fetch.c b/builtin/fetch.c
index aa688291613..04cf5518d2c 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -125,7 +125,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "submodule.fetchjobs")) {
-		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
+		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v, kvi);
 		return 0;
 	} else if (!strcmp(k, "fetch.recursesubmodules")) {
 		recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
@@ -133,7 +133,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "fetch.parallel")) {
-		fetch_parallel_config = git_config_int(k, v);
+		fetch_parallel_config = git_config_int(k, v, kvi);
 		if (fetch_parallel_config < 0)
 			die(_("fetch.parallel cannot be negative"));
 		if (!fetch_parallel_config)
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index a7375d61d02..cde4a575836 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -40,7 +40,7 @@ static int fsmonitor_config(const char *var, const char *value,
 			    struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, kvi);
 		if (i < 1)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__IPC_THREADS, i);
@@ -49,7 +49,7 @@ static int fsmonitor_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, FSMONITOR__START_TIMEOUT)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, kvi);
 		if (i < 0)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__START_TIMEOUT, i);
@@ -59,7 +59,7 @@ static int fsmonitor_config(const char *var, const char *value,
 
 	if (!strcmp(var, FSMONITOR__ANNOUNCE_STARTUP)) {
 		int is_bool;
-		int i = git_config_bool_or_int(var, value, &is_bool);
+		int i = git_config_bool_or_int(var, value, kvi, &is_bool);
 		if (i < 0)
 			return error(_("value of '%s' not bool or int: %d"),
 				     var, i);
diff --git a/builtin/grep.c b/builtin/grep.c
index 6e795f9f3a2..edb57f048ef 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -298,7 +298,7 @@ static int grep_cmd_config(const char *var, const char *value,
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
-		num_threads = git_config_int(var, value);
+		num_threads = git_config_int(var, value, kvi);
 		if (num_threads < 0)
 			die(_("invalid number of threads specified (%d) for %s"),
 			    num_threads, var);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 4450510ddfc..e7685fa9a6f 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1584,13 +1584,13 @@ static int git_index_pack_config(const char *k, const char *v,
 	struct pack_idx_option *opts = cb;
 
 	if (!strcmp(k, "pack.indexversion")) {
-		opts->version = git_config_int(k, v);
+		opts->version = git_config_int(k, v, kvi);
 		if (opts->version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32), opts->version);
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		nr_threads = git_config_int(k, v);
+		nr_threads = git_config_int(k, v, kvi);
 		if (nr_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    nr_threads);
diff --git a/builtin/log.c b/builtin/log.c
index f8e61330491..805320a1abf 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -569,7 +569,7 @@ static int git_log_config(const char *var, const char *value,
 	if (!strcmp(var, "format.subjectprefix"))
 		return git_config_string(&fmt_patch_subject_prefix, var, value);
 	if (!strcmp(var, "format.filenamemaxlength")) {
-		fmt_patch_name_max = git_config_int(var, value);
+		fmt_patch_name_max = git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp(var, "format.encodeemailheaders")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index ca023000cc0..cde11f83f81 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3138,23 +3138,23 @@ static int git_pack_config(const char *k, const char *v,
 			   struct key_value_info *kvi, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
-		window = git_config_int(k, v);
+		window = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.windowmemory")) {
-		window_memory_limit = git_config_ulong(k, v);
+		window_memory_limit = git_config_ulong(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.depth")) {
-		depth = git_config_int(k, v);
+		depth = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachesize")) {
-		max_delta_cache_size = git_config_int(k, v);
+		max_delta_cache_size = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachelimit")) {
-		cache_max_small_delta_size = git_config_int(k, v);
+		cache_max_small_delta_size = git_config_int(k, v, kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.writebitmaphashcache")) {
@@ -3180,7 +3180,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		delta_search_threads = git_config_int(k, v);
+		delta_search_threads = git_config_int(k, v, kvi);
 		if (delta_search_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    delta_search_threads);
@@ -3191,7 +3191,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.indexversion")) {
-		pack_idx_opts.version = git_config_int(k, v);
+		pack_idx_opts.version = git_config_int(k, v, kvi);
 		if (pack_idx_opts.version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32),
 			    pack_idx_opts.version);
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 2f5fd2abbc3..d2bc0fead9f 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -154,12 +154,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.unpacklimit") == 0) {
-		receive_unpack_limit = git_config_int(var, value);
+		receive_unpack_limit = git_config_int(var, value, kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "transfer.unpacklimit") == 0) {
-		transfer_unpack_limit = git_config_int(var, value);
+		transfer_unpack_limit = git_config_int(var, value, kvi);
 		return 0;
 	}
 
@@ -227,7 +227,7 @@ static int receive_pack_config(const char *var, const char *value,
 		return git_config_string(&cert_nonce_seed, var, value);
 
 	if (strcmp(var, "receive.certnonceslop") == 0) {
-		nonce_stamp_slop_limit = git_config_ulong(var, value);
+		nonce_stamp_slop_limit = git_config_ulong(var, value, kvi);
 		return 0;
 	}
 
@@ -242,12 +242,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.keepalive") == 0) {
-		keepalive_in_sec = git_config_int(var, value);
+		keepalive_in_sec = git_config_int(var, value, kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "receive.maxinputsize") == 0) {
-		max_input_size = git_config_int64(var, value);
+		max_input_size = git_config_int64(var, value, kvi);
 		return 0;
 	}
 
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 8570effbf0d..bda10764db5 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2187,13 +2187,13 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
-				   struct key_value_info *kvi UNUSED,
+				   struct key_value_info *kvi,
 				   void *cb)
 {
 	int *max_jobs = cb;
 
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, kvi);
 	return 0;
 }
 
diff --git a/config.c b/config.c
index 819e9e5966c..34a111b2e1c 100644
--- a/config.c
+++ b/config.c
@@ -66,18 +66,8 @@ struct config_reader {
 	 *
 	 * 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;
 };
 /*
  * Where possible, prefer to accept "struct config_reader" as an arg than to use
@@ -89,8 +79,6 @@ static struct config_reader the_reader;
 static inline void config_reader_push_source(struct config_reader *reader,
 					     struct config_source *top)
 {
-	if (reader->config_kvi)
-		BUG("source should not be set while iterating a config set");
 	top->prev = reader->source;
 	reader->source = top;
 }
@@ -105,12 +93,6 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 	return ret;
 }
 
-static inline void config_reader_set_kvi(struct config_reader *reader,
-					 struct key_value_info *kvi)
-{
-	reader->config_kvi = kvi;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -1330,80 +1312,74 @@ 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,
+			   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 (!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 || !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,
+		   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, 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,
+			       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,
+			   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;
 }
 
@@ -1508,7 +1484,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,
+			   struct key_value_info *kvi, int *is_bool)
 {
 	int v = git_parse_maybe_bool_text(value);
 	if (0 <= v) {
@@ -1516,7 +1493,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)
@@ -1642,7 +1619,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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, kvi);
 			if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz)
 				return error(_("abbrev length out of range: %d"), abbrev);
 			default_abbrev = abbrev;
@@ -1654,7 +1631,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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, kvi);
 		if (level == -1)
 			level = Z_DEFAULT_COMPRESSION;
 		else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -1665,7 +1642,7 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.compression")) {
-		int level = git_config_int(var, value);
+		int level = git_config_int(var, value, kvi);
 		if (level == -1)
 			level = Z_DEFAULT_COMPRESSION;
 		else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -1679,7 +1656,7 @@ static int git_default_core_config(const char *var, const char *value,
 
 	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, kvi);
 
 		/* This value must be multiple of (pagesize * 2) */
 		packed_git_window_size /= pgsz_x2;
@@ -1690,17 +1667,17 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.bigfilethreshold")) {
-		big_file_threshold = git_config_ulong(var, value);
+		big_file_threshold = git_config_ulong(var, value, kvi);
 		return 0;
 	}
 
 	if (!strcmp(var, "core.packedgitlimit")) {
-		packed_git_limit = git_config_ulong(var, value);
+		packed_git_limit = git_config_ulong(var, value, 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, kvi);
 		return 0;
 	}
 
@@ -1984,12 +1961,12 @@ int git_default_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "pack.packsizelimit")) {
-		pack_size_limit_cfg = git_config_ulong(var, value);
+		pack_size_limit_cfg = git_config_ulong(var, value, kvi);
 		return 0;
 	}
 
 	if (!strcmp(var, "pack.compression")) {
-		int level = git_config_int(var, value);
+		int level = git_config_int(var, value, kvi);
 		if (level == -1)
 			level = Z_DEFAULT_COMPRESSION;
 		else if (level < 0 || level > Z_BEST_COMPRESSION)
@@ -2323,11 +2300,8 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 		values = &entry->value_list;
 		kvi = values->items[value_index].util;
 
-		config_reader_set_kvi(reader, values->items[value_index].util);
-
 		if (fn(entry->key, values->items[value_index].string, kvi, data) < 0)
 			git_die_config_linenr(entry->key, kvi->filename, kvi->linenr);
-		config_reader_set_kvi(reader, NULL);
 	}
 }
 
@@ -2509,11 +2483,12 @@ int git_configset_add_file(struct config_set *set, const char *filename)
 	return git_config_from_file(config_set_callback, filename, &data);
 }
 
-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
@@ -2523,7 +2498,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;
 }
 
@@ -2576,7 +2554,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;
@@ -2586,7 +2564,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;
@@ -2599,8 +2577,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;
@@ -2609,8 +2589,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;
@@ -2619,7 +2601,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
@@ -2630,8 +2612,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;
@@ -2640,7 +2624,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;
@@ -2652,7 +2636,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;
@@ -2722,7 +2706,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,
@@ -3946,18 +3930,6 @@ 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 *config_origin_type_name(enum config_origin_type type)
 {
 	switch (type) {
@@ -3996,17 +3968,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;
-}
-
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index 9e3c2bdb429..0fe56a4d650 100644
--- a/config.h
+++ b/config.h
@@ -229,22 +229,23 @@ int git_parse_maybe_bool(const char *);
  * Parse the string to an integer, including unit factors. Dies on error;
  * otherwise, returns the parsed result.
  */
-int git_config_int(const char *, const char *);
+int git_config_int(const char *, const char *, struct key_value_info *);
 
-int64_t git_config_int64(const char *, const char *);
+int64_t git_config_int64(const char *, const char *, struct key_value_info *);
 
 /**
  * Identical to `git_config_int`, but for unsigned longs.
  */
-unsigned long git_config_ulong(const char *, const char *);
+unsigned long git_config_ulong(const char *, const char *, struct key_value_info *);
 
-ssize_t git_config_ssize_t(const char *, const char *);
+ssize_t git_config_ssize_t(const char *, const char *, struct key_value_info *);
 
 /**
  * Same as `git_config_bool`, except that integers are returned as-is, and
  * an `is_bool` flag is unset.
  */
-int git_config_bool_or_int(const char *, const char *, int *);
+int git_config_bool_or_int(const char *, const char *, struct key_value_info *,
+			   int *);
 
 /**
  * Parse a string into a boolean value, respecting keywords like "true" and
@@ -509,7 +510,8 @@ int git_configset_get(struct config_set *cs, const char *key);
  * touching `value`. The caller should not free or modify `value`, as it
  * is owned by the cache.
  */
-int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
+int git_configset_get_value(struct config_set *cs, const char *key,
+			    const char **dest, struct key_value_info *kvi);
 
 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
diff --git a/contrib/coccinelle/git_config_number.cocci b/contrib/coccinelle/git_config_number.cocci
new file mode 100644
index 00000000000..f46c74dd23c
--- /dev/null
+++ b/contrib/coccinelle/git_config_number.cocci
@@ -0,0 +1,27 @@
+@@
+identifier C1, C2, C3;
+@@
+(
+(
+git_config_int
+|
+git_config_int64
+|
+git_config_ulong
+|
+git_config_ssize_t
+)
+  (C1, C2
++ , kvi
+  )
+|
+(
+git_configset_get_value
+|
+git_config_bool_or_int
+)
+  (C1, C2,
++ kvi,
+  C3
+  )
+)
diff --git a/diff.c b/diff.c
index d7ed2dc900b..da7cd353a6d 100644
--- a/diff.c
+++ b/diff.c
@@ -372,13 +372,14 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.context")) {
-		diff_context_default = git_config_int(var, value);
+		diff_context_default = git_config_int(var, value, kvi);
 		if (diff_context_default < 0)
 			return -1;
 		return 0;
 	}
 	if (!strcmp(var, "diff.interhunkcontext")) {
-		diff_interhunk_context_default = git_config_int(var, value);
+		diff_interhunk_context_default = git_config_int(var, value,
+								kvi);
 		if (diff_interhunk_context_default < 0)
 			return -1;
 		return 0;
@@ -404,7 +405,7 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.statgraphwidth")) {
-		diff_stat_graph_width = git_config_int(var, value);
+		diff_stat_graph_width = git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp(var, "diff.external"))
@@ -443,7 +444,7 @@ int git_diff_basic_config(const char *var, const char *value,
 	const char *name;
 
 	if (!strcmp(var, "diff.renamelimit")) {
-		diff_rename_limit_default = git_config_int(var, value);
+		diff_rename_limit_default = git_config_int(var, value, kvi);
 		return 0;
 	}
 
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 97358034fa0..d1b59af44bb 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -24,7 +24,7 @@ int fmt_merge_msg_config(const char *key, const char *value,
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
-		merge_log_config = git_config_bool_or_int(key, value, &is_bool);
+		merge_log_config = git_config_bool_or_int(key, value, kvi, &is_bool);
 		if (!is_bool && merge_log_config < 0)
 			return error("%s: negative length %s", key, value);
 		if (is_bool && merge_log_config)
diff --git a/help.c b/help.c
index 43d1eb702cd..08f0b953736 100644
--- a/help.c
+++ b/help.c
@@ -545,8 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
-				  struct key_value_info *kvi UNUSED,
-				  void *cb UNUSED)
+				  struct key_value_info *kvi, void *cb UNUSED)
 {
 	const char *p;
 
@@ -560,7 +559,7 @@ static int git_unknown_cmd_config(const char *var, const char *value,
 		} else if (!strcmp(value, "prompt")) {
 			autocorrect = AUTOCORRECT_PROMPT;
 		} else {
-			int v = git_config_int(var, value);
+			int v = git_config_int(var, value, kvi);
 			autocorrect = (v < 0)
 				? AUTOCORRECT_IMMEDIATELY : v;
 		}
diff --git a/http.c b/http.c
index 3d4292eba6a..a26c3dff827 100644
--- a/http.c
+++ b/http.c
@@ -412,21 +412,21 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.minsessions", var)) {
-		min_curl_sessions = git_config_int(var, value);
+		min_curl_sessions = git_config_int(var, value, kvi);
 		if (min_curl_sessions > 1)
 			min_curl_sessions = 1;
 		return 0;
 	}
 	if (!strcmp("http.maxrequests", var)) {
-		max_requests = git_config_int(var, value);
+		max_requests = git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedlimit", var)) {
-		curl_low_speed_limit = (long)git_config_int(var, value);
+		curl_low_speed_limit = (long)git_config_int(var, value, kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedtime", var)) {
-		curl_low_speed_time = (long)git_config_int(var, value);
+		curl_low_speed_time = (long)git_config_int(var, value, kvi);
 		return 0;
 	}
 
@@ -462,7 +462,7 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.postbuffer", var)) {
-		http_post_buffer = git_config_ssize_t(var, value);
+		http_post_buffer = git_config_ssize_t(var, value, kvi);
 		if (http_post_buffer < 0)
 			warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
 		if (http_post_buffer < LARGE_PACKET_MAX)
diff --git a/imap-send.c b/imap-send.c
index 3cc98f1a0a5..3c391a52c5a 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1341,7 +1341,7 @@ static int git_imap_config(const char *var, const char *val,
 	else if (!strcmp("imap.authmethod", var))
 		return git_config_string(&server.auth_method, var, val);
 	else if (!strcmp("imap.port", var))
-		server.port = git_config_int(var, val);
+		server.port = git_config_int(var, val, kvi);
 	else if (!strcmp("imap.host", var)) {
 		if (!val) {
 			git_die_config("imap.host", "Missing value for 'imap.host'");
diff --git a/sequencer.c b/sequencer.c
index 171561c2cdb..76b4750b4bd 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2878,7 +2878,7 @@ static int git_config_string_dup(char **dest,
 }
 
 static int populate_opts_cb(const char *key, const char *value,
-			    struct key_value_info *kvi UNUSED, void *data)
+			    struct key_value_info *kvi, void *data)
 {
 	struct replay_opts *opts = data;
 	int error_flag = 1;
@@ -2886,26 +2886,26 @@ static int populate_opts_cb(const char *key, const char *value,
 	if (!value)
 		error_flag = 0;
 	else if (!strcmp(key, "options.no-commit"))
-		opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
+		opts->no_commit = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.edit"))
-		opts->edit = git_config_bool_or_int(key, value, &error_flag);
+		opts->edit = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty"))
 		opts->allow_empty =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty-message"))
 		opts->allow_empty_message =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.keep-redundant-commits"))
 		opts->keep_redundant_commits =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.signoff"))
-		opts->signoff = git_config_bool_or_int(key, value, &error_flag);
+		opts->signoff = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.record-origin"))
-		opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
+		opts->record_origin = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-ff"))
-		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
+		opts->allow_ff = git_config_bool_or_int(key, value, kvi, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
-		opts->mainline = git_config_int(key, value);
+		opts->mainline = git_config_int(key, value, kvi);
 	else if (!strcmp(key, "options.strategy"))
 		git_config_string_dup(&opts->strategy, key, value);
 	else if (!strcmp(key, "options.gpg-sign"))
@@ -2915,7 +2915,7 @@ static int populate_opts_cb(const char *key, const char *value,
 		opts->xopts[opts->xopts_nr++] = xstrdup(value);
 	} else if (!strcmp(key, "options.allow-rerere-auto"))
 		opts->allow_rerere_auto =
-			git_config_bool_or_int(key, value, &error_flag) ?
+			git_config_bool_or_int(key, value, kvi, &error_flag) ?
 				RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
 	else if (!strcmp(key, "options.default-msg-cleanup")) {
 		opts->explicit_cleanup = 1;
diff --git a/setup.c b/setup.c
index a461dd15233..75934d7438f 100644
--- a/setup.c
+++ b/setup.c
@@ -594,7 +594,7 @@ static int check_repo_format(const char *var, const char *value,
 	const char *ext;
 
 	if (strcmp(var, "core.repositoryformatversion") == 0)
-		data->version = git_config_int(var, value);
+		data->version = git_config_int(var, value, kvi);
 	else if (skip_prefix(var, "extensions.", &ext)) {
 		switch (handle_extension_v0(var, value, ext, data)) {
 		case EXTENSION_ERROR:
diff --git a/submodule-config.c b/submodule-config.c
index 7d773f33621..b86547fd1ee 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -302,9 +302,10 @@ static int parse_fetch_recurse(const char *opt, const char *arg,
 	}
 }
 
-int parse_submodule_fetchjobs(const char *var, const char *value)
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      struct key_value_info *kvi)
 {
-	int fetchjobs = git_config_int(var, value);
+	int fetchjobs = git_config_int(var, value, kvi);
 	if (fetchjobs < 0)
 		die(_("negative values not allowed for submodule.fetchJobs"));
 	if (!fetchjobs)
@@ -848,14 +849,13 @@ struct fetch_config {
 };
 
 static int gitmodules_fetch_config(const char *var, const char *value,
-				   struct key_value_info *kvi UNUSED,
-				   void *cb)
+				   struct key_value_info *kvi, void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
 		if (config->max_children)
 			*(config->max_children) =
-				parse_submodule_fetchjobs(var, value);
+				parse_submodule_fetchjobs(var, value, kvi);
 		return 0;
 	} else if (!strcmp(var, "fetch.recursesubmodules")) {
 		if (config->recurse_submodules)
@@ -877,12 +877,11 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
-					  struct key_value_info *kvi UNUSED,
-					  void *cb)
+					  struct key_value_info *kvi, void *cb)
 {
 	int *max_jobs = cb;
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, kvi);
 	return 0;
 }
 
diff --git a/submodule-config.h b/submodule-config.h
index c2045875bbb..944cae75cc9 100644
--- a/submodule-config.h
+++ b/submodule-config.h
@@ -50,7 +50,8 @@ struct repository;
 
 void submodule_cache_free(struct submodule_cache *cache);
 
-int parse_submodule_fetchjobs(const char *var, const char *value);
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      struct key_value_info *kvi);
 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 struct option;
 int option_fetch_parse_recurse_submodules(const struct option *opt,
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 7027ffa187f..737505583d4 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -61,12 +61,12 @@ static int iterate_cb(const char *var, const char *value,
 }
 
 static int parse_int_cb(const char *var, const char *value,
-			struct key_value_info *kvi UNUSED, void *data)
+			struct key_value_info *kvi, void *data)
 {
 	const char *key_to_match = data;
 
 	if (!strcmp(key_to_match, var)) {
-		int parsed = git_config_int(value, value);
+		int parsed = git_config_int(value, value, kvi);
 		printf("%d\n", parsed);
 	}
 	return 0;
@@ -179,7 +179,7 @@ int cmd__config(int argc, const char **argv)
 				goto exit2;
 			}
 		}
-		if (!git_configset_get_value(&cs, argv[2], &v)) {
+		if (!git_configset_get_value(&cs, argv[2], &v, NULL)) {
 			if (!v)
 				printf("(NULL)\n");
 			else
diff --git a/upload-pack.c b/upload-pack.c
index 5f8232ff078..7cf776cde91 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1264,7 +1264,8 @@ static int find_symref(const char *refname,
 }
 
 static int parse_object_filter_config(const char *var, const char *value,
-				       struct upload_pack_data *data)
+				      struct key_value_info *kvi,
+				      struct upload_pack_data *data)
 {
 	struct strbuf buf = STRBUF_INIT;
 	const char *sub, *key;
@@ -1291,7 +1292,8 @@ static int parse_object_filter_config(const char *var, const char *value,
 		}
 		string_list_insert(&data->allowed_filters, buf.buf)->util =
 			(void *)(intptr_t)1;
-		data->tree_filter_max_depth = git_config_ulong(var, value);
+		data->tree_filter_max_depth = git_config_ulong(var, value,
+							       kvi);
 	}
 
 	strbuf_release(&buf);
@@ -1299,7 +1301,7 @@ static int parse_object_filter_config(const char *var, const char *value,
 }
 
 static int upload_pack_config(const char *var, const char *value,
-			      struct key_value_info *kvi UNUSED,
+			      struct key_value_info *kvi,
 			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
@@ -1320,7 +1322,7 @@ static int upload_pack_config(const char *var, const char *value,
 		else
 			data->allow_uor &= ~ALLOW_ANY_SHA1;
 	} else if (!strcmp("uploadpack.keepalive", var)) {
-		data->keepalive = git_config_int(var, value);
+		data->keepalive = git_config_int(var, value, kvi);
 		if (!data->keepalive)
 			data->keepalive = -1;
 	} else if (!strcmp("uploadpack.allowfilter", var)) {
@@ -1335,7 +1337,7 @@ static int upload_pack_config(const char *var, const char *value,
 		data->advertise_sid = git_config_bool(var, value);
 	}
 
-	if (parse_object_filter_config(var, value, data) < 0)
+	if (parse_object_filter_config(var, value, kvi, data) < 0)
 		return -1;
 
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
diff --git a/worktree.c b/worktree.c
index b5ee71c5ebd..1fbdbd745fb 100644
--- a/worktree.c
+++ b/worktree.c
@@ -835,7 +835,7 @@ int init_worktree_config(struct repository *r)
 	 * Relocate that value to avoid breaking all worktrees with this
 	 * upgrade to worktree config.
 	 */
-	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
+	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
 		if ((res = move_config_setting("core.worktree", core_worktree,
 					       common_config_file,
 					       main_worktree_file)))
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 12/14] config.c: remove config_reader from configsets
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (10 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 11/14] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-05-30 18:42   ` [PATCH v2 13/14] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
                     ` (2 subsequent siblings)
  14 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Remove the last usage of "struct config_reader" from configsets by
copying the "kvi" arg instead of recomputing "kvi" from
config_reader.source. Since we no longer need to pass both "struct
config_reader" and "struct config_set" in a single "void *cb", remove
"struct configset_add_data" too.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 41 ++++++++++-------------------------------
 1 file changed, 10 insertions(+), 31 deletions(-)

diff --git a/config.c b/config.c
index 34a111b2e1c..4d381f93bcc 100644
--- a/config.c
+++ b/config.c
@@ -2285,8 +2285,7 @@ int config_with_options(config_fn_t fn, void *data,
 	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;
@@ -2378,7 +2377,6 @@ static int configset_find_element(struct config_set *set, const char *key,
 }
 
 static int configset_add_value(struct key_value_info *kvi_p,
-			       struct config_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2409,11 +2407,7 @@ static int configset_add_value(struct key_value_info *kvi_p,
 	l_item->e = e;
 	l_item->value_index = e->value_list.nr - 1;
 
-	if (reader->source->name) {
-		kvi_from_source(reader->source, kvi_p->scope, kv_info);
-	} else {
-		kvi_from_param(kv_info);
-	}
+	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
 	si->util = kv_info;
 
 	return 0;
@@ -2461,26 +2455,17 @@ 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,
 			       struct key_value_info *kvi, void *cb)
 {
-	struct configset_add_data *data = cb;
-	configset_add_value(kvi, data->config_reader, data->config_set, key, value);
+	struct config_set *set = cb;
+	configset_add_value(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,
@@ -2646,7 +2631,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;
@@ -2658,10 +2642,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, &opts) < 0)
+	if (config_with_options(config_set_callback, repo->config, NULL,
+				&opts) < 0)
 		/*
 		 * config_with_options() normally returns only
 		 * zero, as most errors are fatal, and
@@ -2693,7 +2675,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)
@@ -2800,19 +2782,16 @@ 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, &opts);
+	config_with_options(config_set_callback, &protected_config, 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' */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 13/14] config: add kvi.path, use it to evaluate includes
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (11 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 12/14] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-02  0:06     ` Jonathan Tan
  2023-05-30 18:42   ` [PATCH v2 14/14] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Include directives are evaluated using the path of the config file. To
reduce the dependence on "config_reader.source", add a new
"key_value_info.path" member and use that instead of
"config_source.path".

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 41 +++++++++++++++++++----------------------
 config.h |  1 +
 2 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/config.c b/config.c
index 4d381f93bcc..09950b46127 100644
--- a/config.c
+++ b/config.c
@@ -154,7 +154,6 @@ struct config_include_data {
 	void *data;
 	const struct config_options *opts;
 	struct git_config_source *config_source;
-	struct config_reader *config_reader;
 
 	/*
 	 * All remote URLs discovered when reading all config files.
@@ -173,9 +172,7 @@ 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,
-			       struct key_value_info *kvi,
-			       const char *path,
+static int handle_path_include(struct key_value_info *kvi, const char *path,
 			       struct config_include_data *inc)
 {
 	int ret = 0;
@@ -197,14 +194,14 @@ static int handle_path_include(struct config_source *cs,
 	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;
 	}
@@ -212,8 +209,8 @@ static int handle_path_include(struct config_source *cs,
 	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_with_options(git_config_include, path, inc,
 							kvi->scope, NULL);
@@ -231,7 +228,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(struct key_value_info *kvi,
 					     struct strbuf *pat)
 {
 	struct strbuf path = STRBUF_INIT;
@@ -248,11 +245,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?");
@@ -267,7 +264,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(struct key_value_info *kvi,
 			     const struct config_options *opts,
 			     const char *cond, size_t cond_len, int icase)
 {
@@ -284,7 +281,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)
@@ -419,16 +416,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(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,
@@ -443,7 +440,6 @@ static int git_config_include(const char *var, const char *value,
 			      struct key_value_info *kvi, 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;
@@ -457,16 +453,16 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, kvi, value, inc);
+		ret = handle_path_include(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(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, kvi, value, inc);
+		ret = handle_path_include(kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -648,6 +644,7 @@ void kvi_from_param(struct key_value_info *out)
 	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,
@@ -1048,6 +1045,7 @@ static void kvi_from_source(struct config_source *cs,
 	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,
@@ -2254,7 +2252,6 @@ int config_with_options(config_fn_t fn, void *data,
 		inc.data = data;
 		inc.opts = opts;
 		inc.config_source = config_source;
-		inc.config_reader = &the_reader;
 		fn = git_config_include;
 		data = &inc;
 	}
diff --git a/config.h b/config.h
index 0fe56a4d650..123917d1b0a 100644
--- a/config.h
+++ b/config.h
@@ -116,6 +116,7 @@ struct key_value_info {
 	int linenr;
 	enum config_origin_type origin_type;
 	enum config_scope scope;
+	const char *path;
 };
 
 /**
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v2 14/14] config: pass source to config_parser_event_fn_t
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (12 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 13/14] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
@ 2023-05-30 18:42   ` Glen Choo via GitGitGadget
  2023-06-02  0:08     ` Jonathan Tan
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
  14 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-05-30 18:42 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

..so that the callback can use a "struct config_source" parameter
instead of "config_reader.source". "struct config_source" is internal to
config.c, but this refactor is okay because this function has only ever
been (and probably ever will be) used internally by config.c.

As a result, the_reader isn't used anywhere, so "struct config_reader"
is obsolete (it was only intended to be used with the_reader). Remove
them.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 77 ++++++++++----------------------------------------------
 config.h |  6 +++++
 2 files changed, 19 insertions(+), 64 deletions(-)

diff --git a/config.c b/config.c
index 09950b46127..c8d941ca627 100644
--- a/config.c
+++ b/config.c
@@ -59,40 +59,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).
-	 */
-	struct config_source *source;
-};
-/*
- * 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 int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -738,14 +704,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;
 	struct key_value_info kvi = { 0 };
 
-	source.origin_type = CONFIG_ORIGIN_CMDLINE;
-	config_reader_push_source(&the_reader, &source);
-
 	kvi_from_param(&kvi);
-
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -802,7 +763,6 @@ out:
 	strbuf_release(&envvar);
 	strvec_clear(&to_free);
 	free(envw);
-	config_reader_pop_source(&the_reader);
 	return ret;
 }
 
@@ -1028,7 +988,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;
@@ -1986,8 +1946,7 @@ int git_default_config(const char *var, const char *value,
  * 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,
+static int do_config_from(struct config_source *top, config_fn_t fn,
 			  void *data, enum config_scope scope,
 			  const struct config_options *opts)
 {
@@ -2000,21 +1959,17 @@ 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, &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, enum config_scope scope,
@@ -2033,7 +1988,7 @@ 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, scope, opts);
+	ret = do_config_from(&top, fn, data, scope, opts);
 	funlockfile(f);
 	return ret;
 }
@@ -2041,8 +1996,8 @@ static int do_config_from_file(struct config_reader *reader,
 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, scope, 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,
@@ -2056,9 +2011,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, scope,
-					  opts);
+		ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
+					  filename, f, data, scope, opts);
 		fclose(f);
 	}
 	return ret;
@@ -2089,7 +2043,7 @@ 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, scope, opts);
+	return do_config_from(&top, fn, data, scope, opts);
 }
 
 int git_config_from_blob_oid(config_fn_t fn,
@@ -2182,8 +2136,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,
 				  config_fn_t fn, void *data)
 {
 	int ret = 0;
@@ -2272,7 +2225,7 @@ int config_with_options(config_fn_t fn, void *data,
 		ret = git_config_from_blob_ref(fn, repo, config_source->blob,
 					       data, config_source->scope);
 	} else {
-		ret = do_git_config_sequence(&the_reader, opts, fn, data);
+		ret = do_git_config_sequence(opts, fn, data);
 	}
 
 	if (inc.remote_urls) {
@@ -2978,7 +2931,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;
@@ -3024,11 +2976,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;
@@ -3349,8 +3300,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)
diff --git a/config.h b/config.h
index 123917d1b0a..eaf24aba25d 100644
--- a/config.h
+++ b/config.h
@@ -73,6 +73,7 @@ enum config_event_t {
 	CONFIG_EVENT_ERROR
 };
 
+struct config_source;
 /*
  * The parser event function (if not NULL) is called with the event type and
  * the begin/end offsets of the parsed elements.
@@ -82,6 +83,7 @@ enum config_event_t {
  */
 typedef int (*config_parser_event_fn_t)(enum config_event_t type,
 					size_t begin_offset, size_t end_offset,
+					struct config_source *cs,
 					void *event_fn_data);
 
 struct config_options {
@@ -101,6 +103,10 @@ struct config_options {
 
 	const char *commondir;
 	const char *git_dir;
+	/*
+	 * event_fn and event_fn_data are for internal use only. Handles events
+	 * emitted by the config parser.
+	 */
 	config_parser_event_fn_t event_fn;
 	void *event_fn_data;
 	enum config_error_action {
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-05-30 18:42   ` [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t Glen Choo via GitGitGadget
@ 2023-06-01  9:50     ` Phillip Wood
  2023-06-01 16:22       ` Glen Choo
  0 siblings, 1 reply; 115+ messages in thread
From: Phillip Wood @ 2023-06-01  9:50 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget, git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

Hi Glen

On 30/05/2023 19:42, Glen Choo via GitGitGadget wrote:
> From: Glen Choo <chooglen@google.com>
> 
> ..without actually changing any of its implementations. This commit does
> not build - I've split this out for readability, but post-RFC I will
> squash this with the rest of the refactor + cocci changes.

While this ends up with a huge amount of churn, I think that is probably 
inevitable if we're going to get rid of global state (I see Ævar 
suggested adding a separate set of callbacks but I'm not sure how that 
would work with chaining up to git_default_config() and it would be nice 
to improve our error messages with filename and line information). I've 
not been following this thread all that closely but a couple of thoughts 
crossed by mind.

  - is it worth making struct key_value_info opaque and provide getters
    for the fields so we can change the implementation in the future
    without having to modify every user. We could rename it
    config_context or something generic like that if we think it might
    grow in scope in the future.

  - (probably impractical) could we stuff the key and value into struct
    key_value_info so config_fn_t becomes
    fn(const struct key_value_info, void *data)
    that would get rid of all the UNUSED annotations but would mean even
    more churn. The advantage is that one could add functions like
    kvi_bool_or_int(kvi, &is_bool) and get good error messages because
    all the config parsing functions would all have access to location
    information.

Best Wishes

Phillip

> Signed-off-by: Glen Choo <chooglen@google.com>
> ---
>   config.c                                      |   8 +-
>   config.h                                      |  16 +-
>   .../coccinelle/config_fn_kvi.pending.cocci    | 146 ++++++++++++++++++
>   3 files changed, 158 insertions(+), 12 deletions(-)
>   create mode 100644 contrib/coccinelle/config_fn_kvi.pending.cocci
> 
> diff --git a/config.c b/config.c
> index 493f47df8ae..945f4f3b77e 100644
> --- a/config.c
> +++ b/config.c
> @@ -489,7 +489,7 @@ 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, NULL, inc->data);
>   	if (ret < 0)
>   		return ret;
>   
> @@ -671,7 +671,7 @@ static int config_parse_pair(const char *key, const char *value,
>   	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, NULL, data) < 0) ? -1 : 0;
>   	free(canonical_name);
>   	return ret;
>   }
> @@ -959,7 +959,7 @@ 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);
> +	ret = fn(name->buf, value, NULL, data);
>   	if (ret >= 0)
>   		cs->linenr++;
>   	return ret;
> @@ -2303,7 +2303,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
>   
>   		config_reader_set_kvi(reader, values->items[value_index].util);
>   
> -		if (fn(entry->key, values->items[value_index].string, data) < 0)
> +		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
>   			git_die_config_linenr(entry->key,
>   					      reader->config_kvi->filename,
>   					      reader->config_kvi->linenr);
> diff --git a/config.h b/config.h
> index 247b572b37b..9d052c52c3c 100644
> --- a/config.h
> +++ b/config.h
> @@ -111,6 +111,13 @@ struct config_options {
>   	} error_action;
>   };
>   
> +struct key_value_info {
> +	const char *filename;
> +	int linenr;
> +	enum config_origin_type origin_type;
> +	enum config_scope scope;
> +};
> +
>   /**
>    * A config callback function takes three parameters:
>    *
> @@ -129,7 +136,7 @@ struct config_options {
>    * A config callback should return 0 for success, or -1 if the variable
>    * could not be parsed properly.
>    */
> -typedef int (*config_fn_t)(const char *, const char *, void *);
> +typedef int (*config_fn_t)(const char *, const char *, struct key_value_info *, void *);
>   
>   int git_default_config(const char *, const char *, void *);
>   
> @@ -667,13 +674,6 @@ int git_config_get_expiry(const char *key, const char **output);
>   /* parse either "this many days" integer, or "5.days.ago" approxidate */
>   int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
>   
> -struct key_value_info {
> -	const char *filename;
> -	int linenr;
> -	enum config_origin_type origin_type;
> -	enum config_scope scope;
> -};
> -
>   /**
>    * First prints the error message specified by the caller in `err` and then
>    * dies printing the line number and the file name of the highest priority
> diff --git a/contrib/coccinelle/config_fn_kvi.pending.cocci b/contrib/coccinelle/config_fn_kvi.pending.cocci
> new file mode 100644
> index 00000000000..d4c84599afa
> --- /dev/null
> +++ b/contrib/coccinelle/config_fn_kvi.pending.cocci
> @@ -0,0 +1,146 @@
> +// These are safe to apply to *.c *.h builtin/*.c
> +
> +@ get_fn @
> +identifier fn, R;
> +@@
> +(
> +(
> +git_config_from_file
> +|
> +git_config_from_file_with_options
> +|
> +git_config_from_mem
> +|
> +git_config_from_blob_oid
> +|
> +read_early_config
> +|
> +read_very_early_config
> +|
> +config_with_options
> +|
> +git_config
> +|
> +git_protected_config
> +|
> +config_from_gitmodules
> +)
> +  (fn, ...)
> +|
> +repo_config(R, fn, ...)
> +)
> +
> +@ extends get_fn @
> +identifier C1, C2, D;
> +@@
> +int fn(const char *C1, const char *C2,
> ++  struct key_value_info *kvi,
> +  void *D);
> +
> +@ extends get_fn @
> +@@
> +int fn(const char *, const char *,
> ++  struct key_value_info *,
> +  void *);
> +
> +@ extends get_fn @
> +// Don't change fns that look like callback fns but aren't
> +identifier fn2 != tar_filter_config && != git_diff_heuristic_config &&
> +  != git_default_submodule_config && != git_color_config &&
> +  != bundle_list_update && != parse_object_filter_config;
> +identifier C1, C2, D1, D2, S;
> +attribute name UNUSED;
> +@@
> +int fn(const char *C1, const char *C2,
> ++  struct key_value_info *kvi,
> +  void *D1) {
> +<+...
> +(
> +fn2(C1, C2,
> ++ kvi,
> +D2);
> +|
> +if(fn2(C1, C2,
> ++ kvi,
> +D2) < 0) { ... }
> +|
> +return fn2(C1, C2,
> ++ kvi,
> +D2);
> +|
> +S = fn2(C1, C2,
> ++ kvi,
> +D2);
> +)
> +...+>
> +  }
> +
> +@ extends get_fn@
> +identifier C1, C2, D;
> +attribute name UNUSED;
> +@@
> +int fn(const char *C1, const char *C2,
> ++  struct key_value_info *kvi UNUSED,
> +  void *D) {...}
> +
> +
> +// The previous rules don't catch all callbacks, especially if they're defined
> +// in a separate file from the git_config() call. Fix these manually.
> +@@
> +identifier C1, C2, D;
> +attribute name UNUSED;
> +@@
> +int
> +(
> +git_ident_config
> +|
> +urlmatch_collect_fn
> +|
> +write_one_config
> +|
> +forbid_remote_url
> +|
> +credential_config_callback
> +)
> +  (const char *C1, const char *C2,
> ++  struct key_value_info *kvi UNUSED,
> +  void *D) {...}
> +
> +@@
> +identifier C1, C2, D, D2, S, fn2;
> +@@
> +int
> +(
> +http_options
> +|
> +git_status_config
> +|
> +git_commit_config
> +|
> +git_default_core_config
> +|
> +grep_config
> +)
> +  (const char *C1, const char *C2,
> ++  struct key_value_info *kvi,
> +  void *D) {
> +<+...
> +(
> +fn2(C1, C2,
> ++ kvi,
> +D2);
> +|
> +if(fn2(C1, C2,
> ++ kvi,
> +D2) < 0) { ... }
> +|
> +return fn2(C1, C2,
> ++ kvi,
> +D2);
> +|
> +S = fn2(C1, C2,
> ++ kvi,
> +D2);
> +)
> +...+>
> +  }

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-06-01  9:50     ` Phillip Wood
@ 2023-06-01 16:22       ` Glen Choo
  2023-06-02  9:54         ` Phillip Wood
  0 siblings, 1 reply; 115+ messages in thread
From: Glen Choo @ 2023-06-01 16:22 UTC (permalink / raw)
  To: phillip.wood, Glen Choo via GitGitGadget, git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

Phillip Wood <phillip.wood123@gmail.com> writes:

>   - is it worth making struct key_value_info opaque and provide getters
>     for the fields so we can change the implementation in the future
>     without having to modify every user. We could rename it
>     config_context or something generic like that if we think it might
>     grow in scope in the future.

Yes! I planned to do the key_value_info -> config_context conversion
when I send the first non-RFC version for this exact reason.

>   - (probably impractical) could we stuff the key and value into struct
>     key_value_info so config_fn_t becomes
>     fn(const struct key_value_info, void *data)
>     that would get rid of all the UNUSED annotations but would mean even
>     more churn.

Some of my colleagues also suggested this off-list. I think it is
impractical for this series because I don't think anyone could
reasonably review with all of the added churn. At least its current
form, the churn is mostly concentrated in the signatures, but performing
^this change would make the bodies quite churny too.

After this series, I think it becomes somewhat feasible with coccinelle.
My .cocci files were difficult to craft because we couldn't rely on the
signature of config_fn_t alone to tell us if the function is actually
used as config_fn_t, but after this series, we can just use the
signature since config_fn_t has a struct key_value_info param.

>     The advantage is that one could add functions like
>     kvi_bool_or_int(kvi, &is_bool) and get good error messages because
>     all the config parsing functions would all have access to location
>     information.

Interesting, I hadn't considered this possibility. This seems like a
pretty good abstraction to me, though I worry about the feasibility
since this is yet again more churn.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 05/14] (RFC-only) config: finish config_fn_t refactor
  2023-05-30 18:42   ` [PATCH v2 05/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
@ 2023-06-01 22:17     ` Jonathan Tan
  0 siblings, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-01 22:17 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>
> 
> Here's an exhaustive list of all of the changes:

Up to here the changes are all mechanical, and look good.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 06/14] config.c: pass kvi in configsets
  2023-05-30 18:42   ` [PATCH v2 06/14] config.c: pass kvi in configsets Glen Choo via GitGitGadget
@ 2023-06-01 22:21     ` Jonathan Tan
  0 siblings, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-01 22:21 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>
> 
> Trivially pass "struct key_value_info" to config callbacks in
> configset_iter(). Then, in config callbacks that are only used with
> configsets, use the "kvi" arg to replace calls to current_config_*(),

OK, so the end result of this patch is, indeed, that configset_iter()
passes kvi, and this is proven by some callbacks being changed to use
the kvi provided, and still functioning. I did not verify that the
callbacks in this patch were the *only* callbacks that are only used
with configsets, but I don't think that is important for the purpose of
reviewing this patch - as long as current_config_* has all been removed
at the end of this patchset, then things are fine.

> and delete current_config_line() because it has no remaining callers.

Ah, great that we can remove one of the global-reliant functions as a
result of this.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 07/14] config: provide kvi with config files
  2023-05-30 18:42   ` [PATCH v2 07/14] config: provide kvi with config files Glen Choo via GitGitGadget
@ 2023-06-01 22:41     ` Jonathan Tan
  2023-06-01 23:54     ` Jonathan Tan
  1 sibling, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-01 22:41 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>

For some reason, I was having difficulty understanding the commit
message, so I looked at the code and I think I could reason out the
purpose of this patch. It is 2-fold:

 - teach git_config_from_file_with_options() to supply information to
   its callbacks through a kvi argument
 - its kvi argument requires a scope; get it not through
   current_config_scope() but through an argument

And as a corollary of the latter point, the scope is UNKNOWN when no
global scope is set by the surrounding code (if a scope is set by the
surrounding code, use that instead).

Unlike the previous patch, since no callers were changed, the only
way we can see that this has happened is through reading the code of
git_config_from_file_with_options(), but I've read it and it looks good.
(Again, as long as current_config_* is all gone at the end of the patch
set, then things are fine.)

> Refactor out the configset logic that caches "struct config_source" and
> "enum config_scope" as a "struct key_value_info", and use it to pass the
> "kvi" arg to config callbacks when parsing config files. Get the "enum
> config_scope" value by plumbing an additional arg through
> git_config_from_file_with_options() and the underlying machinery.
> 
> We do not exercise the "kvi" arg yet because the remaining
> current_config_*() callers may be used with config_with_options(), which
> may read config from parameters, but parameters don't pass "kvi" yet.
> 
> Signed-off-by: Glen Choo <chooglen@google.com>

Now that I reread this, I can see that it hit all the points that I
wrote at the top. So I think no change is needed in the commit message,
unless another reviewer also found it unclear.

The code itself looks good.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 09/14] config.c: provide kvi with CLI config
  2023-05-30 18:42   ` [PATCH v2 09/14] config.c: provide kvi with CLI config Glen Choo via GitGitGadget
@ 2023-06-01 23:35     ` Jonathan Tan
  2023-06-02 17:26       ` Glen Choo
  0 siblings, 1 reply; 115+ messages in thread
From: Jonathan Tan @ 2023-06-01 23:35 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>
> 
> plumb "kvi" so that we can replace
> nearly all calls to current_config_*(). (The exception is an edge case
> where trace2/*.c calls current_config_scope().

Probably clearer to say:

  plumb "kvi" so that we can remove all calls of
  current_config_scope() except from trace2/*.c (which will be handled in
  a later commit), and remove all other current_config_*() (the
  functions themselves and their calls).

> Plumbing "kvi" reveals a few places where we've been doing the wrong
> thing:
> 
> * git_config_parse_parameter() hasn't been setting config source
>   information, so plumb "kvi" there too.
> 
> * "git config --get-urlmatch --show-scope" iterates config to collect
>   values, but then attempts to display the scope after config iteration.
>   Fix this by copying the "kvi" arg in the collection phase so that it
>   can be read back later. This means that we can now support "git config
>   --get-urlmatch --show-origin" (we don't allow this combination of args
>   because of this bug), but that is left unchanged for now.
> 
> * "git config --default" doesn't have config source metadata when
>   displaying the default value. Fix this by treating the default value
>   as if it came from the command line (e.g. like we do with "git -c" or
>   "git config --file"), using kvi_from_param().
> 
> Signed-off-by: Glen Choo <chooglen@google.com>

Thanks for noticing and fixing these.

> +	memcpy(&matched->kvi, kvi, sizeof(struct key_value_info));

Can this just be

  matched->kvi = *kvi;

?

If not, for the sizeof, use *kvi as the argument instead of struct
key_value_info.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 10/14] trace2: plumb config kvi
  2023-05-30 18:42   ` [PATCH v2 10/14] trace2: plumb config kvi Glen Choo via GitGitGadget
@ 2023-06-01 23:38     ` Jonathan Tan
  0 siblings, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-01 23:38 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>

Thanks - a mechanical code change that allows for quite some code to be
removed. Looks good.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 11/14] config: pass kvi to die_bad_number()
  2023-05-30 18:42   ` [PATCH v2 11/14] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
@ 2023-06-01 23:48     ` Jonathan Tan
  2023-06-02 17:23       ` Glen Choo
  0 siblings, 1 reply; 115+ messages in thread
From: Jonathan Tan @ 2023-06-01 23:48 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> Outside of config.c, config callbacks now need to pass "kvi" to any of
> the git_config_<type>() functions that parse a config string into a
> number type. Included is a .cocci patch to make that refactor. In cases
> where "kvi" would never be used, pass NULL, e.g.:
> 
> - In config.c, when we are parsing a boolean instead of a number
> - In builtin/config.c, when calling normalize_value() before setting
>   config to something the user gave us.

In these cases, could we synthesize a kvi instead of using NULL? I
believe there are already code paths that use an UNKNOWN scope - these
seem similar to that.

Otherwise looks good - a straightforward, mostly mechanical, change.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 07/14] config: provide kvi with config files
  2023-05-30 18:42   ` [PATCH v2 07/14] config: provide kvi with config files Glen Choo via GitGitGadget
  2023-06-01 22:41     ` Jonathan Tan
@ 2023-06-01 23:54     ` Jonathan Tan
  1 sibling, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-01 23:54 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> @@ -963,7 +965,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, NULL, data);
> +	kvi->linenr = cs->linenr;
> +	ret = fn(name->buf, value, kvi, data);

Forgot to mention in my other email...it's a pity that we have to bump
the kvi->linenr like this because the original kvi generated from the
cs is now out-to-date w.r.t. the cs in terms of line number (you can
see in the context how the cs linenr is also updated), so there are now
2 sources of truth with regards to the line number. I can't think of a
better way to do this, though.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 13/14] config: add kvi.path, use it to evaluate includes
  2023-05-30 18:42   ` [PATCH v2 13/14] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
@ 2023-06-02  0:06     ` Jonathan Tan
  0 siblings, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-02  0:06 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>
> 
> Include directives are evaluated using the path of the config file. To
> reduce the dependence on "config_reader.source", add a new
> "key_value_info.path" member and use that instead of
> "config_source.path".

Maybe add:

  This allows us to remove a "struct config_reader *" field from "struct
  config_include_data", which will subsequently allow us to remove "struct
  config_reader" entirely.
   
The code change looks good.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 14/14] config: pass source to config_parser_event_fn_t
  2023-05-30 18:42   ` [PATCH v2 14/14] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
@ 2023-06-02  0:08     ` Jonathan Tan
  2023-06-02 17:20       ` Glen Choo
  0 siblings, 1 reply; 115+ messages in thread
From: Jonathan Tan @ 2023-06-02  0:08 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>
> 
> ..so that the callback can use a "struct config_source" parameter
> instead of "config_reader.source". "struct config_source" is internal to
> config.c, but this refactor is okay because this function has only ever
> been (and probably ever will be) used internally by config.c.

Maybe s/this refactor/adding a pointer to a struct defined in a .c into
a public function signature defined in a .h/

So this means that callers cannot instantiate that public function
unless they are in config.c, but I see that it has been appropriately
documented, so it should be fine.

Thanks for this series. Overall I had some minor comments, but things
look good overall.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-06-01 16:22       ` Glen Choo
@ 2023-06-02  9:54         ` Phillip Wood
  2023-06-02 16:46           ` Glen Choo
  0 siblings, 1 reply; 115+ messages in thread
From: Phillip Wood @ 2023-06-02  9:54 UTC (permalink / raw)
  To: Glen Choo, phillip.wood, Glen Choo via GitGitGadget, git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

On 01/06/2023 17:22, Glen Choo wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>>    - is it worth making struct key_value_info opaque and provide getters
>>      for the fields so we can change the implementation in the future
>>      without having to modify every user. We could rename it
>>      config_context or something generic like that if we think it might
>>      grow in scope in the future.
> 
> Yes! I planned to do the key_value_info -> config_context conversion
> when I send the first non-RFC version for this exact reason.

That's great

>>    - (probably impractical) could we stuff the key and value into struct
>>      key_value_info so config_fn_t becomes
>>      fn(const struct key_value_info, void *data)
>>      that would get rid of all the UNUSED annotations but would mean even
>>      more churn.
> 
> Some of my colleagues also suggested this off-list. I think it is
> impractical for this series because I don't think anyone could
> reasonably review with all of the added churn. At least its current
> form, the churn is mostly concentrated in the signatures, but performing
> ^this change would make the bodies quite churny too.

I agree that keeping the churn to the function signatures makes it 
bearable. I wonder though if we could make the change by doing

-git_default_config(const char *key, const char *value, void *data)
+git_default_config(const struct key_value_info *kvi, void *data)
  {
+	const char *key = kvi_key(kvi);
+	const char *value = kvi_value(kvi);
+

That would add to the diffstat but I think it wouldn't really be any 
harder to review than just changing the signature as we're not modifying 
any existing lines in the function body, just adding a couple of 
variable declarations to the start of the function. If there is an error 
in either of the variable declarations then the compiler will complain 
as "key" or "value" will end up not being declared. It would pave the 
way for gradually changing the function bodies to use "kvi" directly and 
removing "key" and "value"

> After this series, I think it becomes somewhat feasible with coccinelle.
> My .cocci files were difficult to craft because we couldn't rely on the
> signature of config_fn_t alone to tell us if the function is actually
> used as config_fn_t, but after this series, we can just use the
> signature since config_fn_t has a struct key_value_info param.

That's an interesting possibility, I worry though that two huge changes 
to the config callbacks might be one too many though.

>>      The advantage is that one could add functions like
>>      kvi_bool_or_int(kvi, &is_bool) and get good error messages because
>>      all the config parsing functions would all have access to location
>>      information.
> 
> Interesting, I hadn't considered this possibility. This seems like a
> pretty good abstraction to me, though I worry about the feasibility
> since this is yet again more churn.

It could be done gradually though, converting one config callback at a 
time once the relevant changes have been made to config.c.

As an aside, I think we'd also want a couple of helpers for matching 
keys so we can just write kvi_match_key(kvi, "user.name") or 
kvi_skip_key_prefix(kvi, "core.", &p) rather than having to extract the 
key name first.

Best Wishes

Phillip


^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-06-02  9:54         ` Phillip Wood
@ 2023-06-02 16:46           ` Glen Choo
  2023-06-05  9:38             ` Phillip Wood
  0 siblings, 1 reply; 115+ messages in thread
From: Glen Choo @ 2023-06-02 16:46 UTC (permalink / raw)
  To: Phillip Wood, phillip.wood, Glen Choo via GitGitGadget, git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

Phillip Wood <phillip.wood123@gmail.com> writes:

>> Some of my colleagues also suggested this off-list. I think it is
>> impractical for this series because I don't think anyone could
>> reasonably review with all of the added churn. At least its current
>> form, the churn is mostly concentrated in the signatures, but performing
>> ^this change would make the bodies quite churny too.
>
> I agree that keeping the churn to the function signatures makes it 
> bearable. I wonder though if we could make the change by doing
>
> -git_default_config(const char *key, const char *value, void *data)
> +git_default_config(const struct key_value_info *kvi, void *data)
>   {
> +	const char *key = kvi_key(kvi);
> +	const char *value = kvi_value(kvi);

Ah, yes that seems reasonable to review (and most importantly for me, it
is also doable with coccinelle once I figure out how :P). I also agree
that it's better to do it all in one change than two.

> As an aside, I think we'd also want a couple of helpers for matching 
> keys so we can just write kvi_match_key(kvi, "user.name") or 
> kvi_skip_key_prefix(kvi, "core.", &p) rather than having to extract the 
> key name first.

Yes, and that would also abstract over implementation details like
matching keys using strcasecmp() and not strcmp(). For reasons like
this, I think your proposal paves the way for a harder-to-misuse API.

I still have some nagging, probably irrational fear that consolidating
all of the config_fn_t args is trickier to manage than adding a single
key_value_info arg. It definitely *sounds* trickier, but I can't really
think of a real downside.

Maybe I just have to try it and send the result for others to consider.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 14/14] config: pass source to config_parser_event_fn_t
  2023-06-02  0:08     ` Jonathan Tan
@ 2023-06-02 17:20       ` Glen Choo
  0 siblings, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-02 17:20 UTC (permalink / raw)
  To: Jonathan Tan, Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

Jonathan Tan <jonathantanmy@google.com> writes:

> Thanks for this series. Overall I had some minor comments, but things
> look good overall.

Thank you so much. I appreciate the thorough effort and all of the
suggestions.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 11/14] config: pass kvi to die_bad_number()
  2023-06-01 23:48     ` Jonathan Tan
@ 2023-06-02 17:23       ` Glen Choo
  0 siblings, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-02 17:23 UTC (permalink / raw)
  To: Jonathan Tan, Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

Jonathan Tan <jonathantanmy@google.com> writes:

> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> Outside of config.c, config callbacks now need to pass "kvi" to any of
>> the git_config_<type>() functions that parse a config string into a
>> number type. Included is a .cocci patch to make that refactor. In cases
>> where "kvi" would never be used, pass NULL, e.g.:
>> 
>> - In config.c, when we are parsing a boolean instead of a number
>> - In builtin/config.c, when calling normalize_value() before setting
>>   config to something the user gave us.
>
> In these cases, could we synthesize a kvi instead of using NULL? I
> believe there are already code paths that use an UNKNOWN scope - these
> seem similar to that.

Okay, that sounds reasonable. This has echoes of Philip Wood's
suggestion (elsewhere in the thread) of combining all of the config_fn_t
args into a single struct, which means we can no longer use NULL as the
default.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 09/14] config.c: provide kvi with CLI config
  2023-06-01 23:35     ` Jonathan Tan
@ 2023-06-02 17:26       ` Glen Choo
  0 siblings, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-02 17:26 UTC (permalink / raw)
  To: Jonathan Tan, Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

Jonathan Tan <jonathantanmy@google.com> writes:

>> +	memcpy(&matched->kvi, kvi, sizeof(struct key_value_info));
>
> Can this just be
>
>   matched->kvi = *kvi;
>
> ?

If I remember correctly (big if), we have to copy the memory because the
config machinery allocates kvi on the stack, and the relevant functions
have returned by then. Hm, does this suggest that kvi should be const?

> If not, for the sizeof, use *kvi as the argument instead of struct
> key_value_info.

Makes sense.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-06-02 16:46           ` Glen Choo
@ 2023-06-05  9:38             ` Phillip Wood
  2023-06-09 23:19               ` Glen Choo
  0 siblings, 1 reply; 115+ messages in thread
From: Phillip Wood @ 2023-06-05  9:38 UTC (permalink / raw)
  To: Glen Choo, phillip.wood, Glen Choo via GitGitGadget, git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

On 02/06/2023 17:46, Glen Choo wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
>> As an aside, I think we'd also want a couple of helpers for matching
>> keys so we can just write kvi_match_key(kvi, "user.name") or
>> kvi_skip_key_prefix(kvi, "core.", &p) rather than having to extract the
>> key name first.
> 
> Yes, and that would also abstract over implementation details like
> matching keys using strcasecmp() and not strcmp(). For reasons like
> this, I think your proposal paves the way for a harder-to-misuse API.
> 
> I still have some nagging, probably irrational fear that consolidating
> all of the config_fn_t args is trickier to manage than adding a single
> key_value_info arg. It definitely *sounds* trickier, but I can't really
> think of a real downside.
>
> Maybe I just have to try it and send the result for others to consider.

That's probably the best way to see if it is an improvement - you can 
always blame me if it turns out not to be! Hopefully it isn't too much 
work to add enough api to be able to convert a couple of config_fn_t 
functions to see how it pans out.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t
  2023-06-05  9:38             ` Phillip Wood
@ 2023-06-09 23:19               ` Glen Choo
  0 siblings, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-09 23:19 UTC (permalink / raw)
  To: Phillip Wood, phillip.wood, Glen Choo via GitGitGadget, git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer

Phillip Wood <phillip.wood123@gmail.com> writes:

> On 02/06/2023 17:46, Glen Choo wrote:
>> Phillip Wood <phillip.wood123@gmail.com> writes:
>>> As an aside, I think we'd also want a couple of helpers for matching
>>> keys so we can just write kvi_match_key(kvi, "user.name") or
>>> kvi_skip_key_prefix(kvi, "core.", &p) rather than having to extract the
>>> key name first.
>> 
>> Yes, and that would also abstract over implementation details like
>> matching keys using strcasecmp() and not strcmp(). For reasons like
>> this, I think your proposal paves the way for a harder-to-misuse API.
>> 
>> I still have some nagging, probably irrational fear that consolidating
>> all of the config_fn_t args is trickier to manage than adding a single
>> key_value_info arg. It definitely *sounds* trickier, but I can't really
>> think of a real downside.
>>
>> Maybe I just have to try it and send the result for others to consider.
>
> That's probably the best way to see if it is an improvement - you can 
> always blame me if it turns out not to be! Hopefully it isn't too much 
> work to add enough api to be able to convert a couple of config_fn_t 
> functions to see how it pans out.
>
I was preparing a reroll that incorporated some of the suggestions here,
but when I was done, I didn't really feel like it was better (or at the
very least, worth the extra churn) compared to the original approach.

The 3 versions I was comparing are:

1. Adding a new "struct config_context" arg to config_fn_t that only
  contains a .kvi member. This is basically the approach in v1 and v2
  (adding an extra args) but wrapping it in a "struct config_context" so
  that we can grow it later.

2. Stuffing "key" and "value" into "struct key_value_info" and removing
  "key" and "value" from config_fn_t.

3. Creating a "struct config_context" that contains "key", "value" and
  "struct key_value_info" as separate members, and removing
  "key" and "value" from config_fn_t.

I didn't try using opaque getters - I think that's nice to have, but
isn't crucial to this series and introduces a lot of churn.

I've uploaded an implementation of option 3. [1]. The result is quite
ugly in some places. In particular, config callbacks are used to calling
other config callbacks with slightly different args (e.g. massaging the
"key" but keeping the "value" the same, see patches 3,5/14 for
examples.), so to make this work, I ended up creating a new
"config_context", copying over the relevant members, then changing the
members that need to be different. I didn't pursue the refactor to its
end state (e.g. some of the CLI config machinery in the earlier patches
and git_config_int()/ functions that end in die_bad_number() in later
patches, take "key", "value", "key_value_info" as separate args instead
of a single "config_context" arg) so I haven't reaped all of the
benefits, but I thought the range-diff was getting churny enough that I
wasn't interested in pursuing the idea further.

I toyed with option 2 for a bit. Unsurprisingly, it requires even more
copying of members, though in some ways, it's better than option 3
because callers only have to initialize one "struct key_value_info"
instead of "struct key_value_info" _and_ "struct config_context".
However, it requires reworking the machinery quite a lot because
"key_value_info" is currently used _just_ for config source information,
and there are several cases where we need to propagate the config source
information separately from the key/value (e.g. the configsets store
"key" in a hashmap key, "value" in a string_list_item.string, and
"key_value_info" in a string_list_item.util). I'm convinced that this is
better interface-wise than option 3, but the required effort is high
enough that I don't think we should do this unless we see a real need
for it.

With that in mind, I'm tempted to continue with option 1 for now, but
let me know if I'm missing something.

[1] https://github.com/git/git/compare/master...chooglen:git:config/no-global-combined-kv-context?expand=1

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v3 00/12] config: remove global state from config iteration
  2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
                     ` (13 preceding siblings ...)
  2023-05-30 18:42   ` [PATCH v2 14/14] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
@ 2023-06-20 19:43   ` Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
                       ` (13 more replies)
  14 siblings, 14 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

Junio: I rebased this onto a newer "master". I assume this is a noop for
you - I noticed that the RFC versions weren't applied anyway (good).

Thanks for the thoughtful review on the last round, all. This is the
first non-RFC reroll, and hopefully the last ;) This is basically the
RFC v2 with the post-RFC cleanups I've been talking about, plus review
comments. I did a decent amount of experimentation with Phillip's
alternative config_fn_t signatures, and while I think they make sense in
the long term (when we add additional helpers, make structs opaque,
etc), they introduce a lot of distracting churn but don't really change
the status quo, so I've left them out of this series.

A major departure from the RFC is that the large refactoring patches
have now been squashed together. If you'd would like to meaningfully
range-diff those commits against v2, you can use:

  git fetch https://github.com/chooglen/git/ config/no-global-unsquashed-cocci

which uses the same base as v1 and v2.

= Changes since v2

* Rebase onto a newer version of "master"

* To accomodate future changes to config_fn_t, use "const struct
  config_context *ctx" (which has a .kvi member) instead of directly
  referencing "kvi".

* Add appropriate comments to config.h.

* .kvi is now const.

* Introduce KVI_INIT as a safer default to "= { 0 }".

* Incorporate wording suggestions

= Description

This series removes all global state from config iteration, i.e. parsing
config and iterating configsets, by passing config metadata as a "struct
key_value_info" to the "config_fn_t" callbacks. This allows us to
get rid of:

 * "the_reader" (formerly "cf", "current_parsing_scope",
   "current_config_kvi"), and the config.c machinery that maintained it.
   This only needed to be global because it was read by...
 * The "current_*" functions that read metadata about the 'current'
   config value ("current_config_scope", "current_config_name", etc),
   which are replaced by reading values from the "struct key_value_info"
   passed to the callbacks.

As a result:

 * Config iterating code can be moved to into its own library with few
   modifications. C.f. libification efforts [1].
 * The config iterating code can be safely called in parallel.
 * We expose and fix instances where the "current_*" functions were being
   called outside of config callbacks.

We've had this idea of doing this "config_fn_t" refactor for a long time
[2], but we've never attempted it because we wanted to avoid churn. After
attempting it, though, I'm quite convinced that this is the right way
forward for config, since the lack of global state makes things much easier
to reason about. The churn is also quite manageable:

 * The vast majority of changes can be handled with cocci.
 * The few cases that aren't covered by cocci have obvious fixes.
 * The change is simple enough for in-flight topics to perform and conflicts
   will be caught at build time anyway.

To avoid _future_ churn (if we want to change config_fn_t again), I
opted to pass a config_context arg instead of using key_value_info
directly. We can make changes we want to that arg while leaving the
signature untouched.

= Patch overview

* 1-3/12 add the "ctx" parameter to the config_fn_t signature.

* 4-9/12 converts the config.c machinery off "config_reader.config_kvi"
  and "config_reader.source" and onto ctx->kvi. We convert the config.c
  machinery from "config_reader" to "kvi" one-by-one: configsets, then
  files, then CLI. To exercise the "kvi" arg as soon as possible, we
  convert from current_*() to "kvi" as soon as it is available. For
  example, in 4/12 "kvi" is available only in configsets, so we convert
  the current_*() call sites that are only reached via configsets and
  leave the others untouched. This means that we have a mix of
  current_*() and "kvi" in the middle, but auditing the changes is
  relatively easy, since you only need to verify that a callback isn't
  relying on the "kvi" arg before it is available, and that current_*()
  and "kvi" give the same value.

  * 6-7/12 squashes some bugs where builtin/config.c was calling the
    current_*() API outside of config callbacks. The "kvi" plumbing
    doesn't just make the bugs apparent, it also provides an obvious way
    to fix the bugs (by injecting "kvi" into the right places in
    builtin/config.c). These would have been nontrivial to fix if we
    were still using global state.

* 10-12/12 remove config_reader by taking advantage of "kvi" and doing
  some other light plumbing.

= Alternatives considered

Ævar suggested in [3] that we might be able to do the refactor incrementally
by having both the old "config_fn_t" and the new "config_fn_t with kvi",
which lets us convert some of config iterating (e.g. configsets) without
touching the others (e.g. config parsing). I experimented with that for a
bit, and it turned out that doing it all at once is actually less work
because we don't have to worry about the case where the same "config_fn_t"
is used in both git_config() and git_config_from_file().

Jonathan Tan suggested in [4] that to reduce churn, we might be able to
convert many of the config_fn_t-s to the config set API before attempting
this refactor. But (hopefully), these patches show that the churn is
manageable even without this preparatory step.

[1]
https://lore.kernel.org/git/CAJoAoZ=Cig_kLocxKGax31sU7Xe4==BGzC__Bg2_pr7krNq6MA@mail.gmail.com/
[2]
https://lore.kernel.org/git/CAPc5daV6bdUKS-ExHmpT4Ppy2S832NXoyPw7aOLP7fG=WrBPgg@mail.gmail.com/
[3]
https://lore.kernel.org/git/RFC-patch-5.5-2b80d293c83-20230317T042408Z-avarab@gmail.com
[4]
https://lore.kernel.org/git/20230306195756.3399115-1-jonathantanmy@google.com/


Glen Choo (12):
  config: inline git_color_default_config
  urlmatch.h: use config_fn_t type
  config: add ctx arg to config_fn_t
  config.c: pass ctx in configsets
  config: pass ctx with config files
  builtin/config.c: test misuse of format_config()
  config.c: pass ctx with CLI config
  trace2: plumb config kvi
  config: pass kvi to die_bad_number()
  config.c: remove config_reader from configsets
  config: add kvi.path, use it to evaluate includes
  config: pass source to config_parser_event_fn_t

 alias.c                                       |   3 +-
 archive-tar.c                                 |   5 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   5 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   8 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |  12 +-
 builtin/clean.c                               |   9 +-
 builtin/clone.c                               |  11 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   3 +-
 builtin/commit.c                              |  20 +-
 builtin/config.c                              |  72 ++-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |  13 +-
 builtin/fsmonitor--daemon.c                   |  11 +-
 builtin/grep.c                                |  12 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   9 +-
 builtin/log.c                                 |  12 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |  19 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |  15 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |  15 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   8 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   3 +-
 builtin/tag.c                                 |   9 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   9 +-
 color.c                                       |   8 -
 color.h                                       |   6 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      | 551 +++++++-----------
 config.h                                      |  80 ++-
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_ctx.pending.cocci    | 144 +++++
 contrib/coccinelle/git_config_number.cocci    |  27 +
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   4 +-
 diff.c                                        |  19 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   7 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |  12 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   7 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |   9 +-
 http.c                                        |  15 +-
 ident.c                                       |   4 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   7 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   1 +
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   4 +-
 notes.c                                       |   4 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   8 +-
 revision.c                                    |   4 +-
 scalar.c                                      |   4 +-
 sequencer.c                                   |  29 +-
 setup.c                                       |  18 +-
 submodule-config.c                            |  31 +-
 submodule-config.h                            |   3 +-
 t/helper/test-config.c                        |  24 +-
 t/helper/test-userdiff.c                      |   4 +-
 t/t1300-config.sh                             |  27 +
 trace2.c                                      |   4 +-
 trace2.h                                      |   3 +-
 trace2/tr2_cfg.c                              |  16 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trace2/tr2_tgt.h                              |   4 +-
 trace2/tr2_tgt_event.c                        |   4 +-
 trace2/tr2_tgt_normal.c                       |   4 +-
 trace2/tr2_tgt_perf.c                         |   4 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |  18 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   8 +-
 worktree.c                                    |   2 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   4 +-
 103 files changed, 956 insertions(+), 638 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci
 create mode 100644 contrib/coccinelle/git_config_number.cocci


base-commit: d7d8841f67f29e6ecbad85a11805c907d0f00d5d
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1497%2Fchooglen%2Fconfig%2Fno-global-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1497/chooglen/config/no-global-v3
Pull-Request: https://github.com/git/git/pull/1497

Range-diff vs v2:

  1:  d5edf7e3fdd =  1:  26109b65142 config: inline git_color_default_config
  2:  821f0b90580 =  2:  1aeffcb1395 urlmatch.h: use config_fn_t type
  3:  6834e37066e <  -:  ----------- (RFC-only) config: add kvi arg to config_fn_t
  4:  bd52c6232ec !  3:  d3eb439aad2 (RFC-only) config: apply cocci to config_fn_t implementations
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    (RFC-only) config: apply cocci to config_fn_t implementations
     -
     -    Pass "struct key_value_info" to *most* functions that are invoked as
     -    "config_fn_t" callbacks by applying
     -    contrib/coccinelle/config_fn_kvi.pending.cocci. None of the functions
     -    actually use the "kvi" arg yet (besides propagating it to a function
     -    that now expects "kvi"), but this will be addressed in a later commit.
     -    When deciding whether or not to propagate "kvi" to an inner function,
     -    only propagate the "kvi" arg if the inner function is actually invoked
     -    elsewhere as a config callback; it does not matter whether the function
     -    happens have the same signature as config_fn_t.
     -
     -    This commit does not build and has several style issues (e.g. a lack of
     -    spacing around the "kvi" arg), but I've split this out for the RFC so
     -    that it's more obvious which changes are automatic vs manual. Post-RFC I
     -    will squash this with the rest of the refactor + cocci changes.
     +    config: add ctx arg to config_fn_t
     +
     +    Add a new "const struct config_context *ctx" arg to config_fn_t to hold
     +    additional information about the config iteration operation.
     +    config_context has a "struct key_value_info kvi" member that holds
     +    metadata about the config source being read (e.g. what kind of config
     +    source it is, the filename, etc). In this series, we're only interested
     +    in .kvi, so we could have just used "struct key_value_info" as an arg,
     +    but config_context makes it possible to add/adjust members in the future
     +    without changing the config_fn_t signature. We could also consider other
     +    ways of organizing the args (e.g. moving the config name and value into
     +    config_context or key_value_info), but in my experiments, the
     +    incremental benefit doesn't justify the added complexity (e.g. a
     +    config_fn_t will sometimes invoke another config_fn_t but with a
     +    different config value).
     +
     +    In subsequent commits, the .kvi member will replace the global "struct
     +    config_reader" in config.c, making config iteration a global-free
     +    operation. It requires much more work for the machinery to provide
     +    meaningful values of .kvi, so for now, merely change the signature and
     +    call sites, pass NULL as a placeholder value, and don't rely on the arg
     +    in any meaningful way.
     +
     +    Most of the changes are performed by
     +    contrib/coccinelle/config_fn_ctx.pending.cocci, which, for every
     +    config_fn_t:
     +
     +    - Modifies the signature to accept "const struct config_context *ctx"
     +    - Passes "ctx" to any inner config_fn_t, if needed
     +    - Adds UNUSED attributes to "ctx", if needed
     +
     +    Most config_fn_t instances are easily identified by seeing if they are
     +    called by the various config functions. Most of the remaining ones are
     +    manually named in the .cocci patch. Manual cleanups are still needed,
     +    but the majority of it is trivial; it's either adjusting config_fn_t
     +    that the .cocci patch didn't catch, or adding forward declarations of
     +    "struct config_context ctx" to make the signatures make sense.
     +
     +    The non-trivial changes are in cases where we are invoking a config_fn_t
     +    outside of config machinery, and we now need to decide what value of
     +    "ctx" to pass. These cases are:
     +
     +    - trace2/tr2_cfg.c:tr2_cfg_set_fl()
     +
     +      This is indirectly called by git_config_set() so that the trace2
     +      machinery can notice the new config values and update its settings
     +      using the tr2 config parsing function, i.e. tr2_cfg_cb().
     +
     +    - builtin/checkout.c:checkout_main()
     +
     +      This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
     +      This might be worth refactoring away in the future, since
     +      git_xmerge_config() can call git_default_config(), which can do much
     +      more than just parsing.
     +
     +    Handle them by creating a KVI_INIT macro that initializes "struct
     +    key_value_info" to a reasonable default, and use that to construct the
     +    "ctx" arg.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     @@ alias.c: struct config_alias_data {
       
      -static int config_alias_cb(const char *key, const char *value, void *d)
      +static int config_alias_cb(const char *key, const char *value,
     -+			   struct key_value_info *kvi UNUSED, void *d)
     ++			   const struct config_context *ctx UNUSED, void *d)
       {
       	struct config_alias_data *data = d;
       	const char *p;
     @@ archive-tar.c: static int tar_filter_config(const char *var, const char *value,
       
      -static int git_tar_config(const char *var, const char *value, void *cb)
      +static int git_tar_config(const char *var, const char *value,
     -+			  struct key_value_info *kvi UNUSED, void *cb)
     ++			  const struct config_context *ctx UNUSED, void *cb)
       {
       	if (!strcmp(var, "tar.umask")) {
       		if (value && !strcmp(value, "user")) {
     @@ archive-zip.c: static void dos_time(timestamp_t *timestamp, int *dos_date, int *
       }
       
       static int archive_zip_config(const char *var, const char *value,
     -+			      struct key_value_info *kvi UNUSED,
     ++			      const struct config_context *ctx UNUSED,
       			      void *data UNUSED)
       {
       	return userdiff_config(var, value);
     @@ builtin/add.c: static struct option builtin_add_options[] = {
       
      -static int add_config(const char *var, const char *value, void *cb)
      +static int add_config(const char *var, const char *value,
     -+		      struct key_value_info *kvi, void *cb)
     ++		      const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "add.ignoreerrors") ||
       	    !strcmp(var, "add.ignore-errors")) {
     @@ builtin/add.c: static int add_config(const char *var, const char *value, void *c
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static const char embedded_advice[] = N_(
     @@ builtin/blame.c: static const char *add_prefix(const char *prefix, const char *p
       
      -static int git_blame_config(const char *var, const char *value, void *cb)
      +static int git_blame_config(const char *var, const char *value,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "blame.showroot")) {
       		show_root = git_config_bool(var, value);
     @@ builtin/blame.c: static int git_blame_config(const char *var, const char *value,
       		return -1;
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static int blame_copy_callback(const struct option *option, const char *arg, int unset)
     @@ builtin/branch.c: static unsigned int colopts;
       
      -static int git_branch_config(const char *var, const char *value, void *cb)
      +static int git_branch_config(const char *var, const char *value,
     -+			     struct key_value_info *kvi, void *cb)
     ++			     const struct config_context *ctx, void *cb)
       {
       	const char *slot_name;
       
     @@ builtin/branch.c: static int git_branch_config(const char *var, const char *valu
       		return -1;
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static const char *branch_get_color(enum color_branch ix)
     @@ builtin/cat-file.c: static int batch_objects(struct batch_options *opt)
       
      -static int git_cat_file_config(const char *var, const char *value, void *cb)
      +static int git_cat_file_config(const char *var, const char *value,
     -+			       struct key_value_info *kvi, void *cb)
     ++			       const struct config_context *ctx, void *cb)
       {
       	if (userdiff_config(var, value) < 0)
       		return -1;
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static int batch_option_callback(const struct option *opt,
     @@ builtin/checkout.c: static int switch_branches(const struct checkout_opts *opts,
       
      -static int git_checkout_config(const char *var, const char *value, void *cb)
      +static int git_checkout_config(const char *var, const char *value,
     -+			       struct key_value_info *kvi, void *cb)
     ++			       const struct config_context *ctx, void *cb)
       {
       	struct checkout_opts *opts = cb;
       
     @@ builtin/checkout.c: static int git_checkout_config(const char *var, const char *
       		return git_default_submodule_config(var, value, NULL);
       
      -	return git_xmerge_config(var, value, NULL);
     -+	return git_xmerge_config(var, value,kvi, NULL);
     ++	return git_xmerge_config(var, value, ctx, NULL);
       }
       
       static void setup_new_branch_info_and_source_tree(
     +@@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const char *prefix,
     + 	}
     + 
     + 	if (opts->conflict_style) {
     ++		struct key_value_info kvi = KVI_INIT;
     ++		struct config_context ctx = {
     ++			.kvi = &kvi,
     ++		};
     + 		opts->merge = 1; /* implied */
     +-		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
     ++		git_xmerge_config("merge.conflictstyle", opts->conflict_style,
     ++				  &ctx, NULL);
     + 	}
     + 	if (opts->force) {
     + 		opts->discard_changes = 1;
      
       ## builtin/clean.c ##
      @@ builtin/clean.c: struct menu_stuff {
     @@ builtin/clean.c: struct menu_stuff {
       
      -static int git_clean_config(const char *var, const char *value, void *cb)
      +static int git_clean_config(const char *var, const char *value,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	const char *slot_name;
       
     @@ builtin/clean.c: static int git_clean_config(const char *var, const char *value,
       		return -1;
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static const char *clean_get_color(enum color_clean ix)
     @@ builtin/clone.c: static int checkout(int submodule_progress, int filter_submodul
       
      -static int git_clone_config(const char *k, const char *v, void *cb)
      +static int git_clone_config(const char *k, const char *v,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(k, "clone.defaultremotename")) {
       		free(remote_name);
     @@ builtin/clone.c: static int git_clone_config(const char *k, const char *v, void
       		config_filter_submodules = git_config_bool(k, v);
       
      -	return git_default_config(k, v, cb);
     -+	return git_default_config(k, v,kvi, cb);
     ++	return git_default_config(k, v, ctx, cb);
       }
       
      -static int write_one_config(const char *key, const char *value, void *data)
      +static int write_one_config(const char *key, const char *value,
     -+			    struct key_value_info *kvi UNUSED, void *data)
     ++			    const struct config_context *ctx,
     ++			    void *data)
       {
       	/*
       	 * give git_clone_config a chance to write config values back to the
     + 	 * environment, since git_config_set_multivar_gently only deals with
     + 	 * config-file writes
     + 	 */
     +-	int apply_failed = git_clone_config(key, value, data);
     ++	int apply_failed = git_clone_config(key, value, ctx, data);
     + 	if (apply_failed)
     + 		return apply_failed;
     + 
      
       ## builtin/column.c ##
      @@ builtin/column.c: static const char * const builtin_column_usage[] = {
     @@ builtin/column.c: static const char * const builtin_column_usage[] = {
       
      -static int column_config(const char *var, const char *value, void *cb)
      +static int column_config(const char *var, const char *value,
     -+			 struct key_value_info *kvi UNUSED, void *cb)
     ++			 const struct config_context *ctx UNUSED, void *cb)
       {
       	return git_column_config(var, value, cb, &colopts);
       }
     @@ builtin/commit-graph.c: static int write_option_max_new_filters(const struct opt
       }
       
       static int git_commit_graph_write_config(const char *var, const char *value,
     -+					 struct key_value_info *kvi UNUSED,
     ++					 const struct config_context *ctx UNUSED,
       					 void *cb UNUSED)
       {
       	if (!strcmp(var, "commitgraph.maxnewfilters"))
     @@ builtin/commit.c: static int parse_status_slot(const char *slot)
       
      -static int git_status_config(const char *k, const char *v, void *cb)
      +static int git_status_config(const char *k, const char *v,
     -+			     struct key_value_info *kvi, void *cb)
     ++			     const struct config_context *ctx, void *cb)
       {
       	struct wt_status *s = cb;
       	const char *slot_name;
     @@ builtin/commit.c: static int git_status_config(const char *k, const char *v, voi
       		return 0;
       	}
      -	return git_diff_ui_config(k, v, NULL);
     -+	return git_diff_ui_config(k, v,kvi, NULL);
     ++	return git_diff_ui_config(k, v, ctx, NULL);
       }
       
       int cmd_status(int argc, const char **argv, const char *prefix)
     @@ builtin/commit.c: int cmd_status(int argc, const char **argv, const char *prefix
       
      -static int git_commit_config(const char *k, const char *v, void *cb)
      +static int git_commit_config(const char *k, const char *v,
     -+			     struct key_value_info *kvi, void *cb)
     ++			     const struct config_context *ctx, void *cb)
       {
       	struct wt_status *s = cb;
       
     @@ builtin/commit.c: static int git_commit_config(const char *k, const char *v, voi
       	}
       
      -	return git_status_config(k, v, s);
     -+	return git_status_config(k, v,kvi, s);
     ++	return git_status_config(k, v, ctx, s);
       }
       
       int cmd_commit(int argc, const char **argv, const char *prefix)
     @@ builtin/config.c: static void show_config_scope(struct strbuf *buf)
       }
       
       static int show_all_config(const char *key_, const char *value_,
     --			   void *cb UNUSED)
     -+			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
     ++			   const struct config_context *ctx UNUSED,
     + 			   void *cb UNUSED)
       {
       	if (show_origin || show_scope) {
     - 		struct strbuf buf = STRBUF_INIT;
      @@ builtin/config.c: 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_,
     -+			  struct key_value_info *kvi UNUSED, void *cb)
     ++			  const struct config_context *ctx UNUSED, void *cb)
       {
       	struct strbuf_list *values = cb;
       
     @@ builtin/config.c: static const char *get_colorbool_slot;
       static char parsed_color[COLOR_MAXLEN];
       
       static int git_get_color_config(const char *var, const char *value,
     -+				struct key_value_info *kvi UNUSED,
     ++				const struct config_context *ctx UNUSED,
       				void *cb UNUSED)
       {
       	if (!strcmp(var, get_color_slot)) {
     @@ builtin/config.c: 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,
     -+				    struct key_value_info *kvi UNUSED,
     ++				    const struct config_context *ctx UNUSED,
       				    void *data UNUSED)
       {
       	if (!strcmp(var, get_colorbool_slot))
     @@ builtin/config.c: struct urlmatch_current_candidate_value {
       
      -static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
      +static int urlmatch_collect_fn(const char *var, const char *value,
     -+			       struct key_value_info *kvi UNUSED, void *cb)
     ++			       const struct config_context *ctx UNUSED,
     ++			       void *cb)
       {
       	struct string_list *values = cb;
       	struct string_list_item *item = string_list_insert(values, var);
     @@ builtin/difftool.c: static const char *const builtin_difftool_usage[] = {
       
      -static int difftool_config(const char *var, const char *value, void *cb)
      +static int difftool_config(const char *var, const char *value,
     -+			   struct key_value_info *kvi, void *cb)
     ++			   const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "difftool.trustexitcode")) {
       		trust_exit_code = git_config_bool(var, value);
     @@ builtin/difftool.c: static const char *const builtin_difftool_usage[] = {
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static int print_tool_help(void)
      
       ## builtin/fetch.c ##
     -@@ builtin/fetch.c: static int fetch_write_commit_graph = -1;
     - static int stdin_refspecs = 0;
     - static int negotiate_only;
     +@@ builtin/fetch.c: struct fetch_config {
     + 	enum display_format display_format;
     + };
       
      -static int git_fetch_config(const char *k, const char *v, void *cb)
      +static int git_fetch_config(const char *k, const char *v,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
     - 	if (!strcmp(k, "fetch.prune")) {
     - 		fetch_prune_config = git_config_bool(k, v);
     + 	struct fetch_config *fetch_config = cb;
     + 
      @@ builtin/fetch.c: static int git_fetch_config(const char *k, const char *v, void *cb)
     - 		return 0;
     + 			    "fetch.output", v);
       	}
       
      -	return git_default_config(k, v, cb);
     -+	return git_default_config(k, v,kvi, cb);
     ++	return git_default_config(k, v, ctx, cb);
       }
       
       static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
     @@ builtin/fetch.c: struct remote_group_data {
       
      -static int get_remote_group(const char *key, const char *value, void *priv)
      +static int get_remote_group(const char *key, const char *value,
     -+			    struct key_value_info *kvi UNUSED, void *priv)
     ++			    const struct config_context *ctx UNUSED,
     ++			    void *priv)
       {
       	struct remote_group_data *g = priv;
       
     @@ builtin/fsmonitor--daemon.c: static int fsmonitor__start_timeout_sec = 60;
       
      -static int fsmonitor_config(const char *var, const char *value, void *cb)
      +static int fsmonitor_config(const char *var, const char *value,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
       		int i = git_config_int(var, value);
     @@ builtin/fsmonitor--daemon.c: static int fsmonitor_config(const char *var, const
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       /*
     @@ builtin/grep.c: static int wait_all(void)
       
      -static int grep_cmd_config(const char *var, const char *value, void *cb)
      +static int grep_cmd_config(const char *var, const char *value,
     -+			   struct key_value_info *kvi, void *cb)
     ++			   const struct config_context *ctx, void *cb)
       {
     - 	int st = grep_config(var, value, cb);
     +-	int st = grep_config(var, value, cb);
     ++	int st = grep_config(var, value, ctx, cb);
       
       	if (git_color_config(var, value, cb) < 0)
       		st = -1;
      -	else if (git_default_config(var, value, cb) < 0)
     -+	else if (git_default_config(var, value,kvi, cb) < 0)
     ++	else if (git_default_config(var, value, ctx, cb) < 0)
       		st = -1;
       
       	if (!strcmp(var, "grep.threads")) {
     @@ builtin/help.c: static int add_man_viewer_info(const char *var, const char *valu
       
      -static int git_help_config(const char *var, const char *value, void *cb)
      +static int git_help_config(const char *var, const char *value,
     -+			   struct key_value_info *kvi, void *cb)
     ++			   const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "help.format")) {
       		if (!value)
     @@ builtin/help.c: static int git_help_config(const char *var, const char *value, v
       		return add_man_viewer_info(var, value);
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static struct cmdnames main_cmds, other_cmds;
     @@ builtin/index-pack.c: static void final(const char *final_pack_name, const char
       
      -static int git_index_pack_config(const char *k, const char *v, void *cb)
      +static int git_index_pack_config(const char *k, const char *v,
     -+				 struct key_value_info *kvi, void *cb)
     ++				 const struct config_context *ctx, void *cb)
       {
       	struct pack_idx_option *opts = cb;
       
     @@ builtin/index-pack.c: static int git_index_pack_config(const char *k, const char
       			opts->flags &= ~WRITE_REV;
       	}
      -	return git_default_config(k, v, cb);
     -+	return git_default_config(k, v,kvi, cb);
     ++	return git_default_config(k, v, ctx, cb);
       }
       
       static int cmp_uint32(const void *a_, const void *b_)
     @@ builtin/log.c: static int cmd_log_walk(struct rev_info *rev)
       
      -static int git_log_config(const char *var, const char *value, void *cb)
      +static int git_log_config(const char *var, const char *value,
     -+			  struct key_value_info *kvi, void *cb)
     ++			  const struct config_context *ctx, void *cb)
       {
       	const char *slot_name;
       
     @@ builtin/log.c: static int git_log_config(const char *var, const char *value, voi
       	}
       
      -	return git_diff_ui_config(var, value, cb);
     -+	return git_diff_ui_config(var, value,kvi, cb);
     ++	return git_diff_ui_config(var, value, ctx, cb);
       }
       
       int cmd_whatchanged(int argc, const char **argv, const char *prefix)
     @@ builtin/log.c: static enum cover_from_description parse_cover_from_description(c
       
      -static int git_format_config(const char *var, const char *value, void *cb)
      +static int git_format_config(const char *var, const char *value,
     -+			     struct key_value_info *kvi, void *cb)
     ++			     const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "format.headers")) {
       		if (!value)
     @@ builtin/log.c: static int git_format_config(const char *var, const char *value,
       		return 0;
       
      -	return git_log_config(var, value, cb);
     -+	return git_log_config(var, value,kvi, cb);
     ++	return git_log_config(var, value, ctx, cb);
       }
       
       static const char *output_directory = NULL;
     @@ builtin/merge.c: static void parse_branch_merge_options(char *bmo)
       
      -static int git_merge_config(const char *k, const char *v, void *cb)
      +static int git_merge_config(const char *k, const char *v,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	int status;
       	const char *str;
     @@ builtin/merge.c: static int git_merge_config(const char *k, const char *v, void
       	}
       
      -	status = fmt_merge_msg_config(k, v, cb);
     -+	status = fmt_merge_msg_config(k, v,kvi, cb);
     ++	status = fmt_merge_msg_config(k, v, ctx, cb);
       	if (status)
       		return status;
      -	return git_diff_ui_config(k, v, cb);
     -+	return git_diff_ui_config(k, v,kvi, cb);
     ++	return git_diff_ui_config(k, v, ctx, cb);
       }
       
       static int read_tree_trivial(struct object_id *common, struct object_id *head,
     @@ builtin/multi-pack-index.c: static struct option *add_common_options(struct opti
       }
       
       static int git_multi_pack_index_write_config(const char *var, const char *value,
     -+					     struct key_value_info *kvi UNUSED,
     ++					     const struct config_context *ctx UNUSED,
       					     void *cb UNUSED)
       {
       	if (!strcmp(var, "pack.writebitmaphashcache")) {
     @@ builtin/pack-objects.c: static void prepare_pack(int window, int depth)
       
      -static int git_pack_config(const char *k, const char *v, void *cb)
      +static int git_pack_config(const char *k, const char *v,
     -+			   struct key_value_info *kvi, void *cb)
     ++			   const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(k, "pack.window")) {
       		window = git_config_int(k, v);
     @@ builtin/pack-objects.c: static int git_pack_config(const char *k, const char *v,
       		oidmap_put(&configured_exclusions, ex);
       	}
      -	return git_default_config(k, v, cb);
     -+	return git_default_config(k, v,kvi, cb);
     ++	return git_default_config(k, v, ctx, cb);
       }
       
       /* Counters for trace2 output when in --stdin-packs mode. */
     @@ builtin/patch-id.c: struct patch_id_opts {
       
      -static int git_patch_id_config(const char *var, const char *value, void *cb)
      +static int git_patch_id_config(const char *var, const char *value,
     -+			       struct key_value_info *kvi, void *cb)
     ++			       const struct config_context *ctx, void *cb)
       {
       	struct patch_id_opts *opts = cb;
       
     @@ builtin/patch-id.c: static int git_patch_id_config(const char *var, const char *
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       int cmd_patch_id(int argc, const char **argv, const char *prefix)
     @@ builtin/pull.c: static enum rebase_type config_get_rebase(int *rebase_unspecifie
        */
      -static int git_pull_config(const char *var, const char *value, void *cb)
      +static int git_pull_config(const char *var, const char *value,
     -+			   struct key_value_info *kvi, void *cb)
     ++			   const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "rebase.autostash")) {
       		config_autostash = git_config_bool(var, value);
     @@ builtin/pull.c: static int git_pull_config(const char *var, const char *value, v
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       /**
     @@ builtin/push.c: static void set_push_cert_flags(int *flags, int v)
       
      -static int git_push_config(const char *k, const char *v, void *cb)
      +static int git_push_config(const char *k, const char *v,
     -+			   struct key_value_info *kvi, void *cb)
     ++			   const struct config_context *ctx, void *cb)
       {
       	const char *slot_name;
       	int *flags = cb;
     @@ builtin/push.c: static int git_push_config(const char *k, const char *v, void *c
       	}
       
      -	return git_default_config(k, v, NULL);
     -+	return git_default_config(k, v,kvi, NULL);
     ++	return git_default_config(k, v, ctx, NULL);
       }
       
       int cmd_push(int argc, const char **argv, const char *prefix)
     @@ builtin/read-tree.c: static int debug_merge(const struct cache_entry * const *st
       
      -static int git_read_tree_config(const char *var, const char *value, void *cb)
      +static int git_read_tree_config(const char *var, const char *value,
     -+				struct key_value_info *kvi, void *cb)
     ++				const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "submodule.recurse"))
       		return git_default_submodule_config(var, value, cb);
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
     @@ builtin/rebase.c: static void parse_rebase_merges_value(struct rebase_options *o
       
      -static int rebase_config(const char *var, const char *value, void *data)
      +static int rebase_config(const char *var, const char *value,
     -+			 struct key_value_info *kvi, void *data)
     ++			 const struct config_context *ctx, void *data)
       {
       	struct rebase_options *opts = data;
       
     @@ builtin/rebase.c: static int rebase_config(const char *var, const char *value, v
       	}
       
      -	return git_default_config(var, value, data);
     -+	return git_default_config(var, value,kvi, data);
     ++	return git_default_config(var, value, ctx, data);
       }
       
       static int checkout_up_to_date(struct rebase_options *options)
     @@ builtin/receive-pack.c: static enum deny_action parse_deny_action(const char *va
       
      -static int receive_pack_config(const char *var, const char *value, void *cb)
      +static int receive_pack_config(const char *var, const char *value,
     -+			       struct key_value_info *kvi, void *cb)
     ++			       const struct config_context *ctx, void *cb)
       {
       	int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
       
     @@ builtin/receive-pack.c: static int receive_pack_config(const char *var, const ch
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static void show_ref(const char *path, const struct object_id *oid)
     @@ builtin/reflog.c: static struct reflog_expire_cfg *find_cfg_ent(const char *patt
       
      -static int reflog_expire_config(const char *var, const char *value, void *cb)
      +static int reflog_expire_config(const char *var, const char *value,
     -+				struct key_value_info *kvi, void *cb)
     ++				const struct config_context *ctx, void *cb)
       {
       	const char *pattern, *key;
       	size_t pattern_len;
     @@ builtin/reflog.c: static int reflog_expire_config(const char *var, const char *v
       
       	if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
      -		return git_default_config(var, value, cb);
     -+		return git_default_config(var, value,kvi, cb);
     ++		return git_default_config(var, value, ctx, cb);
       
       	if (!strcmp(key, "reflogexpire")) {
       		slot = EXPIRE_TOTAL;
     @@ builtin/reflog.c: static int reflog_expire_config(const char *var, const char *v
       			return -1;
       	} else
      -		return git_default_config(var, value, cb);
     -+		return git_default_config(var, value,kvi, cb);
     ++		return git_default_config(var, value, ctx, cb);
       
       	if (!pattern) {
       		switch (slot) {
     @@ builtin/remote.c: static const char *abbrev_ref(const char *name, const char *pr
       #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
       
       static int config_read_branches(const char *key, const char *value,
     -+				struct key_value_info *kvi UNUSED,
     ++				const struct config_context *ctx UNUSED,
       				void *data UNUSED)
       {
       	const char *orig_key = key;
     @@ builtin/remote.c: struct push_default_info
       
       static int config_read_push_default(const char *key, const char *value,
      -	void *cb)
     -+	struct key_value_info *kvi UNUSED, void *cb)
     ++	const struct config_context *ctx UNUSED, void *cb)
       {
       	struct push_default_info* info = cb;
       	if (strcmp(key, "remote.pushdefault") ||
     @@ builtin/remote.c: static int prune(int argc, const char **argv, const char *pref
       
      -static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
      +static int get_remote_default(const char *key, const char *value UNUSED,
     -+			      struct key_value_info *kvi UNUSED, void *priv)
     ++			      const struct config_context *ctx UNUSED,
     ++			      void *priv)
       {
       	if (strcmp(key, "remotes.default") == 0) {
       		int *found = priv;
     @@ builtin/repack.c: struct pack_objects_args {
       
      -static int repack_config(const char *var, const char *value, void *cb)
      +static int repack_config(const char *var, const char *value,
     -+			 struct key_value_info *kvi, void *cb)
     ++			 const struct config_context *ctx, void *cb)
       {
       	struct pack_objects_args *cruft_po_args = cb;
       	if (!strcmp(var, "repack.usedeltabaseoffset")) {
     @@ builtin/repack.c: static int repack_config(const char *var, const char *value, v
       	if (!strcmp(var, "repack.cruftthreads"))
       		return git_config_string(&cruft_po_args->threads, var, value);
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       /*
     @@ builtin/reset.c: static int reset_refs(const char *rev, const struct object_id *
       
      -static int git_reset_config(const char *var, const char *value, void *cb)
      +static int git_reset_config(const char *var, const char *value,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "submodule.recurse"))
       		return git_default_submodule_config(var, value, cb);
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       int cmd_reset(int argc, const char **argv, const char *prefix)
     @@ builtin/send-pack.c: static void print_helper_status(struct ref *ref)
       
      -static int send_pack_config(const char *k, const char *v, void *cb)
      +static int send_pack_config(const char *k, const char *v,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(k, "push.gpgsign")) {
       		const char *value;
     @@ builtin/send-pack.c: static int send_pack_config(const char *k, const char *v, v
       		}
       	}
      -	return git_default_config(k, v, cb);
     -+	return git_default_config(k, v,kvi, cb);
     ++	return git_default_config(k, v, ctx, cb);
       }
       
       int cmd_send_pack(int argc, const char **argv, const char *prefix)
     @@ builtin/show-branch.c: static void append_one_rev(const char *av)
       
      -static int git_show_branch_config(const char *var, const char *value, void *cb)
      +static int git_show_branch_config(const char *var, const char *value,
     -+				  struct key_value_info *kvi, void *cb)
     ++				  const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "showbranch.default")) {
       		if (!value)
     @@ builtin/show-branch.c: static int git_show_branch_config(const char *var, const
       		return -1;
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
     @@ builtin/stash.c: static int show_stat = 1;
       
      -static int git_stash_config(const char *var, const char *value, void *cb)
      +static int git_stash_config(const char *var, const char *value,
     -+			    struct key_value_info *kvi, void *cb)
     ++			    const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "stash.showstat")) {
       		show_stat = git_config_bool(var, value);
     @@ builtin/stash.c: static int git_stash_config(const char *var, const char *value,
       		return 0;
       	}
      -	return git_diff_basic_config(var, value, cb);
     -+	return git_diff_basic_config(var, value,kvi, cb);
     ++	return git_diff_basic_config(var, value, ctx, cb);
       }
       
       static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
     @@ builtin/submodule--helper.c: static int update_clone_task_finished(int result,
       }
       
       static int git_update_clone_config(const char *var, const char *value,
     -+				   struct key_value_info *kvi UNUSED,
     ++				   const struct config_context *ctx UNUSED,
       				   void *cb)
       {
       	int *max_jobs = cb;
     @@ builtin/tag.c: static const char tag_template_nocleanup[] =
       
      -static int git_tag_config(const char *var, const char *value, void *cb)
      +static int git_tag_config(const char *var, const char *value,
     -+			  struct key_value_info *kvi, void *cb)
     ++			  const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "tag.gpgsign")) {
       		config_sign_tag = git_config_bool(var, value);
     @@ builtin/tag.c: static int git_tag_config(const char *var, const char *value, voi
       		return -1;
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static void write_tag_body(int fd, const struct object_id *oid)
     @@ builtin/var.c: static const struct git_var *get_git_var(const char *var)
       
      -static int show_config(const char *var, const char *value, void *cb)
      +static int show_config(const char *var, const char *value,
     -+		       struct key_value_info *kvi, void *cb)
     ++		       const struct config_context *ctx, void *cb)
       {
       	if (value)
       		printf("%s=%s\n", var, value);
       	else
       		printf("%s\n", var);
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
     @@ builtin/worktree.c: static int verbose;
       
      -static int git_worktree_config(const char *var, const char *value, void *cb)
      +static int git_worktree_config(const char *var, const char *value,
     -+			       struct key_value_info *kvi, void *cb)
     ++			       const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "worktree.guessremote")) {
       		guess_remote = git_config_bool(var, value);
     @@ builtin/worktree.c: static int verbose;
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static int delete_git_dir(const char *id)
     @@ bundle-uri.c: static int bundle_list_update(const char *key, const char *value,
       
      -static int config_to_bundle_list(const char *key, const char *value, void *data)
      +static int config_to_bundle_list(const char *key, const char *value,
     -+				 struct key_value_info *kvi UNUSED,
     ++				 const struct config_context *ctx UNUSED,
      +				 void *data)
       {
       	struct bundle_list *list = data;
     @@ bundle-uri.c: cached:
       
      -static int config_to_packet_line(const char *key, const char *value, void *data)
      +static int config_to_packet_line(const char *key, const char *value,
     -+				 struct key_value_info *kvi UNUSED,
     ++				 const struct config_context *ctx UNUSED,
      +				 void *data)
       {
       	struct packet_reader *writer = data;
       
      
     + ## compat/mingw.c ##
     +@@ compat/mingw.c: static int core_restrict_inherited_handles = -1;
     + static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
     + static char *unset_environment_variables;
     + 
     +-int mingw_core_config(const char *var, const char *value, void *cb)
     ++int mingw_core_config(const char *var, const char *value,
     ++		      const struct config_context *ctx, void *cb)
     + {
     + 	if (!strcmp(var, "core.hidedotfiles")) {
     + 		if (value && !strcasecmp(value, "dotgitonly"))
     +
     + ## compat/mingw.h ##
     +@@ compat/mingw.h: typedef _sigset_t sigset_t;
     + #undef _POSIX_THREAD_SAFE_FUNCTIONS
     + #endif
     + 
     +-int mingw_core_config(const char *var, const char *value, void *cb);
     ++struct config_context;
     ++int mingw_core_config(const char *var, const char *value,
     ++		      const struct config_context *ctx, void *cb);
     + #define platform_core_config mingw_core_config
     + 
     + /*
     +
       ## config.c ##
      @@ config.c: struct config_include_data {
       };
     @@ config.c: struct config_include_data {
       
      -static int git_config_include(const char *var, const char *value, void *data);
      +static int git_config_include(const char *var, const char *value,
     -+			      struct key_value_info *kvi, void *data);
     ++			      const struct config_context *ctx, void *data);
       
       #define MAX_INCLUDE_DEPTH 10
       static const char include_depth_advice[] = N_(
     @@ config.c: static int include_by_branch(const char *cond, size_t cond_len)
       
      -static int add_remote_url(const char *var, const char *value, void *data)
      +static int add_remote_url(const char *var, const char *value,
     -+			  struct key_value_info *kvi UNUSED, void *data)
     ++			  const struct config_context *ctx UNUSED, void *data)
       {
       	struct string_list *remote_urls = data;
       	const char *remote_name;
     @@ config.c: static void populate_remote_urls(struct config_include_data *inc)
       }
       
       static int forbid_remote_url(const char *var, const char *value UNUSED,
     -+			     struct key_value_info *kvi UNUSED,
     ++			     const struct config_context *ctx UNUSED,
       			     void *data UNUSED)
       {
       	const char *remote_name;
     @@ config.c: static int include_condition_is_true(struct config_source *cs,
       
      -static int git_config_include(const char *var, const char *value, void *data)
      +static int git_config_include(const char *var, const char *value,
     -+			      struct key_value_info *kvi UNUSED, void *data)
     ++			      const struct config_context *ctx,
     ++			      void *data)
       {
       	struct config_include_data *inc = data;
       	struct config_source *cs = inc->config_reader->source;
     +@@ config.c: 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, NULL, inc->data);
     + 	if (ret < 0)
     + 		return ret;
     + 
     +@@ config.c: static int config_parse_pair(const char *key, const char *value,
     + 	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, NULL, data) < 0) ? -1 : 0;
     + 	free(canonical_name);
     + 	return ret;
     + }
     +@@ config.c: 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);
     ++	ret = fn(name->buf, value, NULL, data);
     + 	if (ret >= 0)
     + 		cs->linenr++;
     + 	return ret;
      @@ config.c: 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,
     -+				   struct key_value_info *kvi, void *cb)
     ++				   const struct config_context *ctx, void *cb)
       {
       	/* This needs a better name */
       	if (!strcmp(var, "core.filemode")) {
     @@ config.c: static int git_default_core_config(const char *var, const char *value,
       
       	/* Add other config variables here and to Documentation/config.txt. */
      -	return platform_core_config(var, value, cb);
     -+	return platform_core_config(var, value,kvi, cb);
     ++	return platform_core_config(var, value, ctx, cb);
       }
       
       static int git_default_sparse_config(const char *var, const char *value)
     @@ config.c: static int git_default_mailmap_config(const char *var, const char *val
       
      -int git_default_config(const char *var, const char *value, void *cb)
      +int git_default_config(const char *var, const char *value,
     -+		       struct key_value_info *kvi, void *cb)
     ++		       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,kvi, 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,kvi, cb);
     ++		return git_ident_config(var, value, ctx, cb);
       
       	if (starts_with(var, "i18n."))
       		return git_default_i18n_config(var, value);
     +@@ config.c: static void configset_iter(struct config_reader *reader, struct config_set *set,
     + 
     + 		config_reader_set_kvi(reader, values->items[value_index].util);
     + 
     +-		if (fn(entry->key, values->items[value_index].string, data) < 0)
     ++		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
     + 			git_die_config_linenr(entry->key,
     + 					      reader->config_kvi->filename,
     + 					      reader->config_kvi->linenr);
      @@ config.c: struct configset_add_data {
       };
       #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,
     -+			       struct key_value_info *kvi UNUSED, void *cb)
     ++			       const struct config_context *ctx UNUSED,
     ++			       void *cb)
       {
       	struct configset_add_data *data = cb;
       	configset_add_value(data->config_reader, data->config_set, key, value);
     @@ config.c: static int store_aux_event(enum config_event_t type,
       
      -static int store_aux(const char *key, const char *value, void *cb)
      +static int store_aux(const char *key, const char *value,
     -+		     struct key_value_info *kvi UNUSED, void *cb)
     ++		     const struct config_context *ctx UNUSED, void *cb)
       {
       	struct config_store_data *store = cb;
       
      
       ## config.h ##
     -@@ config.h: struct key_value_info {
     +@@ config.h: struct config_options {
     + 	} error_action;
     + };
     + 
     ++/* Config source metadata for a given config key-value pair */
     ++struct key_value_info {
     ++	const char *filename;
     ++	int linenr;
     ++	enum config_origin_type origin_type;
     ++	enum config_scope scope;
     ++};
     ++#define KVI_INIT { \
     ++	.filename = NULL, \
     ++	.linenr = -1, \
     ++	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
     ++	.scope = CONFIG_SCOPE_UNKNOWN, \
     ++}
     ++
     ++/* Captures additional information that a config callback can use. */
     ++struct config_context {
     ++	/* Config source metadata for key and value. */
     ++	const struct key_value_info *kvi;
     ++};
     ++#define CONFIG_CONTEXT_INIT { 0 }
     ++
     + /**
     +- * A config callback function takes three parameters:
     ++ * A config callback function takes four parameters:
     +  *
     +  * - the name of the parsed variable. This is in canonical "flat" form: the
     +  *   section, subsection, and variable segments will be separated by dots,
     +@@ config.h: struct config_options {
     +  *   value specified, the value will be NULL (typically this means it
     +  *   should be interpreted as boolean true).
     +  *
     ++ * - the 'config context', that is, additional information about the config
     ++ *   iteration operation provided by the config machinery. For example, this
     ++ *   includes information about the config source being parsed (e.g. the
     ++ *   filename).
     ++ *
     +  * - a void pointer passed in by the caller of the config API; this can
     +  *   contain callback-specific data
     +  *
     +  * A config callback should return 0 for success, or -1 if the variable
     +  * could not be parsed properly.
        */
     - typedef int (*config_fn_t)(const char *, const char *, struct key_value_info *, void *);
     +-typedef int (*config_fn_t)(const char *, const char *, void *);
     ++typedef int (*config_fn_t)(const char *, const char *,
     ++			   const struct config_context *, void *);
       
      -int git_default_config(const char *, const char *, void *);
     -+int git_default_config(const char *, const char *, struct key_value_info *,
     -+		       void *);
     ++int git_default_config(const char *, const char *,
     ++		       const struct config_context *, void *);
       
       /**
        * Read a specific file in git-config format.
     +@@ config.h: int git_config_get_expiry(const char *key, const char **output);
     + /* parse either "this many days" integer, or "5.days.ago" approxidate */
     + int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
     + 
     +-struct key_value_info {
     +-	const char *filename;
     +-	int linenr;
     +-	enum config_origin_type origin_type;
     +-	enum config_scope scope;
     +-};
     +-
     + /**
     +  * First prints the error message specified by the caller in `err` and then
     +  * dies printing the line number and the file name of the highest priority
      
       ## connect.c ##
      @@ connect.c: static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
     @@ connect.c: static struct child_process *git_tcp_connect(int fd[2], char *host, i
       
       static int git_proxy_command_options(const char *var, const char *value,
      -		void *cb)
     -+		struct key_value_info *kvi, void *cb)
     ++		const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "core.gitproxy")) {
       		const char *for_pos;
     @@ connect.c: static int git_proxy_command_options(const char *var, const char *val
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static int git_use_proxy(const char *host)
      
     + ## contrib/coccinelle/config_fn_ctx.pending.cocci (new) ##
     +@@
     ++@ get_fn @
     ++identifier fn, R;
     ++@@
     ++(
     ++(
     ++git_config_from_file
     ++|
     ++git_config_from_file_with_options
     ++|
     ++git_config_from_mem
     ++|
     ++git_config_from_blob_oid
     ++|
     ++read_early_config
     ++|
     ++read_very_early_config
     ++|
     ++config_with_options
     ++|
     ++git_config
     ++|
     ++git_protected_config
     ++|
     ++config_from_gitmodules
     ++)
     ++  (fn, ...)
     ++|
     ++repo_config(R, fn, ...)
     ++)
     ++
     ++@ extends get_fn @
     ++identifier C1, C2, D;
     ++@@
     ++int fn(const char *C1, const char *C2,
     +++ const struct config_context *ctx,
     ++  void *D);
     ++
     ++@ extends get_fn @
     ++@@
     ++int fn(const char *, const char *,
     +++ const struct config_context *,
     ++  void *);
     ++
     ++@ extends get_fn @
     ++// Don't change fns that look like callback fns but aren't
     ++identifier fn2 != tar_filter_config && != git_diff_heuristic_config &&
     ++  != git_default_submodule_config && != git_color_config &&
     ++  != bundle_list_update && != parse_object_filter_config;
     ++identifier C1, C2, D1, D2, S;
     ++attribute name UNUSED;
     ++@@
     ++int fn(const char *C1, const char *C2,
     +++ const struct config_context *ctx,
     ++  void *D1) {
     ++<+...
     ++(
     ++fn2(C1, C2
     +++ , ctx
     ++, D2);
     ++|
     ++if(fn2(C1, C2
     +++ , ctx
     ++, D2) < 0) { ... }
     ++|
     ++return fn2(C1, C2
     +++ , ctx
     ++, D2);
     ++|
     ++S = fn2(C1, C2
     +++ , ctx
     ++, D2);
     ++)
     ++...+>
     ++  }
     ++
     ++@ extends get_fn@
     ++identifier C1, C2, D;
     ++attribute name UNUSED;
     ++@@
     ++int fn(const char *C1, const char *C2,
     +++ const struct config_context *ctx UNUSED,
     ++  void *D) {...}
     ++
     ++
     ++// The previous rules don't catch all callbacks, especially if they're defined
     ++// in a separate file from the git_config() call. Fix these manually.
     ++@@
     ++identifier C1, C2, D;
     ++attribute name UNUSED;
     ++@@
     ++int
     ++(
     ++git_ident_config
     ++|
     ++urlmatch_collect_fn
     ++|
     ++write_one_config
     ++|
     ++forbid_remote_url
     ++|
     ++credential_config_callback
     ++)
     ++  (const char *C1, const char *C2,
     +++ const struct config_context *ctx UNUSED,
     ++  void *D) {...}
     ++
     ++@@
     ++identifier C1, C2, D, D2, S, fn2;
     ++@@
     ++int
     ++(
     ++http_options
     ++|
     ++git_status_config
     ++|
     ++git_commit_config
     ++|
     ++git_default_core_config
     ++|
     ++grep_config
     ++)
     ++  (const char *C1, const char *C2,
     +++ const struct config_context *ctx,
     ++  void *D) {
     ++<+...
     ++(
     ++fn2(C1, C2
     +++ , ctx
     ++, D2);
     ++|
     ++if(fn2(C1, C2
     +++ , ctx
     ++, D2) < 0) { ... }
     ++|
     ++return fn2(C1, C2
     +++ , ctx
     ++, D2);
     ++|
     ++S = fn2(C1, C2
     +++ , ctx
     ++, D2);
     ++)
     ++...+>
     ++  }
     +
       ## convert.c ##
      @@ convert.c: static int apply_filter(const char *path, const char *src, size_t len,
       	return 0;
     @@ convert.c: static int apply_filter(const char *path, const char *src, size_t len
       
      -static int read_convert_config(const char *var, const char *value, void *cb UNUSED)
      +static int read_convert_config(const char *var, const char *value,
     -+			       struct key_value_info *kvi UNUSED,
     ++			       const struct config_context *ctx UNUSED,
      +			       void *cb UNUSED)
       {
       	const char *key, *name;
     @@ credential.c: static int credential_from_potentially_partial_url(struct credenti
       						   const char *url);
       
       static int credential_config_callback(const char *var, const char *value,
     -+				      struct key_value_info *kvi UNUSED,
     ++				      const struct config_context *ctx UNUSED,
       				      void *data)
       {
       	struct credential *c = data;
     @@ delta-islands.c: static void free_remote_islands(kh_str_t *remote_islands)
       
      -static int island_config_callback(const char *k, const char *v, void *cb)
      +static int island_config_callback(const char *k, const char *v,
     -+				  struct key_value_info *kvi UNUSED, void *cb)
     ++				  const struct config_context *ctx UNUSED,
     ++				  void *cb)
       {
       	struct island_load_data *ild = cb;
       
     @@ diff.c: static unsigned parse_color_moved_ws(const char *arg)
       
      -int git_diff_ui_config(const char *var, const char *value, void *cb)
      +int git_diff_ui_config(const char *var, const char *value,
     -+		       struct key_value_info *kvi, void *cb)
     ++		       const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
       		diff_use_color_default = git_config_colorbool(var, value);
     @@ diff.c: int git_diff_ui_config(const char *var, const char *value, void *cb)
       		return -1;
       
      -	return git_diff_basic_config(var, value, cb);
     -+	return git_diff_basic_config(var, value,kvi, cb);
     ++	return git_diff_basic_config(var, value, ctx, cb);
       }
       
      -int git_diff_basic_config(const char *var, const char *value, void *cb)
      +int git_diff_basic_config(const char *var, const char *value,
     -+			  struct key_value_info *kvi, void *cb)
     ++			  const struct config_context *ctx, void *cb)
       {
       	const char *name;
       
     @@ diff.c: int git_diff_basic_config(const char *var, const char *value, void *cb)
       		return -1;
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static char *quote_two(const char *one, const char *two)
     @@ diff.h: void free_diffstat_info(struct diffstat_t *diffstat);
       		   const char **optarg);
       
      -int git_diff_basic_config(const char *var, const char *value, void *cb);
     ++struct config_context;
      +int git_diff_basic_config(const char *var, const char *value,
     -+			  struct key_value_info *kvi, void *cb);
     ++			  const struct config_context *ctx, void *cb);
       int git_diff_heuristic_config(const char *var, const char *value, void *cb);
       void init_diff_ui_defaults(void);
      -int git_diff_ui_config(const char *var, const char *value, void *cb);
      +int git_diff_ui_config(const char *var, const char *value,
     -+		       struct key_value_info *kvi, void *cb);
     ++		       const struct config_context *ctx, void *cb);
       void repo_diff_setup(struct repository *, struct diff_options *);
       struct option *add_diff_options(const struct option *, struct diff_options *);
       int diff_opt_parse(struct diff_options *, const char **, int, const char *);
     @@ fetch-pack.c: static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
       
      -static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
      +static int fetch_pack_config_cb(const char *var, const char *value,
     -+				struct key_value_info *kvi, void *cb)
     ++				const struct config_context *ctx, void *cb)
       {
       	if (strcmp(var, "fetch.fsck.skiplist") == 0) {
       		const char *path;
     @@ fetch-pack.c: static int fetch_pack_config_cb(const char *var, const char *value
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       static void fetch_pack_config(void)
     @@ fmt-merge-msg.c: static int use_branch_desc;
       
      -int fmt_merge_msg_config(const char *key, const char *value, void *cb)
      +int fmt_merge_msg_config(const char *key, const char *value,
     -+			 struct key_value_info *kvi, void *cb)
     ++			 const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
       		int is_bool;
     @@ fmt-merge-msg.c: int fmt_merge_msg_config(const char *key, const char *value, vo
       		suppress_dest_pattern_seen = 1;
       	} else {
      -		return git_default_config(key, value, cb);
     -+		return git_default_config(key, value,kvi, cb);
     ++		return git_default_config(key, value, ctx, cb);
       	}
       	return 0;
       }
     @@ fmt-merge-msg.h: struct fmt_merge_msg_opts {
       extern int merge_log_config;
      -int fmt_merge_msg_config(const char *key, const char *value, void *cb);
      +int fmt_merge_msg_config(const char *key, const char *value,
     -+			 struct key_value_info *kvi, void *cb);
     ++			 const struct config_context *ctx, void *cb);
       int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
       		  struct fmt_merge_msg_opts *);
       
     @@ fsck.c: struct fsck_gitmodules_data {
       
      -static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
      +static int fsck_gitmodules_fn(const char *var, const char *value,
     -+			      struct key_value_info *kvi UNUSED, void *vdata)
     ++			      const struct config_context *ctx UNUSED,
     ++			      void *vdata)
       {
       	struct fsck_gitmodules_data *data = vdata;
       	const char *subsection, *key;
     @@ fsck.c: int fsck_finish(struct fsck_options *options)
       
      -int git_fsck_config(const char *var, const char *value, void *cb)
      +int git_fsck_config(const char *var, const char *value,
     -+		    struct key_value_info *kvi, void *cb)
     ++		    const struct config_context *ctx, void *cb)
       {
       	struct fsck_options *options = cb;
       	if (strcmp(var, "fsck.skiplist") == 0) {
     @@ fsck.c: int git_fsck_config(const char *var, const char *value, void *cb)
       	}
       
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
       
       /*
      
       ## fsck.h ##
     -@@ fsck.h: const char *fsck_describe_object(struct fsck_options *options,
     +@@ fsck.h: void fsck_put_object_name(struct fsck_options *options,
     + const char *fsck_describe_object(struct fsck_options *options,
     + 				 const struct object_id *oid);
     + 
     ++struct key_value_info;
     + /*
        * git_config() callback for use by fsck-y tools that want to support
        * fsck.<msg> fsck.skipList etc.
        */
      -int git_fsck_config(const char *var, const char *value, void *cb);
      +int git_fsck_config(const char *var, const char *value,
     -+		    struct key_value_info *kvi, void *cb);
     ++		    const struct config_context *ctx, void *cb);
       
       #endif
      
     + ## git-compat-util.h ##
     +@@ git-compat-util.h: typedef uintmax_t timestamp_t;
     + #endif
     + 
     + #ifndef platform_core_config
     ++struct config_context;
     + static inline int noop_core_config(const char *var UNUSED,
     + 				   const char *value UNUSED,
     ++				   const struct config_context *ctx UNUSED,
     + 				   void *cb UNUSED)
     + {
     + 	return 0;
     +
       ## gpg-interface.c ##
      @@
       #include "alias.h"
       #include "wrapper.h"
       
      -static int git_gpg_config(const char *, const char *, void *);
     -+static int git_gpg_config(const char *, const char *, struct key_value_info *,
     -+			  void *);
     ++static int git_gpg_config(const char *, const char *,
     ++			  const struct config_context *, void *);
       
       static void gpg_interface_lazy_init(void)
       {
     @@ gpg-interface.c: void set_signing_key(const char *key)
       
      -static int git_gpg_config(const char *var, const char *value, void *cb UNUSED)
      +static int git_gpg_config(const char *var, const char *value,
     -+			  struct key_value_info *kvi UNUSED, void *cb UNUSED)
     ++			  const struct config_context *ctx UNUSED,
     ++			  void *cb UNUSED)
       {
       	struct gpg_format *fmt = NULL;
       	char *fmtname = NULL;
     @@ grep.c: define_list_config_array_extra(color_grep_slots, {"match"});
        */
      -int grep_config(const char *var, const char *value, void *cb)
      +int grep_config(const char *var, const char *value,
     -+		struct key_value_info *kvi UNUSED, void *cb)
     ++		const struct config_context *ctx, void *cb)
       {
       	struct grep_opt *opt = cb;
       	const char *slot;
     +@@ grep.c: int grep_config(const char *var, const char *value, void *cb)
     + 	if (!strcmp(var, "color.grep"))
     + 		opt->color = git_config_colorbool(var, value);
     + 	if (!strcmp(var, "color.grep.match")) {
     +-		if (grep_config("color.grep.matchcontext", value, cb) < 0)
     ++		if (grep_config("color.grep.matchcontext", value, ctx, cb) < 0)
     + 			return -1;
     +-		if (grep_config("color.grep.matchselected", value, cb) < 0)
     ++		if (grep_config("color.grep.matchselected", value, ctx, cb) < 0)
     + 			return -1;
     + 	} else if (skip_prefix(var, "color.grep.", &slot)) {
     + 		int i = LOOKUP_CONFIG(color_grep_slots, slot);
     +
     + ## grep.h ##
     +@@ grep.h: struct grep_opt {
     + 	.output = std_output, \
     + }
     + 
     +-int grep_config(const char *var, const char *value, void *);
     ++struct config_context;
     ++int grep_config(const char *var, const char *value,
     ++		const struct config_context *ctx, void *data);
     + void grep_init(struct grep_opt *, struct repository *repo);
     + 
     + void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
      
       ## help.c ##
      @@ help.c: void load_command_list(const char *prefix,
     @@ help.c: void load_command_list(const char *prefix,
       
      -static int get_colopts(const char *var, const char *value, void *data)
      +static int get_colopts(const char *var, const char *value,
     -+		       struct key_value_info *kvi UNUSED, void *data)
     ++		       const struct config_context *ctx UNUSED, void *data)
       {
       	unsigned int *colopts = data;
       
     @@ help.c: void list_developer_interfaces_help(void)
       
      -static int get_alias(const char *var, const char *value, void *data)
      +static int get_alias(const char *var, const char *value,
     -+		     struct key_value_info *kvi UNUSED, void *data)
     ++		     const struct config_context *ctx UNUSED, void *data)
       {
       	struct string_list *list = data;
       
     @@ help.c: static struct cmdnames aliases;
       #define AUTOCORRECT_IMMEDIATELY (-1)
       
       static int git_unknown_cmd_config(const char *var, const char *value,
     -+				  struct key_value_info *kvi UNUSED,
     ++				  const struct config_context *ctx UNUSED,
       				  void *cb UNUSED)
       {
       	const char *p;
      
     + ## http.c ##
     +@@ http.c: static void process_curl_messages(void)
     + 	}
     + }
     + 
     +-static int http_options(const char *var, const char *value, void *cb)
     ++static int http_options(const char *var, const char *value,
     ++			const struct config_context *ctx, void *data)
     + {
     + 	if (!strcmp("http.version", var)) {
     + 		return git_config_string(&curl_http_version, var, value);
     +@@ http.c: static int http_options(const char *var, const char *value, void *cb)
     + 	}
     + 
     + 	/* Fall back on the default ones */
     +-	return git_default_config(var, value, cb);
     ++	return git_default_config(var, value, ctx, data);
     + }
     + 
     + static int curl_empty_auth_enabled(void)
     +
       ## ident.c ##
      @@ ident.c: static int set_ident(const char *var, const char *value)
       	return 0;
     @@ ident.c: static int set_ident(const char *var, const char *value)
       
      -int git_ident_config(const char *var, const char *value, void *data UNUSED)
      +int git_ident_config(const char *var, const char *value,
     -+		     struct key_value_info *kvi UNUSED, void *data UNUSED)
     ++		     const struct config_context *ctx UNUSED,
     ++		     void *data UNUSED)
       {
       	if (!strcmp(var, "user.useconfigonly")) {
       		ident_use_config_only = git_config_bool(var, value);
     @@ ident.h: const char *fmt_name(enum want_ident);
       int author_ident_sufficiently_given(void);
       
      -int git_ident_config(const char *, const char *, void *);
     -+int git_ident_config(const char *, const char *,
     -+		     struct key_value_info *UNUSED, void *);
     ++struct config_context;
     ++int git_ident_config(const char *, const char *, const struct config_context *,
     ++		     void *);
       
       #endif
      
     @@ imap-send.c: static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, i
       
      -static int git_imap_config(const char *var, const char *val, void *cb)
      +static int git_imap_config(const char *var, const char *val,
     -+			   struct key_value_info *kvi, void *cb)
     ++			   const struct config_context *ctx, void *cb)
       {
       
       	if (!strcmp("imap.sslverify", var))
     @@ imap-send.c: static int git_imap_config(const char *var, const char *val, void *
       		}
       	} else
      -		return git_default_config(var, val, cb);
     -+		return git_default_config(var, val,kvi, cb);
     ++		return git_default_config(var, val, ctx, cb);
       
       	return 0;
       }
     @@ ll-merge.c: static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
       static const char *default_ll_merge;
       
       static int read_merge_config(const char *var, const char *value,
     -+			     struct key_value_info *kvi UNUSED,
     ++			     const struct config_context *ctx UNUSED,
       			     void *cb UNUSED)
       {
       	struct ll_merge_driver *fn;
     @@ ls-refs.c: static void send_possibly_unborn_head(struct ls_refs_data *data)
       }
       
       static int ls_refs_config(const char *var, const char *value,
     --			  void *cb_data)
     -+			  struct key_value_info *kvi UNUSED, void *cb_data)
     ++			  const struct config_context *ctx UNUSED,
     + 			  void *cb_data)
       {
       	struct ls_refs_data *data = cb_data;
     - 	/*
      
       ## mailinfo.c ##
      @@ mailinfo.c: int mailinfo_parse_quoted_cr_action(const char *actionstr, int *action)
     @@ mailinfo.c: int mailinfo_parse_quoted_cr_action(const char *actionstr, int *acti
       
      -static int git_mailinfo_config(const char *var, const char *value, void *mi_)
      +static int git_mailinfo_config(const char *var, const char *value,
     -+			       struct key_value_info *kvi, void *mi_)
     ++			       const struct config_context *ctx, void *mi_)
       {
       	struct mailinfo *mi = mi_;
       
       	if (!starts_with(var, "mailinfo."))
      -		return git_default_config(var, value, NULL);
     -+		return git_default_config(var, value,kvi, NULL);
     ++		return git_default_config(var, value, ctx, NULL);
       	if (!strcmp(var, "mailinfo.scissors")) {
       		mi->use_scissors = git_config_bool(var, value);
       		return 0;
     @@ notes-utils.c: static combine_notes_fn parse_combine_notes_fn(const char *v)
       
      -static int notes_rewrite_config(const char *k, const char *v, void *cb)
      +static int notes_rewrite_config(const char *k, const char *v,
     -+				struct key_value_info *kvi UNUSED, void *cb)
     ++				const struct config_context *ctx UNUSED,
     ++				void *cb)
       {
       	struct notes_rewrite_cfg *c = cb;
       	if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
     @@ notes.c: void string_list_add_refs_from_colon_sep(struct string_list *list,
       
      -static int notes_display_config(const char *k, const char *v, void *cb)
      +static int notes_display_config(const char *k, const char *v,
     -+				struct key_value_info *kvi UNUSED, void *cb)
     ++				const struct config_context *ctx UNUSED,
     ++				void *cb)
       {
       	int *load_refs = cb;
       
     @@ pager.c: static void wait_for_pager_signal(int signo)
       }
       
       static int core_pager_config(const char *var, const char *value,
     -+			     struct key_value_info *kvi UNUSED,
     ++			     const struct config_context *ctx UNUSED,
       			     void *data UNUSED)
       {
       	if (!strcmp(var, "core.pager"))
     @@ pager.c: struct pager_command_config_data {
       
      -static int pager_command_config(const char *var, const char *value, void *vdata)
      +static int pager_command_config(const char *var, const char *value,
     -+				struct key_value_info *kvi UNUSED,
     ++				const struct config_context *ctx UNUSED,
      +				void *vdata)
       {
       	struct pager_command_config_data *data = vdata;
     @@ pretty.c: static void save_user_format(struct rev_info *rev, const char *cp, int
       }
       
       static int git_pretty_formats_config(const char *var, const char *value,
     -+				     struct key_value_info *kvi UNUSED,
     ++				     const struct config_context *ctx UNUSED,
       				     void *cb UNUSED)
       {
       	struct cmt_fmt_map *commit_format = NULL;
     @@ promisor-remote.c: static void promisor_remote_move_to_tail(struct promisor_remo
       
      -static int promisor_remote_config(const char *var, const char *value, void *data)
      +static int promisor_remote_config(const char *var, const char *value,
     -+				  struct key_value_info *kvi UNUSED,
     ++				  const struct config_context *ctx UNUSED,
      +				  void *data)
       {
       	struct promisor_remote_config *config = data;
     @@ remote.c: static void read_branches_file(struct remote_state *remote_state,
       
      -static int handle_config(const char *key, const char *value, void *cb)
      +static int handle_config(const char *key, const char *value,
     -+			 struct key_value_info *kvi UNUSED, void *cb)
     ++			 const struct config_context *ctx UNUSED, void *cb)
       {
       	const char *name;
       	size_t namelen;
     @@ revision.c: struct exclude_hidden_refs_cb {
       
      -static int hide_refs_config(const char *var, const char *value, void *cb_data)
      +static int hide_refs_config(const char *var, const char *value,
     -+			    struct key_value_info *kvi UNUSED, void *cb_data)
     ++			    const struct config_context *ctx UNUSED,
     ++			    void *cb_data)
       {
       	struct exclude_hidden_refs_cb *cb = cb_data;
       	cb->exclusions->hidden_refs_configured = 1;
     @@ scalar.c: static int cmd_register(int argc, const char **argv)
       
      -static int get_scalar_repos(const char *key, const char *value, void *data)
      +static int get_scalar_repos(const char *key, const char *value,
     -+			    struct key_value_info *kvi UNUSED, void *data)
     ++			    const struct config_context *ctx UNUSED,
     ++			    void *data)
       {
       	struct string_list *list = data;
       
     @@ sequencer.c: static struct update_ref_record *init_update_ref_record(const char
       
      -static int git_sequencer_config(const char *k, const char *v, void *cb)
      +static int git_sequencer_config(const char *k, const char *v,
     -+				struct key_value_info *kvi, void *cb)
     ++				const struct config_context *ctx, void *cb)
       {
       	struct replay_opts *opts = cb;
       	int status;
     @@ sequencer.c: static int git_sequencer_config(const char *k, const char *v, void
       		opts->commit_use_reference = git_config_bool(k, v);
       
      -	return git_diff_basic_config(k, v, NULL);
     -+	return git_diff_basic_config(k, v,kvi, NULL);
     ++	return git_diff_basic_config(k, v, ctx, NULL);
       }
       
       void sequencer_init_config(struct replay_opts *opts)
     @@ sequencer.c: static int git_config_string_dup(char **dest,
       
      -static int populate_opts_cb(const char *key, const char *value, void *data)
      +static int populate_opts_cb(const char *key, const char *value,
     -+			    struct key_value_info *kvi UNUSED, void *data)
     ++			    const struct config_context *ctx UNUSED,
     ++			    void *data)
       {
       	struct replay_opts *opts = data;
       	int error_flag = 1;
     @@ setup.c: no_prevention_needed:
       
      -static int read_worktree_config(const char *var, const char *value, void *vdata)
      +static int read_worktree_config(const char *var, const char *value,
     -+				struct key_value_info *kvi UNUSED,
     ++				const struct config_context *ctx UNUSED,
      +				void *vdata)
       {
       	struct repository_format *data = vdata;
     @@ setup.c: static enum extension_result handle_extension(const char *var,
       
      -static int check_repo_format(const char *var, const char *value, void *vdata)
      +static int check_repo_format(const char *var, const char *value,
     -+			     struct key_value_info *kvi, void *vdata)
     ++			     const struct config_context *ctx, void *vdata)
       {
       	struct repository_format *data = vdata;
       	const char *ext;
     @@ setup.c: static int check_repo_format(const char *var, const char *value, void *
       	}
       
      -	return read_worktree_config(var, value, vdata);
     -+	return read_worktree_config(var, value,kvi, vdata);
     ++	return read_worktree_config(var, value, ctx, vdata);
       }
       
       static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
     @@ setup.c: struct safe_directory_data {
       
      -static int safe_directory_cb(const char *key, const char *value, void *d)
      +static int safe_directory_cb(const char *key, const char *value,
     -+			     struct key_value_info *kvi UNUSED, void *d)
     ++			     const struct config_context *ctx UNUSED, void *d)
       {
       	struct safe_directory_data *data = d;
       
     @@ setup.c: static int ensure_valid_ownership(const char *gitfile,
       
      -static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
      +static int allowed_bare_repo_cb(const char *key, const char *value,
     -+				struct key_value_info *kvi UNUSED, void *d)
     ++				const struct config_context *ctx UNUSED,
     ++				void *d)
       {
       	enum allowed_bare_repo *allowed_bare_repo = d;
       
     @@ submodule-config.c: struct parse_config_parameter {
        */
      -static int parse_config(const char *var, const char *value, void *data)
      +static int parse_config(const char *var, const char *value,
     -+			struct key_value_info *kvi UNUSED, void *data)
     ++			const struct config_context *ctx UNUSED, void *data)
       {
       	struct parse_config_parameter *me = data;
       	struct submodule *submodule;
     @@ submodule-config.c: out:
       
      -static int gitmodules_cb(const char *var, const char *value, void *data)
      +static int gitmodules_cb(const char *var, const char *value,
     -+			 struct key_value_info *kvi UNUSED, void *data)
     ++			 const struct config_context *ctx, void *data)
       {
       	struct repository *repo = data;
       	struct parse_config_parameter parameter;
     +@@ submodule-config.c: static int gitmodules_cb(const char *var, const char *value, void *data)
     + 	parameter.gitmodules_oid = null_oid();
     + 	parameter.overwrite = 1;
     + 
     +-	return parse_config(var, value, &parameter);
     ++	return parse_config(var, value, ctx, &parameter);
     + }
     + 
     + void repo_read_gitmodules(struct repository *repo, int skip_if_read)
      @@ submodule-config.c: void submodule_free(struct repository *r)
       		submodule_cache_clear(r->submodule_cache);
       }
       
      -static int config_print_callback(const char *var, const char *value, void *cb_data)
      +static int config_print_callback(const char *var, const char *value,
     -+				 struct key_value_info *kvi UNUSED,
     ++				 const struct config_context *ctx UNUSED,
      +				 void *cb_data)
       {
       	char *wanted_key = cb_data;
     @@ submodule-config.c: struct fetch_config {
       
      -static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
      +static int gitmodules_fetch_config(const char *var, const char *value,
     -+				   struct key_value_info *kvi UNUSED,
     ++				   const struct config_context *ctx UNUSED,
      +				   void *cb)
       {
       	struct fetch_config *config = cb;
     @@ submodule-config.c: void fetch_config_from_gitmodules(int *max_children, int *re
       }
       
       static int gitmodules_update_clone_config(const char *var, const char *value,
     -+					  struct key_value_info *kvi UNUSED,
     ++					  const struct config_context *ctx UNUSED,
       					  void *cb)
       {
       	int *max_jobs = cb;
     @@ t/helper/test-config.c
       
      -static int iterate_cb(const char *var, const char *value, void *data UNUSED)
      +static int iterate_cb(const char *var, const char *value,
     -+		      struct key_value_info *kvi UNUSED, void *data UNUSED)
     ++		      const struct config_context *ctx UNUSED,
     ++		      void *data UNUSED)
       {
       	static int nr;
       
     @@ t/helper/test-config.c: static int iterate_cb(const char *var, const char *value
       
      -static int parse_int_cb(const char *var, const char *value, void *data)
      +static int parse_int_cb(const char *var, const char *value,
     -+			struct key_value_info *kvi UNUSED, void *data)
     ++			const struct config_context *ctx UNUSED, void *data)
       {
       	const char *key_to_match = data;
       
     @@ t/helper/test-config.c: static int parse_int_cb(const char *var, const char *val
       
      -static int early_config_cb(const char *var, const char *value, void *vdata)
      +static int early_config_cb(const char *var, const char *value,
     -+			   struct key_value_info *kvi UNUSED, void *vdata)
     ++			   const struct config_context *ctx UNUSED,
     ++			   void *vdata)
       {
       	const char *key = vdata;
       
     @@ t/helper/test-userdiff.c: static int driver_cb(struct userdiff_driver *driver,
       
      -static int cmd__userdiff_config(const char *var, const char *value, void *cb UNUSED)
      +static int cmd__userdiff_config(const char *var, const char *value,
     -+				struct key_value_info *kvi UNUSED,
     ++				const struct config_context *ctx UNUSED,
      +				void *cb UNUSED)
       {
       	if (userdiff_config(var, value) < 0)
     @@ trace2/tr2_cfg.c: struct tr2_cfg_data {
        */
      -static int tr2_cfg_cb(const char *key, const char *value, void *d)
      +static int tr2_cfg_cb(const char *key, const char *value,
     -+		      struct key_value_info *kvi UNUSED, void *d)
     ++		      const struct config_context *ctx UNUSED, void *d)
       {
       	struct strbuf **s;
       	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
     +@@ trace2/tr2_cfg.c: void tr2_list_env_vars_fl(const char *file, int line)
     + void tr2_cfg_set_fl(const char *file, int line, const char *key,
     + 		    const char *value)
     + {
     ++	struct key_value_info kvi = KVI_INIT;
     ++	struct config_context ctx = {
     ++		.kvi = &kvi,
     ++	};
     + 	struct tr2_cfg_data data = { file, line };
     + 
     + 	if (tr2_cfg_load_patterns() > 0)
     +-		tr2_cfg_cb(key, value, &data);
     ++		tr2_cfg_cb(key, value, &ctx, &data);
     + }
      
       ## trace2/tr2_sysenv.c ##
      @@ trace2/tr2_sysenv.c: static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
     @@ trace2/tr2_sysenv.c: static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
       
      -static int tr2_sysenv_cb(const char *key, const char *value, void *d)
      +static int tr2_sysenv_cb(const char *key, const char *value,
     -+			 struct key_value_info *kvi UNUSED, void *d)
     ++			 const struct config_context *ctx UNUSED, void *d)
       {
       	int k;
       
     @@ trailer.c: static struct {
       };
       
       static int git_trailer_default_config(const char *conf_key, const char *value,
     -+				      struct key_value_info *kvi UNUSED,
     ++				      const struct config_context *ctx UNUSED,
       				      void *cb UNUSED)
       {
       	const char *trailer_item, *variable_name;
     @@ trailer.c: static int git_trailer_default_config(const char *conf_key, const cha
       }
       
       static int git_trailer_config(const char *conf_key, const char *value,
     -+			      struct key_value_info *kvi UNUSED,
     ++			      const struct config_context *ctx UNUSED,
       			      void *cb UNUSED)
       {
       	const char *trailer_item, *variable_name;
     @@ upload-pack.c: static int parse_object_filter_config(const char *var, const char
       
      -static int upload_pack_config(const char *var, const char *value, void *cb_data)
      +static int upload_pack_config(const char *var, const char *value,
     -+			      struct key_value_info *kvi UNUSED,
     ++			      const struct config_context *ctx UNUSED,
      +			      void *cb_data)
       {
       	struct upload_pack_data *data = cb_data;
     @@ upload-pack.c: static int upload_pack_config(const char *var, const char *value,
       
      -static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
      +static int upload_pack_protected_config(const char *var, const char *value,
     -+					struct key_value_info *kvi UNUSED,
     ++					const struct config_context *ctx UNUSED,
      +					void *cb_data)
       {
       	struct upload_pack_data *data = cb_data;
     @@ urlmatch.c: static int cmp_matches(const struct urlmatch_item *a,
       
      -int urlmatch_config_entry(const char *var, const char *value, void *cb)
      +int urlmatch_config_entry(const char *var, const char *value,
     -+			  struct key_value_info *kvi UNUSED, void *cb)
     ++			  const struct config_context *ctx, void *cb)
       {
       	struct string_list_item *item;
       	struct urlmatch_config *collect = cb;
     +@@ urlmatch.c: int urlmatch_config_entry(const char *var, const char *value, void *cb)
     + 
     + 	if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
     + 		if (collect->cascade_fn)
     +-			return collect->cascade_fn(var, value, cb);
     ++			return collect->cascade_fn(var, value, ctx, cb);
     + 		return 0; /* not interested */
     + 	}
     + 	dot = strrchr(key, '.');
     +@@ urlmatch.c: int urlmatch_config_entry(const char *var, const char *value, void *cb)
     + 	strbuf_addstr(&synthkey, collect->section);
     + 	strbuf_addch(&synthkey, '.');
     + 	strbuf_addstr(&synthkey, key);
     +-	retval = collect->collect_fn(synthkey.buf, value, collect->cb);
     ++	retval = collect->collect_fn(synthkey.buf, value, ctx, collect->cb);
     + 
     + 	strbuf_release(&synthkey);
     + 	return retval;
      
       ## urlmatch.h ##
      @@ urlmatch.h: struct urlmatch_config {
     @@ urlmatch.h: struct urlmatch_config {
       
      -int urlmatch_config_entry(const char *var, const char *value, void *cb);
      +int urlmatch_config_entry(const char *var, const char *value,
     -+			  struct key_value_info *kvi, void *cb);
     ++			  const struct config_context *ctx, void *cb);
       void urlmatch_config_release(struct urlmatch_config *config);
       
       #endif /* URL_MATCH_H */
     @@ xdiff-interface.c: int xdiff_compare_lines(const char *l1, long s1,
       
      -int git_xmerge_config(const char *var, const char *value, void *cb)
      +int git_xmerge_config(const char *var, const char *value,
     -+		      struct key_value_info *kvi, void *cb)
     ++		      const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "merge.conflictstyle")) {
       		if (!value)
     @@ xdiff-interface.c: int git_xmerge_config(const char *var, const char *value, voi
       		return 0;
       	}
      -	return git_default_config(var, value, cb);
     -+	return git_default_config(var, value,kvi, cb);
     ++	return git_default_config(var, value, ctx, cb);
       }
      
       ## xdiff-interface.h ##
     @@ xdiff-interface.h: int buffer_is_binary(const char *ptr, unsigned long size);
       void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
       void xdiff_clear_find_func(xdemitconf_t *xecfg);
      -int git_xmerge_config(const char *var, const char *value, void *cb);
     ++struct config_context;
      +int git_xmerge_config(const char *var, const char *value,
     -+		      struct key_value_info *kvi, void *cb);
     ++		      const struct config_context *ctx, void *cb);
       extern int git_xmerge_style;
       
       /*
  5:  f363b160259 <  -:  ----------- (RFC-only) config: finish config_fn_t refactor
  6:  f57c1007cad !  4:  c5051ddc10d config.c: pass kvi in configsets
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config.c: pass kvi in configsets
     +    config.c: pass ctx in configsets
      
     -    Trivially pass "struct key_value_info" to config callbacks in
     -    configset_iter(). Then, in config callbacks that are only used with
     -    configsets, use the "kvi" arg to replace calls to current_config_*(),
     -    and delete current_config_line() because it has no remaining callers.
     +    Pass config_context to config callbacks in configset_iter(), trivially
     +    setting the .kvi member to the cached key_value_info. Then, in config
     +    callbacks that are only used with configsets, use the .kvi member to
     +    replace calls to current_config_*(), and delete current_config_line()
     +    because it has no remaining callers.
      
          This leaves builtin/config.c and config.c as the only remaining users of
          current_config_*().
     @@ builtin/remote.c: struct push_default_info
       };
       
       static int config_read_push_default(const char *key, const char *value,
     --	struct key_value_info *kvi UNUSED, void *cb)
     -+	struct key_value_info *kvi, void *cb)
     +-	const struct config_context *ctx UNUSED, void *cb)
     ++	const struct config_context *ctx, void *cb)
       {
     ++	const struct key_value_info *kvi = ctx->kvi;
     ++
       	struct push_default_info* info = cb;
       	if (strcmp(key, "remote.pushdefault") ||
       	    !value || strcmp(value, info->old_name))
     @@ config.c: static void configset_iter(struct config_reader *reader, struct config
       	struct string_list *values;
       	struct config_set_element *entry;
       	struct configset_list *list = &set->list;
     -+	struct key_value_info *kvi;
     ++	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;
     +@@ config.c: static void configset_iter(struct config_reader *reader, struct config_set *set,
       		values = &entry->value_list;
     -+		kvi = values->items[value_index].util;
       
       		config_reader_set_kvi(reader, values->items[value_index].util);
     - 
     +-
      -		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
     --			git_die_config_linenr(entry->key,
     ++		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);
      -
     -+		if (fn(entry->key, values->items[value_index].string, kvi, data) < 0)
     -+			git_die_config_linenr(entry->key, kvi->filename, kvi->linenr);
     ++					      ctx.kvi->filename,
     ++					      ctx.kvi->linenr);
       		config_reader_set_kvi(reader, NULL);
       	}
       }
     @@ remote.c: static void read_branches_file(struct remote_state *remote_state,
       }
       
       static int handle_config(const char *key, const char *value,
     --			 struct key_value_info *kvi UNUSED, void *cb)
     -+			 struct key_value_info *kvi, void *cb)
     +-			 const struct config_context *ctx UNUSED, void *cb)
     ++			 const struct config_context *ctx, void *cb)
       {
       	const char *name;
       	size_t namelen;
     +@@ remote.c: static int handle_config(const char *key, const char *value,
     + 	struct remote *remote;
     + 	struct branch *branch;
     + 	struct remote_state *remote_state = cb;
     ++	const struct key_value_info *kvi = ctx->kvi;
     + 
     + 	if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
     + 		/* There is no subsection. */
      @@ remote.c: static int handle_config(const char *key, const char *value,
       	}
       	remote = make_remote(remote_state, name, namelen);
     @@ t/helper/test-config.c
        */
       
       static int iterate_cb(const char *var, const char *value,
     --		      struct key_value_info *kvi UNUSED, void *data UNUSED)
     -+		      struct key_value_info *kvi, void *data UNUSED)
     +-		      const struct config_context *ctx UNUSED,
     ++		      const struct config_context *ctx,
     + 		      void *data UNUSED)
       {
     ++	const struct key_value_info *kvi = ctx->kvi;
       	static int nr;
       
     + 	if (nr++)
      @@ t/helper/test-config.c: static int iterate_cb(const char *var, const char *value,
       
       	printf("key=%s\n", var);
  7:  641a56f0b40 !  5:  595e7d2e163 config: provide kvi with config files
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config: provide kvi with config files
     +    config: pass ctx with config files
      
     -    Refactor out the configset logic that caches "struct config_source" and
     -    "enum config_scope" as a "struct key_value_info", and use it to pass the
     -    "kvi" arg to config callbacks when parsing config files. Get the "enum
     -    config_scope" value by plumbing an additional arg through
     -    git_config_from_file_with_options() and the underlying machinery.
     +    Pass config_context to config_callbacks when parsing config files. To
     +    provide the .kvi member, refactor out the configset logic that caches
     +    "struct config_source" and "enum config_scope" as a "struct
     +    key_value_info". Make the "enum config_scope" available to the config
     +    file machinery by plumbing an additional arg through
     +    git_config_from_file_with_options().
      
     -    We do not exercise the "kvi" arg yet because the remaining
     -    current_config_*() callers may be used with config_with_options(), which
     -    may read config from parameters, but parameters don't pass "kvi" yet.
     +    We do not exercise ctx yet because the remaining current_config_*()
     +    callers may be used with config_with_options(), which may read config
     +    from parameters, but parameters don't pass ctx yet.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     @@ config.c: static int handle_path_include(struct config_source *cs, const char *p
       		inc->depth--;
       	}
       cleanup:
     -@@ config.c: static int include_condition_is_true(struct config_source *cs,
     - }
     - 
     - static int git_config_include(const char *var, const char *value,
     --			      struct key_value_info *kvi UNUSED, void *data)
     -+			      struct key_value_info *kvi, void *data)
     - {
     - 	struct config_include_data *inc = data;
     - 	struct config_source *cs = inc->config_reader->source;
      @@ config.c: static int git_config_include(const char *var, const char *value,
       	 * Pass along all values, including "include" directives; this makes it
       	 * possible to query information on the includes themselves.
       	 */
      -	ret = inc->fn(var, value, NULL, inc->data);
     -+	ret = inc->fn(var, value, kvi, inc->data);
     ++	ret = inc->fn(var, value, ctx, inc->data);
       	if (ret < 0)
       		return ret;
       
     @@ config.c: static char *parse_value(struct config_source *cs)
       {
       	int c;
       	char *value;
     + 	int ret;
     ++	struct config_context ctx = {
     ++		.kvi = kvi,
     ++	};
     + 
     + 	/* Get the full name */
     + 	for (;;) {
      @@ config.c: 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, NULL, data);
      +	kvi->linenr = cs->linenr;
     -+	ret = fn(name->buf, value, kvi, data);
     ++	ret = fn(name->buf, value, &ctx, data);
       	if (ret >= 0)
       		cs->linenr++;
       	return ret;
     @@ config.c: int git_default_config(const char *var, const char *value,
      +			  void *data, enum config_scope scope,
       			  const struct config_options *opts)
       {
     -+	struct key_value_info kvi = { 0 };
     ++	struct key_value_info kvi = KVI_INIT;
       	int ret;
       
       	/* push config-file parsing state stack */
     @@ config.c: int git_config_set_multivar_in_file_gently(const char *config_filename
       			goto out_free;
      
       ## config.h ##
     -@@ config.h: int git_default_config(const char *, const char *, struct key_value_info *,
     +@@ config.h: int git_default_config(const char *, const char *,
       int git_config_from_file(config_fn_t fn, const char *, void *);
       
       int git_config_from_file_with_options(config_fn_t fn, const char *,
  8:  74f43fc727e =  6:  a2a891a069f builtin/config.c: test misuse of format_config()
  9:  3760015d2c0 !  7:  1fb1708bbd9 config.c: provide kvi with CLI config
     @@ Metadata
      Author: Glen Choo <chooglen@google.com>
      
       ## Commit message ##
     -    config.c: provide kvi with CLI config
     +    config.c: pass ctx with CLI config
      
     -    Refactor out kvi_from_param() from the logic that caches CLI config in
     -    configsets, and use it to pass the "kvi" arg to config callbacks when
     -    parsing CLI config. Now that "kvi" is always present when config
     -    machinery calls config callbacks, plumb "kvi" so that we can replace
     -    nearly all calls to current_config_*(). (The exception is an edge case
     -    where trace2/*.c calls current_config_scope(). That will be handled in a
     -    later commit.) Note that this results in "kvi" containing a different,
     -    more complete set of information than the mocked up "struct
     -    config_source" in git_config_from_parameters().
     +    Pass config_context when parsing CLI config. To provide the .kvi member,
     +    refactor out kvi_from_param() from the logic that caches CLI config in
     +    configsets. Now that config_context and config_context.kvi is always
     +    present when config machinery calls config callbacks, plumb "kvi" so
     +    that we can remove all calls of current_config_scope() except for
     +    trace2/*.c (which will be handled in a later commit), and remove all
     +    other current_config_*() (the functions themselves and their calls).
     +    Note that this results in .kvi containing a different, more complete
     +    set of information than the mocked up "struct config_source" in
     +    git_config_from_parameters().
      
          Plumbing "kvi" reveals a few places where we've been doing the wrong
          thing:
     @@ Commit message
      
          * "git config --get-urlmatch --show-scope" iterates config to collect
            values, but then attempts to display the scope after config iteration.
     -      Fix this by copying the "kvi" arg in the collection phase so that it
     +      Fix this by copying the "kvi" value in the collection phase so that it
            can be read back later. This means that we can now support "git config
            --get-urlmatch --show-origin" (we don't allow this combination of args
            because of this bug), but that is left unchanged for now.
     @@ builtin/config.c: static void check_argc(int argc, int min, int max)
       }
       
      -static void show_config_origin(struct strbuf *buf)
     -+static void show_config_origin(struct key_value_info *kvi, struct strbuf *buf)
     ++static void show_config_origin(const struct key_value_info *kvi,
     ++			       struct strbuf *buf)
       {
       	const char term = end_nul ? '\0' : '\t';
       
     @@ builtin/config.c: static void check_argc(int argc, int min, int max)
       }
       
      -static void show_config_scope(struct strbuf *buf)
     -+static void show_config_scope(struct key_value_info *kvi, 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());
     @@ builtin/config.c: static void check_argc(int argc, int min, int max)
       }
       
       static int show_all_config(const char *key_, const char *value_,
     --			   struct key_value_info *kvi UNUSED, void *cb UNUSED)
     -+			   struct key_value_info *kvi, void *cb UNUSED)
     +-			   const struct config_context *ctx UNUSED,
     ++			   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)
     @@ builtin/config.c: struct strbuf_list {
       
      -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_, struct key_value_info *kvi)
     ++			 const char *value_, const struct key_value_info *kvi)
       {
       	if (show_scope)
      -		show_config_scope(buf);
     @@ builtin/config.c: static int format_config(struct strbuf *buf, const char *key_,
       }
       
       static int collect_config(const char *key_, const char *value_,
     --			  struct key_value_info *kvi UNUSED, void *cb)
     -+			  struct key_value_info *kvi, void *cb)
     +-			  const struct config_context *ctx UNUSED, void *cb)
     ++			  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;
      @@ builtin/config.c: static int collect_config(const char *key_, const char *value_,
       	ALLOC_GROW(values->items, values->nr + 1, values->alloc);
       	strbuf_init(&values->items[values->nr], 0);
     @@ builtin/config.c: static int get_value(const char *key_, const char *regex_, uns
       			    &given_config_source, &config_options);
       
       	if (!values.nr && default_value) {
     -+		struct key_value_info kvi = { 0 };
     ++		struct key_value_info kvi = KVI_INIT;
       		struct strbuf *item;
      +
      +		kvi_from_param(&kvi);
     @@ builtin/config.c: static void check_write(void)
       };
       
       static int urlmatch_collect_fn(const char *var, const char *value,
     --			       struct key_value_info *kvi UNUSED, void *cb)
     -+			       struct key_value_info *kvi, void *cb)
     +-			       const struct config_context *ctx UNUSED,
     ++			       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));
      @@ builtin/config.c: static int urlmatch_collect_fn(const char *var, const char *value,
       	} else {
       		strbuf_reset(&matched->value);
       	}
     -+	memcpy(&matched->kvi, kvi, sizeof(struct key_value_info));
     ++	matched->kvi = *kvi;
       
       	if (value) {
       		strbuf_addstr(&matched->value, value);
     @@ config.c: static const char include_depth_advice[] = 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(struct config_source *cs,
     -+			       struct key_value_info *kvi,
     ++			       const struct key_value_info *kvi,
      +			       const char *path,
       			       struct config_include_data *inc)
       {
     @@ config.c: static int git_config_include(const char *var, const char *value,
       
       	if (!strcmp(var, "include.path"))
      -		ret = handle_path_include(cs, value, inc);
     -+		ret = handle_path_include(cs, kvi, value, inc);
     ++		ret = handle_path_include(cs, ctx->kvi, value, inc);
       
       	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
       	    cond && include_condition_is_true(cs, inc, cond, cond_len) &&
     @@ config.c: static int git_config_include(const char *var, const char *value,
       		if (inc->opts->unconditional_remote_url)
       			inc->fn = forbid_remote_url;
      -		ret = handle_path_include(cs, value, inc);
     -+		ret = handle_path_include(cs, kvi, value, inc);
     ++		ret = handle_path_include(cs, ctx->kvi, value, inc);
       		inc->fn = old_fn;
       	}
       
     @@ config.c: out_free_ret_1:
       {
       	char *canonical_name;
       	int ret;
     -@@ config.c: static int config_parse_pair(const char *key, const char *value,
     ++	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, NULL, data) < 0) ? -1 : 0;
     -+	ret = (fn(canonical_name, value, kvi, data) < 0) ? -1 : 0;
     ++	ret = (fn(canonical_name, value, &ctx, data) < 0) ? -1 : 0;
       	free(canonical_name);
       	return ret;
       }
     @@ config.c: static int config_parse_pair(const char *key, const char *value,
       	const char *value;
       	struct strbuf **pair;
       	int ret;
     -+	struct key_value_info kvi = { 0 };
     ++	struct key_value_info kvi = KVI_INIT;
      +
      +	kvi_from_param(&kvi);
       
     @@ config.c: int git_config_from_parameters(config_fn_t fn, void *data)
       	int ret = 0;
       	char *envw = NULL;
       	struct config_source source = CONFIG_SOURCE_INIT;
     -+	struct key_value_info kvi = { 0 };
     ++	struct key_value_info kvi = KVI_INIT;
       
       	source.origin_type = CONFIG_ORIGIN_CMDLINE;
       	config_reader_push_source(&the_reader, &source);
     @@ config.c: static int configset_find_element(struct config_set *set, const char *
       }
       
      -static int configset_add_value(struct config_reader *reader,
     -+static int configset_add_value(struct key_value_info *kvi_p,
     ++static int configset_add_value(const struct key_value_info *kvi_p,
      +			       struct config_reader *reader,
       			       struct config_set *set, const char *key,
       			       const char *value)
     @@ config.c: struct configset_add_data {
       #define CONFIGSET_ADD_INIT { 0 }
       
       static int config_set_callback(const char *key, const char *value,
     --			       struct key_value_info *kvi UNUSED, void *cb)
     -+			       struct key_value_info *kvi, void *cb)
     +-			       const struct config_context *ctx UNUSED,
     ++			       const struct config_context *ctx,
     + 			       void *cb)
       {
       	struct configset_add_data *data = cb;
      -	configset_add_value(data->config_reader, data->config_set, key, value);
     -+	configset_add_value(kvi, data->config_reader, data->config_set, key, value);
     ++	configset_add_value(ctx->kvi, data->config_reader, data->config_set,
     ++			    key, value);
       	return 0;
       }
       
 10:  7dc0c46b864 !  8:  66572df7beb trace2: plumb config kvi
     @@ Commit message
          trace2_def_param_fl(), which gets called by two code paths:
      
          - Through tr2_cfg_cb(), which is a config callback, so it trivially
     -      receives "kvi".
     +      receives "kvi" via the "struct config_context ctx" parameter.
      
          - Through tr2_list_env_vars_fl(), which is a high level function that
            lists environment variables for tracing. This has been secretly
     @@ trace2.c: void trace2_thread_exit_fl(const char *file, int line)
       
       void trace2_def_param_fl(const char *file, int line, const char *param,
      -			 const char *value)
     -+			 const char *value, struct key_value_info *kvi)
     ++			 const char *value, const struct key_value_info *kvi)
       {
       	struct tr2_tgt *tgt_j;
       	int j;
     @@ trace2.h: void trace2_thread_exit_fl(const char *file, int line);
        */
       void trace2_def_param_fl(const char *file, int line, const char *param,
      -			 const char *value);
     -+			 const char *value, struct key_value_info *kvi);
     ++			 const char *value, const struct key_value_info *kvi);
       
       #define trace2_def_param(param, value) \
       	trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
      
       ## trace2/tr2_cfg.c ##
     -@@ trace2/tr2_cfg.c: struct tr2_cfg_data {
     -  * See if the given config key matches any of our patterns of interest.
     -  */
     - static int tr2_cfg_cb(const char *key, const char *value,
     --		      struct key_value_info *kvi UNUSED, void *d)
     -+		      struct key_value_info *kvi, void *d)
     - {
     - 	struct strbuf **s;
     - 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
      @@ trace2/tr2_cfg.c: static int tr2_cfg_cb(const char *key, const char *value,
       		struct strbuf *buf = *s;
       		int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
       		if (wm == WM_MATCH) {
      -			trace2_def_param_fl(data->file, data->line, key, value);
      +			trace2_def_param_fl(data->file, data->line, key, value,
     -+					    kvi);
     ++					    ctx->kvi);
       			return 0;
       		}
       	}
     @@ trace2/tr2_cfg.c: void tr2_cfg_list_config_fl(const char *file, int line)
       
       void tr2_list_env_vars_fl(const char *file, int line)
       {
     -+	struct key_value_info kvi = { 0 };
     ++	struct key_value_info kvi = KVI_INIT;
       	struct strbuf **s;
       
      +	kvi_from_param(&kvi);
     @@ trace2/tr2_tgt.h: typedef void(tr2_tgt_evt_exec_result_fl_t)(const char *file, i
       typedef void(tr2_tgt_evt_param_fl_t)(const char *file, int line,
      -				     const char *param, const char *value);
      +				     const char *param, const char *value,
     -+				     struct key_value_info *kvi);
     ++				     const struct key_value_info *kvi);
       
       typedef void(tr2_tgt_evt_repo_fl_t)(const char *file, int line,
       				    const struct repository *repo);
     @@ trace2/tr2_tgt_event.c: static void fn_exec_result_fl(const char *file, int line
       
       static void fn_param_fl(const char *file, int line, const char *param,
      -			const char *value)
     -+			const char *value, struct key_value_info *kvi)
     ++			const char *value, const struct key_value_info *kvi)
       {
       	const char *event_name = "def_param";
       	struct json_writer jw = JSON_WRITER_INIT;
     @@ trace2/tr2_tgt_normal.c: static void fn_exec_result_fl(const char *file, int lin
       
       static void fn_param_fl(const char *file, int line, const char *param,
      -			const char *value)
     -+			const char *value, struct key_value_info *kvi)
     ++			const char *value, const struct key_value_info *kvi)
       {
       	struct strbuf buf_payload = STRBUF_INIT;
      -	enum config_scope scope = current_config_scope();
     @@ trace2/tr2_tgt_perf.c: static void fn_exec_result_fl(const char *file, int line,
       
       static void fn_param_fl(const char *file, int line, const char *param,
      -			const char *value)
     -+			const char *value, struct key_value_info *kvi)
     ++			const char *value, const struct key_value_info *kvi)
       {
       	const char *event_name = "def_param";
       	struct strbuf buf_payload = STRBUF_INIT;
 11:  504eb206b5a !  9:  123e19dda4a config: pass kvi to die_bad_number()
     @@ Commit message
      
          In config.c, this requires changing the signature of
          git_configset_get_value() to 'return' "kvi" in an out parameter so that
     -    git_configset_get_<type>() can pass it to git_config_<type>().
     +    git_configset_get_<type>() can pass it to git_config_<type>(). Only
     +    numeric types will use "kvi", so for non-numeric types (e.g.
     +    git_configset_get_string()), pass NULL to indicate that the out
     +    parameter isn't needed.
      
     -    Outside of config.c, config callbacks now need to pass "kvi" to any of
     -    the git_config_<type>() functions that parse a config string into a
     -    number type. Included is a .cocci patch to make that refactor. In cases
     -    where "kvi" would never be used, pass NULL, e.g.:
     +    Outside of config.c, config callbacks now need to pass "ctx->kvi" to any
     +    of the git_config_<type>() functions that parse a config string into a
     +    number type. Included is a .cocci patch to make that refactor.
      
     -    - In config.c, when we are parsing a boolean instead of a number
     -    - In builtin/config.c, when calling normalize_value() before setting
     -      config to something the user gave us.
     +    The only exceptional case is builtin/config.c, where git_config_<type>()
     +    is called outside of a config callback (namely, on user-provided input),
     +    so config source information has never been available. In this case,
     +    die_bad_number() defaults to a generic, but perfectly descriptive
     +    message. Let's provide a safe, non-NULL for "kvi" anyway, but make sure
     +    not to change the message.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     @@ archive-tar.c: static int tar_filter_config(const char *var, const char *value,
       }
       
       static int git_tar_config(const char *var, const char *value,
     --			  struct key_value_info *kvi UNUSED, void *cb)
     -+			  struct key_value_info *kvi, void *cb)
     +-			  const struct config_context *ctx UNUSED, void *cb)
     ++			  const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, "tar.umask")) {
       		if (value && !strcmp(value, "user")) {
     @@ archive-tar.c: static int tar_filter_config(const char *var, const char *value,
       			umask(tar_umask);
       		} else {
      -			tar_umask = git_config_int(var, value);
     -+			tar_umask = git_config_int(var, value, kvi);
     ++			tar_umask = git_config_int(var, value, ctx->kvi);
       		}
       		return 0;
       	}
     @@ builtin/commit-graph.c: static int write_option_max_new_filters(const struct opt
       }
       
       static int git_commit_graph_write_config(const char *var, const char *value,
     --					 struct key_value_info *kvi UNUSED,
     -+					 struct key_value_info *kvi,
     +-					 const struct config_context *ctx UNUSED,
     ++					 const struct config_context *ctx,
       					 void *cb UNUSED)
       {
       	if (!strcmp(var, "commitgraph.maxnewfilters"))
      -		write_opts.max_new_filters = git_config_int(var, value);
     -+		write_opts.max_new_filters = git_config_int(var, value, kvi);
     ++		write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
       	/*
       	 * No need to fall-back to 'git_default_config', since this was already
       	 * called in 'cmd_commit_graph()'.
     @@ builtin/commit.c: static int git_status_config(const char *k, const char *v,
       	if (!strcmp(k, "status.submodulesummary")) {
       		int is_bool;
      -		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
     -+		s->submodule_summary = git_config_bool_or_int(k, v, kvi,
     ++		s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
      +							      &is_bool);
       		if (is_bool && s->submodule_summary)
       			s->submodule_summary = -1;
     @@ builtin/commit.c: static int git_status_config(const char *k, const char *v,
       	if (!strcmp(k, "diff.renamelimit")) {
       		if (s->rename_limit == -1)
      -			s->rename_limit = git_config_int(k, v);
     -+			s->rename_limit = git_config_int(k, v, kvi);
     ++			s->rename_limit = git_config_int(k, v, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(k, "status.renamelimit")) {
      -		s->rename_limit = git_config_int(k, v);
     -+		s->rename_limit = git_config_int(k, v, kvi);
     ++		s->rename_limit = git_config_int(k, v, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(k, "diff.renames")) {
     @@ builtin/commit.c: static int git_commit_config(const char *k, const char *v,
       	if (!strcmp(k, "commit.verbose")) {
       		int is_bool;
      -		config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
     -+		config_commit_verbose = git_config_bool_or_int(k, v, kvi,
     ++		config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
      +							       &is_bool);
       		return 0;
       	}
     @@ builtin/config.c: static char *normalize_value(const char *key, const char *valu
       		if (!is_bool)
       			return xstrfmt("%d", v);
       		else
     +@@ builtin/config.c: 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));
     + 
      @@ builtin/config.c: 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], NULL);
     ++		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"
     @@ builtin/config.c: int cmd_config(int argc, const char **argv, const char *prefix
       		check_write();
       		check_argc(argc, 2, 3);
      -		value = normalize_value(argv[0], argv[1]);
     -+		value = normalize_value(argv[0], argv[1], NULL);
     ++		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);
     @@ builtin/config.c: int cmd_config(int argc, const char **argv, const char *prefix
       		check_write();
       		check_argc(argc, 2, 2);
      -		value = normalize_value(argv[0], argv[1]);
     -+		value = normalize_value(argv[0], argv[1], NULL);
     ++		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,
     @@ builtin/config.c: int cmd_config(int argc, const char **argv, const char *prefix
       		check_write();
       		check_argc(argc, 2, 3);
      -		value = normalize_value(argv[0], argv[1]);
     -+		value = normalize_value(argv[0], argv[1], NULL);
     ++		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);
     @@ builtin/fetch.c: static int git_fetch_config(const char *k, const char *v,
       
       	if (!strcmp(k, "submodule.fetchjobs")) {
      -		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
     -+		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v, kvi);
     ++		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v, ctx->kvi);
       		return 0;
       	} else if (!strcmp(k, "fetch.recursesubmodules")) {
       		recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
     @@ builtin/fetch.c: static int git_fetch_config(const char *k, const char *v,
       
       	if (!strcmp(k, "fetch.parallel")) {
      -		fetch_parallel_config = git_config_int(k, v);
     -+		fetch_parallel_config = git_config_int(k, v, kvi);
     ++		fetch_parallel_config = git_config_int(k, v, ctx->kvi);
       		if (fetch_parallel_config < 0)
       			die(_("fetch.parallel cannot be negative"));
       		if (!fetch_parallel_config)
      
       ## builtin/fsmonitor--daemon.c ##
      @@ builtin/fsmonitor--daemon.c: static int fsmonitor_config(const char *var, const char *value,
     - 			    struct key_value_info *kvi, void *cb)
     + 			    const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
      -		int i = git_config_int(var, value);
     -+		int i = git_config_int(var, value, kvi);
     ++		int i = git_config_int(var, value, ctx->kvi);
       		if (i < 1)
       			return error(_("value of '%s' out of range: %d"),
       				     FSMONITOR__IPC_THREADS, i);
     @@ builtin/fsmonitor--daemon.c: static int fsmonitor_config(const char *var, const
       
       	if (!strcmp(var, FSMONITOR__START_TIMEOUT)) {
      -		int i = git_config_int(var, value);
     -+		int i = git_config_int(var, value, kvi);
     ++		int i = git_config_int(var, value, ctx->kvi);
       		if (i < 0)
       			return error(_("value of '%s' out of range: %d"),
       				     FSMONITOR__START_TIMEOUT, i);
     @@ builtin/fsmonitor--daemon.c: static int fsmonitor_config(const char *var, const
       	if (!strcmp(var, FSMONITOR__ANNOUNCE_STARTUP)) {
       		int is_bool;
      -		int i = git_config_bool_or_int(var, value, &is_bool);
     -+		int i = git_config_bool_or_int(var, value, kvi, &is_bool);
     ++		int i = git_config_bool_or_int(var, value, ctx->kvi, &is_bool);
       		if (i < 0)
       			return error(_("value of '%s' not bool or int: %d"),
       				     var, i);
     @@ builtin/grep.c: static int grep_cmd_config(const char *var, const char *value,
       
       	if (!strcmp(var, "grep.threads")) {
      -		num_threads = git_config_int(var, value);
     -+		num_threads = git_config_int(var, value, kvi);
     ++		num_threads = git_config_int(var, value, ctx->kvi);
       		if (num_threads < 0)
       			die(_("invalid number of threads specified (%d) for %s"),
       			    num_threads, var);
     @@ builtin/index-pack.c: static int git_index_pack_config(const char *k, const char
       
       	if (!strcmp(k, "pack.indexversion")) {
      -		opts->version = git_config_int(k, v);
     -+		opts->version = git_config_int(k, v, kvi);
     ++		opts->version = git_config_int(k, v, ctx->kvi);
       		if (opts->version > 2)
       			die(_("bad pack.indexVersion=%"PRIu32), opts->version);
       		return 0;
       	}
       	if (!strcmp(k, "pack.threads")) {
      -		nr_threads = git_config_int(k, v);
     -+		nr_threads = git_config_int(k, v, kvi);
     ++		nr_threads = git_config_int(k, v, ctx->kvi);
       		if (nr_threads < 0)
       			die(_("invalid number of threads specified (%d)"),
       			    nr_threads);
     @@ builtin/log.c: static int git_log_config(const char *var, const char *value,
       		return git_config_string(&fmt_patch_subject_prefix, var, value);
       	if (!strcmp(var, "format.filenamemaxlength")) {
      -		fmt_patch_name_max = git_config_int(var, value);
     -+		fmt_patch_name_max = git_config_int(var, value, kvi);
     ++		fmt_patch_name_max = git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(var, "format.encodeemailheaders")) {
      
       ## builtin/pack-objects.c ##
      @@ builtin/pack-objects.c: static int git_pack_config(const char *k, const char *v,
     - 			   struct key_value_info *kvi, void *cb)
     + 			   const struct config_context *ctx, void *cb)
       {
       	if (!strcmp(k, "pack.window")) {
      -		window = git_config_int(k, v);
     -+		window = git_config_int(k, v, kvi);
     ++		window = git_config_int(k, v, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(k, "pack.windowmemory")) {
      -		window_memory_limit = git_config_ulong(k, v);
     -+		window_memory_limit = git_config_ulong(k, v, kvi);
     ++		window_memory_limit = git_config_ulong(k, v, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(k, "pack.depth")) {
      -		depth = git_config_int(k, v);
     -+		depth = git_config_int(k, v, kvi);
     ++		depth = git_config_int(k, v, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(k, "pack.deltacachesize")) {
      -		max_delta_cache_size = git_config_int(k, v);
     -+		max_delta_cache_size = git_config_int(k, v, kvi);
     ++		max_delta_cache_size = git_config_int(k, v, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(k, "pack.deltacachelimit")) {
      -		cache_max_small_delta_size = git_config_int(k, v);
     -+		cache_max_small_delta_size = git_config_int(k, v, kvi);
     ++		cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(k, "pack.writebitmaphashcache")) {
     @@ builtin/pack-objects.c: static int git_pack_config(const char *k, const char *v,
       	}
       	if (!strcmp(k, "pack.threads")) {
      -		delta_search_threads = git_config_int(k, v);
     -+		delta_search_threads = git_config_int(k, v, kvi);
     ++		delta_search_threads = git_config_int(k, v, ctx->kvi);
       		if (delta_search_threads < 0)
       			die(_("invalid number of threads specified (%d)"),
       			    delta_search_threads);
     @@ builtin/pack-objects.c: static int git_pack_config(const char *k, const char *v,
       	}
       	if (!strcmp(k, "pack.indexversion")) {
      -		pack_idx_opts.version = git_config_int(k, v);
     -+		pack_idx_opts.version = git_config_int(k, v, kvi);
     ++		pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
       		if (pack_idx_opts.version > 2)
       			die(_("bad pack.indexVersion=%"PRIu32),
       			    pack_idx_opts.version);
     @@ builtin/receive-pack.c: static int receive_pack_config(const char *var, const ch
       
       	if (strcmp(var, "receive.unpacklimit") == 0) {
      -		receive_unpack_limit = git_config_int(var, value);
     -+		receive_unpack_limit = git_config_int(var, value, kvi);
     ++		receive_unpack_limit = git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       
       	if (strcmp(var, "transfer.unpacklimit") == 0) {
      -		transfer_unpack_limit = git_config_int(var, value);
     -+		transfer_unpack_limit = git_config_int(var, value, kvi);
     ++		transfer_unpack_limit = git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       
     @@ builtin/receive-pack.c: static int receive_pack_config(const char *var, const ch
       
       	if (strcmp(var, "receive.certnonceslop") == 0) {
      -		nonce_stamp_slop_limit = git_config_ulong(var, value);
     -+		nonce_stamp_slop_limit = git_config_ulong(var, value, kvi);
     ++		nonce_stamp_slop_limit = git_config_ulong(var, value, ctx->kvi);
       		return 0;
       	}
       
     @@ builtin/receive-pack.c: static int receive_pack_config(const char *var, const ch
       
       	if (strcmp(var, "receive.keepalive") == 0) {
      -		keepalive_in_sec = git_config_int(var, value);
     -+		keepalive_in_sec = git_config_int(var, value, kvi);
     ++		keepalive_in_sec = git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       
       	if (strcmp(var, "receive.maxinputsize") == 0) {
      -		max_input_size = git_config_int64(var, value);
     -+		max_input_size = git_config_int64(var, value, kvi);
     ++		max_input_size = git_config_int64(var, value, ctx->kvi);
       		return 0;
       	}
       
     @@ builtin/submodule--helper.c: static int update_clone_task_finished(int result,
       }
       
       static int git_update_clone_config(const char *var, const char *value,
     --				   struct key_value_info *kvi UNUSED,
     -+				   struct key_value_info *kvi,
     +-				   const struct config_context *ctx UNUSED,
     ++				   const struct config_context *ctx,
       				   void *cb)
       {
       	int *max_jobs = cb;
       
       	if (!strcmp(var, "submodule.fetchjobs"))
      -		*max_jobs = parse_submodule_fetchjobs(var, value);
     -+		*max_jobs = parse_submodule_fetchjobs(var, value, kvi);
     ++		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
       	return 0;
       }
       
     @@ config.c: int git_parse_ssize_t(const char *value, ssize_t *ret)
      -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,
     -+			   struct key_value_info *kvi)
     ++			   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 = "";
     @@ config.c: int git_parse_ssize_t(const char *value, ssize_t *ret)
      -	reader_origin_type(reader, &config_origin);
      -
      -	if (!config_name)
     -+	if (!kvi || !kvi->filename)
     ++	if (!kvi->filename)
       		die(_(bad_numeric), value, name, _(error_type));
       
      -	switch (config_origin) {
     @@ config.c: int git_parse_ssize_t(const char *value, ssize_t *ret)
       
      -int git_config_int(const char *name, const char *value)
      +int git_config_int(const char *name, const char *value,
     -+		   struct key_value_info *kvi)
     ++		   const struct key_value_info *kvi)
       {
       	int ret;
       	if (!git_parse_int(value, &ret))
     @@ config.c: int git_parse_ssize_t(const char *value, ssize_t *ret)
       }
       
      -int64_t git_config_int64(const char *name, const char *value)
     -+int64_t git_config_int64(const char *name, const char *value, struct key_value_info *kvi)
     ++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))
     @@ config.c: int git_parse_ssize_t(const char *value, ssize_t *ret)
       
      -unsigned long git_config_ulong(const char *name, const char *value)
      +unsigned long git_config_ulong(const char *name, const char *value,
     -+			       struct key_value_info *kvi)
     ++			       const struct key_value_info *kvi)
       {
       	unsigned long ret;
       	if (!git_parse_ulong(value, &ret))
     @@ config.c: int git_parse_ssize_t(const char *value, ssize_t *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,
     -+			   struct key_value_info *kvi)
     ++			   const struct key_value_info *kvi)
       {
       	ssize_t ret;
       	if (!git_parse_ssize_t(value, &ret))
     @@ config.c: int git_parse_maybe_bool(const char *value)
       
      -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,
     -+			   struct key_value_info *kvi, int *is_bool)
     ++			   const struct key_value_info *kvi, int *is_bool)
       {
       	int v = git_parse_maybe_bool_text(value);
       	if (0 <= v) {
     @@ config.c: static int git_default_core_config(const char *var, const char *value,
       			default_abbrev = the_hash_algo->hexsz;
       		else {
      -			int abbrev = git_config_int(var, value);
     -+			int abbrev = git_config_int(var, value, kvi);
     ++			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;
     @@ config.c: static int git_default_core_config(const char *var, const char *value,
       
       	if (!strcmp(var, "core.loosecompression")) {
      -		int level = git_config_int(var, value);
     -+		int level = git_config_int(var, value, kvi);
     ++		int level = git_config_int(var, value, ctx->kvi);
       		if (level == -1)
       			level = Z_DEFAULT_COMPRESSION;
       		else if (level < 0 || level > Z_BEST_COMPRESSION)
     @@ config.c: static int git_default_core_config(const char *var, const char *value,
       
       	if (!strcmp(var, "core.compression")) {
      -		int level = git_config_int(var, value);
     -+		int level = git_config_int(var, value, kvi);
     ++		int level = git_config_int(var, value, ctx->kvi);
       		if (level == -1)
       			level = Z_DEFAULT_COMPRESSION;
       		else if (level < 0 || level > Z_BEST_COMPRESSION)
     @@ config.c: static int git_default_core_config(const char *var, const char *value,
       	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, kvi);
     ++		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;
     @@ config.c: static int git_default_core_config(const char *var, const char *value,
       
       	if (!strcmp(var, "core.bigfilethreshold")) {
      -		big_file_threshold = git_config_ulong(var, value);
     -+		big_file_threshold = git_config_ulong(var, value, kvi);
     ++		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, kvi);
     ++		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, kvi);
     ++		delta_base_cache_limit = git_config_ulong(var, value, ctx->kvi);
       		return 0;
       	}
       
     @@ config.c: int git_default_config(const char *var, const char *value,
       
       	if (!strcmp(var, "pack.packsizelimit")) {
      -		pack_size_limit_cfg = git_config_ulong(var, value);
     -+		pack_size_limit_cfg = git_config_ulong(var, value, kvi);
     ++		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, kvi);
     ++		int level = git_config_int(var, value, ctx->kvi);
       		if (level == -1)
       			level = Z_DEFAULT_COMPRESSION;
       		else if (level < 0 || level > Z_BEST_COMPRESSION)
      @@ config.c: static void configset_iter(struct config_reader *reader, struct config_set *set,
     + 		value_index = list->items[i].value_index;
       		values = &entry->value_list;
     - 		kvi = values->items[value_index].util;
       
      -		config_reader_set_kvi(reader, values->items[value_index].util);
     --
     - 		if (fn(entry->key, values->items[value_index].string, kvi, data) < 0)
     - 			git_die_config_linenr(entry->key, kvi->filename, kvi->linenr);
     + 		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,
     + 					      ctx.kvi->filename,
     + 					      ctx.kvi->linenr);
      -		config_reader_set_kvi(reader, NULL);
       	}
       }
     @@ config.h: int git_parse_maybe_bool(const char *);
        * otherwise, returns the parsed result.
        */
      -int git_config_int(const char *, const char *);
     -+int git_config_int(const char *, const char *, struct key_value_info *);
     ++int git_config_int(const char *, const char *, const struct key_value_info *);
       
      -int64_t git_config_int64(const char *, const char *);
     -+int64_t git_config_int64(const char *, const char *, struct key_value_info *);
     ++int64_t git_config_int64(const char *, const char *,
     ++			 const struct key_value_info *);
       
       /**
        * Identical to `git_config_int`, but for unsigned longs.
        */
      -unsigned long git_config_ulong(const char *, const char *);
     -+unsigned long git_config_ulong(const char *, const char *, struct key_value_info *);
     ++unsigned long git_config_ulong(const char *, const char *,
     ++			       const struct key_value_info *);
       
      -ssize_t git_config_ssize_t(const char *, const char *);
     -+ssize_t git_config_ssize_t(const char *, const char *, struct key_value_info *);
     ++ssize_t git_config_ssize_t(const char *, const char *,
     ++			   const struct key_value_info *);
       
       /**
        * Same as `git_config_bool`, except that integers are returned as-is, and
        * an `is_bool` flag is unset.
        */
      -int git_config_bool_or_int(const char *, const char *, int *);
     -+int git_config_bool_or_int(const char *, const char *, struct key_value_info *,
     -+			   int *);
     ++int git_config_bool_or_int(const char *, const char *,
     ++			   const struct key_value_info *, int *);
       
       /**
        * Parse a string into a boolean value, respecting keywords like "true" and
     @@ contrib/coccinelle/git_config_number.cocci (new)
      +git_config_ssize_t
      +)
      +  (C1, C2
     -++ , kvi
     +++ , ctx->kvi
      +  )
      +|
      +(
     @@ contrib/coccinelle/git_config_number.cocci (new)
      +|
      +git_config_bool_or_int
      +)
     -+  (C1, C2,
     -++ kvi,
     -+  C3
     ++  (C1, C2
     +++ , ctx->kvi
     ++ , C3
      +  )
      +)
      
     @@ diff.c: int git_diff_ui_config(const char *var, const char *value,
       	}
       	if (!strcmp(var, "diff.context")) {
      -		diff_context_default = git_config_int(var, value);
     -+		diff_context_default = git_config_int(var, value, kvi);
     ++		diff_context_default = git_config_int(var, value, ctx->kvi);
       		if (diff_context_default < 0)
       			return -1;
       		return 0;
     @@ diff.c: int git_diff_ui_config(const char *var, const char *value,
       	if (!strcmp(var, "diff.interhunkcontext")) {
      -		diff_interhunk_context_default = git_config_int(var, value);
      +		diff_interhunk_context_default = git_config_int(var, value,
     -+								kvi);
     ++								ctx->kvi);
       		if (diff_interhunk_context_default < 0)
       			return -1;
       		return 0;
     @@ diff.c: int git_diff_ui_config(const char *var, const char *value,
       	}
       	if (!strcmp(var, "diff.statgraphwidth")) {
      -		diff_stat_graph_width = git_config_int(var, value);
     -+		diff_stat_graph_width = git_config_int(var, value, kvi);
     ++		diff_stat_graph_width = git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp(var, "diff.external"))
     @@ diff.c: int git_diff_basic_config(const char *var, const char *value,
       
       	if (!strcmp(var, "diff.renamelimit")) {
      -		diff_rename_limit_default = git_config_int(var, value);
     -+		diff_rename_limit_default = git_config_int(var, value, kvi);
     ++		diff_rename_limit_default = git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       
     @@ fmt-merge-msg.c: int fmt_merge_msg_config(const char *key, const char *value,
       	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
       		int is_bool;
      -		merge_log_config = git_config_bool_or_int(key, value, &is_bool);
     -+		merge_log_config = git_config_bool_or_int(key, value, kvi, &is_bool);
     ++		merge_log_config = git_config_bool_or_int(key, value, ctx->kvi, &is_bool);
       		if (!is_bool && merge_log_config < 0)
       			return error("%s: negative length %s", key, value);
       		if (is_bool && merge_log_config)
     @@ help.c: static struct cmdnames aliases;
       #define AUTOCORRECT_IMMEDIATELY (-1)
       
       static int git_unknown_cmd_config(const char *var, const char *value,
     --				  struct key_value_info *kvi UNUSED,
     --				  void *cb UNUSED)
     -+				  struct key_value_info *kvi, void *cb UNUSED)
     +-				  const struct config_context *ctx UNUSED,
     ++				  const struct config_context *ctx,
     + 				  void *cb UNUSED)
       {
       	const char *p;
     - 
      @@ help.c: static int git_unknown_cmd_config(const char *var, const char *value,
       		} else if (!strcmp(value, "prompt")) {
       			autocorrect = AUTOCORRECT_PROMPT;
       		} else {
      -			int v = git_config_int(var, value);
     -+			int v = git_config_int(var, value, kvi);
     ++			int v = git_config_int(var, value, ctx->kvi);
       			autocorrect = (v < 0)
       				? AUTOCORRECT_IMMEDIATELY : v;
       		}
     @@ http.c: static int http_options(const char *var, const char *value,
       
       	if (!strcmp("http.minsessions", var)) {
      -		min_curl_sessions = git_config_int(var, value);
     -+		min_curl_sessions = git_config_int(var, value, kvi);
     ++		min_curl_sessions = git_config_int(var, value, ctx->kvi);
       		if (min_curl_sessions > 1)
       			min_curl_sessions = 1;
       		return 0;
       	}
       	if (!strcmp("http.maxrequests", var)) {
      -		max_requests = git_config_int(var, value);
     -+		max_requests = git_config_int(var, value, kvi);
     ++		max_requests = git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp("http.lowspeedlimit", var)) {
      -		curl_low_speed_limit = (long)git_config_int(var, value);
     -+		curl_low_speed_limit = (long)git_config_int(var, value, kvi);
     ++		curl_low_speed_limit = (long)git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       	if (!strcmp("http.lowspeedtime", var)) {
      -		curl_low_speed_time = (long)git_config_int(var, value);
     -+		curl_low_speed_time = (long)git_config_int(var, value, kvi);
     ++		curl_low_speed_time = (long)git_config_int(var, value, ctx->kvi);
       		return 0;
       	}
       
     @@ http.c: static int http_options(const char *var, const char *value,
       
       	if (!strcmp("http.postbuffer", var)) {
      -		http_post_buffer = git_config_ssize_t(var, value);
     -+		http_post_buffer = git_config_ssize_t(var, value, kvi);
     ++		http_post_buffer = git_config_ssize_t(var, value, ctx->kvi);
       		if (http_post_buffer < 0)
       			warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
       		if (http_post_buffer < LARGE_PACKET_MAX)
     @@ imap-send.c: static int git_imap_config(const char *var, const char *val,
       		return git_config_string(&server.auth_method, var, val);
       	else if (!strcmp("imap.port", var))
      -		server.port = git_config_int(var, val);
     -+		server.port = git_config_int(var, val, kvi);
     ++		server.port = git_config_int(var, val, ctx->kvi);
       	else if (!strcmp("imap.host", var)) {
       		if (!val) {
       			git_die_config("imap.host", "Missing value for 'imap.host'");
     @@ sequencer.c: static int git_config_string_dup(char **dest,
       }
       
       static int populate_opts_cb(const char *key, const char *value,
     --			    struct key_value_info *kvi UNUSED, void *data)
     -+			    struct key_value_info *kvi, void *data)
     +-			    const struct config_context *ctx UNUSED,
     ++			    const struct config_context *ctx,
     + 			    void *data)
       {
       	struct replay_opts *opts = data;
     - 	int error_flag = 1;
      @@ sequencer.c: static int populate_opts_cb(const char *key, const char *value,
       	if (!value)
       		error_flag = 0;
       	else if (!strcmp(key, "options.no-commit"))
      -		opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
     -+		opts->no_commit = git_config_bool_or_int(key, value, kvi, &error_flag);
     ++		opts->no_commit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.edit"))
      -		opts->edit = git_config_bool_or_int(key, value, &error_flag);
     -+		opts->edit = git_config_bool_or_int(key, value, kvi, &error_flag);
     ++		opts->edit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.allow-empty"))
       		opts->allow_empty =
      -			git_config_bool_or_int(key, value, &error_flag);
     -+			git_config_bool_or_int(key, value, kvi, &error_flag);
     ++			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.allow-empty-message"))
       		opts->allow_empty_message =
      -			git_config_bool_or_int(key, value, &error_flag);
     -+			git_config_bool_or_int(key, value, kvi, &error_flag);
     ++			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.keep-redundant-commits"))
       		opts->keep_redundant_commits =
      -			git_config_bool_or_int(key, value, &error_flag);
     -+			git_config_bool_or_int(key, value, kvi, &error_flag);
     ++			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.signoff"))
      -		opts->signoff = git_config_bool_or_int(key, value, &error_flag);
     -+		opts->signoff = git_config_bool_or_int(key, value, kvi, &error_flag);
     ++		opts->signoff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.record-origin"))
      -		opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
     -+		opts->record_origin = git_config_bool_or_int(key, value, kvi, &error_flag);
     ++		opts->record_origin = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.allow-ff"))
      -		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
     -+		opts->allow_ff = git_config_bool_or_int(key, value, kvi, &error_flag);
     ++		opts->allow_ff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
       	else if (!strcmp(key, "options.mainline"))
      -		opts->mainline = git_config_int(key, value);
     -+		opts->mainline = git_config_int(key, value, kvi);
     ++		opts->mainline = git_config_int(key, value, ctx->kvi);
       	else if (!strcmp(key, "options.strategy"))
       		git_config_string_dup(&opts->strategy, key, value);
       	else if (!strcmp(key, "options.gpg-sign"))
      @@ sequencer.c: static int populate_opts_cb(const char *key, const char *value,
     - 		opts->xopts[opts->xopts_nr++] = xstrdup(value);
     + 		strvec_push(&opts->xopts, value);
       	} else if (!strcmp(key, "options.allow-rerere-auto"))
       		opts->allow_rerere_auto =
      -			git_config_bool_or_int(key, value, &error_flag) ?
     -+			git_config_bool_or_int(key, value, kvi, &error_flag) ?
     ++			git_config_bool_or_int(key, value, ctx->kvi, &error_flag) ?
       				RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
       	else if (!strcmp(key, "options.default-msg-cleanup")) {
       		opts->explicit_cleanup = 1;
     @@ setup.c: static int check_repo_format(const char *var, const char *value,
       
       	if (strcmp(var, "core.repositoryformatversion") == 0)
      -		data->version = git_config_int(var, value);
     -+		data->version = git_config_int(var, value, kvi);
     ++		data->version = git_config_int(var, value, ctx->kvi);
       	else if (skip_prefix(var, "extensions.", &ext)) {
       		switch (handle_extension_v0(var, value, ext, data)) {
       		case EXTENSION_ERROR:
     @@ submodule-config.c: static int parse_fetch_recurse(const char *opt, const char *
       
      -int parse_submodule_fetchjobs(const char *var, const char *value)
      +int parse_submodule_fetchjobs(const char *var, const char *value,
     -+			      struct key_value_info *kvi)
     ++			      const struct key_value_info *kvi)
       {
      -	int fetchjobs = git_config_int(var, value);
      +	int fetchjobs = git_config_int(var, value, kvi);
     @@ submodule-config.c: struct fetch_config {
       };
       
       static int gitmodules_fetch_config(const char *var, const char *value,
     --				   struct key_value_info *kvi UNUSED,
     --				   void *cb)
     -+				   struct key_value_info *kvi, void *cb)
     +-				   const struct config_context *ctx UNUSED,
     ++				   const struct config_context *ctx,
     + 				   void *cb)
       {
       	struct fetch_config *config = cb;
       	if (!strcmp(var, "submodule.fetchjobs")) {
       		if (config->max_children)
       			*(config->max_children) =
      -				parse_submodule_fetchjobs(var, value);
     -+				parse_submodule_fetchjobs(var, value, kvi);
     ++				parse_submodule_fetchjobs(var, value, ctx->kvi);
       		return 0;
       	} else if (!strcmp(var, "fetch.recursesubmodules")) {
       		if (config->recurse_submodules)
     @@ submodule-config.c: void fetch_config_from_gitmodules(int *max_children, int *re
       }
       
       static int gitmodules_update_clone_config(const char *var, const char *value,
     --					  struct key_value_info *kvi UNUSED,
     --					  void *cb)
     -+					  struct key_value_info *kvi, void *cb)
     +-					  const struct config_context *ctx UNUSED,
     ++					  const struct config_context *ctx,
     + 					  void *cb)
       {
       	int *max_jobs = cb;
       	if (!strcmp(var, "submodule.fetchjobs"))
      -		*max_jobs = parse_submodule_fetchjobs(var, value);
     -+		*max_jobs = parse_submodule_fetchjobs(var, value, kvi);
     ++		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
       	return 0;
       }
       
     @@ submodule-config.h: struct repository;
       
      -int parse_submodule_fetchjobs(const char *var, const char *value);
      +int parse_submodule_fetchjobs(const char *var, const char *value,
     -+			      struct key_value_info *kvi);
     ++			      const struct key_value_info *kvi);
       int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
       struct option;
       int option_fetch_parse_recurse_submodules(const struct option *opt,
     @@ t/helper/test-config.c: static int iterate_cb(const char *var, const char *value
       }
       
       static int parse_int_cb(const char *var, const char *value,
     --			struct key_value_info *kvi UNUSED, void *data)
     -+			struct key_value_info *kvi, void *data)
     +-			const struct config_context *ctx UNUSED, void *data)
     ++			const struct config_context *ctx, void *data)
       {
       	const char *key_to_match = data;
       
       	if (!strcmp(key_to_match, var)) {
      -		int parsed = git_config_int(value, value);
     -+		int parsed = git_config_int(value, value, kvi);
     ++		int parsed = git_config_int(value, value, ctx->kvi);
       		printf("%d\n", parsed);
       	}
       	return 0;
     @@ t/helper/test-config.c: int cmd__config(int argc, const char **argv)
       				printf("(NULL)\n");
       			else
      
     + ## trace2/tr2_cfg.c ##
     +@@ trace2/tr2_cfg.c: struct tr2_cfg_data {
     +  * See if the given config key matches any of our patterns of interest.
     +  */
     + static int tr2_cfg_cb(const char *key, const char *value,
     +-		      const struct config_context *ctx UNUSED, void *d)
     ++		      const struct config_context *ctx, void *d)
     + {
     + 	struct strbuf **s;
     + 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
     +
       ## upload-pack.c ##
      @@ upload-pack.c: static int find_symref(const char *refname,
       }
       
       static int parse_object_filter_config(const char *var, const char *value,
      -				       struct upload_pack_data *data)
     -+				      struct key_value_info *kvi,
     ++				      const struct key_value_info *kvi,
      +				      struct upload_pack_data *data)
       {
       	struct strbuf buf = STRBUF_INIT;
     @@ upload-pack.c: static int parse_object_filter_config(const char *var, const char
       }
       
       static int upload_pack_config(const char *var, const char *value,
     --			      struct key_value_info *kvi UNUSED,
     -+			      struct key_value_info *kvi,
     +-			      const struct config_context *ctx UNUSED,
     ++			      const struct config_context *ctx,
       			      void *cb_data)
       {
       	struct upload_pack_data *data = cb_data;
     @@ upload-pack.c: static int upload_pack_config(const char *var, const char *value,
       			data->allow_uor &= ~ALLOW_ANY_SHA1;
       	} else if (!strcmp("uploadpack.keepalive", var)) {
      -		data->keepalive = git_config_int(var, value);
     -+		data->keepalive = git_config_int(var, value, kvi);
     ++		data->keepalive = git_config_int(var, value, ctx->kvi);
       		if (!data->keepalive)
       			data->keepalive = -1;
       	} else if (!strcmp("uploadpack.allowfilter", var)) {
     @@ upload-pack.c: static int upload_pack_config(const char *var, const char *value,
       	}
       
      -	if (parse_object_filter_config(var, value, data) < 0)
     -+	if (parse_object_filter_config(var, value, kvi, data) < 0)
     ++	if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
       		return -1;
       
       	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
 12:  52db0d3be82 ! 10:  8ec24b018e9 config.c: remove config_reader from configsets
     @@ config.c: int config_with_options(config_fn_t fn, void *data,
      @@ config.c: static int configset_find_element(struct config_set *set, const char *key,
       }
       
     - static int configset_add_value(struct key_value_info *kvi_p,
     + static int configset_add_value(const struct key_value_info *kvi_p,
      -			       struct config_reader *reader,
       			       struct config_set *set, const char *key,
       			       const char *value)
       {
     -@@ config.c: static int configset_add_value(struct key_value_info *kvi_p,
     +@@ config.c: static int configset_add_value(const struct key_value_info *kvi_p,
       	l_item->e = e;
       	l_item->value_index = e->value_list.nr - 1;
       
     @@ config.c: void git_configset_clear(struct config_set *set)
      -#define CONFIGSET_ADD_INIT { 0 }
      -
       static int config_set_callback(const char *key, const char *value,
     - 			       struct key_value_info *kvi, void *cb)
     + 			       const struct config_context *ctx,
     + 			       void *cb)
       {
      -	struct configset_add_data *data = cb;
     --	configset_add_value(kvi, data->config_reader, data->config_set, key, value);
     +-	configset_add_value(ctx->kvi, data->config_reader, data->config_set,
     +-			    key, value);
      +	struct config_set *set = cb;
     -+	configset_add_value(kvi, set, key, value);
     ++	configset_add_value(ctx->kvi, set, key, value);
       	return 0;
       }
       
 13:  7d9b9eefc78 ! 11:  8ae115cff88 config: add kvi.path, use it to evaluate includes
     @@ Commit message
          Include directives are evaluated using the path of the config file. To
          reduce the dependence on "config_reader.source", add a new
          "key_value_info.path" member and use that instead of
     -    "config_source.path".
     +    "config_source.path". This allows us to remove a "struct config_reader
     +    *" field from "struct config_include_data", which will subsequently
     +    allow us to remove "struct config_reader" entirely.
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     @@ config.c: static const char include_depth_advice[] = N_(
       "	%s\n"
       "This might be due to circular includes.");
      -static int handle_path_include(struct config_source *cs,
     --			       struct key_value_info *kvi,
     --			       const char *path,
     -+static int handle_path_include(struct key_value_info *kvi, const char *path,
     +-			       const struct key_value_info *kvi,
     ++static int handle_path_include(const struct key_value_info *kvi,
     + 			       const char *path,
       			       struct config_include_data *inc)
       {
     - 	int ret = 0;
      @@ config.c: static int handle_path_include(struct config_source *cs,
       	if (!is_absolute_path(path)) {
       		char *slash;
     @@ config.c: static void add_trailing_starstar_for_dir(struct strbuf *pat)
       }
       
      -static int prepare_include_condition_pattern(struct config_source *cs,
     -+static int prepare_include_condition_pattern(struct key_value_info *kvi,
     ++static int prepare_include_condition_pattern(const struct key_value_info *kvi,
       					     struct strbuf *pat)
       {
       	struct strbuf path = STRBUF_INIT;
     @@ config.c: static int prepare_include_condition_pattern(struct config_source *cs,
       }
       
      -static int include_by_gitdir(struct config_source *cs,
     -+static int include_by_gitdir(struct key_value_info *kvi,
     ++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)
       {
     @@ config.c: static int include_by_remote_url(struct config_include_data *inc,
       }
       
      -static int include_condition_is_true(struct config_source *cs,
     -+static int include_condition_is_true(struct key_value_info *kvi,
     ++static int include_condition_is_true(const struct key_value_info *kvi,
       				     struct config_include_data *inc,
       				     const char *cond, size_t cond_len)
       {
     @@ config.c: static int include_by_remote_url(struct config_include_data *inc,
       		return include_by_branch(cond, cond_len);
       	else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
      @@ config.c: static int git_config_include(const char *var, const char *value,
     - 			      struct key_value_info *kvi, void *data)
     + 			      void *data)
       {
       	struct config_include_data *inc = data;
      -	struct config_source *cs = inc->config_reader->source;
     @@ config.c: static int git_config_include(const char *var, const char *value,
       		return ret;
       
       	if (!strcmp(var, "include.path"))
     --		ret = handle_path_include(cs, kvi, value, inc);
     -+		ret = handle_path_include(kvi, value, inc);
     +-		ret = handle_path_include(cs, ctx->kvi, 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(kvi, 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, kvi, value, inc);
     -+		ret = handle_path_include(kvi, value, inc);
     +-		ret = handle_path_include(cs, ctx->kvi, value, inc);
     ++		ret = handle_path_include(ctx->kvi, value, inc);
       		inc->fn = old_fn;
       	}
       
     @@ config.h: struct key_value_info {
       	enum config_scope scope;
      +	const char *path;
       };
     + #define KVI_INIT { \
     + 	.filename = NULL, \
     + 	.linenr = -1, \
     + 	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
     + 	.scope = CONFIG_SCOPE_UNKNOWN, \
     ++	.path = NULL, \
     + }
       
     - /**
     + /* Captures additional information that a config callback can use. */
 14:  9e35b5b1f4d ! 12:  484d553cc7d config: pass source to config_parser_event_fn_t
     @@ Commit message
      
          ..so that the callback can use a "struct config_source" parameter
          instead of "config_reader.source". "struct config_source" is internal to
     -    config.c, but this refactor is okay because this function has only ever
     -    been (and probably ever will be) used internally by config.c.
     +    config.c, so we are adding a pointer to a struct defined in config.c
     +    into a public function signature defined in config.h, but this is okay
     +    because this function has only ever been (and probably ever will be)
     +    used internally by config.c.
      
          As a result, the_reader isn't used anywhere, so "struct config_reader"
          is obsolete (it was only intended to be used with the_reader). Remove
     @@ config.c: int git_config_from_parameters(config_fn_t fn, void *data)
       	int ret = 0;
       	char *envw = NULL;
      -	struct config_source source = CONFIG_SOURCE_INIT;
     - 	struct key_value_info kvi = { 0 };
     + 	struct key_value_info kvi = KVI_INIT;
       
      -	source.origin_type = CONFIG_ORIGIN_CMDLINE;
      -	config_reader_push_source(&the_reader, &source);

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v3 01/12] config: inline git_color_default_config
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 21:01       ` Junio C Hamano
  2023-06-20 19:43     ` [PATCH v3 02/12] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
                       ` (12 subsequent siblings)
  13 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

git_color_default_config() is a shorthand for calling two other config
callbacks. There are no other non-static functions that do this and it
will complicate our refactoring of config_fn_t so inline it instead.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/branch.c      | 5 ++++-
 builtin/clean.c       | 6 ++++--
 builtin/grep.c        | 5 ++++-
 builtin/show-branch.c | 5 ++++-
 builtin/tag.c         | 6 +++++-
 color.c               | 8 --------
 color.h               | 6 +-----
 7 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index e6c2655af68..df99e38847b 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -117,7 +117,10 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/clean.c b/builtin/clean.c
index 78852d28cec..57e7f7cac64 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -130,8 +130,10 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	/* inspect the color.ui config variable and others */
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/grep.c b/builtin/grep.c
index b86c754defb..76cf999d310 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -293,7 +293,10 @@ static int wait_all(void)
 static int grep_cmd_config(const char *var, const char *value, void *cb)
 {
 	int st = grep_config(var, value, cb);
-	if (git_color_default_config(var, value, NULL) < 0)
+
+	if (git_color_config(var, value, cb) < 0)
+		st = -1;
+	else if (git_default_config(var, value, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 7ef4a642c17..a2461270d4b 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -579,7 +579,10 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/tag.c b/builtin/tag.c
index 49b64c7a288..1acf5f7a59f 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -209,7 +209,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 
 	if (starts_with(var, "column."))
 		return git_column_config(var, value, "tag", &colopts);
-	return git_color_default_config(var, value, cb);
+
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/color.c b/color.c
index 83abb11eda0..b24b19566b9 100644
--- a/color.c
+++ b/color.c
@@ -430,14 +430,6 @@ int git_color_config(const char *var, const char *value, void *cb UNUSED)
 	return 0;
 }
 
-int git_color_default_config(const char *var, const char *value, void *cb)
-{
-	if (git_color_config(var, value, cb) < 0)
-		return -1;
-
-	return git_default_config(var, value, cb);
-}
-
 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
 {
 	if (*color)
diff --git a/color.h b/color.h
index cfc8f841b23..bb28343be21 100644
--- a/color.h
+++ b/color.h
@@ -88,12 +88,8 @@ extern const int column_colors_ansi_max;
  */
 extern int color_stdout_is_tty;
 
-/*
- * Use the first one if you need only color config; the second is a convenience
- * if you are just going to change to git_default_config, too.
- */
+/* Parse color config. */
 int git_color_config(const char *var, const char *value, void *cb);
-int git_color_default_config(const char *var, const char *value, void *cb);
 
 /*
  * Parse a config option, which can be a boolean or one of
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 02/12] urlmatch.h: use config_fn_t type
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 21:02       ` Junio C Hamano
  2023-06-20 19:43     ` [PATCH v3 03/12] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
                       ` (11 subsequent siblings)
  13 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

These are actually used as config callbacks, so use the typedef-ed type
and make future refactors easier.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 urlmatch.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/urlmatch.h b/urlmatch.h
index 9f40b00bfb8..bee374a642c 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -2,6 +2,7 @@
 #define URL_MATCH_H
 
 #include "string-list.h"
+#include "config.h"
 
 struct url_info {
 	/* normalized url on success, must be freed, otherwise NULL */
@@ -48,8 +49,8 @@ struct urlmatch_config {
 	const char *key;
 
 	void *cb;
-	int (*collect_fn)(const char *var, const char *value, void *cb);
-	int (*cascade_fn)(const char *var, const char *value, void *cb);
+	config_fn_t collect_fn;
+	config_fn_t cascade_fn;
 	/*
 	 * Compare the two matches, the one just discovered and the existing
 	 * best match and return a negative value if the found item is to be
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 03/12] config: add ctx arg to config_fn_t
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 02/12] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 04/12] config.c: pass ctx in configsets Glen Choo via GitGitGadget
                       ` (10 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Add a new "const struct config_context *ctx" arg to config_fn_t to hold
additional information about the config iteration operation.
config_context has a "struct key_value_info kvi" member that holds
metadata about the config source being read (e.g. what kind of config
source it is, the filename, etc). In this series, we're only interested
in .kvi, so we could have just used "struct key_value_info" as an arg,
but config_context makes it possible to add/adjust members in the future
without changing the config_fn_t signature. We could also consider other
ways of organizing the args (e.g. moving the config name and value into
config_context or key_value_info), but in my experiments, the
incremental benefit doesn't justify the added complexity (e.g. a
config_fn_t will sometimes invoke another config_fn_t but with a
different config value).

In subsequent commits, the .kvi member will replace the global "struct
config_reader" in config.c, making config iteration a global-free
operation. It requires much more work for the machinery to provide
meaningful values of .kvi, so for now, merely change the signature and
call sites, pass NULL as a placeholder value, and don't rely on the arg
in any meaningful way.

Most of the changes are performed by
contrib/coccinelle/config_fn_ctx.pending.cocci, which, for every
config_fn_t:

- Modifies the signature to accept "const struct config_context *ctx"
- Passes "ctx" to any inner config_fn_t, if needed
- Adds UNUSED attributes to "ctx", if needed

Most config_fn_t instances are easily identified by seeing if they are
called by the various config functions. Most of the remaining ones are
manually named in the .cocci patch. Manual cleanups are still needed,
but the majority of it is trivial; it's either adjusting config_fn_t
that the .cocci patch didn't catch, or adding forward declarations of
"struct config_context ctx" to make the signatures make sense.

The non-trivial changes are in cases where we are invoking a config_fn_t
outside of config machinery, and we now need to decide what value of
"ctx" to pass. These cases are:

- trace2/tr2_cfg.c:tr2_cfg_set_fl()

  This is indirectly called by git_config_set() so that the trace2
  machinery can notice the new config values and update its settings
  using the tr2 config parsing function, i.e. tr2_cfg_cb().

- builtin/checkout.c:checkout_main()

  This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
  This might be worth refactoring away in the future, since
  git_xmerge_config() can call git_default_config(), which can do much
  more than just parsing.

Handle them by creating a KVI_INIT macro that initializes "struct
key_value_info" to a reasonable default, and use that to construct the
"ctx" arg.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 alias.c                                       |   3 +-
 archive-tar.c                                 |   3 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   5 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   5 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |  12 +-
 builtin/clean.c                               |   5 +-
 builtin/clone.c                               |  11 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   1 +
 builtin/commit.c                              |  10 +-
 builtin/config.c                              |  10 +-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |   9 +-
 builtin/fsmonitor--daemon.c                   |   5 +-
 builtin/grep.c                                |   7 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   5 +-
 builtin/log.c                                 |  10 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |   5 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |   5 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |   7 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   5 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   1 +
 builtin/tag.c                                 |   5 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   8 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      |  38 +++--
 config.h                                      |  41 +++--
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_ctx.pending.cocci    | 144 ++++++++++++++++++
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   4 +-
 diff.c                                        |  10 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   5 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |   9 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   7 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |   7 +-
 http.c                                        |   5 +-
 ident.c                                       |   4 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   5 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   1 +
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   4 +-
 notes.c                                       |   4 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   3 +-
 revision.c                                    |   4 +-
 scalar.c                                      |   4 +-
 sequencer.c                                   |   9 +-
 setup.c                                       |  16 +-
 submodule-config.c                            |  17 ++-
 t/helper/test-config.c                        |  11 +-
 t/helper/test-userdiff.c                      |   4 +-
 trace2/tr2_cfg.c                              |   9 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |   8 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   3 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   4 +-
 91 files changed, 515 insertions(+), 181 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci

diff --git a/alias.c b/alias.c
index 54a1a23d2cf..910dd252a01 100644
--- a/alias.c
+++ b/alias.c
@@ -12,7 +12,8 @@ struct config_alias_data {
 	struct string_list *list;
 };
 
-static int config_alias_cb(const char *key, const char *value, void *d)
+static int config_alias_cb(const char *key, const char *value,
+			   const struct config_context *ctx UNUSED, void *d)
 {
 	struct config_alias_data *data = d;
 	const char *p;
diff --git a/archive-tar.c b/archive-tar.c
index 4cd81d8161e..ef06e516b1f 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -411,7 +411,8 @@ static int tar_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int git_tar_config(const char *var, const char *value, void *cb)
+static int git_tar_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
diff --git a/archive-zip.c b/archive-zip.c
index d0d065a312e..b6811951955 100644
--- a/archive-zip.c
+++ b/archive-zip.c
@@ -617,6 +617,7 @@ static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
 }
 
 static int archive_zip_config(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
 			      void *data UNUSED)
 {
 	return userdiff_config(var, value);
diff --git a/builtin/add.c b/builtin/add.c
index 76cc026a68a..23a93b71d93 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -357,7 +357,8 @@ static struct option builtin_add_options[] = {
 	OPT_END(),
 };
 
-static int add_config(const char *var, const char *value, void *cb)
+static int add_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "add.ignoreerrors") ||
 	    !strcmp(var, "add.ignore-errors")) {
@@ -365,7 +366,7 @@ static int add_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char embedded_advice[] = N_(
diff --git a/builtin/blame.c b/builtin/blame.c
index 2df6039a6e0..d0970a1ab13 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -694,7 +694,8 @@ static const char *add_prefix(const char *prefix, const char *path)
 	return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 }
 
-static int git_blame_config(const char *var, const char *value, void *cb)
+static int git_blame_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "blame.showroot")) {
 		show_root = git_config_bool(var, value);
@@ -767,7 +768,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
diff --git a/builtin/branch.c b/builtin/branch.c
index df99e38847b..23bfb4a771a 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -83,7 +83,8 @@ static unsigned int colopts;
 
 define_list_config_array(color_branch_slots);
 
-static int git_branch_config(const char *var, const char *value, void *cb)
+static int git_branch_config(const char *var, const char *value,
+			     const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -120,7 +121,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 0bafc14e6c0..a7d88c055a0 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -885,12 +885,13 @@ static int batch_objects(struct batch_options *opt)
 	return retval;
 }
 
-static int git_cat_file_config(const char *var, const char *value, void *cb)
+static int git_cat_file_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int batch_option_callback(const struct option *opt,
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 715eeb5048f..4e1f7dc26c2 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1186,7 +1186,8 @@ static int switch_branches(const struct checkout_opts *opts,
 	return ret || writeout_error;
 }
 
-static int git_checkout_config(const char *var, const char *value, void *cb)
+static int git_checkout_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	struct checkout_opts *opts = cb;
 
@@ -1202,7 +1203,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
 
-	return git_xmerge_config(var, value, NULL);
+	return git_xmerge_config(var, value, ctx, NULL);
 }
 
 static void setup_new_branch_info_and_source_tree(
@@ -1689,8 +1690,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 	}
 
 	if (opts->conflict_style) {
+		struct key_value_info kvi = KVI_INIT;
+		struct config_context ctx = {
+			.kvi = &kvi,
+		};
 		opts->merge = 1; /* implied */
-		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
+		git_xmerge_config("merge.conflictstyle", opts->conflict_style,
+				  &ctx, NULL);
 	}
 	if (opts->force) {
 		opts->discard_changes = 1;
diff --git a/builtin/clean.c b/builtin/clean.c
index 57e7f7cac64..5eff1b802a7 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -103,7 +103,8 @@ struct menu_stuff {
 
 define_list_config_array(color_interactive_slots);
 
-static int git_clean_config(const char *var, const char *value, void *cb)
+static int git_clean_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -133,7 +134,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/clone.c b/builtin/clone.c
index 15f9912b4ca..c0f6e067493 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -790,7 +790,8 @@ static int checkout(int submodule_progress, int filter_submodules)
 	return err;
 }
 
-static int git_clone_config(const char *k, const char *v, void *cb)
+static int git_clone_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "clone.defaultremotename")) {
 		free(remote_name);
@@ -801,17 +802,19 @@ static int git_clone_config(const char *k, const char *v, void *cb)
 	if (!strcmp(k, "clone.filtersubmodules"))
 		config_filter_submodules = git_config_bool(k, v);
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
-static int write_one_config(const char *key, const char *value, void *data)
+static int write_one_config(const char *key, const char *value,
+			    const struct config_context *ctx,
+			    void *data)
 {
 	/*
 	 * give git_clone_config a chance to write config values back to the
 	 * environment, since git_config_set_multivar_gently only deals with
 	 * config-file writes
 	 */
-	int apply_failed = git_clone_config(key, value, data);
+	int apply_failed = git_clone_config(key, value, ctx, data);
 	if (apply_failed)
 		return apply_failed;
 
diff --git a/builtin/column.c b/builtin/column.c
index de623a16c2d..4a6148ca479 100644
--- a/builtin/column.c
+++ b/builtin/column.c
@@ -13,7 +13,8 @@ static const char * const builtin_column_usage[] = {
 };
 static unsigned int colopts;
 
-static int column_config(const char *var, const char *value, void *cb)
+static int column_config(const char *var, const char *value,
+			 const struct config_context *ctx UNUSED, void *cb)
 {
 	return git_column_config(var, value, cb, &colopts);
 }
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index a3d00fa232b..13d35b00ca8 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -186,6 +186,7 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
+					 const struct config_context *ctx UNUSED,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be2211..3bc87e5fc97 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1405,7 +1405,8 @@ static int parse_status_slot(const char *slot)
 	return LOOKUP_CONFIG(color_status_slots, slot);
 }
 
-static int git_status_config(const char *k, const char *v, void *cb)
+static int git_status_config(const char *k, const char *v,
+			     const struct config_context *ctx, void *cb)
 {
 	struct wt_status *s = cb;
 	const char *slot_name;
@@ -1490,7 +1491,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
 		s->detect_rename = git_config_rename(k, v);
 		return 0;
 	}
-	return git_diff_ui_config(k, v, NULL);
+	return git_diff_ui_config(k, v, ctx, NULL);
 }
 
 int cmd_status(int argc, const char **argv, const char *prefix)
@@ -1605,7 +1606,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int git_commit_config(const char *k, const char *v, void *cb)
+static int git_commit_config(const char *k, const char *v,
+			     const struct config_context *ctx, void *cb)
 {
 	struct wt_status *s = cb;
 
@@ -1627,7 +1629,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_status_config(k, v, s);
+	return git_status_config(k, v, ctx, s);
 }
 
 int cmd_commit(int argc, const char **argv, const char *prefix)
diff --git a/builtin/config.c b/builtin/config.c
index ff2fe8ef125..e14f967bbea 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -216,6 +216,7 @@ static void show_config_scope(struct strbuf *buf)
 }
 
 static int show_all_config(const char *key_, const char *value_,
+			   const struct config_context *ctx UNUSED,
 			   void *cb UNUSED)
 {
 	if (show_origin || show_scope) {
@@ -300,7 +301,8 @@ 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 UNUSED, void *cb)
 {
 	struct strbuf_list *values = cb;
 
@@ -468,6 +470,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)) {
@@ -500,6 +503,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))
@@ -557,7 +561,9 @@ struct urlmatch_current_candidate_value {
 	struct strbuf value;
 };
 
-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 UNUSED,
+			       void *cb)
 {
 	struct string_list *values = cb;
 	struct string_list_item *item = string_list_insert(values, var);
diff --git a/builtin/difftool.c b/builtin/difftool.c
index 0049342f5c0..f289530068b 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -40,14 +40,15 @@ static const char *const builtin_difftool_usage[] = {
 	NULL
 };
 
-static int difftool_config(const char *var, const char *value, void *cb)
+static int difftool_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "difftool.trustexitcode")) {
 		trust_exit_code = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int print_tool_help(void)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 849a9be421d..b607605b491 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -111,7 +111,8 @@ struct fetch_config {
 	enum display_format display_format;
 };
 
-static int git_fetch_config(const char *k, const char *v, void *cb)
+static int git_fetch_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	struct fetch_config *fetch_config = cb;
 
@@ -165,7 +166,7 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
 			    "fetch.output", v);
 	}
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
@@ -1794,7 +1795,9 @@ struct remote_group_data {
 	struct string_list *list;
 };
 
-static int get_remote_group(const char *key, const char *value, void *priv)
+static int get_remote_group(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *priv)
 {
 	struct remote_group_data *g = priv;
 
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index f6dd9a784c1..91a776e2f17 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -37,7 +37,8 @@ static int fsmonitor__start_timeout_sec = 60;
 #define FSMONITOR__ANNOUNCE_STARTUP "fsmonitor.announcestartup"
 static int fsmonitor__announce_startup = 0;
 
-static int fsmonitor_config(const char *var, const char *value, void *cb)
+static int fsmonitor_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
 		int i = git_config_int(var, value);
@@ -67,7 +68,7 @@ static int fsmonitor_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/builtin/grep.c b/builtin/grep.c
index 76cf999d310..757d52b94ec 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -290,13 +290,14 @@ static int wait_all(void)
 	return hit;
 }
 
-static int grep_cmd_config(const char *var, const char *value, void *cb)
+static int grep_cmd_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
-	int st = grep_config(var, value, cb);
+	int st = grep_config(var, value, ctx, cb);
 
 	if (git_color_config(var, value, cb) < 0)
 		st = -1;
-	else if (git_default_config(var, value, cb) < 0)
+	else if (git_default_config(var, value, ctx, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/help.c b/builtin/help.c
index d3cf4af3f6e..c348f201254 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -398,7 +398,8 @@ static int add_man_viewer_info(const char *var, const char *value)
 	return 0;
 }
 
-static int git_help_config(const char *var, const char *value, void *cb)
+static int git_help_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "help.format")) {
 		if (!value)
@@ -421,7 +422,7 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index bb67e166559..31914aac298 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1581,7 +1581,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
 	strbuf_release(&pack_name);
 }
 
-static int git_index_pack_config(const char *k, const char *v, void *cb)
+static int git_index_pack_config(const char *k, const char *v,
+				 const struct config_context *ctx, void *cb)
 {
 	struct pack_idx_option *opts = cb;
 
@@ -1608,7 +1609,7 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
 		else
 			opts->flags &= ~WRITE_REV;
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 static int cmp_uint32(const void *a_, const void *b_)
diff --git a/builtin/log.c b/builtin/log.c
index 4c45a47ecf4..ac5f74b6b06 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -564,7 +564,8 @@ static int cmd_log_walk(struct rev_info *rev)
 	return retval;
 }
 
-static int git_log_config(const char *var, const char *value, void *cb)
+static int git_log_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -613,7 +614,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_diff_ui_config(var, value, cb);
+	return git_diff_ui_config(var, value, ctx, cb);
 }
 
 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
@@ -979,7 +980,8 @@ static enum cover_from_description parse_cover_from_description(const char *arg)
 		die(_("%s: invalid cover from description mode"), arg);
 }
 
-static int git_format_config(const char *var, const char *value, void *cb)
+static int git_format_config(const char *var, const char *value,
+			     const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "format.headers")) {
 		if (!value)
@@ -1108,7 +1110,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "diff.noprefix"))
 		return 0;
 
-	return git_log_config(var, value, cb);
+	return git_log_config(var, value, ctx, cb);
 }
 
 static const char *output_directory = NULL;
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb0..cad624fb797 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -623,7 +623,8 @@ static void parse_branch_merge_options(char *bmo)
 	free(argv);
 }
 
-static int git_merge_config(const char *k, const char *v, void *cb)
+static int git_merge_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	int status;
 	const char *str;
@@ -668,10 +669,10 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	status = fmt_merge_msg_config(k, v, cb);
+	status = fmt_merge_msg_config(k, v, ctx, cb);
 	if (status)
 		return status;
-	return git_diff_ui_config(k, v, cb);
+	return git_diff_ui_config(k, v, ctx, cb);
 }
 
 static int read_tree_trivial(struct object_id *common, struct object_id *head,
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 1b5083f8b26..a0a7b82cc69 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -82,6 +82,7 @@ static struct option *add_common_options(struct option *prev)
 }
 
 static int git_multi_pack_index_write_config(const char *var, const char *value,
+					     const struct config_context *ctx UNUSED,
 					     void *cb UNUSED)
 {
 	if (!strcmp(var, "pack.writebitmaphashcache")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 9cfc8801f9b..ad4d8fafb0b 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3135,7 +3135,8 @@ static void prepare_pack(int window, int depth)
 	free(delta_list);
 }
 
-static int git_pack_config(const char *k, const char *v, void *cb)
+static int git_pack_config(const char *k, const char *v,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
 		window = git_config_int(k, v);
@@ -3227,7 +3228,7 @@ static int git_pack_config(const char *k, const char *v, void *cb)
 		ex->uri = xstrdup(pack_end + 1);
 		oidmap_put(&configured_exclusions, ex);
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 /* Counters for trace2 output when in --stdin-packs mode. */
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 9d5585d3a72..03eddd0fb82 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -196,7 +196,8 @@ struct patch_id_opts {
 	int verbatim;
 };
 
-static int git_patch_id_config(const char *var, const char *value, void *cb)
+static int git_patch_id_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	struct patch_id_opts *opts = cb;
 
@@ -209,7 +210,7 @@ static int git_patch_id_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_patch_id(int argc, const char **argv, const char *prefix)
diff --git a/builtin/pull.c b/builtin/pull.c
index 0c7bac97b75..83fca5b1d46 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -361,7 +361,8 @@ static enum rebase_type config_get_rebase(int *rebase_unspecified)
 /**
  * Read config variables.
  */
-static int git_pull_config(const char *var, const char *value, void *cb)
+static int git_pull_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "rebase.autostash")) {
 		config_autostash = git_config_bool(var, value);
@@ -374,7 +375,7 @@ static int git_pull_config(const char *var, const char *value, void *cb)
 		check_trust_level = 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /**
diff --git a/builtin/push.c b/builtin/push.c
index dbdf609daf3..a2f68f77324 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -510,7 +510,8 @@ static void set_push_cert_flags(int *flags, int v)
 }
 
 
-static int git_push_config(const char *k, const char *v, void *cb)
+static int git_push_config(const char *k, const char *v,
+			   const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 	int *flags = cb;
@@ -577,7 +578,7 @@ static int git_push_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_default_config(k, v, NULL);
+	return git_default_config(k, v, ctx, NULL);
 }
 
 int cmd_push(int argc, const char **argv, const char *prefix)
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 440f19b1b87..8877dd6d4b5 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -102,12 +102,13 @@ static int debug_merge(const struct cache_entry * const *stages,
 	return 0;
 }
 
-static int git_read_tree_config(const char *var, const char *value, void *cb)
+static int git_read_tree_config(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d11..60930e2d8e0 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -772,7 +772,8 @@ static void parse_rebase_merges_value(struct rebase_options *options, const char
 		die(_("Unknown rebase-merges mode: %s"), value);
 }
 
-static int rebase_config(const char *var, const char *value, void *data)
+static int rebase_config(const char *var, const char *value,
+			 const struct config_context *ctx, void *data)
 {
 	struct rebase_options *opts = data;
 
@@ -831,7 +832,7 @@ static int rebase_config(const char *var, const char *value, void *data)
 		return git_config_string(&opts->default_backend, var, value);
 	}
 
-	return git_default_config(var, value, data);
+	return git_default_config(var, value, ctx, data);
 }
 
 static int checkout_up_to_date(struct rebase_options *options)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 1a31a583674..94d9898aff7 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -139,7 +139,8 @@ static enum deny_action parse_deny_action(const char *var, const char *value)
 	return DENY_IGNORE;
 }
 
-static int receive_pack_config(const char *var, const char *value, void *cb)
+static int receive_pack_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
 
@@ -266,7 +267,7 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void show_ref(const char *path, const struct object_id *oid)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index a1fa0c855f4..84251cc9517 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -108,7 +108,8 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
 #define EXPIRE_TOTAL   01
 #define EXPIRE_UNREACH 02
 
-static int reflog_expire_config(const char *var, const char *value, void *cb)
+static int reflog_expire_config(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	const char *pattern, *key;
 	size_t pattern_len;
@@ -117,7 +118,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 	struct reflog_expire_cfg *ent;
 
 	if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value, ctx, cb);
 
 	if (!strcmp(key, "reflogexpire")) {
 		slot = EXPIRE_TOTAL;
@@ -128,7 +129,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 		if (git_config_expiry_date(&expire, var, value))
 			return -1;
 	} else
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value, ctx, cb);
 
 	if (!pattern) {
 		switch (slot) {
diff --git a/builtin/remote.c b/builtin/remote.c
index 1e0b137d977..87de81105e2 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -268,6 +268,7 @@ static const char *abbrev_ref(const char *name, const char *prefix)
 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
 
 static int config_read_branches(const char *key, const char *value,
+				const struct config_context *ctx UNUSED,
 				void *data UNUSED)
 {
 	const char *orig_key = key;
@@ -645,7 +646,7 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	void *cb)
+	const struct config_context *ctx UNUSED, void *cb)
 {
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
@@ -1494,7 +1495,9 @@ static int prune(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
+static int get_remote_default(const char *key, const char *value UNUSED,
+			      const struct config_context *ctx UNUSED,
+			      void *priv)
 {
 	if (strcmp(key, "remotes.default") == 0) {
 		int *found = priv;
diff --git a/builtin/repack.c b/builtin/repack.c
index 0541c3ce157..6f74570bf94 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -59,7 +59,8 @@ struct pack_objects_args {
 	int local;
 };
 
-static int repack_config(const char *var, const char *value, void *cb)
+static int repack_config(const char *var, const char *value,
+			 const struct config_context *ctx, void *cb)
 {
 	struct pack_objects_args *cruft_po_args = cb;
 	if (!strcmp(var, "repack.usedeltabaseoffset")) {
@@ -91,7 +92,7 @@ static int repack_config(const char *var, const char *value, void *cb)
 		return git_config_string(&cruft_po_args->depth, var, value);
 	if (!strcmp(var, "repack.cruftthreads"))
 		return git_config_string(&cruft_po_args->threads, var, value);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/builtin/reset.c b/builtin/reset.c
index f99f32d5802..1ae82f2e89c 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -312,12 +312,13 @@ static int reset_refs(const char *rev, const struct object_id *oid)
 	return update_ref_status;
 }
 
-static int git_reset_config(const char *var, const char *value, void *cb)
+static int git_reset_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_reset(int argc, const char **argv, const char *prefix)
diff --git a/builtin/send-pack.c b/builtin/send-pack.c
index 4784143004d..cd6d9e41129 100644
--- a/builtin/send-pack.c
+++ b/builtin/send-pack.c
@@ -131,7 +131,8 @@ static void print_helper_status(struct ref *ref)
 	strbuf_release(&buf);
 }
 
-static int send_pack_config(const char *k, const char *v, void *cb)
+static int send_pack_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "push.gpgsign")) {
 		const char *value;
@@ -151,7 +152,7 @@ static int send_pack_config(const char *k, const char *v, void *cb)
 			}
 		}
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 int cmd_send_pack(int argc, const char **argv, const char *prefix)
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index a2461270d4b..f2fd245b838 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -559,7 +559,8 @@ static void append_one_rev(const char *av)
 	die("bad sha1 reference %s", av);
 }
 
-static int git_show_branch_config(const char *var, const char *value, void *cb)
+static int git_show_branch_config(const char *var, const char *value,
+				  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "showbranch.default")) {
 		if (!value)
@@ -582,7 +583,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/stash.c b/builtin/stash.c
index a7e17ffe384..e5c4246d2d4 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -837,7 +837,8 @@ static int show_stat = 1;
 static int show_patch;
 static int show_include_untracked;
 
-static int git_stash_config(const char *var, const char *value, void *cb)
+static int git_stash_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "stash.showstat")) {
 		show_stat = git_config_bool(var, value);
@@ -851,7 +852,7 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 		show_include_untracked = git_config_bool(var, value);
 		return 0;
 	}
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value, ctx, cb);
 }
 
 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 6bf8d666ce9..3f8f3692937 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2189,6 +2189,7 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
+				   const struct config_context *ctx UNUSED,
 				   void *cb)
 {
 	int *max_jobs = cb;
diff --git a/builtin/tag.c b/builtin/tag.c
index 1acf5f7a59f..b7dfb5e2cad 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -188,7 +188,8 @@ static const char tag_template_nocleanup[] =
 	"Lines starting with '%c' will be kept; you may remove them"
 	" yourself if you want to.\n");
 
-static int git_tag_config(const char *var, const char *value, void *cb)
+static int git_tag_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "tag.gpgsign")) {
 		config_sign_tag = git_config_bool(var, value);
@@ -213,7 +214,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/builtin/var.c b/builtin/var.c
index 21499989807..ae011bdf409 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -71,13 +71,14 @@ static const struct git_var *get_git_var(const char *var)
 	return NULL;
 }
 
-static int show_config(const char *var, const char *value, void *cb)
+static int show_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb)
 {
 	if (value)
 		printf("%s=%s\n", var, value);
 	else
 		printf("%s\n", var);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index f3180463be2..d6f6d82bbec 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -107,14 +107,15 @@ static int verbose;
 static int guess_remote;
 static timestamp_t expire;
 
-static int git_worktree_config(const char *var, const char *value, void *cb)
+static int git_worktree_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "worktree.guessremote")) {
 		guess_remote = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int delete_git_dir(const char *id)
diff --git a/bundle-uri.c b/bundle-uri.c
index 2a2db1a1d39..0d5acc3dc51 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -224,7 +224,9 @@ static int bundle_list_update(const char *key, const char *value,
 	return 0;
 }
 
-static int config_to_bundle_list(const char *key, const char *value, void *data)
+static int config_to_bundle_list(const char *key, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *data)
 {
 	struct bundle_list *list = data;
 	return bundle_list_update(key, value, list);
@@ -871,7 +873,9 @@ cached:
 	return advertise_bundle_uri;
 }
 
-static int config_to_packet_line(const char *key, const char *value, void *data)
+static int config_to_packet_line(const char *key, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *data)
 {
 	struct packet_reader *writer = data;
 
diff --git a/compat/mingw.c b/compat/mingw.c
index d06cdc6254f..4186bc7a417 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -244,7 +244,8 @@ static int core_restrict_inherited_handles = -1;
 static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
 static char *unset_environment_variables;
 
-int mingw_core_config(const char *var, const char *value, void *cb)
+int mingw_core_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "core.hidedotfiles")) {
 		if (value && !strcasecmp(value, "dotgitonly"))
diff --git a/compat/mingw.h b/compat/mingw.h
index 209cf7cebad..5e34c873473 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -11,7 +11,9 @@ typedef _sigset_t sigset_t;
 #undef _POSIX_THREAD_SAFE_FUNCTIONS
 #endif
 
-int mingw_core_config(const char *var, const char *value, void *cb);
+struct config_context;
+int mingw_core_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb);
 #define platform_core_config mingw_core_config
 
 /*
diff --git a/config.c b/config.c
index b79baf83e35..670d92cd76b 100644
--- a/config.c
+++ b/config.c
@@ -208,7 +208,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_(
@@ -387,7 +388,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;
@@ -421,6 +423,7 @@ static void populate_remote_urls(struct config_include_data *inc)
 }
 
 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;
@@ -484,7 +487,9 @@ 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;
@@ -496,7 +501,7 @@ 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, NULL, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -678,7 +683,7 @@ static int config_parse_pair(const char *key, const char *value,
 	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, NULL, data) < 0) ? -1 : 0;
 	free(canonical_name);
 	return ret;
 }
@@ -966,7 +971,7 @@ 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);
+	ret = fn(name->buf, value, NULL, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1560,7 +1565,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")) {
@@ -1845,7 +1851,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)
@@ -1947,15 +1953,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);
@@ -2310,7 +2317,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
 
-		if (fn(entry->key, values->items[value_index].string, data) < 0)
+		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
 			git_die_config_linenr(entry->key,
 					      reader->config_kvi->filename,
 					      reader->config_kvi->linenr);
@@ -2488,7 +2495,9 @@ struct configset_add_data {
 };
 #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 UNUSED,
+			       void *cb)
 {
 	struct configset_add_data *data = cb;
 	configset_add_value(data->config_reader, data->config_set, key, value);
@@ -3098,7 +3107,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;
 
diff --git a/config.h b/config.h
index 247b572b37b..bc52ceb72f2 100644
--- a/config.h
+++ b/config.h
@@ -111,8 +111,29 @@ struct config_options {
 	} error_action;
 };
 
+/* Config source metadata for a given config key-value pair */
+struct key_value_info {
+	const char *filename;
+	int linenr;
+	enum config_origin_type origin_type;
+	enum config_scope scope;
+};
+#define KVI_INIT { \
+	.filename = NULL, \
+	.linenr = -1, \
+	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
+	.scope = CONFIG_SCOPE_UNKNOWN, \
+}
+
+/* Captures additional information that a config callback can use. */
+struct config_context {
+	/* Config source metadata for key and value. */
+	const struct key_value_info *kvi;
+};
+#define CONFIG_CONTEXT_INIT { 0 }
+
 /**
- * A config callback function takes three parameters:
+ * A config callback function takes four parameters:
  *
  * - the name of the parsed variable. This is in canonical "flat" form: the
  *   section, subsection, and variable segments will be separated by dots,
@@ -123,15 +144,22 @@ struct config_options {
  *   value specified, the value will be NULL (typically this means it
  *   should be interpreted as boolean true).
  *
+ * - the 'config context', that is, additional information about the config
+ *   iteration operation provided by the config machinery. For example, this
+ *   includes information about the config source being parsed (e.g. the
+ *   filename).
+ *
  * - a void pointer passed in by the caller of the config API; this can
  *   contain callback-specific data
  *
  * A config callback should return 0 for success, or -1 if the variable
  * could not be parsed properly.
  */
-typedef int (*config_fn_t)(const char *, const char *, void *);
+typedef int (*config_fn_t)(const char *, const char *,
+			   const struct config_context *, void *);
 
-int git_default_config(const char *, const char *, void *);
+int git_default_config(const char *, const char *,
+		       const struct config_context *, void *);
 
 /**
  * Read a specific file in git-config format.
@@ -667,13 +695,6 @@ int git_config_get_expiry(const char *key, const char **output);
 /* parse either "this many days" integer, or "5.days.ago" approxidate */
 int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
 
-struct key_value_info {
-	const char *filename;
-	int linenr;
-	enum config_origin_type origin_type;
-	enum config_scope scope;
-};
-
 /**
  * First prints the error message specified by the caller in `err` and then
  * dies printing the line number and the file name of the highest priority
diff --git a/connect.c b/connect.c
index 3a0186280c4..cddac1d96b8 100644
--- a/connect.c
+++ b/connect.c
@@ -964,7 +964,7 @@ static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
 static char *git_proxy_command;
 
 static int git_proxy_command_options(const char *var, const char *value,
-		void *cb)
+		const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "core.gitproxy")) {
 		const char *for_pos;
@@ -1010,7 +1010,7 @@ static int git_proxy_command_options(const char *var, const char *value,
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int git_use_proxy(const char *host)
diff --git a/contrib/coccinelle/config_fn_ctx.pending.cocci b/contrib/coccinelle/config_fn_ctx.pending.cocci
new file mode 100644
index 00000000000..6d3d1000a96
--- /dev/null
+++ b/contrib/coccinelle/config_fn_ctx.pending.cocci
@@ -0,0 +1,144 @@
+@ get_fn @
+identifier fn, R;
+@@
+(
+(
+git_config_from_file
+|
+git_config_from_file_with_options
+|
+git_config_from_mem
+|
+git_config_from_blob_oid
+|
+read_early_config
+|
+read_very_early_config
+|
+config_with_options
+|
+git_config
+|
+git_protected_config
+|
+config_from_gitmodules
+)
+  (fn, ...)
+|
+repo_config(R, fn, ...)
+)
+
+@ extends get_fn @
+identifier C1, C2, D;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D);
+
+@ extends get_fn @
+@@
+int fn(const char *, const char *,
++ const struct config_context *,
+  void *);
+
+@ extends get_fn @
+// Don't change fns that look like callback fns but aren't
+identifier fn2 != tar_filter_config && != git_diff_heuristic_config &&
+  != git_default_submodule_config && != git_color_config &&
+  != bundle_list_update && != parse_object_filter_config;
+identifier C1, C2, D1, D2, S;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D1) {
+<+...
+(
+fn2(C1, C2
++ , ctx
+, D2);
+|
+if(fn2(C1, C2
++ , ctx
+, D2) < 0) { ... }
+|
+return fn2(C1, C2
++ , ctx
+, D2);
+|
+S = fn2(C1, C2
++ , ctx
+, D2);
+)
+...+>
+  }
+
+@ extends get_fn@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx UNUSED,
+  void *D) {...}
+
+
+// The previous rules don't catch all callbacks, especially if they're defined
+// in a separate file from the git_config() call. Fix these manually.
+@@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int
+(
+git_ident_config
+|
+urlmatch_collect_fn
+|
+write_one_config
+|
+forbid_remote_url
+|
+credential_config_callback
+)
+  (const char *C1, const char *C2,
++ const struct config_context *ctx UNUSED,
+  void *D) {...}
+
+@@
+identifier C1, C2, D, D2, S, fn2;
+@@
+int
+(
+http_options
+|
+git_status_config
+|
+git_commit_config
+|
+git_default_core_config
+|
+grep_config
+)
+  (const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D) {
+<+...
+(
+fn2(C1, C2
++ , ctx
+, D2);
+|
+if(fn2(C1, C2
++ , ctx
+, D2) < 0) { ... }
+|
+return fn2(C1, C2
++ , ctx
+, D2);
+|
+S = fn2(C1, C2
++ , ctx
+, D2);
+)
+...+>
+  }
diff --git a/convert.c b/convert.c
index 9ee79fe4699..8e96cf83030 100644
--- a/convert.c
+++ b/convert.c
@@ -1015,7 +1015,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
 	return 0;
 }
 
-static int read_convert_config(const char *var, const char *value, void *cb UNUSED)
+static int read_convert_config(const char *var, const char *value,
+			       const struct config_context *ctx UNUSED,
+			       void *cb UNUSED)
 {
 	const char *key, *name;
 	size_t namelen;
diff --git a/credential.c b/credential.c
index 023b59d5711..92b8d9707da 100644
--- a/credential.c
+++ b/credential.c
@@ -48,6 +48,7 @@ static int credential_from_potentially_partial_url(struct credential *c,
 						   const char *url);
 
 static int credential_config_callback(const char *var, const char *value,
+				      const struct config_context *ctx UNUSED,
 				      void *data)
 {
 	struct credential *c = data;
diff --git a/delta-islands.c b/delta-islands.c
index c824a5f6a42..5fc6ea6ff55 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -341,7 +341,9 @@ static void free_remote_islands(kh_str_t *remote_islands)
 	kh_destroy_str(remote_islands);
 }
 
-static int island_config_callback(const char *k, const char *v, void *cb)
+static int island_config_callback(const char *k, const char *v,
+				  const struct config_context *ctx UNUSED,
+				  void *cb)
 {
 	struct island_load_data *ild = cb;
 
diff --git a/diff.c b/diff.c
index 1cdac6ed363..07be3bee137 100644
--- a/diff.c
+++ b/diff.c
@@ -357,7 +357,8 @@ static unsigned parse_color_moved_ws(const char *arg)
 	return ret;
 }
 
-int git_diff_ui_config(const char *var, const char *value, void *cb)
+int git_diff_ui_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 		diff_use_color_default = git_config_colorbool(var, value);
@@ -440,10 +441,11 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value, ctx, cb);
 }
 
-int git_diff_basic_config(const char *var, const char *value, void *cb)
+int git_diff_basic_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	const char *name;
 
@@ -495,7 +497,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
 	if (git_diff_heuristic_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static char *quote_two(const char *one, const char *two)
diff --git a/diff.h b/diff.h
index 3a7a9e8b888..1deea9cb64a 100644
--- a/diff.h
+++ b/diff.h
@@ -531,10 +531,13 @@ void free_diffstat_info(struct diffstat_t *diffstat);
 int parse_long_opt(const char *opt, const char **argv,
 		   const char **optarg);
 
-int git_diff_basic_config(const char *var, const char *value, void *cb);
+struct config_context;
+int git_diff_basic_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb);
 int git_diff_heuristic_config(const char *var, const char *value, void *cb);
 void init_diff_ui_defaults(void);
-int git_diff_ui_config(const char *var, const char *value, void *cb);
+int git_diff_ui_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb);
 void repo_diff_setup(struct repository *, struct diff_options *);
 struct option *add_diff_options(const struct option *, struct diff_options *);
 int diff_opt_parse(struct diff_options *, const char **, int, const char *);
diff --git a/fetch-pack.c b/fetch-pack.c
index 0f71054fbae..5f3d40f3d09 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1860,7 +1860,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	return ref;
 }
 
-static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
+static int fetch_pack_config_cb(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	if (strcmp(var, "fetch.fsck.skiplist") == 0) {
 		const char *path;
@@ -1882,7 +1883,7 @@ static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void fetch_pack_config(void)
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715ba..10137444321 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -20,7 +20,8 @@ static int use_branch_desc;
 static int suppress_dest_pattern_seen;
 static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP;
 
-int fmt_merge_msg_config(const char *key, const char *value, void *cb)
+int fmt_merge_msg_config(const char *key, const char *value,
+			 const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
@@ -40,7 +41,7 @@ int fmt_merge_msg_config(const char *key, const char *value, void *cb)
 			string_list_append(&suppress_dest_patterns, value);
 		suppress_dest_pattern_seen = 1;
 	} else {
-		return git_default_config(key, value, cb);
+		return git_default_config(key, value, ctx, cb);
 	}
 	return 0;
 }
diff --git a/fmt-merge-msg.h b/fmt-merge-msg.h
index 99054042dc5..73ca3e44652 100644
--- a/fmt-merge-msg.h
+++ b/fmt-merge-msg.h
@@ -13,7 +13,8 @@ struct fmt_merge_msg_opts {
 };
 
 extern int merge_log_config;
-int fmt_merge_msg_config(const char *key, const char *value, void *cb);
+int fmt_merge_msg_config(const char *key, const char *value,
+			 const struct config_context *ctx, void *cb);
 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 		  struct fmt_merge_msg_opts *);
 
diff --git a/fsck.c b/fsck.c
index 3261ef9ec28..55b6a694853 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1163,7 +1163,9 @@ struct fsck_gitmodules_data {
 	int ret;
 };
 
-static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
+static int fsck_gitmodules_fn(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
+			      void *vdata)
 {
 	struct fsck_gitmodules_data *data = vdata;
 	const char *subsection, *key;
@@ -1373,7 +1375,8 @@ int fsck_finish(struct fsck_options *options)
 	return ret;
 }
 
-int git_fsck_config(const char *var, const char *value, void *cb)
+int git_fsck_config(const char *var, const char *value,
+		    const struct config_context *ctx, void *cb)
 {
 	struct fsck_options *options = cb;
 	if (strcmp(var, "fsck.skiplist") == 0) {
@@ -1394,7 +1397,7 @@ int git_fsck_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/fsck.h b/fsck.h
index e17730e9da9..6359ba359bd 100644
--- a/fsck.h
+++ b/fsck.h
@@ -233,10 +233,12 @@ void fsck_put_object_name(struct fsck_options *options,
 const char *fsck_describe_object(struct fsck_options *options,
 				 const struct object_id *oid);
 
+struct key_value_info;
 /*
  * git_config() callback for use by fsck-y tools that want to support
  * fsck.<msg> fsck.skipList etc.
  */
-int git_fsck_config(const char *var, const char *value, void *cb);
+int git_fsck_config(const char *var, const char *value,
+		    const struct config_context *ctx, void *cb);
 
 #endif
diff --git a/git-compat-util.h b/git-compat-util.h
index 5b2b99c17c5..14e8aacb957 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -440,8 +440,10 @@ typedef uintmax_t timestamp_t;
 #endif
 
 #ifndef platform_core_config
+struct config_context;
 static inline int noop_core_config(const char *var UNUSED,
 				   const char *value UNUSED,
+				   const struct config_context *ctx UNUSED,
 				   void *cb UNUSED)
 {
 	return 0;
diff --git a/gpg-interface.c b/gpg-interface.c
index 19a3471a0b5..57c862a3a22 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -14,7 +14,8 @@
 #include "alias.h"
 #include "wrapper.h"
 
-static int git_gpg_config(const char *, const char *, void *);
+static int git_gpg_config(const char *, const char *,
+			  const struct config_context *, void *);
 
 static void gpg_interface_lazy_init(void)
 {
@@ -720,7 +721,9 @@ void set_signing_key(const char *key)
 	configured_signing_key = xstrdup(key);
 }
 
-static int git_gpg_config(const char *var, const char *value, void *cb UNUSED)
+static int git_gpg_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED,
+			  void *cb UNUSED)
 {
 	struct gpg_format *fmt = NULL;
 	char *fmtname = NULL;
diff --git a/grep.c b/grep.c
index f00986c451a..fc22c3e2afb 100644
--- a/grep.c
+++ b/grep.c
@@ -56,7 +56,8 @@ define_list_config_array_extra(color_grep_slots, {"match"});
  * Read the configuration file once and store it in
  * the grep_defaults template.
  */
-int grep_config(const char *var, const char *value, void *cb)
+int grep_config(const char *var, const char *value,
+		const struct config_context *ctx, void *cb)
 {
 	struct grep_opt *opt = cb;
 	const char *slot;
@@ -91,9 +92,9 @@ int grep_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "color.grep"))
 		opt->color = git_config_colorbool(var, value);
 	if (!strcmp(var, "color.grep.match")) {
-		if (grep_config("color.grep.matchcontext", value, cb) < 0)
+		if (grep_config("color.grep.matchcontext", value, ctx, cb) < 0)
 			return -1;
-		if (grep_config("color.grep.matchselected", value, cb) < 0)
+		if (grep_config("color.grep.matchselected", value, ctx, cb) < 0)
 			return -1;
 	} else if (skip_prefix(var, "color.grep.", &slot)) {
 		int i = LOOKUP_CONFIG(color_grep_slots, slot);
diff --git a/grep.h b/grep.h
index c59592e3bdb..926c0875c42 100644
--- a/grep.h
+++ b/grep.h
@@ -202,7 +202,9 @@ struct grep_opt {
 	.output = std_output, \
 }
 
-int grep_config(const char *var, const char *value, void *);
+struct config_context;
+int grep_config(const char *var, const char *value,
+		const struct config_context *ctx, void *data);
 void grep_init(struct grep_opt *, struct repository *repo);
 
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
diff --git a/help.c b/help.c
index 5d7637dce92..ac0ae5ac0dc 100644
--- a/help.c
+++ b/help.c
@@ -309,7 +309,8 @@ void load_command_list(const char *prefix,
 	exclude_cmds(other_cmds, main_cmds);
 }
 
-static int get_colopts(const char *var, const char *value, void *data)
+static int get_colopts(const char *var, const char *value,
+		       const struct config_context *ctx UNUSED, void *data)
 {
 	unsigned int *colopts = data;
 
@@ -459,7 +460,8 @@ void list_developer_interfaces_help(void)
 	putchar('\n');
 }
 
-static int get_alias(const char *var, const char *value, void *data)
+static int get_alias(const char *var, const char *value,
+		     const struct config_context *ctx UNUSED, void *data)
 {
 	struct string_list *list = data;
 
@@ -543,6 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
+				  const struct config_context *ctx UNUSED,
 				  void *cb UNUSED)
 {
 	const char *p;
diff --git a/http.c b/http.c
index bb58bb3e6a3..762502828c9 100644
--- a/http.c
+++ b/http.c
@@ -363,7 +363,8 @@ static void process_curl_messages(void)
 	}
 }
 
-static int http_options(const char *var, const char *value, void *cb)
+static int http_options(const char *var, const char *value,
+			const struct config_context *ctx, void *data)
 {
 	if (!strcmp("http.version", var)) {
 		return git_config_string(&curl_http_version, var, value);
@@ -534,7 +535,7 @@ static int http_options(const char *var, const char *value, void *cb)
 	}
 
 	/* Fall back on the default ones */
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, data);
 }
 
 static int curl_empty_auth_enabled(void)
diff --git a/ident.c b/ident.c
index 8fad92d7007..08be4d0747d 100644
--- a/ident.c
+++ b/ident.c
@@ -671,7 +671,9 @@ static int set_ident(const char *var, const char *value)
 	return 0;
 }
 
-int git_ident_config(const char *var, const char *value, void *data UNUSED)
+int git_ident_config(const char *var, const char *value,
+		     const struct config_context *ctx UNUSED,
+		     void *data UNUSED)
 {
 	if (!strcmp(var, "user.useconfigonly")) {
 		ident_use_config_only = git_config_bool(var, value);
diff --git a/ident.h b/ident.h
index 96a64896a01..6a79febba15 100644
--- a/ident.h
+++ b/ident.h
@@ -62,6 +62,8 @@ const char *fmt_name(enum want_ident);
 int committer_ident_sufficiently_given(void);
 int author_ident_sufficiently_given(void);
 
-int git_ident_config(const char *, const char *, void *);
+struct config_context;
+int git_ident_config(const char *, const char *, const struct config_context *,
+		     void *);
 
 #endif
diff --git a/imap-send.c b/imap-send.c
index 7f5426177a1..47777e76861 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1323,7 +1323,8 @@ static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
 	return 1;
 }
 
-static int git_imap_config(const char *var, const char *val, void *cb)
+static int git_imap_config(const char *var, const char *val,
+			   const struct config_context *ctx, void *cb)
 {
 
 	if (!strcmp("imap.sslverify", var))
@@ -1357,7 +1358,7 @@ static int git_imap_config(const char *var, const char *val, void *cb)
 			server.host = xstrdup(val);
 		}
 	} else
-		return git_default_config(var, val, cb);
+		return git_default_config(var, val, ctx, cb);
 
 	return 0;
 }
diff --git a/ll-merge.c b/ll-merge.c
index 07ec16e8e5b..3936d112e00 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -254,6 +254,7 @@ static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
 static const char *default_ll_merge;
 
 static int read_merge_config(const char *var, const char *value,
+			     const struct config_context *ctx UNUSED,
 			     void *cb UNUSED)
 {
 	struct ll_merge_driver *fn;
diff --git a/ls-refs.c b/ls-refs.c
index f385938b64c..a29c2364a59 100644
--- a/ls-refs.c
+++ b/ls-refs.c
@@ -137,6 +137,7 @@ static void send_possibly_unborn_head(struct ls_refs_data *data)
 }
 
 static int ls_refs_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED,
 			  void *cb_data)
 {
 	struct ls_refs_data *data = cb_data;
diff --git a/mailinfo.c b/mailinfo.c
index 2aeb20e5e62..931505363cd 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -1241,12 +1241,13 @@ int mailinfo_parse_quoted_cr_action(const char *actionstr, int *action)
 	return 0;
 }
 
-static int git_mailinfo_config(const char *var, const char *value, void *mi_)
+static int git_mailinfo_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *mi_)
 {
 	struct mailinfo *mi = mi_;
 
 	if (!starts_with(var, "mailinfo."))
-		return git_default_config(var, value, NULL);
+		return git_default_config(var, value, ctx, NULL);
 	if (!strcmp(var, "mailinfo.scissors")) {
 		mi->use_scissors = git_config_bool(var, value);
 		return 0;
diff --git a/notes-utils.c b/notes-utils.c
index 4a793eb347f..97c031c26ec 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -94,7 +94,9 @@ static combine_notes_fn parse_combine_notes_fn(const char *v)
 		return NULL;
 }
 
-static int notes_rewrite_config(const char *k, const char *v, void *cb)
+static int notes_rewrite_config(const char *k, const char *v,
+				const struct config_context *ctx UNUSED,
+				void *cb)
 {
 	struct notes_rewrite_cfg *c = cb;
 	if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
diff --git a/notes.c b/notes.c
index f51a2d3630e..e68645a4b89 100644
--- a/notes.c
+++ b/notes.c
@@ -974,7 +974,9 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
 	free(globs_copy);
 }
 
-static int notes_display_config(const char *k, const char *v, void *cb)
+static int notes_display_config(const char *k, const char *v,
+				const struct config_context *ctx UNUSED,
+				void *cb)
 {
 	int *load_refs = cb;
 
diff --git a/pager.c b/pager.c
index 63055d0873f..b8822a9381e 100644
--- a/pager.c
+++ b/pager.c
@@ -43,6 +43,7 @@ static void wait_for_pager_signal(int signo)
 }
 
 static int core_pager_config(const char *var, const char *value,
+			     const struct config_context *ctx UNUSED,
 			     void *data UNUSED)
 {
 	if (!strcmp(var, "core.pager"))
@@ -228,7 +229,9 @@ struct pager_command_config_data {
 	char *value;
 };
 
-static int pager_command_config(const char *var, const char *value, void *vdata)
+static int pager_command_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *vdata)
 {
 	struct pager_command_config_data *data = vdata;
 	const char *cmd;
diff --git a/pretty.c b/pretty.c
index 0bb938021ba..87245353452 100644
--- a/pretty.c
+++ b/pretty.c
@@ -56,6 +56,7 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
 }
 
 static int git_pretty_formats_config(const char *var, const char *value,
+				     const struct config_context *ctx UNUSED,
 				     void *cb UNUSED)
 {
 	struct cmt_fmt_map *commit_format = NULL;
diff --git a/promisor-remote.c b/promisor-remote.c
index 1adcd6fb0a5..c22abb85b15 100644
--- a/promisor-remote.c
+++ b/promisor-remote.c
@@ -100,7 +100,9 @@ static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
 	config->promisors_tail = &r->next;
 }
 
-static int promisor_remote_config(const char *var, const char *value, void *data)
+static int promisor_remote_config(const char *var, const char *value,
+				  const struct config_context *ctx UNUSED,
+				  void *data)
 {
 	struct promisor_remote_config *config = data;
 	const char *name;
diff --git a/remote.c b/remote.c
index 0764fca0db9..dafb1acdc38 100644
--- a/remote.c
+++ b/remote.c
@@ -349,7 +349,8 @@ static void read_branches_file(struct remote_state *remote_state,
 	remote->fetch_tags = 1; /* always auto-follow */
 }
 
-static int handle_config(const char *key, const char *value, void *cb)
+static int handle_config(const char *key, const char *value,
+			 const struct config_context *ctx UNUSED, void *cb)
 {
 	const char *name;
 	size_t namelen;
diff --git a/revision.c b/revision.c
index b33cc1d106a..87ed8ccd444 100644
--- a/revision.c
+++ b/revision.c
@@ -1572,7 +1572,9 @@ struct exclude_hidden_refs_cb {
 	const char *section;
 };
 
-static int hide_refs_config(const char *var, const char *value, void *cb_data)
+static int hide_refs_config(const char *var, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *cb_data)
 {
 	struct exclude_hidden_refs_cb *cb = cb_data;
 	cb->exclusions->hidden_refs_configured = 1;
diff --git a/scalar.c b/scalar.c
index 1326e1f6089..df7358f481c 100644
--- a/scalar.c
+++ b/scalar.c
@@ -594,7 +594,9 @@ static int cmd_register(int argc, const char **argv)
 	return register_dir();
 }
 
-static int get_scalar_repos(const char *key, const char *value, void *data)
+static int get_scalar_repos(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *data)
 {
 	struct string_list *list = data;
 
diff --git a/sequencer.c b/sequencer.c
index bceb6abcb6c..34754d17596 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -219,7 +219,8 @@ static struct update_ref_record *init_update_ref_record(const char *ref)
 	return rec;
 }
 
-static int git_sequencer_config(const char *k, const char *v, void *cb)
+static int git_sequencer_config(const char *k, const char *v,
+				const struct config_context *ctx, void *cb)
 {
 	struct replay_opts *opts = cb;
 	int status;
@@ -274,7 +275,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
 	if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
 		opts->commit_use_reference = git_config_bool(k, v);
 
-	return git_diff_basic_config(k, v, NULL);
+	return git_diff_basic_config(k, v, ctx, NULL);
 }
 
 void sequencer_init_config(struct replay_opts *opts)
@@ -2881,7 +2882,9 @@ static int git_config_string_dup(char **dest,
 	return 0;
 }
 
-static int populate_opts_cb(const char *key, const char *value, void *data)
+static int populate_opts_cb(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *data)
 {
 	struct replay_opts *opts = data;
 	int error_flag = 1;
diff --git a/setup.c b/setup.c
index 458582207ea..e7f81151ef0 100644
--- a/setup.c
+++ b/setup.c
@@ -517,7 +517,9 @@ no_prevention_needed:
 	startup_info->original_cwd = NULL;
 }
 
-static int read_worktree_config(const char *var, const char *value, void *vdata)
+static int read_worktree_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *vdata)
 {
 	struct repository_format *data = vdata;
 
@@ -588,7 +590,8 @@ static enum extension_result handle_extension(const char *var,
 	return EXTENSION_UNKNOWN;
 }
 
-static int check_repo_format(const char *var, const char *value, void *vdata)
+static int check_repo_format(const char *var, const char *value,
+			     const struct config_context *ctx, void *vdata)
 {
 	struct repository_format *data = vdata;
 	const char *ext;
@@ -617,7 +620,7 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
 		}
 	}
 
-	return read_worktree_config(var, value, vdata);
+	return read_worktree_config(var, value, ctx, vdata);
 }
 
 static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
@@ -1116,7 +1119,8 @@ struct safe_directory_data {
 	int is_safe;
 };
 
-static int safe_directory_cb(const char *key, const char *value, void *d)
+static int safe_directory_cb(const char *key, const char *value,
+			     const struct config_context *ctx UNUSED, void *d)
 {
 	struct safe_directory_data *data = d;
 
@@ -1172,7 +1176,9 @@ static int ensure_valid_ownership(const char *gitfile,
 	return data.is_safe;
 }
 
-static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
+static int allowed_bare_repo_cb(const char *key, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *d)
 {
 	enum allowed_bare_repo *allowed_bare_repo = d;
 
diff --git a/submodule-config.c b/submodule-config.c
index 58dfbde9ae5..b364244102e 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -426,7 +426,8 @@ struct parse_config_parameter {
  * config store (.git/config, etc).  Callers are responsible for
  * checking for overrides in the main config store when appropriate.
  */
-static int parse_config(const char *var, const char *value, void *data)
+static int parse_config(const char *var, const char *value,
+			const struct config_context *ctx UNUSED, void *data)
 {
 	struct parse_config_parameter *me = data;
 	struct submodule *submodule;
@@ -675,7 +676,8 @@ out:
 	}
 }
 
-static int gitmodules_cb(const char *var, const char *value, void *data)
+static int gitmodules_cb(const char *var, const char *value,
+			 const struct config_context *ctx, void *data)
 {
 	struct repository *repo = data;
 	struct parse_config_parameter parameter;
@@ -685,7 +687,7 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
 	parameter.gitmodules_oid = null_oid();
 	parameter.overwrite = 1;
 
-	return parse_config(var, value, &parameter);
+	return parse_config(var, value, ctx, &parameter);
 }
 
 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
@@ -802,7 +804,9 @@ void submodule_free(struct repository *r)
 		submodule_cache_clear(r->submodule_cache);
 }
 
-static int config_print_callback(const char *var, const char *value, void *cb_data)
+static int config_print_callback(const char *var, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *cb_data)
 {
 	char *wanted_key = cb_data;
 
@@ -844,7 +848,9 @@ struct fetch_config {
 	int *recurse_submodules;
 };
 
-static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
+static int gitmodules_fetch_config(const char *var, const char *value,
+				   const struct config_context *ctx UNUSED,
+				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
@@ -872,6 +878,7 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
+					  const struct config_context *ctx UNUSED,
 					  void *cb)
 {
 	int *max_jobs = cb;
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index ad78fc17683..85ad815358e 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -42,7 +42,9 @@
  *
  */
 
-static int iterate_cb(const char *var, const char *value, void *data UNUSED)
+static int iterate_cb(const char *var, const char *value,
+		      const struct config_context *ctx UNUSED,
+		      void *data UNUSED)
 {
 	static int nr;
 
@@ -59,7 +61,8 @@ static int iterate_cb(const char *var, const char *value, void *data UNUSED)
 	return 0;
 }
 
-static int parse_int_cb(const char *var, const char *value, void *data)
+static int parse_int_cb(const char *var, const char *value,
+			const struct config_context *ctx UNUSED, void *data)
 {
 	const char *key_to_match = data;
 
@@ -70,7 +73,9 @@ static int parse_int_cb(const char *var, const char *value, void *data)
 	return 0;
 }
 
-static int early_config_cb(const char *var, const char *value, void *vdata)
+static int early_config_cb(const char *var, const char *value,
+			   const struct config_context *ctx UNUSED,
+			   void *vdata)
 {
 	const char *key = vdata;
 
diff --git a/t/helper/test-userdiff.c b/t/helper/test-userdiff.c
index 680124a6760..0ce31ce59f5 100644
--- a/t/helper/test-userdiff.c
+++ b/t/helper/test-userdiff.c
@@ -12,7 +12,9 @@ static int driver_cb(struct userdiff_driver *driver,
 	return 0;
 }
 
-static int cmd__userdiff_config(const char *var, const char *value, void *cb UNUSED)
+static int cmd__userdiff_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *cb UNUSED)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 78cfc15d52d..83bc4fd109c 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -99,7 +99,8 @@ struct tr2_cfg_data {
 /*
  * See if the given config key matches any of our patterns of interest.
  */
-static int tr2_cfg_cb(const char *key, const char *value, void *d)
+static int tr2_cfg_cb(const char *key, const char *value,
+		      const struct config_context *ctx UNUSED, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
@@ -142,8 +143,12 @@ void tr2_list_env_vars_fl(const char *file, int line)
 void tr2_cfg_set_fl(const char *file, int line, const char *key,
 		    const char *value)
 {
+	struct key_value_info kvi = KVI_INIT;
+	struct config_context ctx = {
+		.kvi = &kvi,
+	};
 	struct tr2_cfg_data data = { file, line };
 
 	if (tr2_cfg_load_patterns() > 0)
-		tr2_cfg_cb(key, value, &data);
+		tr2_cfg_cb(key, value, &ctx, &data);
 }
diff --git a/trace2/tr2_sysenv.c b/trace2/tr2_sysenv.c
index 069786cb927..f26ec95ab4d 100644
--- a/trace2/tr2_sysenv.c
+++ b/trace2/tr2_sysenv.c
@@ -57,7 +57,8 @@ static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
 };
 /* clang-format on */
 
-static int tr2_sysenv_cb(const char *key, const char *value, void *d)
+static int tr2_sysenv_cb(const char *key, const char *value,
+			 const struct config_context *ctx UNUSED, void *d)
 {
 	int k;
 
diff --git a/trailer.c b/trailer.c
index a2c3ed6f28c..06dc0b7f683 100644
--- a/trailer.c
+++ b/trailer.c
@@ -482,6 +482,7 @@ static struct {
 };
 
 static int git_trailer_default_config(const char *conf_key, const char *value,
+				      const struct config_context *ctx UNUSED,
 				      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
@@ -514,6 +515,7 @@ static int git_trailer_default_config(const char *conf_key, const char *value,
 }
 
 static int git_trailer_config(const char *conf_key, const char *value,
+			      const struct config_context *ctx UNUSED,
 			      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
diff --git a/upload-pack.c b/upload-pack.c
index d3312006a32..951fd1f9c25 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1309,7 +1309,9 @@ static int parse_object_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int upload_pack_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_config(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
+			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
@@ -1350,7 +1352,9 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
 }
 
-static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_protected_config(const char *var, const char *value,
+					const struct config_context *ctx UNUSED,
+					void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
diff --git a/urlmatch.c b/urlmatch.c
index eba0bdd77fe..1c45f23adf2 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -551,7 +551,8 @@ static int cmp_matches(const struct urlmatch_item *a,
 	return 0;
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb)
+int urlmatch_config_entry(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	struct string_list_item *item;
 	struct urlmatch_config *collect = cb;
@@ -565,7 +566,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)
 
 	if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
 		if (collect->cascade_fn)
-			return collect->cascade_fn(var, value, cb);
+			return collect->cascade_fn(var, value, ctx, cb);
 		return 0; /* not interested */
 	}
 	dot = strrchr(key, '.');
@@ -609,7 +610,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)
 	strbuf_addstr(&synthkey, collect->section);
 	strbuf_addch(&synthkey, '.');
 	strbuf_addstr(&synthkey, key);
-	retval = collect->collect_fn(synthkey.buf, value, collect->cb);
+	retval = collect->collect_fn(synthkey.buf, value, ctx, collect->cb);
 
 	strbuf_release(&synthkey);
 	return retval;
diff --git a/urlmatch.h b/urlmatch.h
index bee374a642c..5ba85cea139 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -71,7 +71,8 @@ struct urlmatch_config {
 	.vars = STRING_LIST_INIT_DUP, \
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb);
+int urlmatch_config_entry(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb);
 void urlmatch_config_release(struct urlmatch_config *config);
 
 #endif /* URL_MATCH_H */
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 0460e03f5ed..dcbb5e09857 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -307,7 +307,8 @@ int xdiff_compare_lines(const char *l1, long s1,
 
 int git_xmerge_style = -1;
 
-int git_xmerge_config(const char *var, const char *value, void *cb)
+int git_xmerge_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "merge.conflictstyle")) {
 		if (!value)
@@ -327,5 +328,5 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
 			    value, var);
 		return 0;
 	}
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
diff --git a/xdiff-interface.h b/xdiff-interface.h
index 733c364d26c..e6f80df0462 100644
--- a/xdiff-interface.h
+++ b/xdiff-interface.h
@@ -50,7 +50,9 @@ int buffer_is_binary(const char *ptr, unsigned long size);
 
 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
 void xdiff_clear_find_func(xdemitconf_t *xecfg);
-int git_xmerge_config(const char *var, const char *value, void *cb);
+struct config_context;
+int git_xmerge_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb);
 extern int git_xmerge_style;
 
 /*
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 04/12] config.c: pass ctx in configsets
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (2 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 03/12] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 21:19       ` Junio C Hamano
  2023-06-20 19:43     ` [PATCH v3 05/12] config: pass ctx with config files Glen Choo via GitGitGadget
                       ` (9 subsequent siblings)
  13 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context to config callbacks in configset_iter(), trivially
setting the .kvi member to the cached key_value_info. Then, in config
callbacks that are only used with configsets, use the .kvi member to
replace calls to current_config_*(), and delete current_config_line()
because it has no remaining callers.

This leaves builtin/config.c and config.c as the only remaining users of
current_config_*().

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/remote.c       | 10 ++++++----
 config.c               | 35 ++++++++++++++++-------------------
 config.h               |  2 +-
 remote.c               |  7 ++++---
 t/helper/test-config.c | 11 ++++++-----
 5 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index 87de81105e2..d47f9ee21cf 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -646,17 +646,19 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	const struct config_context *ctx UNUSED, void *cb)
+	const struct config_context *ctx, void *cb)
 {
+	const struct key_value_info *kvi = ctx->kvi;
+
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
 	    !value || strcmp(value, info->old_name))
 		return 0;
 
-	info->scope = current_config_scope();
+	info->scope = kvi->scope;
 	strbuf_reset(&info->origin);
-	strbuf_addstr(&info->origin, current_config_name());
-	info->linenr = current_config_line();
+	strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
+	info->linenr = kvi->linenr;
 
 	return 0;
 }
diff --git a/config.c b/config.c
index 670d92cd76b..1e146c861b1 100644
--- a/config.c
+++ b/config.c
@@ -2309,6 +2309,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 	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;
@@ -2316,12 +2317,11 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 		values = &entry->value_list;
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
-
-		if (fn(entry->key, values->items[value_index].string, NULL, 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);
-
+					      ctx.kvi->filename,
+					      ctx.kvi->linenr);
 		config_reader_set_kvi(reader, NULL);
 	}
 }
@@ -3974,13 +3974,8 @@ static int reader_origin_type(struct config_reader *reader,
 	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";
@@ -3997,6 +3992,16 @@ const char *current_config_origin_type(void)
 	}
 }
 
+const char *current_config_origin_type(void)
+{
+	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
+
+	if (reader_origin_type(&the_reader, &type))
+		BUG("current_config_origin_type called outside config callback");
+
+	return config_origin_type_name(type);
+}
+
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4044,14 +4049,6 @@ enum config_scope current_config_scope(void)
 		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;
diff --git a/config.h b/config.h
index bc52ceb72f2..b4236e120a8 100644
--- a/config.h
+++ b/config.h
@@ -387,7 +387,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 enum config_scope current_config_scope(void);
 const char *current_config_origin_type(void);
 const char *current_config_name(void);
-int current_config_line(void);
+const char *config_origin_type_name(enum config_origin_type type);
 
 /*
  * Match and parse a config key of the form:
diff --git a/remote.c b/remote.c
index dafb1acdc38..37d413e7892 100644
--- a/remote.c
+++ b/remote.c
@@ -350,7 +350,7 @@ static void read_branches_file(struct remote_state *remote_state,
 }
 
 static int handle_config(const char *key, const char *value,
-			 const struct config_context *ctx UNUSED, void *cb)
+			 const struct config_context *ctx, void *cb)
 {
 	const char *name;
 	size_t namelen;
@@ -358,6 +358,7 @@ static int handle_config(const char *key, const char *value,
 	struct remote *remote;
 	struct branch *branch;
 	struct remote_state *remote_state = cb;
+	const struct key_value_info *kvi = ctx->kvi;
 
 	if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
 		/* There is no subsection. */
@@ -415,8 +416,8 @@ static int handle_config(const char *key, const char *value,
 	}
 	remote = make_remote(remote_state, name, namelen);
 	remote->origin = REMOTE_CONFIG;
-	if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
-	    current_config_scope() == CONFIG_SCOPE_WORKTREE)
+	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
+	    kvi->scope == CONFIG_SCOPE_WORKTREE)
 		remote->configured_in_repo = 1;
 	if (!strcmp(subkey, "mirror"))
 		remote->mirror = git_config_bool(key, value);
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 85ad815358e..3f4c3678318 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -43,9 +43,10 @@
  */
 
 static int iterate_cb(const char *var, const char *value,
-		      const struct config_context *ctx UNUSED,
+		      const struct config_context *ctx,
 		      void *data UNUSED)
 {
+	const struct key_value_info *kvi = ctx->kvi;
 	static int nr;
 
 	if (nr++)
@@ -53,10 +54,10 @@ static int iterate_cb(const char *var, const char *value,
 
 	printf("key=%s\n", var);
 	printf("value=%s\n", value ? value : "(null)");
-	printf("origin=%s\n", current_config_origin_type());
-	printf("name=%s\n", current_config_name());
-	printf("lno=%d\n", current_config_line());
-	printf("scope=%s\n", config_scope_name(current_config_scope()));
+	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
+	printf("name=%s\n", kvi->filename ? kvi->filename : "");
+	printf("lno=%d\n", kvi->linenr);
+	printf("scope=%s\n", config_scope_name(kvi->scope));
 
 	return 0;
 }
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 05/12] config: pass ctx with config files
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (3 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 04/12] config.c: pass ctx in configsets Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
                       ` (8 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context to config_callbacks when parsing config files. To
provide the .kvi member, refactor out the configset logic that caches
"struct config_source" and "enum config_scope" as a "struct
key_value_info". Make the "enum config_scope" available to the config
file machinery by plumbing an additional arg through
git_config_from_file_with_options().

We do not exercise ctx yet because the remaining current_config_*()
callers may be used with config_with_options(), which may read config
from parameters, but parameters don't pass ctx yet.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 bundle-uri.c       |   1 +
 config.c           | 105 ++++++++++++++++++++++++++++++---------------
 config.h           |   8 ++--
 fsck.c             |   3 +-
 submodule-config.c |   5 ++-
 5 files changed, 81 insertions(+), 41 deletions(-)

diff --git a/bundle-uri.c b/bundle-uri.c
index 0d5acc3dc51..64f32387745 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -255,6 +255,7 @@ int bundle_uri_parse_config_format(const char *uri,
 	}
 	result = git_config_from_file_with_options(config_to_bundle_list,
 						   filename, list,
+						   CONFIG_SCOPE_UNKNOWN,
 						   &opts);
 
 	if (!result && list->mode == BUNDLE_MODE_NONE) {
diff --git a/config.c b/config.c
index 1e146c861b1..79a6e7cce7e 100644
--- a/config.c
+++ b/config.c
@@ -258,7 +258,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    !cs ? "<unknown>" :
 			    cs->name ? cs->name :
 			    "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,
+							current_config_scope(),
+							NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -501,7 +503,7 @@ static int git_config_include(const char *var, const char *value,
 	 * Pass along all values, including "include" directives; this makes it
 	 * possible to query information on the includes themselves.
 	 */
-	ret = inc->fn(var, value, NULL, inc->data);
+	ret = inc->fn(var, value, ctx, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -937,12 +939,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 (;;) {
@@ -971,7 +976,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, NULL, data);
+	kvi->linenr = cs->linenr;
+	ret = fn(name->buf, value, &ctx, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1070,8 +1076,19 @@ 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;
+}
+
 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;
@@ -1155,7 +1172,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;
 	}
 
@@ -2013,9 +2030,11 @@ int git_default_config(const char *var, const char *value,
  * this function.
  */
 static int do_config_from(struct config_reader *reader,
-			  struct config_source *top, config_fn_t fn, void *data,
+			  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 */
@@ -2025,8 +2044,9 @@ static int do_config_from(struct config_reader *reader,
 	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);
@@ -2040,7 +2060,8 @@ static int do_config_from_file(struct config_reader *reader,
 			       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;
@@ -2055,19 +2076,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(reader, &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);
+				   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;
@@ -2078,7 +2100,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
 	f = fopen_or_warn(filename, "r");
 	if (f) {
 		ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
-					  filename, filename, f, data, opts);
+					  filename, filename, f, data, scope,
+					  opts);
 		fclose(f);
 	}
 	return ret;
@@ -2086,13 +2109,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;
 
@@ -2107,14 +2132,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(&the_reader, &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;
@@ -2130,7 +2156,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;
@@ -2139,13 +2165,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)
@@ -2220,27 +2247,34 @@ static int do_git_config_sequence(struct config_reader *reader,
 	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 && repository_format_worktree_config) {
 		char *path = git_pathdup("config.worktree");
 		if (!access_or_die(path, R_OK, 0))
-			ret += git_config_from_file(fn, path, data);
+			ret += git_config_from_file_with_options(fn, path, data,
+								 CONFIG_SCOPE_WORKTREE,
+								 NULL);
 		free(path);
 	}
 
@@ -2282,14 +2316,16 @@ int config_with_options(config_fn_t fn, void *data,
 	 * 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) {
 		struct repository *repo = config_source->repo ?
 			config_source->repo : the_repository;
 		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, fn, data);
 	}
@@ -2432,16 +2468,14 @@ static int configset_add_value(struct config_reader *reader,
 	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;
+		kvi_from_source(reader->source, current_config_scope(), kv_info);
 	} 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->scope = reader->parsing_scope;
 	si->util = kv_info;
 
 	return 0;
@@ -3482,7 +3516,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;
diff --git a/config.h b/config.h
index b4236e120a8..1fc746c3a46 100644
--- a/config.h
+++ b/config.h
@@ -170,16 +170,18 @@ int git_default_config(const char *, const char *,
 int git_config_from_file(config_fn_t fn, const char *, void *);
 
 int git_config_from_file_with_options(config_fn_t fn, const char *,
-				      void *,
+				      void *, enum config_scope,
 				      const struct config_options *);
 int git_config_from_mem(config_fn_t fn,
 			const enum config_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);
 int git_config_from_blob_oid(config_fn_t fn, const char *name,
 			     struct repository *repo,
-			     const struct object_id *oid, void *data);
+			     const struct object_id *oid, void *data,
+			     enum config_scope scope);
 void git_config_push_parameter(const char *text);
 void git_config_push_env(const char *spec);
 int git_config_from_parameters(config_fn_t fn, void *data);
diff --git a/fsck.c b/fsck.c
index 55b6a694853..f92c216fb5c 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1238,7 +1238,8 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
 		data.ret = 0;
 		config_opts.error_action = CONFIG_ERROR_SILENT;
 		if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
-					".gitmodules", buf, size, &data, &config_opts))
+					".gitmodules", buf, size, &data,
+					CONFIG_SCOPE_UNKNOWN, &config_opts))
 			data.ret |= report(options, oid, OBJ_BLOB,
 					FSCK_MSG_GITMODULES_PARSE,
 					"could not parse gitmodules blob");
diff --git a/submodule-config.c b/submodule-config.c
index b364244102e..e19ab593aa4 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -606,7 +606,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 	parameter.gitmodules_oid = &oid;
 	parameter.overwrite = 0;
 	git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
-			config, config_size, &parameter, NULL);
+			    config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
 	strbuf_release(&rev);
 	free(config);
 
@@ -715,7 +715,8 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
 
 	if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
 		git_config_from_blob_oid(gitmodules_cb, rev.buf,
-					 the_repository, &oid, the_repository);
+					 the_repository, &oid, the_repository,
+					 CONFIG_SCOPE_UNKNOWN);
 	}
 	strbuf_release(&rev);
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 06/12] builtin/config.c: test misuse of format_config()
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (4 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 05/12] config: pass ctx with config files Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 21:35       ` Junio C Hamano
  2023-06-23 20:32       ` Jonathan Tan
  2023-06-20 19:43     ` [PATCH v3 07/12] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
                       ` (7 subsequent siblings)
  13 siblings, 2 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

current_config_*() functions aren't meant to be called outside of
config callbacks because they read state that is only set when iterating
through config. However, several sites in builtin/config.c are
indirectly calling these functions outside of config callbacks thanks to
the format_config() helper. Show the current, bad behavior via tests
so that the fixes in a subsequent commit will be clearer.

The misbehaving cases are:

* "git config --get-urlmatch --show-scope" results in an "unknown"
   scope, where it arguably should show the config file's scope. It's
   clear that this wasn't intended, though: we knew that
   "--get-urlmatch" couldn't show config source metadata, which is why
   "--show-origin" was marked incompatible with "--get-urlmatch" when
   it was introduced [1]. It was most likely a mistake that we allowed
   "--show-scope" to sneak through.

* Similarly, "git config --default" doesn't set config source metadata ,
  so "--show-scope" also results in "unknown", and "--show-origin"
  results in a BUG().

[1] https://lore.kernel.org/git/20160205112001.GA13397@sigill.intra.peff.net/

Signed-off-by: Glen Choo <chooglen@google.com>
---
 t/t1300-config.sh | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 86bfbc2b364..fa6a8df2521 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1668,6 +1668,21 @@ test_expect_success 'urlmatch' '
 	test_cmp expect actual
 '
 
+test_expect_success 'urlmatch with --show-scope' '
+	cat >.git/config <<-\EOF &&
+	[http "https://weak.example.com"]
+		sslVerify = false
+		cookieFile = /tmp/cookie.txt
+	EOF
+
+	cat >expect <<-EOF &&
+	unknown	http.cookiefile /tmp/cookie.txt
+	unknown	http.sslverify false
+	EOF
+	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'urlmatch favors more specific URLs' '
 	cat >.git/config <<-\EOF &&
 	[http "https://example.com/"]
@@ -2055,6 +2070,10 @@ test_expect_success '--show-origin blob ref' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-origin with --default' '
+	test_must_fail git config --show-origin --default foo some.key
+'
+
 test_expect_success '--show-scope with --list' '
 	cat >expect <<-EOF &&
 	global	user.global=true
@@ -2123,6 +2142,12 @@ test_expect_success '--show-scope with --show-origin' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-scope with --default' '
+	git config --show-scope --default foo some.key >actual &&
+	echo "unknown	foo" >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'override global and system config' '
 	test_when_finished rm -f \"\$HOME\"/.gitconfig &&
 	cat >"$HOME"/.gitconfig <<-EOF &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 07/12] config.c: pass ctx with CLI config
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (5 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-23 20:35       ` Jonathan Tan
  2023-06-20 19:43     ` [PATCH v3 08/12] trace2: plumb config kvi Glen Choo via GitGitGadget
                       ` (6 subsequent siblings)
  13 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context when parsing CLI config. To provide the .kvi member,
refactor out kvi_from_param() from the logic that caches CLI config in
configsets. Now that config_context and config_context.kvi is always
present when config machinery calls config callbacks, plumb "kvi" so
that we can remove all calls of current_config_scope() except for
trace2/*.c (which will be handled in a later commit), and remove all
other current_config_*() (the functions themselves and their calls).
Note that this results in .kvi containing a different, more complete
set of information than the mocked up "struct config_source" in
git_config_from_parameters().

Plumbing "kvi" reveals a few places where we've been doing the wrong
thing:

* git_config_parse_parameter() hasn't been setting config source
  information, so plumb "kvi" there too.

* "git config --get-urlmatch --show-scope" iterates config to collect
  values, but then attempts to display the scope after config iteration.
  Fix this by copying the "kvi" value in the collection phase so that it
  can be read back later. This means that we can now support "git config
  --get-urlmatch --show-origin" (we don't allow this combination of args
  because of this bug), but that is left unchanged for now.

* "git config --default" doesn't have config source metadata when
  displaying the default value. Fix this by treating the default value
  as if it came from the command line (e.g. like we do with "git -c" or
  "git config --file"), using kvi_from_param().

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/config.c  | 47 +++++++++++++++++----------
 config.c          | 82 +++++++++++++++++++++++------------------------
 config.h          |  3 +-
 t/t1300-config.sh | 10 +++---
 4 files changed, 78 insertions(+), 64 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index e14f967bbea..0c01ded5f05 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -193,38 +193,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 UNUSED,
+			   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);
@@ -242,12 +246,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) {
@@ -302,9 +307,10 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
 }
 
 static int collect_config(const char *key_, const char *value_,
-			  const struct config_context *ctx UNUSED, void *cb)
+			  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 +325,7 @@ static int collect_config(const char *key_, const char *value_,
 	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)
@@ -380,11 +386,14 @@ static int get_value(const char *key_, const char *regex_, unsigned flags)
 			    &given_config_source, &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);
 	}
@@ -559,15 +568,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,
-			       const struct config_context *ctx UNUSED,
+			       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));
@@ -576,6 +587,7 @@ static int urlmatch_collect_fn(const char *var, const char *value,
 	} else {
 		strbuf_reset(&matched->value);
 	}
+	matched->kvi = *kvi;
 
 	if (value) {
 		strbuf_addstr(&matched->value, value);
@@ -622,7 +634,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);
 
diff --git a/config.c b/config.c
index 79a6e7cce7e..cfc5c7f10b3 100644
--- a/config.c
+++ b/config.c
@@ -218,7 +218,9 @@ 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(struct config_source *cs,
+			       const struct key_value_info *kvi,
+			       const char *path,
 			       struct config_include_data *inc)
 {
 	int ret = 0;
@@ -259,8 +261,7 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    cs->name ? cs->name :
 			    "the command line");
 		ret = git_config_from_file_with_options(git_config_include, path, inc,
-							current_config_scope(),
-							NULL);
+							kvi->scope, NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -508,7 +509,7 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, ctx->kvi, value, inc);
 
 	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
 	    cond && include_condition_is_true(cs, inc, cond, cond_len) &&
@@ -517,7 +518,7 @@ static int git_config_include(const char *var, const char *value,
 
 		if (inc->opts->unconditional_remote_url)
 			inc->fn = forbid_remote_url;
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, ctx->kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -675,27 +676,44 @@ 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, NULL, 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;
+}
+
 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])
@@ -714,12 +732,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) {
@@ -753,7 +772,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 {
@@ -778,10 +797,13 @@ int git_config_from_parameters(config_fn_t fn, void *data)
 	int ret = 0;
 	char *envw = NULL;
 	struct config_source source = CONFIG_SOURCE_INIT;
+	struct key_value_info kvi = KVI_INIT;
 
 	source.origin_type = CONFIG_ORIGIN_CMDLINE;
 	config_reader_push_source(&the_reader, &source);
 
+	kvi_from_param(&kvi);
+
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -817,7 +839,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;
 			}
@@ -828,7 +850,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;
 		}
@@ -2434,7 +2456,8 @@ 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_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2465,16 +2488,10 @@ 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) {
-		kvi_from_source(reader->source, current_config_scope(), kv_info);
+		kvi_from_source(reader->source, kvi_p->scope, kv_info);
 	} 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;
+		kvi_from_param(kv_info);
 	}
 	si->util = kv_info;
 
@@ -2530,11 +2547,12 @@ struct configset_add_data {
 #define CONFIGSET_ADD_INIT { 0 }
 
 static int config_set_callback(const char *key, const char *value,
-			       const struct config_context *ctx UNUSED,
+			       const struct config_context *ctx,
 			       void *cb)
 {
 	struct configset_add_data *data = cb;
-	configset_add_value(data->config_reader, data->config_set, key, value);
+	configset_add_value(ctx->kvi, data->config_reader, data->config_set,
+			    key, value);
 	return 0;
 }
 
@@ -4027,16 +4045,6 @@ const char *config_origin_type_name(enum config_origin_type type)
 	}
 }
 
-const char *current_config_origin_type(void)
-{
-	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
-
-	if (reader_origin_type(&the_reader, &type))
-		BUG("current_config_origin_type called outside config callback");
-
-	return config_origin_type_name(type);
-}
-
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4068,14 +4076,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	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)
diff --git a/config.h b/config.h
index 1fc746c3a46..7954a8cb104 100644
--- a/config.h
+++ b/config.h
@@ -387,9 +387,8 @@ void git_global_config(char **user, char **xdg);
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
 enum config_scope current_config_scope(void);
-const char *current_config_origin_type(void);
-const char *current_config_name(void);
 const char *config_origin_type_name(enum config_origin_type type);
+void kvi_from_param(struct key_value_info *out);
 
 /*
  * Match and parse a config key of the form:
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index fa6a8df2521..387d336c91f 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1676,8 +1676,8 @@ test_expect_success 'urlmatch with --show-scope' '
 	EOF
 
 	cat >expect <<-EOF &&
-	unknown	http.cookiefile /tmp/cookie.txt
-	unknown	http.sslverify false
+	local	http.cookiefile /tmp/cookie.txt
+	local	http.sslverify false
 	EOF
 	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
 	test_cmp expect actual
@@ -2071,7 +2071,9 @@ test_expect_success '--show-origin blob ref' '
 '
 
 test_expect_success '--show-origin with --default' '
-	test_must_fail git config --show-origin --default foo some.key
+	git config --show-origin --default foo some.key >actual &&
+	echo "command line:	foo" >expect &&
+	test_cmp expect actual
 '
 
 test_expect_success '--show-scope with --list' '
@@ -2144,7 +2146,7 @@ test_expect_success '--show-scope with --show-origin' '
 
 test_expect_success '--show-scope with --default' '
 	git config --show-scope --default foo some.key >actual &&
-	echo "unknown	foo" >expect &&
+	echo "command	foo" >expect &&
 	test_cmp expect actual
 '
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 08/12] trace2: plumb config kvi
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (6 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 07/12] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-23 20:40       ` Jonathan Tan
  2023-06-20 19:43     ` [PATCH v3 09/12] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
                       ` (5 subsequent siblings)
  13 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

There is a code path starting from trace2_def_param_fl() that eventually
calls current_config_scope(), and thus it needs to have "kvi" plumbed
through it. Additional plumbing is also needed to get "kvi" to
trace2_def_param_fl(), which gets called by two code paths:

- Through tr2_cfg_cb(), which is a config callback, so it trivially
  receives "kvi" via the "struct config_context ctx" parameter.

- Through tr2_list_env_vars_fl(), which is a high level function that
  lists environment variables for tracing. This has been secretly
  behaving like git_config_from_parameters() (in that it parses config
  from environment variables/the CLI), but does not set config source
  information.

  Teach tr2_list_env_vars_fl() to be well-behaved by using
  kvi_from_param(), which is used elsewhere for CLI/environment
  variable-based config.

As a result, current_config_scope() has no more callers, so remove it.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c                | 46 -----------------------------------------
 config.h                |  1 -
 trace2.c                |  4 ++--
 trace2.h                |  3 ++-
 trace2/tr2_cfg.c        |  7 +++++--
 trace2/tr2_tgt.h        |  4 +++-
 trace2/tr2_tgt_event.c  |  4 ++--
 trace2/tr2_tgt_normal.c |  4 ++--
 trace2/tr2_tgt_perf.c   |  4 ++--
 9 files changed, 18 insertions(+), 59 deletions(-)

diff --git a/config.c b/config.c
index cfc5c7f10b3..f519656ebcf 100644
--- a/config.c
+++ b/config.c
@@ -85,16 +85,6 @@ struct config_reader {
 	 */
 	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
@@ -125,19 +115,9 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 static inline void config_reader_set_kvi(struct config_reader *reader,
 					 struct key_value_info *kvi)
 {
-	if (kvi && (reader->source || reader->parsing_scope))
-		BUG("kvi should not be set while parsing a config source");
 	reader->config_kvi = kvi;
 }
 
-static inline void config_reader_set_scope(struct config_reader *reader,
-					   enum config_scope scope)
-{
-	if (scope && reader->config_kvi)
-		BUG("scope should only be set when iterating through a config source");
-	reader->parsing_scope = scope;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -411,18 +391,12 @@ 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, &opts);
-
-	config_reader_set_scope(inc->config_reader, store_scope);
 }
 
 static int forbid_remote_url(const char *var, const char *value UNUSED,
@@ -2256,7 +2230,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 	char *xdg_config = NULL;
 	char *user_config = NULL;
 	char *repo_config;
-	enum config_scope prev_parsing_scope = reader->parsing_scope;
 
 	if (opts->commondir)
 		repo_config = mkpathdup("%s/config", opts->commondir);
@@ -2265,7 +2238,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 	else
 		repo_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))
@@ -2273,7 +2245,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 							 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))
@@ -2284,13 +2255,11 @@ static int do_git_config_sequence(struct config_reader *reader,
 		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_with_options(fn, repo_config, data,
 							 CONFIG_SCOPE_LOCAL, NULL);
 
-	config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
 	if (!opts->ignore_worktree && repository_format_worktree_config) {
 		char *path = git_pathdup("config.worktree");
 		if (!access_or_die(path, R_OK, 0))
@@ -2300,11 +2269,9 @@ static int do_git_config_sequence(struct config_reader *reader,
 		free(path);
 	}
 
-	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);
@@ -2317,7 +2284,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) {
@@ -2330,9 +2296,6 @@ int config_with_options(config_fn_t fn, void *data,
 		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.
@@ -2356,7 +2319,6 @@ int config_with_options(config_fn_t fn, void *data,
 		string_list_clear(inc.remote_urls, 0);
 		FREE_AND_NULL(inc.remote_urls);
 	}
-	config_reader_set_scope(&the_reader, prev_scope);
 	return ret;
 }
 
@@ -4076,14 +4038,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	return 0;
 }
 
-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 lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index 7954a8cb104..092db17d2e4 100644
--- a/config.h
+++ b/config.h
@@ -386,7 +386,6 @@ void git_global_config(char **user, char **xdg);
 
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
-enum config_scope current_config_scope(void);
 const char *config_origin_type_name(enum config_origin_type type);
 void kvi_from_param(struct key_value_info *out);
 
diff --git a/trace2.c b/trace2.c
index 0efc4e7b958..49c23bfd05a 100644
--- a/trace2.c
+++ b/trace2.c
@@ -634,7 +634,7 @@ void trace2_thread_exit_fl(const char *file, int line)
 }
 
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value)
+			 const char *value, const struct key_value_info *kvi)
 {
 	struct tr2_tgt *tgt_j;
 	int j;
@@ -644,7 +644,7 @@ void trace2_def_param_fl(const char *file, int line, const char *param,
 
 	for_each_wanted_builtin (j, tgt_j)
 		if (tgt_j->pfn_param_fl)
-			tgt_j->pfn_param_fl(file, line, param, value);
+			tgt_j->pfn_param_fl(file, line, param, value, kvi);
 }
 
 void trace2_def_repo_fl(const char *file, int line, struct repository *repo)
diff --git a/trace2.h b/trace2.h
index 4ced30c0db3..f5c5a9e6bac 100644
--- a/trace2.h
+++ b/trace2.h
@@ -325,6 +325,7 @@ void trace2_thread_exit_fl(const char *file, int line);
 
 #define trace2_thread_exit() trace2_thread_exit_fl(__FILE__, __LINE__)
 
+struct key_value_info;
 /*
  * Emits a "def_param" message containing a key/value pair.
  *
@@ -334,7 +335,7 @@ void trace2_thread_exit_fl(const char *file, int line);
  * `core.abbrev`, `status.showUntrackedFiles`, or `--no-ahead-behind`.
  */
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value);
+			 const char *value, const struct key_value_info *kvi);
 
 #define trace2_def_param(param, value) \
 	trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 83bc4fd109c..00d546a27cd 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -109,7 +109,8 @@ static int tr2_cfg_cb(const char *key, const char *value,
 		struct strbuf *buf = *s;
 		int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
 		if (wm == WM_MATCH) {
-			trace2_def_param_fl(data->file, data->line, key, value);
+			trace2_def_param_fl(data->file, data->line, key, value,
+					    ctx->kvi);
 			return 0;
 		}
 	}
@@ -127,8 +128,10 @@ void tr2_cfg_list_config_fl(const char *file, int line)
 
 void tr2_list_env_vars_fl(const char *file, int line)
 {
+	struct key_value_info kvi = KVI_INIT;
 	struct strbuf **s;
 
+	kvi_from_param(&kvi);
 	if (tr2_load_env_vars() <= 0)
 		return;
 
@@ -136,7 +139,7 @@ void tr2_list_env_vars_fl(const char *file, int line)
 		struct strbuf *buf = *s;
 		const char *val = getenv(buf->buf);
 		if (val && *val)
-			trace2_def_param_fl(file, line, buf->buf, val);
+			trace2_def_param_fl(file, line, buf->buf, val, &kvi);
 	}
 }
 
diff --git a/trace2/tr2_tgt.h b/trace2/tr2_tgt.h
index bf8745c4f05..1f626cffea0 100644
--- a/trace2/tr2_tgt.h
+++ b/trace2/tr2_tgt.h
@@ -69,8 +69,10 @@ typedef void(tr2_tgt_evt_exec_result_fl_t)(const char *file, int line,
 					   uint64_t us_elapsed_absolute,
 					   int exec_id, int code);
 
+struct key_value_info;
 typedef void(tr2_tgt_evt_param_fl_t)(const char *file, int line,
-				     const char *param, const char *value);
+				     const char *param, const char *value,
+				     const struct key_value_info *kvi);
 
 typedef void(tr2_tgt_evt_repo_fl_t)(const char *file, int line,
 				    const struct repository *repo);
diff --git a/trace2/tr2_tgt_event.c b/trace2/tr2_tgt_event.c
index 2af53e5d4de..53091781eca 100644
--- a/trace2/tr2_tgt_event.c
+++ b/trace2/tr2_tgt_event.c
@@ -477,11 +477,11 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct json_writer jw = JSON_WRITER_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	jw_object_begin(&jw, 0);
diff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c
index 1ebfb464d54..d25ea131643 100644
--- a/trace2/tr2_tgt_normal.c
+++ b/trace2/tr2_tgt_normal.c
@@ -297,10 +297,10 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	struct strbuf buf_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
diff --git a/trace2/tr2_tgt_perf.c b/trace2/tr2_tgt_perf.c
index 328e483a05e..a6f9a8a193e 100644
--- a/trace2/tr2_tgt_perf.c
+++ b/trace2/tr2_tgt_perf.c
@@ -439,12 +439,12 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct strbuf buf_payload = STRBUF_INIT;
 	struct strbuf scope_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "%s:%s", param, value);
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 09/12] config: pass kvi to die_bad_number()
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (7 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 08/12] trace2: plumb config kvi Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 10/12] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
                       ` (4 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Plumb "struct key_value_info" through all code paths that end in
die_bad_number(), which lets us remove the helper functions that read
analogous values from "struct config_reader". As a result, nothing reads
config_reader.config_kvi any more, so remove that too.

In config.c, this requires changing the signature of
git_configset_get_value() to 'return' "kvi" in an out parameter so that
git_configset_get_<type>() can pass it to git_config_<type>(). Only
numeric types will use "kvi", so for non-numeric types (e.g.
git_configset_get_string()), pass NULL to indicate that the out
parameter isn't needed.

Outside of config.c, config callbacks now need to pass "ctx->kvi" to any
of the git_config_<type>() functions that parse a config string into a
number type. Included is a .cocci patch to make that refactor.

The only exceptional case is builtin/config.c, where git_config_<type>()
is called outside of a config callback (namely, on user-provided input),
so config source information has never been available. In this case,
die_bad_number() defaults to a generic, but perfectly descriptive
message. Let's provide a safe, non-NULL for "kvi" anyway, but make sure
not to change the message.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 archive-tar.c                              |   4 +-
 builtin/commit-graph.c                     |   4 +-
 builtin/commit.c                           |  10 +-
 builtin/config.c                           |  21 +--
 builtin/fetch.c                            |   4 +-
 builtin/fsmonitor--daemon.c                |   6 +-
 builtin/grep.c                             |   2 +-
 builtin/index-pack.c                       |   4 +-
 builtin/log.c                              |   2 +-
 builtin/pack-objects.c                     |  14 +-
 builtin/receive-pack.c                     |  10 +-
 builtin/submodule--helper.c                |   4 +-
 config.c                                   | 156 ++++++++-------------
 config.h                                   |  17 ++-
 contrib/coccinelle/git_config_number.cocci |  27 ++++
 diff.c                                     |   9 +-
 fmt-merge-msg.c                            |   2 +-
 help.c                                     |   4 +-
 http.c                                     |  10 +-
 imap-send.c                                |   2 +-
 sequencer.c                                |  22 +--
 setup.c                                    |   2 +-
 submodule-config.c                         |  13 +-
 submodule-config.h                         |   3 +-
 t/helper/test-config.c                     |   6 +-
 trace2/tr2_cfg.c                           |   2 +-
 upload-pack.c                              |  12 +-
 worktree.c                                 |   2 +-
 28 files changed, 191 insertions(+), 183 deletions(-)
 create mode 100644 contrib/coccinelle/git_config_number.cocci

diff --git a/archive-tar.c b/archive-tar.c
index ef06e516b1f..3df8af6d1b1 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -412,14 +412,14 @@ static int tar_filter_config(const char *var, const char *value,
 }
 
 static int git_tar_config(const char *var, const char *value,
-			  const struct config_context *ctx UNUSED, void *cb)
+			  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
 			tar_umask = umask(0);
 			umask(tar_umask);
 		} else {
-			tar_umask = git_config_int(var, value);
+			tar_umask = git_config_int(var, value, ctx->kvi);
 		}
 		return 0;
 	}
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 13d35b00ca8..c7e27a8c0a2 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -186,11 +186,11 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
-					 const struct config_context *ctx UNUSED,
+					 const struct config_context *ctx,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
-		write_opts.max_new_filters = git_config_int(var, value);
+		write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
 	/*
 	 * No need to fall-back to 'git_default_config', since this was already
 	 * called in 'cmd_commit_graph()'.
diff --git a/builtin/commit.c b/builtin/commit.c
index 3bc87e5fc97..4e5e7722238 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1415,7 +1415,8 @@ static int git_status_config(const char *k, const char *v,
 		return git_column_config(k, v, "status", &s->colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
-		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
+		s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
+							      &is_bool);
 		if (is_bool && s->submodule_summary)
 			s->submodule_summary = -1;
 		return 0;
@@ -1475,11 +1476,11 @@ static int git_status_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "diff.renamelimit")) {
 		if (s->rename_limit == -1)
-			s->rename_limit = git_config_int(k, v);
+			s->rename_limit = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "status.renamelimit")) {
-		s->rename_limit = git_config_int(k, v);
+		s->rename_limit = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "diff.renames")) {
@@ -1625,7 +1626,8 @@ static int git_commit_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "commit.verbose")) {
 		int is_bool;
-		config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
+		config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
+							       &is_bool);
 		return 0;
 	}
 
diff --git a/builtin/config.c b/builtin/config.c
index 0c01ded5f05..c61988a7bf3 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -261,13 +261,14 @@ static int format_config(struct strbuf *buf, const char *key_,
 
 		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
@@ -422,7 +423,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;
@@ -437,12 +439,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
@@ -669,6 +671,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));
 
@@ -886,7 +889,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"
@@ -895,7 +898,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);
@@ -903,7 +906,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,
@@ -912,7 +915,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);
diff --git a/builtin/fetch.c b/builtin/fetch.c
index b607605b491..033d1fe65d2 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -138,7 +138,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "submodule.fetchjobs")) {
-		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
+		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v, ctx->kvi);
 		return 0;
 	} else if (!strcmp(k, "fetch.recursesubmodules")) {
 		recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
@@ -146,7 +146,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "fetch.parallel")) {
-		fetch_parallel_config = git_config_int(k, v);
+		fetch_parallel_config = git_config_int(k, v, ctx->kvi);
 		if (fetch_parallel_config < 0)
 			die(_("fetch.parallel cannot be negative"));
 		if (!fetch_parallel_config)
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 91a776e2f17..6d2826b07da 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -41,7 +41,7 @@ static int fsmonitor_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, ctx->kvi);
 		if (i < 1)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__IPC_THREADS, i);
@@ -50,7 +50,7 @@ static int fsmonitor_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, FSMONITOR__START_TIMEOUT)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, ctx->kvi);
 		if (i < 0)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__START_TIMEOUT, i);
@@ -60,7 +60,7 @@ static int fsmonitor_config(const char *var, const char *value,
 
 	if (!strcmp(var, FSMONITOR__ANNOUNCE_STARTUP)) {
 		int is_bool;
-		int i = git_config_bool_or_int(var, value, &is_bool);
+		int i = git_config_bool_or_int(var, value, ctx->kvi, &is_bool);
 		if (i < 0)
 			return error(_("value of '%s' not bool or int: %d"),
 				     var, i);
diff --git a/builtin/grep.c b/builtin/grep.c
index 757d52b94ec..3a464e6faab 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -301,7 +301,7 @@ static int grep_cmd_config(const char *var, const char *value,
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
-		num_threads = git_config_int(var, value);
+		num_threads = git_config_int(var, value, ctx->kvi);
 		if (num_threads < 0)
 			die(_("invalid number of threads specified (%d) for %s"),
 			    num_threads, var);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 31914aac298..857518fdf83 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1587,13 +1587,13 @@ static int git_index_pack_config(const char *k, const char *v,
 	struct pack_idx_option *opts = cb;
 
 	if (!strcmp(k, "pack.indexversion")) {
-		opts->version = git_config_int(k, v);
+		opts->version = git_config_int(k, v, ctx->kvi);
 		if (opts->version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32), opts->version);
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		nr_threads = git_config_int(k, v);
+		nr_threads = git_config_int(k, v, ctx->kvi);
 		if (nr_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    nr_threads);
diff --git a/builtin/log.c b/builtin/log.c
index ac5f74b6b06..675cf358f7a 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -574,7 +574,7 @@ static int git_log_config(const char *var, const char *value,
 	if (!strcmp(var, "format.subjectprefix"))
 		return git_config_string(&fmt_patch_subject_prefix, var, value);
 	if (!strcmp(var, "format.filenamemaxlength")) {
-		fmt_patch_name_max = git_config_int(var, value);
+		fmt_patch_name_max = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(var, "format.encodeemailheaders")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index ad4d8fafb0b..d6d9079fd93 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3139,23 +3139,23 @@ static int git_pack_config(const char *k, const char *v,
 			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
-		window = git_config_int(k, v);
+		window = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.windowmemory")) {
-		window_memory_limit = git_config_ulong(k, v);
+		window_memory_limit = git_config_ulong(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.depth")) {
-		depth = git_config_int(k, v);
+		depth = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachesize")) {
-		max_delta_cache_size = git_config_int(k, v);
+		max_delta_cache_size = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachelimit")) {
-		cache_max_small_delta_size = git_config_int(k, v);
+		cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.writebitmaphashcache")) {
@@ -3181,7 +3181,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		delta_search_threads = git_config_int(k, v);
+		delta_search_threads = git_config_int(k, v, ctx->kvi);
 		if (delta_search_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    delta_search_threads);
@@ -3192,7 +3192,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.indexversion")) {
-		pack_idx_opts.version = git_config_int(k, v);
+		pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
 		if (pack_idx_opts.version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32),
 			    pack_idx_opts.version);
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 94d9898aff7..98f6f0038f0 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -158,12 +158,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.unpacklimit") == 0) {
-		receive_unpack_limit = git_config_int(var, value);
+		receive_unpack_limit = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "transfer.unpacklimit") == 0) {
-		transfer_unpack_limit = git_config_int(var, value);
+		transfer_unpack_limit = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -231,7 +231,7 @@ static int receive_pack_config(const char *var, const char *value,
 		return git_config_string(&cert_nonce_seed, var, value);
 
 	if (strcmp(var, "receive.certnonceslop") == 0) {
-		nonce_stamp_slop_limit = git_config_ulong(var, value);
+		nonce_stamp_slop_limit = git_config_ulong(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -246,12 +246,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.keepalive") == 0) {
-		keepalive_in_sec = git_config_int(var, value);
+		keepalive_in_sec = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "receive.maxinputsize") == 0) {
-		max_input_size = git_config_int64(var, value);
+		max_input_size = git_config_int64(var, value, ctx->kvi);
 		return 0;
 	}
 
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 3f8f3692937..9e78e5f7a9d 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2189,13 +2189,13 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
-				   const struct config_context *ctx UNUSED,
+				   const struct config_context *ctx,
 				   void *cb)
 {
 	int *max_jobs = cb;
 
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
 	return 0;
 }
 
diff --git a/config.c b/config.c
index f519656ebcf..a2887b856aa 100644
--- a/config.c
+++ b/config.c
@@ -73,18 +73,8 @@ struct config_reader {
 	 *
 	 * 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;
 };
 /*
  * Where possible, prefer to accept "struct config_reader" as an arg than to use
@@ -96,8 +86,6 @@ static struct config_reader the_reader;
 static inline void config_reader_push_source(struct config_reader *reader,
 					     struct config_source *top)
 {
-	if (reader->config_kvi)
-		BUG("source should not be set while iterating a config set");
 	top->prev = reader->source;
 	reader->source = top;
 }
@@ -112,12 +100,6 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 	return ret;
 }
 
-static inline void config_reader_set_kvi(struct config_reader *reader,
-					 struct key_value_info *kvi)
-{
-	reader->config_kvi = kvi;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -1344,80 +1326,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;
 }
 
@@ -1522,7 +1502,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) {
@@ -1530,7 +1511,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)
@@ -1656,7 +1637,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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;
@@ -1668,7 +1649,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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)
@@ -1679,7 +1660,7 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	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)
@@ -1693,7 +1674,7 @@ static int git_default_core_config(const char *var, const char *value,
 
 	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;
@@ -1704,17 +1685,17 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	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;
 	}
 
@@ -1998,12 +1979,12 @@ int git_default_config(const char *var, const char *value,
 	}
 
 	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)
@@ -2336,13 +2317,11 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 		value_index = list->items[i].value_index;
 		values = &entry->value_list;
 
-		config_reader_set_kvi(reader, values->items[value_index].util);
 		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,
 					      ctx.kvi->filename,
 					      ctx.kvi->linenr);
-		config_reader_set_kvi(reader, NULL);
 	}
 }
 
@@ -2526,11 +2505,12 @@ int git_configset_add_file(struct config_set *set, const char *filename)
 	return git_config_from_file(config_set_callback, filename, &data);
 }
 
-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
@@ -2540,7 +2520,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;
 }
 
@@ -2593,7 +2576,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;
@@ -2603,7 +2586,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;
@@ -2616,8 +2599,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;
@@ -2626,8 +2611,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;
@@ -2636,7 +2623,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
@@ -2647,8 +2634,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;
@@ -2657,7 +2646,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;
@@ -2669,7 +2658,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;
@@ -2739,7 +2728,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,
@@ -3977,18 +3966,6 @@ 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 *config_origin_type_name(enum config_origin_type type)
 {
 	switch (type) {
@@ -4027,17 +4004,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;
-}
-
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index 092db17d2e4..a8f41c59e6c 100644
--- a/config.h
+++ b/config.h
@@ -249,22 +249,26 @@ int git_parse_maybe_bool(const char *);
  * Parse the string to an integer, including unit factors. Dies on error;
  * otherwise, returns the parsed result.
  */
-int git_config_int(const char *, const char *);
+int git_config_int(const char *, const char *, const struct key_value_info *);
 
-int64_t git_config_int64(const char *, const char *);
+int64_t git_config_int64(const char *, const char *,
+			 const struct key_value_info *);
 
 /**
  * Identical to `git_config_int`, but for unsigned longs.
  */
-unsigned long git_config_ulong(const char *, const char *);
+unsigned long git_config_ulong(const char *, const char *,
+			       const struct key_value_info *);
 
-ssize_t git_config_ssize_t(const char *, const char *);
+ssize_t git_config_ssize_t(const char *, const char *,
+			   const struct key_value_info *);
 
 /**
  * Same as `git_config_bool`, except that integers are returned as-is, and
  * an `is_bool` flag is unset.
  */
-int git_config_bool_or_int(const char *, const char *, int *);
+int git_config_bool_or_int(const char *, const char *,
+			   const struct key_value_info *, int *);
 
 /**
  * Parse a string into a boolean value, respecting keywords like "true" and
@@ -529,7 +533,8 @@ int git_configset_get(struct config_set *cs, const char *key);
  * touching `value`. The caller should not free or modify `value`, as it
  * is owned by the cache.
  */
-int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
+int git_configset_get_value(struct config_set *cs, const char *key,
+			    const char **dest, struct key_value_info *kvi);
 
 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
diff --git a/contrib/coccinelle/git_config_number.cocci b/contrib/coccinelle/git_config_number.cocci
new file mode 100644
index 00000000000..7b57dceefe6
--- /dev/null
+++ b/contrib/coccinelle/git_config_number.cocci
@@ -0,0 +1,27 @@
+@@
+identifier C1, C2, C3;
+@@
+(
+(
+git_config_int
+|
+git_config_int64
+|
+git_config_ulong
+|
+git_config_ssize_t
+)
+  (C1, C2
++ , ctx->kvi
+  )
+|
+(
+git_configset_get_value
+|
+git_config_bool_or_int
+)
+  (C1, C2
++ , ctx->kvi
+ , C3
+  )
+)
diff --git a/diff.c b/diff.c
index 07be3bee137..8775323e39c 100644
--- a/diff.c
+++ b/diff.c
@@ -379,13 +379,14 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.context")) {
-		diff_context_default = git_config_int(var, value);
+		diff_context_default = git_config_int(var, value, ctx->kvi);
 		if (diff_context_default < 0)
 			return -1;
 		return 0;
 	}
 	if (!strcmp(var, "diff.interhunkcontext")) {
-		diff_interhunk_context_default = git_config_int(var, value);
+		diff_interhunk_context_default = git_config_int(var, value,
+								ctx->kvi);
 		if (diff_interhunk_context_default < 0)
 			return -1;
 		return 0;
@@ -411,7 +412,7 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.statgraphwidth")) {
-		diff_stat_graph_width = git_config_int(var, value);
+		diff_stat_graph_width = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(var, "diff.external"))
@@ -450,7 +451,7 @@ int git_diff_basic_config(const char *var, const char *value,
 	const char *name;
 
 	if (!strcmp(var, "diff.renamelimit")) {
-		diff_rename_limit_default = git_config_int(var, value);
+		diff_rename_limit_default = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 10137444321..3ae59bde2f2 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -25,7 +25,7 @@ int fmt_merge_msg_config(const char *key, const char *value,
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
-		merge_log_config = git_config_bool_or_int(key, value, &is_bool);
+		merge_log_config = git_config_bool_or_int(key, value, ctx->kvi, &is_bool);
 		if (!is_bool && merge_log_config < 0)
 			return error("%s: negative length %s", key, value);
 		if (is_bool && merge_log_config)
diff --git a/help.c b/help.c
index ac0ae5ac0dc..389382b1482 100644
--- a/help.c
+++ b/help.c
@@ -545,7 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
-				  const struct config_context *ctx UNUSED,
+				  const struct config_context *ctx,
 				  void *cb UNUSED)
 {
 	const char *p;
@@ -560,7 +560,7 @@ static int git_unknown_cmd_config(const char *var, const char *value,
 		} else if (!strcmp(value, "prompt")) {
 			autocorrect = AUTOCORRECT_PROMPT;
 		} else {
-			int v = git_config_int(var, value);
+			int v = git_config_int(var, value, ctx->kvi);
 			autocorrect = (v < 0)
 				? AUTOCORRECT_IMMEDIATELY : v;
 		}
diff --git a/http.c b/http.c
index 762502828c9..b12b234bde1 100644
--- a/http.c
+++ b/http.c
@@ -414,21 +414,21 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.minsessions", var)) {
-		min_curl_sessions = git_config_int(var, value);
+		min_curl_sessions = git_config_int(var, value, ctx->kvi);
 		if (min_curl_sessions > 1)
 			min_curl_sessions = 1;
 		return 0;
 	}
 	if (!strcmp("http.maxrequests", var)) {
-		max_requests = git_config_int(var, value);
+		max_requests = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedlimit", var)) {
-		curl_low_speed_limit = (long)git_config_int(var, value);
+		curl_low_speed_limit = (long)git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedtime", var)) {
-		curl_low_speed_time = (long)git_config_int(var, value);
+		curl_low_speed_time = (long)git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -464,7 +464,7 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.postbuffer", var)) {
-		http_post_buffer = git_config_ssize_t(var, value);
+		http_post_buffer = git_config_ssize_t(var, value, ctx->kvi);
 		if (http_post_buffer < 0)
 			warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
 		if (http_post_buffer < LARGE_PACKET_MAX)
diff --git a/imap-send.c b/imap-send.c
index 47777e76861..3518a4ace6c 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1342,7 +1342,7 @@ static int git_imap_config(const char *var, const char *val,
 	else if (!strcmp("imap.authmethod", var))
 		return git_config_string(&server.auth_method, var, val);
 	else if (!strcmp("imap.port", var))
-		server.port = git_config_int(var, val);
+		server.port = git_config_int(var, val, ctx->kvi);
 	else if (!strcmp("imap.host", var)) {
 		if (!val) {
 			git_die_config("imap.host", "Missing value for 'imap.host'");
diff --git a/sequencer.c b/sequencer.c
index 34754d17596..a0aaee3056d 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2883,7 +2883,7 @@ static int git_config_string_dup(char **dest,
 }
 
 static int populate_opts_cb(const char *key, const char *value,
-			    const struct config_context *ctx UNUSED,
+			    const struct config_context *ctx,
 			    void *data)
 {
 	struct replay_opts *opts = data;
@@ -2892,26 +2892,26 @@ static int populate_opts_cb(const char *key, const char *value,
 	if (!value)
 		error_flag = 0;
 	else if (!strcmp(key, "options.no-commit"))
-		opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
+		opts->no_commit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.edit"))
-		opts->edit = git_config_bool_or_int(key, value, &error_flag);
+		opts->edit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty"))
 		opts->allow_empty =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty-message"))
 		opts->allow_empty_message =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.keep-redundant-commits"))
 		opts->keep_redundant_commits =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.signoff"))
-		opts->signoff = git_config_bool_or_int(key, value, &error_flag);
+		opts->signoff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.record-origin"))
-		opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
+		opts->record_origin = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-ff"))
-		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
+		opts->allow_ff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
-		opts->mainline = git_config_int(key, value);
+		opts->mainline = git_config_int(key, value, ctx->kvi);
 	else if (!strcmp(key, "options.strategy"))
 		git_config_string_dup(&opts->strategy, key, value);
 	else if (!strcmp(key, "options.gpg-sign"))
@@ -2920,7 +2920,7 @@ static int populate_opts_cb(const char *key, const char *value,
 		strvec_push(&opts->xopts, value);
 	} else if (!strcmp(key, "options.allow-rerere-auto"))
 		opts->allow_rerere_auto =
-			git_config_bool_or_int(key, value, &error_flag) ?
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag) ?
 				RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
 	else if (!strcmp(key, "options.default-msg-cleanup")) {
 		opts->explicit_cleanup = 1;
diff --git a/setup.c b/setup.c
index e7f81151ef0..260ad92df6e 100644
--- a/setup.c
+++ b/setup.c
@@ -597,7 +597,7 @@ static int check_repo_format(const char *var, const char *value,
 	const char *ext;
 
 	if (strcmp(var, "core.repositoryformatversion") == 0)
-		data->version = git_config_int(var, value);
+		data->version = git_config_int(var, value, ctx->kvi);
 	else if (skip_prefix(var, "extensions.", &ext)) {
 		switch (handle_extension_v0(var, value, ext, data)) {
 		case EXTENSION_ERROR:
diff --git a/submodule-config.c b/submodule-config.c
index e19ab593aa4..0cda0f659f2 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -304,9 +304,10 @@ static int parse_fetch_recurse(const char *opt, const char *arg,
 	}
 }
 
-int parse_submodule_fetchjobs(const char *var, const char *value)
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      const struct key_value_info *kvi)
 {
-	int fetchjobs = git_config_int(var, value);
+	int fetchjobs = git_config_int(var, value, kvi);
 	if (fetchjobs < 0)
 		die(_("negative values not allowed for submodule.fetchJobs"));
 	if (!fetchjobs)
@@ -850,14 +851,14 @@ struct fetch_config {
 };
 
 static int gitmodules_fetch_config(const char *var, const char *value,
-				   const struct config_context *ctx UNUSED,
+				   const struct config_context *ctx,
 				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
 		if (config->max_children)
 			*(config->max_children) =
-				parse_submodule_fetchjobs(var, value);
+				parse_submodule_fetchjobs(var, value, ctx->kvi);
 		return 0;
 	} else if (!strcmp(var, "fetch.recursesubmodules")) {
 		if (config->recurse_submodules)
@@ -879,12 +880,12 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
-					  const struct config_context *ctx UNUSED,
+					  const struct config_context *ctx,
 					  void *cb)
 {
 	int *max_jobs = cb;
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
 	return 0;
 }
 
diff --git a/submodule-config.h b/submodule-config.h
index c2045875bbb..2a37689cc27 100644
--- a/submodule-config.h
+++ b/submodule-config.h
@@ -50,7 +50,8 @@ struct repository;
 
 void submodule_cache_free(struct submodule_cache *cache);
 
-int parse_submodule_fetchjobs(const char *var, const char *value);
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      const struct key_value_info *kvi);
 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 struct option;
 int option_fetch_parse_recurse_submodules(const struct option *opt,
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 3f4c3678318..ed444ca4c25 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -63,12 +63,12 @@ static int iterate_cb(const char *var, const char *value,
 }
 
 static int parse_int_cb(const char *var, const char *value,
-			const struct config_context *ctx UNUSED, void *data)
+			const struct config_context *ctx, void *data)
 {
 	const char *key_to_match = data;
 
 	if (!strcmp(key_to_match, var)) {
-		int parsed = git_config_int(value, value);
+		int parsed = git_config_int(value, value, ctx->kvi);
 		printf("%d\n", parsed);
 	}
 	return 0;
@@ -182,7 +182,7 @@ int cmd__config(int argc, const char **argv)
 				goto exit2;
 			}
 		}
-		if (!git_configset_get_value(&cs, argv[2], &v)) {
+		if (!git_configset_get_value(&cs, argv[2], &v, NULL)) {
 			if (!v)
 				printf("(NULL)\n");
 			else
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 00d546a27cd..733d9d2872a 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -100,7 +100,7 @@ struct tr2_cfg_data {
  * See if the given config key matches any of our patterns of interest.
  */
 static int tr2_cfg_cb(const char *key, const char *value,
-		      const struct config_context *ctx UNUSED, void *d)
+		      const struct config_context *ctx, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
diff --git a/upload-pack.c b/upload-pack.c
index 951fd1f9c25..c03415a5460 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1275,7 +1275,8 @@ static int find_symref(const char *refname,
 }
 
 static int parse_object_filter_config(const char *var, const char *value,
-				       struct upload_pack_data *data)
+				      const struct key_value_info *kvi,
+				      struct upload_pack_data *data)
 {
 	struct strbuf buf = STRBUF_INIT;
 	const char *sub, *key;
@@ -1302,7 +1303,8 @@ static int parse_object_filter_config(const char *var, const char *value,
 		}
 		string_list_insert(&data->allowed_filters, buf.buf)->util =
 			(void *)(intptr_t)1;
-		data->tree_filter_max_depth = git_config_ulong(var, value);
+		data->tree_filter_max_depth = git_config_ulong(var, value,
+							       kvi);
 	}
 
 	strbuf_release(&buf);
@@ -1310,7 +1312,7 @@ static int parse_object_filter_config(const char *var, const char *value,
 }
 
 static int upload_pack_config(const char *var, const char *value,
-			      const struct config_context *ctx UNUSED,
+			      const struct config_context *ctx,
 			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
@@ -1331,7 +1333,7 @@ static int upload_pack_config(const char *var, const char *value,
 		else
 			data->allow_uor &= ~ALLOW_ANY_SHA1;
 	} else if (!strcmp("uploadpack.keepalive", var)) {
-		data->keepalive = git_config_int(var, value);
+		data->keepalive = git_config_int(var, value, ctx->kvi);
 		if (!data->keepalive)
 			data->keepalive = -1;
 	} else if (!strcmp("uploadpack.allowfilter", var)) {
@@ -1346,7 +1348,7 @@ static int upload_pack_config(const char *var, const char *value,
 		data->advertise_sid = git_config_bool(var, value);
 	}
 
-	if (parse_object_filter_config(var, value, data) < 0)
+	if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
 		return -1;
 
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
diff --git a/worktree.c b/worktree.c
index b5ee71c5ebd..1fbdbd745fb 100644
--- a/worktree.c
+++ b/worktree.c
@@ -835,7 +835,7 @@ int init_worktree_config(struct repository *r)
 	 * Relocate that value to avoid breaking all worktrees with this
 	 * upgrade to worktree config.
 	 */
-	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
+	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
 		if ((res = move_config_setting("core.worktree", core_worktree,
 					       common_config_file,
 					       main_worktree_file)))
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 10/12] config.c: remove config_reader from configsets
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (8 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 09/12] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-23 20:57       ` Jonathan Tan
  2023-06-20 19:43     ` [PATCH v3 11/12] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
                       ` (3 subsequent siblings)
  13 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Remove the last usage of "struct config_reader" from configsets by
copying the "kvi" arg instead of recomputing "kvi" from
config_reader.source. Since we no longer need to pass both "struct
config_reader" and "struct config_set" in a single "void *cb", remove
"struct configset_add_data" too.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 42 ++++++++++--------------------------------
 1 file changed, 10 insertions(+), 32 deletions(-)

diff --git a/config.c b/config.c
index a2887b856aa..958ba166cf9 100644
--- a/config.c
+++ b/config.c
@@ -2303,8 +2303,7 @@ int config_with_options(config_fn_t fn, void *data,
 	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;
@@ -2398,7 +2397,6 @@ static int configset_find_element(struct config_set *set, const char *key,
 }
 
 static int configset_add_value(const struct key_value_info *kvi_p,
-			       struct config_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2429,11 +2427,7 @@ static int configset_add_value(const struct key_value_info *kvi_p,
 	l_item->e = e;
 	l_item->value_index = e->value_list.nr - 1;
 
-	if (reader->source->name) {
-		kvi_from_source(reader->source, kvi_p->scope, kv_info);
-	} else {
-		kvi_from_param(kv_info);
-	}
+	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
 	si->util = kv_info;
 
 	return 0;
@@ -2481,28 +2475,18 @@ 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,
 			       const struct config_context *ctx,
 			       void *cb)
 {
-	struct configset_add_data *data = cb;
-	configset_add_value(ctx->kvi, 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,
@@ -2668,7 +2652,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;
@@ -2680,10 +2663,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, &opts) < 0)
+	if (config_with_options(config_set_callback, repo->config, NULL,
+				&opts) < 0)
 		/*
 		 * config_with_options() normally returns only
 		 * zero, as most errors are fatal, and
@@ -2715,7 +2696,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)
@@ -2822,19 +2803,16 @@ 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, &opts);
+	config_with_options(config_set_callback, &protected_config, 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' */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 11/12] config: add kvi.path, use it to evaluate includes
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (9 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 10/12] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 19:43     ` [PATCH v3 12/12] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
                       ` (2 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Include directives are evaluated using the path of the config file. To
reduce the dependence on "config_reader.source", add a new
"key_value_info.path" member and use that instead of
"config_source.path". This allows us to remove a "struct config_reader
*" field from "struct config_include_data", which will subsequently
allow us to remove "struct config_reader" entirely.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 40 +++++++++++++++++++---------------------
 config.h |  2 ++
 2 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/config.c b/config.c
index 958ba166cf9..04c7db6f1be 100644
--- a/config.c
+++ b/config.c
@@ -161,7 +161,6 @@ struct config_include_data {
 	void *data;
 	const struct config_options *opts;
 	struct git_config_source *config_source;
-	struct config_reader *config_reader;
 
 	/*
 	 * All remote URLs discovered when reading all config files.
@@ -180,8 +179,7 @@ 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 struct key_value_info *kvi,
+static int handle_path_include(const struct key_value_info *kvi,
 			       const char *path,
 			       struct config_include_data *inc)
 {
@@ -204,14 +202,14 @@ static int handle_path_include(struct config_source *cs,
 	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;
 	}
@@ -219,8 +217,8 @@ static int handle_path_include(struct config_source *cs,
 	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_with_options(git_config_include, path, inc,
 							kvi->scope, NULL);
@@ -238,7 +236,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;
@@ -255,11 +253,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?");
@@ -274,7 +272,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)
 {
@@ -291,7 +289,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)
@@ -426,16 +424,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,
@@ -451,7 +449,6 @@ static int git_config_include(const char *var, const char *value,
 			      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;
@@ -465,16 +462,16 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, ctx->kvi, 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, ctx->kvi, value, inc);
+		ret = handle_path_include(ctx->kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -659,6 +656,7 @@ void kvi_from_param(struct key_value_info *out)
 	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,
@@ -1062,6 +1060,7 @@ static void kvi_from_source(struct config_source *cs,
 	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,
@@ -2272,7 +2271,6 @@ int config_with_options(config_fn_t fn, void *data,
 		inc.data = data;
 		inc.opts = opts;
 		inc.config_source = config_source;
-		inc.config_reader = &the_reader;
 		fn = git_config_include;
 		data = &inc;
 	}
diff --git a/config.h b/config.h
index a8f41c59e6c..8841b7a1cf6 100644
--- a/config.h
+++ b/config.h
@@ -117,12 +117,14 @@ struct key_value_info {
 	int linenr;
 	enum config_origin_type origin_type;
 	enum config_scope scope;
+	const char *path;
 };
 #define KVI_INIT { \
 	.filename = NULL, \
 	.linenr = -1, \
 	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
 	.scope = CONFIG_SCOPE_UNKNOWN, \
+	.path = NULL, \
 }
 
 /* Captures additional information that a config callback can use. */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v3 12/12] config: pass source to config_parser_event_fn_t
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (10 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 11/12] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
@ 2023-06-20 19:43     ` Glen Choo via GitGitGadget
  2023-06-20 21:46       ` Junio C Hamano
  2023-06-21 21:46     ` [PATCH v3 00/12] config: remove global state from config iteration Junio C Hamano
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
  13 siblings, 1 reply; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-20 19:43 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

..so that the callback can use a "struct config_source" parameter
instead of "config_reader.source". "struct config_source" is internal to
config.c, so we are adding a pointer to a struct defined in config.c
into a public function signature defined in config.h, but this is okay
because this function has only ever been (and probably ever will be)
used internally by config.c.

As a result, the_reader isn't used anywhere, so "struct config_reader"
is obsolete (it was only intended to be used with the_reader). Remove
them.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 77 ++++++++++----------------------------------------------
 config.h |  6 +++++
 2 files changed, 19 insertions(+), 64 deletions(-)

diff --git a/config.c b/config.c
index 04c7db6f1be..c58e44b15c5 100644
--- a/config.c
+++ b/config.c
@@ -66,40 +66,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).
-	 */
-	struct config_source *source;
-};
-/*
- * 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 int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -750,14 +716,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;
 	struct key_value_info kvi = KVI_INIT;
 
-	source.origin_type = CONFIG_ORIGIN_CMDLINE;
-	config_reader_push_source(&the_reader, &source);
-
 	kvi_from_param(&kvi);
-
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -814,7 +775,6 @@ out:
 	strbuf_release(&envvar);
 	strvec_clear(&to_free);
 	free(envw);
-	config_reader_pop_source(&the_reader);
 	return ret;
 }
 
@@ -1043,7 +1003,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;
@@ -2005,8 +1965,7 @@ int git_default_config(const char *var, const char *value,
  * 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,
+static int do_config_from(struct config_source *top, config_fn_t fn,
 			  void *data, enum config_scope scope,
 			  const struct config_options *opts)
 {
@@ -2019,21 +1978,17 @@ 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, &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, enum config_scope scope,
@@ -2052,7 +2007,7 @@ 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, scope, opts);
+	ret = do_config_from(&top, fn, data, scope, opts);
 	funlockfile(f);
 	return ret;
 }
@@ -2060,8 +2015,8 @@ static int do_config_from_file(struct config_reader *reader,
 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, scope, 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,
@@ -2075,9 +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, scope,
-					  opts);
+		ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
+					  filename, f, data, scope, opts);
 		fclose(f);
 	}
 	return ret;
@@ -2108,7 +2062,7 @@ 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, scope, opts);
+	return do_config_from(&top, fn, data, scope, opts);
 }
 
 int git_config_from_blob_oid(config_fn_t fn,
@@ -2201,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,
 				  config_fn_t fn, void *data)
 {
 	int ret = 0;
@@ -2291,7 +2244,7 @@ int config_with_options(config_fn_t fn, void *data,
 		ret = git_config_from_blob_ref(fn, repo, config_source->blob,
 					       data, config_source->scope);
 	} else {
-		ret = do_git_config_sequence(&the_reader, opts, fn, data);
+		ret = do_git_config_sequence(opts, fn, data);
 	}
 
 	if (inc.remote_urls) {
@@ -3000,7 +2953,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;
@@ -3046,11 +2998,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;
@@ -3371,8 +3322,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)
diff --git a/config.h b/config.h
index 8841b7a1cf6..f663cdd8e1b 100644
--- a/config.h
+++ b/config.h
@@ -73,6 +73,7 @@ enum config_event_t {
 	CONFIG_EVENT_ERROR
 };
 
+struct config_source;
 /*
  * The parser event function (if not NULL) is called with the event type and
  * the begin/end offsets of the parsed elements.
@@ -82,6 +83,7 @@ enum config_event_t {
  */
 typedef int (*config_parser_event_fn_t)(enum config_event_t type,
 					size_t begin_offset, size_t end_offset,
+					struct config_source *cs,
 					void *event_fn_data);
 
 struct config_options {
@@ -101,6 +103,10 @@ struct config_options {
 
 	const char *commondir;
 	const char *git_dir;
+	/*
+	 * event_fn and event_fn_data are for internal use only. Handles events
+	 * emitted by the config parser.
+	 */
 	config_parser_event_fn_t event_fn;
 	void *event_fn_data;
 	enum config_error_action {
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 01/12] config: inline git_color_default_config
  2023-06-20 19:43     ` [PATCH v3 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
@ 2023-06-20 21:01       ` Junio C Hamano
  0 siblings, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-20 21:01 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Glen Choo <chooglen@google.com>
>
> git_color_default_config() is a shorthand for calling two other config
> callbacks. There are no other non-static functions that do this and it
> will complicate our refactoring of config_fn_t so inline it instead.
>
> Signed-off-by: Glen Choo <chooglen@google.com>
> ---

We would need to deal with git_default_config() that is called from
everywhere in exactly the same way, so I am not sure if the presence
of git_color_default_config() will complicate so much to require
this step while git_default_config() can be dealt with without
inlining into its callers, but this change does not hurt anybody
either.

OK.  

>  builtin/branch.c      | 5 ++++-
>  builtin/clean.c       | 6 ++++--
>  builtin/grep.c        | 5 ++++-
>  builtin/show-branch.c | 5 ++++-
>  builtin/tag.c         | 6 +++++-
>  color.c               | 8 --------
>  color.h               | 6 +-----
>  7 files changed, 22 insertions(+), 19 deletions(-)
>
> diff --git a/builtin/branch.c b/builtin/branch.c
> index e6c2655af68..df99e38847b 100644
> --- a/builtin/branch.c
> +++ b/builtin/branch.c
> @@ -117,7 +117,10 @@ static int git_branch_config(const char *var, const char *value, void *cb)
>  		return 0;
>  	}
>  
> -	return git_color_default_config(var, value, cb);
> +	if (git_color_config(var, value, cb) < 0)
> +		return -1;
> +
> +	return git_default_config(var, value, cb);
>  }
>  
>  static const char *branch_get_color(enum color_branch ix)
> diff --git a/builtin/clean.c b/builtin/clean.c
> index 78852d28cec..57e7f7cac64 100644
> --- a/builtin/clean.c
> +++ b/builtin/clean.c
> @@ -130,8 +130,10 @@ static int git_clean_config(const char *var, const char *value, void *cb)
>  		return 0;
>  	}
>  
> -	/* inspect the color.ui config variable and others */
> -	return git_color_default_config(var, value, cb);
> +	if (git_color_config(var, value, cb) < 0)
> +		return -1;
> +
> +	return git_default_config(var, value, cb);
>  }
>  
>  static const char *clean_get_color(enum color_clean ix)
> diff --git a/builtin/grep.c b/builtin/grep.c
> index b86c754defb..76cf999d310 100644
> --- a/builtin/grep.c
> +++ b/builtin/grep.c
> @@ -293,7 +293,10 @@ static int wait_all(void)
>  static int grep_cmd_config(const char *var, const char *value, void *cb)
>  {
>  	int st = grep_config(var, value, cb);
> -	if (git_color_default_config(var, value, NULL) < 0)
> +
> +	if (git_color_config(var, value, cb) < 0)
> +		st = -1;
> +	else if (git_default_config(var, value, cb) < 0)
>  		st = -1;
>  
>  	if (!strcmp(var, "grep.threads")) {
> diff --git a/builtin/show-branch.c b/builtin/show-branch.c
> index 7ef4a642c17..a2461270d4b 100644
> --- a/builtin/show-branch.c
> +++ b/builtin/show-branch.c
> @@ -579,7 +579,10 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
>  		return 0;
>  	}
>  
> -	return git_color_default_config(var, value, cb);
> +	if (git_color_config(var, value, cb) < 0)
> +		return -1;
> +
> +	return git_default_config(var, value, cb);
>  }
>  
>  static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
> diff --git a/builtin/tag.c b/builtin/tag.c
> index 49b64c7a288..1acf5f7a59f 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -209,7 +209,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
>  
>  	if (starts_with(var, "column."))
>  		return git_column_config(var, value, "tag", &colopts);
> -	return git_color_default_config(var, value, cb);
> +
> +	if (git_color_config(var, value, cb) < 0)
> +		return -1;
> +
> +	return git_default_config(var, value, cb);
>  }
>  
>  static void write_tag_body(int fd, const struct object_id *oid)
> diff --git a/color.c b/color.c
> index 83abb11eda0..b24b19566b9 100644
> --- a/color.c
> +++ b/color.c
> @@ -430,14 +430,6 @@ int git_color_config(const char *var, const char *value, void *cb UNUSED)
>  	return 0;
>  }
>  
> -int git_color_default_config(const char *var, const char *value, void *cb)
> -{
> -	if (git_color_config(var, value, cb) < 0)
> -		return -1;
> -
> -	return git_default_config(var, value, cb);
> -}
> -
>  void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
>  {
>  	if (*color)
> diff --git a/color.h b/color.h
> index cfc8f841b23..bb28343be21 100644
> --- a/color.h
> +++ b/color.h
> @@ -88,12 +88,8 @@ extern const int column_colors_ansi_max;
>   */
>  extern int color_stdout_is_tty;
>  
> -/*
> - * Use the first one if you need only color config; the second is a convenience
> - * if you are just going to change to git_default_config, too.
> - */
> +/* Parse color config. */
>  int git_color_config(const char *var, const char *value, void *cb);
> -int git_color_default_config(const char *var, const char *value, void *cb);
>  
>  /*
>   * Parse a config option, which can be a boolean or one of

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 02/12] urlmatch.h: use config_fn_t type
  2023-06-20 19:43     ` [PATCH v3 02/12] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
@ 2023-06-20 21:02       ` Junio C Hamano
  0 siblings, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-20 21:02 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Glen Choo <chooglen@google.com>
>
> These are actually used as config callbacks, so use the typedef-ed type
> and make future refactors easier.
>
> Signed-off-by: Glen Choo <chooglen@google.com>
> ---
>  urlmatch.h | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/urlmatch.h b/urlmatch.h
> index 9f40b00bfb8..bee374a642c 100644
> --- a/urlmatch.h
> +++ b/urlmatch.h
> @@ -2,6 +2,7 @@
>  #define URL_MATCH_H
>  
>  #include "string-list.h"
> +#include "config.h"
>  
>  struct url_info {
>  	/* normalized url on success, must be freed, otherwise NULL */
> @@ -48,8 +49,8 @@ struct urlmatch_config {
>  	const char *key;
>  
>  	void *cb;
> -	int (*collect_fn)(const char *var, const char *value, void *cb);
> -	int (*cascade_fn)(const char *var, const char *value, void *cb);
> +	config_fn_t collect_fn;
> +	config_fn_t cascade_fn;

Makes sense.

>  	/*
>  	 * Compare the two matches, the one just discovered and the existing
>  	 * best match and return a negative value if the found item is to be

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 04/12] config.c: pass ctx in configsets
  2023-06-20 19:43     ` [PATCH v3 04/12] config.c: pass ctx in configsets Glen Choo via GitGitGadget
@ 2023-06-20 21:19       ` Junio C Hamano
  0 siblings, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-20 21:19 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:

>  static int config_read_push_default(const char *key, const char *value,
> -	const struct config_context *ctx UNUSED, void *cb)
> +	const struct config_context *ctx, void *cb)
>  {
> +	const struct key_value_info *kvi = ctx->kvi;
> +
>  	struct push_default_info* info = cb;
>  	if (strcmp(key, "remote.pushdefault") ||
>  	    !value || strcmp(value, info->old_name))
>  		return 0;
>  
> -	info->scope = current_config_scope();
> +	info->scope = kvi->scope;
>  	strbuf_reset(&info->origin);
> -	strbuf_addstr(&info->origin, current_config_name());
> -	info->linenr = current_config_line();
> +	strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
> +	info->linenr = kvi->linenr;

Yay!

It is very pleasing to see these current_*() functions made
unnecessary.  This step only allows us to remove the _line() but not
yet _scope(), but as long as we lose them at the end, going one step
at a time is perfectly fine and readable.

> +const char *current_config_origin_type(void)
> +{
> +	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
> +
> +	if (reader_origin_type(&the_reader, &type))
> +		BUG("current_config_origin_type called outside config callback");
> +
> +	return config_origin_type_name(type);
> +}

We still rely on the fact that the_reader is a global singleton, but
that is of course OK in this early part of the series.

Looking nice.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 06/12] builtin/config.c: test misuse of format_config()
  2023-06-20 19:43     ` [PATCH v3 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
@ 2023-06-20 21:35       ` Junio C Hamano
  2023-06-20 23:06         ` Glen Choo
  2023-06-23 20:32       ` Jonathan Tan
  1 sibling, 1 reply; 115+ messages in thread
From: Junio C Hamano @ 2023-06-20 21:35 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Glen Choo <chooglen@google.com>
>
> current_config_*() functions aren't meant to be called outside of
> config callbacks because they read state that is only set when iterating
> through config.

Interesting.  And config_context is about expressing the state of
the iteration, so presumably some of these callers that do not have
config_context to pass (because they happen outside iterations) will
now (can|have to) pass NULL or something to say "I am asking format
but from outside any iteration" or something?

> +test_expect_success 'urlmatch with --show-scope' '
> +	cat >.git/config <<-\EOF &&
> +	[http "https://weak.example.com"]
> +		sslVerify = false
> +		cookieFile = /tmp/cookie.txt
> +	EOF
> +
> +	cat >expect <<-EOF &&
> +	unknown	http.cookiefile /tmp/cookie.txt
> +	unknown	http.sslverify false
> +	EOF
> +	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
> +	test_cmp expect actual
> +'
> +
>  test_expect_success 'urlmatch favors more specific URLs' '
>  	cat >.git/config <<-\EOF &&
>  	[http "https://example.com/"]
> @@ -2055,6 +2070,10 @@ test_expect_success '--show-origin blob ref' '
>  	test_cmp expect output
>  '
>  
> +test_expect_success '--show-origin with --default' '
> +	test_must_fail git config --show-origin --default foo some.key
> +'
> +
>  test_expect_success '--show-scope with --list' '
>  	cat >expect <<-EOF &&
>  	global	user.global=true
> @@ -2123,6 +2142,12 @@ test_expect_success '--show-scope with --show-origin' '
>  	test_cmp expect output
>  '
>  
> +test_expect_success '--show-scope with --default' '
> +	git config --show-scope --default foo some.key >actual &&
> +	echo "unknown	foo" >expect &&
> +	test_cmp expect actual
> +'
> +
>  test_expect_success 'override global and system config' '
>  	test_when_finished rm -f \"\$HOME\"/.gitconfig &&
>  	cat >"$HOME"/.gitconfig <<-EOF &&

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 12/12] config: pass source to config_parser_event_fn_t
  2023-06-20 19:43     ` [PATCH v3 12/12] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
@ 2023-06-20 21:46       ` Junio C Hamano
  0 siblings, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-20 21:46 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Glen Choo <chooglen@google.com>
>
> ..so that the callback can use a "struct config_source" parameter
> instead of "config_reader.source". "struct config_source" is internal to
> config.c, so we are adding a pointer to a struct defined in config.c
> into a public function signature defined in config.h, but this is okay
> because this function has only ever been (and probably ever will be)
> used internally by config.c.
>
> As a result, the_reader isn't used anywhere, so "struct config_reader"
> is obsolete (it was only intended to be used with the_reader). Remove
> them.
>
> Signed-off-by: Glen Choo <chooglen@google.com>
> ---
>  config.c | 77 ++++++++++----------------------------------------------
>  config.h |  6 +++++
>  2 files changed, 19 insertions(+), 64 deletions(-)

Together with the removal of current_* functions in earlier steps,
getting rid of the_reader interface makes the config API more easily
reused in other contexts, I guess.  Nicely done.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 06/12] builtin/config.c: test misuse of format_config()
  2023-06-20 21:35       ` Junio C Hamano
@ 2023-06-20 23:06         ` Glen Choo
  0 siblings, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-20 23:06 UTC (permalink / raw)
  To: Junio C Hamano, Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood

Junio C Hamano <gitster@pobox.com> writes:

>> current_config_*() functions aren't meant to be called outside of
>> config callbacks because they read state that is only set when iterating
>> through config.
>
> Interesting.  And config_context is about expressing the state of
> the iteration, so presumably some of these callers that do not have
> config_context to pass (because they happen outside iterations) will
> now (can|have to) pass NULL or something to say "I am asking format
> but from outside any iteration" or something?

Yup, they now have to pass a default value for config_context. For .kvi,
it should be what's given by KVI_INIT (the next patch has an example).

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 00/12] config: remove global state from config iteration
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (11 preceding siblings ...)
  2023-06-20 19:43     ` [PATCH v3 12/12] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
@ 2023-06-21 21:46     ` Junio C Hamano
  2023-06-21 23:06       ` Glen Choo
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
  13 siblings, 1 reply; 115+ messages in thread
From: Junio C Hamano @ 2023-06-21 21:46 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:

> Junio: I rebased this onto a newer "master". I assume this is a noop for
> you - I noticed that the RFC versions weren't applied anyway (good).

It would be a noop only if I do not queue this iteration.

I've reviewed them in its current shape, but it seems to cause too
many conflicts even when merged to 'next', let alone 'seen', with
interactions with topics in flight:

 * ds/add-i-color-configuration-fix (easy)
 * ps/fetch-cleanups (easy but messy)
 * vd/worktree-config-is-per-repository (moderately messy)

some of which may have graduated to 'master' in the meantime, so it
might not be a bad idea to rebase on a more recent 'master' after
you collect and adjust for review comments on v3.

Thanks.


^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 00/12] config: remove global state from config iteration
  2023-06-21 21:46     ` [PATCH v3 00/12] config: remove global state from config iteration Junio C Hamano
@ 2023-06-21 23:06       ` Glen Choo
  2023-06-23 21:02         ` Jonathan Tan
  0 siblings, 1 reply; 115+ messages in thread
From: Glen Choo @ 2023-06-21 23:06 UTC (permalink / raw)
  To: Junio C Hamano, Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood

Junio C Hamano <gitster@pobox.com> writes:

> I've reviewed them in its current shape, but it seems to cause too
> many conflicts even when merged to 'next', let alone 'seen', with
> interactions with topics in flight:
>
>  * ds/add-i-color-configuration-fix (easy)
>  * ps/fetch-cleanups (easy but messy)
>  * vd/worktree-config-is-per-repository (moderately messy)

Ah, sorry. I ran some trial merges against these before I sent out v3,
but I forgot as I sent this out. Not queueing this version sounds fine.

> some of which may have graduated to 'master' in the meantime, so it
> might not be a bad idea to rebase on a more recent 'master' after
> you collect and adjust for review comments on v3.

Sounds good. I suppose it would also be worthwhile to base it on
conflicting topics queued for 'next'.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 06/12] builtin/config.c: test misuse of format_config()
  2023-06-20 19:43     ` [PATCH v3 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
  2023-06-20 21:35       ` Junio C Hamano
@ 2023-06-23 20:32       ` Jonathan Tan
  2023-06-24  1:31         ` Jeff King
  2023-06-28 17:28         ` Glen Choo
  1 sibling, 2 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-23 20:32 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> +test_expect_success '--show-origin with --default' '
> +	test_must_fail git config --show-origin --default foo some.key
> +'

On my machine, this fails with

  BUG: config.c:4035: current_config_origin_type called outside config callback
  /usr/local/google/home/jonathantanmy/git/t/test-lib-functions.sh: line 1067: 3255109 Aborted                 "$@" 2>&7
  test_must_fail: died by signal 6: git config --show-origin --default foo some.key

(So it indeed fails, as expected, but test_must_fail seems to not like
the exit code.)

Not sure if we need to be more precise with the exit code we're testing
for.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 07/12] config.c: pass ctx with CLI config
  2023-06-20 19:43     ` [PATCH v3 07/12] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
@ 2023-06-23 20:35       ` Jonathan Tan
  2023-06-23 21:41         ` Glen Choo
  0 siblings, 1 reply; 115+ messages in thread
From: Jonathan Tan @ 2023-06-23 20:35 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> @@ -2465,16 +2488,10 @@ 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) {
> -		kvi_from_source(reader->source, current_config_scope(), kv_info);
> +		kvi_from_source(reader->source, kvi_p->scope, kv_info);
>  	} 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;
> +		kvi_from_param(kv_info);
>  	}
>  	si->util = kv_info;
>  

Any reason to remove the "if (!reader->source)" guard? I don't think
this patch does anything to ensure that reader is present. We can
probably remove this once "reader" is removed.

The rest of the patch looks fine (and all patches prior look fine too.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 08/12] trace2: plumb config kvi
  2023-06-20 19:43     ` [PATCH v3 08/12] trace2: plumb config kvi Glen Choo via GitGitGadget
@ 2023-06-23 20:40       ` Jonathan Tan
  0 siblings, 0 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-23 20:40 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> @@ -125,19 +115,9 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
>  static inline void config_reader_set_kvi(struct config_reader *reader,
>  					 struct key_value_info *kvi)
>  {
> -	if (kvi && (reader->source || reader->parsing_scope))
> -		BUG("kvi should not be set while parsing a config source");
>  	reader->config_kvi = kvi;
>  }
>  

We're removing this check now because reader->parsing_scope is
removed in this patch. Makes sense (no reason to just remove the
reader->parsing_scope first and then remove the whole check later).

Rest of the patch looks good too.
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 10/12] config.c: remove config_reader from configsets
  2023-06-20 19:43     ` [PATCH v3 10/12] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
@ 2023-06-23 20:57       ` Jonathan Tan
  2023-06-23 21:33         ` Junio C Hamano
  0 siblings, 1 reply; 115+ messages in thread
From: Jonathan Tan @ 2023-06-23 20:57 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> @@ -2429,11 +2427,7 @@ static int configset_add_value(const struct key_value_info *kvi_p,
>  	l_item->e = e;
>  	l_item->value_index = e->value_list.nr - 1;
>  
> -	if (reader->source->name) {
> -		kvi_from_source(reader->source, kvi_p->scope, kv_info);
> -	} else {
> -		kvi_from_param(kv_info);
> -	}
> +	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
>  	si->util = kv_info;
>  
>  	return 0;

Ah, I remember seeing this memcpy from the previous round, but forgot to
comment on it (I only commented on another instance, [1]).

Other than that, up to here looks good.

[1] https://lore.kernel.org/git/20230601233550.429921-1-jonathantanmy@google.com/
 

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 00/12] config: remove global state from config iteration
  2023-06-21 23:06       ` Glen Choo
@ 2023-06-23 21:02         ` Jonathan Tan
  2023-06-23 21:33           ` Junio C Hamano
  2023-06-23 21:45           ` Glen Choo
  0 siblings, 2 replies; 115+ messages in thread
From: Jonathan Tan @ 2023-06-23 21:02 UTC (permalink / raw)
  To: Glen Choo
  Cc: Jonathan Tan, Junio C Hamano, Glen Choo via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Emily Shaffer,
	Phillip Wood

Glen Choo <chooglen@google.com> writes:
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > I've reviewed them in its current shape, but it seems to cause too
> > many conflicts even when merged to 'next', let alone 'seen', with
> > interactions with topics in flight:
> >
> >  * ds/add-i-color-configuration-fix (easy)
> >  * ps/fetch-cleanups (easy but messy)
> >  * vd/worktree-config-is-per-repository (moderately messy)
> 
> Ah, sorry. I ran some trial merges against these before I sent out v3,
> but I forgot as I sent this out. Not queueing this version sounds fine.
> 
> > some of which may have graduated to 'master' in the meantime, so it
> > might not be a bad idea to rebase on a more recent 'master' after
> > you collect and adjust for review comments on v3.
> 
> Sounds good. I suppose it would also be worthwhile to base it on
> conflicting topics queued for 'next'.

Glen, if you can, rebase the patches *before* updating them based on my
comments so that it's easier for reviewers to see if my comments have
been addressed. (My comments are minor, so they should still be relevant
even after rebasing.)

Overall the patch set looks good aside from some minor comments that
I've given as replies to the patches.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 10/12] config.c: remove config_reader from configsets
  2023-06-23 20:57       ` Jonathan Tan
@ 2023-06-23 21:33         ` Junio C Hamano
  0 siblings, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-23 21:33 UTC (permalink / raw)
  To: Jonathan Tan
  Cc: Glen Choo via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Emily Shaffer,
	Phillip Wood, Glen Choo

Jonathan Tan <jonathantanmy@google.com> writes:

> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> @@ -2429,11 +2427,7 @@ static int configset_add_value(const struct key_value_info *kvi_p,
>>  	l_item->e = e;
>>  	l_item->value_index = e->value_list.nr - 1;
>>  
>> -	if (reader->source->name) {
>> -		kvi_from_source(reader->source, kvi_p->scope, kv_info);
>> -	} else {
>> -		kvi_from_param(kv_info);
>> -	}
>> +	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
>>  	si->util = kv_info;
>>  
>>  	return 0;
>
> Ah, I remember seeing this memcpy from the previous round, but forgot to
> comment on it (I only commented on another instance, [1]).

Yeah, I noticed this and recalled seeing a comment on structure
assignment.  As both are pointers,

    *kv_info = *kvi_p;

would sufficiently stand out to notify us that we are doing a
not-trivial assignment here, and would avoid (slight) risks of typos
by type checking.

> Other than that, up to here looks good.
>
> [1] https://lore.kernel.org/git/20230601233550.429921-1-jonathantanmy@google.com/
>  

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 00/12] config: remove global state from config iteration
  2023-06-23 21:02         ` Jonathan Tan
@ 2023-06-23 21:33           ` Junio C Hamano
  2023-06-23 21:45           ` Glen Choo
  1 sibling, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-23 21:33 UTC (permalink / raw)
  To: Jonathan Tan
  Cc: Glen Choo, Glen Choo via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Emily Shaffer,
	Phillip Wood

Jonathan Tan <jonathantanmy@google.com> writes:

> Glen Choo <chooglen@google.com> writes:
>> Junio C Hamano <gitster@pobox.com> writes:
>> 
>> > I've reviewed them in its current shape, but it seems to cause too
>> > many conflicts even when merged to 'next', let alone 'seen', with
>> > interactions with topics in flight:
>> >
>> >  * ds/add-i-color-configuration-fix (easy)
>> >  * ps/fetch-cleanups (easy but messy)
>> >  * vd/worktree-config-is-per-repository (moderately messy)
>> 
>> Ah, sorry. I ran some trial merges against these before I sent out v3,
>> but I forgot as I sent this out. Not queueing this version sounds fine.
>> 
>> > some of which may have graduated to 'master' in the meantime, so it
>> > might not be a bad idea to rebase on a more recent 'master' after
>> > you collect and adjust for review comments on v3.
>> 
>> Sounds good. I suppose it would also be worthwhile to base it on
>> conflicting topics queued for 'next'.
>
> Glen, if you can, rebase the patches *before* updating them based on my
> comments so that it's easier for reviewers to see if my comments have
> been addressed. (My comments are minor, so they should still be relevant
> even after rebasing.)
>
> Overall the patch set looks good aside from some minor comments that
> I've given as replies to the patches.

Thanks for a quick review (and thank you, Glen, for working on it).


^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 07/12] config.c: pass ctx with CLI config
  2023-06-23 20:35       ` Jonathan Tan
@ 2023-06-23 21:41         ` Glen Choo
  0 siblings, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-23 21:41 UTC (permalink / raw)
  To: Jonathan Tan, Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood

Jonathan Tan <jonathantanmy@google.com> writes:

> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> @@ -2465,16 +2488,10 @@ 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) {
>> -		kvi_from_source(reader->source, current_config_scope(), kv_info);
>> +		kvi_from_source(reader->source, kvi_p->scope, kv_info);
>>  	} 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;
>> +		kvi_from_param(kv_info);
>>  	}
>>  	si->util = kv_info;
>>  
>
> Any reason to remove the "if (!reader->source)" guard? I don't think
> this patch does anything to ensure that reader is present. We can
> probably remove this once "reader" is removed.

Hm you're right, this shouldn't be removed here. I think this a leftover
from an ancient version where I dropped references to "reader".

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 00/12] config: remove global state from config iteration
  2023-06-23 21:02         ` Jonathan Tan
  2023-06-23 21:33           ` Junio C Hamano
@ 2023-06-23 21:45           ` Glen Choo
  1 sibling, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-23 21:45 UTC (permalink / raw)
  To: Jonathan Tan
  Cc: Jonathan Tan, Junio C Hamano, Glen Choo via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Emily Shaffer,
	Phillip Wood

Jonathan Tan <jonathantanmy@google.com> writes:

>> Sounds good. I suppose it would also be worthwhile to base it on
>> conflicting topics queued for 'next'.
>
> Glen, if you can, rebase the patches *before* updating them based on my
> comments so that it's easier for reviewers to see if my comments have
> been addressed. (My comments are minor, so they should still be relevant
> even after rebasing.)

Good idea. Fortunately, I already have that prepared, so there's not
much extra work for me.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 06/12] builtin/config.c: test misuse of format_config()
  2023-06-23 20:32       ` Jonathan Tan
@ 2023-06-24  1:31         ` Jeff King
  2023-06-28 17:28         ` Glen Choo
  1 sibling, 0 replies; 115+ messages in thread
From: Jeff King @ 2023-06-24  1:31 UTC (permalink / raw)
  To: Jonathan Tan
  Cc: Glen Choo via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Emily Shaffer,
	Phillip Wood, Glen Choo

On Fri, Jun 23, 2023 at 01:32:19PM -0700, Jonathan Tan wrote:

> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> > +test_expect_success '--show-origin with --default' '
> > +	test_must_fail git config --show-origin --default foo some.key
> > +'
> 
> On my machine, this fails with
> 
>   BUG: config.c:4035: current_config_origin_type called outside config callback
>   /usr/local/google/home/jonathantanmy/git/t/test-lib-functions.sh: line 1067: 3255109 Aborted                 "$@" 2>&7
>   test_must_fail: died by signal 6: git config --show-origin --default foo some.key
> 
> (So it indeed fails, as expected, but test_must_fail seems to not like
> the exit code.)
> 
> Not sure if we need to be more precise with the exit code we're testing
> for.

That is test_must_fail operating as intended. We should _never_ hit a
BUG() call, no matter what garbage we get from the user or the disk. The
resolution is generally one of:

  - there really is a bug, so we should fix it

  - the invocation is garbage and expected to fail, but we should catch
    it sooner and give a nice error message, rather than getting as far
    as a BUG() call

But I didn't look at the patches to know which case this is.

-Peff

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v4 00/12] config: remove global state from config iteration
  2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
                       ` (12 preceding siblings ...)
  2023-06-21 21:46     ` [PATCH v3 00/12] config: remove global state from config iteration Junio C Hamano
@ 2023-06-26 18:11     ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
                         ` (13 more replies)
  13 siblings, 14 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo

Thanks for the careful reread of the previous round and the helpful suggestions.
This version is _mostly_ just the previous one rebased onto a newer 'master'
with the conflicting topics applied, per Jonathan's and Junio's suggestions, and
I plan to address Jonathan's comments in the next version.

Junio: There is now a minor conflict with the bugfix I sent in [1] (the
resolution is to just pick this series). It seemed small enough that I didn't
think it was worth basing this series on top of that, but let me know if you
feel otherwise. If both series happen to go through the same release cycle, we
could also drop that one-patch.

I also tested these patches on top of 'seen', and the only conflict I found was
with en/header-split-cache-h-part-3, where ll-merge.c got renamed to merge-ll.c,
so we need to apply the config refactor there (should work with .cocci).

= Changes since v3

- Rebase onto newer 'master'
- Move the 'remove UNUSED from tr2_cfg_cb' hunk from 9/12 -> 8/12. It should
  have been there all along; v3 8/12 didn't build at all.

[1] https://lore.kernel.org/git/pull.1535.git.git.1687801297404.gitgitgadget@gmail.com/


Glen Choo (12):
  config: inline git_color_default_config
  urlmatch.h: use config_fn_t type
  config: add ctx arg to config_fn_t
  config.c: pass ctx in configsets
  config: pass ctx with config files
  builtin/config.c: test misuse of format_config()
  config.c: pass ctx with CLI config
  trace2: plumb config kvi
  config: pass kvi to die_bad_number()
  config.c: remove config_reader from configsets
  config: add kvi.path, use it to evaluate includes
  config: pass source to config_parser_event_fn_t

 alias.c                                       |   3 +-
 archive-tar.c                                 |   5 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   8 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   8 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |  12 +-
 builtin/clean.c                               |   9 +-
 builtin/clone.c                               |  11 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   3 +-
 builtin/commit.c                              |  20 +-
 builtin/config.c                              |  72 ++-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |  13 +-
 builtin/fsmonitor--daemon.c                   |  11 +-
 builtin/grep.c                                |  12 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   9 +-
 builtin/log.c                                 |  12 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |  19 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |  15 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |  15 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   8 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   3 +-
 builtin/tag.c                                 |   9 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   9 +-
 color.c                                       |   8 -
 color.h                                       |   6 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      | 552 +++++++-----------
 config.h                                      |  80 ++-
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_ctx.pending.cocci    | 144 +++++
 contrib/coccinelle/git_config_number.cocci    |  27 +
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   4 +-
 diff.c                                        |  19 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   7 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |  12 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   7 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |   9 +-
 http.c                                        |  15 +-
 ident.c                                       |   4 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   7 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   1 +
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   4 +-
 notes.c                                       |   4 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   8 +-
 revision.c                                    |   4 +-
 scalar.c                                      |   4 +-
 sequencer.c                                   |  29 +-
 setup.c                                       |  18 +-
 submodule-config.c                            |  31 +-
 submodule-config.h                            |   3 +-
 t/helper/test-config.c                        |  24 +-
 t/helper/test-userdiff.c                      |   4 +-
 t/t1300-config.sh                             |  27 +
 trace2.c                                      |   4 +-
 trace2.h                                      |   3 +-
 trace2/tr2_cfg.c                              |  16 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trace2/tr2_tgt.h                              |   4 +-
 trace2/tr2_tgt_event.c                        |   4 +-
 trace2/tr2_tgt_normal.c                       |   4 +-
 trace2/tr2_tgt_perf.c                         |   4 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |  18 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   8 +-
 worktree.c                                    |   2 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   4 +-
 103 files changed, 960 insertions(+), 638 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci
 create mode 100644 contrib/coccinelle/git_config_number.cocci


base-commit: 6ff334181cfb6485d3ba50843038209a2a253907
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1497%2Fchooglen%2Fconfig%2Fno-global-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1497/chooglen/config/no-global-v4
Pull-Request: https://github.com/git/git/pull/1497

Range-diff vs v3:

  1:  26109b65142 !  1:  7bfffb454c5 config: inline git_color_default_config
     @@ Commit message
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     + ## builtin/add.c ##
     +@@ builtin/add.c: static int add_config(const char *var, const char *value, void *cb)
     + 		return 0;
     + 	}
     + 
     +-	return git_color_default_config(var, value, cb);
     ++	if (git_color_config(var, value, cb) < 0)
     ++		return -1;
     ++
     ++	return git_default_config(var, value, cb);
     + }
     + 
     + static const char embedded_advice[] = N_(
     +
       ## builtin/branch.c ##
      @@ builtin/branch.c: static int git_branch_config(const char *var, const char *value, void *cb)
       		return 0;
  2:  1aeffcb1395 =  2:  739c519ce62 urlmatch.h: use config_fn_t type
  3:  d3eb439aad2 !  3:  a9a0a50f32a config: add ctx arg to config_fn_t
     @@ builtin/add.c: static struct option builtin_add_options[] = {
       	if (!strcmp(var, "add.ignoreerrors") ||
       	    !strcmp(var, "add.ignore-errors")) {
      @@ builtin/add.c: static int add_config(const char *var, const char *value, void *cb)
     - 		return 0;
     - 	}
     + 	if (git_color_config(var, value, cb) < 0)
     + 		return -1;
       
      -	return git_default_config(var, value, cb);
      +	return git_default_config(var, value, ctx, cb);
     @@ builtin/difftool.c: static const char *const builtin_difftool_usage[] = {
      
       ## builtin/fetch.c ##
      @@ builtin/fetch.c: struct fetch_config {
     - 	enum display_format display_format;
     + 	int submodule_fetch_jobs;
       };
       
      -static int git_fetch_config(const char *k, const char *v, void *cb)
  4:  c5051ddc10d =  4:  39b2e291f86 config.c: pass ctx in configsets
  5:  595e7d2e163 !  5:  bfc6d2833c5 config: pass ctx with config files
     @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
      +							 CONFIG_SCOPE_LOCAL, NULL);
       
       	config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
     - 	if (!opts->ignore_worktree && repository_format_worktree_config) {
     - 		char *path = git_pathdup("config.worktree");
     - 		if (!access_or_die(path, R_OK, 0))
     --			ret += git_config_from_file(fn, path, data);
     -+			ret += git_config_from_file_with_options(fn, path, data,
     + 	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);
     - 		free(path);
       	}
       
     + 	config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
      @@ config.c: int config_with_options(config_fn_t fn, void *data,
       	 * regular lookup sequence.
       	 */
     @@ config.c: int config_with_options(config_fn_t fn, void *data,
      +							data, config_source->scope,
      +							NULL);
       	} else if (config_source && config_source->blob) {
     - 		struct repository *repo = config_source->repo ?
     - 			config_source->repo : the_repository;
       		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, fn, data);
     + 		ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
       	}
      @@ config.c: static int configset_add_value(struct config_reader *reader,
       	if (!reader->source)
  6:  a2a891a069f =  6:  897bdc759b5 builtin/config.c: test misuse of format_config()
  7:  1fb1708bbd9 !  7:  33e4437737d config.c: pass ctx with CLI config
     @@ builtin/config.c: static int collect_config(const char *key_, const char *value_
       
       static int get_value(const char *key_, const char *regex_, unsigned flags)
      @@ builtin/config.c: static int get_value(const char *key_, const char *regex_, unsigned flags)
     - 			    &given_config_source, &config_options);
     + 			    &config_options);
       
       	if (!values.nr && default_value) {
      +		struct key_value_info kvi = KVI_INIT;
  8:  66572df7beb !  8:  9bd5f60282c trace2: plumb config kvi
     @@ config.c: static void populate_remote_urls(struct config_include_data *inc)
      -
       	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, &opts);
     + 	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,
      @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     - 	char *xdg_config = NULL;
       	char *user_config = NULL;
       	char *repo_config;
     + 	char *worktree_config;
      -	enum config_scope prev_parsing_scope = reader->parsing_scope;
       
     - 	if (opts->commondir)
     - 		repo_config = mkpathdup("%s/config", opts->commondir);
     + 	/*
     + 	 * Ensure that either:
      @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     - 	else
     - 		repo_config = NULL;
     + 		worktree_config = NULL;
     + 	}
       
      -	config_reader_set_scope(reader, CONFIG_SCOPE_SYSTEM);
       	if (git_config_system() && system_config &&
     @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
       							 CONFIG_SCOPE_LOCAL, NULL);
       
      -	config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
     - 	if (!opts->ignore_worktree && repository_format_worktree_config) {
     - 		char *path = git_pathdup("config.worktree");
     - 		if (!access_or_die(path, R_OK, 0))
     + 	if (!opts->ignore_worktree && worktree_config &&
     + 	    repo && repo->repository_format_worktree_config &&
     + 	    !access_or_die(worktree_config, R_OK, 0)) {
      @@ config.c: static int do_git_config_sequence(struct config_reader *reader,
     - 		free(path);
     + 								 NULL);
       	}
       
      -	config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
     @@ trace2.h: void trace2_thread_exit_fl(const char *file, int line);
       	trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
      
       ## trace2/tr2_cfg.c ##
     +@@ trace2/tr2_cfg.c: struct tr2_cfg_data {
     +  * See if the given config key matches any of our patterns of interest.
     +  */
     + static int tr2_cfg_cb(const char *key, const char *value,
     +-		      const struct config_context *ctx UNUSED, void *d)
     ++		      const struct config_context *ctx, void *d)
     + {
     + 	struct strbuf **s;
     + 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
      @@ trace2/tr2_cfg.c: static int tr2_cfg_cb(const char *key, const char *value,
       		struct strbuf *buf = *s;
       		int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
  9:  123e19dda4a !  9:  114723ee4a7 config: pass kvi to die_bad_number()
     @@ builtin/fetch.c: static int git_fetch_config(const char *k, const char *v,
       	}
       
       	if (!strcmp(k, "submodule.fetchjobs")) {
     --		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
     -+		submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v, ctx->kvi);
     +-		fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v);
     ++		fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v, ctx->kvi);
       		return 0;
       	} else if (!strcmp(k, "fetch.recursesubmodules")) {
     - 		recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
     + 		fetch_config->recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
      @@ builtin/fetch.c: static int git_fetch_config(const char *k, const char *v,
       	}
       
       	if (!strcmp(k, "fetch.parallel")) {
     --		fetch_parallel_config = git_config_int(k, v);
     -+		fetch_parallel_config = git_config_int(k, v, ctx->kvi);
     - 		if (fetch_parallel_config < 0)
     +-		fetch_config->parallel = git_config_int(k, v);
     ++		fetch_config->parallel = git_config_int(k, v, ctx->kvi);
     + 		if (fetch_config->parallel < 0)
       			die(_("fetch.parallel cannot be negative"));
     - 		if (!fetch_parallel_config)
     + 		if (!fetch_config->parallel)
      
       ## builtin/fsmonitor--daemon.c ##
      @@ builtin/fsmonitor--daemon.c: static int fsmonitor_config(const char *var, const char *value,
     @@ t/helper/test-config.c: int cmd__config(int argc, const char **argv)
       				printf("(NULL)\n");
       			else
      
     - ## trace2/tr2_cfg.c ##
     -@@ trace2/tr2_cfg.c: struct tr2_cfg_data {
     -  * See if the given config key matches any of our patterns of interest.
     -  */
     - static int tr2_cfg_cb(const char *key, const char *value,
     --		      const struct config_context *ctx UNUSED, void *d)
     -+		      const struct config_context *ctx, void *d)
     - {
     - 	struct strbuf **s;
     - 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
     -
       ## upload-pack.c ##
      @@ upload-pack.c: static int find_symref(const char *refname,
       }
 10:  8ec24b018e9 ! 10:  807057b6d7f config.c: remove config_reader from configsets
     @@ config.c: static void repo_read_config(struct repository *repo)
      -	data.config_set = repo->config;
      -	data.config_reader = &the_reader;
      -
     --	if (config_with_options(config_set_callback, &data, NULL, &opts) < 0)
     +-	if (config_with_options(config_set_callback, &data, NULL, repo, &opts) < 0)
      +	if (config_with_options(config_set_callback, repo->config, NULL,
     -+				&opts) < 0)
     ++				repo, &opts) < 0)
       		/*
       		 * config_with_options() normally returns only
       		 * zero, as most errors are fatal, and
     @@ config.c: static void read_protected_config(void)
       	git_configset_init(&protected_config);
      -	data.config_set = &protected_config;
      -	data.config_reader = &the_reader;
     --	config_with_options(config_set_callback, &data, NULL, &opts);
     -+	config_with_options(config_set_callback, &protected_config, NULL, &opts);
     +-	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)
 11:  8ae115cff88 ! 11:  3f0f84df972 config: add kvi.path, use it to evaluate includes
     @@ Commit message
      
       ## config.c ##
      @@ config.c: struct config_include_data {
     - 	void *data;
       	const struct config_options *opts;
       	struct git_config_source *config_source;
     + 	struct repository *repo;
      -	struct config_reader *config_reader;
       
       	/*
     @@ config.c: static void kvi_from_source(struct config_source *cs,
       
       static int git_parse_source(struct config_source *cs, config_fn_t fn,
      @@ config.c: int config_with_options(config_fn_t fn, void *data,
     - 		inc.data = data;
       		inc.opts = opts;
     + 		inc.repo = repo;
       		inc.config_source = config_source;
      -		inc.config_reader = &the_reader;
       		fn = git_config_include;
 12:  484d553cc7d ! 12:  fe2f154fe8b config: pass source to config_parser_event_fn_t
     @@ config.c: int git_config_system(void)
      -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)
       {
     - 	int ret = 0;
      @@ config.c: int config_with_options(config_fn_t fn, void *data,
       		ret = git_config_from_blob_ref(fn, repo, config_source->blob,
       					       data, config_source->scope);
       	} else {
     --		ret = do_git_config_sequence(&the_reader, opts, fn, data);
     -+		ret = do_git_config_sequence(opts, fn, data);
     +-		ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
     ++		ret = do_git_config_sequence(opts, repo, fn, data);
       	}
       
       	if (inc.remote_urls) {

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v4 01/12] config: inline git_color_default_config
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 02/12] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
                         ` (12 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

git_color_default_config() is a shorthand for calling two other config
callbacks. There are no other non-static functions that do this and it
will complicate our refactoring of config_fn_t so inline it instead.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/add.c         | 5 ++++-
 builtin/branch.c      | 5 ++++-
 builtin/clean.c       | 6 ++++--
 builtin/grep.c        | 5 ++++-
 builtin/show-branch.c | 5 ++++-
 builtin/tag.c         | 6 +++++-
 color.c               | 8 --------
 color.h               | 6 +-----
 8 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 6137e7b4ad7..e01efdfc50d 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -365,7 +365,10 @@ static int add_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char embedded_advice[] = N_(
diff --git a/builtin/branch.c b/builtin/branch.c
index 075e580d224..8337b9e71bb 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -117,7 +117,10 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/clean.c b/builtin/clean.c
index 78852d28cec..57e7f7cac64 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -130,8 +130,10 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	/* inspect the color.ui config variable and others */
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/grep.c b/builtin/grep.c
index b86c754defb..76cf999d310 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -293,7 +293,10 @@ static int wait_all(void)
 static int grep_cmd_config(const char *var, const char *value, void *cb)
 {
 	int st = grep_config(var, value, cb);
-	if (git_color_default_config(var, value, NULL) < 0)
+
+	if (git_color_config(var, value, cb) < 0)
+		st = -1;
+	else if (git_default_config(var, value, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 7ef4a642c17..a2461270d4b 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -579,7 +579,10 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/tag.c b/builtin/tag.c
index 49b64c7a288..1acf5f7a59f 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -209,7 +209,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 
 	if (starts_with(var, "column."))
 		return git_column_config(var, value, "tag", &colopts);
-	return git_color_default_config(var, value, cb);
+
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/color.c b/color.c
index 83abb11eda0..b24b19566b9 100644
--- a/color.c
+++ b/color.c
@@ -430,14 +430,6 @@ int git_color_config(const char *var, const char *value, void *cb UNUSED)
 	return 0;
 }
 
-int git_color_default_config(const char *var, const char *value, void *cb)
-{
-	if (git_color_config(var, value, cb) < 0)
-		return -1;
-
-	return git_default_config(var, value, cb);
-}
-
 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
 {
 	if (*color)
diff --git a/color.h b/color.h
index cfc8f841b23..bb28343be21 100644
--- a/color.h
+++ b/color.h
@@ -88,12 +88,8 @@ extern const int column_colors_ansi_max;
  */
 extern int color_stdout_is_tty;
 
-/*
- * Use the first one if you need only color config; the second is a convenience
- * if you are just going to change to git_default_config, too.
- */
+/* Parse color config. */
 int git_color_config(const char *var, const char *value, void *cb);
-int git_color_default_config(const char *var, const char *value, void *cb);
 
 /*
  * Parse a config option, which can be a boolean or one of
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 02/12] urlmatch.h: use config_fn_t type
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 03/12] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
                         ` (11 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

These are actually used as config callbacks, so use the typedef-ed type
and make future refactors easier.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 urlmatch.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/urlmatch.h b/urlmatch.h
index 9f40b00bfb8..bee374a642c 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -2,6 +2,7 @@
 #define URL_MATCH_H
 
 #include "string-list.h"
+#include "config.h"
 
 struct url_info {
 	/* normalized url on success, must be freed, otherwise NULL */
@@ -48,8 +49,8 @@ struct urlmatch_config {
 	const char *key;
 
 	void *cb;
-	int (*collect_fn)(const char *var, const char *value, void *cb);
-	int (*cascade_fn)(const char *var, const char *value, void *cb);
+	config_fn_t collect_fn;
+	config_fn_t cascade_fn;
 	/*
 	 * Compare the two matches, the one just discovered and the existing
 	 * best match and return a negative value if the found item is to be
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 03/12] config: add ctx arg to config_fn_t
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 02/12] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 04/12] config.c: pass ctx in configsets Glen Choo via GitGitGadget
                         ` (10 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Add a new "const struct config_context *ctx" arg to config_fn_t to hold
additional information about the config iteration operation.
config_context has a "struct key_value_info kvi" member that holds
metadata about the config source being read (e.g. what kind of config
source it is, the filename, etc). In this series, we're only interested
in .kvi, so we could have just used "struct key_value_info" as an arg,
but config_context makes it possible to add/adjust members in the future
without changing the config_fn_t signature. We could also consider other
ways of organizing the args (e.g. moving the config name and value into
config_context or key_value_info), but in my experiments, the
incremental benefit doesn't justify the added complexity (e.g. a
config_fn_t will sometimes invoke another config_fn_t but with a
different config value).

In subsequent commits, the .kvi member will replace the global "struct
config_reader" in config.c, making config iteration a global-free
operation. It requires much more work for the machinery to provide
meaningful values of .kvi, so for now, merely change the signature and
call sites, pass NULL as a placeholder value, and don't rely on the arg
in any meaningful way.

Most of the changes are performed by
contrib/coccinelle/config_fn_ctx.pending.cocci, which, for every
config_fn_t:

- Modifies the signature to accept "const struct config_context *ctx"
- Passes "ctx" to any inner config_fn_t, if needed
- Adds UNUSED attributes to "ctx", if needed

Most config_fn_t instances are easily identified by seeing if they are
called by the various config functions. Most of the remaining ones are
manually named in the .cocci patch. Manual cleanups are still needed,
but the majority of it is trivial; it's either adjusting config_fn_t
that the .cocci patch didn't catch, or adding forward declarations of
"struct config_context ctx" to make the signatures make sense.

The non-trivial changes are in cases where we are invoking a config_fn_t
outside of config machinery, and we now need to decide what value of
"ctx" to pass. These cases are:

- trace2/tr2_cfg.c:tr2_cfg_set_fl()

  This is indirectly called by git_config_set() so that the trace2
  machinery can notice the new config values and update its settings
  using the tr2 config parsing function, i.e. tr2_cfg_cb().

- builtin/checkout.c:checkout_main()

  This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
  This might be worth refactoring away in the future, since
  git_xmerge_config() can call git_default_config(), which can do much
  more than just parsing.

Handle them by creating a KVI_INIT macro that initializes "struct
key_value_info" to a reasonable default, and use that to construct the
"ctx" arg.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 alias.c                                       |   3 +-
 archive-tar.c                                 |   3 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   5 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   5 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |  12 +-
 builtin/clean.c                               |   5 +-
 builtin/clone.c                               |  11 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   1 +
 builtin/commit.c                              |  10 +-
 builtin/config.c                              |  10 +-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |   9 +-
 builtin/fsmonitor--daemon.c                   |   5 +-
 builtin/grep.c                                |   7 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   5 +-
 builtin/log.c                                 |  10 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |   5 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |   5 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |   7 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   5 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   1 +
 builtin/tag.c                                 |   5 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   8 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      |  38 +++--
 config.h                                      |  41 +++--
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_ctx.pending.cocci    | 144 ++++++++++++++++++
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   4 +-
 diff.c                                        |  10 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   5 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |   9 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   7 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |   7 +-
 http.c                                        |   5 +-
 ident.c                                       |   4 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   5 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   1 +
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   4 +-
 notes.c                                       |   4 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   3 +-
 revision.c                                    |   4 +-
 scalar.c                                      |   4 +-
 sequencer.c                                   |   9 +-
 setup.c                                       |  16 +-
 submodule-config.c                            |  17 ++-
 t/helper/test-config.c                        |  11 +-
 t/helper/test-userdiff.c                      |   4 +-
 trace2/tr2_cfg.c                              |   9 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |   8 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   3 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   4 +-
 91 files changed, 515 insertions(+), 181 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci

diff --git a/alias.c b/alias.c
index 54a1a23d2cf..910dd252a01 100644
--- a/alias.c
+++ b/alias.c
@@ -12,7 +12,8 @@ struct config_alias_data {
 	struct string_list *list;
 };
 
-static int config_alias_cb(const char *key, const char *value, void *d)
+static int config_alias_cb(const char *key, const char *value,
+			   const struct config_context *ctx UNUSED, void *d)
 {
 	struct config_alias_data *data = d;
 	const char *p;
diff --git a/archive-tar.c b/archive-tar.c
index 4cd81d8161e..ef06e516b1f 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -411,7 +411,8 @@ static int tar_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int git_tar_config(const char *var, const char *value, void *cb)
+static int git_tar_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
diff --git a/archive-zip.c b/archive-zip.c
index d0d065a312e..b6811951955 100644
--- a/archive-zip.c
+++ b/archive-zip.c
@@ -617,6 +617,7 @@ static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
 }
 
 static int archive_zip_config(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
 			      void *data UNUSED)
 {
 	return userdiff_config(var, value);
diff --git a/builtin/add.c b/builtin/add.c
index e01efdfc50d..1009ae13c13 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -357,7 +357,8 @@ static struct option builtin_add_options[] = {
 	OPT_END(),
 };
 
-static int add_config(const char *var, const char *value, void *cb)
+static int add_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "add.ignoreerrors") ||
 	    !strcmp(var, "add.ignore-errors")) {
@@ -368,7 +369,7 @@ static int add_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char embedded_advice[] = N_(
diff --git a/builtin/blame.c b/builtin/blame.c
index 2df6039a6e0..d0970a1ab13 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -694,7 +694,8 @@ static const char *add_prefix(const char *prefix, const char *path)
 	return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 }
 
-static int git_blame_config(const char *var, const char *value, void *cb)
+static int git_blame_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "blame.showroot")) {
 		show_root = git_config_bool(var, value);
@@ -767,7 +768,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
diff --git a/builtin/branch.c b/builtin/branch.c
index 8337b9e71bb..af6d2e75fb0 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -83,7 +83,8 @@ static unsigned int colopts;
 
 define_list_config_array(color_branch_slots);
 
-static int git_branch_config(const char *var, const char *value, void *cb)
+static int git_branch_config(const char *var, const char *value,
+			     const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -120,7 +121,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 7ff56d5a781..b3c41b54c8d 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -873,12 +873,13 @@ static int batch_objects(struct batch_options *opt)
 	return retval;
 }
 
-static int git_cat_file_config(const char *var, const char *value, void *cb)
+static int git_cat_file_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int batch_option_callback(const struct option *opt,
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 715eeb5048f..4e1f7dc26c2 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1186,7 +1186,8 @@ static int switch_branches(const struct checkout_opts *opts,
 	return ret || writeout_error;
 }
 
-static int git_checkout_config(const char *var, const char *value, void *cb)
+static int git_checkout_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	struct checkout_opts *opts = cb;
 
@@ -1202,7 +1203,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
 
-	return git_xmerge_config(var, value, NULL);
+	return git_xmerge_config(var, value, ctx, NULL);
 }
 
 static void setup_new_branch_info_and_source_tree(
@@ -1689,8 +1690,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 	}
 
 	if (opts->conflict_style) {
+		struct key_value_info kvi = KVI_INIT;
+		struct config_context ctx = {
+			.kvi = &kvi,
+		};
 		opts->merge = 1; /* implied */
-		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
+		git_xmerge_config("merge.conflictstyle", opts->conflict_style,
+				  &ctx, NULL);
 	}
 	if (opts->force) {
 		opts->discard_changes = 1;
diff --git a/builtin/clean.c b/builtin/clean.c
index 57e7f7cac64..5eff1b802a7 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -103,7 +103,8 @@ struct menu_stuff {
 
 define_list_config_array(color_interactive_slots);
 
-static int git_clean_config(const char *var, const char *value, void *cb)
+static int git_clean_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -133,7 +134,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/clone.c b/builtin/clone.c
index 15f9912b4ca..c0f6e067493 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -790,7 +790,8 @@ static int checkout(int submodule_progress, int filter_submodules)
 	return err;
 }
 
-static int git_clone_config(const char *k, const char *v, void *cb)
+static int git_clone_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "clone.defaultremotename")) {
 		free(remote_name);
@@ -801,17 +802,19 @@ static int git_clone_config(const char *k, const char *v, void *cb)
 	if (!strcmp(k, "clone.filtersubmodules"))
 		config_filter_submodules = git_config_bool(k, v);
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
-static int write_one_config(const char *key, const char *value, void *data)
+static int write_one_config(const char *key, const char *value,
+			    const struct config_context *ctx,
+			    void *data)
 {
 	/*
 	 * give git_clone_config a chance to write config values back to the
 	 * environment, since git_config_set_multivar_gently only deals with
 	 * config-file writes
 	 */
-	int apply_failed = git_clone_config(key, value, data);
+	int apply_failed = git_clone_config(key, value, ctx, data);
 	if (apply_failed)
 		return apply_failed;
 
diff --git a/builtin/column.c b/builtin/column.c
index de623a16c2d..4a6148ca479 100644
--- a/builtin/column.c
+++ b/builtin/column.c
@@ -13,7 +13,8 @@ static const char * const builtin_column_usage[] = {
 };
 static unsigned int colopts;
 
-static int column_config(const char *var, const char *value, void *cb)
+static int column_config(const char *var, const char *value,
+			 const struct config_context *ctx UNUSED, void *cb)
 {
 	return git_column_config(var, value, cb, &colopts);
 }
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index dd732b35348..1185c49239a 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -186,6 +186,7 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
+					 const struct config_context *ctx UNUSED,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
diff --git a/builtin/commit.c b/builtin/commit.c
index 9ab57ea1aaa..6a2b2503328 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1405,7 +1405,8 @@ static int parse_status_slot(const char *slot)
 	return LOOKUP_CONFIG(color_status_slots, slot);
 }
 
-static int git_status_config(const char *k, const char *v, void *cb)
+static int git_status_config(const char *k, const char *v,
+			     const struct config_context *ctx, void *cb)
 {
 	struct wt_status *s = cb;
 	const char *slot_name;
@@ -1490,7 +1491,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
 		s->detect_rename = git_config_rename(k, v);
 		return 0;
 	}
-	return git_diff_ui_config(k, v, NULL);
+	return git_diff_ui_config(k, v, ctx, NULL);
 }
 
 int cmd_status(int argc, const char **argv, const char *prefix)
@@ -1605,7 +1606,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int git_commit_config(const char *k, const char *v, void *cb)
+static int git_commit_config(const char *k, const char *v,
+			     const struct config_context *ctx, void *cb)
 {
 	struct wt_status *s = cb;
 
@@ -1627,7 +1629,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_status_config(k, v, s);
+	return git_status_config(k, v, ctx, s);
 }
 
 int cmd_commit(int argc, const char **argv, const char *prefix)
diff --git a/builtin/config.c b/builtin/config.c
index d40fddb042a..f4fccf99cb8 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -217,6 +217,7 @@ static void show_config_scope(struct strbuf *buf)
 }
 
 static int show_all_config(const char *key_, const char *value_,
+			   const struct config_context *ctx UNUSED,
 			   void *cb UNUSED)
 {
 	if (show_origin || show_scope) {
@@ -301,7 +302,8 @@ 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 UNUSED, void *cb)
 {
 	struct strbuf_list *values = cb;
 
@@ -470,6 +472,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)) {
@@ -503,6 +506,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))
@@ -561,7 +565,9 @@ struct urlmatch_current_candidate_value {
 	struct strbuf value;
 };
 
-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 UNUSED,
+			       void *cb)
 {
 	struct string_list *values = cb;
 	struct string_list_item *item = string_list_insert(values, var);
diff --git a/builtin/difftool.c b/builtin/difftool.c
index 0049342f5c0..f289530068b 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -40,14 +40,15 @@ static const char *const builtin_difftool_usage[] = {
 	NULL
 };
 
-static int difftool_config(const char *var, const char *value, void *cb)
+static int difftool_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "difftool.trustexitcode")) {
 		trust_exit_code = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int print_tool_help(void)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index e3871048cf6..a4aa0fbb8f5 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -110,7 +110,8 @@ struct fetch_config {
 	int submodule_fetch_jobs;
 };
 
-static int git_fetch_config(const char *k, const char *v, void *cb)
+static int git_fetch_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	struct fetch_config *fetch_config = cb;
 
@@ -164,7 +165,7 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
 			    "fetch.output", v);
 	}
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
@@ -1799,7 +1800,9 @@ struct remote_group_data {
 	struct string_list *list;
 };
 
-static int get_remote_group(const char *key, const char *value, void *priv)
+static int get_remote_group(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *priv)
 {
 	struct remote_group_data *g = priv;
 
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index f6dd9a784c1..91a776e2f17 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -37,7 +37,8 @@ static int fsmonitor__start_timeout_sec = 60;
 #define FSMONITOR__ANNOUNCE_STARTUP "fsmonitor.announcestartup"
 static int fsmonitor__announce_startup = 0;
 
-static int fsmonitor_config(const char *var, const char *value, void *cb)
+static int fsmonitor_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
 		int i = git_config_int(var, value);
@@ -67,7 +68,7 @@ static int fsmonitor_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/builtin/grep.c b/builtin/grep.c
index 76cf999d310..757d52b94ec 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -290,13 +290,14 @@ static int wait_all(void)
 	return hit;
 }
 
-static int grep_cmd_config(const char *var, const char *value, void *cb)
+static int grep_cmd_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
-	int st = grep_config(var, value, cb);
+	int st = grep_config(var, value, ctx, cb);
 
 	if (git_color_config(var, value, cb) < 0)
 		st = -1;
-	else if (git_default_config(var, value, cb) < 0)
+	else if (git_default_config(var, value, ctx, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/help.c b/builtin/help.c
index d3cf4af3f6e..c348f201254 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -398,7 +398,8 @@ static int add_man_viewer_info(const char *var, const char *value)
 	return 0;
 }
 
-static int git_help_config(const char *var, const char *value, void *cb)
+static int git_help_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "help.format")) {
 		if (!value)
@@ -421,7 +422,7 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index d0d8067510b..de8884ea5c2 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1581,7 +1581,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
 	strbuf_release(&pack_name);
 }
 
-static int git_index_pack_config(const char *k, const char *v, void *cb)
+static int git_index_pack_config(const char *k, const char *v,
+				 const struct config_context *ctx, void *cb)
 {
 	struct pack_idx_option *opts = cb;
 
@@ -1608,7 +1609,7 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
 		else
 			opts->flags &= ~WRITE_REV;
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 static int cmp_uint32(const void *a_, const void *b_)
diff --git a/builtin/log.c b/builtin/log.c
index c85f13a5d5e..09d6a13075b 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -564,7 +564,8 @@ static int cmd_log_walk(struct rev_info *rev)
 	return retval;
 }
 
-static int git_log_config(const char *var, const char *value, void *cb)
+static int git_log_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -613,7 +614,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_diff_ui_config(var, value, cb);
+	return git_diff_ui_config(var, value, ctx, cb);
 }
 
 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
@@ -979,7 +980,8 @@ static enum cover_from_description parse_cover_from_description(const char *arg)
 		die(_("%s: invalid cover from description mode"), arg);
 }
 
-static int git_format_config(const char *var, const char *value, void *cb)
+static int git_format_config(const char *var, const char *value,
+			     const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "format.headers")) {
 		if (!value)
@@ -1108,7 +1110,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "diff.noprefix"))
 		return 0;
 
-	return git_log_config(var, value, cb);
+	return git_log_config(var, value, ctx, cb);
 }
 
 static const char *output_directory = NULL;
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb0..cad624fb797 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -623,7 +623,8 @@ static void parse_branch_merge_options(char *bmo)
 	free(argv);
 }
 
-static int git_merge_config(const char *k, const char *v, void *cb)
+static int git_merge_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	int status;
 	const char *str;
@@ -668,10 +669,10 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	status = fmt_merge_msg_config(k, v, cb);
+	status = fmt_merge_msg_config(k, v, ctx, cb);
 	if (status)
 		return status;
-	return git_diff_ui_config(k, v, cb);
+	return git_diff_ui_config(k, v, ctx, cb);
 }
 
 static int read_tree_trivial(struct object_id *common, struct object_id *head,
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 1b5083f8b26..a0a7b82cc69 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -82,6 +82,7 @@ static struct option *add_common_options(struct option *prev)
 }
 
 static int git_multi_pack_index_write_config(const char *var, const char *value,
+					     const struct config_context *ctx UNUSED,
 					     void *cb UNUSED)
 {
 	if (!strcmp(var, "pack.writebitmaphashcache")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 3af2d84f589..34aa0b483a0 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3135,7 +3135,8 @@ static void prepare_pack(int window, int depth)
 	free(delta_list);
 }
 
-static int git_pack_config(const char *k, const char *v, void *cb)
+static int git_pack_config(const char *k, const char *v,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
 		window = git_config_int(k, v);
@@ -3227,7 +3228,7 @@ static int git_pack_config(const char *k, const char *v, void *cb)
 		ex->uri = xstrdup(pack_end + 1);
 		oidmap_put(&configured_exclusions, ex);
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 /* Counters for trace2 output when in --stdin-packs mode. */
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 9d5585d3a72..03eddd0fb82 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -196,7 +196,8 @@ struct patch_id_opts {
 	int verbatim;
 };
 
-static int git_patch_id_config(const char *var, const char *value, void *cb)
+static int git_patch_id_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	struct patch_id_opts *opts = cb;
 
@@ -209,7 +210,7 @@ static int git_patch_id_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_patch_id(int argc, const char **argv, const char *prefix)
diff --git a/builtin/pull.c b/builtin/pull.c
index 0c7bac97b75..83fca5b1d46 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -361,7 +361,8 @@ static enum rebase_type config_get_rebase(int *rebase_unspecified)
 /**
  * Read config variables.
  */
-static int git_pull_config(const char *var, const char *value, void *cb)
+static int git_pull_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "rebase.autostash")) {
 		config_autostash = git_config_bool(var, value);
@@ -374,7 +375,7 @@ static int git_pull_config(const char *var, const char *value, void *cb)
 		check_trust_level = 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /**
diff --git a/builtin/push.c b/builtin/push.c
index dbdf609daf3..a2f68f77324 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -510,7 +510,8 @@ static void set_push_cert_flags(int *flags, int v)
 }
 
 
-static int git_push_config(const char *k, const char *v, void *cb)
+static int git_push_config(const char *k, const char *v,
+			   const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 	int *flags = cb;
@@ -577,7 +578,7 @@ static int git_push_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_default_config(k, v, NULL);
+	return git_default_config(k, v, ctx, NULL);
 }
 
 int cmd_push(int argc, const char **argv, const char *prefix)
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 440f19b1b87..8877dd6d4b5 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -102,12 +102,13 @@ static int debug_merge(const struct cache_entry * const *stages,
 	return 0;
 }
 
-static int git_read_tree_config(const char *var, const char *value, void *cb)
+static int git_read_tree_config(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d11..60930e2d8e0 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -772,7 +772,8 @@ static void parse_rebase_merges_value(struct rebase_options *options, const char
 		die(_("Unknown rebase-merges mode: %s"), value);
 }
 
-static int rebase_config(const char *var, const char *value, void *data)
+static int rebase_config(const char *var, const char *value,
+			 const struct config_context *ctx, void *data)
 {
 	struct rebase_options *opts = data;
 
@@ -831,7 +832,7 @@ static int rebase_config(const char *var, const char *value, void *data)
 		return git_config_string(&opts->default_backend, var, value);
 	}
 
-	return git_default_config(var, value, data);
+	return git_default_config(var, value, ctx, data);
 }
 
 static int checkout_up_to_date(struct rebase_options *options)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 1a31a583674..94d9898aff7 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -139,7 +139,8 @@ static enum deny_action parse_deny_action(const char *var, const char *value)
 	return DENY_IGNORE;
 }
 
-static int receive_pack_config(const char *var, const char *value, void *cb)
+static int receive_pack_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
 
@@ -266,7 +267,7 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void show_ref(const char *path, const struct object_id *oid)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index a1fa0c855f4..84251cc9517 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -108,7 +108,8 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
 #define EXPIRE_TOTAL   01
 #define EXPIRE_UNREACH 02
 
-static int reflog_expire_config(const char *var, const char *value, void *cb)
+static int reflog_expire_config(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	const char *pattern, *key;
 	size_t pattern_len;
@@ -117,7 +118,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 	struct reflog_expire_cfg *ent;
 
 	if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value, ctx, cb);
 
 	if (!strcmp(key, "reflogexpire")) {
 		slot = EXPIRE_TOTAL;
@@ -128,7 +129,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 		if (git_config_expiry_date(&expire, var, value))
 			return -1;
 	} else
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value, ctx, cb);
 
 	if (!pattern) {
 		switch (slot) {
diff --git a/builtin/remote.c b/builtin/remote.c
index 1e0b137d977..87de81105e2 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -268,6 +268,7 @@ static const char *abbrev_ref(const char *name, const char *prefix)
 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
 
 static int config_read_branches(const char *key, const char *value,
+				const struct config_context *ctx UNUSED,
 				void *data UNUSED)
 {
 	const char *orig_key = key;
@@ -645,7 +646,7 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	void *cb)
+	const struct config_context *ctx UNUSED, void *cb)
 {
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
@@ -1494,7 +1495,9 @@ static int prune(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
+static int get_remote_default(const char *key, const char *value UNUSED,
+			      const struct config_context *ctx UNUSED,
+			      void *priv)
 {
 	if (strcmp(key, "remotes.default") == 0) {
 		int *found = priv;
diff --git a/builtin/repack.c b/builtin/repack.c
index 0541c3ce157..6f74570bf94 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -59,7 +59,8 @@ struct pack_objects_args {
 	int local;
 };
 
-static int repack_config(const char *var, const char *value, void *cb)
+static int repack_config(const char *var, const char *value,
+			 const struct config_context *ctx, void *cb)
 {
 	struct pack_objects_args *cruft_po_args = cb;
 	if (!strcmp(var, "repack.usedeltabaseoffset")) {
@@ -91,7 +92,7 @@ static int repack_config(const char *var, const char *value, void *cb)
 		return git_config_string(&cruft_po_args->depth, var, value);
 	if (!strcmp(var, "repack.cruftthreads"))
 		return git_config_string(&cruft_po_args->threads, var, value);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/builtin/reset.c b/builtin/reset.c
index f99f32d5802..1ae82f2e89c 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -312,12 +312,13 @@ static int reset_refs(const char *rev, const struct object_id *oid)
 	return update_ref_status;
 }
 
-static int git_reset_config(const char *var, const char *value, void *cb)
+static int git_reset_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_reset(int argc, const char **argv, const char *prefix)
diff --git a/builtin/send-pack.c b/builtin/send-pack.c
index 4784143004d..cd6d9e41129 100644
--- a/builtin/send-pack.c
+++ b/builtin/send-pack.c
@@ -131,7 +131,8 @@ static void print_helper_status(struct ref *ref)
 	strbuf_release(&buf);
 }
 
-static int send_pack_config(const char *k, const char *v, void *cb)
+static int send_pack_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "push.gpgsign")) {
 		const char *value;
@@ -151,7 +152,7 @@ static int send_pack_config(const char *k, const char *v, void *cb)
 			}
 		}
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 int cmd_send_pack(int argc, const char **argv, const char *prefix)
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index a2461270d4b..f2fd245b838 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -559,7 +559,8 @@ static void append_one_rev(const char *av)
 	die("bad sha1 reference %s", av);
 }
 
-static int git_show_branch_config(const char *var, const char *value, void *cb)
+static int git_show_branch_config(const char *var, const char *value,
+				  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "showbranch.default")) {
 		if (!value)
@@ -582,7 +583,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/stash.c b/builtin/stash.c
index a7e17ffe384..e5c4246d2d4 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -837,7 +837,8 @@ static int show_stat = 1;
 static int show_patch;
 static int show_include_untracked;
 
-static int git_stash_config(const char *var, const char *value, void *cb)
+static int git_stash_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "stash.showstat")) {
 		show_stat = git_config_bool(var, value);
@@ -851,7 +852,7 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 		show_include_untracked = git_config_bool(var, value);
 		return 0;
 	}
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value, ctx, cb);
 }
 
 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 6a16208e8a8..f8e9d85e77a 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2192,6 +2192,7 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
+				   const struct config_context *ctx UNUSED,
 				   void *cb)
 {
 	int *max_jobs = cb;
diff --git a/builtin/tag.c b/builtin/tag.c
index 1acf5f7a59f..b7dfb5e2cad 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -188,7 +188,8 @@ static const char tag_template_nocleanup[] =
 	"Lines starting with '%c' will be kept; you may remove them"
 	" yourself if you want to.\n");
 
-static int git_tag_config(const char *var, const char *value, void *cb)
+static int git_tag_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "tag.gpgsign")) {
 		config_sign_tag = git_config_bool(var, value);
@@ -213,7 +214,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/builtin/var.c b/builtin/var.c
index 21499989807..ae011bdf409 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -71,13 +71,14 @@ static const struct git_var *get_git_var(const char *var)
 	return NULL;
 }
 
-static int show_config(const char *var, const char *value, void *cb)
+static int show_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb)
 {
 	if (value)
 		printf("%s=%s\n", var, value);
 	else
 		printf("%s\n", var);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 5a9cf076ad2..a30d37a273f 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -128,14 +128,15 @@ static int verbose;
 static int guess_remote;
 static timestamp_t expire;
 
-static int git_worktree_config(const char *var, const char *value, void *cb)
+static int git_worktree_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "worktree.guessremote")) {
 		guess_remote = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int delete_git_dir(const char *id)
diff --git a/bundle-uri.c b/bundle-uri.c
index 2a2db1a1d39..0d5acc3dc51 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -224,7 +224,9 @@ static int bundle_list_update(const char *key, const char *value,
 	return 0;
 }
 
-static int config_to_bundle_list(const char *key, const char *value, void *data)
+static int config_to_bundle_list(const char *key, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *data)
 {
 	struct bundle_list *list = data;
 	return bundle_list_update(key, value, list);
@@ -871,7 +873,9 @@ cached:
 	return advertise_bundle_uri;
 }
 
-static int config_to_packet_line(const char *key, const char *value, void *data)
+static int config_to_packet_line(const char *key, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *data)
 {
 	struct packet_reader *writer = data;
 
diff --git a/compat/mingw.c b/compat/mingw.c
index d06cdc6254f..4186bc7a417 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -244,7 +244,8 @@ static int core_restrict_inherited_handles = -1;
 static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
 static char *unset_environment_variables;
 
-int mingw_core_config(const char *var, const char *value, void *cb)
+int mingw_core_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "core.hidedotfiles")) {
 		if (value && !strcasecmp(value, "dotgitonly"))
diff --git a/compat/mingw.h b/compat/mingw.h
index 209cf7cebad..5e34c873473 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -11,7 +11,9 @@ typedef _sigset_t sigset_t;
 #undef _POSIX_THREAD_SAFE_FUNCTIONS
 #endif
 
-int mingw_core_config(const char *var, const char *value, void *cb);
+struct config_context;
+int mingw_core_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb);
 #define platform_core_config mingw_core_config
 
 /*
diff --git a/config.c b/config.c
index 2eeb6621421..850e432e301 100644
--- a/config.c
+++ b/config.c
@@ -209,7 +209,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_(
@@ -388,7 +389,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;
@@ -423,6 +425,7 @@ static void populate_remote_urls(struct config_include_data *inc)
 }
 
 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;
@@ -486,7 +489,9 @@ 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;
@@ -498,7 +503,7 @@ 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, NULL, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -680,7 +685,7 @@ static int config_parse_pair(const char *key, const char *value,
 	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, NULL, data) < 0) ? -1 : 0;
 	free(canonical_name);
 	return ret;
 }
@@ -968,7 +973,7 @@ 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);
+	ret = fn(name->buf, value, NULL, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1562,7 +1567,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")) {
@@ -1842,7 +1848,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)
@@ -1944,15 +1950,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);
@@ -2318,7 +2325,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
 
-		if (fn(entry->key, values->items[value_index].string, data) < 0)
+		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
 			git_die_config_linenr(entry->key,
 					      reader->config_kvi->filename,
 					      reader->config_kvi->linenr);
@@ -2496,7 +2503,9 @@ struct configset_add_data {
 };
 #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 UNUSED,
+			       void *cb)
 {
 	struct configset_add_data *data = cb;
 	configset_add_value(data->config_reader, data->config_set, key, value);
@@ -3106,7 +3115,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;
 
diff --git a/config.h b/config.h
index d1c5577589e..cd30125a8a4 100644
--- a/config.h
+++ b/config.h
@@ -110,8 +110,29 @@ struct config_options {
 	} error_action;
 };
 
+/* Config source metadata for a given config key-value pair */
+struct key_value_info {
+	const char *filename;
+	int linenr;
+	enum config_origin_type origin_type;
+	enum config_scope scope;
+};
+#define KVI_INIT { \
+	.filename = NULL, \
+	.linenr = -1, \
+	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
+	.scope = CONFIG_SCOPE_UNKNOWN, \
+}
+
+/* Captures additional information that a config callback can use. */
+struct config_context {
+	/* Config source metadata for key and value. */
+	const struct key_value_info *kvi;
+};
+#define CONFIG_CONTEXT_INIT { 0 }
+
 /**
- * A config callback function takes three parameters:
+ * A config callback function takes four parameters:
  *
  * - the name of the parsed variable. This is in canonical "flat" form: the
  *   section, subsection, and variable segments will be separated by dots,
@@ -122,15 +143,22 @@ struct config_options {
  *   value specified, the value will be NULL (typically this means it
  *   should be interpreted as boolean true).
  *
+ * - the 'config context', that is, additional information about the config
+ *   iteration operation provided by the config machinery. For example, this
+ *   includes information about the config source being parsed (e.g. the
+ *   filename).
+ *
  * - a void pointer passed in by the caller of the config API; this can
  *   contain callback-specific data
  *
  * A config callback should return 0 for success, or -1 if the variable
  * could not be parsed properly.
  */
-typedef int (*config_fn_t)(const char *, const char *, void *);
+typedef int (*config_fn_t)(const char *, const char *,
+			   const struct config_context *, void *);
 
-int git_default_config(const char *, const char *, void *);
+int git_default_config(const char *, const char *,
+		       const struct config_context *, void *);
 
 /**
  * Read a specific file in git-config format.
@@ -667,13 +695,6 @@ int git_config_get_expiry(const char *key, const char **output);
 /* parse either "this many days" integer, or "5.days.ago" approxidate */
 int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
 
-struct key_value_info {
-	const char *filename;
-	int linenr;
-	enum config_origin_type origin_type;
-	enum config_scope scope;
-};
-
 /**
  * First prints the error message specified by the caller in `err` and then
  * dies printing the line number and the file name of the highest priority
diff --git a/connect.c b/connect.c
index 3a0186280c4..cddac1d96b8 100644
--- a/connect.c
+++ b/connect.c
@@ -964,7 +964,7 @@ static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
 static char *git_proxy_command;
 
 static int git_proxy_command_options(const char *var, const char *value,
-		void *cb)
+		const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "core.gitproxy")) {
 		const char *for_pos;
@@ -1010,7 +1010,7 @@ static int git_proxy_command_options(const char *var, const char *value,
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int git_use_proxy(const char *host)
diff --git a/contrib/coccinelle/config_fn_ctx.pending.cocci b/contrib/coccinelle/config_fn_ctx.pending.cocci
new file mode 100644
index 00000000000..6d3d1000a96
--- /dev/null
+++ b/contrib/coccinelle/config_fn_ctx.pending.cocci
@@ -0,0 +1,144 @@
+@ get_fn @
+identifier fn, R;
+@@
+(
+(
+git_config_from_file
+|
+git_config_from_file_with_options
+|
+git_config_from_mem
+|
+git_config_from_blob_oid
+|
+read_early_config
+|
+read_very_early_config
+|
+config_with_options
+|
+git_config
+|
+git_protected_config
+|
+config_from_gitmodules
+)
+  (fn, ...)
+|
+repo_config(R, fn, ...)
+)
+
+@ extends get_fn @
+identifier C1, C2, D;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D);
+
+@ extends get_fn @
+@@
+int fn(const char *, const char *,
++ const struct config_context *,
+  void *);
+
+@ extends get_fn @
+// Don't change fns that look like callback fns but aren't
+identifier fn2 != tar_filter_config && != git_diff_heuristic_config &&
+  != git_default_submodule_config && != git_color_config &&
+  != bundle_list_update && != parse_object_filter_config;
+identifier C1, C2, D1, D2, S;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D1) {
+<+...
+(
+fn2(C1, C2
++ , ctx
+, D2);
+|
+if(fn2(C1, C2
++ , ctx
+, D2) < 0) { ... }
+|
+return fn2(C1, C2
++ , ctx
+, D2);
+|
+S = fn2(C1, C2
++ , ctx
+, D2);
+)
+...+>
+  }
+
+@ extends get_fn@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx UNUSED,
+  void *D) {...}
+
+
+// The previous rules don't catch all callbacks, especially if they're defined
+// in a separate file from the git_config() call. Fix these manually.
+@@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int
+(
+git_ident_config
+|
+urlmatch_collect_fn
+|
+write_one_config
+|
+forbid_remote_url
+|
+credential_config_callback
+)
+  (const char *C1, const char *C2,
++ const struct config_context *ctx UNUSED,
+  void *D) {...}
+
+@@
+identifier C1, C2, D, D2, S, fn2;
+@@
+int
+(
+http_options
+|
+git_status_config
+|
+git_commit_config
+|
+git_default_core_config
+|
+grep_config
+)
+  (const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D) {
+<+...
+(
+fn2(C1, C2
++ , ctx
+, D2);
+|
+if(fn2(C1, C2
++ , ctx
+, D2) < 0) { ... }
+|
+return fn2(C1, C2
++ , ctx
+, D2);
+|
+S = fn2(C1, C2
++ , ctx
+, D2);
+)
+...+>
+  }
diff --git a/convert.c b/convert.c
index 9ee79fe4699..8e96cf83030 100644
--- a/convert.c
+++ b/convert.c
@@ -1015,7 +1015,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
 	return 0;
 }
 
-static int read_convert_config(const char *var, const char *value, void *cb UNUSED)
+static int read_convert_config(const char *var, const char *value,
+			       const struct config_context *ctx UNUSED,
+			       void *cb UNUSED)
 {
 	const char *key, *name;
 	size_t namelen;
diff --git a/credential.c b/credential.c
index 8825c6f1320..d6647541634 100644
--- a/credential.c
+++ b/credential.c
@@ -49,6 +49,7 @@ static int credential_from_potentially_partial_url(struct credential *c,
 						   const char *url);
 
 static int credential_config_callback(const char *var, const char *value,
+				      const struct config_context *ctx UNUSED,
 				      void *data)
 {
 	struct credential *c = data;
diff --git a/delta-islands.c b/delta-islands.c
index c824a5f6a42..5fc6ea6ff55 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -341,7 +341,9 @@ static void free_remote_islands(kh_str_t *remote_islands)
 	kh_destroy_str(remote_islands);
 }
 
-static int island_config_callback(const char *k, const char *v, void *cb)
+static int island_config_callback(const char *k, const char *v,
+				  const struct config_context *ctx UNUSED,
+				  void *cb)
 {
 	struct island_load_data *ild = cb;
 
diff --git a/diff.c b/diff.c
index c106f8a4ffa..0e382c8f7f0 100644
--- a/diff.c
+++ b/diff.c
@@ -357,7 +357,8 @@ static unsigned parse_color_moved_ws(const char *arg)
 	return ret;
 }
 
-int git_diff_ui_config(const char *var, const char *value, void *cb)
+int git_diff_ui_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 		diff_use_color_default = git_config_colorbool(var, value);
@@ -440,10 +441,11 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value, ctx, cb);
 }
 
-int git_diff_basic_config(const char *var, const char *value, void *cb)
+int git_diff_basic_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	const char *name;
 
@@ -495,7 +497,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
 	if (git_diff_heuristic_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static char *quote_two(const char *one, const char *two)
diff --git a/diff.h b/diff.h
index 6c10ce289da..33a4f4d5988 100644
--- a/diff.h
+++ b/diff.h
@@ -531,10 +531,13 @@ void free_diffstat_info(struct diffstat_t *diffstat);
 int parse_long_opt(const char *opt, const char **argv,
 		   const char **optarg);
 
-int git_diff_basic_config(const char *var, const char *value, void *cb);
+struct config_context;
+int git_diff_basic_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb);
 int git_diff_heuristic_config(const char *var, const char *value, void *cb);
 void init_diff_ui_defaults(void);
-int git_diff_ui_config(const char *var, const char *value, void *cb);
+int git_diff_ui_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb);
 void repo_diff_setup(struct repository *, struct diff_options *);
 struct option *add_diff_options(const struct option *, struct diff_options *);
 int diff_opt_parse(struct diff_options *, const char **, int, const char *);
diff --git a/fetch-pack.c b/fetch-pack.c
index 0f71054fbae..5f3d40f3d09 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1860,7 +1860,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	return ref;
 }
 
-static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
+static int fetch_pack_config_cb(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	if (strcmp(var, "fetch.fsck.skiplist") == 0) {
 		const char *path;
@@ -1882,7 +1883,7 @@ static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void fetch_pack_config(void)
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715ba..10137444321 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -20,7 +20,8 @@ static int use_branch_desc;
 static int suppress_dest_pattern_seen;
 static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP;
 
-int fmt_merge_msg_config(const char *key, const char *value, void *cb)
+int fmt_merge_msg_config(const char *key, const char *value,
+			 const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
@@ -40,7 +41,7 @@ int fmt_merge_msg_config(const char *key, const char *value, void *cb)
 			string_list_append(&suppress_dest_patterns, value);
 		suppress_dest_pattern_seen = 1;
 	} else {
-		return git_default_config(key, value, cb);
+		return git_default_config(key, value, ctx, cb);
 	}
 	return 0;
 }
diff --git a/fmt-merge-msg.h b/fmt-merge-msg.h
index 99054042dc5..73ca3e44652 100644
--- a/fmt-merge-msg.h
+++ b/fmt-merge-msg.h
@@ -13,7 +13,8 @@ struct fmt_merge_msg_opts {
 };
 
 extern int merge_log_config;
-int fmt_merge_msg_config(const char *key, const char *value, void *cb);
+int fmt_merge_msg_config(const char *key, const char *value,
+			 const struct config_context *ctx, void *cb);
 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 		  struct fmt_merge_msg_opts *);
 
diff --git a/fsck.c b/fsck.c
index 3261ef9ec28..55b6a694853 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1163,7 +1163,9 @@ struct fsck_gitmodules_data {
 	int ret;
 };
 
-static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
+static int fsck_gitmodules_fn(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
+			      void *vdata)
 {
 	struct fsck_gitmodules_data *data = vdata;
 	const char *subsection, *key;
@@ -1373,7 +1375,8 @@ int fsck_finish(struct fsck_options *options)
 	return ret;
 }
 
-int git_fsck_config(const char *var, const char *value, void *cb)
+int git_fsck_config(const char *var, const char *value,
+		    const struct config_context *ctx, void *cb)
 {
 	struct fsck_options *options = cb;
 	if (strcmp(var, "fsck.skiplist") == 0) {
@@ -1394,7 +1397,7 @@ int git_fsck_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/fsck.h b/fsck.h
index e17730e9da9..6359ba359bd 100644
--- a/fsck.h
+++ b/fsck.h
@@ -233,10 +233,12 @@ void fsck_put_object_name(struct fsck_options *options,
 const char *fsck_describe_object(struct fsck_options *options,
 				 const struct object_id *oid);
 
+struct key_value_info;
 /*
  * git_config() callback for use by fsck-y tools that want to support
  * fsck.<msg> fsck.skipList etc.
  */
-int git_fsck_config(const char *var, const char *value, void *cb);
+int git_fsck_config(const char *var, const char *value,
+		    const struct config_context *ctx, void *cb);
 
 #endif
diff --git a/git-compat-util.h b/git-compat-util.h
index 5b2b99c17c5..14e8aacb957 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -440,8 +440,10 @@ typedef uintmax_t timestamp_t;
 #endif
 
 #ifndef platform_core_config
+struct config_context;
 static inline int noop_core_config(const char *var UNUSED,
 				   const char *value UNUSED,
+				   const struct config_context *ctx UNUSED,
 				   void *cb UNUSED)
 {
 	return 0;
diff --git a/gpg-interface.c b/gpg-interface.c
index 19a3471a0b5..57c862a3a22 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -14,7 +14,8 @@
 #include "alias.h"
 #include "wrapper.h"
 
-static int git_gpg_config(const char *, const char *, void *);
+static int git_gpg_config(const char *, const char *,
+			  const struct config_context *, void *);
 
 static void gpg_interface_lazy_init(void)
 {
@@ -720,7 +721,9 @@ void set_signing_key(const char *key)
 	configured_signing_key = xstrdup(key);
 }
 
-static int git_gpg_config(const char *var, const char *value, void *cb UNUSED)
+static int git_gpg_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED,
+			  void *cb UNUSED)
 {
 	struct gpg_format *fmt = NULL;
 	char *fmtname = NULL;
diff --git a/grep.c b/grep.c
index f00986c451a..fc22c3e2afb 100644
--- a/grep.c
+++ b/grep.c
@@ -56,7 +56,8 @@ define_list_config_array_extra(color_grep_slots, {"match"});
  * Read the configuration file once and store it in
  * the grep_defaults template.
  */
-int grep_config(const char *var, const char *value, void *cb)
+int grep_config(const char *var, const char *value,
+		const struct config_context *ctx, void *cb)
 {
 	struct grep_opt *opt = cb;
 	const char *slot;
@@ -91,9 +92,9 @@ int grep_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "color.grep"))
 		opt->color = git_config_colorbool(var, value);
 	if (!strcmp(var, "color.grep.match")) {
-		if (grep_config("color.grep.matchcontext", value, cb) < 0)
+		if (grep_config("color.grep.matchcontext", value, ctx, cb) < 0)
 			return -1;
-		if (grep_config("color.grep.matchselected", value, cb) < 0)
+		if (grep_config("color.grep.matchselected", value, ctx, cb) < 0)
 			return -1;
 	} else if (skip_prefix(var, "color.grep.", &slot)) {
 		int i = LOOKUP_CONFIG(color_grep_slots, slot);
diff --git a/grep.h b/grep.h
index c59592e3bdb..926c0875c42 100644
--- a/grep.h
+++ b/grep.h
@@ -202,7 +202,9 @@ struct grep_opt {
 	.output = std_output, \
 }
 
-int grep_config(const char *var, const char *value, void *);
+struct config_context;
+int grep_config(const char *var, const char *value,
+		const struct config_context *ctx, void *data);
 void grep_init(struct grep_opt *, struct repository *repo);
 
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
diff --git a/help.c b/help.c
index 5d7637dce92..ac0ae5ac0dc 100644
--- a/help.c
+++ b/help.c
@@ -309,7 +309,8 @@ void load_command_list(const char *prefix,
 	exclude_cmds(other_cmds, main_cmds);
 }
 
-static int get_colopts(const char *var, const char *value, void *data)
+static int get_colopts(const char *var, const char *value,
+		       const struct config_context *ctx UNUSED, void *data)
 {
 	unsigned int *colopts = data;
 
@@ -459,7 +460,8 @@ void list_developer_interfaces_help(void)
 	putchar('\n');
 }
 
-static int get_alias(const char *var, const char *value, void *data)
+static int get_alias(const char *var, const char *value,
+		     const struct config_context *ctx UNUSED, void *data)
 {
 	struct string_list *list = data;
 
@@ -543,6 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
+				  const struct config_context *ctx UNUSED,
 				  void *cb UNUSED)
 {
 	const char *p;
diff --git a/http.c b/http.c
index bb58bb3e6a3..762502828c9 100644
--- a/http.c
+++ b/http.c
@@ -363,7 +363,8 @@ static void process_curl_messages(void)
 	}
 }
 
-static int http_options(const char *var, const char *value, void *cb)
+static int http_options(const char *var, const char *value,
+			const struct config_context *ctx, void *data)
 {
 	if (!strcmp("http.version", var)) {
 		return git_config_string(&curl_http_version, var, value);
@@ -534,7 +535,7 @@ static int http_options(const char *var, const char *value, void *cb)
 	}
 
 	/* Fall back on the default ones */
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, data);
 }
 
 static int curl_empty_auth_enabled(void)
diff --git a/ident.c b/ident.c
index 8fad92d7007..08be4d0747d 100644
--- a/ident.c
+++ b/ident.c
@@ -671,7 +671,9 @@ static int set_ident(const char *var, const char *value)
 	return 0;
 }
 
-int git_ident_config(const char *var, const char *value, void *data UNUSED)
+int git_ident_config(const char *var, const char *value,
+		     const struct config_context *ctx UNUSED,
+		     void *data UNUSED)
 {
 	if (!strcmp(var, "user.useconfigonly")) {
 		ident_use_config_only = git_config_bool(var, value);
diff --git a/ident.h b/ident.h
index 96a64896a01..6a79febba15 100644
--- a/ident.h
+++ b/ident.h
@@ -62,6 +62,8 @@ const char *fmt_name(enum want_ident);
 int committer_ident_sufficiently_given(void);
 int author_ident_sufficiently_given(void);
 
-int git_ident_config(const char *, const char *, void *);
+struct config_context;
+int git_ident_config(const char *, const char *, const struct config_context *,
+		     void *);
 
 #endif
diff --git a/imap-send.c b/imap-send.c
index 7f5426177a1..47777e76861 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1323,7 +1323,8 @@ static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
 	return 1;
 }
 
-static int git_imap_config(const char *var, const char *val, void *cb)
+static int git_imap_config(const char *var, const char *val,
+			   const struct config_context *ctx, void *cb)
 {
 
 	if (!strcmp("imap.sslverify", var))
@@ -1357,7 +1358,7 @@ static int git_imap_config(const char *var, const char *val, void *cb)
 			server.host = xstrdup(val);
 		}
 	} else
-		return git_default_config(var, val, cb);
+		return git_default_config(var, val, ctx, cb);
 
 	return 0;
 }
diff --git a/ll-merge.c b/ll-merge.c
index 07ec16e8e5b..3936d112e00 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -254,6 +254,7 @@ static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
 static const char *default_ll_merge;
 
 static int read_merge_config(const char *var, const char *value,
+			     const struct config_context *ctx UNUSED,
 			     void *cb UNUSED)
 {
 	struct ll_merge_driver *fn;
diff --git a/ls-refs.c b/ls-refs.c
index f385938b64c..a29c2364a59 100644
--- a/ls-refs.c
+++ b/ls-refs.c
@@ -137,6 +137,7 @@ static void send_possibly_unborn_head(struct ls_refs_data *data)
 }
 
 static int ls_refs_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED,
 			  void *cb_data)
 {
 	struct ls_refs_data *data = cb_data;
diff --git a/mailinfo.c b/mailinfo.c
index 2aeb20e5e62..931505363cd 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -1241,12 +1241,13 @@ int mailinfo_parse_quoted_cr_action(const char *actionstr, int *action)
 	return 0;
 }
 
-static int git_mailinfo_config(const char *var, const char *value, void *mi_)
+static int git_mailinfo_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *mi_)
 {
 	struct mailinfo *mi = mi_;
 
 	if (!starts_with(var, "mailinfo."))
-		return git_default_config(var, value, NULL);
+		return git_default_config(var, value, ctx, NULL);
 	if (!strcmp(var, "mailinfo.scissors")) {
 		mi->use_scissors = git_config_bool(var, value);
 		return 0;
diff --git a/notes-utils.c b/notes-utils.c
index 4a793eb347f..97c031c26ec 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -94,7 +94,9 @@ static combine_notes_fn parse_combine_notes_fn(const char *v)
 		return NULL;
 }
 
-static int notes_rewrite_config(const char *k, const char *v, void *cb)
+static int notes_rewrite_config(const char *k, const char *v,
+				const struct config_context *ctx UNUSED,
+				void *cb)
 {
 	struct notes_rewrite_cfg *c = cb;
 	if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
diff --git a/notes.c b/notes.c
index f51a2d3630e..e68645a4b89 100644
--- a/notes.c
+++ b/notes.c
@@ -974,7 +974,9 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
 	free(globs_copy);
 }
 
-static int notes_display_config(const char *k, const char *v, void *cb)
+static int notes_display_config(const char *k, const char *v,
+				const struct config_context *ctx UNUSED,
+				void *cb)
 {
 	int *load_refs = cb;
 
diff --git a/pager.c b/pager.c
index 63055d0873f..b8822a9381e 100644
--- a/pager.c
+++ b/pager.c
@@ -43,6 +43,7 @@ static void wait_for_pager_signal(int signo)
 }
 
 static int core_pager_config(const char *var, const char *value,
+			     const struct config_context *ctx UNUSED,
 			     void *data UNUSED)
 {
 	if (!strcmp(var, "core.pager"))
@@ -228,7 +229,9 @@ struct pager_command_config_data {
 	char *value;
 };
 
-static int pager_command_config(const char *var, const char *value, void *vdata)
+static int pager_command_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *vdata)
 {
 	struct pager_command_config_data *data = vdata;
 	const char *cmd;
diff --git a/pretty.c b/pretty.c
index 0bb938021ba..87245353452 100644
--- a/pretty.c
+++ b/pretty.c
@@ -56,6 +56,7 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
 }
 
 static int git_pretty_formats_config(const char *var, const char *value,
+				     const struct config_context *ctx UNUSED,
 				     void *cb UNUSED)
 {
 	struct cmt_fmt_map *commit_format = NULL;
diff --git a/promisor-remote.c b/promisor-remote.c
index 1adcd6fb0a5..c22abb85b15 100644
--- a/promisor-remote.c
+++ b/promisor-remote.c
@@ -100,7 +100,9 @@ static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
 	config->promisors_tail = &r->next;
 }
 
-static int promisor_remote_config(const char *var, const char *value, void *data)
+static int promisor_remote_config(const char *var, const char *value,
+				  const struct config_context *ctx UNUSED,
+				  void *data)
 {
 	struct promisor_remote_config *config = data;
 	const char *name;
diff --git a/remote.c b/remote.c
index 1bcd36e358a..241999c2842 100644
--- a/remote.c
+++ b/remote.c
@@ -349,7 +349,8 @@ static void read_branches_file(struct remote_state *remote_state,
 	remote->fetch_tags = 1; /* always auto-follow */
 }
 
-static int handle_config(const char *key, const char *value, void *cb)
+static int handle_config(const char *key, const char *value,
+			 const struct config_context *ctx UNUSED, void *cb)
 {
 	const char *name;
 	size_t namelen;
diff --git a/revision.c b/revision.c
index b33cc1d106a..87ed8ccd444 100644
--- a/revision.c
+++ b/revision.c
@@ -1572,7 +1572,9 @@ struct exclude_hidden_refs_cb {
 	const char *section;
 };
 
-static int hide_refs_config(const char *var, const char *value, void *cb_data)
+static int hide_refs_config(const char *var, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *cb_data)
 {
 	struct exclude_hidden_refs_cb *cb = cb_data;
 	cb->exclusions->hidden_refs_configured = 1;
diff --git a/scalar.c b/scalar.c
index 1326e1f6089..df7358f481c 100644
--- a/scalar.c
+++ b/scalar.c
@@ -594,7 +594,9 @@ static int cmd_register(int argc, const char **argv)
 	return register_dir();
 }
 
-static int get_scalar_repos(const char *key, const char *value, void *data)
+static int get_scalar_repos(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *data)
 {
 	struct string_list *list = data;
 
diff --git a/sequencer.c b/sequencer.c
index bceb6abcb6c..34754d17596 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -219,7 +219,8 @@ static struct update_ref_record *init_update_ref_record(const char *ref)
 	return rec;
 }
 
-static int git_sequencer_config(const char *k, const char *v, void *cb)
+static int git_sequencer_config(const char *k, const char *v,
+				const struct config_context *ctx, void *cb)
 {
 	struct replay_opts *opts = cb;
 	int status;
@@ -274,7 +275,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
 	if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
 		opts->commit_use_reference = git_config_bool(k, v);
 
-	return git_diff_basic_config(k, v, NULL);
+	return git_diff_basic_config(k, v, ctx, NULL);
 }
 
 void sequencer_init_config(struct replay_opts *opts)
@@ -2881,7 +2882,9 @@ static int git_config_string_dup(char **dest,
 	return 0;
 }
 
-static int populate_opts_cb(const char *key, const char *value, void *data)
+static int populate_opts_cb(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *data)
 {
 	struct replay_opts *opts = data;
 	int error_flag = 1;
diff --git a/setup.c b/setup.c
index 6f6e92b96be..fadba5bab4b 100644
--- a/setup.c
+++ b/setup.c
@@ -517,7 +517,9 @@ no_prevention_needed:
 	startup_info->original_cwd = NULL;
 }
 
-static int read_worktree_config(const char *var, const char *value, void *vdata)
+static int read_worktree_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *vdata)
 {
 	struct repository_format *data = vdata;
 
@@ -588,7 +590,8 @@ static enum extension_result handle_extension(const char *var,
 	return EXTENSION_UNKNOWN;
 }
 
-static int check_repo_format(const char *var, const char *value, void *vdata)
+static int check_repo_format(const char *var, const char *value,
+			     const struct config_context *ctx, void *vdata)
 {
 	struct repository_format *data = vdata;
 	const char *ext;
@@ -617,7 +620,7 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
 		}
 	}
 
-	return read_worktree_config(var, value, vdata);
+	return read_worktree_config(var, value, ctx, vdata);
 }
 
 static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
@@ -1115,7 +1118,8 @@ struct safe_directory_data {
 	int is_safe;
 };
 
-static int safe_directory_cb(const char *key, const char *value, void *d)
+static int safe_directory_cb(const char *key, const char *value,
+			     const struct config_context *ctx UNUSED, void *d)
 {
 	struct safe_directory_data *data = d;
 
@@ -1171,7 +1175,9 @@ static int ensure_valid_ownership(const char *gitfile,
 	return data.is_safe;
 }
 
-static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
+static int allowed_bare_repo_cb(const char *key, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *d)
 {
 	enum allowed_bare_repo *allowed_bare_repo = d;
 
diff --git a/submodule-config.c b/submodule-config.c
index 7eb7a0d88d2..a38d4d49731 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -426,7 +426,8 @@ struct parse_config_parameter {
  * config store (.git/config, etc).  Callers are responsible for
  * checking for overrides in the main config store when appropriate.
  */
-static int parse_config(const char *var, const char *value, void *data)
+static int parse_config(const char *var, const char *value,
+			const struct config_context *ctx UNUSED, void *data)
 {
 	struct parse_config_parameter *me = data;
 	struct submodule *submodule;
@@ -674,7 +675,8 @@ out:
 	}
 }
 
-static int gitmodules_cb(const char *var, const char *value, void *data)
+static int gitmodules_cb(const char *var, const char *value,
+			 const struct config_context *ctx, void *data)
 {
 	struct repository *repo = data;
 	struct parse_config_parameter parameter;
@@ -684,7 +686,7 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
 	parameter.gitmodules_oid = null_oid();
 	parameter.overwrite = 1;
 
-	return parse_config(var, value, &parameter);
+	return parse_config(var, value, ctx, &parameter);
 }
 
 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
@@ -801,7 +803,9 @@ void submodule_free(struct repository *r)
 		submodule_cache_clear(r->submodule_cache);
 }
 
-static int config_print_callback(const char *var, const char *value, void *cb_data)
+static int config_print_callback(const char *var, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *cb_data)
 {
 	char *wanted_key = cb_data;
 
@@ -843,7 +847,9 @@ struct fetch_config {
 	int *recurse_submodules;
 };
 
-static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
+static int gitmodules_fetch_config(const char *var, const char *value,
+				   const struct config_context *ctx UNUSED,
+				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
@@ -871,6 +877,7 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
+					  const struct config_context *ctx UNUSED,
 					  void *cb)
 {
 	int *max_jobs = cb;
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index ad78fc17683..85ad815358e 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -42,7 +42,9 @@
  *
  */
 
-static int iterate_cb(const char *var, const char *value, void *data UNUSED)
+static int iterate_cb(const char *var, const char *value,
+		      const struct config_context *ctx UNUSED,
+		      void *data UNUSED)
 {
 	static int nr;
 
@@ -59,7 +61,8 @@ static int iterate_cb(const char *var, const char *value, void *data UNUSED)
 	return 0;
 }
 
-static int parse_int_cb(const char *var, const char *value, void *data)
+static int parse_int_cb(const char *var, const char *value,
+			const struct config_context *ctx UNUSED, void *data)
 {
 	const char *key_to_match = data;
 
@@ -70,7 +73,9 @@ static int parse_int_cb(const char *var, const char *value, void *data)
 	return 0;
 }
 
-static int early_config_cb(const char *var, const char *value, void *vdata)
+static int early_config_cb(const char *var, const char *value,
+			   const struct config_context *ctx UNUSED,
+			   void *vdata)
 {
 	const char *key = vdata;
 
diff --git a/t/helper/test-userdiff.c b/t/helper/test-userdiff.c
index 680124a6760..0ce31ce59f5 100644
--- a/t/helper/test-userdiff.c
+++ b/t/helper/test-userdiff.c
@@ -12,7 +12,9 @@ static int driver_cb(struct userdiff_driver *driver,
 	return 0;
 }
 
-static int cmd__userdiff_config(const char *var, const char *value, void *cb UNUSED)
+static int cmd__userdiff_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *cb UNUSED)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 78cfc15d52d..83bc4fd109c 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -99,7 +99,8 @@ struct tr2_cfg_data {
 /*
  * See if the given config key matches any of our patterns of interest.
  */
-static int tr2_cfg_cb(const char *key, const char *value, void *d)
+static int tr2_cfg_cb(const char *key, const char *value,
+		      const struct config_context *ctx UNUSED, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
@@ -142,8 +143,12 @@ void tr2_list_env_vars_fl(const char *file, int line)
 void tr2_cfg_set_fl(const char *file, int line, const char *key,
 		    const char *value)
 {
+	struct key_value_info kvi = KVI_INIT;
+	struct config_context ctx = {
+		.kvi = &kvi,
+	};
 	struct tr2_cfg_data data = { file, line };
 
 	if (tr2_cfg_load_patterns() > 0)
-		tr2_cfg_cb(key, value, &data);
+		tr2_cfg_cb(key, value, &ctx, &data);
 }
diff --git a/trace2/tr2_sysenv.c b/trace2/tr2_sysenv.c
index 069786cb927..f26ec95ab4d 100644
--- a/trace2/tr2_sysenv.c
+++ b/trace2/tr2_sysenv.c
@@ -57,7 +57,8 @@ static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
 };
 /* clang-format on */
 
-static int tr2_sysenv_cb(const char *key, const char *value, void *d)
+static int tr2_sysenv_cb(const char *key, const char *value,
+			 const struct config_context *ctx UNUSED, void *d)
 {
 	int k;
 
diff --git a/trailer.c b/trailer.c
index a2c3ed6f28c..06dc0b7f683 100644
--- a/trailer.c
+++ b/trailer.c
@@ -482,6 +482,7 @@ static struct {
 };
 
 static int git_trailer_default_config(const char *conf_key, const char *value,
+				      const struct config_context *ctx UNUSED,
 				      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
@@ -514,6 +515,7 @@ static int git_trailer_default_config(const char *conf_key, const char *value,
 }
 
 static int git_trailer_config(const char *conf_key, const char *value,
+			      const struct config_context *ctx UNUSED,
 			      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
diff --git a/upload-pack.c b/upload-pack.c
index d3312006a32..951fd1f9c25 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1309,7 +1309,9 @@ static int parse_object_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int upload_pack_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_config(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
+			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
@@ -1350,7 +1352,9 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
 }
 
-static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_protected_config(const char *var, const char *value,
+					const struct config_context *ctx UNUSED,
+					void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
diff --git a/urlmatch.c b/urlmatch.c
index eba0bdd77fe..1c45f23adf2 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -551,7 +551,8 @@ static int cmp_matches(const struct urlmatch_item *a,
 	return 0;
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb)
+int urlmatch_config_entry(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	struct string_list_item *item;
 	struct urlmatch_config *collect = cb;
@@ -565,7 +566,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)
 
 	if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
 		if (collect->cascade_fn)
-			return collect->cascade_fn(var, value, cb);
+			return collect->cascade_fn(var, value, ctx, cb);
 		return 0; /* not interested */
 	}
 	dot = strrchr(key, '.');
@@ -609,7 +610,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)
 	strbuf_addstr(&synthkey, collect->section);
 	strbuf_addch(&synthkey, '.');
 	strbuf_addstr(&synthkey, key);
-	retval = collect->collect_fn(synthkey.buf, value, collect->cb);
+	retval = collect->collect_fn(synthkey.buf, value, ctx, collect->cb);
 
 	strbuf_release(&synthkey);
 	return retval;
diff --git a/urlmatch.h b/urlmatch.h
index bee374a642c..5ba85cea139 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -71,7 +71,8 @@ struct urlmatch_config {
 	.vars = STRING_LIST_INIT_DUP, \
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb);
+int urlmatch_config_entry(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb);
 void urlmatch_config_release(struct urlmatch_config *config);
 
 #endif /* URL_MATCH_H */
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 0460e03f5ed..dcbb5e09857 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -307,7 +307,8 @@ int xdiff_compare_lines(const char *l1, long s1,
 
 int git_xmerge_style = -1;
 
-int git_xmerge_config(const char *var, const char *value, void *cb)
+int git_xmerge_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "merge.conflictstyle")) {
 		if (!value)
@@ -327,5 +328,5 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
 			    value, var);
 		return 0;
 	}
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
diff --git a/xdiff-interface.h b/xdiff-interface.h
index 733c364d26c..e6f80df0462 100644
--- a/xdiff-interface.h
+++ b/xdiff-interface.h
@@ -50,7 +50,9 @@ int buffer_is_binary(const char *ptr, unsigned long size);
 
 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
 void xdiff_clear_find_func(xdemitconf_t *xecfg);
-int git_xmerge_config(const char *var, const char *value, void *cb);
+struct config_context;
+int git_xmerge_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb);
 extern int git_xmerge_style;
 
 /*
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 04/12] config.c: pass ctx in configsets
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (2 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 03/12] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 05/12] config: pass ctx with config files Glen Choo via GitGitGadget
                         ` (9 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context to config callbacks in configset_iter(), trivially
setting the .kvi member to the cached key_value_info. Then, in config
callbacks that are only used with configsets, use the .kvi member to
replace calls to current_config_*(), and delete current_config_line()
because it has no remaining callers.

This leaves builtin/config.c and config.c as the only remaining users of
current_config_*().

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/remote.c       | 10 ++++++----
 config.c               | 35 ++++++++++++++++-------------------
 config.h               |  2 +-
 remote.c               |  7 ++++---
 t/helper/test-config.c | 11 ++++++-----
 5 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index 87de81105e2..d47f9ee21cf 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -646,17 +646,19 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	const struct config_context *ctx UNUSED, void *cb)
+	const struct config_context *ctx, void *cb)
 {
+	const struct key_value_info *kvi = ctx->kvi;
+
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
 	    !value || strcmp(value, info->old_name))
 		return 0;
 
-	info->scope = current_config_scope();
+	info->scope = kvi->scope;
 	strbuf_reset(&info->origin);
-	strbuf_addstr(&info->origin, current_config_name());
-	info->linenr = current_config_line();
+	strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
+	info->linenr = kvi->linenr;
 
 	return 0;
 }
diff --git a/config.c b/config.c
index 850e432e301..662d406ac1e 100644
--- a/config.c
+++ b/config.c
@@ -2317,6 +2317,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 	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;
@@ -2324,12 +2325,11 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 		values = &entry->value_list;
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
-
-		if (fn(entry->key, values->items[value_index].string, NULL, 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);
-
+					      ctx.kvi->filename,
+					      ctx.kvi->linenr);
 		config_reader_set_kvi(reader, NULL);
 	}
 }
@@ -3984,13 +3984,8 @@ static int reader_origin_type(struct config_reader *reader,
 	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";
@@ -4007,6 +4002,16 @@ const char *current_config_origin_type(void)
 	}
 }
 
+const char *current_config_origin_type(void)
+{
+	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
+
+	if (reader_origin_type(&the_reader, &type))
+		BUG("current_config_origin_type called outside config callback");
+
+	return config_origin_type_name(type);
+}
+
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4054,14 +4059,6 @@ enum config_scope current_config_scope(void)
 		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;
diff --git a/config.h b/config.h
index cd30125a8a4..ddf147bb2d1 100644
--- a/config.h
+++ b/config.h
@@ -387,7 +387,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 enum config_scope current_config_scope(void);
 const char *current_config_origin_type(void);
 const char *current_config_name(void);
-int current_config_line(void);
+const char *config_origin_type_name(enum config_origin_type type);
 
 /*
  * Match and parse a config key of the form:
diff --git a/remote.c b/remote.c
index 241999c2842..1dab860141b 100644
--- a/remote.c
+++ b/remote.c
@@ -350,7 +350,7 @@ static void read_branches_file(struct remote_state *remote_state,
 }
 
 static int handle_config(const char *key, const char *value,
-			 const struct config_context *ctx UNUSED, void *cb)
+			 const struct config_context *ctx, void *cb)
 {
 	const char *name;
 	size_t namelen;
@@ -358,6 +358,7 @@ static int handle_config(const char *key, const char *value,
 	struct remote *remote;
 	struct branch *branch;
 	struct remote_state *remote_state = cb;
+	const struct key_value_info *kvi = ctx->kvi;
 
 	if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
 		/* There is no subsection. */
@@ -415,8 +416,8 @@ static int handle_config(const char *key, const char *value,
 	}
 	remote = make_remote(remote_state, name, namelen);
 	remote->origin = REMOTE_CONFIG;
-	if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
-	    current_config_scope() == CONFIG_SCOPE_WORKTREE)
+	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
+	    kvi->scope == CONFIG_SCOPE_WORKTREE)
 		remote->configured_in_repo = 1;
 	if (!strcmp(subkey, "mirror"))
 		remote->mirror = git_config_bool(key, value);
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 85ad815358e..3f4c3678318 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -43,9 +43,10 @@
  */
 
 static int iterate_cb(const char *var, const char *value,
-		      const struct config_context *ctx UNUSED,
+		      const struct config_context *ctx,
 		      void *data UNUSED)
 {
+	const struct key_value_info *kvi = ctx->kvi;
 	static int nr;
 
 	if (nr++)
@@ -53,10 +54,10 @@ static int iterate_cb(const char *var, const char *value,
 
 	printf("key=%s\n", var);
 	printf("value=%s\n", value ? value : "(null)");
-	printf("origin=%s\n", current_config_origin_type());
-	printf("name=%s\n", current_config_name());
-	printf("lno=%d\n", current_config_line());
-	printf("scope=%s\n", config_scope_name(current_config_scope()));
+	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
+	printf("name=%s\n", kvi->filename ? kvi->filename : "");
+	printf("lno=%d\n", kvi->linenr);
+	printf("scope=%s\n", config_scope_name(kvi->scope));
 
 	return 0;
 }
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 05/12] config: pass ctx with config files
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (3 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 04/12] config.c: pass ctx in configsets Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
                         ` (8 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context to config_callbacks when parsing config files. To
provide the .kvi member, refactor out the configset logic that caches
"struct config_source" and "enum config_scope" as a "struct
key_value_info". Make the "enum config_scope" available to the config
file machinery by plumbing an additional arg through
git_config_from_file_with_options().

We do not exercise ctx yet because the remaining current_config_*()
callers may be used with config_with_options(), which may read config
from parameters, but parameters don't pass ctx yet.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 bundle-uri.c       |   1 +
 config.c           | 105 ++++++++++++++++++++++++++++++---------------
 config.h           |   8 ++--
 fsck.c             |   3 +-
 submodule-config.c |   5 ++-
 5 files changed, 81 insertions(+), 41 deletions(-)

diff --git a/bundle-uri.c b/bundle-uri.c
index 0d5acc3dc51..64f32387745 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -255,6 +255,7 @@ int bundle_uri_parse_config_format(const char *uri,
 	}
 	result = git_config_from_file_with_options(config_to_bundle_list,
 						   filename, list,
+						   CONFIG_SCOPE_UNKNOWN,
 						   &opts);
 
 	if (!result && list->mode == BUNDLE_MODE_NONE) {
diff --git a/config.c b/config.c
index 662d406ac1e..31718711827 100644
--- a/config.c
+++ b/config.c
@@ -259,7 +259,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    !cs ? "<unknown>" :
 			    cs->name ? cs->name :
 			    "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,
+							current_config_scope(),
+							NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -503,7 +505,7 @@ static int git_config_include(const char *var, const char *value,
 	 * Pass along all values, including "include" directives; this makes it
 	 * possible to query information on the includes themselves.
 	 */
-	ret = inc->fn(var, value, NULL, inc->data);
+	ret = inc->fn(var, value, ctx, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -939,12 +941,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 (;;) {
@@ -973,7 +978,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, NULL, data);
+	kvi->linenr = cs->linenr;
+	ret = fn(name->buf, value, &ctx, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1072,8 +1078,19 @@ 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;
+}
+
 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;
@@ -1157,7 +1174,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;
 	}
 
@@ -2010,9 +2027,11 @@ int git_default_config(const char *var, const char *value,
  * this function.
  */
 static int do_config_from(struct config_reader *reader,
-			  struct config_source *top, config_fn_t fn, void *data,
+			  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 */
@@ -2022,8 +2041,9 @@ static int do_config_from(struct config_reader *reader,
 	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);
@@ -2037,7 +2057,8 @@ static int do_config_from_file(struct config_reader *reader,
 			       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;
@@ -2052,19 +2073,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(reader, &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);
+				   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;
@@ -2075,7 +2097,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
 	f = fopen_or_warn(filename, "r");
 	if (f) {
 		ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
-					  filename, filename, f, data, opts);
+					  filename, filename, f, data, scope,
+					  opts);
 		fclose(f);
 	}
 	return ret;
@@ -2083,13 +2106,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;
 
@@ -2104,14 +2129,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(&the_reader, &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;
@@ -2127,7 +2153,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;
@@ -2136,13 +2162,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)
@@ -2228,27 +2255,34 @@ static int do_git_config_sequence(struct config_reader *reader,
 	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);
@@ -2292,12 +2326,14 @@ int config_with_options(config_fn_t fn, void *data,
 	 * 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);
 	}
@@ -2440,16 +2476,14 @@ static int configset_add_value(struct config_reader *reader,
 	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;
+		kvi_from_source(reader->source, current_config_scope(), kv_info);
 	} 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->scope = reader->parsing_scope;
 	si->util = kv_info;
 
 	return 0;
@@ -3490,7 +3524,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;
diff --git a/config.h b/config.h
index ddf147bb2d1..206bf1f175a 100644
--- a/config.h
+++ b/config.h
@@ -169,16 +169,18 @@ int git_default_config(const char *, const char *,
 int git_config_from_file(config_fn_t fn, const char *, void *);
 
 int git_config_from_file_with_options(config_fn_t fn, const char *,
-				      void *,
+				      void *, enum config_scope,
 				      const struct config_options *);
 int git_config_from_mem(config_fn_t fn,
 			const enum config_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);
 int git_config_from_blob_oid(config_fn_t fn, const char *name,
 			     struct repository *repo,
-			     const struct object_id *oid, void *data);
+			     const struct object_id *oid, void *data,
+			     enum config_scope scope);
 void git_config_push_parameter(const char *text);
 void git_config_push_env(const char *spec);
 int git_config_from_parameters(config_fn_t fn, void *data);
diff --git a/fsck.c b/fsck.c
index 55b6a694853..f92c216fb5c 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1238,7 +1238,8 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
 		data.ret = 0;
 		config_opts.error_action = CONFIG_ERROR_SILENT;
 		if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
-					".gitmodules", buf, size, &data, &config_opts))
+					".gitmodules", buf, size, &data,
+					CONFIG_SCOPE_UNKNOWN, &config_opts))
 			data.ret |= report(options, oid, OBJ_BLOB,
 					FSCK_MSG_GITMODULES_PARSE,
 					"could not parse gitmodules blob");
diff --git a/submodule-config.c b/submodule-config.c
index a38d4d49731..3f25bd13674 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -606,7 +606,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 	parameter.gitmodules_oid = &oid;
 	parameter.overwrite = 0;
 	git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
-			config, config_size, &parameter, NULL);
+			    config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
 	strbuf_release(&rev);
 	free(config);
 
@@ -714,7 +714,8 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
 
 	if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
 		git_config_from_blob_oid(gitmodules_cb, rev.buf,
-					 the_repository, &oid, the_repository);
+					 the_repository, &oid, the_repository,
+					 CONFIG_SCOPE_UNKNOWN);
 	}
 	strbuf_release(&rev);
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 06/12] builtin/config.c: test misuse of format_config()
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (4 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 05/12] config: pass ctx with config files Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 07/12] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
                         ` (7 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

current_config_*() functions aren't meant to be called outside of
config callbacks because they read state that is only set when iterating
through config. However, several sites in builtin/config.c are
indirectly calling these functions outside of config callbacks thanks to
the format_config() helper. Show the current, bad behavior via tests
so that the fixes in a subsequent commit will be clearer.

The misbehaving cases are:

* "git config --get-urlmatch --show-scope" results in an "unknown"
   scope, where it arguably should show the config file's scope. It's
   clear that this wasn't intended, though: we knew that
   "--get-urlmatch" couldn't show config source metadata, which is why
   "--show-origin" was marked incompatible with "--get-urlmatch" when
   it was introduced [1]. It was most likely a mistake that we allowed
   "--show-scope" to sneak through.

* Similarly, "git config --default" doesn't set config source metadata ,
  so "--show-scope" also results in "unknown", and "--show-origin"
  results in a BUG().

[1] https://lore.kernel.org/git/20160205112001.GA13397@sigill.intra.peff.net/

Signed-off-by: Glen Choo <chooglen@google.com>
---
 t/t1300-config.sh | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 86bfbc2b364..fa6a8df2521 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1668,6 +1668,21 @@ test_expect_success 'urlmatch' '
 	test_cmp expect actual
 '
 
+test_expect_success 'urlmatch with --show-scope' '
+	cat >.git/config <<-\EOF &&
+	[http "https://weak.example.com"]
+		sslVerify = false
+		cookieFile = /tmp/cookie.txt
+	EOF
+
+	cat >expect <<-EOF &&
+	unknown	http.cookiefile /tmp/cookie.txt
+	unknown	http.sslverify false
+	EOF
+	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'urlmatch favors more specific URLs' '
 	cat >.git/config <<-\EOF &&
 	[http "https://example.com/"]
@@ -2055,6 +2070,10 @@ test_expect_success '--show-origin blob ref' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-origin with --default' '
+	test_must_fail git config --show-origin --default foo some.key
+'
+
 test_expect_success '--show-scope with --list' '
 	cat >expect <<-EOF &&
 	global	user.global=true
@@ -2123,6 +2142,12 @@ test_expect_success '--show-scope with --show-origin' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-scope with --default' '
+	git config --show-scope --default foo some.key >actual &&
+	echo "unknown	foo" >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'override global and system config' '
 	test_when_finished rm -f \"\$HOME\"/.gitconfig &&
 	cat >"$HOME"/.gitconfig <<-EOF &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 07/12] config.c: pass ctx with CLI config
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (5 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 08/12] trace2: plumb config kvi Glen Choo via GitGitGadget
                         ` (6 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context when parsing CLI config. To provide the .kvi member,
refactor out kvi_from_param() from the logic that caches CLI config in
configsets. Now that config_context and config_context.kvi is always
present when config machinery calls config callbacks, plumb "kvi" so
that we can remove all calls of current_config_scope() except for
trace2/*.c (which will be handled in a later commit), and remove all
other current_config_*() (the functions themselves and their calls).
Note that this results in .kvi containing a different, more complete
set of information than the mocked up "struct config_source" in
git_config_from_parameters().

Plumbing "kvi" reveals a few places where we've been doing the wrong
thing:

* git_config_parse_parameter() hasn't been setting config source
  information, so plumb "kvi" there too.

* "git config --get-urlmatch --show-scope" iterates config to collect
  values, but then attempts to display the scope after config iteration.
  Fix this by copying the "kvi" value in the collection phase so that it
  can be read back later. This means that we can now support "git config
  --get-urlmatch --show-origin" (we don't allow this combination of args
  because of this bug), but that is left unchanged for now.

* "git config --default" doesn't have config source metadata when
  displaying the default value. Fix this by treating the default value
  as if it came from the command line (e.g. like we do with "git -c" or
  "git config --file"), using kvi_from_param().

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/config.c  | 47 +++++++++++++++++----------
 config.c          | 82 +++++++++++++++++++++++------------------------
 config.h          |  3 +-
 t/t1300-config.sh | 10 +++---
 4 files changed, 78 insertions(+), 64 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index f4fccf99cb8..9b9f5527311 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -194,38 +194,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 UNUSED,
+			   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 +247,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) {
@@ -303,9 +308,10 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
 }
 
 static int collect_config(const char *key_, const char *value_,
-			  const struct config_context *ctx UNUSED, void *cb)
+			  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;
@@ -320,7 +326,7 @@ static int collect_config(const char *key_, const char *value_,
 	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)
@@ -382,11 +388,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);
 	}
@@ -563,15 +572,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,
-			       const struct config_context *ctx UNUSED,
+			       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));
@@ -580,6 +591,7 @@ static int urlmatch_collect_fn(const char *var, const char *value,
 	} else {
 		strbuf_reset(&matched->value);
 	}
+	matched->kvi = *kvi;
 
 	if (value) {
 		strbuf_addstr(&matched->value, value);
@@ -627,7 +639,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);
 
diff --git a/config.c b/config.c
index 31718711827..2e24e6ea515 100644
--- a/config.c
+++ b/config.c
@@ -219,7 +219,9 @@ 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(struct config_source *cs,
+			       const struct key_value_info *kvi,
+			       const char *path,
 			       struct config_include_data *inc)
 {
 	int ret = 0;
@@ -260,8 +262,7 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    cs->name ? cs->name :
 			    "the command line");
 		ret = git_config_from_file_with_options(git_config_include, path, inc,
-							current_config_scope(),
-							NULL);
+							kvi->scope, NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -510,7 +511,7 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, ctx->kvi, value, inc);
 
 	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
 	    cond && include_condition_is_true(cs, inc, cond, cond_len) &&
@@ -519,7 +520,7 @@ static int git_config_include(const char *var, const char *value,
 
 		if (inc->opts->unconditional_remote_url)
 			inc->fn = forbid_remote_url;
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, ctx->kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -677,27 +678,44 @@ 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, NULL, 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;
+}
+
 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])
@@ -716,12 +734,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) {
@@ -755,7 +774,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 {
@@ -780,10 +799,13 @@ int git_config_from_parameters(config_fn_t fn, void *data)
 	int ret = 0;
 	char *envw = NULL;
 	struct config_source source = CONFIG_SOURCE_INIT;
+	struct key_value_info kvi = KVI_INIT;
 
 	source.origin_type = CONFIG_ORIGIN_CMDLINE;
 	config_reader_push_source(&the_reader, &source);
 
+	kvi_from_param(&kvi);
+
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -819,7 +841,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;
 			}
@@ -830,7 +852,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;
 		}
@@ -2442,7 +2464,8 @@ 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_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2473,16 +2496,10 @@ 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) {
-		kvi_from_source(reader->source, current_config_scope(), kv_info);
+		kvi_from_source(reader->source, kvi_p->scope, kv_info);
 	} 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;
+		kvi_from_param(kv_info);
 	}
 	si->util = kv_info;
 
@@ -2538,11 +2555,12 @@ struct configset_add_data {
 #define CONFIGSET_ADD_INIT { 0 }
 
 static int config_set_callback(const char *key, const char *value,
-			       const struct config_context *ctx UNUSED,
+			       const struct config_context *ctx,
 			       void *cb)
 {
 	struct configset_add_data *data = cb;
-	configset_add_value(data->config_reader, data->config_set, key, value);
+	configset_add_value(ctx->kvi, data->config_reader, data->config_set,
+			    key, value);
 	return 0;
 }
 
@@ -4037,16 +4055,6 @@ const char *config_origin_type_name(enum config_origin_type type)
 	}
 }
 
-const char *current_config_origin_type(void)
-{
-	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
-
-	if (reader_origin_type(&the_reader, &type))
-		BUG("current_config_origin_type called outside config callback");
-
-	return config_origin_type_name(type);
-}
-
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4078,14 +4086,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	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)
diff --git a/config.h b/config.h
index 206bf1f175a..ea92392400e 100644
--- a/config.h
+++ b/config.h
@@ -387,9 +387,8 @@ void git_global_config(char **user, char **xdg);
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
 enum config_scope current_config_scope(void);
-const char *current_config_origin_type(void);
-const char *current_config_name(void);
 const char *config_origin_type_name(enum config_origin_type type);
+void kvi_from_param(struct key_value_info *out);
 
 /*
  * Match and parse a config key of the form:
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index fa6a8df2521..387d336c91f 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1676,8 +1676,8 @@ test_expect_success 'urlmatch with --show-scope' '
 	EOF
 
 	cat >expect <<-EOF &&
-	unknown	http.cookiefile /tmp/cookie.txt
-	unknown	http.sslverify false
+	local	http.cookiefile /tmp/cookie.txt
+	local	http.sslverify false
 	EOF
 	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
 	test_cmp expect actual
@@ -2071,7 +2071,9 @@ test_expect_success '--show-origin blob ref' '
 '
 
 test_expect_success '--show-origin with --default' '
-	test_must_fail git config --show-origin --default foo some.key
+	git config --show-origin --default foo some.key >actual &&
+	echo "command line:	foo" >expect &&
+	test_cmp expect actual
 '
 
 test_expect_success '--show-scope with --list' '
@@ -2144,7 +2146,7 @@ test_expect_success '--show-scope with --show-origin' '
 
 test_expect_success '--show-scope with --default' '
 	git config --show-scope --default foo some.key >actual &&
-	echo "unknown	foo" >expect &&
+	echo "command	foo" >expect &&
 	test_cmp expect actual
 '
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 08/12] trace2: plumb config kvi
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (6 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 07/12] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 09/12] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
                         ` (5 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

There is a code path starting from trace2_def_param_fl() that eventually
calls current_config_scope(), and thus it needs to have "kvi" plumbed
through it. Additional plumbing is also needed to get "kvi" to
trace2_def_param_fl(), which gets called by two code paths:

- Through tr2_cfg_cb(), which is a config callback, so it trivially
  receives "kvi" via the "struct config_context ctx" parameter.

- Through tr2_list_env_vars_fl(), which is a high level function that
  lists environment variables for tracing. This has been secretly
  behaving like git_config_from_parameters() (in that it parses config
  from environment variables/the CLI), but does not set config source
  information.

  Teach tr2_list_env_vars_fl() to be well-behaved by using
  kvi_from_param(), which is used elsewhere for CLI/environment
  variable-based config.

As a result, current_config_scope() has no more callers, so remove it.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c                | 46 -----------------------------------------
 config.h                |  1 -
 trace2.c                |  4 ++--
 trace2.h                |  3 ++-
 trace2/tr2_cfg.c        |  9 +++++---
 trace2/tr2_tgt.h        |  4 +++-
 trace2/tr2_tgt_event.c  |  4 ++--
 trace2/tr2_tgt_normal.c |  4 ++--
 trace2/tr2_tgt_perf.c   |  4 ++--
 9 files changed, 19 insertions(+), 60 deletions(-)

diff --git a/config.c b/config.c
index 2e24e6ea515..8546fea370e 100644
--- a/config.c
+++ b/config.c
@@ -85,16 +85,6 @@ struct config_reader {
 	 */
 	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
@@ -125,19 +115,9 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 static inline void config_reader_set_kvi(struct config_reader *reader,
 					 struct key_value_info *kvi)
 {
-	if (kvi && (reader->source || reader->parsing_scope))
-		BUG("kvi should not be set while parsing a config source");
 	reader->config_kvi = kvi;
 }
 
-static inline void config_reader_set_scope(struct config_reader *reader,
-					   enum config_scope scope)
-{
-	if (scope && reader->config_kvi)
-		BUG("scope should only be set when iterating through a config source");
-	reader->parsing_scope = scope;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -412,19 +392,13 @@ 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,
@@ -2255,7 +2229,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:
@@ -2273,7 +2246,6 @@ 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))
@@ -2281,7 +2253,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 							 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))
@@ -2292,13 +2263,11 @@ static int do_git_config_sequence(struct config_reader *reader,
 		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_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)) {
@@ -2307,11 +2276,9 @@ static int do_git_config_sequence(struct config_reader *reader,
 								 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);
@@ -2326,7 +2293,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) {
@@ -2340,9 +2306,6 @@ int config_with_options(config_fn_t fn, void *data,
 		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.
@@ -2364,7 +2327,6 @@ int config_with_options(config_fn_t fn, void *data,
 		string_list_clear(inc.remote_urls, 0);
 		FREE_AND_NULL(inc.remote_urls);
 	}
-	config_reader_set_scope(&the_reader, prev_scope);
 	return ret;
 }
 
@@ -4086,14 +4048,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	return 0;
 }
 
-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 lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index ea92392400e..82eeba94e71 100644
--- a/config.h
+++ b/config.h
@@ -386,7 +386,6 @@ void git_global_config(char **user, char **xdg);
 
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
-enum config_scope current_config_scope(void);
 const char *config_origin_type_name(enum config_origin_type type);
 void kvi_from_param(struct key_value_info *out);
 
diff --git a/trace2.c b/trace2.c
index 0efc4e7b958..49c23bfd05a 100644
--- a/trace2.c
+++ b/trace2.c
@@ -634,7 +634,7 @@ void trace2_thread_exit_fl(const char *file, int line)
 }
 
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value)
+			 const char *value, const struct key_value_info *kvi)
 {
 	struct tr2_tgt *tgt_j;
 	int j;
@@ -644,7 +644,7 @@ void trace2_def_param_fl(const char *file, int line, const char *param,
 
 	for_each_wanted_builtin (j, tgt_j)
 		if (tgt_j->pfn_param_fl)
-			tgt_j->pfn_param_fl(file, line, param, value);
+			tgt_j->pfn_param_fl(file, line, param, value, kvi);
 }
 
 void trace2_def_repo_fl(const char *file, int line, struct repository *repo)
diff --git a/trace2.h b/trace2.h
index 4ced30c0db3..f5c5a9e6bac 100644
--- a/trace2.h
+++ b/trace2.h
@@ -325,6 +325,7 @@ void trace2_thread_exit_fl(const char *file, int line);
 
 #define trace2_thread_exit() trace2_thread_exit_fl(__FILE__, __LINE__)
 
+struct key_value_info;
 /*
  * Emits a "def_param" message containing a key/value pair.
  *
@@ -334,7 +335,7 @@ void trace2_thread_exit_fl(const char *file, int line);
  * `core.abbrev`, `status.showUntrackedFiles`, or `--no-ahead-behind`.
  */
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value);
+			 const char *value, const struct key_value_info *kvi);
 
 #define trace2_def_param(param, value) \
 	trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 83bc4fd109c..733d9d2872a 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -100,7 +100,7 @@ struct tr2_cfg_data {
  * See if the given config key matches any of our patterns of interest.
  */
 static int tr2_cfg_cb(const char *key, const char *value,
-		      const struct config_context *ctx UNUSED, void *d)
+		      const struct config_context *ctx, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
@@ -109,7 +109,8 @@ static int tr2_cfg_cb(const char *key, const char *value,
 		struct strbuf *buf = *s;
 		int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
 		if (wm == WM_MATCH) {
-			trace2_def_param_fl(data->file, data->line, key, value);
+			trace2_def_param_fl(data->file, data->line, key, value,
+					    ctx->kvi);
 			return 0;
 		}
 	}
@@ -127,8 +128,10 @@ void tr2_cfg_list_config_fl(const char *file, int line)
 
 void tr2_list_env_vars_fl(const char *file, int line)
 {
+	struct key_value_info kvi = KVI_INIT;
 	struct strbuf **s;
 
+	kvi_from_param(&kvi);
 	if (tr2_load_env_vars() <= 0)
 		return;
 
@@ -136,7 +139,7 @@ void tr2_list_env_vars_fl(const char *file, int line)
 		struct strbuf *buf = *s;
 		const char *val = getenv(buf->buf);
 		if (val && *val)
-			trace2_def_param_fl(file, line, buf->buf, val);
+			trace2_def_param_fl(file, line, buf->buf, val, &kvi);
 	}
 }
 
diff --git a/trace2/tr2_tgt.h b/trace2/tr2_tgt.h
index bf8745c4f05..1f626cffea0 100644
--- a/trace2/tr2_tgt.h
+++ b/trace2/tr2_tgt.h
@@ -69,8 +69,10 @@ typedef void(tr2_tgt_evt_exec_result_fl_t)(const char *file, int line,
 					   uint64_t us_elapsed_absolute,
 					   int exec_id, int code);
 
+struct key_value_info;
 typedef void(tr2_tgt_evt_param_fl_t)(const char *file, int line,
-				     const char *param, const char *value);
+				     const char *param, const char *value,
+				     const struct key_value_info *kvi);
 
 typedef void(tr2_tgt_evt_repo_fl_t)(const char *file, int line,
 				    const struct repository *repo);
diff --git a/trace2/tr2_tgt_event.c b/trace2/tr2_tgt_event.c
index 2af53e5d4de..53091781eca 100644
--- a/trace2/tr2_tgt_event.c
+++ b/trace2/tr2_tgt_event.c
@@ -477,11 +477,11 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct json_writer jw = JSON_WRITER_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	jw_object_begin(&jw, 0);
diff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c
index 1ebfb464d54..d25ea131643 100644
--- a/trace2/tr2_tgt_normal.c
+++ b/trace2/tr2_tgt_normal.c
@@ -297,10 +297,10 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	struct strbuf buf_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
diff --git a/trace2/tr2_tgt_perf.c b/trace2/tr2_tgt_perf.c
index 328e483a05e..a6f9a8a193e 100644
--- a/trace2/tr2_tgt_perf.c
+++ b/trace2/tr2_tgt_perf.c
@@ -439,12 +439,12 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct strbuf buf_payload = STRBUF_INIT;
 	struct strbuf scope_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "%s:%s", param, value);
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 09/12] config: pass kvi to die_bad_number()
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (7 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 08/12] trace2: plumb config kvi Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 10/12] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
                         ` (4 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Plumb "struct key_value_info" through all code paths that end in
die_bad_number(), which lets us remove the helper functions that read
analogous values from "struct config_reader". As a result, nothing reads
config_reader.config_kvi any more, so remove that too.

In config.c, this requires changing the signature of
git_configset_get_value() to 'return' "kvi" in an out parameter so that
git_configset_get_<type>() can pass it to git_config_<type>(). Only
numeric types will use "kvi", so for non-numeric types (e.g.
git_configset_get_string()), pass NULL to indicate that the out
parameter isn't needed.

Outside of config.c, config callbacks now need to pass "ctx->kvi" to any
of the git_config_<type>() functions that parse a config string into a
number type. Included is a .cocci patch to make that refactor.

The only exceptional case is builtin/config.c, where git_config_<type>()
is called outside of a config callback (namely, on user-provided input),
so config source information has never been available. In this case,
die_bad_number() defaults to a generic, but perfectly descriptive
message. Let's provide a safe, non-NULL for "kvi" anyway, but make sure
not to change the message.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 archive-tar.c                              |   4 +-
 builtin/commit-graph.c                     |   4 +-
 builtin/commit.c                           |  10 +-
 builtin/config.c                           |  21 +--
 builtin/fetch.c                            |   4 +-
 builtin/fsmonitor--daemon.c                |   6 +-
 builtin/grep.c                             |   2 +-
 builtin/index-pack.c                       |   4 +-
 builtin/log.c                              |   2 +-
 builtin/pack-objects.c                     |  14 +-
 builtin/receive-pack.c                     |  10 +-
 builtin/submodule--helper.c                |   4 +-
 config.c                                   | 156 ++++++++-------------
 config.h                                   |  17 ++-
 contrib/coccinelle/git_config_number.cocci |  27 ++++
 diff.c                                     |   9 +-
 fmt-merge-msg.c                            |   2 +-
 help.c                                     |   4 +-
 http.c                                     |  10 +-
 imap-send.c                                |   2 +-
 sequencer.c                                |  22 +--
 setup.c                                    |   2 +-
 submodule-config.c                         |  13 +-
 submodule-config.h                         |   3 +-
 t/helper/test-config.c                     |   6 +-
 upload-pack.c                              |  12 +-
 worktree.c                                 |   2 +-
 27 files changed, 190 insertions(+), 182 deletions(-)
 create mode 100644 contrib/coccinelle/git_config_number.cocci

diff --git a/archive-tar.c b/archive-tar.c
index ef06e516b1f..3df8af6d1b1 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -412,14 +412,14 @@ static int tar_filter_config(const char *var, const char *value,
 }
 
 static int git_tar_config(const char *var, const char *value,
-			  const struct config_context *ctx UNUSED, void *cb)
+			  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
 			tar_umask = umask(0);
 			umask(tar_umask);
 		} else {
-			tar_umask = git_config_int(var, value);
+			tar_umask = git_config_int(var, value, ctx->kvi);
 		}
 		return 0;
 	}
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 1185c49239a..b071c5ab646 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -186,11 +186,11 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
-					 const struct config_context *ctx UNUSED,
+					 const struct config_context *ctx,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
-		write_opts.max_new_filters = git_config_int(var, value);
+		write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
 	/*
 	 * No need to fall-back to 'git_default_config', since this was already
 	 * called in 'cmd_commit_graph()'.
diff --git a/builtin/commit.c b/builtin/commit.c
index 6a2b2503328..9fe691470a3 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1415,7 +1415,8 @@ static int git_status_config(const char *k, const char *v,
 		return git_column_config(k, v, "status", &s->colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
-		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
+		s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
+							      &is_bool);
 		if (is_bool && s->submodule_summary)
 			s->submodule_summary = -1;
 		return 0;
@@ -1475,11 +1476,11 @@ static int git_status_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "diff.renamelimit")) {
 		if (s->rename_limit == -1)
-			s->rename_limit = git_config_int(k, v);
+			s->rename_limit = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "status.renamelimit")) {
-		s->rename_limit = git_config_int(k, v);
+		s->rename_limit = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "diff.renames")) {
@@ -1625,7 +1626,8 @@ static int git_commit_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "commit.verbose")) {
 		int is_bool;
-		config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
+		config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
+							       &is_bool);
 		return 0;
 	}
 
diff --git a/builtin/config.c b/builtin/config.c
index 9b9f5527311..680269d263c 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -262,13 +262,14 @@ static int format_config(struct strbuf *buf, const char *key_,
 
 		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
@@ -424,7 +425,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;
@@ -439,12 +441,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
@@ -674,6 +676,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));
 
@@ -891,7 +894,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"
@@ -900,7 +903,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);
@@ -908,7 +911,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,
@@ -917,7 +920,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);
diff --git a/builtin/fetch.c b/builtin/fetch.c
index a4aa0fbb8f5..fe86dfeb62a 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -137,7 +137,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "submodule.fetchjobs")) {
-		fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v);
+		fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v, ctx->kvi);
 		return 0;
 	} else if (!strcmp(k, "fetch.recursesubmodules")) {
 		fetch_config->recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
@@ -145,7 +145,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "fetch.parallel")) {
-		fetch_config->parallel = git_config_int(k, v);
+		fetch_config->parallel = git_config_int(k, v, ctx->kvi);
 		if (fetch_config->parallel < 0)
 			die(_("fetch.parallel cannot be negative"));
 		if (!fetch_config->parallel)
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 91a776e2f17..6d2826b07da 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -41,7 +41,7 @@ static int fsmonitor_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, ctx->kvi);
 		if (i < 1)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__IPC_THREADS, i);
@@ -50,7 +50,7 @@ static int fsmonitor_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, FSMONITOR__START_TIMEOUT)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, ctx->kvi);
 		if (i < 0)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__START_TIMEOUT, i);
@@ -60,7 +60,7 @@ static int fsmonitor_config(const char *var, const char *value,
 
 	if (!strcmp(var, FSMONITOR__ANNOUNCE_STARTUP)) {
 		int is_bool;
-		int i = git_config_bool_or_int(var, value, &is_bool);
+		int i = git_config_bool_or_int(var, value, ctx->kvi, &is_bool);
 		if (i < 0)
 			return error(_("value of '%s' not bool or int: %d"),
 				     var, i);
diff --git a/builtin/grep.c b/builtin/grep.c
index 757d52b94ec..3a464e6faab 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -301,7 +301,7 @@ static int grep_cmd_config(const char *var, const char *value,
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
-		num_threads = git_config_int(var, value);
+		num_threads = git_config_int(var, value, ctx->kvi);
 		if (num_threads < 0)
 			die(_("invalid number of threads specified (%d) for %s"),
 			    num_threads, var);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index de8884ea5c2..e428f6d9a4a 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1587,13 +1587,13 @@ static int git_index_pack_config(const char *k, const char *v,
 	struct pack_idx_option *opts = cb;
 
 	if (!strcmp(k, "pack.indexversion")) {
-		opts->version = git_config_int(k, v);
+		opts->version = git_config_int(k, v, ctx->kvi);
 		if (opts->version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32), opts->version);
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		nr_threads = git_config_int(k, v);
+		nr_threads = git_config_int(k, v, ctx->kvi);
 		if (nr_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    nr_threads);
diff --git a/builtin/log.c b/builtin/log.c
index 09d6a13075b..c7337354aaf 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -574,7 +574,7 @@ static int git_log_config(const char *var, const char *value,
 	if (!strcmp(var, "format.subjectprefix"))
 		return git_config_string(&fmt_patch_subject_prefix, var, value);
 	if (!strcmp(var, "format.filenamemaxlength")) {
-		fmt_patch_name_max = git_config_int(var, value);
+		fmt_patch_name_max = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(var, "format.encodeemailheaders")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 34aa0b483a0..38054a38b2b 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3139,23 +3139,23 @@ static int git_pack_config(const char *k, const char *v,
 			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
-		window = git_config_int(k, v);
+		window = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.windowmemory")) {
-		window_memory_limit = git_config_ulong(k, v);
+		window_memory_limit = git_config_ulong(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.depth")) {
-		depth = git_config_int(k, v);
+		depth = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachesize")) {
-		max_delta_cache_size = git_config_int(k, v);
+		max_delta_cache_size = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachelimit")) {
-		cache_max_small_delta_size = git_config_int(k, v);
+		cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.writebitmaphashcache")) {
@@ -3181,7 +3181,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		delta_search_threads = git_config_int(k, v);
+		delta_search_threads = git_config_int(k, v, ctx->kvi);
 		if (delta_search_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    delta_search_threads);
@@ -3192,7 +3192,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.indexversion")) {
-		pack_idx_opts.version = git_config_int(k, v);
+		pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
 		if (pack_idx_opts.version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32),
 			    pack_idx_opts.version);
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 94d9898aff7..98f6f0038f0 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -158,12 +158,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.unpacklimit") == 0) {
-		receive_unpack_limit = git_config_int(var, value);
+		receive_unpack_limit = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "transfer.unpacklimit") == 0) {
-		transfer_unpack_limit = git_config_int(var, value);
+		transfer_unpack_limit = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -231,7 +231,7 @@ static int receive_pack_config(const char *var, const char *value,
 		return git_config_string(&cert_nonce_seed, var, value);
 
 	if (strcmp(var, "receive.certnonceslop") == 0) {
-		nonce_stamp_slop_limit = git_config_ulong(var, value);
+		nonce_stamp_slop_limit = git_config_ulong(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -246,12 +246,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.keepalive") == 0) {
-		keepalive_in_sec = git_config_int(var, value);
+		keepalive_in_sec = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "receive.maxinputsize") == 0) {
-		max_input_size = git_config_int64(var, value);
+		max_input_size = git_config_int64(var, value, ctx->kvi);
 		return 0;
 	}
 
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index f8e9d85e77a..57185cf5f3d 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2192,13 +2192,13 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
-				   const struct config_context *ctx UNUSED,
+				   const struct config_context *ctx,
 				   void *cb)
 {
 	int *max_jobs = cb;
 
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
 	return 0;
 }
 
diff --git a/config.c b/config.c
index 8546fea370e..a9a45e4ffe0 100644
--- a/config.c
+++ b/config.c
@@ -73,18 +73,8 @@ struct config_reader {
 	 *
 	 * 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;
 };
 /*
  * Where possible, prefer to accept "struct config_reader" as an arg than to use
@@ -96,8 +86,6 @@ static struct config_reader the_reader;
 static inline void config_reader_push_source(struct config_reader *reader,
 					     struct config_source *top)
 {
-	if (reader->config_kvi)
-		BUG("source should not be set while iterating a config set");
 	top->prev = reader->source;
 	reader->source = top;
 }
@@ -112,12 +100,6 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 	return ret;
 }
 
-static inline void config_reader_set_kvi(struct config_reader *reader,
-					 struct key_value_info *kvi)
-{
-	reader->config_kvi = kvi;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -1346,80 +1328,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;
 }
 
@@ -1524,7 +1504,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) {
@@ -1532,7 +1513,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)
@@ -1658,7 +1639,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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;
@@ -1670,7 +1651,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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)
@@ -1681,7 +1662,7 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	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)
@@ -1695,7 +1676,7 @@ static int git_default_core_config(const char *var, const char *value,
 
 	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;
@@ -1706,17 +1687,17 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	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;
 	}
 
@@ -1995,12 +1976,12 @@ int git_default_config(const char *var, const char *value,
 	}
 
 	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)
@@ -2344,13 +2325,11 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 		value_index = list->items[i].value_index;
 		values = &entry->value_list;
 
-		config_reader_set_kvi(reader, values->items[value_index].util);
 		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,
 					      ctx.kvi->filename,
 					      ctx.kvi->linenr);
-		config_reader_set_kvi(reader, NULL);
 	}
 }
 
@@ -2534,11 +2513,12 @@ int git_configset_add_file(struct config_set *set, const char *filename)
 	return git_config_from_file(config_set_callback, filename, &data);
 }
 
-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
@@ -2548,7 +2528,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;
 }
 
@@ -2601,7 +2584,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;
@@ -2611,7 +2594,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;
@@ -2624,8 +2607,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;
@@ -2634,8 +2619,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;
@@ -2644,7 +2631,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
@@ -2655,8 +2642,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;
@@ -2665,7 +2654,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;
@@ -2677,7 +2666,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;
@@ -2747,7 +2736,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,
@@ -3987,18 +3976,6 @@ 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 *config_origin_type_name(enum config_origin_type type)
 {
 	switch (type) {
@@ -4037,17 +4014,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;
-}
-
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index 82eeba94e71..bd366ddb5ef 100644
--- a/config.h
+++ b/config.h
@@ -249,22 +249,26 @@ int git_parse_maybe_bool(const char *);
  * Parse the string to an integer, including unit factors. Dies on error;
  * otherwise, returns the parsed result.
  */
-int git_config_int(const char *, const char *);
+int git_config_int(const char *, const char *, const struct key_value_info *);
 
-int64_t git_config_int64(const char *, const char *);
+int64_t git_config_int64(const char *, const char *,
+			 const struct key_value_info *);
 
 /**
  * Identical to `git_config_int`, but for unsigned longs.
  */
-unsigned long git_config_ulong(const char *, const char *);
+unsigned long git_config_ulong(const char *, const char *,
+			       const struct key_value_info *);
 
-ssize_t git_config_ssize_t(const char *, const char *);
+ssize_t git_config_ssize_t(const char *, const char *,
+			   const struct key_value_info *);
 
 /**
  * Same as `git_config_bool`, except that integers are returned as-is, and
  * an `is_bool` flag is unset.
  */
-int git_config_bool_or_int(const char *, const char *, int *);
+int git_config_bool_or_int(const char *, const char *,
+			   const struct key_value_info *, int *);
 
 /**
  * Parse a string into a boolean value, respecting keywords like "true" and
@@ -529,7 +533,8 @@ int git_configset_get(struct config_set *cs, const char *key);
  * touching `value`. The caller should not free or modify `value`, as it
  * is owned by the cache.
  */
-int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
+int git_configset_get_value(struct config_set *cs, const char *key,
+			    const char **dest, struct key_value_info *kvi);
 
 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
diff --git a/contrib/coccinelle/git_config_number.cocci b/contrib/coccinelle/git_config_number.cocci
new file mode 100644
index 00000000000..7b57dceefe6
--- /dev/null
+++ b/contrib/coccinelle/git_config_number.cocci
@@ -0,0 +1,27 @@
+@@
+identifier C1, C2, C3;
+@@
+(
+(
+git_config_int
+|
+git_config_int64
+|
+git_config_ulong
+|
+git_config_ssize_t
+)
+  (C1, C2
++ , ctx->kvi
+  )
+|
+(
+git_configset_get_value
+|
+git_config_bool_or_int
+)
+  (C1, C2
++ , ctx->kvi
+ , C3
+  )
+)
diff --git a/diff.c b/diff.c
index 0e382c8f7f0..460211e7d40 100644
--- a/diff.c
+++ b/diff.c
@@ -379,13 +379,14 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.context")) {
-		diff_context_default = git_config_int(var, value);
+		diff_context_default = git_config_int(var, value, ctx->kvi);
 		if (diff_context_default < 0)
 			return -1;
 		return 0;
 	}
 	if (!strcmp(var, "diff.interhunkcontext")) {
-		diff_interhunk_context_default = git_config_int(var, value);
+		diff_interhunk_context_default = git_config_int(var, value,
+								ctx->kvi);
 		if (diff_interhunk_context_default < 0)
 			return -1;
 		return 0;
@@ -411,7 +412,7 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.statgraphwidth")) {
-		diff_stat_graph_width = git_config_int(var, value);
+		diff_stat_graph_width = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(var, "diff.external"))
@@ -450,7 +451,7 @@ int git_diff_basic_config(const char *var, const char *value,
 	const char *name;
 
 	if (!strcmp(var, "diff.renamelimit")) {
-		diff_rename_limit_default = git_config_int(var, value);
+		diff_rename_limit_default = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 10137444321..3ae59bde2f2 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -25,7 +25,7 @@ int fmt_merge_msg_config(const char *key, const char *value,
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
-		merge_log_config = git_config_bool_or_int(key, value, &is_bool);
+		merge_log_config = git_config_bool_or_int(key, value, ctx->kvi, &is_bool);
 		if (!is_bool && merge_log_config < 0)
 			return error("%s: negative length %s", key, value);
 		if (is_bool && merge_log_config)
diff --git a/help.c b/help.c
index ac0ae5ac0dc..389382b1482 100644
--- a/help.c
+++ b/help.c
@@ -545,7 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
-				  const struct config_context *ctx UNUSED,
+				  const struct config_context *ctx,
 				  void *cb UNUSED)
 {
 	const char *p;
@@ -560,7 +560,7 @@ static int git_unknown_cmd_config(const char *var, const char *value,
 		} else if (!strcmp(value, "prompt")) {
 			autocorrect = AUTOCORRECT_PROMPT;
 		} else {
-			int v = git_config_int(var, value);
+			int v = git_config_int(var, value, ctx->kvi);
 			autocorrect = (v < 0)
 				? AUTOCORRECT_IMMEDIATELY : v;
 		}
diff --git a/http.c b/http.c
index 762502828c9..b12b234bde1 100644
--- a/http.c
+++ b/http.c
@@ -414,21 +414,21 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.minsessions", var)) {
-		min_curl_sessions = git_config_int(var, value);
+		min_curl_sessions = git_config_int(var, value, ctx->kvi);
 		if (min_curl_sessions > 1)
 			min_curl_sessions = 1;
 		return 0;
 	}
 	if (!strcmp("http.maxrequests", var)) {
-		max_requests = git_config_int(var, value);
+		max_requests = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedlimit", var)) {
-		curl_low_speed_limit = (long)git_config_int(var, value);
+		curl_low_speed_limit = (long)git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedtime", var)) {
-		curl_low_speed_time = (long)git_config_int(var, value);
+		curl_low_speed_time = (long)git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -464,7 +464,7 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.postbuffer", var)) {
-		http_post_buffer = git_config_ssize_t(var, value);
+		http_post_buffer = git_config_ssize_t(var, value, ctx->kvi);
 		if (http_post_buffer < 0)
 			warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
 		if (http_post_buffer < LARGE_PACKET_MAX)
diff --git a/imap-send.c b/imap-send.c
index 47777e76861..3518a4ace6c 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1342,7 +1342,7 @@ static int git_imap_config(const char *var, const char *val,
 	else if (!strcmp("imap.authmethod", var))
 		return git_config_string(&server.auth_method, var, val);
 	else if (!strcmp("imap.port", var))
-		server.port = git_config_int(var, val);
+		server.port = git_config_int(var, val, ctx->kvi);
 	else if (!strcmp("imap.host", var)) {
 		if (!val) {
 			git_die_config("imap.host", "Missing value for 'imap.host'");
diff --git a/sequencer.c b/sequencer.c
index 34754d17596..a0aaee3056d 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2883,7 +2883,7 @@ static int git_config_string_dup(char **dest,
 }
 
 static int populate_opts_cb(const char *key, const char *value,
-			    const struct config_context *ctx UNUSED,
+			    const struct config_context *ctx,
 			    void *data)
 {
 	struct replay_opts *opts = data;
@@ -2892,26 +2892,26 @@ static int populate_opts_cb(const char *key, const char *value,
 	if (!value)
 		error_flag = 0;
 	else if (!strcmp(key, "options.no-commit"))
-		opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
+		opts->no_commit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.edit"))
-		opts->edit = git_config_bool_or_int(key, value, &error_flag);
+		opts->edit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty"))
 		opts->allow_empty =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty-message"))
 		opts->allow_empty_message =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.keep-redundant-commits"))
 		opts->keep_redundant_commits =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.signoff"))
-		opts->signoff = git_config_bool_or_int(key, value, &error_flag);
+		opts->signoff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.record-origin"))
-		opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
+		opts->record_origin = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-ff"))
-		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
+		opts->allow_ff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
-		opts->mainline = git_config_int(key, value);
+		opts->mainline = git_config_int(key, value, ctx->kvi);
 	else if (!strcmp(key, "options.strategy"))
 		git_config_string_dup(&opts->strategy, key, value);
 	else if (!strcmp(key, "options.gpg-sign"))
@@ -2920,7 +2920,7 @@ static int populate_opts_cb(const char *key, const char *value,
 		strvec_push(&opts->xopts, value);
 	} else if (!strcmp(key, "options.allow-rerere-auto"))
 		opts->allow_rerere_auto =
-			git_config_bool_or_int(key, value, &error_flag) ?
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag) ?
 				RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
 	else if (!strcmp(key, "options.default-msg-cleanup")) {
 		opts->explicit_cleanup = 1;
diff --git a/setup.c b/setup.c
index fadba5bab4b..ca80fb464bb 100644
--- a/setup.c
+++ b/setup.c
@@ -597,7 +597,7 @@ static int check_repo_format(const char *var, const char *value,
 	const char *ext;
 
 	if (strcmp(var, "core.repositoryformatversion") == 0)
-		data->version = git_config_int(var, value);
+		data->version = git_config_int(var, value, ctx->kvi);
 	else if (skip_prefix(var, "extensions.", &ext)) {
 		switch (handle_extension_v0(var, value, ext, data)) {
 		case EXTENSION_ERROR:
diff --git a/submodule-config.c b/submodule-config.c
index 3f25bd13674..54be580e2a9 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -304,9 +304,10 @@ static int parse_fetch_recurse(const char *opt, const char *arg,
 	}
 }
 
-int parse_submodule_fetchjobs(const char *var, const char *value)
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      const struct key_value_info *kvi)
 {
-	int fetchjobs = git_config_int(var, value);
+	int fetchjobs = git_config_int(var, value, kvi);
 	if (fetchjobs < 0)
 		die(_("negative values not allowed for submodule.fetchJobs"));
 	if (!fetchjobs)
@@ -849,14 +850,14 @@ struct fetch_config {
 };
 
 static int gitmodules_fetch_config(const char *var, const char *value,
-				   const struct config_context *ctx UNUSED,
+				   const struct config_context *ctx,
 				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
 		if (config->max_children)
 			*(config->max_children) =
-				parse_submodule_fetchjobs(var, value);
+				parse_submodule_fetchjobs(var, value, ctx->kvi);
 		return 0;
 	} else if (!strcmp(var, "fetch.recursesubmodules")) {
 		if (config->recurse_submodules)
@@ -878,12 +879,12 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
-					  const struct config_context *ctx UNUSED,
+					  const struct config_context *ctx,
 					  void *cb)
 {
 	int *max_jobs = cb;
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
 	return 0;
 }
 
diff --git a/submodule-config.h b/submodule-config.h
index c2045875bbb..2a37689cc27 100644
--- a/submodule-config.h
+++ b/submodule-config.h
@@ -50,7 +50,8 @@ struct repository;
 
 void submodule_cache_free(struct submodule_cache *cache);
 
-int parse_submodule_fetchjobs(const char *var, const char *value);
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      const struct key_value_info *kvi);
 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 struct option;
 int option_fetch_parse_recurse_submodules(const struct option *opt,
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 3f4c3678318..ed444ca4c25 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -63,12 +63,12 @@ static int iterate_cb(const char *var, const char *value,
 }
 
 static int parse_int_cb(const char *var, const char *value,
-			const struct config_context *ctx UNUSED, void *data)
+			const struct config_context *ctx, void *data)
 {
 	const char *key_to_match = data;
 
 	if (!strcmp(key_to_match, var)) {
-		int parsed = git_config_int(value, value);
+		int parsed = git_config_int(value, value, ctx->kvi);
 		printf("%d\n", parsed);
 	}
 	return 0;
@@ -182,7 +182,7 @@ int cmd__config(int argc, const char **argv)
 				goto exit2;
 			}
 		}
-		if (!git_configset_get_value(&cs, argv[2], &v)) {
+		if (!git_configset_get_value(&cs, argv[2], &v, NULL)) {
 			if (!v)
 				printf("(NULL)\n");
 			else
diff --git a/upload-pack.c b/upload-pack.c
index 951fd1f9c25..c03415a5460 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1275,7 +1275,8 @@ static int find_symref(const char *refname,
 }
 
 static int parse_object_filter_config(const char *var, const char *value,
-				       struct upload_pack_data *data)
+				      const struct key_value_info *kvi,
+				      struct upload_pack_data *data)
 {
 	struct strbuf buf = STRBUF_INIT;
 	const char *sub, *key;
@@ -1302,7 +1303,8 @@ static int parse_object_filter_config(const char *var, const char *value,
 		}
 		string_list_insert(&data->allowed_filters, buf.buf)->util =
 			(void *)(intptr_t)1;
-		data->tree_filter_max_depth = git_config_ulong(var, value);
+		data->tree_filter_max_depth = git_config_ulong(var, value,
+							       kvi);
 	}
 
 	strbuf_release(&buf);
@@ -1310,7 +1312,7 @@ static int parse_object_filter_config(const char *var, const char *value,
 }
 
 static int upload_pack_config(const char *var, const char *value,
-			      const struct config_context *ctx UNUSED,
+			      const struct config_context *ctx,
 			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
@@ -1331,7 +1333,7 @@ static int upload_pack_config(const char *var, const char *value,
 		else
 			data->allow_uor &= ~ALLOW_ANY_SHA1;
 	} else if (!strcmp("uploadpack.keepalive", var)) {
-		data->keepalive = git_config_int(var, value);
+		data->keepalive = git_config_int(var, value, ctx->kvi);
 		if (!data->keepalive)
 			data->keepalive = -1;
 	} else if (!strcmp("uploadpack.allowfilter", var)) {
@@ -1346,7 +1348,7 @@ static int upload_pack_config(const char *var, const char *value,
 		data->advertise_sid = git_config_bool(var, value);
 	}
 
-	if (parse_object_filter_config(var, value, data) < 0)
+	if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
 		return -1;
 
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
diff --git a/worktree.c b/worktree.c
index c448fecd4b3..af277ec9010 100644
--- a/worktree.c
+++ b/worktree.c
@@ -835,7 +835,7 @@ int init_worktree_config(struct repository *r)
 	 * Relocate that value to avoid breaking all worktrees with this
 	 * upgrade to worktree config.
 	 */
-	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
+	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
 		if ((res = move_config_setting("core.worktree", core_worktree,
 					       common_config_file,
 					       main_worktree_file)))
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 10/12] config.c: remove config_reader from configsets
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (8 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 09/12] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 11/12] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
                         ` (3 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Remove the last usage of "struct config_reader" from configsets by
copying the "kvi" arg instead of recomputing "kvi" from
config_reader.source. Since we no longer need to pass both "struct
config_reader" and "struct config_set" in a single "void *cb", remove
"struct configset_add_data" too.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 43 +++++++++++--------------------------------
 1 file changed, 11 insertions(+), 32 deletions(-)

diff --git a/config.c b/config.c
index a9a45e4ffe0..4782a289363 100644
--- a/config.c
+++ b/config.c
@@ -2311,8 +2311,7 @@ int config_with_options(config_fn_t fn, void *data,
 	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;
@@ -2406,7 +2405,6 @@ static int configset_find_element(struct config_set *set, const char *key,
 }
 
 static int configset_add_value(const struct key_value_info *kvi_p,
-			       struct config_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2437,11 +2435,7 @@ static int configset_add_value(const struct key_value_info *kvi_p,
 	l_item->e = e;
 	l_item->value_index = e->value_list.nr - 1;
 
-	if (reader->source->name) {
-		kvi_from_source(reader->source, kvi_p->scope, kv_info);
-	} else {
-		kvi_from_param(kv_info);
-	}
+	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
 	si->util = kv_info;
 
 	return 0;
@@ -2489,28 +2483,18 @@ 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,
 			       const struct config_context *ctx,
 			       void *cb)
 {
-	struct configset_add_data *data = cb;
-	configset_add_value(ctx->kvi, 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,
@@ -2676,7 +2660,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;
@@ -2688,10 +2671,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
@@ -2723,7 +2704,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)
@@ -2830,19 +2811,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' */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 11/12] config: add kvi.path, use it to evaluate includes
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (9 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 10/12] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 18:11       ` [PATCH v4 12/12] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
                         ` (2 subsequent siblings)
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Include directives are evaluated using the path of the config file. To
reduce the dependence on "config_reader.source", add a new
"key_value_info.path" member and use that instead of
"config_source.path". This allows us to remove a "struct config_reader
*" field from "struct config_include_data", which will subsequently
allow us to remove "struct config_reader" entirely.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 40 +++++++++++++++++++---------------------
 config.h |  2 ++
 2 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/config.c b/config.c
index 4782a289363..070245065f4 100644
--- a/config.c
+++ b/config.c
@@ -162,7 +162,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.
@@ -181,8 +180,7 @@ 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 struct key_value_info *kvi,
+static int handle_path_include(const struct key_value_info *kvi,
 			       const char *path,
 			       struct config_include_data *inc)
 {
@@ -205,14 +203,14 @@ static int handle_path_include(struct config_source *cs,
 	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;
 	}
@@ -220,8 +218,8 @@ static int handle_path_include(struct config_source *cs,
 	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_with_options(git_config_include, path, inc,
 							kvi->scope, NULL);
@@ -239,7 +237,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;
@@ -256,11 +254,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?");
@@ -275,7 +273,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)
 {
@@ -292,7 +290,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)
@@ -428,16 +426,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,
@@ -453,7 +451,6 @@ static int git_config_include(const char *var, const char *value,
 			      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;
@@ -467,16 +464,16 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, ctx->kvi, 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, ctx->kvi, value, inc);
+		ret = handle_path_include(ctx->kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -661,6 +658,7 @@ void kvi_from_param(struct key_value_info *out)
 	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,
@@ -1064,6 +1062,7 @@ static void kvi_from_source(struct config_source *cs,
 	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,
@@ -2282,7 +2281,6 @@ 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;
 	}
diff --git a/config.h b/config.h
index bd366ddb5ef..b5573e67a00 100644
--- a/config.h
+++ b/config.h
@@ -116,12 +116,14 @@ struct key_value_info {
 	int linenr;
 	enum config_origin_type origin_type;
 	enum config_scope scope;
+	const char *path;
 };
 #define KVI_INIT { \
 	.filename = NULL, \
 	.linenr = -1, \
 	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
 	.scope = CONFIG_SCOPE_UNKNOWN, \
+	.path = NULL, \
 }
 
 /* Captures additional information that a config callback can use. */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v4 12/12] config: pass source to config_parser_event_fn_t
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (10 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 11/12] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
@ 2023-06-26 18:11       ` Glen Choo via GitGitGadget
  2023-06-26 20:45       ` [PATCH v4 00/12] config: remove global state from config iteration Junio C Hamano
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
  13 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-26 18:11 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

..so that the callback can use a "struct config_source" parameter
instead of "config_reader.source". "struct config_source" is internal to
config.c, so we are adding a pointer to a struct defined in config.c
into a public function signature defined in config.h, but this is okay
because this function has only ever been (and probably ever will be)
used internally by config.c.

As a result, the_reader isn't used anywhere, so "struct config_reader"
is obsolete (it was only intended to be used with the_reader). Remove
them.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 77 ++++++++++----------------------------------------------
 config.h |  6 +++++
 2 files changed, 19 insertions(+), 64 deletions(-)

diff --git a/config.c b/config.c
index 070245065f4..2ddeb5e2685 100644
--- a/config.c
+++ b/config.c
@@ -66,40 +66,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).
-	 */
-	struct config_source *source;
-};
-/*
- * 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 int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -752,14 +718,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;
 	struct key_value_info kvi = KVI_INIT;
 
-	source.origin_type = CONFIG_ORIGIN_CMDLINE;
-	config_reader_push_source(&the_reader, &source);
-
 	kvi_from_param(&kvi);
-
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -816,7 +777,6 @@ out:
 	strbuf_release(&envvar);
 	strvec_clear(&to_free);
 	free(envw);
-	config_reader_pop_source(&the_reader);
 	return ret;
 }
 
@@ -1045,7 +1005,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;
@@ -2002,8 +1962,7 @@ int git_default_config(const char *var, const char *value,
  * 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,
+static int do_config_from(struct config_source *top, config_fn_t fn,
 			  void *data, enum config_scope scope,
 			  const struct config_options *opts)
 {
@@ -2016,21 +1975,17 @@ 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, &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, enum config_scope scope,
@@ -2049,7 +2004,7 @@ 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, scope, opts);
+	ret = do_config_from(&top, fn, data, scope, opts);
 	funlockfile(f);
 	return ret;
 }
@@ -2057,8 +2012,8 @@ static int do_config_from_file(struct config_reader *reader,
 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, scope, 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,
@@ -2072,9 +2027,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, scope,
-					  opts);
+		ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
+					  filename, f, data, scope, opts);
 		fclose(f);
 	}
 	return ret;
@@ -2105,7 +2059,7 @@ 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, scope, opts);
+	return do_config_from(&top, fn, data, scope, opts);
 }
 
 int git_config_from_blob_oid(config_fn_t fn,
@@ -2198,8 +2152,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)
 {
@@ -2299,7 +2252,7 @@ int config_with_options(config_fn_t fn, void *data,
 		ret = git_config_from_blob_ref(fn, repo, config_source->blob,
 					       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) {
@@ -3009,7 +2962,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;
@@ -3055,11 +3007,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;
@@ -3380,8 +3331,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)
diff --git a/config.h b/config.h
index b5573e67a00..6332d749047 100644
--- a/config.h
+++ b/config.h
@@ -72,6 +72,7 @@ enum config_event_t {
 	CONFIG_EVENT_ERROR
 };
 
+struct config_source;
 /*
  * The parser event function (if not NULL) is called with the event type and
  * the begin/end offsets of the parsed elements.
@@ -81,6 +82,7 @@ enum config_event_t {
  */
 typedef int (*config_parser_event_fn_t)(enum config_event_t type,
 					size_t begin_offset, size_t end_offset,
+					struct config_source *cs,
 					void *event_fn_data);
 
 struct config_options {
@@ -100,6 +102,10 @@ struct config_options {
 
 	const char *commondir;
 	const char *git_dir;
+	/*
+	 * event_fn and event_fn_data are for internal use only. Handles events
+	 * emitted by the config parser.
+	 */
 	config_parser_event_fn_t event_fn;
 	void *event_fn_data;
 	enum config_error_action {
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH v4 00/12] config: remove global state from config iteration
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (11 preceding siblings ...)
  2023-06-26 18:11       ` [PATCH v4 12/12] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
@ 2023-06-26 20:45       ` Junio C Hamano
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
  13 siblings, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-26 20:45 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: git, Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:

> I also tested these patches on top of 'seen', and the only conflict I found was
> with en/header-split-cache-h-part-3, where ll-merge.c got renamed to merge-ll.c,
> so we need to apply the config refactor there (should work with .cocci).

Both recursive and ort merge strategies should be able to deal with
ll-merge.c <-> merge-ll.c, I think.

Thanks.


^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v3 06/12] builtin/config.c: test misuse of format_config()
  2023-06-23 20:32       ` Jonathan Tan
  2023-06-24  1:31         ` Jeff King
@ 2023-06-28 17:28         ` Glen Choo
  1 sibling, 0 replies; 115+ messages in thread
From: Glen Choo @ 2023-06-28 17:28 UTC (permalink / raw)
  To: Jonathan Tan, Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood

Jonathan Tan <jonathantanmy@google.com> writes:

> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> +test_expect_success '--show-origin with --default' '
>> +	test_must_fail git config --show-origin --default foo some.key
>> +'
>
> On my machine, this fails with
>
>   BUG: config.c:4035: current_config_origin_type called outside config callback
>   /usr/local/google/home/jonathantanmy/git/t/test-lib-functions.sh: line 1067: 3255109 Aborted                 "$@" 2>&7
>   test_must_fail: died by signal 6: git config --show-origin --default foo some.key
>
> (So it indeed fails, as expected, but test_must_fail seems to not like
> the exit code.)

Ah you're right. I was under the impression that this was doing the
right thing on MacOS, but it doesn't work there either.

I think a good way to assert that we run into BUG() (Maybe
test_match_signal on the numeric value of SIGABRT? But that sounds error
prone), so I'll either use test_expect_failure or squash this patch.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v5 00/11] config: remove global state from config iteration
  2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
                         ` (12 preceding siblings ...)
  2023-06-26 20:45       ` [PATCH v4 00/12] config: remove global state from config iteration Junio C Hamano
@ 2023-06-28 19:26       ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 01/11] config: inline git_color_default_config Glen Choo via GitGitGadget
                           ` (12 more replies)
  13 siblings, 13 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo

As promised, this version addresses the comments on v3.

= Changes since v4

- Squash 6-7/12 since `test_must_fail` doesn't catch BUG()
- Move a hunk to later in the series where it belongs
- Replace a memcpy with `*a = *b`

= Changes since v3

- Rebase onto newer 'master'
- Move the 'remove UNUSED from tr2_cfg_cb' hunk from 9/12 -> 8/12. It should
  have been there all along; v3 8/12 didn't build at all.


Glen Choo (11):
  config: inline git_color_default_config
  urlmatch.h: use config_fn_t type
  config: add ctx arg to config_fn_t
  config.c: pass ctx in configsets
  config: pass ctx with config files
  config.c: pass ctx with CLI config
  trace2: plumb config kvi
  config: pass kvi to die_bad_number()
  config.c: remove config_reader from configsets
  config: add kvi.path, use it to evaluate includes
  config: pass source to config_parser_event_fn_t

 alias.c                                       |   3 +-
 archive-tar.c                                 |   5 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   8 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   8 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |  12 +-
 builtin/clean.c                               |   9 +-
 builtin/clone.c                               |  11 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   3 +-
 builtin/commit.c                              |  20 +-
 builtin/config.c                              |  72 ++-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |  13 +-
 builtin/fsmonitor--daemon.c                   |  11 +-
 builtin/grep.c                                |  12 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   9 +-
 builtin/log.c                                 |  12 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |  19 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |  15 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |  15 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   8 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   3 +-
 builtin/tag.c                                 |   9 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   9 +-
 color.c                                       |   8 -
 color.h                                       |   6 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      | 552 +++++++-----------
 config.h                                      |  80 ++-
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_ctx.pending.cocci    | 144 +++++
 contrib/coccinelle/git_config_number.cocci    |  27 +
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   4 +-
 diff.c                                        |  19 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   7 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |  12 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   7 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |   9 +-
 http.c                                        |  15 +-
 ident.c                                       |   4 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   7 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   1 +
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   4 +-
 notes.c                                       |   4 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   8 +-
 revision.c                                    |   4 +-
 scalar.c                                      |   4 +-
 sequencer.c                                   |  29 +-
 setup.c                                       |  18 +-
 submodule-config.c                            |  31 +-
 submodule-config.h                            |   3 +-
 t/helper/test-config.c                        |  24 +-
 t/helper/test-userdiff.c                      |   4 +-
 t/t1300-config.sh                             |  27 +
 trace2.c                                      |   4 +-
 trace2.h                                      |   3 +-
 trace2/tr2_cfg.c                              |  16 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trace2/tr2_tgt.h                              |   4 +-
 trace2/tr2_tgt_event.c                        |   4 +-
 trace2/tr2_tgt_normal.c                       |   4 +-
 trace2/tr2_tgt_perf.c                         |   4 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |  18 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   8 +-
 worktree.c                                    |   2 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   4 +-
 103 files changed, 960 insertions(+), 638 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci
 create mode 100644 contrib/coccinelle/git_config_number.cocci


base-commit: 6ff334181cfb6485d3ba50843038209a2a253907
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1497%2Fchooglen%2Fconfig%2Fno-global-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1497/chooglen/config/no-global-v5
Pull-Request: https://github.com/git/git/pull/1497

Range-diff vs v4:

  1:  7bfffb454c5 =  1:  7bfffb454c5 config: inline git_color_default_config
  2:  739c519ce62 =  2:  739c519ce62 urlmatch.h: use config_fn_t type
  3:  a9a0a50f32a =  3:  a9a0a50f32a config: add ctx arg to config_fn_t
  4:  39b2e291f86 =  4:  39b2e291f86 config.c: pass ctx in configsets
  5:  bfc6d2833c5 =  5:  bfc6d2833c5 config: pass ctx with config files
  6:  897bdc759b5 <  -:  ----------- builtin/config.c: test misuse of format_config()
  7:  33e4437737d !  6:  7b24eefbcf3 config.c: pass ctx with CLI config
     @@ Commit message
          * git_config_parse_parameter() hasn't been setting config source
            information, so plumb "kvi" there too.
      
     -    * "git config --get-urlmatch --show-scope" iterates config to collect
     -      values, but then attempts to display the scope after config iteration.
     -      Fix this by copying the "kvi" value in the collection phase so that it
     -      can be read back later. This means that we can now support "git config
     -      --get-urlmatch --show-origin" (we don't allow this combination of args
     -      because of this bug), but that is left unchanged for now.
     +    * Several sites in builtin/config.c have been calling current_config_*()
     +      functions outside of config callbacks (indirectly, via the
     +      format_config() helper), which means they're reading state that isn't
     +      set correctly:
      
     -    * "git config --default" doesn't have config source metadata when
     -      displaying the default value. Fix this by treating the default value
     -      as if it came from the command line (e.g. like we do with "git -c" or
     -      "git config --file"), using kvi_from_param().
     +      * "git config --get-urlmatch --show-scope" iterates config to collect
     +        values, but then attempts to display the scope after config
     +        iteration, causing the "unknown" scope to be shown instead of the
     +        config file's scope. It's clear that this wasn't intended: we knew
     +        that "--get-urlmatch" couldn't show config source metadata, which is
     +        why "--show-origin" was marked incompatible with "--get-urlmatch"
     +        when it was introduced [1]. It was most likely a mistake that we
     +        allowed "--show-scope" to sneak through.
     +
     +        Fix this by copying the "kvi" value in the collection phase so that
     +        it can be read back later. This means that we can now support "git
     +        config --get-urlmatch --show-origin", but that is left unchanged
     +        for now.
     +
     +      * "git config --default" doesn't have config source metadata when
     +        displaying the default value, so "--show-scope" also results in
     +        "unknown", and "--show-origin" results in a BUG(). Fix this by
     +        treating the default value as if it came from the command line (e.g.
     +        like we do with "git -c" or "git config --file"), using
     +        kvi_from_param().
     +
     +    [1] https://lore.kernel.org/git/20160205112001.GA13397@sigill.intra.peff.net/
      
          Signed-off-by: Glen Choo <chooglen@google.com>
      
     @@ config.c: static int configset_find_element(struct config_set *set, const char *
       			       const char *value)
       {
      @@ config.c: 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)
     + 		BUG("configset_add_value has no source");
       	if (reader->source->name) {
      -		kvi_from_source(reader->source, current_config_scope(), kv_info);
      +		kvi_from_source(reader->source, kvi_p->scope, kv_info);
     @@ config.h: void git_global_config(char **user, char **xdg);
        * Match and parse a config key of the form:
      
       ## t/t1300-config.sh ##
     -@@ t/t1300-config.sh: test_expect_success 'urlmatch with --show-scope' '
     - 	EOF
     +@@ t/t1300-config.sh: test_expect_success 'urlmatch' '
     + 	test_cmp expect actual
     + '
       
     - 	cat >expect <<-EOF &&
     --	unknown	http.cookiefile /tmp/cookie.txt
     --	unknown	http.sslverify false
     ++test_expect_success 'urlmatch with --show-scope' '
     ++	cat >.git/config <<-\EOF &&
     ++	[http "https://weak.example.com"]
     ++		sslVerify = false
     ++		cookieFile = /tmp/cookie.txt
     ++	EOF
     ++
     ++	cat >expect <<-EOF &&
      +	local	http.cookiefile /tmp/cookie.txt
      +	local	http.sslverify false
     - 	EOF
     - 	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
     - 	test_cmp expect actual
     ++	EOF
     ++	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
     ++	test_cmp expect actual
     ++'
     ++
     + test_expect_success 'urlmatch favors more specific URLs' '
     + 	cat >.git/config <<-\EOF &&
     + 	[http "https://example.com/"]
      @@ t/t1300-config.sh: test_expect_success '--show-origin blob ref' '
     + 	test_cmp expect output
       '
       
     - test_expect_success '--show-origin with --default' '
     --	test_must_fail git config --show-origin --default foo some.key
     ++test_expect_success '--show-origin with --default' '
      +	git config --show-origin --default foo some.key >actual &&
      +	echo "command line:	foo" >expect &&
      +	test_cmp expect actual
     - '
     - 
     ++'
     ++
       test_expect_success '--show-scope with --list' '
     + 	cat >expect <<-EOF &&
     + 	global	user.global=true
      @@ t/t1300-config.sh: test_expect_success '--show-scope with --show-origin' '
     - 
     - test_expect_success '--show-scope with --default' '
     - 	git config --show-scope --default foo some.key >actual &&
     --	echo "unknown	foo" >expect &&
     -+	echo "command	foo" >expect &&
     - 	test_cmp expect actual
     + 	test_cmp expect output
       '
       
     ++test_expect_success '--show-scope with --default' '
     ++	git config --show-scope --default foo some.key >actual &&
     ++	echo "command	foo" >expect &&
     ++	test_cmp expect actual
     ++'
     ++
     + test_expect_success 'override global and system config' '
     + 	test_when_finished rm -f \"\$HOME\"/.gitconfig &&
     + 	cat >"$HOME"/.gitconfig <<-EOF &&
  8:  9bd5f60282c =  7:  7d64dcbdade trace2: plumb config kvi
  9:  114723ee4a7 =  8:  9e71c10ca0a config: pass kvi to die_bad_number()
 10:  807057b6d7f !  9:  4776600e790 config.c: remove config_reader from configsets
     @@ config.c: static int configset_add_value(const struct key_value_info *kvi_p,
       	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) {
      -		kvi_from_source(reader->source, kvi_p->scope, kv_info);
      -	} else {
      -		kvi_from_param(kv_info);
      -	}
     -+	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
     ++	*kv_info = *kvi_p;
       	si->util = kv_info;
       
       	return 0;
 11:  3f0f84df972 = 10:  2b33977aba6 config: add kvi.path, use it to evaluate includes
 12:  fe2f154fe8b = 11:  8347d3c9b80 config: pass source to config_parser_event_fn_t

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 115+ messages in thread

* [PATCH v5 01/11] config: inline git_color_default_config
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 02/11] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
                           ` (11 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

git_color_default_config() is a shorthand for calling two other config
callbacks. There are no other non-static functions that do this and it
will complicate our refactoring of config_fn_t so inline it instead.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/add.c         | 5 ++++-
 builtin/branch.c      | 5 ++++-
 builtin/clean.c       | 6 ++++--
 builtin/grep.c        | 5 ++++-
 builtin/show-branch.c | 5 ++++-
 builtin/tag.c         | 6 +++++-
 color.c               | 8 --------
 color.h               | 6 +-----
 8 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 6137e7b4ad7..e01efdfc50d 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -365,7 +365,10 @@ static int add_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char embedded_advice[] = N_(
diff --git a/builtin/branch.c b/builtin/branch.c
index 075e580d224..8337b9e71bb 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -117,7 +117,10 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/clean.c b/builtin/clean.c
index 78852d28cec..57e7f7cac64 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -130,8 +130,10 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	/* inspect the color.ui config variable and others */
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/grep.c b/builtin/grep.c
index b86c754defb..76cf999d310 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -293,7 +293,10 @@ static int wait_all(void)
 static int grep_cmd_config(const char *var, const char *value, void *cb)
 {
 	int st = grep_config(var, value, cb);
-	if (git_color_default_config(var, value, NULL) < 0)
+
+	if (git_color_config(var, value, cb) < 0)
+		st = -1;
+	else if (git_default_config(var, value, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 7ef4a642c17..a2461270d4b 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -579,7 +579,10 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_color_default_config(var, value, cb);
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/tag.c b/builtin/tag.c
index 49b64c7a288..1acf5f7a59f 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -209,7 +209,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 
 	if (starts_with(var, "column."))
 		return git_column_config(var, value, "tag", &colopts);
-	return git_color_default_config(var, value, cb);
+
+	if (git_color_config(var, value, cb) < 0)
+		return -1;
+
+	return git_default_config(var, value, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/color.c b/color.c
index 83abb11eda0..b24b19566b9 100644
--- a/color.c
+++ b/color.c
@@ -430,14 +430,6 @@ int git_color_config(const char *var, const char *value, void *cb UNUSED)
 	return 0;
 }
 
-int git_color_default_config(const char *var, const char *value, void *cb)
-{
-	if (git_color_config(var, value, cb) < 0)
-		return -1;
-
-	return git_default_config(var, value, cb);
-}
-
 void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb)
 {
 	if (*color)
diff --git a/color.h b/color.h
index cfc8f841b23..bb28343be21 100644
--- a/color.h
+++ b/color.h
@@ -88,12 +88,8 @@ extern const int column_colors_ansi_max;
  */
 extern int color_stdout_is_tty;
 
-/*
- * Use the first one if you need only color config; the second is a convenience
- * if you are just going to change to git_default_config, too.
- */
+/* Parse color config. */
 int git_color_config(const char *var, const char *value, void *cb);
-int git_color_default_config(const char *var, const char *value, void *cb);
 
 /*
  * Parse a config option, which can be a boolean or one of
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 02/11] urlmatch.h: use config_fn_t type
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 01/11] config: inline git_color_default_config Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 03/11] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
                           ` (10 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

These are actually used as config callbacks, so use the typedef-ed type
and make future refactors easier.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 urlmatch.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/urlmatch.h b/urlmatch.h
index 9f40b00bfb8..bee374a642c 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -2,6 +2,7 @@
 #define URL_MATCH_H
 
 #include "string-list.h"
+#include "config.h"
 
 struct url_info {
 	/* normalized url on success, must be freed, otherwise NULL */
@@ -48,8 +49,8 @@ struct urlmatch_config {
 	const char *key;
 
 	void *cb;
-	int (*collect_fn)(const char *var, const char *value, void *cb);
-	int (*cascade_fn)(const char *var, const char *value, void *cb);
+	config_fn_t collect_fn;
+	config_fn_t cascade_fn;
 	/*
 	 * Compare the two matches, the one just discovered and the existing
 	 * best match and return a negative value if the found item is to be
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 03/11] config: add ctx arg to config_fn_t
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 01/11] config: inline git_color_default_config Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 02/11] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 04/11] config.c: pass ctx in configsets Glen Choo via GitGitGadget
                           ` (9 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Add a new "const struct config_context *ctx" arg to config_fn_t to hold
additional information about the config iteration operation.
config_context has a "struct key_value_info kvi" member that holds
metadata about the config source being read (e.g. what kind of config
source it is, the filename, etc). In this series, we're only interested
in .kvi, so we could have just used "struct key_value_info" as an arg,
but config_context makes it possible to add/adjust members in the future
without changing the config_fn_t signature. We could also consider other
ways of organizing the args (e.g. moving the config name and value into
config_context or key_value_info), but in my experiments, the
incremental benefit doesn't justify the added complexity (e.g. a
config_fn_t will sometimes invoke another config_fn_t but with a
different config value).

In subsequent commits, the .kvi member will replace the global "struct
config_reader" in config.c, making config iteration a global-free
operation. It requires much more work for the machinery to provide
meaningful values of .kvi, so for now, merely change the signature and
call sites, pass NULL as a placeholder value, and don't rely on the arg
in any meaningful way.

Most of the changes are performed by
contrib/coccinelle/config_fn_ctx.pending.cocci, which, for every
config_fn_t:

- Modifies the signature to accept "const struct config_context *ctx"
- Passes "ctx" to any inner config_fn_t, if needed
- Adds UNUSED attributes to "ctx", if needed

Most config_fn_t instances are easily identified by seeing if they are
called by the various config functions. Most of the remaining ones are
manually named in the .cocci patch. Manual cleanups are still needed,
but the majority of it is trivial; it's either adjusting config_fn_t
that the .cocci patch didn't catch, or adding forward declarations of
"struct config_context ctx" to make the signatures make sense.

The non-trivial changes are in cases where we are invoking a config_fn_t
outside of config machinery, and we now need to decide what value of
"ctx" to pass. These cases are:

- trace2/tr2_cfg.c:tr2_cfg_set_fl()

  This is indirectly called by git_config_set() so that the trace2
  machinery can notice the new config values and update its settings
  using the tr2 config parsing function, i.e. tr2_cfg_cb().

- builtin/checkout.c:checkout_main()

  This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
  This might be worth refactoring away in the future, since
  git_xmerge_config() can call git_default_config(), which can do much
  more than just parsing.

Handle them by creating a KVI_INIT macro that initializes "struct
key_value_info" to a reasonable default, and use that to construct the
"ctx" arg.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 alias.c                                       |   3 +-
 archive-tar.c                                 |   3 +-
 archive-zip.c                                 |   1 +
 builtin/add.c                                 |   5 +-
 builtin/blame.c                               |   5 +-
 builtin/branch.c                              |   5 +-
 builtin/cat-file.c                            |   5 +-
 builtin/checkout.c                            |  12 +-
 builtin/clean.c                               |   5 +-
 builtin/clone.c                               |  11 +-
 builtin/column.c                              |   3 +-
 builtin/commit-graph.c                        |   1 +
 builtin/commit.c                              |  10 +-
 builtin/config.c                              |  10 +-
 builtin/difftool.c                            |   5 +-
 builtin/fetch.c                               |   9 +-
 builtin/fsmonitor--daemon.c                   |   5 +-
 builtin/grep.c                                |   7 +-
 builtin/help.c                                |   5 +-
 builtin/index-pack.c                          |   5 +-
 builtin/log.c                                 |  10 +-
 builtin/merge.c                               |   7 +-
 builtin/multi-pack-index.c                    |   1 +
 builtin/pack-objects.c                        |   5 +-
 builtin/patch-id.c                            |   5 +-
 builtin/pull.c                                |   5 +-
 builtin/push.c                                |   5 +-
 builtin/read-tree.c                           |   5 +-
 builtin/rebase.c                              |   5 +-
 builtin/receive-pack.c                        |   5 +-
 builtin/reflog.c                              |   7 +-
 builtin/remote.c                              |   7 +-
 builtin/repack.c                              |   5 +-
 builtin/reset.c                               |   5 +-
 builtin/send-pack.c                           |   5 +-
 builtin/show-branch.c                         |   5 +-
 builtin/stash.c                               |   5 +-
 builtin/submodule--helper.c                   |   1 +
 builtin/tag.c                                 |   5 +-
 builtin/var.c                                 |   5 +-
 builtin/worktree.c                            |   5 +-
 bundle-uri.c                                  |   8 +-
 compat/mingw.c                                |   3 +-
 compat/mingw.h                                |   4 +-
 config.c                                      |  38 +++--
 config.h                                      |  41 +++--
 connect.c                                     |   4 +-
 .../coccinelle/config_fn_ctx.pending.cocci    | 144 ++++++++++++++++++
 convert.c                                     |   4 +-
 credential.c                                  |   1 +
 delta-islands.c                               |   4 +-
 diff.c                                        |  10 +-
 diff.h                                        |   7 +-
 fetch-pack.c                                  |   5 +-
 fmt-merge-msg.c                               |   5 +-
 fmt-merge-msg.h                               |   3 +-
 fsck.c                                        |   9 +-
 fsck.h                                        |   4 +-
 git-compat-util.h                             |   2 +
 gpg-interface.c                               |   7 +-
 grep.c                                        |   7 +-
 grep.h                                        |   4 +-
 help.c                                        |   7 +-
 http.c                                        |   5 +-
 ident.c                                       |   4 +-
 ident.h                                       |   4 +-
 imap-send.c                                   |   5 +-
 ll-merge.c                                    |   1 +
 ls-refs.c                                     |   1 +
 mailinfo.c                                    |   5 +-
 notes-utils.c                                 |   4 +-
 notes.c                                       |   4 +-
 pager.c                                       |   5 +-
 pretty.c                                      |   1 +
 promisor-remote.c                             |   4 +-
 remote.c                                      |   3 +-
 revision.c                                    |   4 +-
 scalar.c                                      |   4 +-
 sequencer.c                                   |   9 +-
 setup.c                                       |  16 +-
 submodule-config.c                            |  17 ++-
 t/helper/test-config.c                        |  11 +-
 t/helper/test-userdiff.c                      |   4 +-
 trace2/tr2_cfg.c                              |   9 +-
 trace2/tr2_sysenv.c                           |   3 +-
 trailer.c                                     |   2 +
 upload-pack.c                                 |   8 +-
 urlmatch.c                                    |   7 +-
 urlmatch.h                                    |   3 +-
 xdiff-interface.c                             |   5 +-
 xdiff-interface.h                             |   4 +-
 91 files changed, 515 insertions(+), 181 deletions(-)
 create mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci

diff --git a/alias.c b/alias.c
index 54a1a23d2cf..910dd252a01 100644
--- a/alias.c
+++ b/alias.c
@@ -12,7 +12,8 @@ struct config_alias_data {
 	struct string_list *list;
 };
 
-static int config_alias_cb(const char *key, const char *value, void *d)
+static int config_alias_cb(const char *key, const char *value,
+			   const struct config_context *ctx UNUSED, void *d)
 {
 	struct config_alias_data *data = d;
 	const char *p;
diff --git a/archive-tar.c b/archive-tar.c
index 4cd81d8161e..ef06e516b1f 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -411,7 +411,8 @@ static int tar_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int git_tar_config(const char *var, const char *value, void *cb)
+static int git_tar_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
diff --git a/archive-zip.c b/archive-zip.c
index d0d065a312e..b6811951955 100644
--- a/archive-zip.c
+++ b/archive-zip.c
@@ -617,6 +617,7 @@ static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
 }
 
 static int archive_zip_config(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
 			      void *data UNUSED)
 {
 	return userdiff_config(var, value);
diff --git a/builtin/add.c b/builtin/add.c
index e01efdfc50d..1009ae13c13 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -357,7 +357,8 @@ static struct option builtin_add_options[] = {
 	OPT_END(),
 };
 
-static int add_config(const char *var, const char *value, void *cb)
+static int add_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "add.ignoreerrors") ||
 	    !strcmp(var, "add.ignore-errors")) {
@@ -368,7 +369,7 @@ static int add_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char embedded_advice[] = N_(
diff --git a/builtin/blame.c b/builtin/blame.c
index 2df6039a6e0..d0970a1ab13 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -694,7 +694,8 @@ static const char *add_prefix(const char *prefix, const char *path)
 	return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 }
 
-static int git_blame_config(const char *var, const char *value, void *cb)
+static int git_blame_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "blame.showroot")) {
 		show_root = git_config_bool(var, value);
@@ -767,7 +768,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
diff --git a/builtin/branch.c b/builtin/branch.c
index 8337b9e71bb..af6d2e75fb0 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -83,7 +83,8 @@ static unsigned int colopts;
 
 define_list_config_array(color_branch_slots);
 
-static int git_branch_config(const char *var, const char *value, void *cb)
+static int git_branch_config(const char *var, const char *value,
+			     const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -120,7 +121,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char *branch_get_color(enum color_branch ix)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 7ff56d5a781..b3c41b54c8d 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -873,12 +873,13 @@ static int batch_objects(struct batch_options *opt)
 	return retval;
 }
 
-static int git_cat_file_config(const char *var, const char *value, void *cb)
+static int git_cat_file_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int batch_option_callback(const struct option *opt,
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 715eeb5048f..4e1f7dc26c2 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1186,7 +1186,8 @@ static int switch_branches(const struct checkout_opts *opts,
 	return ret || writeout_error;
 }
 
-static int git_checkout_config(const char *var, const char *value, void *cb)
+static int git_checkout_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	struct checkout_opts *opts = cb;
 
@@ -1202,7 +1203,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "submodule."))
 		return git_default_submodule_config(var, value, NULL);
 
-	return git_xmerge_config(var, value, NULL);
+	return git_xmerge_config(var, value, ctx, NULL);
 }
 
 static void setup_new_branch_info_and_source_tree(
@@ -1689,8 +1690,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 	}
 
 	if (opts->conflict_style) {
+		struct key_value_info kvi = KVI_INIT;
+		struct config_context ctx = {
+			.kvi = &kvi,
+		};
 		opts->merge = 1; /* implied */
-		git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
+		git_xmerge_config("merge.conflictstyle", opts->conflict_style,
+				  &ctx, NULL);
 	}
 	if (opts->force) {
 		opts->discard_changes = 1;
diff --git a/builtin/clean.c b/builtin/clean.c
index 57e7f7cac64..5eff1b802a7 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -103,7 +103,8 @@ struct menu_stuff {
 
 define_list_config_array(color_interactive_slots);
 
-static int git_clean_config(const char *var, const char *value, void *cb)
+static int git_clean_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -133,7 +134,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static const char *clean_get_color(enum color_clean ix)
diff --git a/builtin/clone.c b/builtin/clone.c
index 15f9912b4ca..c0f6e067493 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -790,7 +790,8 @@ static int checkout(int submodule_progress, int filter_submodules)
 	return err;
 }
 
-static int git_clone_config(const char *k, const char *v, void *cb)
+static int git_clone_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "clone.defaultremotename")) {
 		free(remote_name);
@@ -801,17 +802,19 @@ static int git_clone_config(const char *k, const char *v, void *cb)
 	if (!strcmp(k, "clone.filtersubmodules"))
 		config_filter_submodules = git_config_bool(k, v);
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
-static int write_one_config(const char *key, const char *value, void *data)
+static int write_one_config(const char *key, const char *value,
+			    const struct config_context *ctx,
+			    void *data)
 {
 	/*
 	 * give git_clone_config a chance to write config values back to the
 	 * environment, since git_config_set_multivar_gently only deals with
 	 * config-file writes
 	 */
-	int apply_failed = git_clone_config(key, value, data);
+	int apply_failed = git_clone_config(key, value, ctx, data);
 	if (apply_failed)
 		return apply_failed;
 
diff --git a/builtin/column.c b/builtin/column.c
index de623a16c2d..4a6148ca479 100644
--- a/builtin/column.c
+++ b/builtin/column.c
@@ -13,7 +13,8 @@ static const char * const builtin_column_usage[] = {
 };
 static unsigned int colopts;
 
-static int column_config(const char *var, const char *value, void *cb)
+static int column_config(const char *var, const char *value,
+			 const struct config_context *ctx UNUSED, void *cb)
 {
 	return git_column_config(var, value, cb, &colopts);
 }
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index dd732b35348..1185c49239a 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -186,6 +186,7 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
+					 const struct config_context *ctx UNUSED,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
diff --git a/builtin/commit.c b/builtin/commit.c
index 9ab57ea1aaa..6a2b2503328 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1405,7 +1405,8 @@ static int parse_status_slot(const char *slot)
 	return LOOKUP_CONFIG(color_status_slots, slot);
 }
 
-static int git_status_config(const char *k, const char *v, void *cb)
+static int git_status_config(const char *k, const char *v,
+			     const struct config_context *ctx, void *cb)
 {
 	struct wt_status *s = cb;
 	const char *slot_name;
@@ -1490,7 +1491,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
 		s->detect_rename = git_config_rename(k, v);
 		return 0;
 	}
-	return git_diff_ui_config(k, v, NULL);
+	return git_diff_ui_config(k, v, ctx, NULL);
 }
 
 int cmd_status(int argc, const char **argv, const char *prefix)
@@ -1605,7 +1606,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int git_commit_config(const char *k, const char *v, void *cb)
+static int git_commit_config(const char *k, const char *v,
+			     const struct config_context *ctx, void *cb)
 {
 	struct wt_status *s = cb;
 
@@ -1627,7 +1629,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_status_config(k, v, s);
+	return git_status_config(k, v, ctx, s);
 }
 
 int cmd_commit(int argc, const char **argv, const char *prefix)
diff --git a/builtin/config.c b/builtin/config.c
index d40fddb042a..f4fccf99cb8 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -217,6 +217,7 @@ static void show_config_scope(struct strbuf *buf)
 }
 
 static int show_all_config(const char *key_, const char *value_,
+			   const struct config_context *ctx UNUSED,
 			   void *cb UNUSED)
 {
 	if (show_origin || show_scope) {
@@ -301,7 +302,8 @@ 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 UNUSED, void *cb)
 {
 	struct strbuf_list *values = cb;
 
@@ -470,6 +472,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)) {
@@ -503,6 +506,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))
@@ -561,7 +565,9 @@ struct urlmatch_current_candidate_value {
 	struct strbuf value;
 };
 
-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 UNUSED,
+			       void *cb)
 {
 	struct string_list *values = cb;
 	struct string_list_item *item = string_list_insert(values, var);
diff --git a/builtin/difftool.c b/builtin/difftool.c
index 0049342f5c0..f289530068b 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -40,14 +40,15 @@ static const char *const builtin_difftool_usage[] = {
 	NULL
 };
 
-static int difftool_config(const char *var, const char *value, void *cb)
+static int difftool_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "difftool.trustexitcode")) {
 		trust_exit_code = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int print_tool_help(void)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index e3871048cf6..a4aa0fbb8f5 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -110,7 +110,8 @@ struct fetch_config {
 	int submodule_fetch_jobs;
 };
 
-static int git_fetch_config(const char *k, const char *v, void *cb)
+static int git_fetch_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	struct fetch_config *fetch_config = cb;
 
@@ -164,7 +165,7 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
 			    "fetch.output", v);
 	}
 
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
@@ -1799,7 +1800,9 @@ struct remote_group_data {
 	struct string_list *list;
 };
 
-static int get_remote_group(const char *key, const char *value, void *priv)
+static int get_remote_group(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *priv)
 {
 	struct remote_group_data *g = priv;
 
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index f6dd9a784c1..91a776e2f17 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -37,7 +37,8 @@ static int fsmonitor__start_timeout_sec = 60;
 #define FSMONITOR__ANNOUNCE_STARTUP "fsmonitor.announcestartup"
 static int fsmonitor__announce_startup = 0;
 
-static int fsmonitor_config(const char *var, const char *value, void *cb)
+static int fsmonitor_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
 		int i = git_config_int(var, value);
@@ -67,7 +68,7 @@ static int fsmonitor_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/builtin/grep.c b/builtin/grep.c
index 76cf999d310..757d52b94ec 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -290,13 +290,14 @@ static int wait_all(void)
 	return hit;
 }
 
-static int grep_cmd_config(const char *var, const char *value, void *cb)
+static int grep_cmd_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
-	int st = grep_config(var, value, cb);
+	int st = grep_config(var, value, ctx, cb);
 
 	if (git_color_config(var, value, cb) < 0)
 		st = -1;
-	else if (git_default_config(var, value, cb) < 0)
+	else if (git_default_config(var, value, ctx, cb) < 0)
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
diff --git a/builtin/help.c b/builtin/help.c
index d3cf4af3f6e..c348f201254 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -398,7 +398,8 @@ static int add_man_viewer_info(const char *var, const char *value)
 	return 0;
 }
 
-static int git_help_config(const char *var, const char *value, void *cb)
+static int git_help_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "help.format")) {
 		if (!value)
@@ -421,7 +422,7 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	if (starts_with(var, "man."))
 		return add_man_viewer_info(var, value);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static struct cmdnames main_cmds, other_cmds;
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index d0d8067510b..de8884ea5c2 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1581,7 +1581,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
 	strbuf_release(&pack_name);
 }
 
-static int git_index_pack_config(const char *k, const char *v, void *cb)
+static int git_index_pack_config(const char *k, const char *v,
+				 const struct config_context *ctx, void *cb)
 {
 	struct pack_idx_option *opts = cb;
 
@@ -1608,7 +1609,7 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
 		else
 			opts->flags &= ~WRITE_REV;
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 static int cmp_uint32(const void *a_, const void *b_)
diff --git a/builtin/log.c b/builtin/log.c
index c85f13a5d5e..09d6a13075b 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -564,7 +564,8 @@ static int cmd_log_walk(struct rev_info *rev)
 	return retval;
 }
 
-static int git_log_config(const char *var, const char *value, void *cb)
+static int git_log_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 
@@ -613,7 +614,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_diff_ui_config(var, value, cb);
+	return git_diff_ui_config(var, value, ctx, cb);
 }
 
 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
@@ -979,7 +980,8 @@ static enum cover_from_description parse_cover_from_description(const char *arg)
 		die(_("%s: invalid cover from description mode"), arg);
 }
 
-static int git_format_config(const char *var, const char *value, void *cb)
+static int git_format_config(const char *var, const char *value,
+			     const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "format.headers")) {
 		if (!value)
@@ -1108,7 +1110,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "diff.noprefix"))
 		return 0;
 
-	return git_log_config(var, value, cb);
+	return git_log_config(var, value, ctx, cb);
 }
 
 static const char *output_directory = NULL;
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb0..cad624fb797 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -623,7 +623,8 @@ static void parse_branch_merge_options(char *bmo)
 	free(argv);
 }
 
-static int git_merge_config(const char *k, const char *v, void *cb)
+static int git_merge_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	int status;
 	const char *str;
@@ -668,10 +669,10 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	status = fmt_merge_msg_config(k, v, cb);
+	status = fmt_merge_msg_config(k, v, ctx, cb);
 	if (status)
 		return status;
-	return git_diff_ui_config(k, v, cb);
+	return git_diff_ui_config(k, v, ctx, cb);
 }
 
 static int read_tree_trivial(struct object_id *common, struct object_id *head,
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 1b5083f8b26..a0a7b82cc69 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -82,6 +82,7 @@ static struct option *add_common_options(struct option *prev)
 }
 
 static int git_multi_pack_index_write_config(const char *var, const char *value,
+					     const struct config_context *ctx UNUSED,
 					     void *cb UNUSED)
 {
 	if (!strcmp(var, "pack.writebitmaphashcache")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 3af2d84f589..34aa0b483a0 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3135,7 +3135,8 @@ static void prepare_pack(int window, int depth)
 	free(delta_list);
 }
 
-static int git_pack_config(const char *k, const char *v, void *cb)
+static int git_pack_config(const char *k, const char *v,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
 		window = git_config_int(k, v);
@@ -3227,7 +3228,7 @@ static int git_pack_config(const char *k, const char *v, void *cb)
 		ex->uri = xstrdup(pack_end + 1);
 		oidmap_put(&configured_exclusions, ex);
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 /* Counters for trace2 output when in --stdin-packs mode. */
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 9d5585d3a72..03eddd0fb82 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -196,7 +196,8 @@ struct patch_id_opts {
 	int verbatim;
 };
 
-static int git_patch_id_config(const char *var, const char *value, void *cb)
+static int git_patch_id_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	struct patch_id_opts *opts = cb;
 
@@ -209,7 +210,7 @@ static int git_patch_id_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_patch_id(int argc, const char **argv, const char *prefix)
diff --git a/builtin/pull.c b/builtin/pull.c
index 0c7bac97b75..83fca5b1d46 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -361,7 +361,8 @@ static enum rebase_type config_get_rebase(int *rebase_unspecified)
 /**
  * Read config variables.
  */
-static int git_pull_config(const char *var, const char *value, void *cb)
+static int git_pull_config(const char *var, const char *value,
+			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "rebase.autostash")) {
 		config_autostash = git_config_bool(var, value);
@@ -374,7 +375,7 @@ static int git_pull_config(const char *var, const char *value, void *cb)
 		check_trust_level = 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /**
diff --git a/builtin/push.c b/builtin/push.c
index dbdf609daf3..a2f68f77324 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -510,7 +510,8 @@ static void set_push_cert_flags(int *flags, int v)
 }
 
 
-static int git_push_config(const char *k, const char *v, void *cb)
+static int git_push_config(const char *k, const char *v,
+			   const struct config_context *ctx, void *cb)
 {
 	const char *slot_name;
 	int *flags = cb;
@@ -577,7 +578,7 @@ static int git_push_config(const char *k, const char *v, void *cb)
 		return 0;
 	}
 
-	return git_default_config(k, v, NULL);
+	return git_default_config(k, v, ctx, NULL);
 }
 
 int cmd_push(int argc, const char **argv, const char *prefix)
diff --git a/builtin/read-tree.c b/builtin/read-tree.c
index 440f19b1b87..8877dd6d4b5 100644
--- a/builtin/read-tree.c
+++ b/builtin/read-tree.c
@@ -102,12 +102,13 @@ static int debug_merge(const struct cache_entry * const *stages,
 	return 0;
 }
 
-static int git_read_tree_config(const char *var, const char *value, void *cb)
+static int git_read_tree_config(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d11..60930e2d8e0 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -772,7 +772,8 @@ static void parse_rebase_merges_value(struct rebase_options *options, const char
 		die(_("Unknown rebase-merges mode: %s"), value);
 }
 
-static int rebase_config(const char *var, const char *value, void *data)
+static int rebase_config(const char *var, const char *value,
+			 const struct config_context *ctx, void *data)
 {
 	struct rebase_options *opts = data;
 
@@ -831,7 +832,7 @@ static int rebase_config(const char *var, const char *value, void *data)
 		return git_config_string(&opts->default_backend, var, value);
 	}
 
-	return git_default_config(var, value, data);
+	return git_default_config(var, value, ctx, data);
 }
 
 static int checkout_up_to_date(struct rebase_options *options)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 1a31a583674..94d9898aff7 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -139,7 +139,8 @@ static enum deny_action parse_deny_action(const char *var, const char *value)
 	return DENY_IGNORE;
 }
 
-static int receive_pack_config(const char *var, const char *value, void *cb)
+static int receive_pack_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
 
@@ -266,7 +267,7 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void show_ref(const char *path, const struct object_id *oid)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index a1fa0c855f4..84251cc9517 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -108,7 +108,8 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
 #define EXPIRE_TOTAL   01
 #define EXPIRE_UNREACH 02
 
-static int reflog_expire_config(const char *var, const char *value, void *cb)
+static int reflog_expire_config(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	const char *pattern, *key;
 	size_t pattern_len;
@@ -117,7 +118,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 	struct reflog_expire_cfg *ent;
 
 	if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value, ctx, cb);
 
 	if (!strcmp(key, "reflogexpire")) {
 		slot = EXPIRE_TOTAL;
@@ -128,7 +129,7 @@ static int reflog_expire_config(const char *var, const char *value, void *cb)
 		if (git_config_expiry_date(&expire, var, value))
 			return -1;
 	} else
-		return git_default_config(var, value, cb);
+		return git_default_config(var, value, ctx, cb);
 
 	if (!pattern) {
 		switch (slot) {
diff --git a/builtin/remote.c b/builtin/remote.c
index 1e0b137d977..87de81105e2 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -268,6 +268,7 @@ static const char *abbrev_ref(const char *name, const char *prefix)
 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
 
 static int config_read_branches(const char *key, const char *value,
+				const struct config_context *ctx UNUSED,
 				void *data UNUSED)
 {
 	const char *orig_key = key;
@@ -645,7 +646,7 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	void *cb)
+	const struct config_context *ctx UNUSED, void *cb)
 {
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
@@ -1494,7 +1495,9 @@ static int prune(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
+static int get_remote_default(const char *key, const char *value UNUSED,
+			      const struct config_context *ctx UNUSED,
+			      void *priv)
 {
 	if (strcmp(key, "remotes.default") == 0) {
 		int *found = priv;
diff --git a/builtin/repack.c b/builtin/repack.c
index 0541c3ce157..6f74570bf94 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -59,7 +59,8 @@ struct pack_objects_args {
 	int local;
 };
 
-static int repack_config(const char *var, const char *value, void *cb)
+static int repack_config(const char *var, const char *value,
+			 const struct config_context *ctx, void *cb)
 {
 	struct pack_objects_args *cruft_po_args = cb;
 	if (!strcmp(var, "repack.usedeltabaseoffset")) {
@@ -91,7 +92,7 @@ static int repack_config(const char *var, const char *value, void *cb)
 		return git_config_string(&cruft_po_args->depth, var, value);
 	if (!strcmp(var, "repack.cruftthreads"))
 		return git_config_string(&cruft_po_args->threads, var, value);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/builtin/reset.c b/builtin/reset.c
index f99f32d5802..1ae82f2e89c 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -312,12 +312,13 @@ static int reset_refs(const char *rev, const struct object_id *oid)
 	return update_ref_status;
 }
 
-static int git_reset_config(const char *var, const char *value, void *cb)
+static int git_reset_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "submodule.recurse"))
 		return git_default_submodule_config(var, value, cb);
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_reset(int argc, const char **argv, const char *prefix)
diff --git a/builtin/send-pack.c b/builtin/send-pack.c
index 4784143004d..cd6d9e41129 100644
--- a/builtin/send-pack.c
+++ b/builtin/send-pack.c
@@ -131,7 +131,8 @@ static void print_helper_status(struct ref *ref)
 	strbuf_release(&buf);
 }
 
-static int send_pack_config(const char *k, const char *v, void *cb)
+static int send_pack_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "push.gpgsign")) {
 		const char *value;
@@ -151,7 +152,7 @@ static int send_pack_config(const char *k, const char *v, void *cb)
 			}
 		}
 	}
-	return git_default_config(k, v, cb);
+	return git_default_config(k, v, ctx, cb);
 }
 
 int cmd_send_pack(int argc, const char **argv, const char *prefix)
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index a2461270d4b..f2fd245b838 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -559,7 +559,8 @@ static void append_one_rev(const char *av)
 	die("bad sha1 reference %s", av);
 }
 
-static int git_show_branch_config(const char *var, const char *value, void *cb)
+static int git_show_branch_config(const char *var, const char *value,
+				  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "showbranch.default")) {
 		if (!value)
@@ -582,7 +583,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
diff --git a/builtin/stash.c b/builtin/stash.c
index a7e17ffe384..e5c4246d2d4 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -837,7 +837,8 @@ static int show_stat = 1;
 static int show_patch;
 static int show_include_untracked;
 
-static int git_stash_config(const char *var, const char *value, void *cb)
+static int git_stash_config(const char *var, const char *value,
+			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "stash.showstat")) {
 		show_stat = git_config_bool(var, value);
@@ -851,7 +852,7 @@ static int git_stash_config(const char *var, const char *value, void *cb)
 		show_include_untracked = git_config_bool(var, value);
 		return 0;
 	}
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value, ctx, cb);
 }
 
 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 6a16208e8a8..f8e9d85e77a 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2192,6 +2192,7 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
+				   const struct config_context *ctx UNUSED,
 				   void *cb)
 {
 	int *max_jobs = cb;
diff --git a/builtin/tag.c b/builtin/tag.c
index 1acf5f7a59f..b7dfb5e2cad 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -188,7 +188,8 @@ static const char tag_template_nocleanup[] =
 	"Lines starting with '%c' will be kept; you may remove them"
 	" yourself if you want to.\n");
 
-static int git_tag_config(const char *var, const char *value, void *cb)
+static int git_tag_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "tag.gpgsign")) {
 		config_sign_tag = git_config_bool(var, value);
@@ -213,7 +214,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void write_tag_body(int fd, const struct object_id *oid)
diff --git a/builtin/var.c b/builtin/var.c
index 21499989807..ae011bdf409 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -71,13 +71,14 @@ static const struct git_var *get_git_var(const char *var)
 	return NULL;
 }
 
-static int show_config(const char *var, const char *value, void *cb)
+static int show_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb)
 {
 	if (value)
 		printf("%s=%s\n", var, value);
 	else
 		printf("%s\n", var);
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 5a9cf076ad2..a30d37a273f 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -128,14 +128,15 @@ static int verbose;
 static int guess_remote;
 static timestamp_t expire;
 
-static int git_worktree_config(const char *var, const char *value, void *cb)
+static int git_worktree_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "worktree.guessremote")) {
 		guess_remote = git_config_bool(var, value);
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int delete_git_dir(const char *id)
diff --git a/bundle-uri.c b/bundle-uri.c
index 2a2db1a1d39..0d5acc3dc51 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -224,7 +224,9 @@ static int bundle_list_update(const char *key, const char *value,
 	return 0;
 }
 
-static int config_to_bundle_list(const char *key, const char *value, void *data)
+static int config_to_bundle_list(const char *key, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *data)
 {
 	struct bundle_list *list = data;
 	return bundle_list_update(key, value, list);
@@ -871,7 +873,9 @@ cached:
 	return advertise_bundle_uri;
 }
 
-static int config_to_packet_line(const char *key, const char *value, void *data)
+static int config_to_packet_line(const char *key, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *data)
 {
 	struct packet_reader *writer = data;
 
diff --git a/compat/mingw.c b/compat/mingw.c
index d06cdc6254f..4186bc7a417 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -244,7 +244,8 @@ static int core_restrict_inherited_handles = -1;
 static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
 static char *unset_environment_variables;
 
-int mingw_core_config(const char *var, const char *value, void *cb)
+int mingw_core_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "core.hidedotfiles")) {
 		if (value && !strcasecmp(value, "dotgitonly"))
diff --git a/compat/mingw.h b/compat/mingw.h
index 209cf7cebad..5e34c873473 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -11,7 +11,9 @@ typedef _sigset_t sigset_t;
 #undef _POSIX_THREAD_SAFE_FUNCTIONS
 #endif
 
-int mingw_core_config(const char *var, const char *value, void *cb);
+struct config_context;
+int mingw_core_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb);
 #define platform_core_config mingw_core_config
 
 /*
diff --git a/config.c b/config.c
index 2eeb6621421..850e432e301 100644
--- a/config.c
+++ b/config.c
@@ -209,7 +209,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_(
@@ -388,7 +389,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;
@@ -423,6 +425,7 @@ static void populate_remote_urls(struct config_include_data *inc)
 }
 
 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;
@@ -486,7 +489,9 @@ 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;
@@ -498,7 +503,7 @@ 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, NULL, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -680,7 +685,7 @@ static int config_parse_pair(const char *key, const char *value,
 	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, NULL, data) < 0) ? -1 : 0;
 	free(canonical_name);
 	return ret;
 }
@@ -968,7 +973,7 @@ 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);
+	ret = fn(name->buf, value, NULL, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1562,7 +1567,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")) {
@@ -1842,7 +1848,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)
@@ -1944,15 +1950,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);
@@ -2318,7 +2325,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
 
-		if (fn(entry->key, values->items[value_index].string, data) < 0)
+		if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
 			git_die_config_linenr(entry->key,
 					      reader->config_kvi->filename,
 					      reader->config_kvi->linenr);
@@ -2496,7 +2503,9 @@ struct configset_add_data {
 };
 #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 UNUSED,
+			       void *cb)
 {
 	struct configset_add_data *data = cb;
 	configset_add_value(data->config_reader, data->config_set, key, value);
@@ -3106,7 +3115,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;
 
diff --git a/config.h b/config.h
index d1c5577589e..cd30125a8a4 100644
--- a/config.h
+++ b/config.h
@@ -110,8 +110,29 @@ struct config_options {
 	} error_action;
 };
 
+/* Config source metadata for a given config key-value pair */
+struct key_value_info {
+	const char *filename;
+	int linenr;
+	enum config_origin_type origin_type;
+	enum config_scope scope;
+};
+#define KVI_INIT { \
+	.filename = NULL, \
+	.linenr = -1, \
+	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
+	.scope = CONFIG_SCOPE_UNKNOWN, \
+}
+
+/* Captures additional information that a config callback can use. */
+struct config_context {
+	/* Config source metadata for key and value. */
+	const struct key_value_info *kvi;
+};
+#define CONFIG_CONTEXT_INIT { 0 }
+
 /**
- * A config callback function takes three parameters:
+ * A config callback function takes four parameters:
  *
  * - the name of the parsed variable. This is in canonical "flat" form: the
  *   section, subsection, and variable segments will be separated by dots,
@@ -122,15 +143,22 @@ struct config_options {
  *   value specified, the value will be NULL (typically this means it
  *   should be interpreted as boolean true).
  *
+ * - the 'config context', that is, additional information about the config
+ *   iteration operation provided by the config machinery. For example, this
+ *   includes information about the config source being parsed (e.g. the
+ *   filename).
+ *
  * - a void pointer passed in by the caller of the config API; this can
  *   contain callback-specific data
  *
  * A config callback should return 0 for success, or -1 if the variable
  * could not be parsed properly.
  */
-typedef int (*config_fn_t)(const char *, const char *, void *);
+typedef int (*config_fn_t)(const char *, const char *,
+			   const struct config_context *, void *);
 
-int git_default_config(const char *, const char *, void *);
+int git_default_config(const char *, const char *,
+		       const struct config_context *, void *);
 
 /**
  * Read a specific file in git-config format.
@@ -667,13 +695,6 @@ int git_config_get_expiry(const char *key, const char **output);
 /* parse either "this many days" integer, or "5.days.ago" approxidate */
 int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
 
-struct key_value_info {
-	const char *filename;
-	int linenr;
-	enum config_origin_type origin_type;
-	enum config_scope scope;
-};
-
 /**
  * First prints the error message specified by the caller in `err` and then
  * dies printing the line number and the file name of the highest priority
diff --git a/connect.c b/connect.c
index 3a0186280c4..cddac1d96b8 100644
--- a/connect.c
+++ b/connect.c
@@ -964,7 +964,7 @@ static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
 static char *git_proxy_command;
 
 static int git_proxy_command_options(const char *var, const char *value,
-		void *cb)
+		const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "core.gitproxy")) {
 		const char *for_pos;
@@ -1010,7 +1010,7 @@ static int git_proxy_command_options(const char *var, const char *value,
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static int git_use_proxy(const char *host)
diff --git a/contrib/coccinelle/config_fn_ctx.pending.cocci b/contrib/coccinelle/config_fn_ctx.pending.cocci
new file mode 100644
index 00000000000..6d3d1000a96
--- /dev/null
+++ b/contrib/coccinelle/config_fn_ctx.pending.cocci
@@ -0,0 +1,144 @@
+@ get_fn @
+identifier fn, R;
+@@
+(
+(
+git_config_from_file
+|
+git_config_from_file_with_options
+|
+git_config_from_mem
+|
+git_config_from_blob_oid
+|
+read_early_config
+|
+read_very_early_config
+|
+config_with_options
+|
+git_config
+|
+git_protected_config
+|
+config_from_gitmodules
+)
+  (fn, ...)
+|
+repo_config(R, fn, ...)
+)
+
+@ extends get_fn @
+identifier C1, C2, D;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D);
+
+@ extends get_fn @
+@@
+int fn(const char *, const char *,
++ const struct config_context *,
+  void *);
+
+@ extends get_fn @
+// Don't change fns that look like callback fns but aren't
+identifier fn2 != tar_filter_config && != git_diff_heuristic_config &&
+  != git_default_submodule_config && != git_color_config &&
+  != bundle_list_update && != parse_object_filter_config;
+identifier C1, C2, D1, D2, S;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D1) {
+<+...
+(
+fn2(C1, C2
++ , ctx
+, D2);
+|
+if(fn2(C1, C2
++ , ctx
+, D2) < 0) { ... }
+|
+return fn2(C1, C2
++ , ctx
+, D2);
+|
+S = fn2(C1, C2
++ , ctx
+, D2);
+)
+...+>
+  }
+
+@ extends get_fn@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int fn(const char *C1, const char *C2,
++ const struct config_context *ctx UNUSED,
+  void *D) {...}
+
+
+// The previous rules don't catch all callbacks, especially if they're defined
+// in a separate file from the git_config() call. Fix these manually.
+@@
+identifier C1, C2, D;
+attribute name UNUSED;
+@@
+int
+(
+git_ident_config
+|
+urlmatch_collect_fn
+|
+write_one_config
+|
+forbid_remote_url
+|
+credential_config_callback
+)
+  (const char *C1, const char *C2,
++ const struct config_context *ctx UNUSED,
+  void *D) {...}
+
+@@
+identifier C1, C2, D, D2, S, fn2;
+@@
+int
+(
+http_options
+|
+git_status_config
+|
+git_commit_config
+|
+git_default_core_config
+|
+grep_config
+)
+  (const char *C1, const char *C2,
++ const struct config_context *ctx,
+  void *D) {
+<+...
+(
+fn2(C1, C2
++ , ctx
+, D2);
+|
+if(fn2(C1, C2
++ , ctx
+, D2) < 0) { ... }
+|
+return fn2(C1, C2
++ , ctx
+, D2);
+|
+S = fn2(C1, C2
++ , ctx
+, D2);
+)
+...+>
+  }
diff --git a/convert.c b/convert.c
index 9ee79fe4699..8e96cf83030 100644
--- a/convert.c
+++ b/convert.c
@@ -1015,7 +1015,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
 	return 0;
 }
 
-static int read_convert_config(const char *var, const char *value, void *cb UNUSED)
+static int read_convert_config(const char *var, const char *value,
+			       const struct config_context *ctx UNUSED,
+			       void *cb UNUSED)
 {
 	const char *key, *name;
 	size_t namelen;
diff --git a/credential.c b/credential.c
index 8825c6f1320..d6647541634 100644
--- a/credential.c
+++ b/credential.c
@@ -49,6 +49,7 @@ static int credential_from_potentially_partial_url(struct credential *c,
 						   const char *url);
 
 static int credential_config_callback(const char *var, const char *value,
+				      const struct config_context *ctx UNUSED,
 				      void *data)
 {
 	struct credential *c = data;
diff --git a/delta-islands.c b/delta-islands.c
index c824a5f6a42..5fc6ea6ff55 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -341,7 +341,9 @@ static void free_remote_islands(kh_str_t *remote_islands)
 	kh_destroy_str(remote_islands);
 }
 
-static int island_config_callback(const char *k, const char *v, void *cb)
+static int island_config_callback(const char *k, const char *v,
+				  const struct config_context *ctx UNUSED,
+				  void *cb)
 {
 	struct island_load_data *ild = cb;
 
diff --git a/diff.c b/diff.c
index c106f8a4ffa..0e382c8f7f0 100644
--- a/diff.c
+++ b/diff.c
@@ -357,7 +357,8 @@ static unsigned parse_color_moved_ws(const char *arg)
 	return ret;
 }
 
-int git_diff_ui_config(const char *var, const char *value, void *cb)
+int git_diff_ui_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
 		diff_use_color_default = git_config_colorbool(var, value);
@@ -440,10 +441,11 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
 	if (git_color_config(var, value, cb) < 0)
 		return -1;
 
-	return git_diff_basic_config(var, value, cb);
+	return git_diff_basic_config(var, value, ctx, cb);
 }
 
-int git_diff_basic_config(const char *var, const char *value, void *cb)
+int git_diff_basic_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	const char *name;
 
@@ -495,7 +497,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
 	if (git_diff_heuristic_config(var, value, cb) < 0)
 		return -1;
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static char *quote_two(const char *one, const char *two)
diff --git a/diff.h b/diff.h
index 6c10ce289da..33a4f4d5988 100644
--- a/diff.h
+++ b/diff.h
@@ -531,10 +531,13 @@ void free_diffstat_info(struct diffstat_t *diffstat);
 int parse_long_opt(const char *opt, const char **argv,
 		   const char **optarg);
 
-int git_diff_basic_config(const char *var, const char *value, void *cb);
+struct config_context;
+int git_diff_basic_config(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb);
 int git_diff_heuristic_config(const char *var, const char *value, void *cb);
 void init_diff_ui_defaults(void);
-int git_diff_ui_config(const char *var, const char *value, void *cb);
+int git_diff_ui_config(const char *var, const char *value,
+		       const struct config_context *ctx, void *cb);
 void repo_diff_setup(struct repository *, struct diff_options *);
 struct option *add_diff_options(const struct option *, struct diff_options *);
 int diff_opt_parse(struct diff_options *, const char **, int, const char *);
diff --git a/fetch-pack.c b/fetch-pack.c
index 0f71054fbae..5f3d40f3d09 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1860,7 +1860,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	return ref;
 }
 
-static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
+static int fetch_pack_config_cb(const char *var, const char *value,
+				const struct config_context *ctx, void *cb)
 {
 	if (strcmp(var, "fetch.fsck.skiplist") == 0) {
 		const char *path;
@@ -1882,7 +1883,7 @@ static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 static void fetch_pack_config(void)
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715ba..10137444321 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -20,7 +20,8 @@ static int use_branch_desc;
 static int suppress_dest_pattern_seen;
 static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP;
 
-int fmt_merge_msg_config(const char *key, const char *value, void *cb)
+int fmt_merge_msg_config(const char *key, const char *value,
+			 const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
@@ -40,7 +41,7 @@ int fmt_merge_msg_config(const char *key, const char *value, void *cb)
 			string_list_append(&suppress_dest_patterns, value);
 		suppress_dest_pattern_seen = 1;
 	} else {
-		return git_default_config(key, value, cb);
+		return git_default_config(key, value, ctx, cb);
 	}
 	return 0;
 }
diff --git a/fmt-merge-msg.h b/fmt-merge-msg.h
index 99054042dc5..73ca3e44652 100644
--- a/fmt-merge-msg.h
+++ b/fmt-merge-msg.h
@@ -13,7 +13,8 @@ struct fmt_merge_msg_opts {
 };
 
 extern int merge_log_config;
-int fmt_merge_msg_config(const char *key, const char *value, void *cb);
+int fmt_merge_msg_config(const char *key, const char *value,
+			 const struct config_context *ctx, void *cb);
 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 		  struct fmt_merge_msg_opts *);
 
diff --git a/fsck.c b/fsck.c
index 3261ef9ec28..55b6a694853 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1163,7 +1163,9 @@ struct fsck_gitmodules_data {
 	int ret;
 };
 
-static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
+static int fsck_gitmodules_fn(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
+			      void *vdata)
 {
 	struct fsck_gitmodules_data *data = vdata;
 	const char *subsection, *key;
@@ -1373,7 +1375,8 @@ int fsck_finish(struct fsck_options *options)
 	return ret;
 }
 
-int git_fsck_config(const char *var, const char *value, void *cb)
+int git_fsck_config(const char *var, const char *value,
+		    const struct config_context *ctx, void *cb)
 {
 	struct fsck_options *options = cb;
 	if (strcmp(var, "fsck.skiplist") == 0) {
@@ -1394,7 +1397,7 @@ int git_fsck_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
 
 /*
diff --git a/fsck.h b/fsck.h
index e17730e9da9..6359ba359bd 100644
--- a/fsck.h
+++ b/fsck.h
@@ -233,10 +233,12 @@ void fsck_put_object_name(struct fsck_options *options,
 const char *fsck_describe_object(struct fsck_options *options,
 				 const struct object_id *oid);
 
+struct key_value_info;
 /*
  * git_config() callback for use by fsck-y tools that want to support
  * fsck.<msg> fsck.skipList etc.
  */
-int git_fsck_config(const char *var, const char *value, void *cb);
+int git_fsck_config(const char *var, const char *value,
+		    const struct config_context *ctx, void *cb);
 
 #endif
diff --git a/git-compat-util.h b/git-compat-util.h
index 5b2b99c17c5..14e8aacb957 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -440,8 +440,10 @@ typedef uintmax_t timestamp_t;
 #endif
 
 #ifndef platform_core_config
+struct config_context;
 static inline int noop_core_config(const char *var UNUSED,
 				   const char *value UNUSED,
+				   const struct config_context *ctx UNUSED,
 				   void *cb UNUSED)
 {
 	return 0;
diff --git a/gpg-interface.c b/gpg-interface.c
index 19a3471a0b5..57c862a3a22 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -14,7 +14,8 @@
 #include "alias.h"
 #include "wrapper.h"
 
-static int git_gpg_config(const char *, const char *, void *);
+static int git_gpg_config(const char *, const char *,
+			  const struct config_context *, void *);
 
 static void gpg_interface_lazy_init(void)
 {
@@ -720,7 +721,9 @@ void set_signing_key(const char *key)
 	configured_signing_key = xstrdup(key);
 }
 
-static int git_gpg_config(const char *var, const char *value, void *cb UNUSED)
+static int git_gpg_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED,
+			  void *cb UNUSED)
 {
 	struct gpg_format *fmt = NULL;
 	char *fmtname = NULL;
diff --git a/grep.c b/grep.c
index f00986c451a..fc22c3e2afb 100644
--- a/grep.c
+++ b/grep.c
@@ -56,7 +56,8 @@ define_list_config_array_extra(color_grep_slots, {"match"});
  * Read the configuration file once and store it in
  * the grep_defaults template.
  */
-int grep_config(const char *var, const char *value, void *cb)
+int grep_config(const char *var, const char *value,
+		const struct config_context *ctx, void *cb)
 {
 	struct grep_opt *opt = cb;
 	const char *slot;
@@ -91,9 +92,9 @@ int grep_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "color.grep"))
 		opt->color = git_config_colorbool(var, value);
 	if (!strcmp(var, "color.grep.match")) {
-		if (grep_config("color.grep.matchcontext", value, cb) < 0)
+		if (grep_config("color.grep.matchcontext", value, ctx, cb) < 0)
 			return -1;
-		if (grep_config("color.grep.matchselected", value, cb) < 0)
+		if (grep_config("color.grep.matchselected", value, ctx, cb) < 0)
 			return -1;
 	} else if (skip_prefix(var, "color.grep.", &slot)) {
 		int i = LOOKUP_CONFIG(color_grep_slots, slot);
diff --git a/grep.h b/grep.h
index c59592e3bdb..926c0875c42 100644
--- a/grep.h
+++ b/grep.h
@@ -202,7 +202,9 @@ struct grep_opt {
 	.output = std_output, \
 }
 
-int grep_config(const char *var, const char *value, void *);
+struct config_context;
+int grep_config(const char *var, const char *value,
+		const struct config_context *ctx, void *data);
 void grep_init(struct grep_opt *, struct repository *repo);
 
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
diff --git a/help.c b/help.c
index 5d7637dce92..ac0ae5ac0dc 100644
--- a/help.c
+++ b/help.c
@@ -309,7 +309,8 @@ void load_command_list(const char *prefix,
 	exclude_cmds(other_cmds, main_cmds);
 }
 
-static int get_colopts(const char *var, const char *value, void *data)
+static int get_colopts(const char *var, const char *value,
+		       const struct config_context *ctx UNUSED, void *data)
 {
 	unsigned int *colopts = data;
 
@@ -459,7 +460,8 @@ void list_developer_interfaces_help(void)
 	putchar('\n');
 }
 
-static int get_alias(const char *var, const char *value, void *data)
+static int get_alias(const char *var, const char *value,
+		     const struct config_context *ctx UNUSED, void *data)
 {
 	struct string_list *list = data;
 
@@ -543,6 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
+				  const struct config_context *ctx UNUSED,
 				  void *cb UNUSED)
 {
 	const char *p;
diff --git a/http.c b/http.c
index bb58bb3e6a3..762502828c9 100644
--- a/http.c
+++ b/http.c
@@ -363,7 +363,8 @@ static void process_curl_messages(void)
 	}
 }
 
-static int http_options(const char *var, const char *value, void *cb)
+static int http_options(const char *var, const char *value,
+			const struct config_context *ctx, void *data)
 {
 	if (!strcmp("http.version", var)) {
 		return git_config_string(&curl_http_version, var, value);
@@ -534,7 +535,7 @@ static int http_options(const char *var, const char *value, void *cb)
 	}
 
 	/* Fall back on the default ones */
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, data);
 }
 
 static int curl_empty_auth_enabled(void)
diff --git a/ident.c b/ident.c
index 8fad92d7007..08be4d0747d 100644
--- a/ident.c
+++ b/ident.c
@@ -671,7 +671,9 @@ static int set_ident(const char *var, const char *value)
 	return 0;
 }
 
-int git_ident_config(const char *var, const char *value, void *data UNUSED)
+int git_ident_config(const char *var, const char *value,
+		     const struct config_context *ctx UNUSED,
+		     void *data UNUSED)
 {
 	if (!strcmp(var, "user.useconfigonly")) {
 		ident_use_config_only = git_config_bool(var, value);
diff --git a/ident.h b/ident.h
index 96a64896a01..6a79febba15 100644
--- a/ident.h
+++ b/ident.h
@@ -62,6 +62,8 @@ const char *fmt_name(enum want_ident);
 int committer_ident_sufficiently_given(void);
 int author_ident_sufficiently_given(void);
 
-int git_ident_config(const char *, const char *, void *);
+struct config_context;
+int git_ident_config(const char *, const char *, const struct config_context *,
+		     void *);
 
 #endif
diff --git a/imap-send.c b/imap-send.c
index 7f5426177a1..47777e76861 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1323,7 +1323,8 @@ static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
 	return 1;
 }
 
-static int git_imap_config(const char *var, const char *val, void *cb)
+static int git_imap_config(const char *var, const char *val,
+			   const struct config_context *ctx, void *cb)
 {
 
 	if (!strcmp("imap.sslverify", var))
@@ -1357,7 +1358,7 @@ static int git_imap_config(const char *var, const char *val, void *cb)
 			server.host = xstrdup(val);
 		}
 	} else
-		return git_default_config(var, val, cb);
+		return git_default_config(var, val, ctx, cb);
 
 	return 0;
 }
diff --git a/ll-merge.c b/ll-merge.c
index 07ec16e8e5b..3936d112e00 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -254,6 +254,7 @@ static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
 static const char *default_ll_merge;
 
 static int read_merge_config(const char *var, const char *value,
+			     const struct config_context *ctx UNUSED,
 			     void *cb UNUSED)
 {
 	struct ll_merge_driver *fn;
diff --git a/ls-refs.c b/ls-refs.c
index f385938b64c..a29c2364a59 100644
--- a/ls-refs.c
+++ b/ls-refs.c
@@ -137,6 +137,7 @@ static void send_possibly_unborn_head(struct ls_refs_data *data)
 }
 
 static int ls_refs_config(const char *var, const char *value,
+			  const struct config_context *ctx UNUSED,
 			  void *cb_data)
 {
 	struct ls_refs_data *data = cb_data;
diff --git a/mailinfo.c b/mailinfo.c
index 2aeb20e5e62..931505363cd 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -1241,12 +1241,13 @@ int mailinfo_parse_quoted_cr_action(const char *actionstr, int *action)
 	return 0;
 }
 
-static int git_mailinfo_config(const char *var, const char *value, void *mi_)
+static int git_mailinfo_config(const char *var, const char *value,
+			       const struct config_context *ctx, void *mi_)
 {
 	struct mailinfo *mi = mi_;
 
 	if (!starts_with(var, "mailinfo."))
-		return git_default_config(var, value, NULL);
+		return git_default_config(var, value, ctx, NULL);
 	if (!strcmp(var, "mailinfo.scissors")) {
 		mi->use_scissors = git_config_bool(var, value);
 		return 0;
diff --git a/notes-utils.c b/notes-utils.c
index 4a793eb347f..97c031c26ec 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -94,7 +94,9 @@ static combine_notes_fn parse_combine_notes_fn(const char *v)
 		return NULL;
 }
 
-static int notes_rewrite_config(const char *k, const char *v, void *cb)
+static int notes_rewrite_config(const char *k, const char *v,
+				const struct config_context *ctx UNUSED,
+				void *cb)
 {
 	struct notes_rewrite_cfg *c = cb;
 	if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
diff --git a/notes.c b/notes.c
index f51a2d3630e..e68645a4b89 100644
--- a/notes.c
+++ b/notes.c
@@ -974,7 +974,9 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
 	free(globs_copy);
 }
 
-static int notes_display_config(const char *k, const char *v, void *cb)
+static int notes_display_config(const char *k, const char *v,
+				const struct config_context *ctx UNUSED,
+				void *cb)
 {
 	int *load_refs = cb;
 
diff --git a/pager.c b/pager.c
index 63055d0873f..b8822a9381e 100644
--- a/pager.c
+++ b/pager.c
@@ -43,6 +43,7 @@ static void wait_for_pager_signal(int signo)
 }
 
 static int core_pager_config(const char *var, const char *value,
+			     const struct config_context *ctx UNUSED,
 			     void *data UNUSED)
 {
 	if (!strcmp(var, "core.pager"))
@@ -228,7 +229,9 @@ struct pager_command_config_data {
 	char *value;
 };
 
-static int pager_command_config(const char *var, const char *value, void *vdata)
+static int pager_command_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *vdata)
 {
 	struct pager_command_config_data *data = vdata;
 	const char *cmd;
diff --git a/pretty.c b/pretty.c
index 0bb938021ba..87245353452 100644
--- a/pretty.c
+++ b/pretty.c
@@ -56,6 +56,7 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
 }
 
 static int git_pretty_formats_config(const char *var, const char *value,
+				     const struct config_context *ctx UNUSED,
 				     void *cb UNUSED)
 {
 	struct cmt_fmt_map *commit_format = NULL;
diff --git a/promisor-remote.c b/promisor-remote.c
index 1adcd6fb0a5..c22abb85b15 100644
--- a/promisor-remote.c
+++ b/promisor-remote.c
@@ -100,7 +100,9 @@ static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
 	config->promisors_tail = &r->next;
 }
 
-static int promisor_remote_config(const char *var, const char *value, void *data)
+static int promisor_remote_config(const char *var, const char *value,
+				  const struct config_context *ctx UNUSED,
+				  void *data)
 {
 	struct promisor_remote_config *config = data;
 	const char *name;
diff --git a/remote.c b/remote.c
index 1bcd36e358a..241999c2842 100644
--- a/remote.c
+++ b/remote.c
@@ -349,7 +349,8 @@ static void read_branches_file(struct remote_state *remote_state,
 	remote->fetch_tags = 1; /* always auto-follow */
 }
 
-static int handle_config(const char *key, const char *value, void *cb)
+static int handle_config(const char *key, const char *value,
+			 const struct config_context *ctx UNUSED, void *cb)
 {
 	const char *name;
 	size_t namelen;
diff --git a/revision.c b/revision.c
index b33cc1d106a..87ed8ccd444 100644
--- a/revision.c
+++ b/revision.c
@@ -1572,7 +1572,9 @@ struct exclude_hidden_refs_cb {
 	const char *section;
 };
 
-static int hide_refs_config(const char *var, const char *value, void *cb_data)
+static int hide_refs_config(const char *var, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *cb_data)
 {
 	struct exclude_hidden_refs_cb *cb = cb_data;
 	cb->exclusions->hidden_refs_configured = 1;
diff --git a/scalar.c b/scalar.c
index 1326e1f6089..df7358f481c 100644
--- a/scalar.c
+++ b/scalar.c
@@ -594,7 +594,9 @@ static int cmd_register(int argc, const char **argv)
 	return register_dir();
 }
 
-static int get_scalar_repos(const char *key, const char *value, void *data)
+static int get_scalar_repos(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *data)
 {
 	struct string_list *list = data;
 
diff --git a/sequencer.c b/sequencer.c
index bceb6abcb6c..34754d17596 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -219,7 +219,8 @@ static struct update_ref_record *init_update_ref_record(const char *ref)
 	return rec;
 }
 
-static int git_sequencer_config(const char *k, const char *v, void *cb)
+static int git_sequencer_config(const char *k, const char *v,
+				const struct config_context *ctx, void *cb)
 {
 	struct replay_opts *opts = cb;
 	int status;
@@ -274,7 +275,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
 	if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
 		opts->commit_use_reference = git_config_bool(k, v);
 
-	return git_diff_basic_config(k, v, NULL);
+	return git_diff_basic_config(k, v, ctx, NULL);
 }
 
 void sequencer_init_config(struct replay_opts *opts)
@@ -2881,7 +2882,9 @@ static int git_config_string_dup(char **dest,
 	return 0;
 }
 
-static int populate_opts_cb(const char *key, const char *value, void *data)
+static int populate_opts_cb(const char *key, const char *value,
+			    const struct config_context *ctx UNUSED,
+			    void *data)
 {
 	struct replay_opts *opts = data;
 	int error_flag = 1;
diff --git a/setup.c b/setup.c
index 6f6e92b96be..fadba5bab4b 100644
--- a/setup.c
+++ b/setup.c
@@ -517,7 +517,9 @@ no_prevention_needed:
 	startup_info->original_cwd = NULL;
 }
 
-static int read_worktree_config(const char *var, const char *value, void *vdata)
+static int read_worktree_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *vdata)
 {
 	struct repository_format *data = vdata;
 
@@ -588,7 +590,8 @@ static enum extension_result handle_extension(const char *var,
 	return EXTENSION_UNKNOWN;
 }
 
-static int check_repo_format(const char *var, const char *value, void *vdata)
+static int check_repo_format(const char *var, const char *value,
+			     const struct config_context *ctx, void *vdata)
 {
 	struct repository_format *data = vdata;
 	const char *ext;
@@ -617,7 +620,7 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
 		}
 	}
 
-	return read_worktree_config(var, value, vdata);
+	return read_worktree_config(var, value, ctx, vdata);
 }
 
 static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
@@ -1115,7 +1118,8 @@ struct safe_directory_data {
 	int is_safe;
 };
 
-static int safe_directory_cb(const char *key, const char *value, void *d)
+static int safe_directory_cb(const char *key, const char *value,
+			     const struct config_context *ctx UNUSED, void *d)
 {
 	struct safe_directory_data *data = d;
 
@@ -1171,7 +1175,9 @@ static int ensure_valid_ownership(const char *gitfile,
 	return data.is_safe;
 }
 
-static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
+static int allowed_bare_repo_cb(const char *key, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *d)
 {
 	enum allowed_bare_repo *allowed_bare_repo = d;
 
diff --git a/submodule-config.c b/submodule-config.c
index 7eb7a0d88d2..a38d4d49731 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -426,7 +426,8 @@ struct parse_config_parameter {
  * config store (.git/config, etc).  Callers are responsible for
  * checking for overrides in the main config store when appropriate.
  */
-static int parse_config(const char *var, const char *value, void *data)
+static int parse_config(const char *var, const char *value,
+			const struct config_context *ctx UNUSED, void *data)
 {
 	struct parse_config_parameter *me = data;
 	struct submodule *submodule;
@@ -674,7 +675,8 @@ out:
 	}
 }
 
-static int gitmodules_cb(const char *var, const char *value, void *data)
+static int gitmodules_cb(const char *var, const char *value,
+			 const struct config_context *ctx, void *data)
 {
 	struct repository *repo = data;
 	struct parse_config_parameter parameter;
@@ -684,7 +686,7 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
 	parameter.gitmodules_oid = null_oid();
 	parameter.overwrite = 1;
 
-	return parse_config(var, value, &parameter);
+	return parse_config(var, value, ctx, &parameter);
 }
 
 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
@@ -801,7 +803,9 @@ void submodule_free(struct repository *r)
 		submodule_cache_clear(r->submodule_cache);
 }
 
-static int config_print_callback(const char *var, const char *value, void *cb_data)
+static int config_print_callback(const char *var, const char *value,
+				 const struct config_context *ctx UNUSED,
+				 void *cb_data)
 {
 	char *wanted_key = cb_data;
 
@@ -843,7 +847,9 @@ struct fetch_config {
 	int *recurse_submodules;
 };
 
-static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
+static int gitmodules_fetch_config(const char *var, const char *value,
+				   const struct config_context *ctx UNUSED,
+				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
@@ -871,6 +877,7 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
+					  const struct config_context *ctx UNUSED,
 					  void *cb)
 {
 	int *max_jobs = cb;
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index ad78fc17683..85ad815358e 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -42,7 +42,9 @@
  *
  */
 
-static int iterate_cb(const char *var, const char *value, void *data UNUSED)
+static int iterate_cb(const char *var, const char *value,
+		      const struct config_context *ctx UNUSED,
+		      void *data UNUSED)
 {
 	static int nr;
 
@@ -59,7 +61,8 @@ static int iterate_cb(const char *var, const char *value, void *data UNUSED)
 	return 0;
 }
 
-static int parse_int_cb(const char *var, const char *value, void *data)
+static int parse_int_cb(const char *var, const char *value,
+			const struct config_context *ctx UNUSED, void *data)
 {
 	const char *key_to_match = data;
 
@@ -70,7 +73,9 @@ static int parse_int_cb(const char *var, const char *value, void *data)
 	return 0;
 }
 
-static int early_config_cb(const char *var, const char *value, void *vdata)
+static int early_config_cb(const char *var, const char *value,
+			   const struct config_context *ctx UNUSED,
+			   void *vdata)
 {
 	const char *key = vdata;
 
diff --git a/t/helper/test-userdiff.c b/t/helper/test-userdiff.c
index 680124a6760..0ce31ce59f5 100644
--- a/t/helper/test-userdiff.c
+++ b/t/helper/test-userdiff.c
@@ -12,7 +12,9 @@ static int driver_cb(struct userdiff_driver *driver,
 	return 0;
 }
 
-static int cmd__userdiff_config(const char *var, const char *value, void *cb UNUSED)
+static int cmd__userdiff_config(const char *var, const char *value,
+				const struct config_context *ctx UNUSED,
+				void *cb UNUSED)
 {
 	if (userdiff_config(var, value) < 0)
 		return -1;
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 78cfc15d52d..83bc4fd109c 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -99,7 +99,8 @@ struct tr2_cfg_data {
 /*
  * See if the given config key matches any of our patterns of interest.
  */
-static int tr2_cfg_cb(const char *key, const char *value, void *d)
+static int tr2_cfg_cb(const char *key, const char *value,
+		      const struct config_context *ctx UNUSED, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
@@ -142,8 +143,12 @@ void tr2_list_env_vars_fl(const char *file, int line)
 void tr2_cfg_set_fl(const char *file, int line, const char *key,
 		    const char *value)
 {
+	struct key_value_info kvi = KVI_INIT;
+	struct config_context ctx = {
+		.kvi = &kvi,
+	};
 	struct tr2_cfg_data data = { file, line };
 
 	if (tr2_cfg_load_patterns() > 0)
-		tr2_cfg_cb(key, value, &data);
+		tr2_cfg_cb(key, value, &ctx, &data);
 }
diff --git a/trace2/tr2_sysenv.c b/trace2/tr2_sysenv.c
index 069786cb927..f26ec95ab4d 100644
--- a/trace2/tr2_sysenv.c
+++ b/trace2/tr2_sysenv.c
@@ -57,7 +57,8 @@ static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
 };
 /* clang-format on */
 
-static int tr2_sysenv_cb(const char *key, const char *value, void *d)
+static int tr2_sysenv_cb(const char *key, const char *value,
+			 const struct config_context *ctx UNUSED, void *d)
 {
 	int k;
 
diff --git a/trailer.c b/trailer.c
index a2c3ed6f28c..06dc0b7f683 100644
--- a/trailer.c
+++ b/trailer.c
@@ -482,6 +482,7 @@ static struct {
 };
 
 static int git_trailer_default_config(const char *conf_key, const char *value,
+				      const struct config_context *ctx UNUSED,
 				      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
@@ -514,6 +515,7 @@ static int git_trailer_default_config(const char *conf_key, const char *value,
 }
 
 static int git_trailer_config(const char *conf_key, const char *value,
+			      const struct config_context *ctx UNUSED,
 			      void *cb UNUSED)
 {
 	const char *trailer_item, *variable_name;
diff --git a/upload-pack.c b/upload-pack.c
index d3312006a32..951fd1f9c25 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1309,7 +1309,9 @@ static int parse_object_filter_config(const char *var, const char *value,
 	return 0;
 }
 
-static int upload_pack_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_config(const char *var, const char *value,
+			      const struct config_context *ctx UNUSED,
+			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
@@ -1350,7 +1352,9 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
 }
 
-static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+static int upload_pack_protected_config(const char *var, const char *value,
+					const struct config_context *ctx UNUSED,
+					void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
 
diff --git a/urlmatch.c b/urlmatch.c
index eba0bdd77fe..1c45f23adf2 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -551,7 +551,8 @@ static int cmp_matches(const struct urlmatch_item *a,
 	return 0;
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb)
+int urlmatch_config_entry(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb)
 {
 	struct string_list_item *item;
 	struct urlmatch_config *collect = cb;
@@ -565,7 +566,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)
 
 	if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
 		if (collect->cascade_fn)
-			return collect->cascade_fn(var, value, cb);
+			return collect->cascade_fn(var, value, ctx, cb);
 		return 0; /* not interested */
 	}
 	dot = strrchr(key, '.');
@@ -609,7 +610,7 @@ int urlmatch_config_entry(const char *var, const char *value, void *cb)
 	strbuf_addstr(&synthkey, collect->section);
 	strbuf_addch(&synthkey, '.');
 	strbuf_addstr(&synthkey, key);
-	retval = collect->collect_fn(synthkey.buf, value, collect->cb);
+	retval = collect->collect_fn(synthkey.buf, value, ctx, collect->cb);
 
 	strbuf_release(&synthkey);
 	return retval;
diff --git a/urlmatch.h b/urlmatch.h
index bee374a642c..5ba85cea139 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -71,7 +71,8 @@ struct urlmatch_config {
 	.vars = STRING_LIST_INIT_DUP, \
 }
 
-int urlmatch_config_entry(const char *var, const char *value, void *cb);
+int urlmatch_config_entry(const char *var, const char *value,
+			  const struct config_context *ctx, void *cb);
 void urlmatch_config_release(struct urlmatch_config *config);
 
 #endif /* URL_MATCH_H */
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 0460e03f5ed..dcbb5e09857 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -307,7 +307,8 @@ int xdiff_compare_lines(const char *l1, long s1,
 
 int git_xmerge_style = -1;
 
-int git_xmerge_config(const char *var, const char *value, void *cb)
+int git_xmerge_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "merge.conflictstyle")) {
 		if (!value)
@@ -327,5 +328,5 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
 			    value, var);
 		return 0;
 	}
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, ctx, cb);
 }
diff --git a/xdiff-interface.h b/xdiff-interface.h
index 733c364d26c..e6f80df0462 100644
--- a/xdiff-interface.h
+++ b/xdiff-interface.h
@@ -50,7 +50,9 @@ int buffer_is_binary(const char *ptr, unsigned long size);
 
 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
 void xdiff_clear_find_func(xdemitconf_t *xecfg);
-int git_xmerge_config(const char *var, const char *value, void *cb);
+struct config_context;
+int git_xmerge_config(const char *var, const char *value,
+		      const struct config_context *ctx, void *cb);
 extern int git_xmerge_style;
 
 /*
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 04/11] config.c: pass ctx in configsets
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (2 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 03/11] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 05/11] config: pass ctx with config files Glen Choo via GitGitGadget
                           ` (8 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context to config callbacks in configset_iter(), trivially
setting the .kvi member to the cached key_value_info. Then, in config
callbacks that are only used with configsets, use the .kvi member to
replace calls to current_config_*(), and delete current_config_line()
because it has no remaining callers.

This leaves builtin/config.c and config.c as the only remaining users of
current_config_*().

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/remote.c       | 10 ++++++----
 config.c               | 35 ++++++++++++++++-------------------
 config.h               |  2 +-
 remote.c               |  7 ++++---
 t/helper/test-config.c | 11 ++++++-----
 5 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index 87de81105e2..d47f9ee21cf 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -646,17 +646,19 @@ struct push_default_info
 };
 
 static int config_read_push_default(const char *key, const char *value,
-	const struct config_context *ctx UNUSED, void *cb)
+	const struct config_context *ctx, void *cb)
 {
+	const struct key_value_info *kvi = ctx->kvi;
+
 	struct push_default_info* info = cb;
 	if (strcmp(key, "remote.pushdefault") ||
 	    !value || strcmp(value, info->old_name))
 		return 0;
 
-	info->scope = current_config_scope();
+	info->scope = kvi->scope;
 	strbuf_reset(&info->origin);
-	strbuf_addstr(&info->origin, current_config_name());
-	info->linenr = current_config_line();
+	strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
+	info->linenr = kvi->linenr;
 
 	return 0;
 }
diff --git a/config.c b/config.c
index 850e432e301..662d406ac1e 100644
--- a/config.c
+++ b/config.c
@@ -2317,6 +2317,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 	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;
@@ -2324,12 +2325,11 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 		values = &entry->value_list;
 
 		config_reader_set_kvi(reader, values->items[value_index].util);
-
-		if (fn(entry->key, values->items[value_index].string, NULL, 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);
-
+					      ctx.kvi->filename,
+					      ctx.kvi->linenr);
 		config_reader_set_kvi(reader, NULL);
 	}
 }
@@ -3984,13 +3984,8 @@ static int reader_origin_type(struct config_reader *reader,
 	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";
@@ -4007,6 +4002,16 @@ const char *current_config_origin_type(void)
 	}
 }
 
+const char *current_config_origin_type(void)
+{
+	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
+
+	if (reader_origin_type(&the_reader, &type))
+		BUG("current_config_origin_type called outside config callback");
+
+	return config_origin_type_name(type);
+}
+
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4054,14 +4059,6 @@ enum config_scope current_config_scope(void)
 		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;
diff --git a/config.h b/config.h
index cd30125a8a4..ddf147bb2d1 100644
--- a/config.h
+++ b/config.h
@@ -387,7 +387,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 enum config_scope current_config_scope(void);
 const char *current_config_origin_type(void);
 const char *current_config_name(void);
-int current_config_line(void);
+const char *config_origin_type_name(enum config_origin_type type);
 
 /*
  * Match and parse a config key of the form:
diff --git a/remote.c b/remote.c
index 241999c2842..1dab860141b 100644
--- a/remote.c
+++ b/remote.c
@@ -350,7 +350,7 @@ static void read_branches_file(struct remote_state *remote_state,
 }
 
 static int handle_config(const char *key, const char *value,
-			 const struct config_context *ctx UNUSED, void *cb)
+			 const struct config_context *ctx, void *cb)
 {
 	const char *name;
 	size_t namelen;
@@ -358,6 +358,7 @@ static int handle_config(const char *key, const char *value,
 	struct remote *remote;
 	struct branch *branch;
 	struct remote_state *remote_state = cb;
+	const struct key_value_info *kvi = ctx->kvi;
 
 	if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
 		/* There is no subsection. */
@@ -415,8 +416,8 @@ static int handle_config(const char *key, const char *value,
 	}
 	remote = make_remote(remote_state, name, namelen);
 	remote->origin = REMOTE_CONFIG;
-	if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
-	    current_config_scope() == CONFIG_SCOPE_WORKTREE)
+	if (kvi->scope == CONFIG_SCOPE_LOCAL ||
+	    kvi->scope == CONFIG_SCOPE_WORKTREE)
 		remote->configured_in_repo = 1;
 	if (!strcmp(subkey, "mirror"))
 		remote->mirror = git_config_bool(key, value);
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 85ad815358e..3f4c3678318 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -43,9 +43,10 @@
  */
 
 static int iterate_cb(const char *var, const char *value,
-		      const struct config_context *ctx UNUSED,
+		      const struct config_context *ctx,
 		      void *data UNUSED)
 {
+	const struct key_value_info *kvi = ctx->kvi;
 	static int nr;
 
 	if (nr++)
@@ -53,10 +54,10 @@ static int iterate_cb(const char *var, const char *value,
 
 	printf("key=%s\n", var);
 	printf("value=%s\n", value ? value : "(null)");
-	printf("origin=%s\n", current_config_origin_type());
-	printf("name=%s\n", current_config_name());
-	printf("lno=%d\n", current_config_line());
-	printf("scope=%s\n", config_scope_name(current_config_scope()));
+	printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
+	printf("name=%s\n", kvi->filename ? kvi->filename : "");
+	printf("lno=%d\n", kvi->linenr);
+	printf("scope=%s\n", config_scope_name(kvi->scope));
 
 	return 0;
 }
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 05/11] config: pass ctx with config files
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (3 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 04/11] config.c: pass ctx in configsets Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 06/11] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
                           ` (7 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context to config_callbacks when parsing config files. To
provide the .kvi member, refactor out the configset logic that caches
"struct config_source" and "enum config_scope" as a "struct
key_value_info". Make the "enum config_scope" available to the config
file machinery by plumbing an additional arg through
git_config_from_file_with_options().

We do not exercise ctx yet because the remaining current_config_*()
callers may be used with config_with_options(), which may read config
from parameters, but parameters don't pass ctx yet.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 bundle-uri.c       |   1 +
 config.c           | 105 ++++++++++++++++++++++++++++++---------------
 config.h           |   8 ++--
 fsck.c             |   3 +-
 submodule-config.c |   5 ++-
 5 files changed, 81 insertions(+), 41 deletions(-)

diff --git a/bundle-uri.c b/bundle-uri.c
index 0d5acc3dc51..64f32387745 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -255,6 +255,7 @@ int bundle_uri_parse_config_format(const char *uri,
 	}
 	result = git_config_from_file_with_options(config_to_bundle_list,
 						   filename, list,
+						   CONFIG_SCOPE_UNKNOWN,
 						   &opts);
 
 	if (!result && list->mode == BUNDLE_MODE_NONE) {
diff --git a/config.c b/config.c
index 662d406ac1e..31718711827 100644
--- a/config.c
+++ b/config.c
@@ -259,7 +259,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    !cs ? "<unknown>" :
 			    cs->name ? cs->name :
 			    "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,
+							current_config_scope(),
+							NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -503,7 +505,7 @@ static int git_config_include(const char *var, const char *value,
 	 * Pass along all values, including "include" directives; this makes it
 	 * possible to query information on the includes themselves.
 	 */
-	ret = inc->fn(var, value, NULL, inc->data);
+	ret = inc->fn(var, value, ctx, inc->data);
 	if (ret < 0)
 		return ret;
 
@@ -939,12 +941,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 (;;) {
@@ -973,7 +978,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, NULL, data);
+	kvi->linenr = cs->linenr;
+	ret = fn(name->buf, value, &ctx, data);
 	if (ret >= 0)
 		cs->linenr++;
 	return ret;
@@ -1072,8 +1078,19 @@ 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;
+}
+
 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;
@@ -1157,7 +1174,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;
 	}
 
@@ -2010,9 +2027,11 @@ int git_default_config(const char *var, const char *value,
  * this function.
  */
 static int do_config_from(struct config_reader *reader,
-			  struct config_source *top, config_fn_t fn, void *data,
+			  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 */
@@ -2022,8 +2041,9 @@ static int do_config_from(struct config_reader *reader,
 	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);
@@ -2037,7 +2057,8 @@ static int do_config_from_file(struct config_reader *reader,
 			       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;
@@ -2052,19 +2073,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(reader, &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);
+				   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;
@@ -2075,7 +2097,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
 	f = fopen_or_warn(filename, "r");
 	if (f) {
 		ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
-					  filename, filename, f, data, opts);
+					  filename, filename, f, data, scope,
+					  opts);
 		fclose(f);
 	}
 	return ret;
@@ -2083,13 +2106,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;
 
@@ -2104,14 +2129,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(&the_reader, &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;
@@ -2127,7 +2153,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;
@@ -2136,13 +2162,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)
@@ -2228,27 +2255,34 @@ static int do_git_config_sequence(struct config_reader *reader,
 	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);
@@ -2292,12 +2326,14 @@ int config_with_options(config_fn_t fn, void *data,
 	 * 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);
 	}
@@ -2440,16 +2476,14 @@ static int configset_add_value(struct config_reader *reader,
 	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;
+		kvi_from_source(reader->source, current_config_scope(), kv_info);
 	} 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->scope = reader->parsing_scope;
 	si->util = kv_info;
 
 	return 0;
@@ -3490,7 +3524,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;
diff --git a/config.h b/config.h
index ddf147bb2d1..206bf1f175a 100644
--- a/config.h
+++ b/config.h
@@ -169,16 +169,18 @@ int git_default_config(const char *, const char *,
 int git_config_from_file(config_fn_t fn, const char *, void *);
 
 int git_config_from_file_with_options(config_fn_t fn, const char *,
-				      void *,
+				      void *, enum config_scope,
 				      const struct config_options *);
 int git_config_from_mem(config_fn_t fn,
 			const enum config_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);
 int git_config_from_blob_oid(config_fn_t fn, const char *name,
 			     struct repository *repo,
-			     const struct object_id *oid, void *data);
+			     const struct object_id *oid, void *data,
+			     enum config_scope scope);
 void git_config_push_parameter(const char *text);
 void git_config_push_env(const char *spec);
 int git_config_from_parameters(config_fn_t fn, void *data);
diff --git a/fsck.c b/fsck.c
index 55b6a694853..f92c216fb5c 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1238,7 +1238,8 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
 		data.ret = 0;
 		config_opts.error_action = CONFIG_ERROR_SILENT;
 		if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
-					".gitmodules", buf, size, &data, &config_opts))
+					".gitmodules", buf, size, &data,
+					CONFIG_SCOPE_UNKNOWN, &config_opts))
 			data.ret |= report(options, oid, OBJ_BLOB,
 					FSCK_MSG_GITMODULES_PARSE,
 					"could not parse gitmodules blob");
diff --git a/submodule-config.c b/submodule-config.c
index a38d4d49731..3f25bd13674 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -606,7 +606,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 	parameter.gitmodules_oid = &oid;
 	parameter.overwrite = 0;
 	git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
-			config, config_size, &parameter, NULL);
+			    config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
 	strbuf_release(&rev);
 	free(config);
 
@@ -714,7 +714,8 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
 
 	if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
 		git_config_from_blob_oid(gitmodules_cb, rev.buf,
-					 the_repository, &oid, the_repository);
+					 the_repository, &oid, the_repository,
+					 CONFIG_SCOPE_UNKNOWN);
 	}
 	strbuf_release(&rev);
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 06/11] config.c: pass ctx with CLI config
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (4 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 05/11] config: pass ctx with config files Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 07/11] trace2: plumb config kvi Glen Choo via GitGitGadget
                           ` (6 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Pass config_context when parsing CLI config. To provide the .kvi member,
refactor out kvi_from_param() from the logic that caches CLI config in
configsets. Now that config_context and config_context.kvi is always
present when config machinery calls config callbacks, plumb "kvi" so
that we can remove all calls of current_config_scope() except for
trace2/*.c (which will be handled in a later commit), and remove all
other current_config_*() (the functions themselves and their calls).
Note that this results in .kvi containing a different, more complete
set of information than the mocked up "struct config_source" in
git_config_from_parameters().

Plumbing "kvi" reveals a few places where we've been doing the wrong
thing:

* git_config_parse_parameter() hasn't been setting config source
  information, so plumb "kvi" there too.

* Several sites in builtin/config.c have been calling current_config_*()
  functions outside of config callbacks (indirectly, via the
  format_config() helper), which means they're reading state that isn't
  set correctly:

  * "git config --get-urlmatch --show-scope" iterates config to collect
    values, but then attempts to display the scope after config
    iteration, causing the "unknown" scope to be shown instead of the
    config file's scope. It's clear that this wasn't intended: we knew
    that "--get-urlmatch" couldn't show config source metadata, which is
    why "--show-origin" was marked incompatible with "--get-urlmatch"
    when it was introduced [1]. It was most likely a mistake that we
    allowed "--show-scope" to sneak through.

    Fix this by copying the "kvi" value in the collection phase so that
    it can be read back later. This means that we can now support "git
    config --get-urlmatch --show-origin", but that is left unchanged
    for now.

  * "git config --default" doesn't have config source metadata when
    displaying the default value, so "--show-scope" also results in
    "unknown", and "--show-origin" results in a BUG(). Fix this by
    treating the default value as if it came from the command line (e.g.
    like we do with "git -c" or "git config --file"), using
    kvi_from_param().

[1] https://lore.kernel.org/git/20160205112001.GA13397@sigill.intra.peff.net/

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/config.c  | 47 ++++++++++++++++++----------
 config.c          | 80 ++++++++++++++++++++++++-----------------------
 config.h          |  3 +-
 t/t1300-config.sh | 27 ++++++++++++++++
 4 files changed, 99 insertions(+), 58 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index f4fccf99cb8..9b9f5527311 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -194,38 +194,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 UNUSED,
+			   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 +247,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) {
@@ -303,9 +308,10 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
 }
 
 static int collect_config(const char *key_, const char *value_,
-			  const struct config_context *ctx UNUSED, void *cb)
+			  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;
@@ -320,7 +326,7 @@ static int collect_config(const char *key_, const char *value_,
 	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)
@@ -382,11 +388,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);
 	}
@@ -563,15 +572,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,
-			       const struct config_context *ctx UNUSED,
+			       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));
@@ -580,6 +591,7 @@ static int urlmatch_collect_fn(const char *var, const char *value,
 	} else {
 		strbuf_reset(&matched->value);
 	}
+	matched->kvi = *kvi;
 
 	if (value) {
 		strbuf_addstr(&matched->value, value);
@@ -627,7 +639,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);
 
diff --git a/config.c b/config.c
index 31718711827..4986942b091 100644
--- a/config.c
+++ b/config.c
@@ -219,7 +219,9 @@ 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(struct config_source *cs,
+			       const struct key_value_info *kvi,
+			       const char *path,
 			       struct config_include_data *inc)
 {
 	int ret = 0;
@@ -260,8 +262,7 @@ static int handle_path_include(struct config_source *cs, const char *path,
 			    cs->name ? cs->name :
 			    "the command line");
 		ret = git_config_from_file_with_options(git_config_include, path, inc,
-							current_config_scope(),
-							NULL);
+							kvi->scope, NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -510,7 +511,7 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, ctx->kvi, value, inc);
 
 	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
 	    cond && include_condition_is_true(cs, inc, cond, cond_len) &&
@@ -519,7 +520,7 @@ static int git_config_include(const char *var, const char *value,
 
 		if (inc->opts->unconditional_remote_url)
 			inc->fn = forbid_remote_url;
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(cs, ctx->kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -677,27 +678,44 @@ 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, NULL, 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;
+}
+
 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])
@@ -716,12 +734,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) {
@@ -755,7 +774,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 {
@@ -780,10 +799,13 @@ int git_config_from_parameters(config_fn_t fn, void *data)
 	int ret = 0;
 	char *envw = NULL;
 	struct config_source source = CONFIG_SOURCE_INIT;
+	struct key_value_info kvi = KVI_INIT;
 
 	source.origin_type = CONFIG_ORIGIN_CMDLINE;
 	config_reader_push_source(&the_reader, &source);
 
+	kvi_from_param(&kvi);
+
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -819,7 +841,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;
 			}
@@ -830,7 +852,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;
 		}
@@ -2442,7 +2464,8 @@ 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_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2476,13 +2499,9 @@ static int configset_add_value(struct config_reader *reader,
 	if (!reader->source)
 		BUG("configset_add_value has no source");
 	if (reader->source->name) {
-		kvi_from_source(reader->source, current_config_scope(), kv_info);
+		kvi_from_source(reader->source, kvi_p->scope, kv_info);
 	} 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;
+		kvi_from_param(kv_info);
 	}
 	si->util = kv_info;
 
@@ -2538,11 +2557,12 @@ struct configset_add_data {
 #define CONFIGSET_ADD_INIT { 0 }
 
 static int config_set_callback(const char *key, const char *value,
-			       const struct config_context *ctx UNUSED,
+			       const struct config_context *ctx,
 			       void *cb)
 {
 	struct configset_add_data *data = cb;
-	configset_add_value(data->config_reader, data->config_set, key, value);
+	configset_add_value(ctx->kvi, data->config_reader, data->config_set,
+			    key, value);
 	return 0;
 }
 
@@ -4037,16 +4057,6 @@ const char *config_origin_type_name(enum config_origin_type type)
 	}
 }
 
-const char *current_config_origin_type(void)
-{
-	enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
-
-	if (reader_origin_type(&the_reader, &type))
-		BUG("current_config_origin_type called outside config callback");
-
-	return config_origin_type_name(type);
-}
-
 const char *config_scope_name(enum config_scope scope)
 {
 	switch (scope) {
@@ -4078,14 +4088,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	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)
diff --git a/config.h b/config.h
index 206bf1f175a..ea92392400e 100644
--- a/config.h
+++ b/config.h
@@ -387,9 +387,8 @@ void git_global_config(char **user, char **xdg);
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
 enum config_scope current_config_scope(void);
-const char *current_config_origin_type(void);
-const char *current_config_name(void);
 const char *config_origin_type_name(enum config_origin_type type);
+void kvi_from_param(struct key_value_info *out);
 
 /*
  * Match and parse a config key of the form:
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 86bfbc2b364..387d336c91f 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1668,6 +1668,21 @@ test_expect_success 'urlmatch' '
 	test_cmp expect actual
 '
 
+test_expect_success 'urlmatch with --show-scope' '
+	cat >.git/config <<-\EOF &&
+	[http "https://weak.example.com"]
+		sslVerify = false
+		cookieFile = /tmp/cookie.txt
+	EOF
+
+	cat >expect <<-EOF &&
+	local	http.cookiefile /tmp/cookie.txt
+	local	http.sslverify false
+	EOF
+	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'urlmatch favors more specific URLs' '
 	cat >.git/config <<-\EOF &&
 	[http "https://example.com/"]
@@ -2055,6 +2070,12 @@ test_expect_success '--show-origin blob ref' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-origin with --default' '
+	git config --show-origin --default foo some.key >actual &&
+	echo "command line:	foo" >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success '--show-scope with --list' '
 	cat >expect <<-EOF &&
 	global	user.global=true
@@ -2123,6 +2144,12 @@ test_expect_success '--show-scope with --show-origin' '
 	test_cmp expect output
 '
 
+test_expect_success '--show-scope with --default' '
+	git config --show-scope --default foo some.key >actual &&
+	echo "command	foo" >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'override global and system config' '
 	test_when_finished rm -f \"\$HOME\"/.gitconfig &&
 	cat >"$HOME"/.gitconfig <<-EOF &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 07/11] trace2: plumb config kvi
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (5 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 06/11] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 08/11] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
                           ` (5 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

There is a code path starting from trace2_def_param_fl() that eventually
calls current_config_scope(), and thus it needs to have "kvi" plumbed
through it. Additional plumbing is also needed to get "kvi" to
trace2_def_param_fl(), which gets called by two code paths:

- Through tr2_cfg_cb(), which is a config callback, so it trivially
  receives "kvi" via the "struct config_context ctx" parameter.

- Through tr2_list_env_vars_fl(), which is a high level function that
  lists environment variables for tracing. This has been secretly
  behaving like git_config_from_parameters() (in that it parses config
  from environment variables/the CLI), but does not set config source
  information.

  Teach tr2_list_env_vars_fl() to be well-behaved by using
  kvi_from_param(), which is used elsewhere for CLI/environment
  variable-based config.

As a result, current_config_scope() has no more callers, so remove it.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c                | 46 -----------------------------------------
 config.h                |  1 -
 trace2.c                |  4 ++--
 trace2.h                |  3 ++-
 trace2/tr2_cfg.c        |  9 +++++---
 trace2/tr2_tgt.h        |  4 +++-
 trace2/tr2_tgt_event.c  |  4 ++--
 trace2/tr2_tgt_normal.c |  4 ++--
 trace2/tr2_tgt_perf.c   |  4 ++--
 9 files changed, 19 insertions(+), 60 deletions(-)

diff --git a/config.c b/config.c
index 4986942b091..d10faba56d3 100644
--- a/config.c
+++ b/config.c
@@ -85,16 +85,6 @@ struct config_reader {
 	 */
 	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
@@ -125,19 +115,9 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 static inline void config_reader_set_kvi(struct config_reader *reader,
 					 struct key_value_info *kvi)
 {
-	if (kvi && (reader->source || reader->parsing_scope))
-		BUG("kvi should not be set while parsing a config source");
 	reader->config_kvi = kvi;
 }
 
-static inline void config_reader_set_scope(struct config_reader *reader,
-					   enum config_scope scope)
-{
-	if (scope && reader->config_kvi)
-		BUG("scope should only be set when iterating through a config source");
-	reader->parsing_scope = scope;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -412,19 +392,13 @@ 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,
@@ -2255,7 +2229,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:
@@ -2273,7 +2246,6 @@ 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))
@@ -2281,7 +2253,6 @@ static int do_git_config_sequence(struct config_reader *reader,
 							 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))
@@ -2292,13 +2263,11 @@ static int do_git_config_sequence(struct config_reader *reader,
 		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_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)) {
@@ -2307,11 +2276,9 @@ static int do_git_config_sequence(struct config_reader *reader,
 								 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);
@@ -2326,7 +2293,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) {
@@ -2340,9 +2306,6 @@ int config_with_options(config_fn_t fn, void *data,
 		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.
@@ -2364,7 +2327,6 @@ int config_with_options(config_fn_t fn, void *data,
 		string_list_clear(inc.remote_urls, 0);
 		FREE_AND_NULL(inc.remote_urls);
 	}
-	config_reader_set_scope(&the_reader, prev_scope);
 	return ret;
 }
 
@@ -4088,14 +4050,6 @@ static int reader_config_name(struct config_reader *reader, const char **out)
 	return 0;
 }
 
-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 lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index ea92392400e..82eeba94e71 100644
--- a/config.h
+++ b/config.h
@@ -386,7 +386,6 @@ void git_global_config(char **user, char **xdg);
 
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 
-enum config_scope current_config_scope(void);
 const char *config_origin_type_name(enum config_origin_type type);
 void kvi_from_param(struct key_value_info *out);
 
diff --git a/trace2.c b/trace2.c
index 0efc4e7b958..49c23bfd05a 100644
--- a/trace2.c
+++ b/trace2.c
@@ -634,7 +634,7 @@ void trace2_thread_exit_fl(const char *file, int line)
 }
 
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value)
+			 const char *value, const struct key_value_info *kvi)
 {
 	struct tr2_tgt *tgt_j;
 	int j;
@@ -644,7 +644,7 @@ void trace2_def_param_fl(const char *file, int line, const char *param,
 
 	for_each_wanted_builtin (j, tgt_j)
 		if (tgt_j->pfn_param_fl)
-			tgt_j->pfn_param_fl(file, line, param, value);
+			tgt_j->pfn_param_fl(file, line, param, value, kvi);
 }
 
 void trace2_def_repo_fl(const char *file, int line, struct repository *repo)
diff --git a/trace2.h b/trace2.h
index 4ced30c0db3..f5c5a9e6bac 100644
--- a/trace2.h
+++ b/trace2.h
@@ -325,6 +325,7 @@ void trace2_thread_exit_fl(const char *file, int line);
 
 #define trace2_thread_exit() trace2_thread_exit_fl(__FILE__, __LINE__)
 
+struct key_value_info;
 /*
  * Emits a "def_param" message containing a key/value pair.
  *
@@ -334,7 +335,7 @@ void trace2_thread_exit_fl(const char *file, int line);
  * `core.abbrev`, `status.showUntrackedFiles`, or `--no-ahead-behind`.
  */
 void trace2_def_param_fl(const char *file, int line, const char *param,
-			 const char *value);
+			 const char *value, const struct key_value_info *kvi);
 
 #define trace2_def_param(param, value) \
 	trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index 83bc4fd109c..733d9d2872a 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -100,7 +100,7 @@ struct tr2_cfg_data {
  * See if the given config key matches any of our patterns of interest.
  */
 static int tr2_cfg_cb(const char *key, const char *value,
-		      const struct config_context *ctx UNUSED, void *d)
+		      const struct config_context *ctx, void *d)
 {
 	struct strbuf **s;
 	struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
@@ -109,7 +109,8 @@ static int tr2_cfg_cb(const char *key, const char *value,
 		struct strbuf *buf = *s;
 		int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
 		if (wm == WM_MATCH) {
-			trace2_def_param_fl(data->file, data->line, key, value);
+			trace2_def_param_fl(data->file, data->line, key, value,
+					    ctx->kvi);
 			return 0;
 		}
 	}
@@ -127,8 +128,10 @@ void tr2_cfg_list_config_fl(const char *file, int line)
 
 void tr2_list_env_vars_fl(const char *file, int line)
 {
+	struct key_value_info kvi = KVI_INIT;
 	struct strbuf **s;
 
+	kvi_from_param(&kvi);
 	if (tr2_load_env_vars() <= 0)
 		return;
 
@@ -136,7 +139,7 @@ void tr2_list_env_vars_fl(const char *file, int line)
 		struct strbuf *buf = *s;
 		const char *val = getenv(buf->buf);
 		if (val && *val)
-			trace2_def_param_fl(file, line, buf->buf, val);
+			trace2_def_param_fl(file, line, buf->buf, val, &kvi);
 	}
 }
 
diff --git a/trace2/tr2_tgt.h b/trace2/tr2_tgt.h
index bf8745c4f05..1f626cffea0 100644
--- a/trace2/tr2_tgt.h
+++ b/trace2/tr2_tgt.h
@@ -69,8 +69,10 @@ typedef void(tr2_tgt_evt_exec_result_fl_t)(const char *file, int line,
 					   uint64_t us_elapsed_absolute,
 					   int exec_id, int code);
 
+struct key_value_info;
 typedef void(tr2_tgt_evt_param_fl_t)(const char *file, int line,
-				     const char *param, const char *value);
+				     const char *param, const char *value,
+				     const struct key_value_info *kvi);
 
 typedef void(tr2_tgt_evt_repo_fl_t)(const char *file, int line,
 				    const struct repository *repo);
diff --git a/trace2/tr2_tgt_event.c b/trace2/tr2_tgt_event.c
index 2af53e5d4de..53091781eca 100644
--- a/trace2/tr2_tgt_event.c
+++ b/trace2/tr2_tgt_event.c
@@ -477,11 +477,11 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct json_writer jw = JSON_WRITER_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	jw_object_begin(&jw, 0);
diff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c
index 1ebfb464d54..d25ea131643 100644
--- a/trace2/tr2_tgt_normal.c
+++ b/trace2/tr2_tgt_normal.c
@@ -297,10 +297,10 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	struct strbuf buf_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
diff --git a/trace2/tr2_tgt_perf.c b/trace2/tr2_tgt_perf.c
index 328e483a05e..a6f9a8a193e 100644
--- a/trace2/tr2_tgt_perf.c
+++ b/trace2/tr2_tgt_perf.c
@@ -439,12 +439,12 @@ static void fn_exec_result_fl(const char *file, int line,
 }
 
 static void fn_param_fl(const char *file, int line, const char *param,
-			const char *value)
+			const char *value, const struct key_value_info *kvi)
 {
 	const char *event_name = "def_param";
 	struct strbuf buf_payload = STRBUF_INIT;
 	struct strbuf scope_payload = STRBUF_INIT;
-	enum config_scope scope = current_config_scope();
+	enum config_scope scope = kvi->scope;
 	const char *scope_name = config_scope_name(scope);
 
 	strbuf_addf(&buf_payload, "%s:%s", param, value);
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 08/11] config: pass kvi to die_bad_number()
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (6 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 07/11] trace2: plumb config kvi Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 09/11] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
                           ` (4 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Plumb "struct key_value_info" through all code paths that end in
die_bad_number(), which lets us remove the helper functions that read
analogous values from "struct config_reader". As a result, nothing reads
config_reader.config_kvi any more, so remove that too.

In config.c, this requires changing the signature of
git_configset_get_value() to 'return' "kvi" in an out parameter so that
git_configset_get_<type>() can pass it to git_config_<type>(). Only
numeric types will use "kvi", so for non-numeric types (e.g.
git_configset_get_string()), pass NULL to indicate that the out
parameter isn't needed.

Outside of config.c, config callbacks now need to pass "ctx->kvi" to any
of the git_config_<type>() functions that parse a config string into a
number type. Included is a .cocci patch to make that refactor.

The only exceptional case is builtin/config.c, where git_config_<type>()
is called outside of a config callback (namely, on user-provided input),
so config source information has never been available. In this case,
die_bad_number() defaults to a generic, but perfectly descriptive
message. Let's provide a safe, non-NULL for "kvi" anyway, but make sure
not to change the message.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 archive-tar.c                              |   4 +-
 builtin/commit-graph.c                     |   4 +-
 builtin/commit.c                           |  10 +-
 builtin/config.c                           |  21 +--
 builtin/fetch.c                            |   4 +-
 builtin/fsmonitor--daemon.c                |   6 +-
 builtin/grep.c                             |   2 +-
 builtin/index-pack.c                       |   4 +-
 builtin/log.c                              |   2 +-
 builtin/pack-objects.c                     |  14 +-
 builtin/receive-pack.c                     |  10 +-
 builtin/submodule--helper.c                |   4 +-
 config.c                                   | 156 ++++++++-------------
 config.h                                   |  17 ++-
 contrib/coccinelle/git_config_number.cocci |  27 ++++
 diff.c                                     |   9 +-
 fmt-merge-msg.c                            |   2 +-
 help.c                                     |   4 +-
 http.c                                     |  10 +-
 imap-send.c                                |   2 +-
 sequencer.c                                |  22 +--
 setup.c                                    |   2 +-
 submodule-config.c                         |  13 +-
 submodule-config.h                         |   3 +-
 t/helper/test-config.c                     |   6 +-
 upload-pack.c                              |  12 +-
 worktree.c                                 |   2 +-
 27 files changed, 190 insertions(+), 182 deletions(-)
 create mode 100644 contrib/coccinelle/git_config_number.cocci

diff --git a/archive-tar.c b/archive-tar.c
index ef06e516b1f..3df8af6d1b1 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -412,14 +412,14 @@ static int tar_filter_config(const char *var, const char *value,
 }
 
 static int git_tar_config(const char *var, const char *value,
-			  const struct config_context *ctx UNUSED, void *cb)
+			  const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "tar.umask")) {
 		if (value && !strcmp(value, "user")) {
 			tar_umask = umask(0);
 			umask(tar_umask);
 		} else {
-			tar_umask = git_config_int(var, value);
+			tar_umask = git_config_int(var, value, ctx->kvi);
 		}
 		return 0;
 	}
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 1185c49239a..b071c5ab646 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -186,11 +186,11 @@ static int write_option_max_new_filters(const struct option *opt,
 }
 
 static int git_commit_graph_write_config(const char *var, const char *value,
-					 const struct config_context *ctx UNUSED,
+					 const struct config_context *ctx,
 					 void *cb UNUSED)
 {
 	if (!strcmp(var, "commitgraph.maxnewfilters"))
-		write_opts.max_new_filters = git_config_int(var, value);
+		write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
 	/*
 	 * No need to fall-back to 'git_default_config', since this was already
 	 * called in 'cmd_commit_graph()'.
diff --git a/builtin/commit.c b/builtin/commit.c
index 6a2b2503328..9fe691470a3 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1415,7 +1415,8 @@ static int git_status_config(const char *k, const char *v,
 		return git_column_config(k, v, "status", &s->colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
-		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
+		s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
+							      &is_bool);
 		if (is_bool && s->submodule_summary)
 			s->submodule_summary = -1;
 		return 0;
@@ -1475,11 +1476,11 @@ static int git_status_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "diff.renamelimit")) {
 		if (s->rename_limit == -1)
-			s->rename_limit = git_config_int(k, v);
+			s->rename_limit = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "status.renamelimit")) {
-		s->rename_limit = git_config_int(k, v);
+		s->rename_limit = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "diff.renames")) {
@@ -1625,7 +1626,8 @@ static int git_commit_config(const char *k, const char *v,
 	}
 	if (!strcmp(k, "commit.verbose")) {
 		int is_bool;
-		config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
+		config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
+							       &is_bool);
 		return 0;
 	}
 
diff --git a/builtin/config.c b/builtin/config.c
index 9b9f5527311..680269d263c 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -262,13 +262,14 @@ static int format_config(struct strbuf *buf, const char *key_,
 
 		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
@@ -424,7 +425,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;
@@ -439,12 +441,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
@@ -674,6 +676,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));
 
@@ -891,7 +894,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"
@@ -900,7 +903,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);
@@ -908,7 +911,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,
@@ -917,7 +920,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);
diff --git a/builtin/fetch.c b/builtin/fetch.c
index a4aa0fbb8f5..fe86dfeb62a 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -137,7 +137,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "submodule.fetchjobs")) {
-		fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v);
+		fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v, ctx->kvi);
 		return 0;
 	} else if (!strcmp(k, "fetch.recursesubmodules")) {
 		fetch_config->recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
@@ -145,7 +145,7 @@ static int git_fetch_config(const char *k, const char *v,
 	}
 
 	if (!strcmp(k, "fetch.parallel")) {
-		fetch_config->parallel = git_config_int(k, v);
+		fetch_config->parallel = git_config_int(k, v, ctx->kvi);
 		if (fetch_config->parallel < 0)
 			die(_("fetch.parallel cannot be negative"));
 		if (!fetch_config->parallel)
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 91a776e2f17..6d2826b07da 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -41,7 +41,7 @@ static int fsmonitor_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, FSMONITOR__IPC_THREADS)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, ctx->kvi);
 		if (i < 1)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__IPC_THREADS, i);
@@ -50,7 +50,7 @@ static int fsmonitor_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, FSMONITOR__START_TIMEOUT)) {
-		int i = git_config_int(var, value);
+		int i = git_config_int(var, value, ctx->kvi);
 		if (i < 0)
 			return error(_("value of '%s' out of range: %d"),
 				     FSMONITOR__START_TIMEOUT, i);
@@ -60,7 +60,7 @@ static int fsmonitor_config(const char *var, const char *value,
 
 	if (!strcmp(var, FSMONITOR__ANNOUNCE_STARTUP)) {
 		int is_bool;
-		int i = git_config_bool_or_int(var, value, &is_bool);
+		int i = git_config_bool_or_int(var, value, ctx->kvi, &is_bool);
 		if (i < 0)
 			return error(_("value of '%s' not bool or int: %d"),
 				     var, i);
diff --git a/builtin/grep.c b/builtin/grep.c
index 757d52b94ec..3a464e6faab 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -301,7 +301,7 @@ static int grep_cmd_config(const char *var, const char *value,
 		st = -1;
 
 	if (!strcmp(var, "grep.threads")) {
-		num_threads = git_config_int(var, value);
+		num_threads = git_config_int(var, value, ctx->kvi);
 		if (num_threads < 0)
 			die(_("invalid number of threads specified (%d) for %s"),
 			    num_threads, var);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index de8884ea5c2..e428f6d9a4a 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1587,13 +1587,13 @@ static int git_index_pack_config(const char *k, const char *v,
 	struct pack_idx_option *opts = cb;
 
 	if (!strcmp(k, "pack.indexversion")) {
-		opts->version = git_config_int(k, v);
+		opts->version = git_config_int(k, v, ctx->kvi);
 		if (opts->version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32), opts->version);
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		nr_threads = git_config_int(k, v);
+		nr_threads = git_config_int(k, v, ctx->kvi);
 		if (nr_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    nr_threads);
diff --git a/builtin/log.c b/builtin/log.c
index 09d6a13075b..c7337354aaf 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -574,7 +574,7 @@ static int git_log_config(const char *var, const char *value,
 	if (!strcmp(var, "format.subjectprefix"))
 		return git_config_string(&fmt_patch_subject_prefix, var, value);
 	if (!strcmp(var, "format.filenamemaxlength")) {
-		fmt_patch_name_max = git_config_int(var, value);
+		fmt_patch_name_max = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(var, "format.encodeemailheaders")) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 34aa0b483a0..38054a38b2b 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3139,23 +3139,23 @@ static int git_pack_config(const char *k, const char *v,
 			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(k, "pack.window")) {
-		window = git_config_int(k, v);
+		window = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.windowmemory")) {
-		window_memory_limit = git_config_ulong(k, v);
+		window_memory_limit = git_config_ulong(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.depth")) {
-		depth = git_config_int(k, v);
+		depth = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachesize")) {
-		max_delta_cache_size = git_config_int(k, v);
+		max_delta_cache_size = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.deltacachelimit")) {
-		cache_max_small_delta_size = git_config_int(k, v);
+		cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(k, "pack.writebitmaphashcache")) {
@@ -3181,7 +3181,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.threads")) {
-		delta_search_threads = git_config_int(k, v);
+		delta_search_threads = git_config_int(k, v, ctx->kvi);
 		if (delta_search_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    delta_search_threads);
@@ -3192,7 +3192,7 @@ static int git_pack_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "pack.indexversion")) {
-		pack_idx_opts.version = git_config_int(k, v);
+		pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
 		if (pack_idx_opts.version > 2)
 			die(_("bad pack.indexVersion=%"PRIu32),
 			    pack_idx_opts.version);
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 94d9898aff7..98f6f0038f0 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -158,12 +158,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.unpacklimit") == 0) {
-		receive_unpack_limit = git_config_int(var, value);
+		receive_unpack_limit = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "transfer.unpacklimit") == 0) {
-		transfer_unpack_limit = git_config_int(var, value);
+		transfer_unpack_limit = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -231,7 +231,7 @@ static int receive_pack_config(const char *var, const char *value,
 		return git_config_string(&cert_nonce_seed, var, value);
 
 	if (strcmp(var, "receive.certnonceslop") == 0) {
-		nonce_stamp_slop_limit = git_config_ulong(var, value);
+		nonce_stamp_slop_limit = git_config_ulong(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -246,12 +246,12 @@ static int receive_pack_config(const char *var, const char *value,
 	}
 
 	if (strcmp(var, "receive.keepalive") == 0) {
-		keepalive_in_sec = git_config_int(var, value);
+		keepalive_in_sec = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
 	if (strcmp(var, "receive.maxinputsize") == 0) {
-		max_input_size = git_config_int64(var, value);
+		max_input_size = git_config_int64(var, value, ctx->kvi);
 		return 0;
 	}
 
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index f8e9d85e77a..57185cf5f3d 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2192,13 +2192,13 @@ static int update_clone_task_finished(int result,
 }
 
 static int git_update_clone_config(const char *var, const char *value,
-				   const struct config_context *ctx UNUSED,
+				   const struct config_context *ctx,
 				   void *cb)
 {
 	int *max_jobs = cb;
 
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
 	return 0;
 }
 
diff --git a/config.c b/config.c
index d10faba56d3..4e3c4dcf415 100644
--- a/config.c
+++ b/config.c
@@ -73,18 +73,8 @@ struct config_reader {
 	 *
 	 * 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;
 };
 /*
  * Where possible, prefer to accept "struct config_reader" as an arg than to use
@@ -96,8 +86,6 @@ static struct config_reader the_reader;
 static inline void config_reader_push_source(struct config_reader *reader,
 					     struct config_source *top)
 {
-	if (reader->config_kvi)
-		BUG("source should not be set while iterating a config set");
 	top->prev = reader->source;
 	reader->source = top;
 }
@@ -112,12 +100,6 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
 	return ret;
 }
 
-static inline void config_reader_set_kvi(struct config_reader *reader,
-					 struct key_value_info *kvi)
-{
-	reader->config_kvi = kvi;
-}
-
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -1346,80 +1328,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;
 }
 
@@ -1524,7 +1504,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) {
@@ -1532,7 +1513,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)
@@ -1658,7 +1639,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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;
@@ -1670,7 +1651,7 @@ static int git_default_core_config(const char *var, const char *value,
 		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)
@@ -1681,7 +1662,7 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	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)
@@ -1695,7 +1676,7 @@ static int git_default_core_config(const char *var, const char *value,
 
 	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;
@@ -1706,17 +1687,17 @@ static int git_default_core_config(const char *var, const char *value,
 	}
 
 	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;
 	}
 
@@ -1995,12 +1976,12 @@ int git_default_config(const char *var, const char *value,
 	}
 
 	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)
@@ -2344,13 +2325,11 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
 		value_index = list->items[i].value_index;
 		values = &entry->value_list;
 
-		config_reader_set_kvi(reader, values->items[value_index].util);
 		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,
 					      ctx.kvi->filename,
 					      ctx.kvi->linenr);
-		config_reader_set_kvi(reader, NULL);
 	}
 }
 
@@ -2536,11 +2515,12 @@ int git_configset_add_file(struct config_set *set, const char *filename)
 	return git_config_from_file(config_set_callback, filename, &data);
 }
 
-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
@@ -2550,7 +2530,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;
 }
 
@@ -2603,7 +2586,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;
@@ -2613,7 +2596,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;
@@ -2626,8 +2609,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;
@@ -2636,8 +2621,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;
@@ -2646,7 +2633,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
@@ -2657,8 +2644,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;
@@ -2667,7 +2656,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;
@@ -2679,7 +2668,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;
@@ -2749,7 +2738,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,
@@ -3989,18 +3978,6 @@ 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 *config_origin_type_name(enum config_origin_type type)
 {
 	switch (type) {
@@ -4039,17 +4016,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;
-}
-
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
diff --git a/config.h b/config.h
index 82eeba94e71..bd366ddb5ef 100644
--- a/config.h
+++ b/config.h
@@ -249,22 +249,26 @@ int git_parse_maybe_bool(const char *);
  * Parse the string to an integer, including unit factors. Dies on error;
  * otherwise, returns the parsed result.
  */
-int git_config_int(const char *, const char *);
+int git_config_int(const char *, const char *, const struct key_value_info *);
 
-int64_t git_config_int64(const char *, const char *);
+int64_t git_config_int64(const char *, const char *,
+			 const struct key_value_info *);
 
 /**
  * Identical to `git_config_int`, but for unsigned longs.
  */
-unsigned long git_config_ulong(const char *, const char *);
+unsigned long git_config_ulong(const char *, const char *,
+			       const struct key_value_info *);
 
-ssize_t git_config_ssize_t(const char *, const char *);
+ssize_t git_config_ssize_t(const char *, const char *,
+			   const struct key_value_info *);
 
 /**
  * Same as `git_config_bool`, except that integers are returned as-is, and
  * an `is_bool` flag is unset.
  */
-int git_config_bool_or_int(const char *, const char *, int *);
+int git_config_bool_or_int(const char *, const char *,
+			   const struct key_value_info *, int *);
 
 /**
  * Parse a string into a boolean value, respecting keywords like "true" and
@@ -529,7 +533,8 @@ int git_configset_get(struct config_set *cs, const char *key);
  * touching `value`. The caller should not free or modify `value`, as it
  * is owned by the cache.
  */
-int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
+int git_configset_get_value(struct config_set *cs, const char *key,
+			    const char **dest, struct key_value_info *kvi);
 
 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
diff --git a/contrib/coccinelle/git_config_number.cocci b/contrib/coccinelle/git_config_number.cocci
new file mode 100644
index 00000000000..7b57dceefe6
--- /dev/null
+++ b/contrib/coccinelle/git_config_number.cocci
@@ -0,0 +1,27 @@
+@@
+identifier C1, C2, C3;
+@@
+(
+(
+git_config_int
+|
+git_config_int64
+|
+git_config_ulong
+|
+git_config_ssize_t
+)
+  (C1, C2
++ , ctx->kvi
+  )
+|
+(
+git_configset_get_value
+|
+git_config_bool_or_int
+)
+  (C1, C2
++ , ctx->kvi
+ , C3
+  )
+)
diff --git a/diff.c b/diff.c
index 0e382c8f7f0..460211e7d40 100644
--- a/diff.c
+++ b/diff.c
@@ -379,13 +379,14 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.context")) {
-		diff_context_default = git_config_int(var, value);
+		diff_context_default = git_config_int(var, value, ctx->kvi);
 		if (diff_context_default < 0)
 			return -1;
 		return 0;
 	}
 	if (!strcmp(var, "diff.interhunkcontext")) {
-		diff_interhunk_context_default = git_config_int(var, value);
+		diff_interhunk_context_default = git_config_int(var, value,
+								ctx->kvi);
 		if (diff_interhunk_context_default < 0)
 			return -1;
 		return 0;
@@ -411,7 +412,7 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.statgraphwidth")) {
-		diff_stat_graph_width = git_config_int(var, value);
+		diff_stat_graph_width = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp(var, "diff.external"))
@@ -450,7 +451,7 @@ int git_diff_basic_config(const char *var, const char *value,
 	const char *name;
 
 	if (!strcmp(var, "diff.renamelimit")) {
-		diff_rename_limit_default = git_config_int(var, value);
+		diff_rename_limit_default = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 10137444321..3ae59bde2f2 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -25,7 +25,7 @@ int fmt_merge_msg_config(const char *key, const char *value,
 {
 	if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
 		int is_bool;
-		merge_log_config = git_config_bool_or_int(key, value, &is_bool);
+		merge_log_config = git_config_bool_or_int(key, value, ctx->kvi, &is_bool);
 		if (!is_bool && merge_log_config < 0)
 			return error("%s: negative length %s", key, value);
 		if (is_bool && merge_log_config)
diff --git a/help.c b/help.c
index ac0ae5ac0dc..389382b1482 100644
--- a/help.c
+++ b/help.c
@@ -545,7 +545,7 @@ static struct cmdnames aliases;
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
 static int git_unknown_cmd_config(const char *var, const char *value,
-				  const struct config_context *ctx UNUSED,
+				  const struct config_context *ctx,
 				  void *cb UNUSED)
 {
 	const char *p;
@@ -560,7 +560,7 @@ static int git_unknown_cmd_config(const char *var, const char *value,
 		} else if (!strcmp(value, "prompt")) {
 			autocorrect = AUTOCORRECT_PROMPT;
 		} else {
-			int v = git_config_int(var, value);
+			int v = git_config_int(var, value, ctx->kvi);
 			autocorrect = (v < 0)
 				? AUTOCORRECT_IMMEDIATELY : v;
 		}
diff --git a/http.c b/http.c
index 762502828c9..b12b234bde1 100644
--- a/http.c
+++ b/http.c
@@ -414,21 +414,21 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.minsessions", var)) {
-		min_curl_sessions = git_config_int(var, value);
+		min_curl_sessions = git_config_int(var, value, ctx->kvi);
 		if (min_curl_sessions > 1)
 			min_curl_sessions = 1;
 		return 0;
 	}
 	if (!strcmp("http.maxrequests", var)) {
-		max_requests = git_config_int(var, value);
+		max_requests = git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedlimit", var)) {
-		curl_low_speed_limit = (long)git_config_int(var, value);
+		curl_low_speed_limit = (long)git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 	if (!strcmp("http.lowspeedtime", var)) {
-		curl_low_speed_time = (long)git_config_int(var, value);
+		curl_low_speed_time = (long)git_config_int(var, value, ctx->kvi);
 		return 0;
 	}
 
@@ -464,7 +464,7 @@ static int http_options(const char *var, const char *value,
 	}
 
 	if (!strcmp("http.postbuffer", var)) {
-		http_post_buffer = git_config_ssize_t(var, value);
+		http_post_buffer = git_config_ssize_t(var, value, ctx->kvi);
 		if (http_post_buffer < 0)
 			warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
 		if (http_post_buffer < LARGE_PACKET_MAX)
diff --git a/imap-send.c b/imap-send.c
index 47777e76861..3518a4ace6c 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1342,7 +1342,7 @@ static int git_imap_config(const char *var, const char *val,
 	else if (!strcmp("imap.authmethod", var))
 		return git_config_string(&server.auth_method, var, val);
 	else if (!strcmp("imap.port", var))
-		server.port = git_config_int(var, val);
+		server.port = git_config_int(var, val, ctx->kvi);
 	else if (!strcmp("imap.host", var)) {
 		if (!val) {
 			git_die_config("imap.host", "Missing value for 'imap.host'");
diff --git a/sequencer.c b/sequencer.c
index 34754d17596..a0aaee3056d 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2883,7 +2883,7 @@ static int git_config_string_dup(char **dest,
 }
 
 static int populate_opts_cb(const char *key, const char *value,
-			    const struct config_context *ctx UNUSED,
+			    const struct config_context *ctx,
 			    void *data)
 {
 	struct replay_opts *opts = data;
@@ -2892,26 +2892,26 @@ static int populate_opts_cb(const char *key, const char *value,
 	if (!value)
 		error_flag = 0;
 	else if (!strcmp(key, "options.no-commit"))
-		opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
+		opts->no_commit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.edit"))
-		opts->edit = git_config_bool_or_int(key, value, &error_flag);
+		opts->edit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty"))
 		opts->allow_empty =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-empty-message"))
 		opts->allow_empty_message =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.keep-redundant-commits"))
 		opts->keep_redundant_commits =
-			git_config_bool_or_int(key, value, &error_flag);
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.signoff"))
-		opts->signoff = git_config_bool_or_int(key, value, &error_flag);
+		opts->signoff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.record-origin"))
-		opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
+		opts->record_origin = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.allow-ff"))
-		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
+		opts->allow_ff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
-		opts->mainline = git_config_int(key, value);
+		opts->mainline = git_config_int(key, value, ctx->kvi);
 	else if (!strcmp(key, "options.strategy"))
 		git_config_string_dup(&opts->strategy, key, value);
 	else if (!strcmp(key, "options.gpg-sign"))
@@ -2920,7 +2920,7 @@ static int populate_opts_cb(const char *key, const char *value,
 		strvec_push(&opts->xopts, value);
 	} else if (!strcmp(key, "options.allow-rerere-auto"))
 		opts->allow_rerere_auto =
-			git_config_bool_or_int(key, value, &error_flag) ?
+			git_config_bool_or_int(key, value, ctx->kvi, &error_flag) ?
 				RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
 	else if (!strcmp(key, "options.default-msg-cleanup")) {
 		opts->explicit_cleanup = 1;
diff --git a/setup.c b/setup.c
index fadba5bab4b..ca80fb464bb 100644
--- a/setup.c
+++ b/setup.c
@@ -597,7 +597,7 @@ static int check_repo_format(const char *var, const char *value,
 	const char *ext;
 
 	if (strcmp(var, "core.repositoryformatversion") == 0)
-		data->version = git_config_int(var, value);
+		data->version = git_config_int(var, value, ctx->kvi);
 	else if (skip_prefix(var, "extensions.", &ext)) {
 		switch (handle_extension_v0(var, value, ext, data)) {
 		case EXTENSION_ERROR:
diff --git a/submodule-config.c b/submodule-config.c
index 3f25bd13674..54be580e2a9 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -304,9 +304,10 @@ static int parse_fetch_recurse(const char *opt, const char *arg,
 	}
 }
 
-int parse_submodule_fetchjobs(const char *var, const char *value)
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      const struct key_value_info *kvi)
 {
-	int fetchjobs = git_config_int(var, value);
+	int fetchjobs = git_config_int(var, value, kvi);
 	if (fetchjobs < 0)
 		die(_("negative values not allowed for submodule.fetchJobs"));
 	if (!fetchjobs)
@@ -849,14 +850,14 @@ struct fetch_config {
 };
 
 static int gitmodules_fetch_config(const char *var, const char *value,
-				   const struct config_context *ctx UNUSED,
+				   const struct config_context *ctx,
 				   void *cb)
 {
 	struct fetch_config *config = cb;
 	if (!strcmp(var, "submodule.fetchjobs")) {
 		if (config->max_children)
 			*(config->max_children) =
-				parse_submodule_fetchjobs(var, value);
+				parse_submodule_fetchjobs(var, value, ctx->kvi);
 		return 0;
 	} else if (!strcmp(var, "fetch.recursesubmodules")) {
 		if (config->recurse_submodules)
@@ -878,12 +879,12 @@ void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 }
 
 static int gitmodules_update_clone_config(const char *var, const char *value,
-					  const struct config_context *ctx UNUSED,
+					  const struct config_context *ctx,
 					  void *cb)
 {
 	int *max_jobs = cb;
 	if (!strcmp(var, "submodule.fetchjobs"))
-		*max_jobs = parse_submodule_fetchjobs(var, value);
+		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
 	return 0;
 }
 
diff --git a/submodule-config.h b/submodule-config.h
index c2045875bbb..2a37689cc27 100644
--- a/submodule-config.h
+++ b/submodule-config.h
@@ -50,7 +50,8 @@ struct repository;
 
 void submodule_cache_free(struct submodule_cache *cache);
 
-int parse_submodule_fetchjobs(const char *var, const char *value);
+int parse_submodule_fetchjobs(const char *var, const char *value,
+			      const struct key_value_info *kvi);
 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 struct option;
 int option_fetch_parse_recurse_submodules(const struct option *opt,
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index 3f4c3678318..ed444ca4c25 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -63,12 +63,12 @@ static int iterate_cb(const char *var, const char *value,
 }
 
 static int parse_int_cb(const char *var, const char *value,
-			const struct config_context *ctx UNUSED, void *data)
+			const struct config_context *ctx, void *data)
 {
 	const char *key_to_match = data;
 
 	if (!strcmp(key_to_match, var)) {
-		int parsed = git_config_int(value, value);
+		int parsed = git_config_int(value, value, ctx->kvi);
 		printf("%d\n", parsed);
 	}
 	return 0;
@@ -182,7 +182,7 @@ int cmd__config(int argc, const char **argv)
 				goto exit2;
 			}
 		}
-		if (!git_configset_get_value(&cs, argv[2], &v)) {
+		if (!git_configset_get_value(&cs, argv[2], &v, NULL)) {
 			if (!v)
 				printf("(NULL)\n");
 			else
diff --git a/upload-pack.c b/upload-pack.c
index 951fd1f9c25..c03415a5460 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1275,7 +1275,8 @@ static int find_symref(const char *refname,
 }
 
 static int parse_object_filter_config(const char *var, const char *value,
-				       struct upload_pack_data *data)
+				      const struct key_value_info *kvi,
+				      struct upload_pack_data *data)
 {
 	struct strbuf buf = STRBUF_INIT;
 	const char *sub, *key;
@@ -1302,7 +1303,8 @@ static int parse_object_filter_config(const char *var, const char *value,
 		}
 		string_list_insert(&data->allowed_filters, buf.buf)->util =
 			(void *)(intptr_t)1;
-		data->tree_filter_max_depth = git_config_ulong(var, value);
+		data->tree_filter_max_depth = git_config_ulong(var, value,
+							       kvi);
 	}
 
 	strbuf_release(&buf);
@@ -1310,7 +1312,7 @@ static int parse_object_filter_config(const char *var, const char *value,
 }
 
 static int upload_pack_config(const char *var, const char *value,
-			      const struct config_context *ctx UNUSED,
+			      const struct config_context *ctx,
 			      void *cb_data)
 {
 	struct upload_pack_data *data = cb_data;
@@ -1331,7 +1333,7 @@ static int upload_pack_config(const char *var, const char *value,
 		else
 			data->allow_uor &= ~ALLOW_ANY_SHA1;
 	} else if (!strcmp("uploadpack.keepalive", var)) {
-		data->keepalive = git_config_int(var, value);
+		data->keepalive = git_config_int(var, value, ctx->kvi);
 		if (!data->keepalive)
 			data->keepalive = -1;
 	} else if (!strcmp("uploadpack.allowfilter", var)) {
@@ -1346,7 +1348,7 @@ static int upload_pack_config(const char *var, const char *value,
 		data->advertise_sid = git_config_bool(var, value);
 	}
 
-	if (parse_object_filter_config(var, value, data) < 0)
+	if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
 		return -1;
 
 	return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
diff --git a/worktree.c b/worktree.c
index c448fecd4b3..af277ec9010 100644
--- a/worktree.c
+++ b/worktree.c
@@ -835,7 +835,7 @@ int init_worktree_config(struct repository *r)
 	 * Relocate that value to avoid breaking all worktrees with this
 	 * upgrade to worktree config.
 	 */
-	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) {
+	if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
 		if ((res = move_config_setting("core.worktree", core_worktree,
 					       common_config_file,
 					       main_worktree_file)))
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 09/11] config.c: remove config_reader from configsets
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (7 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 08/11] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 10/11] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
                           ` (3 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Remove the last usage of "struct config_reader" from configsets by
copying the "kvi" arg instead of recomputing "kvi" from
config_reader.source. Since we no longer need to pass both "struct
config_reader" and "struct config_set" in a single "void *cb", remove
"struct configset_add_data" too.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 45 +++++++++++----------------------------------
 1 file changed, 11 insertions(+), 34 deletions(-)

diff --git a/config.c b/config.c
index 4e3c4dcf415..50dbd89a6d6 100644
--- a/config.c
+++ b/config.c
@@ -2311,8 +2311,7 @@ int config_with_options(config_fn_t fn, void *data,
 	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;
@@ -2406,7 +2405,6 @@ static int configset_find_element(struct config_set *set, const char *key,
 }
 
 static int configset_add_value(const struct key_value_info *kvi_p,
-			       struct config_reader *reader,
 			       struct config_set *set, const char *key,
 			       const char *value)
 {
@@ -2437,13 +2435,7 @@ static int configset_add_value(const struct key_value_info *kvi_p,
 	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) {
-		kvi_from_source(reader->source, kvi_p->scope, kv_info);
-	} else {
-		kvi_from_param(kv_info);
-	}
+	*kv_info = *kvi_p;
 	si->util = kv_info;
 
 	return 0;
@@ -2491,28 +2483,18 @@ 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,
 			       const struct config_context *ctx,
 			       void *cb)
 {
-	struct configset_add_data *data = cb;
-	configset_add_value(ctx->kvi, 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,
@@ -2678,7 +2660,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;
@@ -2690,10 +2671,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
@@ -2725,7 +2704,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)
@@ -2832,19 +2811,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' */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 10/11] config: add kvi.path, use it to evaluate includes
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (8 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 09/11] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 19:26         ` [PATCH v5 11/11] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
                           ` (2 subsequent siblings)
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

Include directives are evaluated using the path of the config file. To
reduce the dependence on "config_reader.source", add a new
"key_value_info.path" member and use that instead of
"config_source.path". This allows us to remove a "struct config_reader
*" field from "struct config_include_data", which will subsequently
allow us to remove "struct config_reader" entirely.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 40 +++++++++++++++++++---------------------
 config.h |  2 ++
 2 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/config.c b/config.c
index 50dbd89a6d6..8d342d84255 100644
--- a/config.c
+++ b/config.c
@@ -162,7 +162,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.
@@ -181,8 +180,7 @@ 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 struct key_value_info *kvi,
+static int handle_path_include(const struct key_value_info *kvi,
 			       const char *path,
 			       struct config_include_data *inc)
 {
@@ -205,14 +203,14 @@ static int handle_path_include(struct config_source *cs,
 	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;
 	}
@@ -220,8 +218,8 @@ static int handle_path_include(struct config_source *cs,
 	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_with_options(git_config_include, path, inc,
 							kvi->scope, NULL);
@@ -239,7 +237,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;
@@ -256,11 +254,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?");
@@ -275,7 +273,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)
 {
@@ -292,7 +290,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)
@@ -428,16 +426,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,
@@ -453,7 +451,6 @@ static int git_config_include(const char *var, const char *value,
 			      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;
@@ -467,16 +464,16 @@ static int git_config_include(const char *var, const char *value,
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, ctx->kvi, 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, ctx->kvi, value, inc);
+		ret = handle_path_include(ctx->kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -661,6 +658,7 @@ void kvi_from_param(struct key_value_info *out)
 	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,
@@ -1064,6 +1062,7 @@ static void kvi_from_source(struct config_source *cs,
 	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,
@@ -2282,7 +2281,6 @@ 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;
 	}
diff --git a/config.h b/config.h
index bd366ddb5ef..b5573e67a00 100644
--- a/config.h
+++ b/config.h
@@ -116,12 +116,14 @@ struct key_value_info {
 	int linenr;
 	enum config_origin_type origin_type;
 	enum config_scope scope;
+	const char *path;
 };
 #define KVI_INIT { \
 	.filename = NULL, \
 	.linenr = -1, \
 	.origin_type = CONFIG_ORIGIN_UNKNOWN, \
 	.scope = CONFIG_SCOPE_UNKNOWN, \
+	.path = NULL, \
 }
 
 /* Captures additional information that a config callback can use. */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 115+ messages in thread

* [PATCH v5 11/11] config: pass source to config_parser_event_fn_t
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (9 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 10/11] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
@ 2023-06-28 19:26         ` Glen Choo via GitGitGadget
  2023-06-28 22:23         ` [PATCH v5 00/11] config: remove global state from config iteration Jonathan Tan
  2023-07-11 18:41         ` Phillip Wood
  12 siblings, 0 replies; 115+ messages in thread
From: Glen Choo via GitGitGadget @ 2023-06-28 19:26 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo, Glen Choo

From: Glen Choo <chooglen@google.com>

..so that the callback can use a "struct config_source" parameter
instead of "config_reader.source". "struct config_source" is internal to
config.c, so we are adding a pointer to a struct defined in config.c
into a public function signature defined in config.h, but this is okay
because this function has only ever been (and probably ever will be)
used internally by config.c.

As a result, the_reader isn't used anywhere, so "struct config_reader"
is obsolete (it was only intended to be used with the_reader). Remove
them.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 77 ++++++++++----------------------------------------------
 config.h |  6 +++++
 2 files changed, 19 insertions(+), 64 deletions(-)

diff --git a/config.c b/config.c
index 8d342d84255..b3de01239d6 100644
--- a/config.c
+++ b/config.c
@@ -66,40 +66,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).
-	 */
-	struct config_source *source;
-};
-/*
- * 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 int pack_compression_seen;
 static int zlib_compression_seen;
 
@@ -752,14 +718,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;
 	struct key_value_info kvi = KVI_INIT;
 
-	source.origin_type = CONFIG_ORIGIN_CMDLINE;
-	config_reader_push_source(&the_reader, &source);
-
 	kvi_from_param(&kvi);
-
 	env = getenv(CONFIG_COUNT_ENVIRONMENT);
 	if (env) {
 		unsigned long count;
@@ -816,7 +777,6 @@ out:
 	strbuf_release(&envvar);
 	strvec_clear(&to_free);
 	free(envw);
-	config_reader_pop_source(&the_reader);
 	return ret;
 }
 
@@ -1045,7 +1005,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;
@@ -2002,8 +1962,7 @@ int git_default_config(const char *var, const char *value,
  * 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,
+static int do_config_from(struct config_source *top, config_fn_t fn,
 			  void *data, enum config_scope scope,
 			  const struct config_options *opts)
 {
@@ -2016,21 +1975,17 @@ 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, &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, enum config_scope scope,
@@ -2049,7 +2004,7 @@ 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, scope, opts);
+	ret = do_config_from(&top, fn, data, scope, opts);
 	funlockfile(f);
 	return ret;
 }
@@ -2057,8 +2012,8 @@ static int do_config_from_file(struct config_reader *reader,
 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, scope, 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,
@@ -2072,9 +2027,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, scope,
-					  opts);
+		ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
+					  filename, f, data, scope, opts);
 		fclose(f);
 	}
 	return ret;
@@ -2105,7 +2059,7 @@ 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, scope, opts);
+	return do_config_from(&top, fn, data, scope, opts);
 }
 
 int git_config_from_blob_oid(config_fn_t fn,
@@ -2198,8 +2152,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)
 {
@@ -2299,7 +2252,7 @@ int config_with_options(config_fn_t fn, void *data,
 		ret = git_config_from_blob_ref(fn, repo, config_source->blob,
 					       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) {
@@ -3009,7 +2962,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;
@@ -3055,11 +3007,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;
@@ -3380,8 +3331,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)
diff --git a/config.h b/config.h
index b5573e67a00..6332d749047 100644
--- a/config.h
+++ b/config.h
@@ -72,6 +72,7 @@ enum config_event_t {
 	CONFIG_EVENT_ERROR
 };
 
+struct config_source;
 /*
  * The parser event function (if not NULL) is called with the event type and
  * the begin/end offsets of the parsed elements.
@@ -81,6 +82,7 @@ enum config_event_t {
  */
 typedef int (*config_parser_event_fn_t)(enum config_event_t type,
 					size_t begin_offset, size_t end_offset,
+					struct config_source *cs,
 					void *event_fn_data);
 
 struct config_options {
@@ -100,6 +102,10 @@ struct config_options {
 
 	const char *commondir;
 	const char *git_dir;
+	/*
+	 * event_fn and event_fn_data are for internal use only. Handles events
+	 * emitted by the config parser.
+	 */
 	config_parser_event_fn_t event_fn;
 	void *event_fn_data;
 	enum config_error_action {
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 115+ messages in thread

* Re: [PATCH v5 00/11] config: remove global state from config iteration
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (10 preceding siblings ...)
  2023-06-28 19:26         ` [PATCH v5 11/11] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
@ 2023-06-28 22:23         ` Jonathan Tan
  2023-06-28 22:47           ` Junio C Hamano
  2023-07-11 18:41         ` Phillip Wood
  12 siblings, 1 reply; 115+ messages in thread
From: Jonathan Tan @ 2023-06-28 22:23 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget
  Cc: Jonathan Tan, git, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Phillip Wood, Jeff King, Glen Choo

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> As promised, this version addresses the comments on v3.
> 
> = Changes since v4
> 
> - Squash 6-7/12 since `test_must_fail` doesn't catch BUG()
> - Move a hunk to later in the series where it belongs
> - Replace a memcpy with `*a = *b`
> 
> = Changes since v3
> 
> - Rebase onto newer 'master'
> - Move the 'remove UNUSED from tr2_cfg_cb' hunk from 9/12 -> 8/12. It should
>   have been there all along; v3 8/12 didn't build at all.

Looks like all my comments have been addressed, so this patch set looks
good to me.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v5 00/11] config: remove global state from config iteration
  2023-06-28 22:23         ` [PATCH v5 00/11] config: remove global state from config iteration Jonathan Tan
@ 2023-06-28 22:47           ` Junio C Hamano
  0 siblings, 0 replies; 115+ messages in thread
From: Junio C Hamano @ 2023-06-28 22:47 UTC (permalink / raw)
  To: Jonathan Tan
  Cc: Glen Choo via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Emily Shaffer,
	Phillip Wood, Jeff King, Glen Choo

Jonathan Tan <jonathantanmy@google.com> writes:

> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> As promised, this version addresses the comments on v3.
>> 
>> = Changes since v4
>> 
>> - Squash 6-7/12 since `test_must_fail` doesn't catch BUG()
>> - Move a hunk to later in the series where it belongs
>> - Replace a memcpy with `*a = *b`
>> 
>> = Changes since v3
>> 
>> - Rebase onto newer 'master'
>> - Move the 'remove UNUSED from tr2_cfg_cb' hunk from 9/12 -> 8/12. It should
>>   have been there all along; v3 8/12 didn't build at all.
>
> Looks like all my comments have been addressed, so this patch set looks
> good to me.

Thanks.  Queued.

^ permalink raw reply	[flat|nested] 115+ messages in thread

* Re: [PATCH v5 00/11] config: remove global state from config iteration
  2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
                           ` (11 preceding siblings ...)
  2023-06-28 22:23         ` [PATCH v5 00/11] config: remove global state from config iteration Jonathan Tan
@ 2023-07-11 18:41         ` Phillip Wood
  12 siblings, 0 replies; 115+ messages in thread
From: Phillip Wood @ 2023-07-11 18:41 UTC (permalink / raw)
  To: Glen Choo via GitGitGadget, git
  Cc: Jonathan Tan, Ævar Arnfjörð Bjarmason,
	Emily Shaffer, Jeff King, Glen Choo

Hi Glen

Sorry it has taken me so long to look at these patches. I see they're in 
master now but I have just had a quick look through anyway and they 
looked good. It would definitely be nice to fix the hacky handling of 
"git checkout --conflict" at some point but that is a long standing 
problem and not the fault of this series.

Best Wishes

Phillip

On 28/06/2023 20:26, Glen Choo via GitGitGadget wrote:
> As promised, this version addresses the comments on v3.
> 
> = Changes since v4
> 
> - Squash 6-7/12 since `test_must_fail` doesn't catch BUG()
> - Move a hunk to later in the series where it belongs
> - Replace a memcpy with `*a = *b`
> 
> = Changes since v3
> 
> - Rebase onto newer 'master'
> - Move the 'remove UNUSED from tr2_cfg_cb' hunk from 9/12 -> 8/12. It should
>    have been there all along; v3 8/12 didn't build at all.
> 
> 
> Glen Choo (11):
>    config: inline git_color_default_config
>    urlmatch.h: use config_fn_t type
>    config: add ctx arg to config_fn_t
>    config.c: pass ctx in configsets
>    config: pass ctx with config files
>    config.c: pass ctx with CLI config
>    trace2: plumb config kvi
>    config: pass kvi to die_bad_number()
>    config.c: remove config_reader from configsets
>    config: add kvi.path, use it to evaluate includes
>    config: pass source to config_parser_event_fn_t
> 
>   alias.c                                       |   3 +-
>   archive-tar.c                                 |   5 +-
>   archive-zip.c                                 |   1 +
>   builtin/add.c                                 |   8 +-
>   builtin/blame.c                               |   5 +-
>   builtin/branch.c                              |   8 +-
>   builtin/cat-file.c                            |   5 +-
>   builtin/checkout.c                            |  12 +-
>   builtin/clean.c                               |   9 +-
>   builtin/clone.c                               |  11 +-
>   builtin/column.c                              |   3 +-
>   builtin/commit-graph.c                        |   3 +-
>   builtin/commit.c                              |  20 +-
>   builtin/config.c                              |  72 ++-
>   builtin/difftool.c                            |   5 +-
>   builtin/fetch.c                               |  13 +-
>   builtin/fsmonitor--daemon.c                   |  11 +-
>   builtin/grep.c                                |  12 +-
>   builtin/help.c                                |   5 +-
>   builtin/index-pack.c                          |   9 +-
>   builtin/log.c                                 |  12 +-
>   builtin/merge.c                               |   7 +-
>   builtin/multi-pack-index.c                    |   1 +
>   builtin/pack-objects.c                        |  19 +-
>   builtin/patch-id.c                            |   5 +-
>   builtin/pull.c                                |   5 +-
>   builtin/push.c                                |   5 +-
>   builtin/read-tree.c                           |   5 +-
>   builtin/rebase.c                              |   5 +-
>   builtin/receive-pack.c                        |  15 +-
>   builtin/reflog.c                              |   7 +-
>   builtin/remote.c                              |  15 +-
>   builtin/repack.c                              |   5 +-
>   builtin/reset.c                               |   5 +-
>   builtin/send-pack.c                           |   5 +-
>   builtin/show-branch.c                         |   8 +-
>   builtin/stash.c                               |   5 +-
>   builtin/submodule--helper.c                   |   3 +-
>   builtin/tag.c                                 |   9 +-
>   builtin/var.c                                 |   5 +-
>   builtin/worktree.c                            |   5 +-
>   bundle-uri.c                                  |   9 +-
>   color.c                                       |   8 -
>   color.h                                       |   6 +-
>   compat/mingw.c                                |   3 +-
>   compat/mingw.h                                |   4 +-
>   config.c                                      | 552 +++++++-----------
>   config.h                                      |  80 ++-
>   connect.c                                     |   4 +-
>   .../coccinelle/config_fn_ctx.pending.cocci    | 144 +++++
>   contrib/coccinelle/git_config_number.cocci    |  27 +
>   convert.c                                     |   4 +-
>   credential.c                                  |   1 +
>   delta-islands.c                               |   4 +-
>   diff.c                                        |  19 +-
>   diff.h                                        |   7 +-
>   fetch-pack.c                                  |   5 +-
>   fmt-merge-msg.c                               |   7 +-
>   fmt-merge-msg.h                               |   3 +-
>   fsck.c                                        |  12 +-
>   fsck.h                                        |   4 +-
>   git-compat-util.h                             |   2 +
>   gpg-interface.c                               |   7 +-
>   grep.c                                        |   7 +-
>   grep.h                                        |   4 +-
>   help.c                                        |   9 +-
>   http.c                                        |  15 +-
>   ident.c                                       |   4 +-
>   ident.h                                       |   4 +-
>   imap-send.c                                   |   7 +-
>   ll-merge.c                                    |   1 +
>   ls-refs.c                                     |   1 +
>   mailinfo.c                                    |   5 +-
>   notes-utils.c                                 |   4 +-
>   notes.c                                       |   4 +-
>   pager.c                                       |   5 +-
>   pretty.c                                      |   1 +
>   promisor-remote.c                             |   4 +-
>   remote.c                                      |   8 +-
>   revision.c                                    |   4 +-
>   scalar.c                                      |   4 +-
>   sequencer.c                                   |  29 +-
>   setup.c                                       |  18 +-
>   submodule-config.c                            |  31 +-
>   submodule-config.h                            |   3 +-
>   t/helper/test-config.c                        |  24 +-
>   t/helper/test-userdiff.c                      |   4 +-
>   t/t1300-config.sh                             |  27 +
>   trace2.c                                      |   4 +-
>   trace2.h                                      |   3 +-
>   trace2/tr2_cfg.c                              |  16 +-
>   trace2/tr2_sysenv.c                           |   3 +-
>   trace2/tr2_tgt.h                              |   4 +-
>   trace2/tr2_tgt_event.c                        |   4 +-
>   trace2/tr2_tgt_normal.c                       |   4 +-
>   trace2/tr2_tgt_perf.c                         |   4 +-
>   trailer.c                                     |   2 +
>   upload-pack.c                                 |  18 +-
>   urlmatch.c                                    |   7 +-
>   urlmatch.h                                    |   8 +-
>   worktree.c                                    |   2 +-
>   xdiff-interface.c                             |   5 +-
>   xdiff-interface.h                             |   4 +-
>   103 files changed, 960 insertions(+), 638 deletions(-)
>   create mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci
>   create mode 100644 contrib/coccinelle/git_config_number.cocci
> 
> 
> base-commit: 6ff334181cfb6485d3ba50843038209a2a253907
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1497%2Fchooglen%2Fconfig%2Fno-global-v5
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1497/chooglen/config/no-global-v5
> Pull-Request: https://github.com/git/git/pull/1497
> 
> Range-diff vs v4:
> 
>    1:  7bfffb454c5 =  1:  7bfffb454c5 config: inline git_color_default_config
>    2:  739c519ce62 =  2:  739c519ce62 urlmatch.h: use config_fn_t type
>    3:  a9a0a50f32a =  3:  a9a0a50f32a config: add ctx arg to config_fn_t
>    4:  39b2e291f86 =  4:  39b2e291f86 config.c: pass ctx in configsets
>    5:  bfc6d2833c5 =  5:  bfc6d2833c5 config: pass ctx with config files
>    6:  897bdc759b5 <  -:  ----------- builtin/config.c: test misuse of format_config()
>    7:  33e4437737d !  6:  7b24eefbcf3 config.c: pass ctx with CLI config
>       @@ Commit message
>            * git_config_parse_parameter() hasn't been setting config source
>              information, so plumb "kvi" there too.
>        
>       -    * "git config --get-urlmatch --show-scope" iterates config to collect
>       -      values, but then attempts to display the scope after config iteration.
>       -      Fix this by copying the "kvi" value in the collection phase so that it
>       -      can be read back later. This means that we can now support "git config
>       -      --get-urlmatch --show-origin" (we don't allow this combination of args
>       -      because of this bug), but that is left unchanged for now.
>       +    * Several sites in builtin/config.c have been calling current_config_*()
>       +      functions outside of config callbacks (indirectly, via the
>       +      format_config() helper), which means they're reading state that isn't
>       +      set correctly:
>        
>       -    * "git config --default" doesn't have config source metadata when
>       -      displaying the default value. Fix this by treating the default value
>       -      as if it came from the command line (e.g. like we do with "git -c" or
>       -      "git config --file"), using kvi_from_param().
>       +      * "git config --get-urlmatch --show-scope" iterates config to collect
>       +        values, but then attempts to display the scope after config
>       +        iteration, causing the "unknown" scope to be shown instead of the
>       +        config file's scope. It's clear that this wasn't intended: we knew
>       +        that "--get-urlmatch" couldn't show config source metadata, which is
>       +        why "--show-origin" was marked incompatible with "--get-urlmatch"
>       +        when it was introduced [1]. It was most likely a mistake that we
>       +        allowed "--show-scope" to sneak through.
>       +
>       +        Fix this by copying the "kvi" value in the collection phase so that
>       +        it can be read back later. This means that we can now support "git
>       +        config --get-urlmatch --show-origin", but that is left unchanged
>       +        for now.
>       +
>       +      * "git config --default" doesn't have config source metadata when
>       +        displaying the default value, so "--show-scope" also results in
>       +        "unknown", and "--show-origin" results in a BUG(). Fix this by
>       +        treating the default value as if it came from the command line (e.g.
>       +        like we do with "git -c" or "git config --file"), using
>       +        kvi_from_param().
>       +
>       +    [1] https://lore.kernel.org/git/20160205112001.GA13397@sigill.intra.peff.net/
>        
>            Signed-off-by: Glen Choo <chooglen@google.com>
>        
>       @@ config.c: static int configset_find_element(struct config_set *set, const char *
>         			       const char *value)
>         {
>        @@ config.c: 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)
>       + 		BUG("configset_add_value has no source");
>         	if (reader->source->name) {
>        -		kvi_from_source(reader->source, current_config_scope(), kv_info);
>        +		kvi_from_source(reader->source, kvi_p->scope, kv_info);
>       @@ config.h: void git_global_config(char **user, char **xdg);
>          * Match and parse a config key of the form:
>        
>         ## t/t1300-config.sh ##
>       -@@ t/t1300-config.sh: test_expect_success 'urlmatch with --show-scope' '
>       - 	EOF
>       +@@ t/t1300-config.sh: test_expect_success 'urlmatch' '
>       + 	test_cmp expect actual
>       + '
>         
>       - 	cat >expect <<-EOF &&
>       --	unknown	http.cookiefile /tmp/cookie.txt
>       --	unknown	http.sslverify false
>       ++test_expect_success 'urlmatch with --show-scope' '
>       ++	cat >.git/config <<-\EOF &&
>       ++	[http "https://weak.example.com"]
>       ++		sslVerify = false
>       ++		cookieFile = /tmp/cookie.txt
>       ++	EOF
>       ++
>       ++	cat >expect <<-EOF &&
>        +	local	http.cookiefile /tmp/cookie.txt
>        +	local	http.sslverify false
>       - 	EOF
>       - 	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
>       - 	test_cmp expect actual
>       ++	EOF
>       ++	git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
>       ++	test_cmp expect actual
>       ++'
>       ++
>       + test_expect_success 'urlmatch favors more specific URLs' '
>       + 	cat >.git/config <<-\EOF &&
>       + 	[http "https://example.com/"]
>        @@ t/t1300-config.sh: test_expect_success '--show-origin blob ref' '
>       + 	test_cmp expect output
>         '
>         
>       - test_expect_success '--show-origin with --default' '
>       --	test_must_fail git config --show-origin --default foo some.key
>       ++test_expect_success '--show-origin with --default' '
>        +	git config --show-origin --default foo some.key >actual &&
>        +	echo "command line:	foo" >expect &&
>        +	test_cmp expect actual
>       - '
>       -
>       ++'
>       ++
>         test_expect_success '--show-scope with --list' '
>       + 	cat >expect <<-EOF &&
>       + 	global	user.global=true
>        @@ t/t1300-config.sh: test_expect_success '--show-scope with --show-origin' '
>       -
>       - test_expect_success '--show-scope with --default' '
>       - 	git config --show-scope --default foo some.key >actual &&
>       --	echo "unknown	foo" >expect &&
>       -+	echo "command	foo" >expect &&
>       - 	test_cmp expect actual
>       + 	test_cmp expect output
>         '
>         
>       ++test_expect_success '--show-scope with --default' '
>       ++	git config --show-scope --default foo some.key >actual &&
>       ++	echo "command	foo" >expect &&
>       ++	test_cmp expect actual
>       ++'
>       ++
>       + test_expect_success 'override global and system config' '
>       + 	test_when_finished rm -f \"\$HOME\"/.gitconfig &&
>       + 	cat >"$HOME"/.gitconfig <<-EOF &&
>    8:  9bd5f60282c =  7:  7d64dcbdade trace2: plumb config kvi
>    9:  114723ee4a7 =  8:  9e71c10ca0a config: pass kvi to die_bad_number()
>   10:  807057b6d7f !  9:  4776600e790 config.c: remove config_reader from configsets
>       @@ config.c: static int configset_add_value(const struct key_value_info *kvi_p,
>         	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) {
>        -		kvi_from_source(reader->source, kvi_p->scope, kv_info);
>        -	} else {
>        -		kvi_from_param(kv_info);
>        -	}
>       -+	memcpy(kv_info, kvi_p, sizeof(struct key_value_info));
>       ++	*kv_info = *kvi_p;
>         	si->util = kv_info;
>         
>         	return 0;
>   11:  3f0f84df972 = 10:  2b33977aba6 config: add kvi.path, use it to evaluate includes
>   12:  fe2f154fe8b = 11:  8347d3c9b80 config: pass source to config_parser_event_fn_t
> 

^ permalink raw reply	[flat|nested] 115+ messages in thread

end of thread, other threads:[~2023-07-11 18:41 UTC | newest]

Thread overview: 115+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-21 19:13 [PATCH 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 01/14] config.c: introduce kvi_fn(), use it for configsets Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 02/14] config.c: use kvi for CLI config Glen Choo via GitGitGadget
2023-05-01 11:06   ` Ævar Arnfjörð Bjarmason
2023-04-21 19:13 ` [PATCH 03/14] config: use kvi for config files Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 04/14] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 05/14] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 06/14] config: inline git_color_default_config Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 07/14] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 08/14] (RFC-only) config: add kvi arg to config_fn_t Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 09/14] (RFC-only) config: apply cocci to config_fn_t implementations Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 10/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
2023-05-01 11:19   ` Ævar Arnfjörð Bjarmason
2023-05-05 21:07     ` Jonathan Tan
2023-05-09 22:46       ` Glen Choo
2023-05-11 16:21         ` Jonathan Tan
2023-05-08 21:00     ` Glen Choo
2023-04-21 19:13 ` [PATCH 11/14] config: remove current_config_(line|name|origin_type) Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 12/14] config: remove current_config_scope() Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 13/14] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
2023-04-21 19:13 ` [PATCH 14/14] config: remove config_reader from configset_add_value Glen Choo via GitGitGadget
2023-05-30 18:41 ` [PATCH v2 00/14] [RFC] config: remove global state from config iteration Glen Choo via GitGitGadget
2023-05-30 18:41   ` [PATCH v2 01/14] config: inline git_color_default_config Glen Choo via GitGitGadget
2023-05-30 18:42   ` [PATCH v2 02/14] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
2023-05-30 18:42   ` [PATCH v2 03/14] (RFC-only) config: add kvi arg to config_fn_t Glen Choo via GitGitGadget
2023-06-01  9:50     ` Phillip Wood
2023-06-01 16:22       ` Glen Choo
2023-06-02  9:54         ` Phillip Wood
2023-06-02 16:46           ` Glen Choo
2023-06-05  9:38             ` Phillip Wood
2023-06-09 23:19               ` Glen Choo
2023-05-30 18:42   ` [PATCH v2 04/14] (RFC-only) config: apply cocci to config_fn_t implementations Glen Choo via GitGitGadget
2023-05-30 18:42   ` [PATCH v2 05/14] (RFC-only) config: finish config_fn_t refactor Glen Choo via GitGitGadget
2023-06-01 22:17     ` Jonathan Tan
2023-05-30 18:42   ` [PATCH v2 06/14] config.c: pass kvi in configsets Glen Choo via GitGitGadget
2023-06-01 22:21     ` Jonathan Tan
2023-05-30 18:42   ` [PATCH v2 07/14] config: provide kvi with config files Glen Choo via GitGitGadget
2023-06-01 22:41     ` Jonathan Tan
2023-06-01 23:54     ` Jonathan Tan
2023-05-30 18:42   ` [PATCH v2 08/14] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
2023-05-30 18:42   ` [PATCH v2 09/14] config.c: provide kvi with CLI config Glen Choo via GitGitGadget
2023-06-01 23:35     ` Jonathan Tan
2023-06-02 17:26       ` Glen Choo
2023-05-30 18:42   ` [PATCH v2 10/14] trace2: plumb config kvi Glen Choo via GitGitGadget
2023-06-01 23:38     ` Jonathan Tan
2023-05-30 18:42   ` [PATCH v2 11/14] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
2023-06-01 23:48     ` Jonathan Tan
2023-06-02 17:23       ` Glen Choo
2023-05-30 18:42   ` [PATCH v2 12/14] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
2023-05-30 18:42   ` [PATCH v2 13/14] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
2023-06-02  0:06     ` Jonathan Tan
2023-05-30 18:42   ` [PATCH v2 14/14] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
2023-06-02  0:08     ` Jonathan Tan
2023-06-02 17:20       ` Glen Choo
2023-06-20 19:43   ` [PATCH v3 00/12] config: remove global state from config iteration Glen Choo via GitGitGadget
2023-06-20 19:43     ` [PATCH v3 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
2023-06-20 21:01       ` Junio C Hamano
2023-06-20 19:43     ` [PATCH v3 02/12] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
2023-06-20 21:02       ` Junio C Hamano
2023-06-20 19:43     ` [PATCH v3 03/12] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
2023-06-20 19:43     ` [PATCH v3 04/12] config.c: pass ctx in configsets Glen Choo via GitGitGadget
2023-06-20 21:19       ` Junio C Hamano
2023-06-20 19:43     ` [PATCH v3 05/12] config: pass ctx with config files Glen Choo via GitGitGadget
2023-06-20 19:43     ` [PATCH v3 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
2023-06-20 21:35       ` Junio C Hamano
2023-06-20 23:06         ` Glen Choo
2023-06-23 20:32       ` Jonathan Tan
2023-06-24  1:31         ` Jeff King
2023-06-28 17:28         ` Glen Choo
2023-06-20 19:43     ` [PATCH v3 07/12] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
2023-06-23 20:35       ` Jonathan Tan
2023-06-23 21:41         ` Glen Choo
2023-06-20 19:43     ` [PATCH v3 08/12] trace2: plumb config kvi Glen Choo via GitGitGadget
2023-06-23 20:40       ` Jonathan Tan
2023-06-20 19:43     ` [PATCH v3 09/12] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
2023-06-20 19:43     ` [PATCH v3 10/12] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
2023-06-23 20:57       ` Jonathan Tan
2023-06-23 21:33         ` Junio C Hamano
2023-06-20 19:43     ` [PATCH v3 11/12] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
2023-06-20 19:43     ` [PATCH v3 12/12] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
2023-06-20 21:46       ` Junio C Hamano
2023-06-21 21:46     ` [PATCH v3 00/12] config: remove global state from config iteration Junio C Hamano
2023-06-21 23:06       ` Glen Choo
2023-06-23 21:02         ` Jonathan Tan
2023-06-23 21:33           ` Junio C Hamano
2023-06-23 21:45           ` Glen Choo
2023-06-26 18:11     ` [PATCH v4 " Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 01/12] config: inline git_color_default_config Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 02/12] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 03/12] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 04/12] config.c: pass ctx in configsets Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 05/12] config: pass ctx with config files Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 06/12] builtin/config.c: test misuse of format_config() Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 07/12] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 08/12] trace2: plumb config kvi Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 09/12] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 10/12] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 11/12] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
2023-06-26 18:11       ` [PATCH v4 12/12] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
2023-06-26 20:45       ` [PATCH v4 00/12] config: remove global state from config iteration Junio C Hamano
2023-06-28 19:26       ` [PATCH v5 00/11] " Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 01/11] config: inline git_color_default_config Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 02/11] urlmatch.h: use config_fn_t type Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 03/11] config: add ctx arg to config_fn_t Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 04/11] config.c: pass ctx in configsets Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 05/11] config: pass ctx with config files Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 06/11] config.c: pass ctx with CLI config Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 07/11] trace2: plumb config kvi Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 08/11] config: pass kvi to die_bad_number() Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 09/11] config.c: remove config_reader from configsets Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 10/11] config: add kvi.path, use it to evaluate includes Glen Choo via GitGitGadget
2023-06-28 19:26         ` [PATCH v5 11/11] config: pass source to config_parser_event_fn_t Glen Choo via GitGitGadget
2023-06-28 22:23         ` [PATCH v5 00/11] config: remove global state from config iteration Jonathan Tan
2023-06-28 22:47           ` Junio C Hamano
2023-07-11 18:41         ` Phillip Wood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).