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

Re: How to get the branch HEAD points to using a plumbing command?

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 5, 2021, 22:55 UTC
Message-ID
<xmqqy2h7xacv.fsf@gitster.c.googlers.com>
In-Reply-To
<132D030A-AAD3-4EFC-9858-7EA39503A746@gmail.com>
Utku <ugultopu@gmail.com> writes:
Show 9 quoted lines
> 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.

Previous: Jeff King
Message 4 of 4 in “How to get the branch HEAD points to using a plumbing command?”
  1. UtkuJan 5, 2021
  2. Ævar Arnfjörð BjarmasonJan 5, 2021
  3. Jeff KingJan 5, 2021
  4. Junio C HamanoJan 5, 2021

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.