git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name
@ 2016-09-20 16:23 Steffen Nurpmeso
  2016-09-20 18:54 ` Bryan Turner
  0 siblings, 1 reply; 7+ messages in thread
From: Steffen Nurpmeso @ 2016-09-20 16:23 UTC (permalink / raw)
  To: git

Hello again,

yah, sorry, i'm back again..
I try to find a way to find the name of the current branch in an
automated way, because i need to ensure that a commit happens on
it and no other branch.  Now the problem arises that the commit
ref at the time of that commit maybe shared in between several
different branches, but no more thereafter, of course:

  ?0[steffen@wales ]$ git branch|grep '^*'
  * stable/v14.9
  ?0[steffen@wales ]$ git name-rev --name-only HEAD
  stable/v14.8

Is there another way except looking into .git/HEAD or using sed(1)
on the output of `branch' to find the right name?
Thank you.
Ciao!

--steffen

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

* Re: v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name
  2016-09-20 16:23 v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name Steffen Nurpmeso
@ 2016-09-20 18:54 ` Bryan Turner
  2016-09-20 19:29   ` Steffen Nurpmeso
  2016-09-21 13:43   ` Jakub Narębski
  0 siblings, 2 replies; 7+ messages in thread
From: Bryan Turner @ 2016-09-20 18:54 UTC (permalink / raw)
  To: Steffen Nurpmeso; +Cc: Git Users

On Tue, Sep 20, 2016 at 9:23 AM, Steffen Nurpmeso <steffen@sdaoden.eu> wrote:
> Hello again,
>
> yah, sorry, i'm back again..
> I try to find a way to find the name of the current branch in an
> automated way, because i need to ensure that a commit happens on
> it and no other branch.  Now the problem arises that the commit
> ref at the time of that commit maybe shared in between several
> different branches, but no more thereafter, of course:
>
>   ?0[steffen@wales ]$ git branch|grep '^*'
>   * stable/v14.9
>   ?0[steffen@wales ]$ git name-rev --name-only HEAD
>   stable/v14.8
>
> Is there another way except looking into .git/HEAD or using sed(1)
> on the output of `branch' to find the right name?

Have you tried "git symbolic-ref HEAD"?

$ git symbolic-ref HEAD
refs/heads/master

If you don't want the fully-qualified ref, you can add --short:

$ git symbolic-ref --short HEAD
master

> Thank you.
> Ciao!
>
> --steffen

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

* Re: v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name
  2016-09-20 18:54 ` Bryan Turner
@ 2016-09-20 19:29   ` Steffen Nurpmeso
  2016-09-21 13:43   ` Jakub Narębski
  1 sibling, 0 replies; 7+ messages in thread
From: Steffen Nurpmeso @ 2016-09-20 19:29 UTC (permalink / raw)
  To: Bryan Turner; +Cc: Git Users

Hey.

Bryan Turner <bturner@atlassian.com> wrote:
 |On Tue, Sep 20, 2016 at 9:23 AM, Steffen Nurpmeso <steffen@sdaoden.eu> \
 |wrote:
 |> yah, sorry, i'm back again..
 |> I try to find a way to find the name of the current branch in an
 |> automated way, because i need to ensure that a commit happens on
 |> it and no other branch.  Now the problem arises that the commit
 |> ref at the time of that commit maybe shared in between several
 |> different branches, but no more thereafter, of course:
 |>
 |>   ?0[steffen@wales ]$ git branch|grep '^*'
 |>   * stable/v14.9
 |>   ?0[steffen@wales ]$ git name-rev --name-only HEAD
 |>   stable/v14.8
 |>
 |> Is there another way except looking into .git/HEAD or using sed(1)
 |> on the output of `branch' to find the right name?
 |
 |Have you tried "git symbolic-ref HEAD"?

Not until now.

 |$ git symbolic-ref HEAD
 |refs/heads/master

Works.

 |If you don't want the fully-qualified ref, you can add --short:
 |
 |$ git symbolic-ref --short HEAD
 |master

Yep, works even better.  Fantastic.  Thank you.  And that command
was already existing when i have learned to use git(1), it is even
in the progit-09.markdown as of 2011-09-22 that i have laying
around.  Five years, and anything forgotten.  That is what you get
from working with blinkers on.  Shame!

Thank you Bryan.

--steffen

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

