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 <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Martín Nieto committed Aug 30, 2012 at 19:23 UTC b347d06bf097aca5effd07871adf4d0c8a7c55bd
2 files changed +60
builtin/branch.c
+26
@@ -881,10 +881,36 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
881 git_config_set_multivar(buf.buf, NULL, NULL, 1);
882 strbuf_release(&buf);
883 } 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 +
888 if (kinds != REF_LOCAL_BRANCH)
889 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);
899 create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
900 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 +
914 } else
915 usage_with_options(builtin_branch_usage, options);
916
t/t3200-branch.sh
+34
@@ -405,6 +405,40 @@ test_expect_success 'test --unset-upstream on a particular branch' \
405 test_must_fail git config branch.my14.remote &&
406 test_must_fail git config branch.my14.merge'
407
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 +
442 # Keep this test last, as it changes the current branch
443 cat >expect <<EOF
444 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master