Thread View: linux.kernel.git
9 messages
9 total messages
Started by "Harald Nordgren via GitGitGadge...
Wed, 25 Nov 2026 18:54
[PATCH] config: make the suggested "set" command safe to paste
Author: "Harald Nordgren via GitGitGadge...
Date: Wed, 25 Nov 2026 18:54
Date: Wed, 25 Nov 2026 18:54
163 lines
16608 bytes
16608 bytes
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}'
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
--
gitgitgadget
Re: [PATCH] config: make the suggested "set" command safe to paste
Author: Ben Knoble
Date: Thu, 26 Nov 2026 12:30
Date: Thu, 26 Nov 2026 12:30
41 lines
12396 bytes
12396 bytes
+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.
Re: [PATCH] config: make the suggested "set" command safe to paste
Author: Phillip Wood
Date: Thu, 26 Nov 2026 14:56
Date: Thu, 26 Nov 2026 14:56
46 lines
12725 bytes
12725 bytes
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
Re: [PATCH] config: make the suggested "set" command safe to paste
Author: Phillip Wood
Date: Thu, 26 Nov 2026 15:00
Date: Thu, 26 Nov 2026 15:00
179 lines
18322 bytes
18322 bytes
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
Re: [PATCH] config: make the suggested "set" command safe to paste
Author: Ben Knoble
Date: Thu, 26 Nov 2026 15:06
Date: Thu, 26 Nov 2026 15:06
50 lines
13312 bytes
13312 bytes
> 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 :)
Re: [PATCH] config: make the suggested "set" command safe to paste
Author: Junio C Hamano
Date: Thu, 26 Nov 2026 18:26
Date: Thu, 26 Nov 2026 18:26
77 lines
16199 bytes
16199 bytes
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.
Re: [PATCH] config: make the suggested "set" command safe to paste
Author: Phillip Wood
Date: Fri, 27 Nov 2026 16:55
Date: Fri, 27 Nov 2026 16:55
113 lines
16697 bytes
16697 bytes
On 26/11/2026 18:26, Junio C Hamano wrote: > 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. I hadn't realized sq_quote_buf_pretty() was used in the tracing output. While we might get away with tweaking the quoting in the tracing output slightly, I don't think we want it to depend on the user's choice of shell. So if we decide we want to support more shells, we should probably introduce a new function for that. >> 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. The paragraph quoted above was taking about improving the readability of the quoted output by removing redundant quotes, not tailoring the output to a specific shell. I'm one of the people who uses different interactive and scripting shells. Personally I'd be content with git always quoting in the style of my interactive shell. If the git makes a suggestion based on a bad input that I passed to my script then I'd want to quoted for my interactive shell, not the shell that the script is written in. If I'm debugging a script, I can always tweak the config setting if I need to. > 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? I agree it is ugly and we're already doing it for '!' to accommodate csh. To support fish we would not need to unconditionally backslash escape backslashes -- our current quoting is almost always correct for fish -- we'd only need to do it when a backslash is followed by another backslash or a single quote. For the latter we're already creating ugly "'\''" output so I don't think that accommodating fish would change our current output most of the time. Having said that it is not possible to support shells whose quoting rules wildly differ from POSIX simply by tweaking the current output. If we want to support something like powershell we'll need a way of configuring the output. > 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. That is certainly simpler and if we abandon any pretense at shell compatibility we can avoid the ugly "'\''" by enclosing each argument in "'" and backslash escaping "'" and "\" within each quoted argument. I do think it is convenient to be able to cut-and-paste the output though. Thanks for the footnote below, that was interesting reading. Phillip > [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.
[PATCH v2] config: make the suggested "set" command safe to paste
Author: "Harald Nordgren via GitGitGadge...
Date: Sat, 28 Nov 2026 18:13
Date: Sat, 28 Nov 2026 18:13
226 lines
19985 bytes
19985 bytes
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
Re: [PATCH v2] config: make the suggested "set" command safe to paste
Author: Phillip Wood
Date: Sun, 29 Nov 2026 16:08
Date: Sun, 29 Nov 2026 16:08
235 lines
20886 bytes
20886 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads