Git development
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org
Cc: Harald Nordgren <haraldnordgren@gmail.com>,
	"D. Ben Knoble" <ben.knoble@gmail.com>
Subject: Re: [PATCH] config: make the suggested "set" command safe to paste
Date: Thu, 26 Nov 2026 15:00:20 +0000	[thread overview]
Message-ID: <7e093731-37f3-4ddf-8cb8-adfb9e6c4c73@gmail.com> (raw)
In-Reply-To: <pull.2457.git.git.1795632875345.gitgitgadget@gmail.com>

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(&quoted, arg);
> +	if (strcmp(quoted.buf, arg) && !strchr(arg, '\''))
> +		strbuf_addf(buf, "'%s'", arg);
> +	else
> +		strbuf_addbuf(buf, &quoted);
> +	strbuf_release(&quoted);
> +}
> +
>   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


  parent reply	other threads:[~2026-11-26 15:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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:06     ` Ben Knoble
2026-11-26 18:26     ` Junio C Hamano
2026-11-26 15:00 ` Phillip Wood [this message]
2026-11-28 18:13 ` [PATCH v2] " Harald Nordgren via GitGitGadget
2026-11-29 16:08   ` Phillip Wood

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7e093731-37f3-4ddf-8cb8-adfb9e6c4c73@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=ben.knoble@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=haraldnordgren@gmail.com \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox