* [PATCH] push: shell-quote names in suggested push commands
@ 2026-11-26 9:42 Nahid Hasan via GitGitGadget
2026-11-26 19:04 ` Junio C Hamano
2026-11-27 2:48 ` [PATCH v2] " Nahid Hasan via GitGitGadget
0 siblings, 2 replies; 9+ messages in thread
From: Nahid Hasan via GitGitGadget @ 2026-11-26 9:42 UTC (permalink / raw)
To: git; +Cc: Nahid Hasan, MD Nahid Hasan Sagor
From: MD Nahid Hasan Sagor <nahidhasan141400@gmail.com>
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, and add tests for each hint.
Signed-off-by: MD Nahid Hasan Sagor <nahidhasan141400@gmail.com>
---
push: shell-quote names in suggested push commands
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2336%2Fnahidhasan141400%2Ffix-push-upstream-quoting-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2336/nahidhasan141400/fix-push-upstream-quoting-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2336
builtin/push.c | 38 ++++++++++++--
t/t5523-push-upstream.sh | 8 +++
t/t5528-push-default.sh | 8 +++
t/t5529-push-errors.sh | 104 +++++++++++++++++++++++++++++++++++++++
4 files changed, 153 insertions(+), 5 deletions(-)
diff --git a/builtin/push.c b/builtin/push.c
index 2377b5af55..a28138fcba 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -18,6 +18,7 @@
#include "transport.h"
#include "parse-options.h"
#include "pkt-line.h"
+#include "quote.h"
#include "submodule.h"
#include "submodule-config.h"
#include "send-pack.h"
@@ -155,6 +156,8 @@ static NORETURN void die_push_simple(struct branch *branch,
const char *advice_automergesimple_maybe = "";
const char *short_upstream = branch->merge[0]->src;
struct repo_config_values *cfg = repo_config_values(the_repository);
+ struct strbuf quoted_remote = STRBUF_INIT;
+ struct strbuf quoted_upstream = STRBUF_INIT;
skip_prefix(short_upstream, "refs/heads/", &short_upstream);
@@ -173,6 +176,14 @@ 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. 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);
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"
@@ -183,8 +194,8 @@ static NORETURN void die_push_simple(struct branch *branch,
"\n"
" git push %s HEAD\n"
"%s%s"),
- remote->name, short_upstream,
- remote->name, advice_pushdefault_maybe,
+ quoted_remote.buf, quoted_upstream.buf,
+ quoted_remote.buf, advice_pushdefault_maybe,
advice_automergesimple_maybe);
}
@@ -204,6 +215,9 @@ static const char *get_upstream_ref(int flags, struct branch *branch, const char
if (!branch->merge_nr || !branch->merge || !branch->remote_name) {
const char *advice_autosetup_maybe = "";
+ struct strbuf quoted_remote = STRBUF_INIT;
+ struct strbuf quoted_branch = STRBUF_INIT;
+
if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
advice_autosetup_maybe = _("\n"
"To have this happen automatically for "
@@ -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.
+ */
+ sq_quote_buf_pretty("ed_remote, remote_name);
+ sq_quote_buf_pretty("ed_branch, branch->name);
die(_("The current branch %s has no upstream branch.\n"
"To push the current branch and set the remote as upstream, use\n"
"\n"
" git push --set-upstream %s %s\n"
"%s"),
branch->name,
- remote_name,
- branch->name,
+ quoted_remote.buf,
+ quoted_branch.buf,
advice_autosetup_maybe);
}
if (branch->merge_nr != 1)
@@ -669,6 +690,8 @@ static void die_if_repo_looks_like_ref(const char *repo)
{
const char *slash = strchr(repo, '/');
struct strbuf name = STRBUF_INIT;
+ struct strbuf quoted_remote = STRBUF_INIT;
+ struct strbuf quoted_ref = STRBUF_INIT;
int code;
if (!slash || !slash[1] || file_exists(repo))
@@ -680,11 +703,16 @@ static void die_if_repo_looks_like_ref(const char *repo)
return;
}
+ sq_quote_buf_pretty("ed_remote, name.buf);
+ sq_quote_buf_pretty("ed_ref, slash + 1);
+
code = die_message(_("'%s' is not a valid push target"), repo);
advise_if_enabled(ADVICE_PUSH_REPO_LOOKS_LIKE_REF,
_("Did you mean to use: git push %s %s?"),
- name.buf, slash + 1);
+ quoted_remote.buf, quoted_ref.buf);
strbuf_release(&name);
+ strbuf_release("ed_remote);
+ strbuf_release("ed_ref);
exit(code);
}
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" &&
+ 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
+'
+
test_done
diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh
index 2bd8759a68..313f0628d4 100755
--- a/t/t5528-push-default.sh
+++ b/t/t5528-push-default.sh
@@ -294,4 +294,12 @@ test_expect_success 'default triangular behavior acts like "current"' '
test_push_success "" main repo2
'
+test_expect_success 'simple push quotes an upstream branch name with metacharacters' '
+ test_config branch.main.remote parent1 &&
+ test_config branch.main.merge "refs/heads/upstream&name" &&
+ git checkout main &&
+ test_must_fail git -c push.default=simple push parent1 2>stderr &&
+ test_grep "git push parent1 HEAD:${SQ}upstream&name${SQ}" stderr
+'
+
test_done
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
+'
+
test_expect_success 'no suggestion when prefix is not a configured remote' '
test_must_fail git push not-a-remote/main 2>stderr &&
test_grep ! "Did you mean" stderr
@@ -94,4 +99,103 @@ test_expect_success 'detect ambiguous refs early' '
test_cmp expect rp-ran
'
+# How a POSIX shell splits a copy-paste command line.
+shell_split () {
+ line=$1
+ eval "set -- $line"
+ printf "%s\n" "$#" "$@"
+}
+
+test_expect_success 'no-upstream advice leaves ordinary names unquoted' '
+ test_must_fail git -c push.default=simple push origin 2>stderr &&
+ test_grep -F "The current branch main has no upstream branch." stderr &&
+ test_grep -F " git push --set-upstream origin main" stderr &&
+ test_grep ! -F "git push --set-upstream ${SQ}origin${SQ}" stderr &&
+ test_grep ! -F "git push --set-upstream origin ${SQ}main${SQ}" stderr
+'
+
+test_expect_success 'simple push mismatch leaves ordinary names unquoted' '
+ test_config branch.main.remote origin &&
+ test_config branch.main.merge refs/heads/other-name &&
+ test_must_fail git -c push.default=simple push origin 2>stderr &&
+ printf "%s\n" \
+ "git push origin HEAD:other-name" \
+ "git push origin HEAD" >expect &&
+ sed -n "/^ git push /s/^ //p" stderr >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-upstream advice quotes metacharacters only in the command' '
+ test_when_finished "git checkout main" &&
+ git remote add "weird&remote" remote.git &&
+ test_when_finished "git remote remove \"weird&remote\"" &&
+ git checkout -b "feat-&;\$name" &&
+ test_when_finished "git checkout main && git branch -D ${SQ}feat-&;\$name${SQ}" &&
+ test_config "branch.feat-&;\$name.remote" "weird&remote" &&
+ test_must_fail git -c push.default=simple push "weird&remote" 2>stderr &&
+ test_grep -F "The current branch feat-&;\$name has no upstream branch." stderr &&
+ test_grep ! -F "The current branch ${SQ}feat-&;\$name${SQ} has no upstream" stderr &&
+ printf "%s\n" \
+ "git push --set-upstream ${SQ}weird&remote${SQ} ${SQ}feat-&;\$name${SQ}" >expect &&
+ sed -n "/^ git push /s/^ //p" stderr >actual &&
+ test_cmp expect actual &&
+ shell_split "$(cat actual)" >actual-split &&
+ printf "%s\n" 5 git push --set-upstream "weird&remote" "feat-&;\$name" >expect-split &&
+ test_cmp expect-split actual-split
+'
+
+test_expect_success 'simple push mismatch quotes remote and upstream names' '
+ git checkout main &&
+ git remote add "weird&remote" remote.git &&
+ test_when_finished "git remote remove \"weird&remote\"" &&
+ test_config branch.main.remote "weird&remote" &&
+ test_config branch.main.merge "refs/heads/feat-&;\$name" &&
+ test_must_fail git -c push.default=simple push "weird&remote" 2>stderr &&
+ printf "%s\n" \
+ "git push ${SQ}weird&remote${SQ} HEAD:${SQ}feat-&;\$name${SQ}" \
+ "git push ${SQ}weird&remote${SQ} HEAD" >expect &&
+ sed -n "/^ git push /s/^ //p" stderr >actual &&
+ test_cmp expect actual &&
+ {
+ while IFS= read -r line
+ do
+ shell_split "$line" || return 1
+ done <actual
+ } >actual-split &&
+ printf "%s\n" \
+ 4 git push "weird&remote" "HEAD:feat-&;\$name" \
+ 4 git push "weird&remote" HEAD >expect-split &&
+ test_cmp expect-split actual-split
+'
+
+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 "weird&remote/feat-&-test" 2>stderr &&
+ test_grep -F "hint: Did you mean to use: git push ${SQ}weird&remote${SQ} ${SQ}feat-&-test${SQ}?" stderr &&
+ hint=$(sed -n "s/.*Did you mean to use: //p" stderr) &&
+ hint=${hint%?} &&
+ shell_split "$hint" >actual-split &&
+ printf "%s\n" 4 git push "weird&remote" "feat-&-test" >expect-split &&
+ test_cmp expect-split actual-split &&
+
+ 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 &&
+
+ test_must_fail git push "origin/feat\$name" 2>stderr &&
+ test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat\$name${SQ}?" stderr &&
+
+ test_must_fail git push "origin/feat & test" 2>stderr &&
+ test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat & test${SQ}?" stderr &&
+ hint=$(sed -n "s/.*Did you mean to use: //p" stderr) &&
+ hint=${hint%?} &&
+ shell_split "$hint" >actual-split &&
+ printf "%s\n" 4 git push origin "feat & test" >expect-split &&
+ test_cmp expect-split actual-split &&
+
+ test_must_fail git push "origin/feat${SQ}test" 2>stderr &&
+ test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat${SQ}\\${SQ}${SQ}test${SQ}?" stderr
+'
+
test_done
base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe
--
gitgitgadget
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH] push: shell-quote names in suggested push commands 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 2026-11-27 2:54 ` Nahid hasan Sagor 2026-11-27 2:48 ` [PATCH v2] " Nahid Hasan via GitGitGadget 1 sibling, 1 reply; 9+ messages in thread From: Junio C Hamano @ 2026-11-26 19:04 UTC (permalink / raw) To: Nahid Hasan via GitGitGadget; +Cc: git, Nahid Hasan "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 && ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] push: shell-quote names in suggested push commands 2026-11-26 19:04 ` Junio C Hamano @ 2026-11-27 2:54 ` Nahid hasan Sagor 0 siblings, 0 replies; 9+ messages in thread From: Nahid hasan Sagor @ 2026-11-27 2:54 UTC (permalink / raw) To: Junio C Hamano; +Cc: Nahid Hasan via GitGitGadget, git 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: > > "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 && ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2] push: shell-quote names in suggested push commands 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 @ 2026-11-27 2:48 ` Nahid Hasan via GitGitGadget 2026-11-27 2:52 ` Nahid hasan Sagor 2026-11-27 5:53 ` Junio C Hamano 1 sibling, 2 replies; 9+ messages in thread From: Nahid Hasan via GitGitGadget @ 2026-11-27 2:48 UTC (permalink / raw) To: git; +Cc: Nahid Hasan, MD Nahid Hasan Sagor From: MD Nahid Hasan Sagor <nahidhasan141400@gmail.com> 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> --- push: shell-quote names in suggested push commands Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2336%2Fnahidhasan141400%2Ffix-push-upstream-quoting-v2 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2336/nahidhasan141400/fix-push-upstream-quoting-v2 Pull-Request: https://github.com/gitgitgadget/git/pull/2336 Range-diff vs v1: 1: 5494c805c1 ! 1: dad0b1673d push: shell-quote names in suggested push commands @@ Commit message 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. + 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> @@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, const char *short_upstream = branch->merge[0]->src; struct repo_config_values *cfg = repo_config_values(the_repository); + struct strbuf quoted_remote = STRBUF_INIT; -+ struct strbuf quoted_upstream = STRBUF_INIT; ++ struct strbuf refspec = STRBUF_INIT; ++ struct strbuf quoted_refspec = STRBUF_INIT; skip_prefix(short_upstream, "refs/heads/", &short_upstream); @@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, + + /* + * 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. ++ * bare; metacharacters are wrapped in single quotes. + */ ++ strbuf_addf(&refspec, "HEAD:%s", short_upstream); + sq_quote_buf_pretty("ed_remote, remote->name); -+ sq_quote_buf_pretty("ed_upstream, short_upstream); ++ sq_quote_buf_pretty("ed_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" -@@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, + "\n" +- " git push %s HEAD:%s\n" ++ " git push %s %s\n" + "\n" + "To push to the branch of the same name on the remote, use\n" "\n" " git push %s HEAD\n" "%s%s"), - remote->name, short_upstream, - remote->name, advice_pushdefault_maybe, -+ quoted_remote.buf, quoted_upstream.buf, ++ quoted_remote.buf, quoted_refspec.buf, + quoted_remote.buf, advice_pushdefault_maybe, advice_automergesimple_maybe); } @@ builtin/push.c: static const char *get_upstream_ref(int flags, struct branch *br } + /* + * 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. ++ * copy-pasteable command is shell-quoted. + */ + sq_quote_buf_pretty("ed_remote, remote_name); + sq_quote_buf_pretty("ed_branch, branch->name); @@ builtin/push.c: static const char *get_upstream_ref(int flags, struct branch *br advice_autosetup_maybe); } if (branch->merge_nr != 1) +@@ builtin/push.c: 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("ed_remote, remote->name); ++ die(_(message_detached_head_die), quoted_remote.buf); ++ } + + dst = branch->refname; + same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL)); @@ builtin/push.c: static void die_if_repo_looks_like_ref(const char *repo) { const char *slash = strchr(repo, '/'); @@ t/t5523-push-upstream.sh: test_expect_success TTY 'quiet push -u' ' ' +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_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 +' @@ t/t5528-push-default.sh: test_expect_success 'default triangular behavior acts l + test_config branch.main.merge "refs/heads/upstream&name" && + git checkout main && + test_must_fail git -c push.default=simple push parent1 2>stderr && -+ test_grep "git push parent1 HEAD:${SQ}upstream&name${SQ}" stderr ++ test_grep "git push parent1 ${SQ}HEAD:upstream&name${SQ}" stderr ++' ++ ++test_expect_success 'detached HEAD advice quotes a remote name with metacharacters' ' ++ git remote add "weird&remote" repo1 && ++ test_when_finished "git remote remove \"weird&remote\"" && ++ test_when_finished "git checkout main" && ++ git checkout --detach && ++ test_must_fail git push "weird&remote" 2>stderr && ++ test_grep " git push ${SQ}weird&remote${SQ} HEAD:" stderr +' + test_done ## t/t5529-push-errors.sh ## -@@ t/t5529-push-errors.sh: 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 -+' -+ - test_expect_success 'no suggestion when prefix is not a configured remote' ' - test_must_fail git push not-a-remote/main 2>stderr && - test_grep ! "Did you mean" stderr @@ t/t5529-push-errors.sh: test_expect_success 'detect ambiguous refs early' ' test_cmp expect rp-ran ' @@ t/t5529-push-errors.sh: test_expect_success 'detect ambiguous refs early' ' +' + +test_expect_success 'no-upstream advice quotes metacharacters only in the command' ' -+ test_when_finished "git checkout main" && + git remote add "weird&remote" remote.git && + test_when_finished "git remote remove \"weird&remote\"" && ++ test_when_finished "git branch -D ${SQ}feat-&;\$name${SQ}" && ++ test_when_finished "git checkout main" && + git checkout -b "feat-&;\$name" && -+ test_when_finished "git checkout main && git branch -D ${SQ}feat-&;\$name${SQ}" && + test_config "branch.feat-&;\$name.remote" "weird&remote" && + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && + test_grep -F "The current branch feat-&;\$name has no upstream branch." stderr && @@ t/t5529-push-errors.sh: test_expect_success 'detect ambiguous refs early' ' + test_config branch.main.merge "refs/heads/feat-&;\$name" && + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && + printf "%s\n" \ -+ "git push ${SQ}weird&remote${SQ} HEAD:${SQ}feat-&;\$name${SQ}" \ ++ "git push ${SQ}weird&remote${SQ} ${SQ}HEAD:feat-&;\$name${SQ}" \ + "git push ${SQ}weird&remote${SQ} HEAD" >expect && + sed -n "/^ git push /s/^ //p" stderr >actual && + test_cmp expect actual && 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 --- a/builtin/push.c +++ b/builtin/push.c @@ -18,6 +18,7 @@ #include "transport.h" #include "parse-options.h" #include "pkt-line.h" +#include "quote.h" #include "submodule.h" #include "submodule-config.h" #include "send-pack.h" @@ -155,6 +156,9 @@ static NORETURN void die_push_simple(struct branch *branch, const char *advice_automergesimple_maybe = ""; const char *short_upstream = branch->merge[0]->src; struct repo_config_values *cfg = repo_config_values(the_repository); + struct strbuf quoted_remote = STRBUF_INIT; + struct strbuf refspec = STRBUF_INIT; + struct strbuf quoted_refspec = STRBUF_INIT; skip_prefix(short_upstream, "refs/heads/", &short_upstream); @@ -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("ed_remote, remote->name); + sq_quote_buf_pretty("ed_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" "\n" "To push to the branch of the same name on the remote, use\n" "\n" " git push %s HEAD\n" "%s%s"), - remote->name, short_upstream, - remote->name, advice_pushdefault_maybe, + quoted_remote.buf, quoted_refspec.buf, + quoted_remote.buf, advice_pushdefault_maybe, advice_automergesimple_maybe); } @@ -204,6 +216,9 @@ static const char *get_upstream_ref(int flags, struct branch *branch, const char if (!branch->merge_nr || !branch->merge || !branch->remote_name) { const char *advice_autosetup_maybe = ""; + struct strbuf quoted_remote = STRBUF_INIT; + struct strbuf quoted_branch = STRBUF_INIT; + if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) { advice_autosetup_maybe = _("\n" "To have this happen automatically for " @@ -211,14 +226,20 @@ 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. + */ + sq_quote_buf_pretty("ed_remote, remote_name); + sq_quote_buf_pretty("ed_branch, branch->name); die(_("The current branch %s has no upstream branch.\n" "To push the current branch and set the remote as upstream, use\n" "\n" " git push --set-upstream %s %s\n" "%s"), branch->name, - remote_name, - branch->name, + quoted_remote.buf, + quoted_branch.buf, advice_autosetup_maybe); } if (branch->merge_nr != 1) @@ -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("ed_remote, remote->name); + die(_(message_detached_head_die), quoted_remote.buf); + } dst = branch->refname; same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL)); @@ -669,6 +694,8 @@ static void die_if_repo_looks_like_ref(const char *repo) { const char *slash = strchr(repo, '/'); struct strbuf name = STRBUF_INIT; + struct strbuf quoted_remote = STRBUF_INIT; + struct strbuf quoted_ref = STRBUF_INIT; int code; if (!slash || !slash[1] || file_exists(repo)) @@ -680,11 +707,16 @@ static void die_if_repo_looks_like_ref(const char *repo) return; } + sq_quote_buf_pretty("ed_remote, name.buf); + sq_quote_buf_pretty("ed_ref, slash + 1); + code = die_message(_("'%s' is not a valid push target"), repo); advise_if_enabled(ADVICE_PUSH_REPO_LOOKS_LIKE_REF, _("Did you mean to use: git push %s %s?"), - name.buf, slash + 1); + quoted_remote.buf, quoted_ref.buf); strbuf_release(&name); + strbuf_release("ed_remote); + strbuf_release("ed_ref); exit(code); } diff --git a/t/t5523-push-upstream.sh b/t/t5523-push-upstream.sh index 22d3e1162c..23b20b0419 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 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_done diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh index 2bd8759a68..0c009f3543 100755 --- a/t/t5528-push-default.sh +++ b/t/t5528-push-default.sh @@ -294,4 +294,21 @@ test_expect_success 'default triangular behavior acts like "current"' ' test_push_success "" main repo2 ' +test_expect_success 'simple push quotes an upstream branch name with metacharacters' ' + test_config branch.main.remote parent1 && + test_config branch.main.merge "refs/heads/upstream&name" && + git checkout main && + test_must_fail git -c push.default=simple push parent1 2>stderr && + test_grep "git push parent1 ${SQ}HEAD:upstream&name${SQ}" stderr +' + +test_expect_success 'detached HEAD advice quotes a remote name with metacharacters' ' + git remote add "weird&remote" repo1 && + test_when_finished "git remote remove \"weird&remote\"" && + test_when_finished "git checkout main" && + git checkout --detach && + test_must_fail git push "weird&remote" 2>stderr && + test_grep " git push ${SQ}weird&remote${SQ} HEAD:" stderr +' + test_done diff --git a/t/t5529-push-errors.sh b/t/t5529-push-errors.sh index 5f8aa1ed19..57d50d8950 100755 --- a/t/t5529-push-errors.sh +++ b/t/t5529-push-errors.sh @@ -94,4 +94,103 @@ test_expect_success 'detect ambiguous refs early' ' test_cmp expect rp-ran ' +# How a POSIX shell splits a copy-paste command line. +shell_split () { + line=$1 + eval "set -- $line" + printf "%s\n" "$#" "$@" +} + +test_expect_success 'no-upstream advice leaves ordinary names unquoted' ' + test_must_fail git -c push.default=simple push origin 2>stderr && + test_grep -F "The current branch main has no upstream branch." stderr && + test_grep -F " git push --set-upstream origin main" stderr && + test_grep ! -F "git push --set-upstream ${SQ}origin${SQ}" stderr && + test_grep ! -F "git push --set-upstream origin ${SQ}main${SQ}" stderr +' + +test_expect_success 'simple push mismatch leaves ordinary names unquoted' ' + test_config branch.main.remote origin && + test_config branch.main.merge refs/heads/other-name && + test_must_fail git -c push.default=simple push origin 2>stderr && + printf "%s\n" \ + "git push origin HEAD:other-name" \ + "git push origin HEAD" >expect && + sed -n "/^ git push /s/^ //p" stderr >actual && + test_cmp expect actual +' + +test_expect_success 'no-upstream advice quotes metacharacters only in the command' ' + git remote add "weird&remote" remote.git && + test_when_finished "git remote remove \"weird&remote\"" && + test_when_finished "git branch -D ${SQ}feat-&;\$name${SQ}" && + test_when_finished "git checkout main" && + git checkout -b "feat-&;\$name" && + test_config "branch.feat-&;\$name.remote" "weird&remote" && + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && + test_grep -F "The current branch feat-&;\$name has no upstream branch." stderr && + test_grep ! -F "The current branch ${SQ}feat-&;\$name${SQ} has no upstream" stderr && + printf "%s\n" \ + "git push --set-upstream ${SQ}weird&remote${SQ} ${SQ}feat-&;\$name${SQ}" >expect && + sed -n "/^ git push /s/^ //p" stderr >actual && + test_cmp expect actual && + shell_split "$(cat actual)" >actual-split && + printf "%s\n" 5 git push --set-upstream "weird&remote" "feat-&;\$name" >expect-split && + test_cmp expect-split actual-split +' + +test_expect_success 'simple push mismatch quotes remote and upstream names' ' + git checkout main && + git remote add "weird&remote" remote.git && + test_when_finished "git remote remove \"weird&remote\"" && + test_config branch.main.remote "weird&remote" && + test_config branch.main.merge "refs/heads/feat-&;\$name" && + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && + printf "%s\n" \ + "git push ${SQ}weird&remote${SQ} ${SQ}HEAD:feat-&;\$name${SQ}" \ + "git push ${SQ}weird&remote${SQ} HEAD" >expect && + sed -n "/^ git push /s/^ //p" stderr >actual && + test_cmp expect actual && + { + while IFS= read -r line + do + shell_split "$line" || return 1 + done <actual + } >actual-split && + printf "%s\n" \ + 4 git push "weird&remote" "HEAD:feat-&;\$name" \ + 4 git push "weird&remote" HEAD >expect-split && + test_cmp expect-split actual-split +' + +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 "weird&remote/feat-&-test" 2>stderr && + test_grep -F "hint: Did you mean to use: git push ${SQ}weird&remote${SQ} ${SQ}feat-&-test${SQ}?" stderr && + hint=$(sed -n "s/.*Did you mean to use: //p" stderr) && + hint=${hint%?} && + shell_split "$hint" >actual-split && + printf "%s\n" 4 git push "weird&remote" "feat-&-test" >expect-split && + test_cmp expect-split actual-split && + + 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 && + + test_must_fail git push "origin/feat\$name" 2>stderr && + test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat\$name${SQ}?" stderr && + + test_must_fail git push "origin/feat & test" 2>stderr && + test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat & test${SQ}?" stderr && + hint=$(sed -n "s/.*Did you mean to use: //p" stderr) && + hint=${hint%?} && + shell_split "$hint" >actual-split && + printf "%s\n" 4 git push origin "feat & test" >expect-split && + test_cmp expect-split actual-split && + + test_must_fail git push "origin/feat${SQ}test" 2>stderr && + test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat${SQ}\\${SQ}${SQ}test${SQ}?" stderr +' + test_done base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe -- gitgitgadget ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2] push: shell-quote names in suggested push commands 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:53 ` Junio C Hamano 1 sibling, 1 reply; 9+ messages in thread From: Nahid hasan Sagor @ 2026-11-27 2:52 UTC (permalink / raw) To: Nahid Hasan via GitGitGadget; +Cc: git 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 8:48 AM Nahid Hasan via GitGitGadget <gitgitgadget@gmail.com> wrote: > > From: MD Nahid Hasan Sagor <nahidhasan141400@gmail.com> > > 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> > --- > push: shell-quote names in suggested push commands > > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2336%2Fnahidhasan141400%2Ffix-push-upstream-quoting-v2 > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2336/nahidhasan141400/fix-push-upstream-quoting-v2 > Pull-Request: https://github.com/gitgitgadget/git/pull/2336 > > Range-diff vs v1: > > 1: 5494c805c1 ! 1: dad0b1673d push: shell-quote names in suggested push commands > @@ Commit message > 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. > + 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> > > @@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, > const char *short_upstream = branch->merge[0]->src; > struct repo_config_values *cfg = repo_config_values(the_repository); > + struct strbuf quoted_remote = STRBUF_INIT; > -+ struct strbuf quoted_upstream = STRBUF_INIT; > ++ struct strbuf refspec = STRBUF_INIT; > ++ struct strbuf quoted_refspec = STRBUF_INIT; > > skip_prefix(short_upstream, "refs/heads/", &short_upstream); > > @@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, > + > + /* > + * 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. > ++ * bare; metacharacters are wrapped in single quotes. > + */ > ++ strbuf_addf(&refspec, "HEAD:%s", short_upstream); > + sq_quote_buf_pretty("ed_remote, remote->name); > -+ sq_quote_buf_pretty("ed_upstream, short_upstream); > ++ sq_quote_buf_pretty("ed_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" > -@@ builtin/push.c: static NORETURN void die_push_simple(struct branch *branch, > + "\n" > +- " git push %s HEAD:%s\n" > ++ " git push %s %s\n" > + "\n" > + "To push to the branch of the same name on the remote, use\n" > "\n" > " git push %s HEAD\n" > "%s%s"), > - remote->name, short_upstream, > - remote->name, advice_pushdefault_maybe, > -+ quoted_remote.buf, quoted_upstream.buf, > ++ quoted_remote.buf, quoted_refspec.buf, > + quoted_remote.buf, advice_pushdefault_maybe, > advice_automergesimple_maybe); > } > @@ builtin/push.c: static const char *get_upstream_ref(int flags, struct branch *br > } > + /* > + * 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. > ++ * copy-pasteable command is shell-quoted. > + */ > + sq_quote_buf_pretty("ed_remote, remote_name); > + sq_quote_buf_pretty("ed_branch, branch->name); > @@ builtin/push.c: static const char *get_upstream_ref(int flags, struct branch *br > advice_autosetup_maybe); > } > if (branch->merge_nr != 1) > +@@ builtin/push.c: 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("ed_remote, remote->name); > ++ die(_(message_detached_head_die), quoted_remote.buf); > ++ } > + > + dst = branch->refname; > + same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL)); > @@ builtin/push.c: static void die_if_repo_looks_like_ref(const char *repo) > { > const char *slash = strchr(repo, '/'); > @@ t/t5523-push-upstream.sh: test_expect_success TTY 'quiet push -u' ' > ' > > +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_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 > +' > @@ t/t5528-push-default.sh: test_expect_success 'default triangular behavior acts l > + test_config branch.main.merge "refs/heads/upstream&name" && > + git checkout main && > + test_must_fail git -c push.default=simple push parent1 2>stderr && > -+ test_grep "git push parent1 HEAD:${SQ}upstream&name${SQ}" stderr > ++ test_grep "git push parent1 ${SQ}HEAD:upstream&name${SQ}" stderr > ++' > ++ > ++test_expect_success 'detached HEAD advice quotes a remote name with metacharacters' ' > ++ git remote add "weird&remote" repo1 && > ++ test_when_finished "git remote remove \"weird&remote\"" && > ++ test_when_finished "git checkout main" && > ++ git checkout --detach && > ++ test_must_fail git push "weird&remote" 2>stderr && > ++ test_grep " git push ${SQ}weird&remote${SQ} HEAD:" stderr > +' > + > test_done > > ## t/t5529-push-errors.sh ## > -@@ t/t5529-push-errors.sh: 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 > -+' > -+ > - test_expect_success 'no suggestion when prefix is not a configured remote' ' > - test_must_fail git push not-a-remote/main 2>stderr && > - test_grep ! "Did you mean" stderr > @@ t/t5529-push-errors.sh: test_expect_success 'detect ambiguous refs early' ' > test_cmp expect rp-ran > ' > @@ t/t5529-push-errors.sh: test_expect_success 'detect ambiguous refs early' ' > +' > + > +test_expect_success 'no-upstream advice quotes metacharacters only in the command' ' > -+ test_when_finished "git checkout main" && > + git remote add "weird&remote" remote.git && > + test_when_finished "git remote remove \"weird&remote\"" && > ++ test_when_finished "git branch -D ${SQ}feat-&;\$name${SQ}" && > ++ test_when_finished "git checkout main" && > + git checkout -b "feat-&;\$name" && > -+ test_when_finished "git checkout main && git branch -D ${SQ}feat-&;\$name${SQ}" && > + test_config "branch.feat-&;\$name.remote" "weird&remote" && > + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && > + test_grep -F "The current branch feat-&;\$name has no upstream branch." stderr && > @@ t/t5529-push-errors.sh: test_expect_success 'detect ambiguous refs early' ' > + test_config branch.main.merge "refs/heads/feat-&;\$name" && > + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && > + printf "%s\n" \ > -+ "git push ${SQ}weird&remote${SQ} HEAD:${SQ}feat-&;\$name${SQ}" \ > ++ "git push ${SQ}weird&remote${SQ} ${SQ}HEAD:feat-&;\$name${SQ}" \ > + "git push ${SQ}weird&remote${SQ} HEAD" >expect && > + sed -n "/^ git push /s/^ //p" stderr >actual && > + test_cmp expect actual && > > > 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 > --- a/builtin/push.c > +++ b/builtin/push.c > @@ -18,6 +18,7 @@ > #include "transport.h" > #include "parse-options.h" > #include "pkt-line.h" > +#include "quote.h" > #include "submodule.h" > #include "submodule-config.h" > #include "send-pack.h" > @@ -155,6 +156,9 @@ static NORETURN void die_push_simple(struct branch *branch, > const char *advice_automergesimple_maybe = ""; > const char *short_upstream = branch->merge[0]->src; > struct repo_config_values *cfg = repo_config_values(the_repository); > + struct strbuf quoted_remote = STRBUF_INIT; > + struct strbuf refspec = STRBUF_INIT; > + struct strbuf quoted_refspec = STRBUF_INIT; > > skip_prefix(short_upstream, "refs/heads/", &short_upstream); > > @@ -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("ed_remote, remote->name); > + sq_quote_buf_pretty("ed_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" > "\n" > "To push to the branch of the same name on the remote, use\n" > "\n" > " git push %s HEAD\n" > "%s%s"), > - remote->name, short_upstream, > - remote->name, advice_pushdefault_maybe, > + quoted_remote.buf, quoted_refspec.buf, > + quoted_remote.buf, advice_pushdefault_maybe, > advice_automergesimple_maybe); > } > > @@ -204,6 +216,9 @@ static const char *get_upstream_ref(int flags, struct branch *branch, const char > > if (!branch->merge_nr || !branch->merge || !branch->remote_name) { > const char *advice_autosetup_maybe = ""; > + struct strbuf quoted_remote = STRBUF_INIT; > + struct strbuf quoted_branch = STRBUF_INIT; > + > if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) { > advice_autosetup_maybe = _("\n" > "To have this happen automatically for " > @@ -211,14 +226,20 @@ 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. > + */ > + sq_quote_buf_pretty("ed_remote, remote_name); > + sq_quote_buf_pretty("ed_branch, branch->name); > die(_("The current branch %s has no upstream branch.\n" > "To push the current branch and set the remote as upstream, use\n" > "\n" > " git push --set-upstream %s %s\n" > "%s"), > branch->name, > - remote_name, > - branch->name, > + quoted_remote.buf, > + quoted_branch.buf, > advice_autosetup_maybe); > } > if (branch->merge_nr != 1) > @@ -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("ed_remote, remote->name); > + die(_(message_detached_head_die), quoted_remote.buf); > + } > > dst = branch->refname; > same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL)); > @@ -669,6 +694,8 @@ static void die_if_repo_looks_like_ref(const char *repo) > { > const char *slash = strchr(repo, '/'); > struct strbuf name = STRBUF_INIT; > + struct strbuf quoted_remote = STRBUF_INIT; > + struct strbuf quoted_ref = STRBUF_INIT; > int code; > > if (!slash || !slash[1] || file_exists(repo)) > @@ -680,11 +707,16 @@ static void die_if_repo_looks_like_ref(const char *repo) > return; > } > > + sq_quote_buf_pretty("ed_remote, name.buf); > + sq_quote_buf_pretty("ed_ref, slash + 1); > + > code = die_message(_("'%s' is not a valid push target"), repo); > advise_if_enabled(ADVICE_PUSH_REPO_LOOKS_LIKE_REF, > _("Did you mean to use: git push %s %s?"), > - name.buf, slash + 1); > + quoted_remote.buf, quoted_ref.buf); > strbuf_release(&name); > + strbuf_release("ed_remote); > + strbuf_release("ed_ref); > exit(code); > } > > diff --git a/t/t5523-push-upstream.sh b/t/t5523-push-upstream.sh > index 22d3e1162c..23b20b0419 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 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_done > diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh > index 2bd8759a68..0c009f3543 100755 > --- a/t/t5528-push-default.sh > +++ b/t/t5528-push-default.sh > @@ -294,4 +294,21 @@ test_expect_success 'default triangular behavior acts like "current"' ' > test_push_success "" main repo2 > ' > > +test_expect_success 'simple push quotes an upstream branch name with metacharacters' ' > + test_config branch.main.remote parent1 && > + test_config branch.main.merge "refs/heads/upstream&name" && > + git checkout main && > + test_must_fail git -c push.default=simple push parent1 2>stderr && > + test_grep "git push parent1 ${SQ}HEAD:upstream&name${SQ}" stderr > +' > + > +test_expect_success 'detached HEAD advice quotes a remote name with metacharacters' ' > + git remote add "weird&remote" repo1 && > + test_when_finished "git remote remove \"weird&remote\"" && > + test_when_finished "git checkout main" && > + git checkout --detach && > + test_must_fail git push "weird&remote" 2>stderr && > + test_grep " git push ${SQ}weird&remote${SQ} HEAD:" stderr > +' > + > test_done > diff --git a/t/t5529-push-errors.sh b/t/t5529-push-errors.sh > index 5f8aa1ed19..57d50d8950 100755 > --- a/t/t5529-push-errors.sh > +++ b/t/t5529-push-errors.sh > @@ -94,4 +94,103 @@ test_expect_success 'detect ambiguous refs early' ' > test_cmp expect rp-ran > ' > > +# How a POSIX shell splits a copy-paste command line. > +shell_split () { > + line=$1 > + eval "set -- $line" > + printf "%s\n" "$#" "$@" > +} > + > +test_expect_success 'no-upstream advice leaves ordinary names unquoted' ' > + test_must_fail git -c push.default=simple push origin 2>stderr && > + test_grep -F "The current branch main has no upstream branch." stderr && > + test_grep -F " git push --set-upstream origin main" stderr && > + test_grep ! -F "git push --set-upstream ${SQ}origin${SQ}" stderr && > + test_grep ! -F "git push --set-upstream origin ${SQ}main${SQ}" stderr > +' > + > +test_expect_success 'simple push mismatch leaves ordinary names unquoted' ' > + test_config branch.main.remote origin && > + test_config branch.main.merge refs/heads/other-name && > + test_must_fail git -c push.default=simple push origin 2>stderr && > + printf "%s\n" \ > + "git push origin HEAD:other-name" \ > + "git push origin HEAD" >expect && > + sed -n "/^ git push /s/^ //p" stderr >actual && > + test_cmp expect actual > +' > + > +test_expect_success 'no-upstream advice quotes metacharacters only in the command' ' > + git remote add "weird&remote" remote.git && > + test_when_finished "git remote remove \"weird&remote\"" && > + test_when_finished "git branch -D ${SQ}feat-&;\$name${SQ}" && > + test_when_finished "git checkout main" && > + git checkout -b "feat-&;\$name" && > + test_config "branch.feat-&;\$name.remote" "weird&remote" && > + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && > + test_grep -F "The current branch feat-&;\$name has no upstream branch." stderr && > + test_grep ! -F "The current branch ${SQ}feat-&;\$name${SQ} has no upstream" stderr && > + printf "%s\n" \ > + "git push --set-upstream ${SQ}weird&remote${SQ} ${SQ}feat-&;\$name${SQ}" >expect && > + sed -n "/^ git push /s/^ //p" stderr >actual && > + test_cmp expect actual && > + shell_split "$(cat actual)" >actual-split && > + printf "%s\n" 5 git push --set-upstream "weird&remote" "feat-&;\$name" >expect-split && > + test_cmp expect-split actual-split > +' > + > +test_expect_success 'simple push mismatch quotes remote and upstream names' ' > + git checkout main && > + git remote add "weird&remote" remote.git && > + test_when_finished "git remote remove \"weird&remote\"" && > + test_config branch.main.remote "weird&remote" && > + test_config branch.main.merge "refs/heads/feat-&;\$name" && > + test_must_fail git -c push.default=simple push "weird&remote" 2>stderr && > + printf "%s\n" \ > + "git push ${SQ}weird&remote${SQ} ${SQ}HEAD:feat-&;\$name${SQ}" \ > + "git push ${SQ}weird&remote${SQ} HEAD" >expect && > + sed -n "/^ git push /s/^ //p" stderr >actual && > + test_cmp expect actual && > + { > + while IFS= read -r line > + do > + shell_split "$line" || return 1 > + done <actual > + } >actual-split && > + printf "%s\n" \ > + 4 git push "weird&remote" "HEAD:feat-&;\$name" \ > + 4 git push "weird&remote" HEAD >expect-split && > + test_cmp expect-split actual-split > +' > + > +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 "weird&remote/feat-&-test" 2>stderr && > + test_grep -F "hint: Did you mean to use: git push ${SQ}weird&remote${SQ} ${SQ}feat-&-test${SQ}?" stderr && > + hint=$(sed -n "s/.*Did you mean to use: //p" stderr) && > + hint=${hint%?} && > + shell_split "$hint" >actual-split && > + printf "%s\n" 4 git push "weird&remote" "feat-&-test" >expect-split && > + test_cmp expect-split actual-split && > + > + 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 && > + > + test_must_fail git push "origin/feat\$name" 2>stderr && > + test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat\$name${SQ}?" stderr && > + > + test_must_fail git push "origin/feat & test" 2>stderr && > + test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat & test${SQ}?" stderr && > + hint=$(sed -n "s/.*Did you mean to use: //p" stderr) && > + hint=${hint%?} && > + shell_split "$hint" >actual-split && > + printf "%s\n" 4 git push origin "feat & test" >expect-split && > + test_cmp expect-split actual-split && > + > + test_must_fail git push "origin/feat${SQ}test" 2>stderr && > + test_grep -F "hint: Did you mean to use: git push origin ${SQ}feat${SQ}\\${SQ}${SQ}test${SQ}?" stderr > +' > + > test_done > > base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe > -- > gitgitgadget ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] push: shell-quote names in suggested push commands 2026-11-27 2:52 ` Nahid hasan Sagor @ 2026-11-27 5:19 ` Junio C Hamano 2026-11-27 5:26 ` Nahid hasan Sagor 0 siblings, 1 reply; 9+ messages in thread From: Junio C Hamano @ 2026-11-27 5:19 UTC (permalink / raw) To: Nahid hasan Sagor; +Cc: Nahid Hasan via GitGitGadget, git Nahid hasan Sagor <nahidhasan141400@gmail.com> writes: > 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 It is not considered good manners to send a new iteration before first finishing the discussion on the reviews you received on your previous iteration. You respond to reviews, saying "I will fix this point", "I think this one is fine as-is because...", and so forth, wait for responses to your response, and eventually come to an agreement with those in the discussion, so both sides know what the next iteration should look like before you start working on it. Only after finishing all of that do you prepare and send a new iteration. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] push: shell-quote names in suggested push commands 2026-11-27 5:19 ` Junio C Hamano @ 2026-11-27 5:26 ` Nahid hasan Sagor 0 siblings, 0 replies; 9+ messages in thread From: Nahid hasan Sagor @ 2026-11-27 5:26 UTC (permalink / raw) To: Junio C Hamano; +Cc: Nahid Hasan via GitGitGadget, git Hi Junio, I am very sorry about that, I didn't realize the workflow here and followed my usual GitHub approach by mistake. Thanks for explaining. I will make sure to finish the discussion first before sending a new iteration going forward. On Fri, Nov 27, 2026 at 11:19 AM Junio C Hamano <gitster@pobox.com> wrote: > > Nahid hasan Sagor <nahidhasan141400@gmail.com> writes: > > > 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 > > It is not considered good manners to send a new iteration before > first finishing the discussion on the reviews you received on your > previous iteration. You respond to reviews, saying "I will fix this > point", "I think this one is fine as-is because...", and so forth, > wait for responses to your response, and eventually come to an > agreement with those in the discussion, so both sides know what the > next iteration should look like before you start working on it. > > Only after finishing all of that do you prepare and send a new > iteration. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] push: shell-quote names in suggested push commands 2026-11-27 2:48 ` [PATCH v2] " Nahid Hasan via GitGitGadget 2026-11-27 2:52 ` Nahid hasan Sagor @ 2026-11-27 5:53 ` Junio C Hamano 2026-11-27 6:06 ` Nahid hasan Sagor 1 sibling, 1 reply; 9+ messages in thread From: Junio C Hamano @ 2026-11-27 5:53 UTC (permalink / raw) To: Nahid Hasan via GitGitGadget; +Cc: git, Nahid Hasan "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. 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. > 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("ed_remote, remote->name); > + sq_quote_buf_pretty("ed_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'. > @@ -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("ed_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. > +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. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] push: shell-quote names in suggested push commands 2026-11-27 5:53 ` Junio C Hamano @ 2026-11-27 6:06 ` Nahid hasan Sagor 0 siblings, 0 replies; 9+ messages in thread From: Nahid hasan Sagor @ 2026-11-27 6:06 UTC (permalink / raw) To: Junio C Hamano; +Cc: Nahid Hasan via GitGitGadget, git On Fri, Nov 27, 2026 at 11:53 AM Junio C Hamano <gitster@pobox.com> wrote: > > "Nahid Hasan via GitGitGadget" <gitgitgadget@gmail.com> writes: > > 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. > Thank you very much for the review and for pointing out the formatting consistency in the commit message. Using single quotes throughout ('git push', 'builtin/push.c', 'sq_quote_buf_pretty()', 'HEAD:<dst>', and 'setup_default_push_refspecs()') makes complete sense for plain-text logs. I will wait to see if any other review comments come in before sending v3 or please let me know whenever you would like me to send v3 with this log message update. Thank you again for your time and guidance. ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-27 6:06 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox