137

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.

rentzsch
  • 3,548
  • 4
  • 27
  • 39
  • 2
    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. – waterproof Jan 13 '15 at 19:55

8 Answers8

210

Try:

hg id -i

Example:

$ hg id -i
adc56745e928
Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • 25
    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. – Joseph Lisee Jun 11 '12 at 17:33
  • 7
    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. – Ry4an Brase Jun 12 '12 at 18:07
  • 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". – Joseph Lisee Jun 13 '12 at 15:52
  • No prob. `hg help revisions` and `hg help revsets` has some crazy powerful shortcuts like that. – Ry4an Brase Jun 13 '12 at 20:47
  • 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](https://www.selenic.com/mercurial/hg.1.html#identify) it prints a summary. – Shelby Moore III Nov 26 '15 at 14:16
47
hg --debug id -i

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

Frank
  • 495
  • 4
  • 2
24

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

hg parent --template '{node}'
shadowspawn
  • 3,039
  • 22
  • 26
  • The hg man page says `hg parents` is [DEPRECATED](https://www.selenic.com/mercurial/hg.1.html#parents), 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](https://www.mercurial-scm.org/wiki/UnderstandingMercurial#line-157). – Shelby Moore III Nov 26 '15 at 16:11
  • 2
    Can someone tell us why hg parents is deprecated and what should we use instead? – Vincent Sep 23 '16 at 19:44
17

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 .
rog
  • 6,252
  • 4
  • 31
  • 25
  • Note if there is an [uncommitted merge](https://www.mercurial-scm.org/wiki/UnderstandingMercurial#line-157), the `.` (dot) only displays the [first parent](https://www.selenic.com/hg/help/revisions) of two parents of the working group. – Shelby Moore III Nov 26 '15 at 16:20
10
hg log -l 1 --template '{node|short}\n'

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

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 2
    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. – Michael Ekstrand Apr 21 '11 at 14:13
  • 6
    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). – Eiver Nov 28 '11 at 11:13
  • 1
    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 Jun 11 '12 at 17:30
  • 1
    Ditto Joe's downvoting. Also @Eiver use `hg --debug id -i` as is said elsewhere. – AJP Jun 05 '14 at 10:09
5

The most specific non-DEPRECATED command which due to the presence of --template can print only revision information if that conciseness is required (as implied by the question):

hg log -l 1 -b . -T '{rev}:{node|short}\n'

Or for unique long form of hash:

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

The -b . or branch(.) (dot for branch name) means the current working directory branch and -r . means the current working directory revision, which is documented in hg help revsets and hg help revisions.

Note if there is an uncommitted merge, the . (dot) only displays the first parent of two parents of the working group.

Community
  • 1
  • 1
Shelby Moore III
  • 6,063
  • 1
  • 33
  • 36
4

In case TortoiseHg is used, right-click the revision row in the Workbench and select "Copy hash" (as per documentation).

enter image description here

texnic
  • 3,959
  • 4
  • 42
  • 75
3

As others have pointed out, don't use log -l.

Use hg log -r . to get detailed information, as opposed to using hg id whose output is limited and it does not support templates. You could also create a little alias like here = log -r . and use hg here. If you only want the hash use hg log -r . --template '{node}\n'.

Gru
  • 400
  • 2
  • 8