config: make numeric parsing errors more clear

If we try to parse an integer config argument and get a number outside of the representable range, we die with the cryptic message: "bad config value for '%s'". We can improve two things: 1. Show the value that produced the error (e.g., bad config value '3g' for 'foo.bar'). 2. Mention the reason the value was rejected (e.g., "invalid unit" versus "out of range"). A few tests need to be updated with the new output, but that should not be representative of real-world breakage, as scripts should not be depending on the exact text of our stderr output, which is subject to i18n anyway. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 8, 2013 at 04:38 UTC 2f666581bbc1895cf66a7007fb222026b299deb9
3 files changed +16 -9
config.c
+12 -5
@@ -543,18 +543,25 @@ int git_parse_ulong(const char *value, unsigned long *ret)
543 return 1;
544 }
545
546 -static void die_bad_config(const char *name)
546 +static void die_bad_number(const char *name, const char *value)
547 {
548 + const char *reason = errno == ERANGE ?
549 + "out of range" :
550 + "invalid unit";
551 + if (!value)
552 + value = "";
553 +
554 if (cf && cf->name)
549 - die("bad config value for '%s' in %s", name, cf->name);
550 - die("bad config value for '%s'", name);
555 + die("bad numeric config value '%s' for '%s' in %s: %s",
556 + value, name, cf->name, reason);
557 + die("bad numeric config value '%s' for '%s': %s", value, name, reason);
558 }
559
560 int git_config_int(const char *name, const char *value)
561 {
562 int ret;
563 if (!git_parse_int(value, &ret))
557 - die_bad_config(name);
564 + die_bad_number(name, value);
565 return ret;
566 }
567
@@ -562,7 +569,7 @@ unsigned long git_config_ulong(const char *name, const char *value)
569 {
570 unsigned long ret;
571 if (!git_parse_ulong(value, &ret))
565 - die_bad_config(name);
572 + die_bad_number(name, value);
573 return ret;
574 }
575
t/t1300-repo-config.sh
+3 -3
@@ -657,11 +657,11 @@ test_expect_success 'invalid unit' '
657 echo 1auto >expect &&
658 git config aninvalid.unit >actual &&
659 test_cmp expect actual &&
660 - cat > expect <<-\EOF
661 - fatal: bad config value for '\''aninvalid.unit'\'' in .git/config
660 + cat >expect <<-\EOF
661 + fatal: bad numeric config value '\''1auto'\'' for '\''aninvalid.unit'\'' in .git/config: invalid unit
662 EOF
663 test_must_fail git config --int --get aninvalid.unit 2>actual &&
664 - test_cmp actual expect
664 + test_i18ncmp expect actual
665 '
666
667 cat > expect << EOF
t/t4055-diff-context.sh
+1 -1
@@ -73,7 +73,7 @@ test_expect_success 'plumbing not affected' '
73 test_expect_success 'non-integer config parsing' '
74 git config diff.context no &&
75 test_must_fail git diff 2>output &&
76 - test_i18ngrep "bad config value" output
76 + test_i18ngrep "bad numeric config value" output
77 '
78
79 test_expect_success 'negative integer config parsing' '