git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* git branch --set-upstream regression in master
@ 2011-09-16 21:43 Jay Soffian
  2011-09-16 22:58 ` Junio C Hamano
  2011-09-16 23:52 ` Junio C Hamano
  0 siblings, 2 replies; 5+ messages in thread
From: Jay Soffian @ 2011-09-16 21:43 UTC (permalink / raw)
  To: git, Junio C Hamano, conrad.irwin

This used to be possible on the checked out branch:

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

Now it gives "fatal: Cannot force update the current branch." which is
broken. You should be able to setup/change the tracking information on
the checked out branch.

It's apparently due to ci/forbid-unwanted-current-branch-update.

Sorry I don't have time to contribute a patch at the moment.

(BTW, --set-upstream still needs to be fixed so that these mean the same thing:

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

and to just allow:

  $ git branch --set-upstream origin/master

w/o having to specify the checked-out branch.)

j.

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

* Re: git branch --set-upstream regression in master
  2011-09-16 21:43 git branch --set-upstream regression in master Jay Soffian
@ 2011-09-16 22:58 ` Junio C Hamano
  2011-09-16 23:14   ` Junio C Hamano
  2011-09-16 23:52 ` Junio C Hamano
  1 sibling, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2011-09-16 22:58 UTC (permalink / raw)
  To: Jay Soffian; +Cc: git, conrad.irwin

Jay Soffian <jaysoffian@gmail.com> writes:

> This used to be possible on the checked out branch:
>
>   $ git branch master --set-upstream origin/master
>
> Now it gives "fatal: Cannot force update the current branch." which is
> broken. You should be able to setup/change the tracking information on
> the checked out branch.
>
> It's apparently due to ci/forbid-unwanted-current-branch-update.

Does

	git branch --set-upstream master origin/master

work? If so then I wouldn't worry too much about it (your "arg then
option" should be forbidden in the longer term anyway). If not, we would
need to patch it.

> (BTW, --set-upstream still needs to be fixed so that these mean the same
> thing:
>
>   $ git branch master --set-upstream origin/master
>   $ git branch --set-upstream origin/master master

If we are doing anythning, I think it needs to be fixed not to allow the
former, period.

> .. to just allow:
>
>   $ git branch --set-upstream origin/master
>
> w/o having to specify the checked-out branch.

That may be a nice feature enhancement, post 1.7.7 release.

I took a brief look at --set-upstream codepath, and I have to say that the
implementation is totally broken with respect to an existing branch.

Given

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

it passes the exact same codepath as

	$ git branch master origin/master

uses, only with a different "track" flag, no?  That is, it calls a
function that is meant to _create_ branch "master" from given branch point
"origin/master", namely create_branch().  And then create_branch(),
contrary to its name, is littered with "dont_change_ref" special case to
work it around, depending on the value of "track".

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

* Re: git branch --set-upstream regression in master
  2011-09-16 22:58 ` Junio C Hamano
