Skip to content

Commit 6021e1d

Browse files
chooglengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a4e7e31 commit 6021e1d

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

builtin/remote.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,17 +646,19 @@ struct push_default_info
646646
};
647647

648648
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)
650650
{
651+
const struct key_value_info *kvi = ctx->kvi;
652+
651653
struct push_default_info* info = cb;
652654
if (strcmp(key, "remote.pushdefault") ||
653655
!value || strcmp(value, info->old_name))
654656
return 0;
655657

656-
info->scope = current_config_scope();
658+
info->scope = kvi->scope;
657659
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;
660662

661663
return 0;
662664
}

config.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,19 +2317,19 @@ static void configset_iter(struct config_reader *reader, struct config_set *set,
23172317
struct string_list *values;
23182318
struct config_set_element *entry;
23192319
struct configset_list *list = &set->list;
2320+
struct config_context ctx = CONFIG_CONTEXT_INIT;
23202321

23212322
for (i = 0; i < list->nr; i++) {
23222323
entry = list->items[i].e;
23232324
value_index = list->items[i].value_index;
23242325
values = &entry->value_list;
23252326

23262327
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)
23292330
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);
23332333
config_reader_set_kvi(reader, NULL);
23342334
}
23352335
}
@@ -3984,13 +3984,8 @@ static int reader_origin_type(struct config_reader *reader,
39843984
return 0;
39853985
}
39863986

3987-
const char *current_config_origin_type(void)
3987+
const char *config_origin_type_name(enum config_origin_type type)
39883988
{
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-
39943989
switch (type) {
39953990
case CONFIG_ORIGIN_BLOB:
39963991
return "blob";
@@ -4007,6 +4002,16 @@ const char *current_config_origin_type(void)
40074002
}
40084003
}
40094004

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+
40104015
const char *config_scope_name(enum config_scope scope)
40114016
{
40124017
switch (scope) {
@@ -4054,14 +4059,6 @@ enum config_scope current_config_scope(void)
40544059
return the_reader.parsing_scope;
40554060
}
40564061

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-
40654062
int lookup_config(const char **mapping, int nr_mapping, const char *var)
40664063
{
40674064
int i;

config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
387387
enum config_scope current_config_scope(void);
388388
const char *current_config_origin_type(void);
389389
const char *current_config_name(void);
390-
int current_config_line(void);
390+
const char *config_origin_type_name(enum config_origin_type type);
391391

392392
/*
393393
* Match and parse a config key of the form:

remote.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,15 @@ static void read_branches_file(struct remote_state *remote_state,
350350
}
351351

352352
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)
354354
{
355355
const char *name;
356356
size_t namelen;
357357
const char *subkey;
358358
struct remote *remote;
359359
struct branch *branch;
360360
struct remote_state *remote_state = cb;
361+
const struct key_value_info *kvi = ctx->kvi;
361362

362363
if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
363364
/* There is no subsection. */
@@ -415,8 +416,8 @@ static int handle_config(const char *key, const char *value,
415416
}
416417
remote = make_remote(remote_state, name, namelen);
417418
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)
420421
remote->configured_in_repo = 1;
421422
if (!strcmp(subkey, "mirror"))
422423
remote->mirror = git_config_bool(key, value);

t/helper/test-config.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,21 @@
4343
*/
4444

4545
static int iterate_cb(const char *var, const char *value,
46-
const struct config_context *ctx UNUSED,
46+
const struct config_context *ctx,
4747
void *data UNUSED)
4848
{
49+
const struct key_value_info *kvi = ctx->kvi;
4950
static int nr;
5051

5152
if (nr++)
5253
putchar('\n');
5354

5455
printf("key=%s\n", var);
5556
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));
6061

6162
return 0;
6263
}

0 commit comments

Comments
 (0)