config.c: remove current_parsing_scope

Add ".parsing_scope" to "struct config_reader" and replace "current_parsing_scope" with "the_reader.parsing_scope. Adjust the comment slightly to make it clearer that the scope applies to the config source (not the current value), and should only be set when parsing a config source. As such, ".parsing_scope" (only set when parsing config sources) and ".config_kvi" (only set when iterating a config set) should not be set together, so enforce this with a setter function. Unlike previous commits, "populate_remote_urls()" still needs to store and restore the 'scope' value because it could have touched "current_parsing_scope" ("config_with_options()" can set the scope). Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Mar 28, 2023 at 17:51 UTC 5cdf18e7cd6d6e3ce2fb21fef2b5ec84e570abf8
1 file changed +37 -26
config.c
+37 -26
@@ -70,6 +70,16 @@ struct config_reader {
70 */
71 struct config_source *source;
72 struct key_value_info *config_kvi;
73 + /*
74 + * The "scope" of the current config source being parsed (repo, global,
75 + * etc). Like "source", this is only set when parsing a config source.
76 + * It's not part of "source" because it transcends a single file (i.e.,
77 + * a file included from .git/config is still in "repo" scope).
78 + *
79 + * When iterating through a configset, the equivalent value is
80 + * "config_kvi.scope" (see above).
81 + */
82 + enum config_scope parsing_scope;
83 };
84 /*
85 * Where possible, prefer to accept "struct config_reader" as an arg than to use
@@ -78,16 +88,6 @@ struct config_reader {
88 */
89 static struct config_reader the_reader;
90
81 -/*
82 - * Similar to the variables above, this gives access to the "scope" of the
83 - * current value (repo, global, etc). For cached values, it can be found via
84 - * the current_config_kvi as above. During parsing, the current value can be
85 - * found in this variable. It's not part of "cf_global" because it transcends a
86 - * single file (i.e., a file included from .git/config is still in "repo"
87 - * scope).
88 - */
89 -static enum config_scope current_parsing_scope;
90 -
91 static inline void config_reader_push_source(struct config_reader *reader,
92 struct config_source *top)
93 {
@@ -110,11 +110,19 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
110 static inline void config_reader_set_kvi(struct config_reader *reader,
111 struct key_value_info *kvi)
112 {
113 - if (kvi && reader->source)
113 + if (kvi && (reader->source || reader->parsing_scope))
114 BUG("kvi should not be set while parsing a config source");
115 reader->config_kvi = kvi;
116 }
117
118 +static inline void config_reader_set_scope(struct config_reader *reader,
119 + enum config_scope scope)
120 +{
121 + if (scope && reader->config_kvi)
122 + BUG("scope should only be set when iterating through a config source");
123 + reader->parsing_scope = scope;
124 +}
125 +
126 static int pack_compression_seen;
127 static int zlib_compression_seen;
128
@@ -383,18 +391,18 @@ static void populate_remote_urls(struct config_include_data *inc)
391 {
392 struct config_options opts;
393
386 - enum config_scope store_scope = current_parsing_scope;
394 + enum config_scope store_scope = inc->config_reader->parsing_scope;
395
396 opts = *inc->opts;
397 opts.unconditional_remote_url = 1;
398
391 - current_parsing_scope = 0;
399 + config_reader_set_scope(inc->config_reader, 0);
400
401 inc->remote_urls = xmalloc(sizeof(*inc->remote_urls));
402 string_list_init_dup(inc->remote_urls);
403 config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts);
404
397 - current_parsing_scope = store_scope;
405 + config_reader_set_scope(inc->config_reader, store_scope);
406 }
407
408 static int forbid_remote_url(const char *var, const char *value UNUSED,
@@ -2159,7 +2167,8 @@ int git_config_system(void)
2167 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
2168 }
2169
2162 -static int do_git_config_sequence(const struct config_options *opts,
2170 +static int do_git_config_sequence(struct config_reader *reader,
2171 + const struct config_options *opts,
2172 config_fn_t fn, void *data)
2173 {
2174 int ret = 0;
@@ -2167,7 +2176,7 @@ static int do_git_config_sequence(const struct config_options *opts,
2176 char *xdg_config = NULL;
2177 char *user_config = NULL;
2178 char *repo_config;
2170 - enum config_scope prev_parsing_scope = current_parsing_scope;
2179 + enum config_scope prev_parsing_scope = reader->parsing_scope;
2180
2181 if (opts->commondir)
2182 repo_config = mkpathdup("%s/config", opts->commondir);
@@ -2176,13 +2185,13 @@ static int do_git_config_sequence(const struct config_options *opts,
2185 else
2186 repo_config = NULL;
2187
2179 - current_parsing_scope = CONFIG_SCOPE_SYSTEM;
2188 + config_reader_set_scope(reader, CONFIG_SCOPE_SYSTEM);
2189 if (git_config_system() && system_config &&
2190 !access_or_die(system_config, R_OK,
2191 opts->system_gently ? ACCESS_EACCES_OK : 0))
2192 ret += git_config_from_file(fn, system_config, data);
2193
2185 - current_parsing_scope = CONFIG_SCOPE_GLOBAL;
2194 + config_reader_set_scope(reader, CONFIG_SCOPE_GLOBAL);
2195 git_global_config(&user_config, &xdg_config);
2196
2197 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
@@ -2191,12 +2200,12 @@ static int do_git_config_sequence(const struct config_options *opts,
2200 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
2201 ret += git_config_from_file(fn, user_config, data);
2202
2194 - current_parsing_scope = CONFIG_SCOPE_LOCAL;
2203 + config_reader_set_scope(reader, CONFIG_SCOPE_LOCAL);
2204 if (!opts->ignore_repo && repo_config &&
2205 !access_or_die(repo_config, R_OK, 0))
2206 ret += git_config_from_file(fn, repo_config, data);
2207
2199 - current_parsing_scope = CONFIG_SCOPE_WORKTREE;
2208 + config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
2209 if (!opts->ignore_worktree && repository_format_worktree_config) {
2210 char *path = git_pathdup("config.worktree");
2211 if (!access_or_die(path, R_OK, 0))
@@ -2204,11 +2213,11 @@ static int do_git_config_sequence(const struct config_options *opts,
2213 free(path);
2214 }
2215
2207 - current_parsing_scope = CONFIG_SCOPE_COMMAND;
2216 + config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
2217 if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
2218 die(_("unable to parse command-line config"));
2219
2211 - current_parsing_scope = prev_parsing_scope;
2220 + config_reader_set_scope(reader, prev_parsing_scope);
2221 free(system_config);
2222 free(xdg_config);
2223 free(user_config);
@@ -2221,6 +2230,7 @@ int config_with_options(config_fn_t fn, void *data,
2230 const struct config_options *opts)
2231 {
2232 struct config_include_data inc = CONFIG_INCLUDE_INIT;
2233 + enum config_scope prev_scope = the_reader.parsing_scope;
2234 int ret;
2235
2236 if (opts->respect_includes) {
@@ -2234,7 +2244,7 @@ int config_with_options(config_fn_t fn, void *data,
2244 }
2245
2246 if (config_source)
2237 - current_parsing_scope = config_source->scope;
2247 + config_reader_set_scope(&the_reader, config_source->scope);
2248
2249 /*
2250 * If we have a specific filename, use it. Otherwise, follow the
@@ -2250,13 +2260,14 @@ int config_with_options(config_fn_t fn, void *data,
2260 ret = git_config_from_blob_ref(fn, repo, config_source->blob,
2261 data);
2262 } else {
2253 - ret = do_git_config_sequence(opts, fn, data);
2263 + ret = do_git_config_sequence(&the_reader, opts, fn, data);
2264 }
2265
2266 if (inc.remote_urls) {
2267 string_list_clear(inc.remote_urls, 0);
2268 FREE_AND_NULL(inc.remote_urls);
2269 }
2270 + config_reader_set_scope(&the_reader, prev_scope);
2271 return ret;
2272 }
2273
@@ -2390,7 +2401,7 @@ static int configset_add_value(struct config_reader *reader,
2401 kv_info->linenr = -1;
2402 kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
2403 }
2393 - kv_info->scope = current_parsing_scope;
2404 + kv_info->scope = reader->parsing_scope;
2405 si->util = kv_info;
2406
2407 return 0;
@@ -3891,7 +3902,7 @@ enum config_scope current_config_scope(void)
3902 if (the_reader.config_kvi)
3903 return the_reader.config_kvi->scope;
3904 else
3894 - return current_parsing_scope;
3905 + return the_reader.parsing_scope;
3906 }
3907
3908 int current_config_line(void)