config.c: pass ctx in configsets

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

Glen Choo committed Jun 28, 2023 at 19:26 UTC 6021e1d1580169532512772afbb86996f717f4ae
5 files changed +33 -32
builtin/remote.c
+6 -4
@@ -646,17 +646,19 @@ struct push_default_info
646 };
647
648 static int config_read_push_default(const char *key, const char *value,
649 - const struct config_context *ctx UNUSED, void *cb)
649 + const struct config_context *ctx, void *cb)
650 {
651 + const struct key_value_info *kvi = ctx->kvi;
652 +
653 struct push_default_info* info = cb;
654 if (strcmp(key, "remote.pushdefault") ||
655 !value || strcmp(value, info->old_name))
656 return 0;
657
656 - info->scope = current_config_scope();
658 + info->scope = kvi->scope;
659 strbuf_reset(&info->origin);
658 - strbuf_addstr(&info->origin, current_config_name());
659 - info->linenr = current_config_line();
660 + strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
661 + info->linenr = kvi->linenr;
662
663 return 0;
664 }
config.c
+16 -19
@@ -2317,6 +2317,7 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
2317 struct string_list *values;
2318 struct config_set_element *entry;
2319 struct configset_list *list = &set->list;
2320 + struct config_context ctx = CONFIG_CONTEXT_INIT;
2321
2322 for (i = 0; i < list->nr; i++) {
2323 entry = list->items[i].e;
@@ -2324,12 +2325,11 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
2325 values = &entry->value_list;
2326
2327 config_reader_set_kvi(reader, values->items[value_index].util);
2327 -
2328 - if (fn(entry->key, values->items[value_index].string, NULL, data) < 0)
2328 + ctx.kvi = values->items[value_index].util;
2329 + if (fn(entry->key, values->items[value_index].string, &ctx, data) < 0)
2330 git_die_config_linenr(entry->key,
2330 - reader->config_kvi->filename,
2331 - reader->config_kvi->linenr);
2332 -
2331 + ctx.kvi->filename,
2332 + ctx.kvi->linenr);
2333 config_reader_set_kvi(reader, NULL);
2334 }
2335 }
@@ -3984,13 +3984,8 @@ static int reader_origin_type(struct config_reader *reader,
3984 return 0;
3985 }
3986
3987 -const char *current_config_origin_type(void)
3987 +const char *config_origin_type_name(enum config_origin_type type)
3988 {
3989 - enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
3990 -
3991 - if (reader_origin_type(&the_reader, &type))
3992 - BUG("current_config_origin_type called outside config callback");
3993 -
3989 switch (type) {
3990 case CONFIG_ORIGIN_BLOB:
3991 return "blob";
@@ -4007,6 +4002,16 @@ const char *current_config_origin_type(void)
4002 }
4003 }
4004
4005 +const char *current_config_origin_type(void)
4006 +{
4007 + enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN;
4008 +
4009 + if (reader_origin_type(&the_reader, &type))
4010 + BUG("current_config_origin_type called outside config callback");
4011 +
4012 + return config_origin_type_name(type);
4013 +}
4014 +
4015 const char *config_scope_name(enum config_scope scope)
4016 {
4017 switch (scope) {
@@ -4054,14 +4059,6 @@ enum config_scope current_config_scope(void)
4059 return the_reader.parsing_scope;
4060 }
4061
4057 -int current_config_line(void)
4058 -{
4059 - if (the_reader.config_kvi)
4060 - return the_reader.config_kvi->linenr;
4061 - else
4062 - return the_reader.source->linenr;
4063 -}
4064 -
4062 int lookup_config(const char **mapping, int nr_mapping, const char *var)
4063 {
4064 int i;
config.h
+1 -1
@@ -387,7 +387,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
387 enum config_scope current_config_scope(void);
388 const char *current_config_origin_type(void);
389 const char *current_config_name(void);
390 -int current_config_line(void);
390 +const char *config_origin_type_name(enum config_origin_type type);
391
392 /*
393 * Match and parse a config key of the form:
remote.c
+4 -3
@@ -350,7 +350,7 @@ static void read_branches_file(struct remote_state *remote_state,
350 }
351
352 static int handle_config(const char *key, const char *value,
353 - const struct config_context *ctx UNUSED, void *cb)
353 + const struct config_context *ctx, void *cb)
354 {
355 const char *name;
356 size_t namelen;
@@ -358,6 +358,7 @@ static int handle_config(const char *key, const char *value,
358 struct remote *remote;
359 struct branch *branch;
360 struct remote_state *remote_state = cb;
361 + const struct key_value_info *kvi = ctx->kvi;
362
363 if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
364 /* There is no subsection. */
@@ -415,8 +416,8 @@ static int handle_config(const char *key, const char *value,
416 }
417 remote = make_remote(remote_state, name, namelen);
418 remote->origin = REMOTE_CONFIG;
418 - if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
419 - current_config_scope() == CONFIG_SCOPE_WORKTREE)
419 + if (kvi->scope == CONFIG_SCOPE_LOCAL ||
420 + kvi->scope == CONFIG_SCOPE_WORKTREE)
421 remote->configured_in_repo = 1;
422 if (!strcmp(subkey, "mirror"))
423 remote->mirror = git_config_bool(key, value);
t/helper/test-config.c
+6 -5
@@ -43,9 +43,10 @@
43 */
44
45 static int iterate_cb(const char *var, const char *value,
46 - const struct config_context *ctx UNUSED,
46 + const struct config_context *ctx,
47 void *data UNUSED)
48 {
49 + const struct key_value_info *kvi = ctx->kvi;
50 static int nr;
51
52 if (nr++)
@@ -53,10 +54,10 @@ static int iterate_cb(const char *var, const char *value,
54
55 printf("key=%s\n", var);
56 printf("value=%s\n", value ? value : "(null)");
56 - printf("origin=%s\n", current_config_origin_type());
57 - printf("name=%s\n", current_config_name());
58 - printf("lno=%d\n", current_config_line());
59 - printf("scope=%s\n", config_scope_name(current_config_scope()));
57 + printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
58 + printf("name=%s\n", kvi->filename ? kvi->filename : "");
59 + printf("lno=%d\n", kvi->linenr);
60 + printf("scope=%s\n", config_scope_name(kvi->scope));
61
62 return 0;
63 }