* Re: v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name
  2016-09-20 18:54 ` Bryan Turner
  2016-09-20 19:29   ` Steffen Nurpmeso
@ 2016-09-21 13:43   ` Jakub Narębski
  2016-09-21 14:21     ` Steffen Nurpmeso
  2016-09-21 16:37     ` Junio C Hamano
  1 sibling, 2 replies; 7+ messages in thread
From: Jakub Narębski @ 2016-09-21 13:43 UTC (permalink / raw)
  To: Bryan Turner, Steffen Nurpmeso; +Cc: Git Users

W dniu 20.09.2016 o 20:54, Bryan Turner pisze:
> On Tue, Sep 20, 2016 at 9:23 AM, Steffen Nurpmeso <steffen@sdaoden.eu> wrote:
>> Hello again,
>>
>> yah, sorry, i'm back again..
>> I try to find a way to find the name of the current branch in an
>> automated way, because i need to ensure that a commit happens on
>> it and no other branch.  Now the problem arises that the commit
>> ref at the time of that commit maybe shared in between several
>> different branches, but no more thereafter, of course:
>>
>>   ?0[steffen@wales ]$ git branch|grep '^*'
>>   * stable/v14.9

Not good, 'git branch' is a porcelain (user facing) command, so it
output may change; e.g. '*' could be replaced with '•'. For example
output for detached HEAD had changed!

>>   ?0[steffen@wales ]$ git name-rev --name-only HEAD
>>   stable/v14.8
>>
>> Is there another way except looking into .git/HEAD or using sed(1)
>> on the output of `branch' to find the right name?
> 
> Have you tried "git symbolic-ref HEAD"?
> 
> $ git symbolic-ref HEAD
> refs/heads/master
> 
> If you don't want the fully-qualified ref, you can add --short:
> 
> $ git symbolic-ref --short HEAD
> master

This does not work for detached HEAD, but perhaps you don't need
to worry about this.

  $ git rev-parse --symbolic-full-name HEAD
  refs/heads/master

But

  $ git checkout HEAD^0
  Note: checking out 'HEAD^0'.

  You are in 'detached HEAD' state. [...]
 
  $ git rev-parse --symbolic-full-name HEAD
  HEAD

  $ git symbolic-ref HEAD
  fatal: ref HEAD is not a symbolic ref

  $ git branch
  * (HEAD detached at 3e2ebf9)
    master

-- 
Jakub Narębski


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

* Re: v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name
  2016-09-21 13:43   ` Jakub Narębski
@ 2016-09-21 14:21     ` Steffen Nurpmeso
  2016-09-21 16:37     ` Junio C Hamano
  1 sibling, 0 replies; 7+ messages in thread
From: Steffen Nurpmeso @ 2016-09-21 14:21 UTC (permalink / raw)
  To: Jakub Narębski; +Cc: Bryan Turner, Git Users

Hello.

Jakub Narębski <jnareb@gmail.com> wrote:
 |W dniu 20.09.2016 o 20:54, Bryan Turner pisze:
 |> On Tue, Sep 20, 2016 at 9:23 AM, Steffen Nurpmeso <steffen@sdaoden.eu> \
 |> wrote:
 |>> Hello again,
 |>>
 |>> yah, sorry, i'm back again..
 |>> I try to find a way to find the name of the current branch in an
 |>> automated way, because i need to ensure that a commit happens on
 |>> it and no other branch.  Now the problem arises that the commit
 |>> ref at the time of that commit maybe shared in between several
 |>> different branches, but no more thereafter, of course:
 |>>
 |>>   ?0[steffen@wales ]$ git branch|grep '^*'
 |>>   * stable/v14.9
 |
 |Not good, 'git branch' is a porcelain (user facing) command, so it
 |output may change; e.g. '*' could be replaced with '•'. For example
 |output for detached HEAD had changed!

Ok.  I went the road Bryan suggested, i had only forgotten this.
Yes, it caused mysterious bugs once rev-parse reversed the output,
but i didn't understand the order at first, anyway.  With todays'
ever-rotating distributions i don't even try to keep up; currently
unthinkable to use the same release of an OS for five years, like
FreeBSD 5.3.  Well.

  ...
 |>> Is there another way except looking into .git/HEAD or using sed(1)
 |>> on the output of `branch' to find the right name?
 |> 
 |> Have you tried "git symbolic-ref HEAD"?
  ...
 |This does not work for detached HEAD, but perhaps you don't need
 |to worry about this.

