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>

Harald Nordgren committed Jun 2, 2026 at 18:43 UTC 03c29e2e980da7595cbade29e02616d2de2c42f8
2 files changed +87 -1
builtin/config.c
+31 -1
@@ -1,6 +1,7 @@
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "abspath.h"
4 +#include "advice.h"
5 #include "config.h"
6 #include "color.h"
7 #include "date.h"
@@ -210,6 +211,26 @@ static void check_argc(int argc, int min, int max)
211 exit(129);
212 }
213
214 +static NORETURN void die_missing_set_value(const char *arg)
215 +{
216 + const char *last_dot = strrchr(arg, '.');
217 + const char *eq = last_dot ? strchr(last_dot + 1, '=') : NULL;
218 + char *prefix = eq ? xstrndup(arg, eq - arg) : NULL;
219 +
220 + if (prefix && git_config_key_is_valid(prefix)) {
221 + error(_("missing value to set to the variable '%s'"), arg);
222 + advise(_("did you mean \"git config set %s %s\"?"),
223 + prefix, eq + 1);
224 + } else if (git_config_key_is_valid(arg)) {
225 + error(_("missing value to set to the variable '%s'"), arg);
226 + } else {
227 + error(_("missing value to set to a variable with an invalid name '%s'"),
228 + arg);
229 + }
230 + free(prefix);
231 + exit(129);
232 +}
233 +
234 static void show_config_origin(const struct config_display_options *opts,
235 const struct key_value_info *kvi,
236 struct strbuf *buf)
@@ -1133,6 +1154,8 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix,
1154
1155 argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage,
1156 PARSE_OPT_STOP_AT_NON_OPTION);
1157 + if (argc == 1)
1158 + die_missing_set_value(argv[0]);
1159 check_argc(argc, 2, 2);
1160
1161 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)
1394 };
1395 char *value = NULL, *comment = NULL;
1396 int ret = 0;
1397 + int actions_implicit;
1398 struct key_value_info default_kvi = KVI_INIT;
1399
1400 argc = parse_options(argc, argv, prefix, opts,
@@ -1385,7 +1409,8 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1409 exit(129);
1410 }
1411
1388 - if (actions == 0)
1412 + actions_implicit = (actions == 0);
1413 + if (actions_implicit)
1414 switch (argc) {
1415 case 1: actions = ACTION_GET; break;
1416 case 2: actions = ACTION_SET; break;
@@ -1394,6 +1419,11 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1419 error(_("no action specified"));
1420 exit(129);
1421 }
1422 + if (actions_implicit && argc == 1) {
1423 + const char *last_dot = strrchr(argv[0], '.');
1424 + if (last_dot && strchr(last_dot + 1, '='))
1425 + die_missing_set_value(argv[0]);
1426 + }
1427 if (display_opts.omit_values &&
1428 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
1429 error(_("--name-only is only applicable to --list or --get-regexp"));
t/t1300-config.sh
+56
@@ -462,6 +462,62 @@ test_expect_success 'invalid key' '
462 test_must_fail git config inval.2key blabla
463 '
464
465 +test_expect_success 'set with 1 arg of "key=value": valid key suggests split form' '
466 + test_must_fail git config set pull.rebase=false 2>err &&
467 + test_grep "missing value to set to the variable .pull\\.rebase=false." err &&
468 + test_grep "did you mean .git config set pull\\.rebase false." err
469 +'
470 +
471 +test_expect_success 'set with 1 arg of "key=value": implicit form suggests split form' '
472 + test_must_fail git config pull.rebase=false 2>err &&
473 + test_grep "missing value to set to the variable .pull\\.rebase=false." err &&
474 + test_grep "did you mean .git config set pull\\.rebase false." err
475 +'
476 +
477 +test_expect_success 'set with 1 arg of "key=value": invalid key does not suggest split form' '
478 + test_must_fail git config set foo=bar 2>err &&
479 + test_grep "missing value to set to a variable with an invalid name .foo=bar." err &&
480 + test_grep ! "did you mean" err
481 +'
482 +
483 +test_expect_success 'set with 1 arg: variable name starting with digit is invalid' '
484 + test_must_fail git config set foo.1bar=baz 2>err &&
485 + test_grep "missing value to set to a variable with an invalid name .foo\\.1bar=baz." err &&
486 + test_grep ! "did you mean" err
487 +'
488 +
489 +test_expect_success 'set with 1 arg: digit-led section name is valid' '
490 + test_must_fail git config set 1foo.bar=baz 2>err &&
491 + test_grep "missing value to set to the variable .1foo\\.bar=baz." err &&
492 + test_grep "did you mean .git config set 1foo\\.bar baz." err
493 +'
494 +
495 +test_expect_success 'set with 1 arg: subsection plus invalid variable name' '
496 + test_must_fail git config set foo.some.b_r=baz 2>err &&
497 + test_grep "missing value to set to a variable with an invalid name .foo\\.some\\.b_r=baz." err &&
498 + test_grep ! "did you mean" err
499 +'
500 +
501 +test_expect_success 'set with 1 arg of valid key reports missing value' '
502 + test_must_fail git config set pull.rebase 2>err &&
503 + test_grep "missing value to set to the variable .pull\\.rebase." err &&
504 + test_grep ! "did you mean" err
505 +'
506 +
507 +test_expect_success 'set with 2 args including "=" in invalid key does not suggest' '
508 + test_must_fail git config set pull.rebase=false true 2>err &&
509 + test_grep "invalid key: pull\\.rebase=false" err &&
510 + test_grep ! "did you mean" err
511 +'
512 +
513 +test_expect_success '"=" inside subsection is valid' '
514 + test_when_finished "rm -f subsection.cfg" &&
515 + git config set -f subsection.cfg foo.bar=baz.boo qux &&
516 + echo qux >expect &&
517 + git config get -f subsection.cfg foo.bar=baz.boo >actual &&
518 + test_cmp expect actual
519 +'
520 +
521 test_expect_success 'correct key' '
522 git config 123456.a123 987
523 '