Skip to content

Commit 809d868

Browse files
chooglengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6021e1d commit 809d868

File tree

5 files changed

+81
-41
lines changed

5 files changed

+81
-41
lines changed

bundle-uri.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ int bundle_uri_parse_config_format(const char *uri,
255255
}
256256
result = git_config_from_file_with_options(config_to_bundle_list,
257257
filename, list,
258+
CONFIG_SCOPE_UNKNOWN,
258259
&opts);
259260

260261
if (!result && list->mode == BUNDLE_MODE_NONE) {

config.c

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ static int handle_path_include(struct config_source *cs, const char *path,
259259
!cs ? "<unknown>" :
260260
cs->name ? cs->name :
261261
"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);
263265
inc->depth--;
264266
}
265267
cleanup:
@@ -503,7 +505,7 @@ static int git_config_include(const char *var, const char *value,
503505
* Pass along all values, including "include" directives; this makes it
504506
* possible to query information on the includes themselves.
505507
*/
506-
ret = inc->fn(var, value, NULL, inc->data);
508+
ret = inc->fn(var, value, ctx, inc->data);
507509
if (ret < 0)
508510
return ret;
509511

@@ -939,12 +941,15 @@ static char *parse_value(struct config_source *cs)
939941
}
940942
}
941943

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)
944946
{
945947
int c;
946948
char *value;
947949
int ret;
950+
struct config_context ctx = {
951+
.kvi = kvi,
952+
};
948953

949954
/* Get the full name */
950955
for (;;) {
@@ -973,7 +978,8 @@ static int get_value(struct config_source *cs, config_fn_t fn, void *data,
973978
* accurate line number in error messages.
974979
*/
975980
cs->linenr--;
976-
ret = fn(name->buf, value, NULL, data);
981+
kvi->linenr = cs->linenr;
982+
ret = fn(name->buf, value, &ctx, data);
977983
if (ret >= 0)
978984
cs->linenr++;
979985
return ret;
@@ -1072,8 +1078,19 @@ static int do_event(struct config_source *cs, enum config_event_t type,
10721078
return 0;
10731079
}
10741080

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+
10751091
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)
10771094
{
10781095
int comment = 0;
10791096
size_t baselen = 0;
@@ -1157,7 +1174,7 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn,
11571174
*/
11581175
strbuf_setlen(var, baselen);
11591176
strbuf_addch(var, tolower(c));
1160-
if (get_value(cs, fn, data, var) < 0)
1177+
if (get_value(cs, kvi, fn, data, var) < 0)
11611178
break;
11621179
}
11631180

@@ -2010,9 +2027,11 @@ int git_default_config(const char *var, const char *value,
20102027
* this function.
20112028
*/
20122029
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,
20142032
const struct config_options *opts)
20152033
{
2034+
struct key_value_info kvi = KVI_INIT;
20162035
int ret;
20172036

20182037
/* push config-file parsing state stack */
@@ -2022,8 +2041,9 @@ static int do_config_from(struct config_reader *reader,
20222041
strbuf_init(&top->value, 1024);
20232042
strbuf_init(&top->var, 1024);
20242043
config_reader_push_source(reader, top);
2044+
kvi_from_source(top, scope, &kvi);
20252045

2026-
ret = git_parse_source(top, fn, data, opts);
2046+
ret = git_parse_source(top, fn, &kvi, data, opts);
20272047

20282048
/* pop config-file parsing state stack */
20292049
strbuf_release(&top->value);
@@ -2037,7 +2057,8 @@ static int do_config_from_file(struct config_reader *reader,
20372057
config_fn_t fn,
20382058
const enum config_origin_type origin_type,
20392059
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)
20412062
{
20422063
struct config_source top = CONFIG_SOURCE_INIT;
20432064
int ret;
@@ -2052,19 +2073,20 @@ static int do_config_from_file(struct config_reader *reader,
20522073
top.do_ftell = config_file_ftell;
20532074

20542075
flockfile(f);
2055-
ret = do_config_from(reader, &top, fn, data, opts);
2076+
ret = do_config_from(reader, &top, fn, data, scope, opts);
20562077
funlockfile(f);
20572078
return ret;
20582079
}
20592080

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)
20612083
{
20622084
return do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_STDIN, "",
2063-
NULL, stdin, data, NULL);
2085+
NULL, stdin, data, scope, NULL);
20642086
}
20652087

20662088
int git_config_from_file_with_options(config_fn_t fn, const char *filename,
2067-
void *data,
2089+
void *data, enum config_scope scope,
20682090
const struct config_options *opts)
20692091
{
20702092
int ret = -1;
@@ -2075,21 +2097,24 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
20752097
f = fopen_or_warn(filename, "r");
20762098
if (f) {
20772099
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);
20792102
fclose(f);
20802103
}
20812104
return ret;
20822105
}
20832106

20842107
int git_config_from_file(config_fn_t fn, const char *filename, void *data)
20852108
{
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);
20872111
}
20882112

