git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] branch: make --set-upstream saner without an explicit starting point
@ 2012-07-05  9:29 Carlos Martín Nieto
  2012-07-05  9:42 ` Jeff King
  2012-07-05 17:03 ` Junio C Hamano
  0 siblings, 2 replies; 11+ messages in thread
From: Carlos Martín Nieto @ 2012-07-05  9:29 UTC (permalink / raw)
  To: git

The branch command assumes HEAD as the starting point if none is
specified. This causes --set-upstream to behave unexpectedly if the
user types

    git branch --set-upstream origin/master

git-branch will assume a second argument of HEAD and create config
entries for a local branch origin/master to track the current
branch. This is rarely, if ever, what the user wants to do.

Catch invocations with --set-upstream and only one branch so the
command above sets up the current branch to track origin's master
branch.

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

I got tired of --set-upstream biting me in the arse so I (presumably)
fixed it. I've only run the t3200 test for now. I'll check the rest of
the suite when I'm in front of a computer that's got some power, but I
don't expect other tests to be affected.

 builtin/branch.c  | 16 ++++++++++++++--
 t/t3200-branch.sh | 23 +++++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 0e060f2..6bbabda 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -853,10 +853,22 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		else
 			usage_with_options(builtin_branch_usage, options);
 	} else if (argc > 0 && argc <= 2) {
+		const char *branch, *upstream;
 		if (kinds != REF_LOCAL_BRANCH)
 			die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
-		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
-			      force_create, reflog, 0, quiet, track);
+
+		/* The usual way, make the branch point be HEAD of none is specified */
+		branch = argv[0];
+		upstream = (argc == 2) ? argv[1] : head;
+
+		/* If the command was 'git branch --set-upstream origin/master',
+		   make HEAD track origin/master, not the other way around */
+		if (track == BRANCH_TRACK_OVERRIDE && argc == 1) {
+			branch = head;
+			upstream = argv[0];
+		}
+
+		create_branch(head, branch, upstream, force_create, reflog, 0, quiet, track);
 	} else
 		usage_with_options(builtin_branch_usage, options);
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index a17f8b2..e06d642 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -369,6 +369,29 @@ test_expect_success \
     'git tag foobar &&
      test_must_fail git branch --track my11 foobar'
 
+test_expect_success 'set upstream with both branches explicit' \
+    'git config remote.local.url . &&
+     git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
+     (git show-ref -q refs/remotes/local/master || git fetch local) &&
+     git branch --no-track my12 &&
+     git branch --set-upstream my12 local/master &&
+     test $(git config branch.my12.remote) = local &&
+     test $(git config branch.my12.merge) = refs/heads/master'
+
+# The unsets at the end is to leave the master config as we found it,
+# so later tests don't get confused
+
+test_expect_success 'set upstream with implicit HEAD as branch to modify' \
+    'git config remote.local.url . &&
+     git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
+     (git show-ref -q refs/remotes/local/master || git fetch local) &&
+     git branch --set-upstream local/master &&
+     test $(git config branch.master.remote) = local &&
+     test $(git config branch.master.merge) = refs/heads/master
+     git config --unset branch.master.remote &&
+     git config --unset branch.master.merge
+'
+
 # 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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2012-08-16 21:59 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-05  9:29 [PATCH] branch: make --set-upstream saner without an explicit starting point Carlos Martín Nieto
2012-07-05  9:42 ` Jeff King
2012-07-05 16:34   ` Carlos Martín Nieto
2012-07-05 17:03 ` Junio C Hamano
2012-07-05 17:44   ` Junio C Hamano
2012-07-06  7:18     ` Carlos Martín Nieto
2012-07-06  7:29       ` Junio C Hamano
2012-07-06  8:03         ` Carlos Martín Nieto
2012-07-18  5:56           ` Junio C Hamano
2012-07-18 15:33             ` Carlos Martín Nieto
2012-08-16 21:58               ` Junio C Hamano

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