config: improve diagnostic for "set" with missing value

"git config set pull.rebase=false" currently fails with "wrong
number of arguments", and the implicit form "git config
pull.rebase=false" fails with "invalid key". Neither points at
the real problem: the value is missing.

Report that directly, and when the argument has the shape
"<valid-key>=<value>", also suggest the split form:

    $ git config set pull.rebase=false
    error: missing value to set to the variable 'pull.rebase=false'
    hint: did you mean "git config set pull.rebase false"?

When the prefix before "=" is not a valid key, drop the hint:

    $ git config set foo=bar
    error: missing value to set to a variable with an invalid name 'foo=bar'

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Harald Nordgren
2026-06-02 18:43:28 +00:00
committed by Junio C Hamano
parent 9b03e2790a
commit 03c29e2e98
2 changed files with 87 additions and 1 deletions

View File

@@ -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"));

View File

@@ -462,6 +462,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
'