Skip to content

Commit 5cdf18e

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

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

config.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ struct config_reader {
7070
*/
7171
struct config_source *source;
7272
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;
7383
};
7484
/*
7585
* Where possible, prefer to accept "struct config_reader" as an arg than to use
@@ -78,16 +88,6 @@ struct config_reader {
7888
*/
7989
static struct config_reader the_reader;
8090

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-
9191
static inline void config_reader_push_source(struct config_reader *reader,
9292
struct config_source *top)
9393
{
@@ -110,11 +110,19 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
110110
static inline void config_reader_set_kvi(struct config_reader *reader,
111111
struct key_value_info *kvi)
112112
{
113-
if (kvi && reader->source)
113+
if (kvi && (reader->source || reader->parsing_scope))
114114
BUG("kvi should not be set while parsing a config source");
115115
reader->config_kvi = kvi;
116116
}
117117

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+
118126
static int pack_compression_seen;
119127
static int zlib_compression_seen;
120128

@@ -383,18 +391,18 @@ static void populate_remote_urls(struct config_include_data *inc)
383391
{
384392
struct config_options opts;
385393

386-
enum config_scope store_scope = current_parsing_scope;
394+
enum config_scope store_scope = inc->config_reader->parsing_scope;
387395

388396
opts = *inc->opts;
389397
opts.unconditional_remote_url = 1;
390398

391-
current_parsing_scope = 0;
399+
config_reader_set_scope(inc->config_reader, 0);
392400

393401
inc->remote_urls = xmalloc(sizeof(*inc->remote_urls));
394402
string_list_init_dup(inc->remote_urls);
395403
config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts);
396404

397-
current_parsing_scope = store_scope;
405+
config_reader_set_scope(inc->config_reader, store_scope);
398406
}
399407

400408
static int forbid_remote_url(const char *var, const char *value UNUSED,
@@ -2159,15 +2167,16 @@ int git_config_system(void)
21592167
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
21602168
}
21612169

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,
21632172
config_fn_t fn, void *data)
21642173
{
21652174
int ret = 0;
21662175
char *system_config = git_system_config();
21672176
char *xdg_config = NULL;
21682177
char *user_config = NULL;
21692178
char *repo_config;
2170-
enum config_scope prev_parsing_scope = current_parsing_scope;
2179+
enum config_scope prev_parsing_scope = reader->parsing_scope;
21712180

21722181
if (opts->commondir)
21732182
repo_config = mkpathdup("%s/config", opts->commondir);
@@ -2176,13 +2185,13 @@ static int do_git_config_sequence(const struct config_options *opts,
21762185
else
21772186
repo_config = NULL;
21782187

2179-
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
2188+
config_reader_set_scope(reader, CONFIG_SCOPE_SYSTEM);
21802189
if (git_config_system() && system_config &&
21812190
!access_or_die(system_config, R_OK,
21822191
opts->system_gently ? ACCESS_EACCES_OK : 0))
21832192
ret += git_config_from_file(fn, system_config, data);
21842193

2185-
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
2194+
config_reader_set_scope(reader, CONFIG_SCOPE_GLOBAL);
21862195
git_global_config(&user_config, &xdg_config);
21872196

21882197
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
@@ -2191,24 +2200,24 @@ static int do_git_config_sequence(const struct config_options *opts,
21912200
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
21922201
ret += git_config_from_file(fn, user_config, data);
21932202

2194-
current_parsing_scope = CONFIG_SCOPE_LOCAL;
2203+
config_reader_set_scope(reader, CONFIG_SCOPE_LOCAL);
21952204
if (!opts->ignore_repo && repo_config &&
21962205
!access_or_die(repo_config, R_OK, 0))
21972206
ret += git_config_from_file(fn, repo_config, data);
21982207

2199-
current_parsing_scope = CONFIG_SCOPE_WORKTREE;
2208+
config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
22002209
if (!opts->ignore_worktree && repository_format_worktree_config) {
22012210
char *path = git_pathdup("config.worktree");
22022211
if (!access_or_die(path, R_OK, 0))
22032212
ret += git_config_from_file(fn, path, data);
22042213
free(path);
22052214
}
22062215

2207-
current_parsing_scope = CONFIG_SCOPE_COMMAND;
2216+
config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
22082217
if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
22092218
die(_("unable to parse command-line config"));
22102219

2211-
current_parsing_scope = prev_parsing_scope;
2220+
config_reader_set_scope(reader, prev_parsing_scope);
22122221
free(system_config);
22132222
free(xdg_config);
22142223
free(user_config);
@@ -2221,6 +2230,7 @@ int config_with_options(config_fn_t fn, void *data,
22212230
const struct config_options *opts)
22222231
{
22232232
struct config_include_data inc = CONFIG_INCLUDE_INIT;
2233+
enum config_scope prev_scope = the_reader.parsing_scope;
22242234
int ret;
22252235

22262236
if (opts->respect_includes) {
@@ -2234,7 +2244,7 @@ int config_with_options(config_fn_t fn, void *data,
22342244
}
22352245

22362246
if (config_source)
2237-
current_parsing_scope = config_source->scope;
2247+
config_reader_set_scope(&the_reader, config_source->scope);
22382248

22392249
/*
22402250
* 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,
22502260
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
22512261
data);
22522262
} else {
2253-
ret = do_git_config_sequence(opts, fn, data);
2263+
ret = do_git_config_sequence(&the_reader, opts, fn, data);
22542264
}
22552265

22562266
if (inc.remote_urls) {
22572267
string_list_clear(inc.remote_urls, 0);
22582268
FREE_AND_NULL(inc.remote_urls);
22592269
}
2270+
config_reader_set_scope(&the_reader, prev_scope);
22602271
return ret;
22612272
}
22622273

@@ -2390,7 +2401,7 @@ static int configset_add_value(struct config_reader *reader,
23902401
kv_info->linenr = -1;
23912402
kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
23922403
}
2393-
kv_info->scope = current_parsing_scope;
2404+
kv_info->scope = reader->parsing_scope;
23942405
si->util = kv_info;
23952406

23962407
return 0;
@@ -3891,7 +3902,7 @@ enum config_scope current_config_scope(void)
38913902
if (the_reader.config_kvi)
38923903
return the_reader.config_kvi->scope;
38933904
else
3894-
return current_parsing_scope;
3905+
return the_reader.parsing_scope;
38953906
}
38963907

38973908
int current_config_line(void)

0 commit comments

Comments
 (0)