Skip to content

Commit b347d06

Browse files
carlosmngitster
authored andcommitted
branch: deprecate --set-upstream and show help if we detect possible mistaken use
This interface is error prone, and a better one (--set-upstream-to) exists. Add a message listing the alternatives and suggest how to fix a --set-upstream invocation in case the user only gives one argument which causes a local branch with the same name as a remote-tracking one to be created. The typical case is git branch --set-upstream origin/master when the user meant git branch --set-upstream master origin/master assuming that the current branch is master. Show a message telling the user how to undo their action and get what they wanted. For the command above, the message would be The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to Branch origin/master set up to track local branch master. If you wanted to make 'master' track 'origin/master', do this: git branch -d origin/master git branch --set-upstream-to origin/master Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b84869e commit b347d06

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

builtin/branch.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,10 +881,36 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
881881
git_config_set_multivar(buf.buf, NULL, NULL, 1);
882882
strbuf_release(&buf);
883883
} else if (argc > 0 && argc <= 2) {
884+
struct branch *branch = branch_get(argv[0]);
885+
int branch_existed = 0, remote_tracking = 0;
886+
struct strbuf buf = STRBUF_INIT;
887+
884888
if (kinds != REF_LOCAL_BRANCH)
885889
die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
890+
891+
if (track == BRANCH_TRACK_OVERRIDE)
892+
fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
893+
894+
strbuf_addf(&buf, "refs/remotes/%s", branch->name);
895+
remote_tracking = ref_exists(buf.buf);
896+
strbuf_release(&buf);
897+
898+
branch_existed = ref_exists(branch->refname);
886899
create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
887900
force_create, reflog, 0, quiet, track);
901+
902+
/*
903+
* We only show the instructions if the user gave us
904+
* one branch which doesn't exist locally, but is the
905+
* name of a remote-tracking branch.
906+
*/
907+
if (argc == 1 && track == BRANCH_TRACK_OVERRIDE &&
908+
!branch_existed && remote_tracking) {
909+
fprintf(stderr, _("\nIf you wanted to make '%s' track '%s', do this:\n\n"), head, branch->name);
910+
fprintf(stderr, _(" git branch -d %s\n"), branch->name);
911+
fprintf(stderr, _(" git branch --set-upstream-to %s\n"), branch->name);
912+
}
913+
888914
} else
889915
usage_with_options(builtin_branch_usage, options);
890916

t/t3200-branch.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,40 @@ test_expect_success 'test --unset-upstream on a particular branch' \
405405
test_must_fail git config branch.my14.remote &&
406406
test_must_fail git config branch.my14.merge'
407407

408+
test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' \
409+
'git update-ref refs/remotes/origin/master HEAD &&
410+
git branch --set-upstream origin/master 2>actual &&
411+
test_when_finished git update-ref -d refs/remotes/origin/master &&
412+
test_when_finished git branch -d origin/master &&
413+
cat >expected <<EOF &&
414+
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
415+
416+
If you wanted to make '"'master'"' track '"'origin/master'"', do this:
417+
418+
git branch -d origin/master
419+
git branch --set-upstream-to origin/master
420+
EOF
421+
test_cmp expected actual
422+
'
423+
424+
test_expect_success '--set-upstream with two args only shows the deprecation message' \
425+
'git branch --set-upstream master my13 2>actual &&
426+
test_when_finished git branch --unset-upstream master &&
427+
cat >expected <<EOF &&
428+
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
429+
EOF
430+
test_cmp expected actual
431+
'
432+
433+
test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' \
434+
'git branch --set-upstream my13 2>actual &&
435+
test_when_finished git branch --unset-upstream my13 &&
436+
cat >expected <<EOF &&
437+
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
438+
EOF
439+
test_cmp expected actual
440+
'
441+
408442
# Keep this test last, as it changes the current branch
409443
cat >expect <<EOF
410444
$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master

0 commit comments

Comments
 (0)