Print Current Mercurial Revision Hash?

55,612

Solution 1

Try:

hg id -i

Example:

$ hg id -i
adc56745e928

Solution 2

hg --debug id -i

This will output the long hash, with a plus if there are uncommitted changes.

Solution 3

You can use --template with the parent command, I use this to get the long hash:

hg parent --template '{node}'

Solution 4

Summarising the answers and their responses, it seems that this is the best way to print the unique (not short form) identifier of the current version:

hg log -l 1 --template '{node}\n' -r .

Solution 5

hg log -l 1 --template '{node|short}\n'

See the docs, paragraphs "The basics of templating" and following.

Share:
55,612
David Peterson
Author by

David Peterson

Updated on August 23, 2022

Comments

  • David Peterson
    David Peterson almost 2 years

    Is there a better way extract the current revision hash in Mercurial than

    hg log -l1|grep changeset|cut -d: -f3

    ?

    Part of my webapp deployment script "tags" the uploaded app tarball with its unique revision hash.

    • waterproof
      waterproof over 9 years
      Note that hg log -l 1 gives you the most recent changeset, not necessarily the one you're currently updated to! The -f flag limits hg log output to ancestors of he current working directory, so hg log -f -l1 is closer to what you want.
  • Michael Ekstrand
    Michael Ekstrand about 13 years
    I wanted almost this, but with the long hash. Aliasing lid to log -l 1 --template '{node}\n' achieved exactly this - print the full revision ID.
  • Eiver
    Eiver over 12 years
    This prints the most recent pulled changeset. Our working directory could be updated to an older changeset. To print the changeset we are updated to, use "hg id". The only problem is, "hg id" does not support templates nor have an option to print long hash (unless someone knows how to do that).
  • Joseph Lisee
    Joseph Lisee about 12 years
    As Eiver said this does not print the actually revision in your working copy only the latest one in your repository. Because using this solution could introduce tracking errors I have down voted this solution.
  • Joseph Lisee
    Joseph Lisee about 12 years
    In case people miss the solutions below if you want the full hash use: hg --debug id -i if you want template support use hg parent --template '{node}' Do not use hg log -l 1, its the latest repository changeset, not the current working copy changeset.
  • Ry4an Brase
    Ry4an Brase about 12 years
    Using --debug in scripts isn't recommended, that output is less carefully controlled w/r/t backward compatibility. Use the template. If you want to do it w/ log then use . dot as the revision.
  • Joseph Lisee
    Joseph Lisee about 12 years
    Thanks Ry4an I had not idea about "-r ." referencing the current working copy revision. That seems like the better move then switching from "log" to "parent".
  • Ry4an Brase
    Ry4an Brase about 12 years
    No prob. hg help revisions and hg help revsets has some crazy powerful shortcuts like that.
  • AJP
    AJP about 10 years
    Ditto Joe's downvoting. Also @Eiver use hg --debug id -i as is said elsewhere.
  • nickd
    nickd about 9 years
    That seems to be the default in 3.1.2
  • Shelby Moore III
    Shelby Moore III over 8 years
    But it is not the most specific answer to the question, because afaik hg id -i prints only the short (12 characters) form of the global hash id and since hg identify lacks --template afaics there is no way to extract just the revision and nothing else since the man page says it prints a summary.
  • Shelby Moore III
    Shelby Moore III over 8 years
    Downvoting because of Ry4an's comment.
  • Shelby Moore III
    Shelby Moore III over 8 years
    The hg man page says hg parents is DEPRECATED, although perhaps that might not have been the case when you wrote this answer. If there is an uncommitted merge, there are two parent revisions.
  • Shelby Moore III
    Shelby Moore III over 8 years
    Note if there is an uncommitted merge, the . (dot) only displays the first parent of two parents of the working group.
  • Vincent
    Vincent almost 8 years
    Can someone tell us why hg parents is deprecated and what should we use instead?