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 <agc.klotz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Andrew Klotz committed Feb 11, 2021 at 20:30 UTC f276e2a469430999ff7e3735ea7b41508ed1abd8
2 files changed +25 -2
config.c
+18 -2
@@ -1030,6 +1030,20 @@ static void die_bad_number(const char *name, const char *value)
1030 }
1031 }
1032
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 +
1047 int git_config_int(const char *name, const char *value)
1048 {
1049 int ret;
@@ -1102,8 +1116,10 @@ int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
1116
1117 int git_config_bool(const char *name, const char *value)
1118 {
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;
1123 }
1124
1125 int git_config_string(const char **dest, const char *var, const char *value)
t/t1300-config.sh
+7
@@ -672,6 +672,13 @@ test_expect_success 'invalid unit' '
672 test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
673 '
674
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 +
682 test_expect_success 'line number is reported correctly' '
683 printf "[bool]\n\tvar\n" >invalid &&
684 test_must_fail git config -f invalid --path bool.var 2>actual &&