Subject: Re: [BUG] Incosistent repository state when trying to rename HEAD in the middle of a rebase

From: Kaartic Sivaraam <[email-masked]>
Date: Thu, 02 Nov 2017 11:03:34 +0530

I was able to spare some time to dig into this and found a few things.

First, it seems that the issue is more generic and the BUG kicks in
whenever HEAD is not a symbolic ref. I noticed that because when HEAD
is a symbolic ref there was a change in the error message shown by git.
In (2.11.0) I get this error message,

    error: refname refs/heads/HEAD not found
    fatal: Branch rename failed

while in the version build from 'next', I get the following error
message,

    error: refname HEAD is a symbolic ref, renaming it is not supported
    fatal: Branch rename failed

    That made me suspicious and I wanted to find where the error message
    got changed and bisected this which pointed to,

    9416812db (branch: forbid refs/heads/HEAD, 2017-10-13)

    This is the same commit which also causes the bug of allowing HEAD to
    be renamed when it is not a symbolic ref. I found a way to fix this but
    am not so sure if it's the right way to do this. (the diff patch is
    found at the end of this mail).

    One more observation I made was that without the patch at the end it is
    NOT possible to rename a branch named "HEAD" created using the older
    version!

    On Sat, 2017-10-28 at 22:28 +0530, Kaartic Sivaraam wrote:
    >     git rebase-i HEAD~

Small correction here. Now you could replace that with the simpler,

    git checkout HEAD^

diff --git a/sha1_name.c b/sha1_name.c
index c7c5ab376..4345e14c9 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -1334,7 +1334,13 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
        strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
        if (name[0] == '-')
                return -1;
+
        strbuf_splice(sb, 0, 0, "refs/heads/", 11);
+
+       /* HEAD is not to be used as a branch name */
+       if(!strcmp(sb->buf, "refs/heads/HEAD"))
+               return -1;
+
        return check_refname_format(sb->buf, 0);
 }

HTH,
Kaartic