config: turn die_on_error into caller-facing enum
authorJeff King <peff@peff.net>
Thu, 28 Jun 2018 22:05:00 +0000 (18:05 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Jul 2018 16:33:03 +0000 (09:33 -0700)
The config code has a die_on_error flag, which lets us emit
an error() instead of dying when we see a bogus config file.
But there's no way for a caller of the config code to set
this: it's auto-set based on whether we're reading a file or
a blob.

Instead, let's add it to the config_options struct. When
it's not set (or we have no options) we'll continue to fall
back to the existing file/blob behavior.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c
config.h
index f4a208a16607c2f3eb10c02d067808d46399b7a3..a1d445ecbf9de640dca7af9215d9028941a75b2a 100644 (file)
--- a/config.c
+++ b/config.c
@@ -31,7 +31,7 @@ struct config_source {
        enum config_origin_type origin_type;
        const char *name;
        const char *path;
        enum config_origin_type origin_type;
        const char *name;
        const char *path;
-       int die_on_error;
+       enum config_error_action default_error_action;
        int linenr;
        int eof;
        struct strbuf value;
        int linenr;
        int eof;
        struct strbuf value;
@@ -809,10 +809,18 @@ static int git_parse_source(config_fn_t fn, void *data,
                                      cf->linenr, cf->name);
        }
 
                                      cf->linenr, cf->name);
        }
 
-       if (cf->die_on_error)
+       switch (opts && opts->error_action ?
+               opts->error_action :
+               cf->default_error_action) {
+       case CONFIG_ERROR_DIE:
                die("%s", error_msg);
                die("%s", error_msg);
-       else
+               break;
+       case CONFIG_ERROR_ERROR:
                error_return = error("%s", error_msg);
                error_return = error("%s", error_msg);
+               break;
+       case CONFIG_ERROR_UNSET:
+               BUG("config error action unset");
+       }
 
        free(error_msg);
        return error_return;
 
        free(error_msg);
        return error_return;
@@ -1520,7 +1528,7 @@ static int do_config_from_file(config_fn_t fn,
        top.origin_type = origin_type;
        top.name = name;
        top.path = path;
        top.origin_type = origin_type;
        top.name = name;
        top.path = path;
-       top.die_on_error = 1;
+       top.default_error_action = CONFIG_ERROR_DIE;
        top.do_fgetc = config_file_fgetc;
        top.do_ungetc = config_file_ungetc;
        top.do_ftell = config_file_ftell;
        top.do_fgetc = config_file_fgetc;
        top.do_ungetc = config_file_ungetc;
        top.do_ftell = config_file_ftell;
@@ -1569,7 +1577,7 @@ int git_config_from_mem(config_fn_t fn, const enum config_origin_type origin_typ
        top.origin_type = origin_type;
        top.name = name;
        top.path = NULL;
        top.origin_type = origin_type;
        top.name = name;
        top.path = NULL;
-       top.die_on_error = 0;
+       top.default_error_action = CONFIG_ERROR_ERROR;
        top.do_fgetc = config_buf_fgetc;
        top.do_ungetc = config_buf_ungetc;
        top.do_ftell = config_buf_ftell;
        top.do_fgetc = config_buf_fgetc;
        top.do_ungetc = config_buf_ungetc;
        top.do_ftell = config_buf_ftell;
index 626d4654bd6f98771be903cca208fdb368920bf4..ce75bf1e5cc20d149c462b1d467640564083257e 100644 (file)
--- a/config.h
+++ b/config.h
@@ -54,6 +54,11 @@ struct config_options {
        const char *git_dir;
        config_parser_event_fn_t event_fn;
        void *event_fn_data;
        const char *git_dir;
        config_parser_event_fn_t event_fn;
        void *event_fn_data;
+       enum config_error_action {
+               CONFIG_ERROR_UNSET = 0, /* use source-specific default */
+               CONFIG_ERROR_DIE, /* die() on error */
+               CONFIG_ERROR_ERROR, /* error() on error, return -1 */
+       } error_action;
 };
 
 typedef int (*config_fn_t)(const char *, const char *, void *);
 };
 
 typedef int (*config_fn_t)(const char *, const char *, void *);