From: Phillip Wood Date: Sun, 29 Nov 2026 16:08:17 GMT Subject: Re: [PATCH v2] config: make the suggested "set" command safe to paste Message-ID: <65a734d2-51ec-42fe-9f2d-9026b58838a5@gmail.com> In-Reply-To: Hi Harald This looks fine to me. We could quite happily test the quoting of the config key and config value in a single test but it is not worth re-rolling just for that. Thanks Phillip On 28/11/2026 18:13, Harald Nordgren via GitGitGadget wrote: > From: Harald Nordgren > > When "git config" is given "=" as a single argument and > the value contains spaces, the suggested command does not work when > pasted back into a shell: > > $ git config status.comparebranches="@{upstream} @{push}" > error: missing value to set to the variable 'status.comparebranches=@{upstream} @{push}' > hint: did you mean "git config set status.comparebranches @{upstream} @{push}"? > > The shell splits the value into two arguments and the command fails > with "wrong number of arguments". > > Quote the key and the value when they need it, and put the command > on a line of its own the way other hints show commands, so it can be > copied without the surrounding quotes: > > hint: did you mean: > hint: > hint: git config set status.comparebranches '@{upstream} @{push}' > > Signed-off-by: Harald Nordgren > --- > config: make the suggested "set" command safe to paste > > When git config = is given a value with spaces, the > suggested command fails when pasted back into a shell. Quote the key and > the value when they need it and put the command on its own line, the way > other hints show commands: > > $ git config status.comparebranches="@{upstream} @{push}" > error: missing value to set to the variable 'status.comparebranches=@{upstream} @{push}' > hint: did you mean: > hint: > hint: git config set status.comparebranches '@{upstream} @{push}' > > > Changes in v2: > > * Quote the suggested key and value with sq_quote_buf_pretty() > directly, without post-processing its output. A value like !git > status is now shown as ''\!'git status'. > * Add a test that a key with spaces is quoted. > * Drop the test for the ! alias value. > > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2457%2FHaraldNordgren%2Fconfig-set-hint-quote-v2 > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2457/HaraldNordgren/config-set-hint-quote-v2 > Pull-Request: https://github.com/git/git/pull/2457 > > Range-diff vs v1: > > 1: 2aeb767b21 ! 1: e3f2747c22 config: make the suggested "set" command safe to paste > @@ Commit message > The shell splits the value into two arguments and the command fails > with "wrong number of arguments". > > - Quote the value when it needs it, and put the command on a line of > - its own the way other hints show commands, so it can be copied > - without the surrounding quotes: > + Quote the key and the value when they need it, and put the command > + on a line of its own the way other hints show commands, so it can be > + copied without the surrounding quotes: > > hint: did you mean: > hint: > @@ Commit message > Signed-off-by: Harald Nordgren > > ## builtin/config.c ## > -@@ builtin/config.c: static void check_argc(int argc, int min, int max) > - exit(129); > - } > - > -+static void add_suggested_arg(struct strbuf *buf, const char *arg) > -+{ > -+ struct strbuf quoted = STRBUF_INIT; > -+ > -+ sq_quote_buf_pretty("ed, arg); > -+ if (strcmp(quoted.buf, arg) && !strchr(arg, '\'')) > -+ strbuf_addf(buf, "'%s'", arg); > -+ else > -+ strbuf_addbuf(buf, "ed); > -+ strbuf_release("ed); > -+} > -+ > - static NORETURN void die_missing_set_value(const char *arg) > - { > - const char *last_dot = strrchr(arg, '.'); > @@ builtin/config.c: static NORETURN void die_missing_set_value(const char *arg) > char *prefix = eq ? xstrndup(arg, eq - arg) : NULL; > > if (prefix && git_config_key_is_valid(prefix)) { > + struct strbuf suggestion = STRBUF_INIT; > + > -+ add_suggested_arg(&suggestion, prefix); > ++ sq_quote_buf_pretty(&suggestion, prefix); > + strbuf_addch(&suggestion, ' '); > -+ add_suggested_arg(&suggestion, eq + 1); > ++ sq_quote_buf_pretty(&suggestion, eq + 1); > error(_("missing value to set to the variable '%s'"), arg); > - advise(_("did you mean \"git config set %s %s\"?"), > - prefix, eq + 1); > @@ t/t1300-config.sh: test_expect_success 'invalid key' ' > + test_cmp expect err > +' > + > -+test_expect_success 'set with 1 arg of "key=value": suggestion keeps shell alias readable' ' > ++test_expect_success 'set with 1 arg of "key=value": suggestion quotes key with spaces' ' > + cat >expect <<-EOF && > -+ error: missing value to set to the variable ${SQ}alias.st=!git status${SQ} > ++ error: missing value to set to the variable ${SQ}branch.my topic.remote=origin${SQ} > + hint: did you mean: > + hint: > -+ hint: git config set alias.st ${SQ}!git status${SQ} > ++ hint: git config set ${SQ}branch.my topic.remote${SQ} origin > + EOF > -+ test_must_fail git config alias.st="!git status" 2>err && > ++ test_must_fail git config "branch.my topic.remote=origin" 2>err && > + test_cmp expect err > ' > > > > builtin/config.c | 9 +++++++-- > t/t1300-config.sh | 49 +++++++++++++++++++++++++++++++++++++++++------ > 2 files changed, 50 insertions(+), 8 deletions(-) > > diff --git a/builtin/config.c b/builtin/config.c > index f786c0b123..736df8ba0f 100644 > --- a/builtin/config.c > +++ b/builtin/config.c > @@ -218,9 +218,14 @@ static NORETURN void die_missing_set_value(const char *arg) > char *prefix = eq ? xstrndup(arg, eq - arg) : NULL; > > if (prefix && git_config_key_is_valid(prefix)) { > + struct strbuf suggestion = STRBUF_INIT; > + > + sq_quote_buf_pretty(&suggestion, prefix); > + strbuf_addch(&suggestion, ' '); > + sq_quote_buf_pretty(&suggestion, eq + 1); > error(_("missing value to set to the variable '%s'"), arg); > - advise(_("did you mean \"git config set %s %s\"?"), > - prefix, eq + 1); > + advise(_("did you mean:\n\n\tgit config set %s"), suggestion.buf); > + strbuf_release(&suggestion); > } else if (git_config_key_is_valid(arg)) { > error(_("missing value to set to the variable '%s'"), arg); > } else { > diff --git a/t/t1300-config.sh b/t/t1300-config.sh > index 16bb396b63..e7b5bde7af 100755 > --- a/t/t1300-config.sh > +++ b/t/t1300-config.sh > @@ -470,15 +470,47 @@ test_expect_success 'invalid key' ' > ' > > test_expect_success 'set with 1 arg of "key=value": valid key suggests split form' ' > + cat >expect <<-EOF && > + error: missing value to set to the variable ${SQ}pull.rebase=false${SQ} > + hint: did you mean: > + hint: > + hint: git config set pull.rebase false > + EOF > 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_cmp expect err > ' > > test_expect_success 'set with 1 arg of "key=value": implicit form suggests split form' ' > + cat >expect <<-EOF && > + error: missing value to set to the variable ${SQ}pull.rebase=false${SQ} > + hint: did you mean: > + hint: > + hint: git config set pull.rebase false > + EOF > 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_cmp expect err > +' > + > +test_expect_success 'set with 1 arg of "key=value": suggestion quotes value with spaces' ' > + cat >expect <<-EOF && > + error: missing value to set to the variable ${SQ}status.comparebranches=@{upstream} @{push}${SQ} > + hint: did you mean: > + hint: > + hint: git config set status.comparebranches ${SQ}@{upstream} @{push}${SQ} > + EOF > + test_must_fail git config status.comparebranches="@{upstream} @{push}" 2>err && > + test_cmp expect err > +' > + > +test_expect_success 'set with 1 arg of "key=value": suggestion quotes key with spaces' ' > + cat >expect <<-EOF && > + error: missing value to set to the variable ${SQ}branch.my topic.remote=origin${SQ} > + hint: did you mean: > + hint: > + hint: git config set ${SQ}branch.my topic.remote${SQ} origin > + EOF > + test_must_fail git config "branch.my topic.remote=origin" 2>err && > + test_cmp expect err > ' > > test_expect_success 'set with 1 arg of "key=value": invalid key does not suggest split form' ' > @@ -494,9 +526,14 @@ test_expect_success 'set with 1 arg: variable name starting with digit is invali > ' > > test_expect_success 'set with 1 arg: digit-led section name is valid' ' > + cat >expect <<-EOF && > + error: missing value to set to the variable ${SQ}1foo.bar=baz${SQ} > + hint: did you mean: > + hint: > + hint: git config set 1foo.bar baz > + EOF > 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_cmp expect err > ' > > test_expect_success 'set with 1 arg: subsection plus invalid variable name' ' > > base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe