git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] push: shell-quote names in suggested push commands

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 27, 2026, 05:53 UTC
Message-ID
<xmqq7bhyes5z.fsf@gitster.g>
In-Reply-To
<pull.2336.v2.git.1795747717319.gitgitgadget@gmail.com>
"Nahid Hasan via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> 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.

Show 27 quoted lines
>  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'.

Show 12 quoted lines
> @@ -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.

Show 7 quoted lines
> +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.
Previous: Nahid hasan SagorNext: Nahid hasan Sagor
Message 8 of 11 in “push: shell-quote names in suggested push commands”
  1. push: shell-quote names in suggested push commandsNahid Hasan via GitGitGadget, Nov 26, 2026
  2. Junio C HamanoNov 26, 2026
  3. Nahid hasan SagorNov 27, 2026
  4. push: shell-quote names in suggested push commandsNahid Hasan via GitGitGadget, Nov 27, 2026
  5. Nahid hasan SagorNov 27, 2026
  6. Junio C HamanoNov 27, 2026
  7. Nahid hasan SagorNov 27, 2026
  8. Junio C HamanoNov 27, 2026
  9. Nahid hasan SagorNov 27, 2026
  10. Patrick SteinhardtDec 1, 2026
  11. Nahid hasan SagorDec 1, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.