Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions builtin/push.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand All @@ -173,18 +177,23 @@ 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. */
strbuf_addf(&refspec, "HEAD:%s", short_upstream);
sq_quote_buf_pretty(&quoted_remote, remote->name);
sq_quote_buf_pretty(&quoted_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);
}

Expand All @@ -204,21 +213,30 @@ 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 "
"branches without a tracking\n"
"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(&quoted_remote, remote_name);
sq_quote_buf_pretty(&quoted_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)
Expand Down Expand Up @@ -249,8 +267,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(&quoted_remote, remote->name);
die(_(message_detached_head_die), quoted_remote.buf);
}

dst = branch->refname;
same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
Expand Down Expand Up @@ -669,6 +691,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))
Expand All @@ -680,11 +704,16 @@ static void die_if_repo_looks_like_ref(const char *repo)
return;
}

sq_quote_buf_pretty(&quoted_remote, name.buf);
sq_quote_buf_pretty(&quoted_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(&quoted_remote);
strbuf_release(&quoted_ref);
exit(code);
}

Expand Down
8 changes: 8 additions & 0 deletions t/t5523-push-upstream.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions t/t5528-push-default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions t/t5529-push-errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,11 @@ test_expect_success 'detect ambiguous refs early' '
test_cmp expect rp-ran
'

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
'

test_done
Loading