Re: [PATCH v2] push: shell-quote names in suggested push commands
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Dec 1, 2026, 16:18 UTC
- Message-ID
- <aw7zZpPPVsZMXb0t@pks.im>
- In-Reply-To
- <pull.2336.v2.git.1795747717319.gitgitgadget@gmail.com>
On Fri, Nov 27, 2026 at 02:48:37AM +0000, Nahid Hasan via GitGitGadget wrote:
Show 23 quoted lines
> diff --git a/builtin/push.c b/builtin/push.c > index 2377b5af55..3908f5f532 100644 > --- a/builtin/push.c > +++ b/builtin/push.c > @@ -155,6 +156,9 @@ static NORETURN void die_push_simple(struct branch *branch, > const char *advice_automergesimple_maybe = ""; > const char *short_upstream = branch->merge[0]->src; > struct repo_config_values *cfg = repo_config_values(the_repository); > + struct strbuf quoted_remote = STRBUF_INIT; > + struct strbuf refspec = STRBUF_INIT; > + struct strbuf quoted_refspec = STRBUF_INIT; > > skip_prefix(short_upstream, "refs/heads/", &short_upstream); > > @@ -173,18 +177,26 @@ static NORETURN void die_push_simple(struct branch *branch, > "won't match the local branch, see option " > "'simple' of branch.autoSetupMerge\n" > "in 'git help config'.\n"); > + > + /* > + * Quote for copy-paste into a POSIX shell. Ordinary names stay > + * bare; metacharacters are wrapped in single quotes. > + */
I think it's unnecessary to document how the quoting is performed, that's none of our business here. So this can be a oneliner:
/* Quote for copy-paste into a POSIX shell. */
> + strbuf_addf(&refspec, "HEAD:%s", short_upstream); > + sq_quote_buf_pretty("ed_remote, remote->name); > + sq_quote_buf_pretty("ed_refspec, refspec.buf);
Do we need the extra `refspec` buffer? As far as I can see, `sq_quote_buf_pretty()` calls `sq_quote_buf()`, and that function handles the case where `dst.buf == src`. So:
strbuf_aqqf("ed_refspec, "HEAD:%s", short_upstream);
sq_quote_buf_pretty("ed_refspec, quoted_refspec.buf);Show 14 quoted lines
> diff --git a/t/t5529-push-errors.sh b/t/t5529-push-errors.sh
> index 5f8aa1ed19..57d50d8950 100755
> --- a/t/t5529-push-errors.sh
> +++ b/t/t5529-push-errors.sh
> @@ -94,4 +94,103 @@ test_expect_success 'detect ambiguous refs early' '
> test_cmp expect rp-ran
> '
>
> +# How a POSIX shell splits a copy-paste command line.
> +shell_split () {
> + line=$1
> + eval "set -- $line"
> + printf "%s\n" "$#" "$@"
> +}Aren't all of these tests it t5529 a tiny bit excessive only to verify that we quote the output correctly? It almost feels as if we're now unit-testing `sq_quote_buf()` itself, but if we wanted to do that we should do it via a proper unit test and not via git-push(1).
Patrick