Skip to content

Commit

Permalink
config: factor out config file stack management
Browse files Browse the repository at this point in the history
Because a config callback may start parsing a new file, the
global context regarding the current config file is stored
as a stack. Currently we only need to manage that stack from
git_config_from_file. Let's factor it out to allow new
sources of config data.

Signed-off-by: Heiko Voigt <[email protected]>
Acked-by: Jeff King <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
hvoigt authored and gitster committed Jul 12, 2013
1 parent b387c77 commit ca4b5de
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,32 @@ int git_default_config(const char *var, const char *value, void *dummy)
return 0;
}

/*
* The fields f and name of top need to be initialized before calling
* this function.
*/
static int do_config_from(struct config_file *top, config_fn_t fn, void *data)
{
int ret;

/* push config-file parsing state stack */
top->prev = cf;
top->linenr = 1;
top->eof = 0;
strbuf_init(&top->value, 1024);
strbuf_init(&top->var, 1024);
cf = top;

ret = git_parse_file(fn, data);

/* pop config-file parsing state stack */
strbuf_release(&top->value);
strbuf_release(&top->var);
cf = top->prev;

return ret;
}

int git_config_from_file(config_fn_t fn, const char *filename, void *data)
{
int ret;
Expand All @@ -905,22 +931,10 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
if (f) {
config_file top;

/* push config-file parsing state stack */
top.prev = cf;
top.f = f;
top.name = filename;
top.linenr = 1;
top.eof = 0;
strbuf_init(&top.value, 1024);
strbuf_init(&top.var, 1024);
cf = &top;

ret = git_parse_file(fn, data);

/* pop config-file parsing state stack */
strbuf_release(&top.value);
strbuf_release(&top.var);
cf = top.prev;

ret = do_config_from(&top, fn, data);

fclose(f);
}
Expand Down

0 comments on commit ca4b5de

Please sign in to comment.