20892113
int git_config_from_mem(config_fn_t fn,
20902114
const enum config_origin_type origin_type,
20912115
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)
20932118
{
20942119
struct config_source top = CONFIG_SOURCE_INIT;
20952120

@@ -2104,14 +2129,15 @@ int git_config_from_mem(config_fn_t fn,
21042129
top.do_ungetc = config_buf_ungetc;
21052130
top.do_ftell = config_buf_ftell;
21062131

2107-
return do_config_from(&the_reader, &top, fn, data, opts);
2132+
return do_config_from(&the_reader, &top, fn, data, scope, opts);
21082133
}
21092134

21102135
int git_config_from_blob_oid(config_fn_t fn,
21112136
const char *name,
21122137
struct repository *repo,
21132138
const struct object_id *oid,
2114-
void *data)
2139+
void *data,
2140+
enum config_scope scope)
21152141
{
21162142
enum object_type type;
21172143
char *buf;
@@ -2127,7 +2153,7 @@ int git_config_from_blob_oid(config_fn_t fn,
21272153
}
21282154

21292155
ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size,
2130-
data, NULL);
2156+
data, scope, NULL);
21312157
free(buf);
21322158

21332159
return ret;
@@ -2136,13 +2162,14 @@ int git_config_from_blob_oid(config_fn_t fn,
21362162
static int git_config_from_blob_ref(config_fn_t fn,
21372163
struct repository *repo,
21382164
const char *name,
2139-
void *data)
2165+
void *data,
2166+
enum config_scope scope)
21402167
{
21412168
struct object_id oid;
21422169

21432170
if (repo_get_oid(repo, name, &oid) < 0)
21442171
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);
21462173
}
21472174

21482175
char *git_system_config(void)
@@ -2228,27 +2255,34 @@ static int do_git_config_sequence(struct config_reader *reader,
22282255
if (git_config_system() && system_config &&
22292256
!access_or_die(system_config, R_OK,
22302257
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);
22322261

22332262
config_reader_set_scope(reader, CONFIG_SCOPE_GLOBAL);
22342263
git_global_config(&user_config, &xdg_config);
22352264

22362265
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);
22382268

22392269
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);
22412272

22422273
config_reader_set_scope(reader, CONFIG_SCOPE_LOCAL);
22432274
if (!opts->ignore_repo && repo_config &&
22442275
!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);
22462278

22472279
config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
22482280
if (!opts->ignore_worktree && worktree_config &&
22492281
repo && repo->repository_format_worktree_config &&
22502282
!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);
22522286
}
22532287

22542288
config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
@@ -2292,12 +2326,14 @@ int config_with_options(config_fn_t fn, void *data,
22922326
* regular lookup sequence.
22932327
*/
22942328
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);
22962330
} 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);
22982334
} else if (config_source && config_source->blob) {
22992335
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
2300-
data);
2336+
data, config_source->scope);
23012337
} else {
23022338
ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
23032339
}
@@ -2440,16 +2476,14 @@ static int configset_add_value(struct config_reader *reader,
24402476
if (!reader->source)
24412477
BUG("configset_add_value has no source");
24422478
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);
24462480
} else {
24472481
/* for values read from `git_config_from_parameters()` */
24482482
kv_info->filename = NULL;
24492483
kv_info->linenr = -1;
24502484
kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
2485+
kv_info->scope = reader->parsing_scope;
24512486
}
2452-
kv_info->scope = reader->parsing_scope;
24532487
si->util = kv_info;
24542488

24552489
return 0;
@@ -3490,7 +3524,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
34903524
*/
34913525
if (git_config_from_file_with_options(store_aux,
34923526
config_filename,
3493-
&store, &opts)) {
3527+
&store, CONFIG_SCOPE_UNKNOWN,
3528+
&opts)) {
34943529
error(_("invalid config file %s"), config_filename);
34953530
ret = CONFIG_INVALID_FILE;
34963531
goto out_free;

config.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,18 @@ int git_default_config(const char *, const char *,
169169
int git_config_from_file(config_fn_t fn, const char *, void *);
170170

171171
int git_config_from_file_with_options(config_fn_t fn, const char *,
172-
void *,
172+
void *, enum config_scope,
173173
const struct config_options *);
174174
int git_config_from_mem(config_fn_t fn,
175175
const enum config_origin_type,
176176
const char *name,
177177
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);
179180
int git_config_from_blob_oid(config_fn_t fn, const char *name,
180181
struct repository *repo,
181-
const struct object_id *oid, void *data);
182+
const struct object_id *oid, void *data,
183+
enum config_scope scope);
182184
void git_config_push_parameter(const char *text);
183185
void git_config_push_env(const char *spec);
184186
int git_config_from_parameters(config_fn_t fn, void *data);

fsck.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,8 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
12381238
data.ret = 0;
12391239
config_opts.error_action = CONFIG_ERROR_SILENT;
12401240
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))
12421243
data.ret |= report(options, oid, OBJ_BLOB,
12431244
FSCK_MSG_GITMODULES_PARSE,
12441245
"could not parse gitmodules blob");

submodule-config.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
606606
parameter.gitmodules_oid = &oid;
607607
parameter.overwrite = 0;
608608
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);
610610
strbuf_release(&rev);
611611
free(config);
612612

@@ -714,7 +714,8 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
714714

715715
if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
716716
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);
718719
}
719720
strbuf_release(&rev);
720721

0 commit comments

Comments
 (0)