threads / bug / 36569

BUG or FEATURE? Use of '/' in branch names

Subject: BUG or FEATURE? Use of '/' in branch names

## tl;dr

8 messages between May 2, 2014 and May 4, 2014.

replies: 7people: 6as markdown or json

Keith Derrick· May 2, 2014, 22:04 UTC · lore
According to https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html a '/' is character to use in a branch name.
git imposes the following rules on how references are named:
1. They can include slash / for hierarchical (directory) grouping, but no slash-separated component can begin with a dot . or end with the sequence .lock.
git-flow, for example,  uses it extensively to prefix branches with 'release/', 'bugfix/', 'hotfix/' etc.
However, I just ran into the following problem
    $git init
    Initialized empty Git repository in /home/keith/play/bug2/.git/
    $touch a
    $git add a
    $git commit -m "C1"
    [master (root-commit) d569d5b] C1
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a
    $git checkout -b hotfix
    Switched to a new branch 'hotfix'
    $git checkout -b hotfix/b2
    error: unable to resolve reference refs/heads/hotfix/b2: Not a directory
    fatal: Failed to lock ref for update: Not a directory
    $
The problem arises when a branch already exists with a name matching the stem of the new branch name.
As far as I can see, this comes from the use of the branch name to create a directory under .git/refs/heads with the name 'hotfix/b2' because .git/refs/heads/hotfix already exists as a plain file.
Note, however that this works
    $git init
    Initialized empty Git repository in /home/keith/play/bug3/.git/
    $touch a
    $git add a && git commit -m 'C1'
    [master (root-commit) 304052c] C1
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a
    $git checkout -b hotfix/b1
    Switched to a new branch 'hotfix/b1'
    $git checkout -b hotfix/b2
    Switched to a new branch 'hotfix/b2'
    $ls .git/refs/heads/ -R
    .git/refs/heads/:
    hotfix  master
    .git/refs/heads/hotfix:
    b1  b2
    $
But, for the reverse reason, I can't now create the branch named 'hotfix'
I can see the value in grouping branches in a directory tree under refs/heads, but wouldn't it make more sense to simply escape the '/' in the branch name so that 'hotfix/b1' is stored on disk as 'hotfix\/b1'?
I found this when trying to document a branching workflow for support branches. The repositories already had branches such as 'release1', 'release2' and I wanted to add branches such as 'release1/develop', 'release2/develop', 'release1/staging', 'release2/staging' etc.
Renaming the existing published branches is not an option for us, I'm afraid.
Felipe Contreras· May 2, 2014, 22:10 UTC · re: Keith Derrick · lore

RE: BUG or FEATURE? Use of '/' in branch names

Keith Derrick wrote:
> I can see the value in grouping branches in a directory tree under
> refs/heads, but wouldn't it make more sense to simply escape the '/'
> in the branch name so that 'hotfix/b1' is stored on disk as
> 'hotfix\/b1'?

This would be nice for remote helpers: Mercurial can have "hotfix" and "hotifx/b1", so importing such a Mercurial repository creates problems.

-- 
Felipe Contreras
Junio C Hamano· May 2, 2014, 22:16 UTC · re: Keith Derrick · lore

Re: BUG or FEATURE? Use of '/' in branch names

Keith Derrick <keith.derrick@lge.com> writes:
> The problem arises when a branch already exists with a name
> matching the stem of the new branch name.
> ...
> But, for the reverse reason, I can't now create the branch named 'hotfix'

All correct. Allowing '/' in branch names came about not with a careful design but was done by a happy accident, and we accepted it under the condition "as long as users know that they cannot have branches D and D/F at the same time, that is fine".

An obvious alternative convention you can adopt would be to use not '/' but some other separating characters (e.g. "_") as your hierarchy delimiter, if you must have D and D_F at the same time.

Jonathan Nieder· May 2, 2014, 22:16 UTC · re: Keith Derrick · lore

Re: BUG or FEATURE? Use of '/' in branch names

Hi Keith,
Keith Derrick wrote:
Show 6 quoted lines
>     $ git checkout -b hotfix
>     Switched to a new branch 'hotfix'
>     $ git checkout -b hotfix/b2
>     error: unable to resolve reference refs/heads/hotfix/b2: Not a directory
>     fatal: Failed to lock ref for update: Not a directory
>     $
That's an ugly message.  I think we can do better. (hint hint)

Longer term, I think people would like to make it possible for a 'hotfix' and 'hotfix/b2' branch to coexist, but that will take some work, and until then, git tries to be careful about enforcing the constraint that they cannot coexist.

Fixing it would be complicated by the need to avoid breaking people with older versions of git when they fetch from you (what happens to the origin/hotfix and origin/hotfix/b2 remote-tracking refs on the client side?).

