Skip to content

Commit 6e8e798

Browse files
chooglengitster
authored andcommitted
config: pass source to config_parser_event_fn_t
..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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 908857a commit 6e8e798

File tree

2 files changed

+19
-64
lines changed

2 files changed

+19
-64
lines changed

config.c

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -66,40 +66,6 @@ struct config_source {
6666
};
6767
#define CONFIG_SOURCE_INIT { 0 }
6868

69-
struct config_reader {
70-
/*
71-
* These members record the "current" config source, which can be
72-
* accessed by parsing callbacks.
73-
*
74-
* The "source" variable will be non-NULL only when we are actually
75-
* parsing a real config source (file, blob, cmdline, etc).
76-
*/
77-
struct config_source *source;
78-
};
79-
/*
80-
* Where possible, prefer to accept "struct config_reader" as an arg than to use
81-
* "the_reader". "the_reader" should only be used if that is infeasible, e.g. in
82-
* a public function.
83-
*/
84-
static struct config_reader the_reader;
85-
86-
static inline void config_reader_push_source(struct config_reader *reader,
87-
struct config_source *top)
88-
{
89-
top->prev = reader->source;
90-
reader->source = top;
91-
}
92-
93-
static inline struct config_source *config_reader_pop_source(struct config_reader *reader)
94-
{
95-
struct config_source *ret;
96-
if (!reader->source)
97-
BUG("tried to pop config source, but we weren't reading config");
98-
ret = reader->source;
99-
reader->source = reader->source->prev;
100-
return ret;
101-
}
102-
10369
static int pack_compression_seen;
10470
static int zlib_compression_seen;
10571

@@ -752,14 +718,9 @@ int git_config_from_parameters(config_fn_t fn, void *data)
752718
struct strvec to_free = STRVEC_INIT;
753719
int ret = 0;
754720
char *envw = NULL;
755-
struct config_source source = CONFIG_SOURCE_INIT;
756721
struct key_value_info kvi = KVI_INIT;
757722

758-
source.origin_type = CONFIG_ORIGIN_CMDLINE;
759-
config_reader_push_source(&the_reader, &source);
760-
761723
kvi_from_param(&kvi);
762-
763724
env = getenv(CONFIG_COUNT_ENVIRONMENT);
764725
if (env) {
765726
unsigned long count;
@@ -816,7 +777,6 @@ int git_config_from_parameters(config_fn_t fn, void *data)
816777
strbuf_release(&envvar);
817778
strvec_clear(&to_free);
818779
free(envw);
819-
config_reader_pop_source(&the_reader);
820780
return ret;
821781
}
822782

@@ -1045,7 +1005,7 @@ static int do_event(struct config_source *cs, enum config_event_t type,
10451005

10461006
if (data->previous_type != CONFIG_EVENT_EOF &&
10471007
data->opts->event_fn(data->previous_type, data->previous_offset,
1048-
offset, data->opts->event_fn_data) < 0)
1008+
offset, cs, data->opts->event_fn_data) < 0)
10491009
return -1;
10501010

