Skip to content

Commit f276e2a

Browse files
KlotzAndrewgitster
authored andcommitted
config: improve error message for boolean config
Currently invalid boolean config values return messages about 'bad numeric', which is slightly misleading when the error was due to a boolean value. We can improve the developer experience by returning a boolean error message when we know the value is neither a bool text or int. before with an invalid boolean value of `non-boolean`, its unclear what numeric is referring to: fatal: bad numeric config value 'non-boolean' for 'commit.gpgsign': invalid unit now the error message mentions `non-boolean` is a bad boolean value: fatal: bad boolean config value 'non-boolean' for 'commit.gpgsign' Signed-off-by: Andrew Klotz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 773e25a commit f276e2a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

config.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,20 @@ static void die_bad_number(const char *name, const char *value)
10301030
}
10311031
}
10321032

1033+
NORETURN
1034+
static void die_bad_bool(const char *name, const char *value)
1035+
{
1036+
if (!strcmp(name, "GIT_TEST_GETTEXT_POISON"))
1037+
/*
1038+
* We explicitly *don't* use _() here since it would
1039+
* cause an infinite loop with _() needing to call
1040+
* use_gettext_poison().
1041+
*/
1042+
die("bad boolean config value '%s' for '%s'", value, name);
1043+
else
1044+
die(_("bad boolean config value '%s' for '%s'"), value, name);
1045+
}
1046+
10331047
int git_config_int(const char *name, const char *value)
10341048
{
10351049
int ret;
@@ -1102,8 +1116,10 @@ int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
11021116

11031117
int git_config_bool(const char *name, const char *value)
11041118
{
1105-
int discard;
1106-
return !!git_config_bool_or_int(name, value, &discard);
1119+
int v = git_parse_maybe_bool(value);
1120+
if (v < 0)
1121+
die_bad_bool(name, value);
1122+
return v;
11071123
}
11081124

11091125
int git_config_string(const char **dest, const char *var, const char *value)

t/t1300-config.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,13 @@ test_expect_success 'invalid unit' '
672672
test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
673673
'
674674

675+
test_expect_success 'invalid unit boolean' '
676+
git config commit.gpgsign "1true" &&
677+
test_cmp_config 1true commit.gpgsign &&
678+
test_must_fail git config --bool --get commit.gpgsign 2>actual &&
679+
test_i18ngrep "bad boolean config value .1true. for .commit.gpgsign." actual
680+
'
681+
675682
test_expect_success 'line number is reported correctly' '
676683
printf "[bool]\n\tvar\n" >invalid &&
677684
test_must_fail git config -f invalid --path bool.var 2>actual &&

0 commit comments

Comments
 (0)