config: pass ctx with config files

Pass config_context to config_callbacks when parsing config files. To provide the .kvi member, refactor out the configset logic that caches "struct config_source" and "enum config_scope" as a "struct key_value_info". Make the "enum config_scope" available to the config file machinery by plumbing an additional arg through git_config_from_file_with_options(). We do not exercise ctx yet because the remaining current_config_*() callers may be used with config_with_options(), which may read config from parameters, but parameters don't pass ctx yet. 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 809d868061c5e55e9b3bd59a4118628318eb46e9
5 files changed +81 -41
bundle-uri.c
+1
@@ -255,6 +255,7 @@ int bundle_uri_parse_config_format(const char *uri,
255 }
256 result = git_config_from_file_with_options(config_to_bundle_list,
257 filename, list,
258 + CONFIG_SCOPE_UNKNOWN,
259 &opts);
260
261 if (!result && list->mode == BUNDLE_MODE_NONE) {
config.c
+70 -35
@@ -259,7 +259,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
259 !cs ? "<unknown>" :
260 cs->name ? cs->name :
261 "the command line");
262 - ret = git_config_from_file(git_config_include, path, inc);
262 + ret = git_config_from_file_with_options(git_config_include, path, inc,
263 + current_config_scope(),
264 + NULL);
265 inc->depth--;
266 }
267 cleanup:
@@ -503,7 +505,7 @@ static int git_config_include(const char *var, const char *value,
505 * Pass along all values, including "include" directives; this makes it
506 * possible to query information on the includes themselves.
507 */
506 - ret = inc->fn(var, value, NULL, inc->data);
508 + ret = inc->fn(var, value, ctx, inc->data);
509 if (ret < 0)
510 return ret;
511
@@ -939,12 +941,15 @@ static char *parse_value(struct config_source *cs)
941 }
942 }
943
942 -static int get_value(struct config_source *cs, config_fn_t fn, void *data,
943 - struct strbuf *name)
944 +static int get_value(struct config_source *cs, struct key_value_info *kvi,
945 + config_fn_t fn, void *data, struct strbuf *name)
946 {
947 int c;
948 char *value;
949 int ret;
950 + struct config_context ctx = {
951 + .kvi = kvi,
952 + };
953
954 /* Get the full name */
955 for (;;) {
@@ -973,7 +978,8 @@ static int get_value(struct config_source *cs, config_fn_t fn, void *data,
978 * accurate line number in error messages.
979 */
980 cs->linenr--;
976 - ret = fn(name->buf, value, NULL, data);
981 + kvi->linenr = cs->linenr;
982 + ret = fn(name->buf, value, &ctx, data);
983 if (ret >= 0)
984 cs->linenr++;
985 return ret;
@@ -1072,8 +1078,19 @@ static int do_event(struct config_source *cs, enum config_event_t type,
1078 return 0;
1079 }
1080
1081 +static void kvi_from_source(struct config_source *cs,
1082 + enum config_scope scope,
1083 + struct key_value_info *out)
1084 +{
1085 + out->filename = strintern(cs->name);
1086 + out->origin_type = cs->origin_type;
1087 + out->linenr = cs->linenr;
1088 + out->scope = scope;
1089 +}
1090 +
1091 static int git_parse_source(struct config_source *cs, config_fn_t fn,
1076 - void *data, const struct config_options *opts)
1092 + struct key_value_info *kvi, void *data,
1093 + const struct config_options *opts)
1094 {
1095 int comment = 0;
1096 size_t baselen = 0;
@@ -1157,7 +1174,7 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn,
1174 */
1175 strbuf_setlen(var, baselen);
1176 strbuf_addch(var, tolower(c));
1160 - if (get_value(cs, fn, data, var) < 0)
1177 + if (get_value(cs, kvi, fn, data, var) < 0)
1178 break;
1179 }
1180
@@ -2010,9 +2027,11 @@ int git_default_config(const char *var, const char *value,
2027 * this function.
2028 */
2029 static int do_config_from(struct config_reader *reader,
2013 - struct config_source *top, config_fn_t fn, void *data,
2030 + struct config_source *top, config_fn_t fn,
2031 + void *data, enum config_scope scope,
2032 const struct config_options *opts)
2033 {
2034 + struct key_value_info kvi = KVI_INIT;
2035 int ret;
2036
2037 /* push config-file parsing state stack */
@@ -2022,8 +2041,9 @@ static int do_config_from(struct config_reader *reader,
2041 strbuf_init(&top->value, 1024);
2042 strbuf_init(&top->var, 1024);
2043 config_reader_push_source(reader, top);
2044 + kvi_from_source(top, scope, &kvi);
2045
2026 - ret = git_parse_source(top, fn, data, opts);
2046 + ret = git_parse_source(top, fn, &kvi, data, opts);
2047
2048 /* pop config-file parsing state stack */
2049 strbuf_release(&top->value);
@@ -2037,7 +2057,8 @@ static int do_config_from_file(struct config_reader *reader,
2057 config_fn_t fn,
2058 const enum config_origin_type origin_type,
2059 const char *name, const char *path, FILE *f,
2040 - void *data, const struct config_options *opts)
2060 + void *data, enum config_scope scope,
2061 + const struct config_options *opts)
2062 {
2063 struct config_source top = CONFIG_SOURCE_INIT;
2064 int ret;
@@ -2052,19 +2073,20 @@ static int do_config_from_file(struct config_reader *reader,
2073 top.do_ftell = config_file_ftell;
2074
2075 flockfile(f);
2055 - ret = do_config_from(reader, &top, fn, data, opts);
2076 + ret = do_config_from(reader, &top, fn, data, scope, opts);
2077 funlockfile(f);
2078 return ret;
2079 }
2080
2060 -static int git_config_from_stdin(config_fn_t fn, void *data)
2081 +static int git_config_from_stdin(config_fn_t fn, void *data,
2082 + enum config_scope scope)
2083 {
2084 return do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_STDIN, "",
2063 - NULL, stdin, data, NULL);
2085 + NULL, stdin, data, scope, NULL);
2086 }
2087
2088 int git_config_from_file_with_options(config_fn_t fn, const char *filename,
2067 - void *data,
2089 + void *data, enum config_scope scope,
2090 const struct config_options *opts)
2091 {
2092 int ret = -1;
@@ -2075,7 +2097,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
2097 f = fopen_or_warn(filename, "r");
2098 if (f) {
2099 ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE,
2078 - filename, filename, f, data, opts);
2100 + filename, filename, f, data, scope,
2101 + opts);
2102 fclose(f);
2103 }
2104 return ret;
@@ -2083,13 +2106,15 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
2106
2107 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
2108 {
2086 - return git_config_from_file_with_options(fn, filename, data, NULL);
2109 + return git_config_from_file_with_options(fn, filename, data,
2110 + CONFIG_SCOPE_UNKNOWN, NULL);
2111 }
2112
2113 int git_config_from_mem(config_fn_t fn,
2114 const enum config_origin_type origin_type,
2115 const char *name, const char *buf, size_t len,
2092 - void *data, const struct config_options *opts)
2116 + void *data, enum config_scope scope,
2117 + const struct config_options *opts)
2118 {
2119 struct config_source top = CONFIG_SOURCE_INIT;
2120
@@ -2104,14 +2129,15 @@ int git_config_from_mem(config_fn_t fn,
2129 top.do_ungetc = config_buf_ungetc;
2130 top.do_ftell = config_buf_ftell;
2131
2107 - return do_config_from(&the_reader, &top, fn, data, opts);
2132 + return do_config_from(&the_reader, &top, fn, data, scope, opts);
2133 }
2134
2135 int git_config_from_blob_oid(config_fn_t fn,
2136 const char *name,
2137 struct repository *repo,
2138 const struct object_id *oid,
2114 - void *data)
2139 + void *data,
2140 + enum config_scope scope)
2141 {
2142 enum object_type type;
2143 char *buf;
@@ -2127,7 +2153,7 @@ int git_config_from_blob_oid(config_fn_t fn,
2153 }
2154
2155 ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size,
2130 - data, NULL);
2156 + data, scope, NULL);
2157 free(buf);
2158
2159 return ret;
@@ -2136,13 +2162,14 @@ int git_config_from_blob_oid(config_fn_t fn,
2162 static int git_config_from_blob_ref(config_fn_t fn,
2163 struct repository *repo,
2164 const char *name,
2139 - void *data)
2165 + void *data,
2166 + enum config_scope scope)
2167 {
2168 struct object_id oid;
2169
2170 if (repo_get_oid(repo, name, &oid) < 0)
2171 return error(_("unable to resolve config blob '%s'"), name);
2145 - return git_config_from_blob_oid(fn, name, repo, &oid, data);
2172 + return git_config_from_blob_oid(fn, name, repo, &oid, data, scope);
2173 }
2174
2175 char *git_system_config(void)
@@ -2228,27 +2255,34 @@ static int do_git_config_sequence(struct config_reader *reader,
2255 if (git_config_system() && system_config &&
2256 !access_or_die(system_config, R_OK,
2257 opts->system_gently ? ACCESS_EACCES_OK : 0))
2231 - ret += git_config_from_file(fn, system_config, data);
2258 + ret += git_config_from_file_with_options(fn, system_config,
2259 + data, CONFIG_SCOPE_SYSTEM,
2260 + NULL);
2261
2262 config_reader_set_scope(reader, CONFIG_SCOPE_GLOBAL);
2263 git_global_config(&user_config, &xdg_config);
2264
2265 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
2237 - ret += git_config_from_file(fn, xdg_config, data);
2266 + ret += git_config_from_file_with_options(fn, xdg_config, data,
2267 + CONFIG_SCOPE_GLOBAL, NULL);
2268
2269 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
2240 - ret += git_config_from_file(fn, user_config, data);
2270 + ret += git_config_from_file_with_options(fn, user_config, data,
2271 + CONFIG_SCOPE_GLOBAL, NULL);
2272
2273 config_reader_set_scope(reader, CONFIG_SCOPE_LOCAL);
2274 if (!opts->ignore_repo && repo_config &&
2275 !access_or_die(repo_config, R_OK, 0))
2245 - ret += git_config_from_file(fn, repo_config, data);
2276 + ret += git_config_from_file_with_options(fn, repo_config, data,
2277 + CONFIG_SCOPE_LOCAL, NULL);
2278
2279 config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
2280 if (!opts->ignore_worktree && worktree_config &&
2281 repo && repo->repository_format_worktree_config &&
2282 !access_or_die(worktree_config, R_OK, 0)) {
2251 - ret += git_config_from_file(fn, worktree_config, data);
2283 + ret += git_config_from_file_with_options(fn, worktree_config, data,
2284 + CONFIG_SCOPE_WORKTREE,
2285 + NULL);
2286 }
2287
2288 config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
@@ -2292,12 +2326,14 @@ int config_with_options(config_fn_t fn, void *data,
2326 * regular lookup sequence.
2327 */
2328 if (config_source && config_source->use_stdin) {
2295 - ret = git_config_from_stdin(fn, data);
2329 + ret = git_config_from_stdin(fn, data, config_source->scope);
2330 } else if (config_source && config_source->file) {
2297 - ret = git_config_from_file(fn, config_source->file, data);
2331 + ret = git_config_from_file_with_options(fn, config_source->file,
2332 + data, config_source->scope,
2333 + NULL);
2334 } else if (config_source && config_source->blob) {
2335 ret = git_config_from_blob_ref(fn, repo, config_source->blob,
2300 - data);
2336 + data, config_source->scope);
2337 } else {
2338 ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
2339 }
@@ -2440,16 +2476,14 @@ static int configset_add_value(struct config_reader *reader,
2476 if (!reader->source)
2477 BUG("configset_add_value has no source");
2478 if (reader->source->name) {
2443 - kv_info->filename = strintern(reader->source->name);
2444 - kv_info->linenr = reader->source->linenr;
2445 - kv_info->origin_type = reader->source->origin_type;
2479 + kvi_from_source(reader->source, current_config_scope(), kv_info);
2480 } else {
2481 /* for values read from `git_config_from_parameters()` */
2482 kv_info->filename = NULL;
2483 kv_info->linenr = -1;
2484 kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
2485 + kv_info->scope = reader->parsing_scope;
2486 }
2452 - kv_info->scope = reader->parsing_scope;
2487 si->util = kv_info;
2488
2489 return 0;
@@ -3490,7 +3524,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
3524 */
3525 if (git_config_from_file_with_options(store_aux,
3526 config_filename,
3493 - &store, &opts)) {
3527 + &store, CONFIG_SCOPE_UNKNOWN,
3528 + &opts)) {
3529 error(_("invalid config file %s"), config_filename);
3530 ret = CONFIG_INVALID_FILE;
3531 goto out_free;
config.h
+5 -3
@@ -169,16 +169,18 @@ int git_default_config(const char *, const char *,
169 int git_config_from_file(config_fn_t fn, const char *, void *);
170
171 int git_config_from_file_with_options(config_fn_t fn, const char *,
172 - void *,
172 + void *, enum config_scope,
173 const struct config_options *);
174 int git_config_from_mem(config_fn_t fn,
175 const enum config_origin_type,
176 const char *name,
177 const char *buf, size_t len,
178 - void *data, const struct config_options *opts);
178 + void *data, enum config_scope scope,
179 + const struct config_options *opts);
180 int git_config_from_blob_oid(config_fn_t fn, const char *name,
181 struct repository *repo,
181 - const struct object_id *oid, void *data);
182 + const struct object_id *oid, void *data,
183 + enum config_scope scope);
184 void git_config_push_parameter(const char *text);
185 void git_config_push_env(const char *spec);
186 int git_config_from_parameters(config_fn_t fn, void *data);
fsck.c
+2 -1
@@ -1238,7 +1238,8 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
1238 data.ret = 0;
1239 config_opts.error_action = CONFIG_ERROR_SILENT;
1240 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
1241 - ".gitmodules", buf, size, &data, &config_opts))
1241 + ".gitmodules", buf, size, &data,
1242 + CONFIG_SCOPE_UNKNOWN, &config_opts))
1243 data.ret |= report(options, oid, OBJ_BLOB,
1244 FSCK_MSG_GITMODULES_PARSE,
1245 "could not parse gitmodules blob");
submodule-config.c
+3 -2
@@ -606,7 +606,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
606 parameter.gitmodules_oid = &oid;
607 parameter.overwrite = 0;
608 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
609 - config, config_size, &parameter, NULL);
609 + config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
610 strbuf_release(&rev);
611 free(config);
612
@@ -714,7 +714,8 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
714
715 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
716 git_config_from_blob_oid(gitmodules_cb, rev.buf,
717 - the_repository, &oid, the_repository);
717 + the_repository, &oid, the_repository,
718 + CONFIG_SCOPE_UNKNOWN);
719 }
720 strbuf_release(&rev);
721