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

Glen Choo committed Jun 28, 2023 at 19:26 UTC 6e8e7981eb86e2c19401825dbdb55d5860d11313
2 files changed +19 -64
config.c
+13 -64
@@ -66,40 +66,6 @@ struct config_source {
66 };
67 #define CONFIG_SOURCE_INIT { 0 }
68
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 -
69 static int pack_compression_seen;
70 static int zlib_compression_seen;
71
@@ -752,14 +718,9 @@ int git_config_from_parameters(config_fn_t fn, void *data)
718 struct strvec to_free = STRVEC_INIT;
719 int ret = 0;
720 char *envw = NULL;
755 - struct config_source source = CONFIG_SOURCE_INIT;
721 struct key_value_info kvi = KVI_INIT;
722
758 - source.origin_type = CONFIG_ORIGIN_CMDLINE;
759 - config_reader_push_source(&the_reader, &source);
760 -
723 kvi_from_param(&kvi);
762 -
724 env = getenv(CONFIG_COUNT_ENVIRONMENT);
725 if (env) {
726 unsigned long count;
@@ -816,7 +777,6 @@ out:
777 strbuf_release(&envvar);
778 strvec_clear(&to_free);
779 free(envw);
819 - config_reader_pop_source(&the_reader);
780 return ret;
781 }
782
@@ -1045,7 +1005,7 @@ static int do_event(struct config_source *cs, enum config_event_t type,
1005
1006 if (data->previous_type != CONFIG_EVENT_EOF &&
1007 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)
1009 return -1;
1010
1011 data->previous_type = type;
@@ -2002,8 +1962,7 @@ int git_default_config(const char *var, const char *value,
1962 * fgetc, ungetc, ftell of top need to be initialized before calling
1963 * this function.
1964 */
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,
1966 void *data, enum config_scope scope,
1967 const struct config_options *opts)
1968 {
@@ -2016,21 +1975,17 @@ static int do_config_from(struct config_reader *reader,
1975 top->total_len = 0;
1976 strbuf_init(&top->value, 1024);
1977 strbuf_init(&top->var, 1024);
2019 - config_reader_push_source(reader, top);
1978 kvi_from_source(top, scope, &kvi);
1979
1980 ret = git_parse_source(top, fn, &kvi, data, opts);
1981
2024 - /* pop config-file parsing state stack */
1982 strbuf_release(&top->value);
1983 strbuf_release(&top->var);
2027 - config_reader_pop_source(reader);
1984
1985 return ret;
1986 }
1987
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,
1989 const enum config_origin_type origin_type,
1990 const char *name, const char *path, FILE *f,
1991 void *data, enum config_scope scope,
@@ -2049,7 +2004,7 @@ static int do_config_from_file(struct config_reader *reader,
2004 top.do_ftell = config_file_ftell;
2005
2006 flockfile(f);
2052 - ret = do_config_from(reader, &top, fn, data, scope, opts);
2007 + ret = do_config_from(&top, fn, data, scope, opts);
2008 funlockfile(f);
2009 return ret;
2010 }
@@ -2057,8 +2012,8 @@ static int do_config_from_file(struct config_reader *reader,
2012 static int git_config_from_stdin(config_fn_t fn, void *data,
2013 enum config_scope scope)
2014 {
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);
2017 }
2018
2019 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,
2027 BUG("filename cannot be NULL");
2028 f = fopen_or_warn(filename, "r");
2029 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);
2032 fclose(f);
2033 }
2034 return ret;
@@ -2105,7 +2059,7 @@ int git_config_from_mem(config_fn_t fn,
2059 top.do_ungetc = config_buf_ungetc;
2060 top.do_ftell = config_buf_ftell;
2061
2108 - return do_config_from(&the_reader, &top, fn, data, scope, opts);
2062 + return do_config_from(&top, fn, data, scope, opts);
2063 }
2064
2065 int git_config_from_blob_oid(config_fn_t fn,
@@ -2198,8 +2152,7 @@ int git_config_system(void)
2152 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
2153 }
2154
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,
2156 const struct repository *repo,
2157 config_fn_t fn, void *data)
2158 {
@@ -2299,7 +2252,7 @@ int config_with_options(config_fn_t fn, void *data,
2252 ret = git_config_from_blob_ref(fn, repo, config_source->blob,
2253 data, config_source->scope);
2254 } else {
2302 - ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
2255 + ret = do_git_config_sequence(opts, repo, fn, data);
2256 }
2257
2258 if (inc.remote_urls) {
@@ -3009,7 +2962,6 @@ void git_die_config(const char *key, const char *err, ...)
2962 */
2963
2964 struct config_store_data {
3012 - struct config_reader *config_reader;
2965 size_t baselen;
2966 char *key;
2967 int do_not_match;
@@ -3055,11 +3007,10 @@ static int matches(const char *key, const char *value,
3007 (value && !regexec(store->value_pattern, value, 0, NULL, 0));
3008 }
3009
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)
3012 {
3013 struct config_store_data *store = data;
3062 - struct config_source *cs = store->config_reader->source;
3014
3015 ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
3016 store->parsed[store->parsed_nr].begin = begin;
@@ -3380,8 +3331,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
3331 size_t contents_sz;
3332 struct config_store_data store = CONFIG_STORE_INIT;
3333
3383 - store.config_reader = &the_reader;
3384 -
3334 /* parse-key returns negative; flip the sign to feed exit(3) */
3335 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
3336 if (ret)
config.h
+6
@@ -72,6 +72,7 @@ enum config_event_t {
72 CONFIG_EVENT_ERROR
73 };
74
75 +struct config_source;
76 /*
77 * The parser event function (if not NULL) is called with the event type and
78 * the begin/end offsets of the parsed elements.
@@ -81,6 +82,7 @@ enum config_event_t {
82 */
83 typedef int (*config_parser_event_fn_t)(enum config_event_t type,
84 size_t begin_offset, size_t end_offset,
85 + struct config_source *cs,
86 void *event_fn_data);
87
88 struct config_options {
@@ -100,6 +102,10 @@ struct config_options {
102
103 const char *commondir;
104 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 + */
109 config_parser_event_fn_t event_fn;
110 void *event_fn_data;
111 enum config_error_action {