Re: [PATCH] push: shell-quote names in suggested push commands
- From
- Nahid hasan Sagor <nahidhasan141400@gmail.com>
- Date
- Nov 27, 2026, 02:54 UTC
- Message-ID
- <CABQNq+=YsQxGLwFQggx9bY9bWw9vOR5hFs2WaF7UfWoievt17A@mail.gmail.com>
- In-Reply-To
- <xmqq7bhzl8i2.fsf@gitster.g>
Hi Junio,
Thanks for the thorough review. I have just sent v2 addressing all of your points:
- Dropped the trailing clause from the commit. - Quoted the entire "HEAD:<short_upstream>" refspec in die_push_simple(). - Removed the "die() does not return" notes from the comments. - Quoted remote->name in setup_default_push_refspecs() for message_detached_head_die[]. - Simplified the test_when_finished cleanup in t5523-push-upstream.sh and removed the redundant single-branch test.
Thanks, MD Nahid Hasan Sagor
On Fri, Nov 27, 2026 at 1:04 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 134 quoted lines
>
> "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 &&