git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Carlos Martín Nieto" <cmn@elego.de>
To: git@vger.kernel.org
Subject: [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch
Date: Mon, 20 Aug 2012 15:47:40 +0200	[thread overview]
Message-ID: <1345470460-28734-4-git-send-email-cmn@elego.de> (raw)
In-Reply-To: <1345470460-28734-1-git-send-email-cmn@elego.de>

This interface is error prone, and a better one (--set-upstream-to)
exists. Suggest how to fix a --set-upstream invocation in case the
user only gives one argument, which makes it likely that he meant to
do the opposite, like with

    git branch --set-upstream origin/master

when they meant one of

    git branch --set-upstream origin/master master
    git branch --set-upstream-to origin/master

While we're at it, add a notice that the --set-upstream flag is
deprecated and will be removed at some point.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>

---

This produces suboptimal output in case that A tracks B and we do

    git checkout B
    git branch --set-upstream A

as it will suggest

    git branch --set-upstream A B

as a way of undoing what we just did. Avoiding it becomes a bit messy
(yet another layer of ifs), so I've left it out. Anybody reckon it's
worth recognising this?
---
 builtin/branch.c  | 35 +++++++++++++++++++++++++++++++++++
 t/t3200-branch.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)

diff --git a/builtin/branch.c b/builtin/branch.c
index 08068f7..33641d9 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -707,6 +707,21 @@ static int edit_branch_description(const char *branch_name)
 	return status;
 }
 
+static void print_set_upstream_warning(const char *branch, int branch_existed, const char *old_upstream)
+{
+	fprintf(stderr, _("If you wanted to make '%s' track '%s', do this:\n\n"), head, branch);
+	if (branch_existed) {
+		if (old_upstream)
+			fprintf(stderr, _("    git branch --set-upstream %s %s\n"), old_upstream, branch);
+		else
+			fprintf(stderr, _("    git branch --unset-upstream %s\n"), branch);
+	} else {
+		fprintf(stderr, _("    git branch -d %s\n"), branch);
+	}
+
+	fprintf(stderr, _("    git branch --set-upstream-to %s\n"), branch);
+}
+
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
 	int delete = 0, rename = 0, force_create = 0, list = 0;
@@ -877,10 +892,30 @@ 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]);
+		const char *old_upstream = NULL;
+		int branch_existed = 0;
+
 		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"));
+
+		/*
+		 * Save what argv[0] was pointing to so we can give
+		 * the --set-upstream-to hint
+		 */
+		if (branch_has_merge_config(branch))
+			old_upstream = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
+
+		branch_existed = ref_exists(branch->refname);
 		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
 			      force_create, reflog, 0, quiet, track);
+
+		if (argc == 1 && track == BRANCH_TRACK_OVERRIDE)
+			print_set_upstream_warning(argv[0], branch_existed, old_upstream);
+
 	} else
 		usage_with_options(builtin_branch_usage, options);
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 93e5d6e..702bffa 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -399,6 +399,58 @@ 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 'test --set-upstream help message with one arg' \
+    'git branch --set-upstream origin/master 2>actual &&
+     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 a branch that already has an upstream' \
+    'git branch --set-upstream-to my12 master &&
+     git branch --set-upstream-to my13 my12 &&
+     test_when_finished git branch --unset-upstream my12 &&
+     test_when_finished git branch --unset-upstream my13 &&
+     git branch --set-upstream my12 2>actual &&
+     cat actual &&
+     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 '"'my12'"', do this:
+
+    git branch --set-upstream my13 my12
+    git branch --set-upstream-to my12
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with a branch that has no upstream' \
+    'git branch --set-upstream my12 2>actual &&
+     test_when_finished git branch --unset-upstream my12
+     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 '"'my12'"', do this:
+
+    git branch --unset-upstream my12
+    git branch --set-upstream-to my12
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with two args should only show 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
+'
+
 # 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.11.1.104.ge7b44f1

  parent reply	other threads:[~2012-08-20 14:01 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 ` Carlos Martín Nieto [this message]
2012-08-20 18:50   ` [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch Junio C Hamano
2012-08-22  1:26     ` Carlos Martín Nieto
2012-08-23 18:56     ` [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use Carlos Martín Nieto
2012-08-23 21:16       ` 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=1345470460-28734-4-git-send-email-cmn@elego.de \
    --to=cmn@elego.de \
    --cc=git@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).