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>
This commit is contained in:
parent
33fdd77e2b
commit
2f666581bb
17
config.c
17
config.c
@ -543,18 +543,25 @@ int git_parse_ulong(const char *value, unsigned long *ret)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void die_bad_config(const char *name)
|
static void die_bad_number(const char *name, const char *value)
|
||||||
{
|
{
|
||||||
|
const char *reason = errno == ERANGE ?
|
||||||
|
"out of range" :
|
||||||
|
"invalid unit";
|
||||||
|
if (!value)
|
||||||
|
value = "";
|
||||||
|
|
||||||
if (cf && cf->name)
|
if (cf && cf->name)
|
||||||
die("bad config value for '%s' in %s", name, cf->name);
|
die("bad numeric config value '%s' for '%s' in %s: %s",
|
||||||
die("bad config value for '%s'", name);
|
value, name, cf->name, reason);
|
||||||
|
die("bad numeric config value '%s' for '%s': %s", value, name, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_config_int(const char *name, const char *value)
|
int git_config_int(const char *name, const char *value)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
if (!git_parse_int(value, &ret))
|
if (!git_parse_int(value, &ret))
|
||||||
die_bad_config(name);
|
die_bad_number(name, value);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -562,7 +569,7 @@ unsigned long git_config_ulong(const char *name, const char *value)
|
|||||||
{
|
{
|
||||||
unsigned long ret;
|
unsigned long ret;
|
||||||
if (!git_parse_ulong(value, &ret))
|
if (!git_parse_ulong(value, &ret))
|
||||||
die_bad_config(name);
|
die_bad_number(name, value);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -657,11 +657,11 @@ test_expect_success 'invalid unit' '
|
|||||||
echo 1auto >expect &&
|
echo 1auto >expect &&
|
||||||
git config aninvalid.unit >actual &&
|
git config aninvalid.unit >actual &&
|
||||||
test_cmp expect actual &&
|
test_cmp expect actual &&
|
||||||
cat > expect <<-\EOF
|
cat >expect <<-\EOF
|
||||||
fatal: bad config value for '\''aninvalid.unit'\'' in .git/config
|
fatal: bad numeric config value '\''1auto'\'' for '\''aninvalid.unit'\'' in .git/config: invalid unit
|
||||||
EOF
|
EOF
|
||||||
test_must_fail git config --int --get aninvalid.unit 2>actual &&
|
test_must_fail git config --int --get aninvalid.unit 2>actual &&
|
||||||
test_cmp actual expect
|
test_i18ncmp expect actual
|
||||||
'
|
'
|
||||||
|
|
||||||
cat > expect << EOF
|
cat > expect << EOF
|
||||||
|
@ -73,7 +73,7 @@ test_expect_success 'plumbing not affected' '
|
|||||||
test_expect_success 'non-integer config parsing' '
|
test_expect_success 'non-integer config parsing' '
|
||||||
git config diff.context no &&
|
git config diff.context no &&
|
||||||
test_must_fail git diff 2>output &&
|
test_must_fail git diff 2>output &&
|
||||||
test_i18ngrep "bad config value" output
|
test_i18ngrep "bad numeric config value" output
|
||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success 'negative integer config parsing' '
|
test_expect_success 'negative integer config parsing' '
|
||||||
|
Loading…
Reference in New Issue
Block a user