Skip to content

Commit e45aa87

Browse files
branch: suggest <remote>/<branch> when set-upstream-to slips
A user reaching for "git branch --set-upstream-to origin main" expects the second token to name the remote branch to track. The option takes a single <remote>/<branch> argument, so the trailing word is read as the local branch to operate on and the command dies with "branch 'main' does not exist", which points at the wrong thing. When the local branch does not exist, the upstream argument is a bare remote name, and that remote has a matching remote-tracking ref, suggest the intended "--set-upstream-to=<remote>/<branch>" form. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
1 parent 1ff279f commit e45aa87

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

builtin/branch.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,19 @@ int cmd_branch(int argc,
957957
if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) {
958958
if (!argc || branch_checked_out(branch->refname))
959959
die(_("no commit on branch '%s' yet"), branch->name);
960+
if (argc == 1 && !strchr(new_upstream, '/') &&
961+
remote_is_configured(remote_get(new_upstream), 0)) {
962+
struct strbuf remote_ref = STRBUF_INIT;
963+
964+
strbuf_addf(&remote_ref, "refs/remotes/%s/%s",
965+
new_upstream, argv[0]);
966+
if (refs_ref_exists(get_main_ref_store(the_repository),
967+
remote_ref.buf))
968+
die(_("branch '%s' does not exist.\n"
969+
"Did you mean to use: --set-upstream-to=%s/%s?"),
970+
branch->name, new_upstream, argv[0]);
971+
strbuf_release(&remote_ref);
972+
}
960973
die(_("branch '%s' does not exist"), branch->name);
961974
}
962975

t/t3200-branch.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,27 @@ test_expect_success '--set-upstream-to fails on a missing dst branch' '
10221022
test_cmp expect err
10231023
'
10241024

1025+
test_expect_success '--set-upstream-to suggests <remote>/<branch> on slip' '
1026+
test_when_finished "git remote remove upstream-remote" &&
1027+
test_config_global protocol.file.allow always &&
1028+
git init --bare slip-remote.git &&
1029+
git remote add upstream-remote slip-remote.git &&
1030+
git push upstream-remote main:slip-feature &&
1031+
git fetch upstream-remote &&
1032+
test_must_fail git rev-parse --verify refs/heads/slip-feature &&
1033+
test_must_fail git branch --set-upstream-to upstream-remote slip-feature 2>err &&
1034+
test_grep "Did you mean to use: --set-upstream-to=upstream-remote/slip-feature?" err
1035+
'
1036+
1037+
test_expect_success '--set-upstream-to does not suggest when no matching remote ref' '
1038+
test_when_finished "git remote remove upstream-remote" &&
1039+
git init --bare slip-remote2.git &&
1040+
git remote add upstream-remote slip-remote2.git &&
1041+
test_must_fail git branch --set-upstream-to upstream-remote no-such-branch 2>err &&
1042+
test_grep "branch ${SQ}no-such-branch${SQ} does not exist" err &&
1043+
test_grep ! "Did you mean" err
1044+
'
1045+
10251046
test_expect_success '--set-upstream-to fails on a missing src branch' '
10261047
test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
10271048
test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err

0 commit comments

Comments
 (0)