All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Carlos Martín Nieto" <cmn@elego.de>
To: gitster@pobox.com
Cc: git@vger.kernel.org
Subject: [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use
Date: Thu, 23 Aug 2012 20:56:26 +0200	[thread overview]
Message-ID: <1345748186-1081-1-git-send-email-cmn@elego.de> (raw)
In-Reply-To: <7vk3wtjt3o.fsf@alter.siamese.dyndns.org>

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>
---

The 'track' in the message is still not great, but it does fit with
the one above. Maybe if we make it say "If youw wanted [...] track the
remote-tracking branch 'origin/master'" it would be clearer?

I've simplified and tightened the logic. Now it will only show the
undo message if the branch didn't exist locally and there is a
remote-tracking branch of the same name.

 builtin/branch.c  | 26 ++++++++++++++++++++++++++
 t/t3200-branch.sh | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/builtin/branch.c b/builtin/branch.c
index 08068f7..a0302a2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -877,10 +877,36 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		git_config_set_multivar(buf.buf, NULL, NULL, 1);
 		strbuf_release(&buf);
 	} else if (argc > 0 && argc <= 2) {
+		struct branch *branch = branch_get(argv[0]);
+		int branch_existed = 0, remote_tracking = 0;
+		struct strbuf buf = STRBUF_INIT;
+
 		if (kinds != REF_LOCAL_BRANCH)
 			die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
+
+		if (track == BRANCH_TRACK_OVERRIDE)
+			fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
+
+		strbuf_addf(&buf, "refs/remotes/%s", branch->name);
+		remote_tracking = ref_exists(buf.buf);
+		strbuf_release(&buf);
+
+		branch_existed = ref_exists(branch->refname);
 		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
 			      force_create, reflog, 0, quiet, track);
+
+		/*
+		 * We only show the instructions if the user gave us
+		 * one branch which doesn't exist locally, but is the
+		 * name of a remote-tracking branch.
+		 */
+		if (argc == 1 && track == BRANCH_TRACK_OVERRIDE &&
+		    !branch_existed && remote_tracking) {
+			fprintf(stderr, _("\nIf you wanted to make '%s' track '%s', do this:\n\n"), head, branch->name);
+			fprintf(stderr, _("    git branch -d %s\n"), branch->name);
+			fprintf(stderr, _("    git branch --set-upstream-to %s\n"), branch->name);
+		}
+
 	} else
 		usage_with_options(builtin_branch_usage, options);
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 93e5d6e..e9e11cf 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -399,6 +399,40 @@ test_expect_success 'test --unset-upstream on a particular branch' \
      test_must_fail git config branch.my14.remote &&
      test_must_fail git config branch.my14.merge'
 
+test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' \
+    'git update-ref refs/remotes/origin/master HEAD &&
+     git branch --set-upstream origin/master 2>actual &&
+     test_when_finished git update-ref -d refs/remotes/origin/master &&
+     test_when_finished git branch -d origin/master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+
+If you wanted to make '"'master'"' track '"'origin/master'"', do this:
+
+    git branch -d origin/master
+    git branch --set-upstream-to origin/master
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with two args only shows the deprecation message' \
+    'git branch --set-upstream master my13 2>actual &&
+     test_when_finished git branch --unset-upstream master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' \
+    'git branch --set-upstream my13 2>actual &&
+     test_when_finished git branch --unset-upstream master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+EOF
+     test_cmp expected actual
+'
+
 # Keep this test last, as it changes the current branch
 cat >expect <<EOF
 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000	branch: Created from master
-- 
1.7.12

  parent reply	other threads:[~2012-08-23 18:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-20 13:47 [PATCH 0/3] Improve branch UI for setting upstream information Carlos Martín Nieto
2012-08-20 13:47 ` [PATCH 1/3] branch: introduce --set-upstream-to Carlos Martín Nieto
2012-08-20 13:47 ` [PATCH 2/3] branch: add --unset-upstream option Carlos Martín Nieto
2012-08-23 21:20   ` Junio C Hamano
2012-08-27 17:30     ` Carlos Martín Nieto
2012-08-27 18:01       ` Junio C Hamano
2012-08-27 18:14         ` Junio C Hamano
2012-08-27 21:33           ` Carlos Martín Nieto
2012-08-20 13:47 ` [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch Carlos Martín Nieto
2012-08-20 18:50   ` Junio C Hamano
2012-08-22  1:26     ` Carlos Martín Nieto
2012-08-23 18:56     ` Carlos Martín Nieto [this message]
2012-08-23 21:16       ` [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1345748186-1081-1-git-send-email-cmn@elego.de \
    --to=cmn@elego.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.