No, not for me: it will only switch in between two different
stable/ which exist.  But thanks, just give it to me!

 |  $ git rev-parse --symbolic-full-name HEAD
 |  refs/heads/master

This is a really good suggestion, which i will remember.  I didn't
know this at all yet:

  $ git rev-parse --symbolic-full-name --abbrev-ref=strict HEAD

Seems to do exactly what i want, non-fragile, then.

 ...
 |But
 ...
 |  You are in 'detached HEAD' state. [...]
 | 
 |  $ git rev-parse --symbolic-full-name HEAD
 |  HEAD
 |
 |  $ git symbolic-ref HEAD
 |  fatal: ref HEAD is not a symbolic ref
 |
 |  $ git branch
 |  * (HEAD detached at 3e2ebf9)
 |    master

And name-rev gives "HEAD master~2" in a test of mine, or only
"master~2", or "undefined" if i use --tags, for completeness sake.

Thanks, Jakub.  I'm using the plumbing.

--steffen

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

* Re: v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name
  2016-09-21 13:43   ` Jakub Narębski
  2016-09-21 14:21     ` Steffen Nurpmeso
@ 2016-09-21 16:37     ` Junio C Hamano
  2016-09-21 17:54       ` Jakub Narębski
  1 sibling, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2016-09-21 16:37 UTC (permalink / raw)
  To: Jakub Narębski; +Cc: Bryan Turner, Steffen Nurpmeso, Git Users

Jakub Narębski <jnareb@gmail.com> writes:

>> Have you tried "git symbolic-ref HEAD"?
>> 
>> $ git symbolic-ref HEAD
>> refs/heads/master
>> 
>> If you don't want the fully-qualified ref, you can add --short:
>> 
>> $ git symbolic-ref --short HEAD
>> master
>
> This does not work for detached HEAD, but perhaps you don't need
> to worry about this.

I am not sure what you mean by "does not work".  Asking what ref
HEAD points at to symbolic-ref will tell you it does not point at
anything by exiting with non-zero status and that can be relied
upon.

Asking "symbolic-ref HEAD" has been the way how "git branch" and
other commands find out what branch is currently checked out for
almost eternity ("git symbolic-ref" appeared in Git v0.99.8).


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

* Re: v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name
  2016-09-21 16:37     ` Junio C Hamano
@ 2016-09-21 17:54       ` Jakub Narębski
  0 siblings, 0 replies; 7+ messages in thread
From: Jakub Narębski @ 2016-09-21 17:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Bryan Turner, Steffen Nurpmeso, Git Users

W dniu 21.09.2016 o 18:37, Junio C Hamano pisze:
> Jakub Narębski <jnareb@gmail.com> writes:
>>> Have you tried "git symbolic-ref HEAD"?
>>>
>>> $ git symbolic-ref HEAD
>>> refs/heads/master
>>>
>>> If you don't want the fully-qualified ref, you can add --short:
>>>
>>> $ git symbolic-ref --short HEAD
>>> master
>>
>> This does not work for detached HEAD, but perhaps you don't need
>> to worry about this.
> 
> I am not sure what you mean by "does not work".  Asking what ref
> HEAD points at to symbolic-ref will tell you it does not point at
> anything by exiting with non-zero status and that can be relied
> upon.
> 
> Asking "symbolic-ref HEAD" has been the way how "git branch" and
> other commands find out what branch is currently checked out for
> almost eternity ("git symbolic-ref" appeared in Git v0.99.8).

I'm sorry, I was wrong saying "does not work".  The problem is not
that it does not work, but that you need to take care of exit 
condition to correctly support detached HEAD state, and not end
with empty name for a branch, for example...

But if handled correctly, git-symbolic-ref is as good as git-rev-parse
as a plumbing to resolve current branch name.

Best,
-- 
Jakub Narębski


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

end of thread, other threads:[~2016-09-21 17:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-20 16:23 v2.9.3 and v2.10.0: `name-ref' HEAD gives wrong branch name Steffen Nurpmeso
2016-09-20 18:54 ` Bryan Turner
2016-09-20 19:29   ` Steffen Nurpmeso
2016-09-21 13:43   ` Jakub Narębski
2016-09-21 14:21     ` Steffen Nurpmeso
2016-09-21 16:37     ` Junio C Hamano
2016-09-21 17:54       ` Jakub Narębski

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).