Skip to content

Commit a798a56

Browse files
chooglengitster
authored andcommitted
config.c: plumb the_reader through callbacks
The remaining references to "cf_global" are in config callback functions. Remove them by plumbing "struct config_reader" via the "*data" arg. In both of the callbacks here, we are only reading from "reader->source". So in the long run, if we had a way to expose readonly information from "reader->source" (probably in the form of "struct key_value_info"), we could undo this patch (i.e. remove "struct config_reader" fom "*data"). Signed-off-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c60285 commit a798a56

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

config.c

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ struct config_reader {
6262
static struct config_reader the_reader;
6363

6464
/*
65+
* FIXME The comments are temporarily out of date since "cf_global" has been
66+
* moved to the_reader, but not current_*.
67+
*
6568
* These variables record the "current" config source, which
6669
* can be accessed by parsing callbacks.
6770
*
@@ -76,11 +79,7 @@ static struct config_reader the_reader;
7679
* at the variables, it's either a bug for it to be called in the first place,
7780
* or it's a function which can be reused for non-config purposes, and should
7881
* fall back to some sane behavior).
79-
*
80-
* FIXME "cf_global" has been replaced by "the_reader.source", remove
81-
* "cf_global" once we plumb "the_reader" through all of the callback functions.
8282
*/
83-
static struct config_source *cf_global;
8483
static struct key_value_info *current_config_kvi;
8584

8685
/*
@@ -98,8 +97,6 @@ static inline void config_reader_push_source(struct config_reader *reader,
9897
{
9998
top->prev = reader->source;
10099
reader->source = top;
101-
/* FIXME remove this when cf_global is removed. */
102-
cf_global = reader->source;
103100
}
104101

105102
static inline struct config_source *config_reader_pop_source(struct config_reader *reader)
@@ -109,8 +106,6 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
109106
BUG("tried to pop config source, but we weren't reading config");
110107
ret = reader->source;
111108
reader->source = reader->source->prev;
112-
/* FIXME remove this when cf_global is removed. */
113-
cf_global = reader->source;
114109
return ret;
115110
}
116111

@@ -175,6 +170,7 @@ struct config_include_data {
175170
void *data;
176171
const struct config_options *opts;
177172
struct git_config_source *config_source;
173+
struct config_reader *config_reader;
178174

179175
/*
180176
* All remote URLs discovered when reading all config files.
@@ -465,6 +461,7 @@ static int include_condition_is_true(struct config_source *cf,
465461
static int git_config_include(const char *var, const char *value, void *data)
466462
{
467463
struct config_include_data *inc = data;
464+
struct config_source *cf = inc->config_reader->source;
468465
const char *cond, *key;
469466
size_t cond_len;
470467
int ret;
@@ -478,16 +475,16 @@ static int git_config_include(const char *var, const char *value, void *data)
478475
return ret;
479476

480477
if (!strcmp(var, "include.path"))
481-
ret = handle_path_include(cf_global, value, inc);
478+
ret = handle_path_include(cf, value, inc);
482479

483480
if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
484-
cond && include_condition_is_true(cf_global, inc, cond, cond_len) &&
481+
cond && include_condition_is_true(cf, inc, cond, cond_len) &&
485482
!strcmp(key, "path")) {
486483
config_fn_t old_fn = inc->fn;
487484

488485
if (inc->opts->unconditional_remote_url)
489486
inc->fn = forbid_remote_url;
490-
ret = handle_path_include(cf_global, value, inc);
487+
ret = handle_path_include(cf, value, inc);
491488
inc->fn = old_fn;
492489
}
493490

@@ -2228,6 +2225,7 @@ int config_with_options(config_fn_t fn, void *data,
22282225
inc.data = data;
22292226
inc.opts = opts;
22302227
inc.config_source = config_source;
2228+
inc.config_reader = &the_reader;
22312229
fn = git_config_include;
22322230
data = &inc;
22332231
}
@@ -2348,7 +2346,9 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
23482346
return found_entry;
23492347
}
23502348

2351-
static int configset_add_value(struct config_set *cs, const char *key, const char *value)
2349+
static int configset_add_value(struct config_reader *reader,
2350+
struct config_set *cs, const char *key,
2351+
const char *value)
23522352
{
23532353
struct config_set_element *e;
23542354
struct string_list_item *si;
@@ -2374,12 +2374,12 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
23742374
l_item->e = e;
23752375
l_item->value_index = e->value_list.nr - 1;
23762376

2377-
if (!cf_global)
2377+
if (!reader->source)
23782378
BUG("configset_add_value has no source");
2379-
if (cf_global->name) {
2380-
kv_info->filename = strintern(cf_global->name);
2381-
kv_info->linenr = cf_global->linenr;
2382-
kv_info->origin_type = cf_global->origin_type;
2379+
if (reader->source->name) {
2380+
kv_info->filename = strintern(reader->source->name);
2381+
kv_info->linenr = reader->source->linenr;
2382+
kv_info->origin_type = reader->source->origin_type;
23832383
} else {
23842384
/* for values read from `git_config_from_parameters()` */
23852385
kv_info->filename = NULL;
@@ -2434,16 +2434,25 @@ void git_configset_clear(struct config_set *cs)
24342434
cs->list.items = NULL;
24352435
}
24362436