@ 2011-09-16 23:14   ` Junio C Hamano
  0 siblings, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2011-09-16 23:14 UTC (permalink / raw)
  To: Jay Soffian; +Cc: git, conrad.irwin

Junio C Hamano <gitster@pobox.com> writes:

> I took a brief look at --set-upstream codepath, and I have to say that the
> implementation is totally broken with respect to an existing branch.
>
> Given
>
> 	$ git branch master --set-upstream origin/master
>
> it passes the exact same codepath as
>
> 	$ git branch master origin/master
>
> uses, only with a different "track" flag, no?  That is, it calls a
> function that is meant to _create_ branch "master" from given branch point
> "origin/master", namely create_branch().  And then create_branch(),
> contrary to its name, is littered with "dont_change_ref" special case to
> work it around, depending on the value of "track".

So here is a quick-and-dirty patch, which may or may not compile or pass
tests.

 branch.c           |   21 ++++++++++++---------
 branch.h           |   12 +++++++++++-
 builtin/branch.c   |    2 +-
 builtin/checkout.c |    3 ++-
 4 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/branch.c b/branch.c
index 478d825..fecedd3 100644
--- a/branch.c
+++ b/branch.c
@@ -135,23 +135,25 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
 	return 0;
 }
 
-int validate_new_branchname(const char *name, struct strbuf *ref, int force)
+int validate_new_branchname(const char *name, struct strbuf *ref,
+			    int force, int attr_only)
 {
-	const char *head;
-	unsigned char sha1[20];
-
 	if (strbuf_check_branch_ref(ref, name))
 		die("'%s' is not a valid branch name.", name);
 
 	if (!ref_exists(ref->buf))
 		return 0;
-	else if (!force)
+	else if (!force && !attr_only)
 		die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
 
-	head = resolve_ref("HEAD", sha1, 0, NULL);
-	if (!is_bare_repository() && head && !strcmp(head, ref->buf))
-		die("Cannot force update the current branch.");
+	if (!attr_only) {
+		const char *head;
+		unsigned char sha1[20];
 
+		head = resolve_ref("HEAD", sha1, 0, NULL);
+		if (!is_bare_repository() && head && !strcmp(head, ref->buf))
+			die("Cannot force update the current branch.");
+	}
 	return 1;
 }
 
@@ -171,7 +173,8 @@ void create_branch(const char *head,
 	if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
 		explicit_tracking = 1;
 
-	if (validate_new_branchname(name, &ref, force || track == BRANCH_TRACK_OVERRIDE)) {
+	if (validate_new_branchname(name, &ref, force,
+				    track == BRANCH_TRACK_OVERRIDE)) {
 		if (!force)
 			dont_change_ref = 1;
 		else
diff --git a/branch.h b/branch.h
index 01544e2..1285158 100644
--- a/branch.h
+++ b/branch.h
@@ -20,8 +20,18 @@ void create_branch(const char *head, const char *name, const char *start_name,
  * interpreted ref in ref, force indicates whether (non-head) branches
  * may be overwritten. A non-zero return value indicates that the force
  * parameter was non-zero and the branch already exists.
+ *
+ * Contrary to all of the above, when attr_only is 1, the caller is
+ * not interested in verifying if it is Ok to update the named
+ * branch to point at a potentially different commit. It is merely
+ * asking if it is OK to change some attribute for the named branch
+ * (e.g. tracking upstream).
+ *
+ * NEEDSWORK: This needs to be split into two separate functions in the
+ * longer run for sanity.
+ *
  */
-int validate_new_branchname(const char *name, struct strbuf *ref, int force);
+int validate_new_branchname(const char *name, struct strbuf *ref, int force, int attr_only);
 
 /*
  * Remove information about the state of working on the current
diff --git a/builtin/branch.c b/builtin/branch.c
index aa705a0..f49596f 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -566,7 +566,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 			die(_("Invalid branch name: '%s'"), oldname);
 	}
 
-	validate_new_branchname(newname, &newref, force);
+	validate_new_branchname(newname, &newref, force, 0);
 
 	strbuf_addf(&logmsg, "Branch: renamed %s to %s",
 		 oldref.buf, newref.buf);
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 3bb6525..5e356a6 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1073,7 +1073,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (opts.new_branch) {
 		struct strbuf buf = STRBUF_INIT;
 
-		opts.branch_exists = validate_new_branchname(opts.new_branch, &buf, !!opts.new_branch_force);
+		opts.branch_exists = validate_new_branchname(opts.new_branch, &buf,
+							     !!opts.new_branch_force, 0);
 
 		strbuf_release(&buf);
 	}

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

* Re: git branch --set-upstream regression in master
  2011-09-16 21:43 git branch --set-upstream regression in master Jay Soffian
  2011-09-16 22:58 ` Junio C Hamano
@ 2011-09-16 23:52 ` Junio C Hamano
  2011-09-17  0:19   ` Jay Soffian
  1 sibling, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2011-09-16 23:52 UTC (permalink / raw)
  To: Jay Soffian; +Cc: git, conrad.irwin

Jay Soffian <jaysoffian@gmail.com> writes:

> This used to be possible on the checked out branch:
>
>   $ git branch --set-upstream origin/master master
>
> Now it gives "fatal: Cannot force update the current branch."
> broken.

Let's do this. I would be happy if I can include it in -rc2 with Tested-by:
or something ;-)

-- >8 --
Subject: [PATCH] branch --set-upstream: regression fix

The "git branch" command, while not in listing mode, calls create_branch()
even when the target branch already exists, and it does so even when it is
not interested in updating the value of the branch (i.e. the name of the
commit object that sits at the tip of the existing branch). This happens
when the command is run with "--set-upstream" option.

The earlier safety-measure to prevent "git branch -f $branch $commit" from
updating the currently checked out branch did not take it into account,
and we no longer can update the tracking information of the current branch.

Minimally fix this regression by telling the validation code if it is
called to really update the value of a potentially existing branch, or if
the caller merely is interested in updating auxiliary aspects of a branch.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 branch.c           |   21 ++++++++++++---------
 branch.h           |   12 +++++++++++-
 builtin/branch.c   |    2 +-
 builtin/checkout.c |    3 ++-
 t/t3200-branch.sh  |   13 +++++++++++++
 5 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/branch.c b/branch.c
index 1fe3078..4338a90 100644
--- a/branch.c
+++ b/branch.c
@@ -135,23 +135,25 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
 	return 0;
 }
 
