resolving a (possibly remote) branch HEAD to a hash

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]