10511011
data->previous_type = type;
@@ -2002,8 +1962,7 @@ int git_default_config(const char *var, const char *value,
20021962
* fgetc, ungetc, ftell of top need to be initialized before calling
20031963
* this function.
20041964
*/
2005-
static int do_config_from(struct config_reader *reader,
2006-
struct config_source *top, config_fn_t fn,
1965+
static int do_config_from(struct config_source *top, config_fn_t fn,
20071966
void *data, enum config_scope scope,
20081967
const struct config_options *opts)
20091968
{
@@ -2016,21 +1975,17 @@ static int do_config_from(struct config_reader *reader,
20161975
top->total_len = 0;
20171976
strbuf_init(&top->value, 1024);
20181977
strbuf_init(&top->var, 1024);
2019-
config_reader_push_source(reader, top);
20201978
kvi_from_source(top, scope, &kvi);
20211979

20221980
ret = git_parse_source(top, fn, &kvi, data, opts);
20231981

2024-
/* pop config-file parsing state stack */
20251982
strbuf_release(&top->value);
20261983
strbuf_release(&top->var);
2027-
config_reader_pop_source(reader);
20281984

20291985
return ret;
20301986
}
20311987

2032-
static int do_config_from_file(struct config_reader *reader,
2033-
config_fn_t fn,
1988+
static int do_config_from_file(config_fn_t fn,
20341989
const enum config_origin_type origin_type,
20351990
const char *name, const char *path, FILE *f,
20361991
void *data, enum config_scope scope,
@@ -2049,16 +2004,16 @@ static int do_config_from_file(struct config_reader *reader,
20492004
top.do_ftell = config_file_ftell;
20502005

20512006
flockfile(f);
2052-
ret = do_config_from(reader, &top, fn, data, scope, opts);
2007+
ret = do_config_from(&top, fn, data, scope, opts);
20532008
funlockfile(f);
20542009
return ret;
20552010
}
20562011

20572012
static int git_config_from_stdin(config_fn_t fn, void *data,
20582013
enum config_scope scope)
20592014
{
2060-
return do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_STDIN, "",
2061-
NULL, stdin, data, scope, NULL);
2015+
return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin,
2016+
data, scope, NULL);
20622017
}
20632018

20642019
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,
20722027
BUG("filename cannot be NULL");
20732028
f = fopen_or_warn(filename, "r");
20742029
if (f) {
2075-
ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
2076-
filename, filename, f, data, scope,
2077-
opts);
2030+
ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
2031+
filename, f, data, scope, opts);
20782032
fclose(f);
20792033
}
20802034
return ret;
@@ -2105,7 +2059,7 @@ int git_config_from_mem(config_fn_t fn,
21052059
top.do_ungetc = config_buf_ungetc;
21062060
top.do_ftell = config_buf_ftell;
21072061

2108-
return do_config_from(&the_reader, &top, fn, data, scope, opts);
2062+
return do_config_from(&top, fn, data, scope, opts);
21092063
}
21102064

21112065
int git_config_from_blob_oid(config_fn_t fn,
@@ -2198,8 +2152,7 @@ int git_config_system(void)
21982152
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
21992153
}
22002154

2201-
static int do_git_config_sequence(struct config_reader *reader,
2202-
const struct config_options *opts,
2155+
static int do_git_config_sequence(const struct config_options *opts,
22032156
const struct repository *repo,
22042157
config_fn_t fn, void *data)
22052158
{
@@ -2299,7 +2252,7 @@ int config_with_options(config_fn_t fn, void *data,
22992252
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
23002253
data, config_source->scope);
23012254
} else {
2302-
ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
2255+
ret = do_git_config_sequence(opts, repo, fn, data);
23032256
}
23042257

23052258
if (inc.remote_urls) {
@@ -3009,7 +2962,6 @@ void git_die_config(const char *key, const char *err, ...)
30092962
*/
30102963

30112964
struct config_store_data {
3012-
struct config_reader *config_reader;
30132965
size_t baselen;
30142966
char *key;
30152967
int do_not_match;
@@ -3055,11 +3007,10 @@ static int matches(const char *key, const char *value,
30553007
(value && !regexec(store->value_pattern, value, 0, NULL, 0));
30563008
}
30573009

3058-
static int store_aux_event(enum config_event_t type,
3059-
size_t begin, size_t end, void *data)
3010+
static int store_aux_event(enum config_event_t type, size_t begin, size_t end,
3011+
struct config_source *cs, void *data)
30603012
{
30613013
struct config_store_data *store = data;
3062-
struct config_source *cs = store->config_reader->source;
30633014

30643015
ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
30653016
store->parsed[store->parsed_nr].begin = begin;
@@ -3380,8 +3331,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
33803331
size_t contents_sz;
33813332
struct config_store_data store = CONFIG_STORE_INIT;
33823333

3383-
store.config_reader = &the_reader;
3384-
33853334
/* parse-key returns negative; flip the sign to feed exit(3) */
33863335
ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
33873336
if (ret)

config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ enum config_event_t {
7272
CONFIG_EVENT_ERROR
7373
};
7474

75+
struct config_source;
7576
/*
7677
* The parser event function (if not NULL) is called with the event type and
7778
* the begin/end offsets of the parsed elements.
@@ -81,6 +82,7 @@ enum config_event_t {
8182
*/
8283
typedef int (*config_parser_event_fn_t)(enum config_event_t type,
8384
size_t begin_offset, size_t end_offset,
85+
struct config_source *cs,
8486
void *event_fn_data);
8587

8688
struct config_options {
@@ -100,6 +102,10 @@ struct config_options {
100102

101103
const char *commondir;
102104
const char *git_dir;
105+
/*
106+
* event_fn and event_fn_data are for internal use only. Handles events
107+
* emitted by the config parser.
108+
*/
103109
config_parser_event_fn_t event_fn;
104110
void *event_fn_data;
105111
enum config_error_action {

0 commit comments

Comments
 (0)