Thanks and hope that helps, Jonathan

Keith Derrick· May 2, 2014, 22:36 UTC · re: Jonathan Nieder · lore

Re: BUG or FEATURE? Use of '/' in branch names

Yes, I've since found some discussion on this, and had already changed to use '-' to append the classifier.

But the other problem is that I can't easily find this restriction documented anywhere - which means it comes as a suprise to people.

As it stands, the documentation implies that what I tried should work. In which case, how it's been *implemented* seems to be breaking the promise of the functional specification (if you view the documentation as such).

Keith
On 05/02/2014 03:16 PM, Jonathan Nieder wrote:
Show 24 quoted lines
> Hi Keith,
>
> Keith Derrick wrote:
>
>>      $ git checkout -b hotfix
>>      Switched to a new branch 'hotfix'
>>      $ git checkout -b hotfix/b2
>>      error: unable to resolve reference refs/heads/hotfix/b2: Not a directory
>>      fatal: Failed to lock ref for update: Not a directory
>>      $
> That's an ugly message.  I think we can do better. (hint hint)
>
> Longer term, I think people would like to make it possible for a
> 'hotfix' and 'hotfix/b2' branch to coexist, but that will take some
> work, and until then, git tries to be careful about enforcing the
> constraint that they cannot coexist.
>
> Fixing it would be complicated by the need to avoid breaking people
> with older versions of git when they fetch from you (what happens to
> the origin/hotfix and origin/hotfix/b2 remote-tracking refs on the
> client side?).
>
> Thanks and hope that helps,
> Jonathan
Jonathan Nieder· May 2, 2014, 22:43 UTC · re: Keith Derrick · lore

Re: BUG or FEATURE? Use of '/' in branch names

Hi,
Keith Derrick wrote:
Show 5 quoted lines
> Yes, I've since found some discussion on this, and had already changed 
> to use '-' to append the classifier.
>
> But the other problem is that I can't easily find this restriction 
> documented anywhere - which means it comes as a suprise to people.

That sounds like another serious bug (the first one was the lousy error message). The current behavior is intended, and it sounds like the documentation is lagging.

Where did you expect to find information about this? Knowing that would help a lot in fixing it.

(The nicest way to indicate where you expected to read about this is with a patch against the relevant file in Documentation/*.txt, of course.)

Thanks again, Jonathan

Dennis Kaarsemaker· May 3, 2014, 07:35 UTC · re: Jonathan Nieder · lore

Re: BUG or FEATURE? Use of '/' in branch names

On vr, 2014-05-02 at 15:16 -0700, Jonathan Nieder wrote:
Show 7 quoted lines
> >     $ git checkout -b hotfix/b2
> >     error: unable to resolve reference refs/heads/hotfix/b2: Not a
> directory
> >     fatal: Failed to lock ref for update: Not a directory
> >     $
> 
> That's an ugly message.  I think we can do better. (hint hint)
2.0.0-rc2 has a better message already:

$ git checkout -b hotfix/b2 error: 'refs/heads/hotfix' exists; cannot create 'refs/heads/hotfix/b2' fatal: Failed to lock ref for update: Not a directory

-- 
Dennis Kaarsemaker
www.kaarsemaker.net
Michael Haggerty· May 4, 2014, 05:39 UTC · re: Dennis Kaarsemaker · lore

Re: BUG or FEATURE? Use of '/' in branch names

On 05/03/2014 09:35 AM, Dennis Kaarsemaker wrote:
Show 14 quoted lines
> On vr, 2014-05-02 at 15:16 -0700, Jonathan Nieder wrote:
>>>     $ git checkout -b hotfix/b2
>>>     error: unable to resolve reference refs/heads/hotfix/b2: Not a
>> directory
>>>     fatal: Failed to lock ref for update: Not a directory
>>>     $
>>
>> That's an ugly message.  I think we can do better. (hint hint)
> 
> 2.0.0-rc2 has a better message already:
> 
> $ git checkout -b hotfix/b2
> error: 'refs/heads/hotfix' exists; cannot create 'refs/heads/hotfix/b2'
> fatal: Failed to lock ref for update: Not a directory

I was trying to remember when this was changed, but at first I couldn't reproduce the "fixed" error message at all. Finally I figured out that the the error message that you get depends on whether the existing reference is loose:

    $ bin-wrappers/git checkout -b master/foo
    error: unable to resolve reference refs/heads/master/foo: Not a
directory
    fatal: Failed to lock ref for update: Not a directory
vs. packed:
    $ bin-wrappers/git checkout -b base/foo
    error: 'refs/heads/base' exists; cannot create 'refs/heads/base/foo'
    fatal: Failed to lock ref for update: Not a directory

It would be good to make the error message uniform and to document this restriction.

Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

← back to recent threads