]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: improve error message for boolean config
authorAndrew Klotz <agc.klotz@gmail.com>
Thu, 11 Feb 2021 20:30:53 +0000 (20:30 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Feb 2021 21:44:55 +0000 (13:44 -0800)
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>
config.c
t/t1300-config.sh

index 1137bd73aff07c77e9b73a0b4c1d193cd38d88de..5d15e556358239067642b68da654514e26c6d7d1 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1030,6 +1030,20 @@ static void die_bad_number(const char *name, const char *value)
        }
 }
 
+NORETURN
+static void die_bad_bool(const char *name, const char *value)
+{
+       if (!strcmp(name, "GIT_TEST_GETTEXT_POISON"))
+               /*
+                * We explicitly *don't* use _() here since it would
+                * cause an infinite loop with _() needing to call
+                * use_gettext_poison().
+                */
+               die("bad boolean config value '%s' for '%s'", value, name);
+       else
+               die(_("bad boolean config value '%s' for '%s'"), value, name);
+}
+
 int git_config_int(const char *name, const char *value)
 {
        int ret;
@@ -1102,8 +1116,10 @@ int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
 
 int git_config_bool(const char *name, const char *value)
 {
-       int discard;
-       return !!git_config_bool_or_int(name, value, &discard);
+       int v = git_parse_maybe_bool(value);
+       if (v < 0)
+               die_bad_bool(name, value);
+       return v;
 }
 
 int git_config_string(const char **dest, const char *var, const char *value)
index 1a4156c70434f34ef7693d60b5932d92d18845b0..d6fb9c175367260a8048d2d88a23bc794d48477d 100755 (executable)
@@ -672,6 +672,13 @@ test_expect_success 'invalid unit' '
        test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
 '
 
+test_expect_success 'invalid unit boolean' '
+       git config commit.gpgsign "1true" &&
+       test_cmp_config 1true commit.gpgsign &&
+       test_must_fail git config --bool --get commit.gpgsign 2>actual &&
+       test_i18ngrep "bad boolean config value .1true. for .commit.gpgsign." actual
+'
+
 test_expect_success 'line number is reported correctly' '
        printf "[bool]\n\tvar\n" >invalid &&
        test_must_fail git config -f invalid --path bool.var 2>actual &&