-int validate_new_branchname(const char *name, struct strbuf *ref, int force)
+int validate_new_branchname(const char *name, struct strbuf *ref,
+			    int force, int attr_only)
 {
-	const char *head;
-	unsigned char sha1[20];
-
 	if (strbuf_check_branch_ref(ref, name))
 		die("'%s' is not a valid branch name.", name);
 
 	if (!ref_exists(ref->buf))
 		return 0;
-	else if (!force)
+	else if (!force && !attr_only)
 		die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
 
-	head = resolve_ref("HEAD", sha1, 0, NULL);
-	if (!is_bare_repository() && head && !strcmp(head, ref->buf))
-		die("Cannot force update the current branch.");
+	if (!attr_only) {
+		const char *head;
+		unsigned char sha1[20];
 
+		head = resolve_ref("HEAD", sha1, 0, NULL);
+		if (!is_bare_repository() && head && !strcmp(head, ref->buf))
+			die("Cannot force update the current branch.");
+	}
 	return 1;
 }
 
@@ -171,7 +173,8 @@ void create_branch(const char *head,
 	if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
 		explicit_tracking = 1;
 
-	if (validate_new_branchname(name, &ref, force || track == BRANCH_TRACK_OVERRIDE)) {
+	if (validate_new_branchname(name, &ref, force,
+				    track == BRANCH_TRACK_OVERRIDE)) {
 		if (!force)
 			dont_change_ref = 1;
 		else
diff --git a/branch.h b/branch.h
index 01544e2..1285158 100644
--- a/branch.h
+++ b/branch.h
@@ -20,8 +20,18 @@ void create_branch(const char *head, const char *name, const char *start_name,
  * interpreted ref in ref, force indicates whether (non-head) branches
  * may be overwritten. A non-zero return value indicates that the force
  * parameter was non-zero and the branch already exists.
+ *
+ * Contrary to all of the above, when attr_only is 1, the caller is
+ * not interested in verifying if it is Ok to update the named
+ * branch to point at a potentially different commit. It is merely
+ * asking if it is OK to change some attribute for the named branch
+ * (e.g. tracking upstream).
+ *
+ * NEEDSWORK: This needs to be split into two separate functions in the
+ * longer run for sanity.
+ *
  */
-int validate_new_branchname(const char *name, struct strbuf *ref, int force);
+int validate_new_branchname(const char *name, struct strbuf *ref, int force, int attr_only);
 
 /*
  * Remove information about the state of working on the current
diff --git a/builtin/branch.c b/builtin/branch.c
index 40f885c..5fb3d85 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -566,7 +566,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 			die(_("Invalid branch name: '%s'"), oldname);
 	}
 
-	validate_new_branchname(newname, &newref, force);
+	validate_new_branchname(newname, &newref, force, 0);
 
 	strbuf_addf(&logmsg, "Branch: renamed %s to %s",
 		 oldref.buf, newref.buf);
diff --git a/builtin/checkout.c b/builtin/checkout.c
index ddefec0..909a334 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1072,7 +1072,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (opts.new_branch) {
 		struct strbuf buf = STRBUF_INIT;
 
-		opts.branch_exists = validate_new_branchname(opts.new_branch, &buf, !!opts.new_branch_force);
+		opts.branch_exists = validate_new_branchname(opts.new_branch, &buf,
+							     !!opts.new_branch_force, 0);
 
 		strbuf_release(&buf);
 	}
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index cb6458d..7633930 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -554,4 +554,17 @@ test_expect_success 'attempt to delete a branch merged to its base' '
 	test_must_fail git branch -d my10
 '
 
+test_expect_success 'use set-upstream on the current branch' '
+	git checkout master &&
+	git --bare init myupstream.git &&
+	git push myupstream.git master:refs/heads/frotz &&
+	git remote add origin myupstream.git &&
+	git fetch &&
+	git branch --set-upstream master origin/frotz &&
+
+	test "z$(git config branch.master.remote)" = "zorigin" &&
+	test "z$(git config branch.master.merge)" = "zrefs/heads/frotz"
+
+'
+
 test_done
-- 
1.7.7.rc1.4.g26e426

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

* Re: git branch --set-upstream regression in master
  2011-09-16 23:52 ` Junio C Hamano
@ 2011-09-17  0:19   ` Jay Soffian
  0 siblings, 0 replies; 5+ messages in thread
From: Jay Soffian @ 2011-09-17  0:19 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, conrad.irwin

On Fri, Sep 16, 2011 at 7:52 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Jay Soffian <jaysoffian@gmail.com> writes:
>
>> This used to be possible on the checked out branch:
>>
>>   $ git branch --set-upstream origin/master master
>>
>> Now it gives "fatal: Cannot force update the current branch."
>> broken.
>
> Let's do this. I would be happy if I can include it in -rc2 with Tested-by:
> or something ;-)

Tested-by: Jay Soffian

Thanks for the quick turnaround. I'll try to find some time to make
--set-upstream a proper option for post 1.7.7.

j.

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

end of thread, other threads:[~2011-09-17  0:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-16 21:43 git branch --set-upstream regression in master Jay Soffian
2011-09-16 22:58 ` Junio C Hamano
2011-09-16 23:14   ` Junio C Hamano
2011-09-16 23:52 ` Junio C Hamano
2011-09-17  0:19   ` Jay Soffian

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