* Re: [PATCH] config: make the suggested "set" command safe to paste
2026-11-25 18:54 [PATCH] config: make the suggested "set" command safe to paste Harald Nordgren via GitGitGadget
@ 2026-11-26 12:30 ` Ben Knoble
2026-11-26 14:56 ` Phillip Wood
2026-11-26 15:00 ` Phillip Wood
2026-11-28 18:13 ` [PATCH v2] " Harald Nordgren via GitGitGadget
2 siblings, 1 reply; 8+ messages in thread
From: Ben Knoble @ 2026-11-26 12:30 UTC (permalink / raw)
To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren, nahidhasan141400
+Cc Nasid, who sent a similar « suggested command should
shell quote » patch. (Pasting links on mobile breaks plain-text mode,
apologies for the vague reference.)
> Le 25 nov. 2026 à 13:54, Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com> a écrit :
>
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> When "git config" is given "<key>=<value>" 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 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:
>
> hint: did you mean:
> hint:
> hint: git config set status.comparebranches '@{upstream} @{push}'
I wanted to bring both folks together to figure out: should we shell quote
assuming a POSIX-like shell, and do the current patches
work or not on Windows?
In other words, would we need to do something different to
behave sensibly in other shells (fish?), or is that not a target worth
worrying about (GfW comes with a Bash environment)?
One other thing: it might be nice to centralize some kind of
automatic quoting for suggested commands.
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] config: make the suggested "set" command safe to paste
2026-11-26 12:30 ` Ben Knoble
@ 2026-11-26 14:56 ` Phillip Wood
2026-11-26 15:06 ` Ben Knoble
2026-11-26 18:26 ` Junio C Hamano
0 siblings, 2 replies; 8+ messages in thread
From: Phillip Wood @ 2026-11-26 14:56 UTC (permalink / raw)
To: Ben Knoble, Harald Nordgren via GitGitGadget
Cc: git, Harald Nordgren, nahidhasan141400
On 26/11/2026 12:30, Ben Knoble wrote:
>> Le 25 nov. 2026 à 13:54, Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com> a écrit :
>>
>> From: Harald Nordgren <haraldnordgren@gmail.com>
>>
>> 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:
>>
>> hint: did you mean:
>> hint:
>> hint: git config set status.comparebranches '@{upstream} @{push}'
>
> I wanted to bring both folks together to figure out: should we shell quote
> assuming a POSIX-like shell, and do the current patches
> work or not on Windows?
>
> In other words, would we need to do something different to
> behave sensibly in other shells (fish?), or is that not a target worth
> worrying about (GfW comes with a Bash environment)?
Ideally sq_quote_buf_pretty() would take the current shell into account
so that it generated different output for powershell, cmd.exe, etc.
Currently the output is suitable for POSIX shells and csh (though this
particular patch does some post-processing that removes support for
csh). As a fish user I agree it would be nice to support that as well.
Fish recognizes \\ and \' as escapes within single quotes, so we could
support it in addition to POSIX shells and csh by always backslash
escaping backslashes as we already do for single quotes and exclamation
marks. A prettier option would be to only backslash escape backslashes
that are followed by a backslash or single quote.
> One other thing: it might be nice to centralize some kind of
> automatic quoting for suggested commands.
sq_quote_buf_pretty() is intended for that use case, so we already have
a central location for that logic. The output could be prettier though,
in particular in would be nice to avoid the redundant pairs of single
quotes at the beginning and end of ''\''hello there'\''' so that it
produced \''hello there'\' instead. "'hello there'" would be even
prettier but would take more work.
Thanks
Phillip
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] config: make the suggested "set" command safe to paste
2026-11-26 14:56 ` Phillip Wood
@ 2026-11-26 15:06 ` Ben Knoble
2026-11-26 18:26 ` Junio C Hamano
1 sibling, 0 replies; 8+ messages in thread
From: Ben Knoble @ 2026-11-26 15:06 UTC (permalink / raw)
To: phillip.wood
Cc: Harald Nordgren via GitGitGadget, git, Harald Nordgren,
nahidhasan141400
> Le 26 nov. 2026 à 09:56, Phillip Wood <phillip.wood123@gmail.com> a écrit :
>
> On 26/11/2026 12:30, Ben Knoble wrote:
>>>> Le 25 nov. 2026 à 13:54, Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com> a écrit :
>>>
>>> From: Harald Nordgren <haraldnordgren@gmail.com>
>>>
>>> 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:
>>>
>>> hint: did you mean:
>>> hint:
>>> hint: git config set status.comparebranches '@{upstream} @{push}'
>> I wanted to bring both folks together to figure out: should we shell quote
>> assuming a POSIX-like shell, and do the current patches
>> work or not on Windows?
>>
>> In other words, would we need to do something different to
>> behave sensibly in other shells (fish?), or is that not a target worth
>> worrying about (GfW comes with a Bash environment)?
>
> Ideally sq_quote_buf_pretty() would take the current shell into account so that it generated different output for powershell, cmd.exe, etc. Currently the output is suitable for POSIX shells and csh (though this particular patch does some post-processing that removes support for csh). As a fish user I agree it would be nice to support that as well. Fish recognizes \\ and \' as escapes within single quotes, so we could support it in addition to POSIX shells and csh by always backslash escaping backslashes as we already do for single quotes and exclamation marks. A prettier option would be to only backslash escape backslashes that are followed by a backslash or single quote.
Agreed this does not need to hold up either patch, if the fixes
can be implemented centrally later ! Thanks.
>> One other thing: it might be nice to centralize some kind of
>> automatic quoting for suggested commands.
>
> sq_quote_buf_pretty() is intended for that use case, so we already have a central location for that logic.
But advice-emitters have to remember to call it, right? I was more hoping
for suggested command-specific thing that never forgot to
shell-quote, but maybe that doesn’t work as well without carefully
marking what needs it. So, ok, makes sense :)
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] config: make the suggested "set" command safe to paste
2026-11-26 14:56 ` Phillip Wood
2026-11-26 15:06 ` Ben Knoble
@ 2026-11-26 18:26 ` Junio C Hamano
1 sibling, 0 replies; 8+ messages in thread
From: Junio C Hamano @ 2026-11-26 18:26 UTC (permalink / raw)
To: Phillip Wood
Cc: Ben Knoble, Harald Nordgren via GitGitGadget, git,
Harald Nordgren, nahidhasan141400
Phillip Wood <phillip.wood123@gmail.com> writes:
> sq_quote_buf_pretty() is intended for that use case, so we already have
> a central location for that logic.
FWIW, as the inventor of the sq_quote() family of helper functions, I
did NOT know that we were allowed to futz with what the
sq_quote_buf_pretty() class of helpers produce without having to worry
about machine readability and output stability.
That is even though we prominently document them like this:
These should only be used for human-readable output, as sq_dequote()
is not smart enough to dequote it.
Two things that come to mind are (1) we may need to find a way to make
it even more prominent, and (2) "smart enough" will become irrelevant
once we start aiming to cater to the user's shell, as predictability is
what sq_dequote() is about, and compatibility with different "faviourite
of the year" shells directly contradicts that goal.
> The output could be prettier though, in particular in would be nice to
> avoid the redundant pairs of single quotes at the beginning and end of
> ''\''hello there'\''' so that it produced \''hello there'\'
> instead. "'hello there'" would be even prettier but would take more
> work.
Yes, but I am not sure if going _specific_ to a single chosen shell,
perhaps via user.shellsyntax = (sh|fish|csh|psh|ksh|...), is a good
direction in general. There are folks who script in one and interact in
another shell.
And no, going _generic_ to be usable by _any_ shells we care about is a
direction I do not think we want to go. If backslash becomes active
even inside a pair of single quotes in some but not all shells, then we
must avoid inter-shell ambiguity by not using backslash while inside a
single-quoted environment, by first closing the currently active single
quote, giving a double-backslash (assuming that it is universally
accepted as a single backslash character by everybody), and then going
back to another single-quoted environment, e.g., "'abc'\\'xyz'", which
is the familiar ugliness of our sq_quote() output. That kind of ugly
output will be needed to be usable by everybody, but then even though it
may be cut-and-paste-able, will it still be understandable?
My pessimistic but practical side is tempted to say that it is a lost
cause to aim for cut-and-paste-ready output. Rather, we may be better
off if we give output that shows clear boundaries between each
"parameter" to "commands", and have the user express that in their
chosen shell.
[Footnote]
* 6fb737be5e ([PATCH] Make sq_expand() available as sq_quote().,
2005-07-07) introduced the sq- (stands for single-quote) quote
mechanism as an externally available API function. 1fbdab21bb
(trace: avoid unnecessary quoting, 2018-01-15) added _pretty() as a
"human readable" variant, the first target being the output from
tracing.
The principles laid out there for "pretty" are worth noting:
Trace output doesn't necessarily need to be shell-compatible,
but:
- the shell language is a good ballpark for what humans
consider readable (well, humans versed in command line
tools)
- the run_command bits can be cut-and-pasted to a shell,
and we'll keep that property
- it covers any cases which would make the output
visually ambiguous (e.g., embedded whitespace or quotes)
My reference to "clear boundaries" is about the third point.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] config: make the suggested "set" command safe to paste
2026-11-25 18:54 [PATCH] config: make the suggested "set" command safe to paste Harald Nordgren via GitGitGadget
2026-11-26 12:30 ` Ben Knoble
@ 2026-11-26 15:00 ` Phillip Wood
2026-11-28 18:13 ` [PATCH v2] " Harald Nordgren via GitGitGadget
2 siblings, 0 replies; 8+ messages in thread
From: Phillip Wood @ 2026-11-26 15:00 UTC (permalink / raw)
To: Harald Nordgren via GitGitGadget, git; +Cc: Harald Nordgren, D. Ben Knoble
Hi Harald
On 25/11/2026 18:54, Harald Nordgren via GitGitGadget wrote:
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> When "git config" is given "<key>=<value>" 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 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:
>
> hint: did you mean:
> hint:
> hint: git config set status.comparebranches '@{upstream} @{push}'
This seems very sensible. It would be nice if the tests checked that the
key was quoted when needed as well. I'm also confused why we're
post-processing the output of sq_quote_buf_pretty(). That function is
supposed to be the way to produce shell quoted values for human
consumption, so if it isn't pretty enough we should fix it so that all
it callers benefit from the nicer output, not hack around it.
I agree with Ben about supporting a wider range of shells, but that need
not hold up this patch.
Thanks
Phillip
>
> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
> ---
> config: make the suggested "set" command safe to paste
>
> When git config <key>=<value> is given a value with spaces, the
> suggested command fails when pasted back into a shell. Quote the value
> when it needs 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}'
>
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2457%2FHaraldNordgren%2Fconfig-set-hint-quote-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2457/HaraldNordgren/config-set-hint-quote-v1
> Pull-Request: https://github.com/git/git/pull/2457
>
> builtin/config.c | 21 ++++++++++++++++++--
> t/t1300-config.sh | 49 +++++++++++++++++++++++++++++++++++++++++------
> 2 files changed, 62 insertions(+), 8 deletions(-)
>
> diff --git a/builtin/config.c b/builtin/config.c
> index 2554322317..795a5fb136 100644
> --- a/builtin/config.c
> +++ b/builtin/config.c
> @@ -211,6 +211,18 @@ 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, '.');
> @@ -218,9 +230,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;
> +
> + add_suggested_arg(&suggestion, prefix);
> + strbuf_addch(&suggestion, ' ');
> + add_suggested_arg(&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 e3f8064889..796e11000c 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 keeps shell alias readable' '
> + cat >expect <<-EOF &&
> + error: missing value to set to the variable ${SQ}alias.st=!git status${SQ}
> + hint: did you mean:
> + hint:
> + hint: git config set alias.st ${SQ}!git status${SQ}
> + EOF
> + test_must_fail git config alias.st="!git status" 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: c51408889c3b372814bc20b9431c3b74d0d268eb
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v2] config: make the suggested "set" command safe to paste
2026-11-25 18:54 [PATCH] config: make the suggested "set" command safe to paste Harald Nordgren via GitGitGadget
2026-11-26 12:30 ` Ben Knoble
2026-11-26 15:00 ` Phillip Wood
@ 2026-11-28 18:13 ` Harald Nordgren via GitGitGadget
2026-11-29 16:08 ` Phillip Wood
2 siblings, 1 reply; 8+ messages in thread
From: Harald Nordgren via GitGitGadget @ 2026-11-28 18:13 UTC (permalink / raw)
To: git; +Cc: Ben Knoble, Phillip Wood, Harald Nordgren, Harald Nordgren
From: Harald Nordgren <haraldnordgren@gmail.com>
When "git config" is given "<key>=<value>" 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 <haraldnordgren@gmail.com>
---
config: make the suggested "set" command safe to paste
When git config <key>=<value> 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 <haraldnordgren@gmail.com>
## 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
--
gitgitgadget
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v2] config: make the suggested "set" command safe to paste
2026-11-28 18:13 ` [PATCH v2] " Harald Nordgren via GitGitGadget
@ 2026-11-29 16:08 ` Phillip Wood
0 siblings, 0 replies; 8+ messages in thread
From: Phillip Wood @ 2026-11-29 16:08 UTC (permalink / raw)
To: Harald Nordgren via GitGitGadget, git; +Cc: Ben Knoble, Harald Nordgren
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 <haraldnordgren@gmail.com>
>
> When "git config" is given "<key>=<value>" 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 <haraldnordgren@gmail.com>
> ---
> config: make the suggested "set" command safe to paste
>
> When git config <key>=<value> 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 <haraldnordgren@gmail.com>
>
> ## 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
^ permalink raw reply [flat|nested] 8+ messages in thread