mirror of
https://github.com/git-for-windows/git.git
synced 2026-06-13 08:57:56 -05:00
Merge branch 'hn/config-typo-advice' into seen
"git config foo.bar=baz" is not likely to be a request to read the value of such a variable with '=' in its name; rather it is plausible that the user meant "git config set foo.bar baz". Give advice when giving an error message. * hn/config-typo-advice: config: improve diagnostic for "set" with missing value config: add git_config_key_is_valid() for quiet validation
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#define USE_THE_REPOSITORY_VARIABLE
|
||||
#include "builtin.h"
|
||||
#include "abspath.h"
|
||||
#include "advice.h"
|
||||
#include "config.h"
|
||||
#include "color.h"
|
||||
#include "date.h"
|
||||
@@ -210,6 +211,26 @@ static void check_argc(int argc, int min, int max)
|
||||
exit(129);
|
||||
}
|
||||
|
||||
static NORETURN void die_missing_set_value(const char *arg)
|
||||
{
|
||||
const char *last_dot = strrchr(arg, '.');
|
||||
const char *eq = last_dot ? strchr(last_dot + 1, '=') : NULL;
|
||||
char *prefix = eq ? xstrndup(arg, eq - arg) : NULL;
|
||||
|
||||
if (prefix && git_config_key_is_valid(prefix)) {
|
||||
error(_("missing value to set to the variable '%s'"), arg);
|
||||
advise(_("did you mean \"git config set %s %s\"?"),
|
||||
prefix, eq + 1);
|
||||
} else if (git_config_key_is_valid(arg)) {
|
||||
error(_("missing value to set to the variable '%s'"), arg);
|
||||
} else {
|
||||
error(_("missing value to set to a variable with an invalid name '%s'"),
|
||||
arg);
|
||||
}
|
||||
free(prefix);
|
||||
exit(129);
|
||||
}
|
||||
|
||||
static void show_config_origin(const struct config_display_options *opts,
|
||||
const struct key_value_info *kvi,
|
||||
struct strbuf *buf)
|
||||
@@ -1133,6 +1154,8 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix,
|
||||
|
||||
argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage,
|
||||
PARSE_OPT_STOP_AT_NON_OPTION);
|
||||
if (argc == 1)
|
||||
die_missing_set_value(argv[0]);
|
||||
check_argc(argc, 2, 2);
|
||||
|
||||
if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
|
||||
@@ -1371,6 +1394,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
|
||||
};
|
||||
char *value = NULL, *comment = NULL;
|
||||
int ret = 0;
|
||||
int actions_implicit;
|
||||
struct key_value_info default_kvi = KVI_INIT;
|
||||
|
||||
argc = parse_options(argc, argv, prefix, opts,
|
||||
@@ -1385,7 +1409,8 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
|
||||
exit(129);
|
||||
}
|
||||
|
||||
if (actions == 0)
|
||||
actions_implicit = (actions == 0);
|
||||
if (actions_implicit)
|
||||
switch (argc) {
|
||||
case 1: actions = ACTION_GET; break;
|
||||
case 2: actions = ACTION_SET; break;
|
||||
@@ -1394,6 +1419,11 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
|
||||
error(_("no action specified"));
|
||||
exit(129);
|
||||
}
|
||||
if (actions_implicit && argc == 1) {
|
||||
const char *last_dot = strrchr(argv[0], '.');
|
||||
if (last_dot && strchr(last_dot + 1, '='))
|
||||
die_missing_set_value(argv[0]);
|
||||
}
|
||||
if (display_opts.omit_values &&
|
||||
!(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
|
||||
error(_("--name-only is only applicable to --list or --get-regexp"));
|
||||
|
||||
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
@@ -343,6 +343,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.
|
||||
|
||||
@@ -469,6 +469,62 @@ test_expect_success 'invalid key' '
|
||||
test_must_fail git config inval.2key blabla
|
||||
'
|
||||
|
||||
test_expect_success 'set with 1 arg of "key=value": valid key suggests split form' '
|
||||
test_must_fail git config set pull.rebase=false 2>err &&
|
||||
test_grep "missing value to set to the variable .pull\\.rebase=false." err &&
|
||||
test_grep "did you mean .git config set pull\\.rebase false." err
|
||||
'
|
||||
|
||||
test_expect_success 'set with 1 arg of "key=value": implicit form suggests split form' '
|
||||
test_must_fail git config pull.rebase=false 2>err &&
|
||||
test_grep "missing value to set to the variable .pull\\.rebase=false." err &&
|
||||
test_grep "did you mean .git config set pull\\.rebase false." err
|
||||
'
|
||||
|
||||
test_expect_success 'set with 1 arg of "key=value": invalid key does not suggest split form' '
|
||||
test_must_fail git config set foo=bar 2>err &&
|
||||
test_grep "missing value to set to a variable with an invalid name .foo=bar." err &&
|
||||
test_grep ! "did you mean" err
|
||||
'
|
||||
|
||||
test_expect_success 'set with 1 arg: variable name starting with digit is invalid' '
|
||||
test_must_fail git config set foo.1bar=baz 2>err &&
|
||||
test_grep "missing value to set to a variable with an invalid name .foo\\.1bar=baz." err &&
|
||||
test_grep ! "did you mean" err
|
||||
'
|
||||
|
||||
test_expect_success 'set with 1 arg: digit-led section name is valid' '
|
||||
test_must_fail git config set 1foo.bar=baz 2>err &&
|
||||
test_grep "missing value to set to the variable .1foo\\.bar=baz." err &&
|
||||
test_grep "did you mean .git config set 1foo\\.bar baz." err
|
||||
'
|
||||
|
||||
test_expect_success 'set with 1 arg: subsection plus invalid variable name' '
|
||||
test_must_fail git config set foo.some.b_r=baz 2>err &&
|
||||
test_grep "missing value to set to a variable with an invalid name .foo\\.some\\.b_r=baz." err &&
|
||||
test_grep ! "did you mean" err
|
||||
'
|
||||
|
||||
test_expect_success 'set with 1 arg of valid key reports missing value' '
|
||||
test_must_fail git config set pull.rebase 2>err &&
|
||||
test_grep "missing value to set to the variable .pull\\.rebase." err &&
|
||||
test_grep ! "did you mean" err
|
||||
'
|
||||
|
||||
test_expect_success 'set with 2 args including "=" in invalid key does not suggest' '
|
||||
test_must_fail git config set pull.rebase=false true 2>err &&
|
||||
test_grep "invalid key: pull\\.rebase=false" err &&
|
||||
test_grep ! "did you mean" err
|
||||
'
|
||||
|
||||
test_expect_success '"=" inside subsection is valid' '
|
||||
test_when_finished "rm -f subsection.cfg" &&
|
||||
git config set -f subsection.cfg foo.bar=baz.boo qux &&
|
||||
echo qux >expect &&
|
||||
git config get -f subsection.cfg foo.bar=baz.boo >actual &&
|
||||
test_cmp expect actual
|
||||
'
|
||||
|
||||
test_expect_success 'correct key' '
|
||||
git config 123456.a123 987
|
||||
'
|
||||
|
||||
Reference in New Issue
Block a user