Skip to content

Commit 2f66658

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33fdd77 commit 2f66658

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

config.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,26 +543,33 @@ int git_parse_ulong(const char *value, unsigned long *ret)
543543
return 1;
544544
}
545545

546-
static void die_bad_config(const char *name)
546+
static void die_bad_number(const char *name, const char *value)
547547
{
548+
const char *reason = errno == ERANGE ?
549+
"out of range" :
550+
"invalid unit";
551+
if (!value)
552+
value = "";
553+
548554
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);
551558
}
552559

553560
int git_config_int(const char *name, const char *value)
554561
{
555562
int ret;
556563
if (!git_parse_int(value, &ret))
557-
die_bad_config(name);
564+
die_bad_number(name, value);
558565
return ret;
559566
}
560567

561568
unsigned long git_config_ulong(const char *name, const char *value)
562569
{
563570
unsigned long ret;
564571
if (!git_parse_ulong(value, &ret))
565-
die_bad_config(name);
572+
die_bad_number(name, value);
566573
return ret;
567574
}
568575

t/t1300-repo-config.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,11 @@ test_expect_success 'invalid unit' '
657657
echo 1auto >expect &&
658658
git config aninvalid.unit >actual &&
659659
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
662662
EOF
663663
test_must_fail git config --int --get aninvalid.unit 2>actual &&
664-
test_cmp actual expect
664+
test_i18ncmp expect actual
665665
'
666666

667667
cat > expect << EOF

t/t4055-diff-context.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ test_expect_success 'plumbing not affected' '
7373
test_expect_success 'non-integer config parsing' '
7474
git config diff.context no &&
7575
test_must_fail git diff 2>output &&
76-
test_i18ngrep "bad config value" output
76+
test_i18ngrep "bad numeric config value" output
7777
'
7878

7979
test_expect_success 'negative integer config parsing' '

0 commit comments

Comments
 (0)