Re: [PATCH v2] push: shell-quote names in suggested push commands
- From
- Nahid hasan Sagor <nahidhasan141400@gmail.com>
- Date
- Dec 1, 2026, 16:39 UTC
- Message-ID
- <CABQNq+=dG+XHcPpS+CzGhM4B+hp_RGRwwQzmMhW7YLKDRAeHXQ@mail.gmail.com>
- In-Reply-To
- <aw7zZpPPVsZMXb0t@pks.im>
Hi Patrick,
Thank you for taking the time to review the patch.
On Tue, Dec 1, 2026 at 10:18 PM Patrick Steinhardt <ps@pks.im> wrote:
Show 30 quoted lines
> > On Fri, Nov 27, 2026 at 02:48:37AM +0000, Nahid Hasan via GitGitGadget wrote: > > 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. */
Makes sense, I will simplify both comments to that oneliner in v3.
Show 10 quoted lines
> > + 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);
If I'm reading `quote.c` right, both `sq_quote_buf_pretty()` and `sq_quote_buf()` append to `dst` rather than replacing its existing contents, so calling `sq_quote_buf_pretty("ed_refspec, quoted_refspec.buf)` after `strbuf_addf()` would produce `"HEAD:mainHEAD:main"` (or `"HEAD:a b'HEAD:a b'"`). Keeping the separate `refspec` strbuf avoids duplicating the prefix.
Show 19 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).Agreed I went a bit overboard trying to cover shell splitting edge cases there. I will trim `t5529-push-errors.sh` down to a concise test covering a remote and branch name with shell metacharacters in v3.