git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* resolving a (possibly remote) branch HEAD to a hash
@ 2015-08-07  9:16 Perry Hutchison
  2015-08-07 17:24 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Perry Hutchison @ 2015-08-07  9:16 UTC (permalink / raw)
  To: git

Given the name of a branch, which might be in either refs/heads
or refs/remotes, how do I spell a query to obtain the HEAD commit
of the refs/heads instance if it exists, else of the refs/remotes
instance?

If the branch is local, I can get the hash of its HEAD commit using
git rev-parse:

  $ git rev-parse master
  23eba8be773637c1a995a3ffe3aeabe921edef76

However, if I try to do the same thing with a remote branch,
rev-parse barfs:

  $ git rev-parse r5.0.1
  r5.0.1
  fatal: ambiguous argument 'r5.0.1': unknown revision or path not in the working tree.
  Use '--' to separate paths from revisions

It works if I explicitly specify that I want the remote instance:

  $ git rev-parse origin/r5.0.1
  bb193a818fc984adbfd631951f3c89c16d5d3476

and the reference is, in fact, not ambiguous:

  $ git for-each-ref --format "%(refname)" | grep '/r5\.0\.1$'
  refs/remotes/origin/r5.0.1

Interestingly, master -- the one that works -- _is_ ambiguous:

  $ git for-each-ref --format "%(refname)" | grep '/master$'
  refs/heads/master
  refs/remotes/origin/master

Unfortunately grep is not a usable solution in general because
the branch name may contain . characters which grep (and egrep)
treat specially; nor is fgrep because it does _not_ treat $
specially; and I haven't found a for-each-ref pattern that will
select what I want.

One approach would be "git checkout r5.0.1" followed by
"git rev-parse HEAD", but I really don't want to perform a
checkout (e.g. the working tree is not necessarily clean).
I just want the same "resolve this reference" logic that
checkout clearly has to be using, but as a pure query that
does not change the state of either the repo or the working
tree.

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

end of thread, other threads:[~2015-08-10 18:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-07  9:16 resolving a (possibly remote) branch HEAD to a hash Perry Hutchison
2015-08-07 17:24 ` Junio C Hamano
2015-08-08  7:05   ` Perry Hutchison
2015-08-10 18:41     ` Junio C Hamano

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 NNTP newsgroup(s).