Skip to content

push: shell-quote names in suggested push commands - #2336

Open
nahidhasan141400 wants to merge 1 commit into
gitgitgadget:masterfrom
nahidhasan141400:fix-push-upstream-quoting
Open

nahidhasan141400 wants to merge 1 commit into
gitgitgadget:masterfrom
nahidhasan141400:fix-push-upstream-quoting

Conversation

@nahidhasan141400

Copy link
Copy Markdown

No description provided.

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Welcome to GitGitGadget

Hi @nahidhasan141400, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that either:

  • Your Pull Request has a good description, if it consists of multiple commits, as it will be used as cover letter.
  • Your Pull Request description is empty, if it consists of a single commit, as the commit message should be descriptive enough by itself.

You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <revi.ewer@example.com>, Ill Takalook <ill.takalook@example.net>

NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description,
because it will result in a malformed CC list on the mailing list. See
example.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join git-mentoring@googlegroups.com, where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

@dscho

dscho commented Nov 26, 2026

Copy link
Copy Markdown
Member

/allow

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

User nahidhasan141400 is now allowed to use GitGitGadget.

WARNING: nahidhasan141400 has no public email address set on GitHub; GitGitGadget needs an email address to Cc: you on your contribution, so that you receive any feedback on the Git mailing list. Go to https://github.com/settings/profile to make your preferred email public to let GitGitGadget know which email address to use.

@nahidhasan141400

Copy link
Copy Markdown
Author

/preview

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Preview email sent as pull.2336.git.1795685836898.gitgitgadget@gmail.com

@nahidhasan141400

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Submitted as pull.2336.git.1795686173178.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2336/nahidhasan141400/fix-push-upstream-quoting-v1

To fetch this version to local tag pr-2336/nahidhasan141400/fix-push-upstream-quoting-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2336/nahidhasan141400/fix-push-upstream-quoting-v1

@gitgitgadget

gitgitgadget Bot commented Nov 26, 2026

Copy link
Copy Markdown

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"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 &&

Branch and remote names in Git may contain shell metacharacters such
as '&', ';', and '$'. When `git push` fails and prints a suggested
command to copy and paste, those names are formatted without shell
quoting. Copying that suggestion into a POSIX shell splits the line;
for example:

    git push --set-upstream origin feat-&-test

backgrounds `git push --set-upstream origin feat-` and tries to run
`-test` as a separate command.

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.

Signed-off-by: MD Nahid Hasan Sagor <nahidhasan141400@gmail.com>
@nahidhasan141400
nahidhasan141400 force-pushed the fix-push-upstream-quoting branch from 5494c80 to dad0b16 Compare November 27, 2026 02:43
@nahidhasan141400

Copy link
Copy Markdown
Author

/preview

@gitgitgadget

gitgitgadget Bot commented Nov 27, 2026

Copy link
Copy Markdown

Preview email sent as pull.2336.v2.git.1795747515825.gitgitgadget@gmail.com

@nahidhasan141400

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 27, 2026

Copy link
Copy Markdown

Submitted as pull.2336.v2.git.1795747717319.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2336/nahidhasan141400/fix-push-upstream-quoting-v2

To fetch this version to local tag pr-2336/nahidhasan141400/fix-push-upstream-quoting-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2336/nahidhasan141400/fix-push-upstream-quoting-v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants