Skip to content

Commit 66f9722

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e333175 commit 66f9722

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

config.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct config_source {
3131
enum config_origin_type origin_type;
3232
const char *name;
3333
const char *path;
34-
int die_on_error;
34+
enum config_error_action default_error_action;
3535
int linenr;
3636
int eof;
3737
struct strbuf value;
@@ -809,10 +809,18 @@ static int git_parse_source(config_fn_t fn, void *data,
809809
cf->linenr, cf->name);
810810
}
811811

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:
813816
die("%s", error_msg);
814-
else
817+
break;
818+
case CONFIG_ERROR_ERROR:
815819
error_return = error("%s", error_msg);
820+
break;
821+
case CONFIG_ERROR_UNSET:
822+
BUG("config error action unset");
823+
}
816824

817825
free(error_msg);
818826
return error_return;
@@ -1520,7 +1528,7 @@ static int do_config_from_file(config_fn_t fn,
15201528
top.origin_type = origin_type;
15211529
top.name = name;
15221530
top.path = path;
1523-
top.die_on_error = 1;
1531+
top.default_error_action = CONFIG_ERROR_DIE;
15241532
top.do_fgetc = config_file_fgetc;
15251533
top.do_ungetc = config_file_ungetc;
15261534
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
15691577
top.origin_type = origin_type;
15701578
top.name = name;
15711579
top.path = NULL;
1572-
top.die_on_error = 0;
1580+
top.default_error_action = CONFIG_ERROR_ERROR;
15731581
top.do_fgetc = config_buf_fgetc;
15741582
top.do_ungetc = config_buf_ungetc;
15751583
top.do_ftell = config_buf_ftell;

config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ struct config_options {
5454
const char *git_dir;
5555
config_parser_event_fn_t event_fn;
5656
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;
5762
};
5863

5964
typedef int (*config_fn_t)(const char *, const char *, void *);

0 commit comments

Comments
 (0)