Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Nahid Hasan via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  Nahid Hasan <nahidhasan141400@gmail.com>
Subject: Re: [PATCH v2] push: shell-quote names in suggested push commands
Date: Thu, 26 Nov 2026 21:53:44 -0800	[thread overview]
Message-ID: <xmqq7bhyes5z.fsf@gitster.g> (raw)
In-Reply-To: <pull.2336.v2.git.1795747717319.gitgitgadget@gmail.com> (Nahid Hasan via GitGitGadget's message of "Fri, 27 Nov 2026 02:48:37 +0000")

"Nahid Hasan via GitGitGadget" <gitgitgadget@gmail.com> writes:

> Format the remote, branch, and ref names in the suggested `git push`
> commands in `builtin/push.c` with `sq_quote_buf_pretty()` so names
> that need quoting are wrapped in single quotes while plain names stay
> bare. A HEAD:<dst> refspec is quoted as one shell word, and the
> detached-HEAD hint in setup_default_push_refspecs() quotes the remote
> name as well.

This iteration cleanly addresses the points raised on v1.  A tiny
formatting nit in the proposed log message is the mix of backticked
tokens like `sq_quote_buf_pretty()` alongside bare ones like HEAD:<dst>
and setup_default_push_refspecs().  We prefer a basic single quotes, as
nobody passes the commit log messages through AsciiDoc or Markdown
processor.  These are plain text.

>  builtin/push.c           | 48 +++++++++++++++----
>  t/t5523-push-upstream.sh |  8 ++++
>  t/t5528-push-default.sh  | 17 +++++++
>  t/t5529-push-errors.sh   | 99 ++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 164 insertions(+), 8 deletions(-)
>
> diff --git a/builtin/push.c b/builtin/push.c
> index 2377b5af55..3908f5f532 100644
> ...
> @@ -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.
> +	 */
> +	strbuf_addf(&refspec, "HEAD:%s", short_upstream);
> +	sq_quote_buf_pretty(&quoted_remote, remote->name);
> +	sq_quote_buf_pretty(&quoted_refspec, refspec.buf);
>  	die(_("The upstream branch of your current branch does not match\n"
>  	      "the name of your current branch.  To push to the upstream branch\n"
>  	      "on the remote, use\n"
>  	      "\n"
> -	      "    git push %s HEAD:%s\n"
> +	      "    git push %s %s\n"

Before this change, die_push_simple() interpolated short_upstream
directly into "HEAD:%s", so metacharacters in either the remote name or
the upstream branch name broke copy-pasted commands.  Quoting the whole
"HEAD:%s" string with sq_quote_buf_pretty() keeps plain refspecs like
HEAD:other-name bare (since ':' is in ok_punct[] inside quote.c) while
turning metacharacter cases into a single shell word such as
'HEAD:upstream&name'.

> @@ -249,8 +270,12 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
>  	}
>  
>  	branch = branch_get(NULL);
> -	if (!branch)
> -		die(_(message_detached_head_die), remote->name);
> +	if (!branch) {
> +		struct strbuf quoted_remote = STRBUF_INIT;
> +
> +		sq_quote_buf_pretty(&quoted_remote, remote->name);
> +		die(_(message_detached_head_die), quoted_remote.buf);
> +	}

Covering the detached HEAD advice alongside get_upstream_ref() and
die_if_repo_looks_like_ref() catches all four places in builtin/push.c
where a suggested "git push" command formats a remote, branch, or ref
name.

> +test_expect_success 'no-upstream advice quotes a branch name with metacharacters' '
> +	test_when_finished "git branch -D \"branch&name\"" &&
> +	test_when_finished "git checkout main" &&
> +	git checkout -b "branch&name" &&
> +	test_must_fail git push upstream 2>stderr &&
> +	test_grep "git push --set-upstream upstream ${SQ}branch&name${SQ}" stderr
> +'

test_when_finished handlers stack and are run in LIFO order, so we try
going back to 'main' first and then remove the funnily named branch,
which makes sense.

The new tests in t5523, t5528, and t5529 exercise both ordinary and
metacharacter names across all four paths and check POSIX word
splitting with shell_split().

Looking good.

  parent reply	other threads:[~2026-11-27  5:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  9:42 [PATCH] push: shell-quote names in suggested push commands Nahid Hasan via GitGitGadget
2026-11-26 19:04 ` Junio C Hamano
2026-11-27  2:54   ` Nahid hasan Sagor
2026-11-27  2:48 ` [PATCH v2] " Nahid Hasan via GitGitGadget
2026-11-27  2:52   ` Nahid hasan Sagor
2026-11-27  5:19     ` Junio C Hamano
2026-11-27  5:26       ` Nahid hasan Sagor
2026-11-27  5:53   ` Junio C Hamano [this message]
2026-11-27  6:06     ` Nahid hasan Sagor

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=xmqq7bhyes5z.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=nahidhasan141400@gmail.com \
    /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