config: factor out config file stack management
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 <hvoigt@hvoigt.net> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Heiko Voigt committed
May 11, 2013 at 15:18 UTC
ca4b5de28bad7d50b882794124ca4e57044a1cba
1 file changed
+28
-14
config.c
+28
-14
@@ -896,6 +896,32 @@ int git_default_config(const char *var, const char *value, void *dummy)
896
return 0;
897
}
898
899
+/*
900
+ * The fields f and name of top need to be initialized before calling
901
+ * this function.
902
+ */
903
+static int do_config_from(struct config_file *top, config_fn_t fn, void *data)
904
+{
905
+ int ret;
906
+
907
+ /* push config-file parsing state stack */
908
+ top->prev = cf;
909
+ top->linenr = 1;
910
+ top->eof = 0;
911
+ strbuf_init(&top->value, 1024);
912
+ strbuf_init(&top->var, 1024);
913
+ cf = top;
914
+
915
+ ret = git_parse_file(fn, data);
916
+
917
+ /* pop config-file parsing state stack */
918
+ strbuf_release(&top->value);
919
+ strbuf_release(&top->var);
920
+ cf = top->prev;
921
+
922
+ return ret;
923
+}
924
+
925
int git_config_from_file(config_fn_t fn, const char *filename, void *data)
926
{
927
int ret;
@@ -905,22 +931,10 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
931
if (f) {
932
config_file top;
933
908
- /* push config-file parsing state stack */
909
- top.prev = cf;
934
top.f = f;
935
top.name = filename;
912
- top.linenr = 1;
913
- top.eof = 0;
914
- strbuf_init(&top.value, 1024);
915
- strbuf_init(&top.var, 1024);
916
- cf = ⊤
917
-
918
- ret = git_parse_file(fn, data);
919
-
920
- /* pop config-file parsing state stack */
921
- strbuf_release(&top.value);
922
- strbuf_release(&top.var);
923
- cf = top.prev;
936
+
937
+ ret = do_config_from(&top, fn, data);
938
939
fclose(f);
940
}