Repository navigation
push: shell-quote names in suggested push commands - #2336
nahidhasan141400 wants to merge 1 commit into
Conversation
Welcome to GitGitGadgetHi @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:
You can CC potential reviewers by adding a footer to the PR description with the following syntax: NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description, 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:
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 patchesBefore 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 Both the person who commented An alternative is the channel Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment If you want to see what email(s) would be sent for a 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 curl -g --user "<EMailAddress>:<Password>" \
--url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txtTo 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): To send a new iteration, just add another PR comment with the contents: 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, |
|
/allow |
|
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. |
|
/preview |
|
Preview email sent as pull.2336.git.1795685836898.gitgitgadget@gmail.com |
|
/submit |
|
Submitted as pull.2336.git.1795686173178.gitgitgadget@gmail.com To fetch this version into To fetch this version to local tag |
|
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("ed_remote, remote->name);
> + sq_quote_buf_pretty("ed_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>
5494c80 to
dad0b16
Compare
|
/preview |
|
Preview email sent as pull.2336.v2.git.1795747515825.gitgitgadget@gmail.com |
|
/submit |
|
Submitted as pull.2336.v2.git.1795747717319.gitgitgadget@gmail.com To fetch this version into To fetch this version to local tag |
No description provided.