2437+
struct configset_add_data {
2438+
struct config_set *config_set;
2439+
struct config_reader *config_reader;
2440+
};
2441+
#define CONFIGSET_ADD_INIT { 0 }
2442+
24372443
static int config_set_callback(const char *key, const char *value, void *cb)
24382444
{
2439-
struct config_set *cs = cb;
2440-
configset_add_value(cs, key, value);
2445+
struct configset_add_data *data = cb;
2446+
configset_add_value(data->config_reader, data->config_set, key, value);
24412447
return 0;
24422448
}
24432449

24442450
int git_configset_add_file(struct config_set *cs, const char *filename)
24452451
{
2446-
return git_config_from_file(config_set_callback, filename, cs);
2452+
struct configset_add_data data = CONFIGSET_ADD_INIT;
2453+
data.config_reader = &the_reader;
2454+
data.config_set = cs;
2455+
return git_config_from_file(config_set_callback, filename, &data);
24472456
}
24482457

24492458
int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
@@ -2558,6 +2567,7 @@ int git_configset_get_pathname(struct config_set *cs, const char *key, const cha
25582567
static void repo_read_config(struct repository *repo)
25592568
{
25602569
struct config_options opts = { 0 };
2570+
struct configset_add_data data = CONFIGSET_ADD_INIT;
25612571

25622572
opts.respect_includes = 1;
25632573
opts.commondir = repo->commondir;
@@ -2569,8 +2579,10 @@ static void repo_read_config(struct repository *repo)
25692579
git_configset_clear(repo->config);
25702580

25712581
git_configset_init(repo->config);
2582+
data.config_set = repo->config;
2583+
data.config_reader = &the_reader;
25722584

2573-
if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
2585+
if (config_with_options(config_set_callback, &data, NULL, &opts) < 0)
25742586
/*
25752587
* config_with_options() normally returns only
25762588
* zero, as most errors are fatal, and
@@ -2696,9 +2708,12 @@ static void read_protected_config(void)
26962708
.ignore_worktree = 1,
26972709
.system_gently = 1,
26982710
};
2711+
struct configset_add_data data = CONFIGSET_ADD_INIT;
2712+
26992713
git_configset_init(&protected_config);
2700-
config_with_options(config_set_callback, &protected_config,
2701-
NULL, &opts);
2714+
data.config_set = &protected_config;
2715+
data.config_reader = &the_reader;
2716+
config_with_options(config_set_callback, &data, NULL, &opts);
27022717
}
27032718

27042719
void git_protected_config(config_fn_t fn, void *data)
@@ -2883,6 +2898,7 @@ void git_die_config(const char *key, const char *err, ...)
28832898
*/
28842899

28852900
struct config_store_data {
2901+
struct config_reader *config_reader;
28862902
size_t baselen;
28872903
char *key;
28882904
int do_not_match;
@@ -2897,6 +2913,7 @@ struct config_store_data {
28972913
unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc;
28982914
unsigned int key_seen:1, section_seen:1, is_keys_section:1;
28992915
};
2916+
#define CONFIG_STORE_INIT { 0 }
29002917

29012918
static void config_store_data_clear(struct config_store_data *store)
29022919
{
@@ -2931,12 +2948,7 @@ static int store_aux_event(enum config_event_t type,
29312948
size_t begin, size_t end, void *data)
29322949
{
29332950
struct config_store_data *store = data;
2934-
/*
2935-
* FIXME Keep using "cf" so that we can avoid rewrapping a
2936-
* really long line below. Remove this when "cf" gets plumbed
2937-
* correctly.
2938-
*/
2939-
struct config_source *cf = cf_global;
2951+
struct config_source *cf = store->config_reader->source;
29402952

29412953
ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
29422954
store->parsed[store->parsed_nr].begin = begin;
@@ -3254,9 +3266,9 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
32543266
char *filename_buf = NULL;
32553267
char *contents = NULL;
32563268
size_t contents_sz;
3257-
struct config_store_data store;
3269+
struct config_store_data store = CONFIG_STORE_INIT;
32583270

3259-
memset(&store, 0, sizeof(store));
3271+
store.config_reader = &the_reader;
32603272

32613273
/* parse-key returns negative; flip the sign to feed exit(3) */
32623274
ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);

0 commit comments

Comments
 (0)