git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git branch -M" regression in 1.7.7?

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Nov 26, 2011, 02:30 UTC
Message-ID
<20111126023002.GA17652@elie.hsd1.il.comcast.net>
In-Reply-To
<CALxtSbRbwkVDKJcXiKY9rHYCjA3XGgCytbXQnRhQvbEnY8SpjA@mail.gmail.com>
Hi,
Josh Chia (谢任中) wrote:
Show 7 quoted lines
> On git 1.7.7.3, when I try to "git branch -M master" when I'm already
> on a branch 'master', I get this error message:
> Cannot force update the current branch
>
> On 1.7.6.4, the command succeeds.
>
> Is this intended?
Yes, but it probably wasn't a good idea.  How about this patch?

A reproduction recipe (preferrably in the form of a patch to t/t3200-branch.sh would be welcome.

-- >8 --
Subject: treat "git branch -M master master" as a no-op again

Before v1.7.7-rc2~1^2~2 (Prevent force-updating of the current branch, 2011-08-20), commands like "git branch -M topic master" could be used even when "master" was the current branch, with the somewhat counterintuitive result that HEAD would point to some place new while the index and worktree kept the content of the old commit. This is not a very sensible operation and the result is what almost nobody would expect, so erroring out in this case was a good change.

However, there is one exception to the "it's usually not obvious what it would mean to overwrite the current branch by another one" rule. Namely:

	git branch -M master master

is clearly meant to be a no-op, even if you are on the master branch. And in the latter case, it can be abbreviated:

	git branch -M master

This seems like a valuable exception to allow, because then "git branch -M foo" would _always_ be allowed --- either 'foo' is not the current branch, and it does the obvious thing, or 'foo' is the current branch, and nothing happens.

Buildbot uses this idiom and was broken in 1.7.7 (it would emit the message "Cannot force update the current branch").

Reported-by: Soeren Sonnenburg <sonne@debian.org>
Reported-by: Josh Chia (谢任中)
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 builtin/branch.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 51ca6a02..24f33b24 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -568,6 +568,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 	unsigned char sha1[20];
 	struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
 	int recovery = 0;
+	int clobber_head_ok;
 
 	if (!oldname)
 		die(_("cannot rename the current branch while not on any."));
@@ -583,7 +584,13 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 			die(_("Invalid branch name: '%s'"), oldname);
 	}
 
-	validate_new_branchname(newname, &newref, force, 0);
+	/*
+	 * A command like "git branch -M currentbranch currentbranch" cannot
+	 * cause the worktree to become inconsistent with HEAD, so allow it.
+	 */
+	clobber_head_ok = !strcmp(oldname, newname);
+
+	validate_new_branchname(newname, &newref, force, clobber_head_ok);
 
 	strbuf_addf(&logmsg, "Branch: renamed %s to %s",
 		 oldref.buf, newref.buf);
-- 
1.7.8.rc3
Previous: ☂Josh Chia (谢任中)Next: Conrad Irwin
Message 2 of 9 in “git branch -M" regression in 1.7.7?”
  1. ☂Josh Chia (谢任中)Nov 26, 2011
  2. Jonathan NiederNov 26, 2011
  3. Test renaming a branch to itselfConrad Irwin, Nov 26, 2011
  4. Jonathan NiederNov 26, 2011
  5. Conrad IrwinNov 26, 2011
  6. Jonathan NiederNov 26, 2011
  7. Junio C HamanoNov 26, 2011
  8. Andreas SchwabNov 26, 2011
  9. Junio C HamanoNov 28, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.