git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* How to get the branch HEAD points to using a plumbing command?
@ 2021-01-05 15:09 Utku
  2021-01-05 15:24 ` Ævar Arnfjörð Bjarmason
  2021-01-05 22:55 ` Junio C Hamano
  0 siblings, 2 replies; 4+ messages in thread
From: Utku @ 2021-01-05 15:09 UTC (permalink / raw)
  To: git

If HEAD points to a branch, then `cat .git/HEAD` returns (at least on my Git version):

    ref: refs/heads/2.x

So, to understand which branch HEAD points to, I can:

- Read file contents of `.git/HEAD`.
- Try to match the contents with the regular expression `^ref: refs\/heads\/(.*)$`. If the capturing group is not empty, then HEAD points to the branch indicated by the capturing group. Otherwise, HEAD does not point to a branch.

To do this using only plumbing commands, I came up with:

- Run `git show-ref --head --heads`.
- Parse the output first by newlines, then by spaces and test if the commit hash of HEAD is present in other lines. If it is, then extract the branch name and return it. If not, return empty string.

My question is, is there a "better" (more idiomatic, less "expensive", etc.) way of doing it using only one plumbing command? This solution is OK but it is more work than just matching one line with a regular expression. The reason I want to do this using only plumbing commands is because I think that plumbing command interface would be more stable than the format (organization, structure) of files under the `.git/` directory across Git versions. Please correct me if I'm wrong in this idea as well.


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

* Re: How to get the branch HEAD points to using a plumbing command?
  2021-01-05 15:09 How to get the branch HEAD points to using a plumbing command? Utku
@ 2021-01-05 15:24 ` Ævar Arnfjörð Bjarmason
  2021-01-05 20:01   ` Jeff King
  2021-01-05 22:55 ` Junio C Hamano
  1 sibling, 1 reply; 4+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-01-05 15:24 UTC (permalink / raw)
  To: Utku; +Cc: git


On Tue, Jan 05 2021, Utku wrote:

> If HEAD points to a branch, then `cat .git/HEAD` returns (at least on my Git version):
>
>     ref: refs/heads/2.x
>
> So, to understand which branch HEAD points to, I can:
>
> - Read file contents of `.git/HEAD`.
> - Try to match the contents with the regular expression `^ref: refs\/heads\/(.*)$`. If the capturing group is not empty, then HEAD points to the branch indicated by the capturing group. Otherwise, HEAD does not point to a branch.
>
> To do this using only plumbing commands, I came up with:
>
> - Run `git show-ref --head --heads`.
> - Parse the output first by newlines, then by spaces and test if the commit hash of HEAD is present in other lines. If it is, then extract the branch name and return it. If not, return empty string.
>
> My question is, is there a "better" (more idiomatic, less "expensive", etc.) way of doing it using only one plumbing command? This solution is OK but it is more work than just matching one line with a regular expression. The reason I want to do this using only plumbing commands is because I think that plumbing command interface would be more stable than the format (organization, structure) of files under the `.git/` directory across Git versions. Please correct me if I'm wrong in this idea as well.

You'll want to use git-rev-parse for this, e.g.:

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

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

* Re: How to get the branch HEAD points to using a plumbing command?
  2021-01-05 15:24 ` Ævar Arnfjörð Bjarmason
@ 2021-01-05 20:01   ` Jeff King
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff King @ 2021-01-05 20:01 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: Utku, git

On Tue, Jan 05, 2021 at 04:24:56PM +0100, Ævar Arnfjörð Bjarmason wrote:

> > My question is, is there a "better" (more idiomatic, less
> > "expensive", etc.) way of doing it using only one plumbing command?
> > This solution is OK but it is more work than just matching one line
> > with a regular expression. The reason I want to do this using only
> > plumbing commands is because I think that plumbing command interface
> > would be more stable than the format (organization, structure) of
> > files under the `.git/` directory across Git versions. Please
> > correct me if I'm wrong in this idea as well.
> 
> You'll want to use git-rev-parse for this, e.g.:
> 
>     $ git rev-parse --symbolic-full-name HEAD
>     refs/heads/master

Another option is:

  git symbolic-ref HEAD

which has the benefit of working even when HEAD points to an unborn
branch. It also has a --short option to drop the refs/heads/ part.

It's also worth noting what you'd see on a detached HEAD. With rev-parse
you'll see "HEAD", but symbolic-ref will exit with a non-zero code.

-Peff

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

* Re: How to get the branch HEAD points to using a plumbing command?
  2021-01-05 15:09 How to get the branch HEAD points to using a plumbing command? Utku
  2021-01-05 15:24 ` Ævar Arnfjörð Bjarmason
@ 2021-01-05 22:55 ` Junio C Hamano
  1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2021-01-05 22:55 UTC (permalink / raw)
  To: Utku; +Cc: git

Utku <ugultopu@gmail.com> writes:

> My question is, is there a "better" (more idiomatic, less
> "expensive", etc.) way of doing it using only one plumbing
> command? This solution is OK but it is more work than just
> matching one line with a regular expression. The reason I want to
> do this using only plumbing commands is because I think that
> plumbing command interface would be more stable than the format
> (organization, structure) of files under the `.git/` directory
> across Git versions. Please correct me if I'm wrong in this idea
> as well.

The "HEAD" pointer can be a symbolic link, not a text file with
"ref: refs/heads/..." in it, and any approach that works directly
with "cat .git/HEAD" would be incorrect, unless it does a stat on
.git/HEAD first to see if it is a symlink.  And such implementation
details can change over time.

So you are right to seek a solution that hides such details from
you, i.e. a Git command, to do so.  It does not have to be plumbing,
though.

The orthodox answer would be to use "git symbolic-ref".  When you
want to learn which underlying ref a symbolic ref is pointing at,
that is the tool designed to answer that question.

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

end of thread, other threads:[~2021-01-05 22:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-05 15:09 How to get the branch HEAD points to using a plumbing command? Utku
2021-01-05 15:24 ` Ævar Arnfjörð Bjarmason
2021-01-05 20:01   ` Jeff King
2021-01-05 22:55 ` Junio C Hamano

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