config: drop cf validity check in get_next_char()
[gitweb.git] / config.c
index aefd80b12a079d4a3c91d43c8a2c33ed6fbd0a38..046642b69f1791373593403ad873b67ed764885f 100644 (file)
--- a/config.c
+++ b/config.c
@@ -169,26 +169,23 @@ int git_config_from_parameters(config_fn_t fn, void *data)
 static int get_next_char(void)
 {
        int c;
-       FILE *f;
+       FILE *f = cf->f;
 
-       c = '\n';
-       if (cf && ((f = cf->f) != NULL)) {
+       c = fgetc(f);
+       if (c == '\r') {
+               /* DOS like systems */
                c = fgetc(f);
-               if (c == '\r') {
-                       /* DOS like systems */
-                       c = fgetc(f);
-                       if (c != '\n') {
-                               ungetc(c, f);
-                               c = '\r';
-                       }
-               }
-               if (c == '\n')
-                       cf->linenr++;
-               if (c == EOF) {
-                       cf->eof = 1;
-                       c = '\n';
+               if (c != '\n') {
+                       ungetc(c, f);
+                       c = '\r';
                }
        }
+       if (c == '\n')
+               cf->linenr++;
+       if (c == EOF) {
+               cf->eof = 1;
+               c = '\n';
+       }
        return c;
 }
 
@@ -896,6 +893,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;
@@ -905,22 +928,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 = ⊤
-
-               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);
        }