config: turn die_on_error into caller-facing enum

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>

Jeff King committed Jun 28, 2018 at 18:05 UTC 66f9722882993f60be656afaae4c5c9ac92957e9
2 files changed +18 -5
config.c
+13 -5
@@ -31,7 +31,7 @@ struct config_source {
31 enum config_origin_type origin_type;
32 const char *name;
33 const char *path;
34 - int die_on_error;
34 + enum config_error_action default_error_action;
35 int linenr;
36 int eof;
37 struct strbuf value;
@@ -809,10 +809,18 @@ static int git_parse_source(config_fn_t fn, void *data,
809 cf->linenr, cf->name);
810 }
811
812 - if (cf->die_on_error)
812 + switch (opts && opts->error_action ?
813 + opts->error_action :
814 + cf->default_error_action) {
815 + case CONFIG_ERROR_DIE:
816 die("%s", error_msg);
814 - else
817 + break;
818 + case CONFIG_ERROR_ERROR:
819 error_return = error("%s", error_msg);
820 + break;
821 + case CONFIG_ERROR_UNSET:
822 + BUG("config error action unset");
823 + }
824
825 free(error_msg);
826 return error_return;
@@ -1520,7 +1528,7 @@ static int do_config_from_file(config_fn_t fn,
1528 top.origin_type = origin_type;
1529 top.name = name;
1530 top.path = path;
1523 - top.die_on_error = 1;
1531 + top.default_error_action = CONFIG_ERROR_DIE;
1532 top.do_fgetc = config_file_fgetc;
1533 top.do_ungetc = config_file_ungetc;
1534 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
1577 top.origin_type = origin_type;
1578 top.name = name;
1579 top.path = NULL;
1572 - top.die_on_error = 0;
1580 + top.default_error_action = CONFIG_ERROR_ERROR;
1581 top.do_fgetc = config_buf_fgetc;
1582 top.do_ungetc = config_buf_ungetc;
1583 top.do_ftell = config_buf_ftell;
config.h
+5
@@ -54,6 +54,11 @@ struct config_options {
54 const char *git_dir;
55 config_parser_event_fn_t event_fn;
56 void *event_fn_data;
57 + enum config_error_action {
58 + CONFIG_ERROR_UNSET = 0, /* use source-specific default */
59 + CONFIG_ERROR_DIE, /* die() on error */
60 + CONFIG_ERROR_ERROR, /* error() on error, return -1 */
61 + } error_action;
62 };
63
64 typedef int (*config_fn_t)(const char *, const char *, void *);