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] push: shell-quote names in suggested push commands
Date: Thu, 26 Nov 2026 11:04:21 -0800	[thread overview]
Message-ID: <xmqq7bhzl8i2.fsf@gitster.g> (raw)
In-Reply-To: <pull.2336.git.1795686173178.gitgitgadget@gmail.com> (Nahid Hasan via GitGitGadget's message of "Thu, 26 Nov 2026 09:42:53 +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, and add tests for each hint.

Drop ", and add tests for each hint" from the proposed log message, as
tests are expected with a fix like this.  Reviewers will notice and
nudge you to add the tests if you don't anyway ;-)

Quoting suggested commands for copy-paste into a POSIX shell is a nice
improvement, and sq_quote_buf_pretty() is the right helper for it so
ordinary names stay uncluttered.

> diff --git a/builtin/push.c b/builtin/push.c
> index 2377b5af55..a28138fcba 100644
> --- a/builtin/push.c
> +++ b/builtin/push.c
> ...
> +	/*
> +	 * Quote for copy-paste into a POSIX shell. Ordinary names stay
> +	 * bare; metacharacters are wrapped in single quotes. die() does
> +	 * not return, so these buffers are intentionally not released.
> +	 */
> +	sq_quote_buf_pretty(&quoted_remote, remote->name);
> +	sq_quote_buf_pretty(&quoted_upstream, short_upstream);

In die_push_simple(), the first suggested command formats the refspec as
"HEAD:%s", and you are feeding short_upstream to the '%s'.

When short_upstream contains metacharacters, sq_quote_buf_pretty() wraps
it in single quotes, yielding e.g. [1], but it is just as ugly as [2].
Wouldn't you rather want to see [3]?

        [1]	HEAD:'upstream&name'
	[2]	HEAD:upstream\&name
	[3]	'HEAD:upstream&name'

All of them would be parsed by POSIX shells as a single argument, so it
is not wrong per se, but since you are not using the basic sq_quote()
and instead chose to use the _pretty variant for prettiness, you may
care.

> @@ -211,14 +225,21 @@ static const char *get_upstream_ref(int flags, struct branch *branch, const char
>  					   "upstream, see 'push.autoSetupRemote' "
>  					   "in 'git help config'.\n");
>  		}
> +		/*
> +		 * The prose keeps the raw branch name. Only the
> +		 * copy-pasteable command is shell-quoted. die() does
> +		 * not return, so these buffers are not released.
> +		 */

Good reasoning.  Common to the previous hunk, I do not think "die() does
not return, ..." should be here.  We are not writing a programming
tutorial for Git developers here.

Move the suggestion ("you can leave resources used to call die()
unreleased, and that is not a leak.") somewhere in the Documentation/
hierarchy, perhaps in MyFirstContribution?

There is one more suggested "git push" command in builtin/push.c that
this patch missed.  The message_detached_head_die[] template is used in
setup_default_push_refspecs(), which reads like this:

    static const char message_detached_head_die[] =
            N_("You are not currently on a branch.\n"
               ...
               "    git push %s HEAD:<name-of-remote-branch>\n");
            ...
            branch = branch_get(NULL);
            if (!branch)
                    die(_(message_detached_head_die), remote->name);

If a user is on a detached HEAD and pushes to a "weird&remote",
remote->name needs quoting.  The user of course needs to be careful to
replace <name-of-remote-branch> with an appropriate name themselves, but
it is quite plausible that they will cut and paste up to the colon that
appears after "HEAD" and then type the name themselves.

> diff --git a/t/t5523-push-upstream.sh b/t/t5523-push-upstream.sh
> index 22d3e1162c..484565d7cb 100755
> --- a/t/t5523-push-upstream.sh
> +++ b/t/t5523-push-upstream.sh
> @@ -134,4 +134,12 @@ test_expect_success TTY 'quiet push -u' '
>  	test_must_be_empty output
>  '
>  
> +test_expect_success 'no-upstream advice quotes a branch name with metacharacters' '
> +	test_when_finished "git checkout main" &&
> +	git checkout -b "branch&name" &&

Two when_finished handlers running "checkout main" (above and below
here) look somewhat strange.  You of course want the handler defined
before running this checkout, and ...

> +	test_when_finished "git checkout main && git branch -D \"branch&name\"" &&
> +	test_must_fail git push upstream 2>stderr &&
> +	test_grep "git push --set-upstream upstream ${SQ}branch&name${SQ}" stderr
> +'

... you want to get rid of that funnily named branch before you complete
this test, and in order to get rid of the funnily named branch, you have
to be on a branch other than that, so it is understandable that it gets
tempting to write "git checkout main &&" in front, but what happens if
that checkout fails for some reason?  You'll leave the funnily named
branch behind.

> diff --git a/t/t5529-push-errors.sh b/t/t5529-push-errors.sh
> index 5f8aa1ed19..8f4705cae6 100755
> --- a/t/t5529-push-errors.sh
> +++ b/t/t5529-push-errors.sh
> @@ -67,6 +67,11 @@ test_expect_success 'suggest <remote> <branch> when the branch has slashes' '
>  	test_grep "hint: Did you mean to use: git push origin feature/x?" stderr
>  '
>  
> +test_expect_success 'suggest quoted branch when the slipped name needs shell quoting' '
> +	test_must_fail git push "origin/branch&name" 2>stderr &&
> +	test_grep "hint: Did you mean to use: git push origin ${SQ}branch&name${SQ}?" stderr
> +'
> +

Isn't this fully covered later in the same file by the new 'slash-slip
advice quotes remote and ref metacharacters' test, which checks both
remote and ref quoting along with shell_split()?  Perhaps drop this one?

> +test_expect_success 'slash-slip advice quotes remote and ref metacharacters' '
> +	git remote add "weird&remote" remote.git &&
> +	test_when_finished "git remote remove \"weird&remote\"" &&
> + ...
> +	test_must_fail git push "origin/feat;id" 2>stderr &&
> +	test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat;id${SQ}?" stderr &&

  reply	other threads:[~2026-11-26 19:04 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 [this message]
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
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=xmqq7bhzl8i2.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