mirror of
https://github.com/git/git
synced 2024-08-27 19:49:21 +00:00
Improve error messages when int/long cannot be parsed from config
If a config file has become mildly corrupted due to a missing LF we may discover some other option joined up against the end of a numeric value. For example: [section] number = 1auto where the "auto" flag was meant to occur on the next line, below "number", but the missing LF has caused it to no longer be its own option. Instead the word "auto" is parsed as a 'unit factor' for the value of "number". Before this change we got the confusing error message: fatal: unknown unit: 'auto' which told us nothing about where the problem appeared. Now we get: fatal: bad config value for 'aninvalid.unit' which at least points the user in the right direction of where to search for the incorrectly formatted configuration file. Noticed by erikh on #git, which received the original error from a simple `git checkout -b` due to a midly corrupted config. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
3a969ef1d6
commit
c8deb5a146
31
config.c
31
config.c
|
@ -234,17 +234,23 @@ static int git_parse_file(config_fn_t fn)
|
|||
die("bad config file line %d in %s", config_linenr, config_file_name);
|
||||
}
|
||||
|
||||
static unsigned long get_unit_factor(const char *end)
|
||||
static int parse_unit_factor(const char *end, unsigned long *val)
|
||||
{
|
||||
if (!*end)
|
||||
return 1;
|
||||
else if (!strcasecmp(end, "k"))
|
||||
return 1024;
|
||||
else if (!strcasecmp(end, "m"))
|
||||
return 1024 * 1024;
|
||||
else if (!strcasecmp(end, "g"))
|
||||
return 1024 * 1024 * 1024;
|
||||
die("unknown unit: '%s'", end);
|
||||
else if (!strcasecmp(end, "k")) {
|
||||
*val *= 1024;
|
||||
return 1;
|
||||
}
|
||||
else if (!strcasecmp(end, "m")) {
|
||||
*val *= 1024 * 1024;
|
||||
return 1;
|
||||
}
|
||||
else if (!strcasecmp(end, "g")) {
|
||||
*val *= 1024 * 1024 * 1024;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int git_parse_long(const char *value, long *ret)
|
||||
|
@ -252,7 +258,10 @@ int git_parse_long(const char *value, long *ret)
|
|||
if (value && *value) {
|
||||
char *end;
|
||||
long val = strtol(value, &end, 0);
|
||||
*ret = val * get_unit_factor(end);
|
||||
unsigned long factor = 1;
|
||||
if (!parse_unit_factor(end, &factor))
|
||||
return 0;
|
||||
*ret = val * factor;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
@ -263,7 +272,9 @@ int git_parse_ulong(const char *value, unsigned long *ret)
|
|||
if (value && *value) {
|
||||
char *end;
|
||||
unsigned long val = strtoul(value, &end, 0);
|
||||
*ret = val * get_unit_factor(end);
|
||||
if (!parse_unit_factor(end, &val))
|
||||
return 0;
|
||||
*ret = val;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
|
|
@ -448,6 +448,23 @@ test_expect_success numbers '
|
|||
test z1048576 = "z$m"
|
||||
'
|
||||
|
||||
cat > expect <<EOF
|
||||
fatal: bad config value for 'aninvalid.unit' in .git/config
|
||||
EOF
|
||||
|
||||
test_expect_success 'invalid unit' '
|
||||
|
||||
git config aninvalid.unit "1auto" &&
|
||||
s=$(git config aninvalid.unit) &&
|
||||
test "z1auto" = "z$s" &&
|
||||
if git config --int --get aninvalid.unit 2>actual
|
||||
then
|
||||
echo config should have failed
|
||||
false
|
||||
fi &&
|
||||
cmp actual expect
|
||||
'
|
||||
|
||||
cat > expect << EOF
|
||||
true
|
||||
false
|
||||
|
|
Loading…
Reference in a new issue