mirror of
https://github.com/git-for-windows/git.git
synced 2026-06-11 19:37:52 -05:00
config: add git_config_key_is_valid() for quiet validation
Move the body of git_config_parse_key() into a static helper do_parse_config_key() that takes a "quiet" flag and treats store_key as optional. git_config_parse_key() becomes a thin wrapper. Add git_config_key_is_valid() for callers that only need to know whether a key is well-formed. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
94f057755b
commit
9b03e2790a
38
config.c
38
config.c
@@ -536,11 +536,14 @@ static inline int iskeychar(int c)
|
||||
* -2 if there is no section name in the key.
|
||||
*
|
||||
* store_key - pointer to char* which will hold a copy of the key with
|
||||
* lowercase section and variable name
|
||||
* lowercase section and variable name, can be NULL to skip
|
||||
* allocation when only validation is needed
|
||||
* baselen - pointer to size_t which will hold the length of the
|
||||
* section + subsection part, can be NULL
|
||||
* quiet - when non-zero, suppress error() reports on rejection
|
||||
*/
|
||||
int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
|
||||
static int do_parse_config_key(const char *key, char **store_key,
|
||||
size_t *baselen_, int quiet)
|
||||
{
|
||||
size_t i, baselen;
|
||||
int dot;
|
||||
@@ -552,12 +555,14 @@ int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
|
||||
*/
|
||||
|
||||
if (last_dot == NULL || last_dot == key) {
|
||||
error(_("key does not contain a section: %s"), key);
|
||||
if (!quiet)
|
||||
error(_("key does not contain a section: %s"), key);
|
||||
return -CONFIG_NO_SECTION_OR_NAME;
|
||||
}
|
||||
|
||||
if (!last_dot[1]) {
|
||||
error(_("key does not contain variable name: %s"), key);
|
||||
if (!quiet)
|
||||
error(_("key does not contain variable name: %s"), key);
|
||||
return -CONFIG_NO_SECTION_OR_NAME;
|
||||
}
|
||||
|
||||
@@ -568,7 +573,8 @@ int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
|
||||
/*
|
||||
* Validate the key and while at it, lower case it for matching.
|
||||
*/
|
||||
*store_key = xmallocz(strlen(key));
|
||||
if (store_key)
|
||||
*store_key = xmallocz(strlen(key));
|
||||
|
||||
dot = 0;
|
||||
for (i = 0; key[i]; i++) {
|
||||
@@ -579,24 +585,38 @@ int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
|
||||
if (!dot || i > baselen) {
|
||||
if (!iskeychar(c) ||
|
||||
(i == baselen + 1 && !isalpha(c))) {
|
||||
error(_("invalid key: %s"), key);
|
||||
if (!quiet)
|
||||
error(_("invalid key: %s"), key);
|
||||
goto out_free_ret_1;
|
||||
}
|
||||
c = tolower(c);
|
||||
} else if (c == '\n') {
|
||||
error(_("invalid key (newline): %s"), key);
|
||||
if (!quiet)
|
||||
error(_("invalid key (newline): %s"), key);
|
||||
goto out_free_ret_1;
|
||||
}
|
||||
(*store_key)[i] = c;
|
||||
if (store_key)
|
||||
(*store_key)[i] = c;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
out_free_ret_1:
|
||||
FREE_AND_NULL(*store_key);
|
||||
if (store_key)
|
||||
FREE_AND_NULL(*store_key);
|
||||
return -CONFIG_INVALID_KEY;
|
||||
}
|
||||
|
||||
int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
|
||||
{
|
||||
return do_parse_config_key(key, store_key, baselen_, 0);
|
||||
}
|
||||
|
||||
int git_config_key_is_valid(const char *key)
|
||||
{
|
||||
return !do_parse_config_key(key, NULL, NULL, 1);
|
||||
}
|
||||
|
||||
static int config_parse_pair(const char *key, const char *value,
|
||||
struct key_value_info *kvi,
|
||||
config_fn_t fn, void *data)
|
||||
|
||||
2
config.h
2
config.h
@@ -337,6 +337,8 @@ void repo_config_set(struct repository *, const char *, const char *);
|
||||
|
||||
int git_config_parse_key(const char *, char **, size_t *);
|
||||
|
||||
int git_config_key_is_valid(const char *);
|
||||
|
||||
/*
|
||||
* The following macros specify flag bits that alter the behavior
|
||||
* of the repo_config_set_multivar*() methods.
|
||||
|
||||
Reference in New Issue
Block a user