@ -1848,7 +1848,7 @@ int git_config_set(const char *key, const char *value)
* baselen - pointer to int which will hold the length of the
* baselen - pointer to int which will hold the length of the
* section + subsection part, can be NULL
* section + subsection part, can be NULL
*/
*/
int git_config_parse_key(const char *key, char **store_key, int *baselen_)
static int git_config_parse_key_1(const char *key, char **store_key, int *baselen_, int quiet)
{
{
int i, dot, baselen;
int i, dot, baselen;
const char *last_dot = strrchr(key, '.');
const char *last_dot = strrchr(key, '.');
@ -1859,11 +1859,13 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
*/
*/
if (last_dot == NULL || last_dot == key) {
if (last_dot == NULL || last_dot == key) {
if (!quiet)
error("key does not contain a section: %s", key);
error("key does not contain a section: %s", key);
return -CONFIG_NO_SECTION_OR_NAME;
return -CONFIG_NO_SECTION_OR_NAME;
}
}
if (!last_dot[1]) {
if (!last_dot[1]) {
if (!quiet)
error("key does not contain variable name: %s", key);
error("key does not contain variable name: %s", key);
return -CONFIG_NO_SECTION_OR_NAME;
return -CONFIG_NO_SECTION_OR_NAME;
}
}
@ -1875,6 +1877,7 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
/*
/*
* Validate the key and while at it, lower case it for matching.
* Validate the key and while at it, lower case it for matching.
*/
*/
if (store_key)
*store_key = xmalloc(strlen(key) + 1);
*store_key = xmalloc(strlen(key) + 1);
dot = 0;
dot = 0;
@ -1886,26 +1889,42 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
if (!dot || i > baselen) {
if (!dot || i > baselen) {
if (!iskeychar(c) ||
if (!iskeychar(c) ||
(i == baselen + 1 && !isalpha(c))) {
(i == baselen + 1 && !isalpha(c))) {
if (!quiet)
error("invalid key: %s", key);
error("invalid key: %s", key);
goto out_free_ret_1;
goto out_free_ret_1;
}
}
c = tolower(c);
c = tolower(c);
} else if (c == '\n') {
} else if (c == '\n') {
if (!quiet)
error("invalid key (newline): %s", key);
error("invalid key (newline): %s", key);
goto out_free_ret_1;
goto out_free_ret_1;
}
}
if (store_key)
(*store_key)[i] = c;
(*store_key)[i] = c;
}
}
if (store_key)
(*store_key)[i] = 0;
(*store_key)[i] = 0;
return 0;
return 0;
out_free_ret_1:
out_free_ret_1:
if (store_key) {
free(*store_key);
free(*store_key);
*store_key = NULL;
*store_key = NULL;
}
return -CONFIG_INVALID_KEY;
return -CONFIG_INVALID_KEY;
}
}
int git_config_parse_key(const char *key, char **store_key, int *baselen)
{
return git_config_parse_key_1(key, store_key, baselen, 0);
}
int git_config_key_is_valid(const char *key)
{
return !git_config_parse_key_1(key, NULL, NULL, 1);
}
/*
/*
* If value==NULL, unset in (remove from) config,
* If value==NULL, unset in (remove from) config,
* if value_regex!=NULL, disregard key/value pairs where value does not match.
* if value_regex!=NULL, disregard key/value pairs where value does not match.