40

I need to get the current mercurial changeset to return in a very simple webservice, just the incrementing revision number and not the hash. I know I can use

 hg --cwd C:\repos\MyRepo parent

which will return me

changeset:   730:9d347e4a8d47
tag:         tip
user:        Simon Martin <simon.martin@mydomain.com>
date:        Tue Jun 12 15:39:45 2012 +0100
summary:     Fixed defect #244...

What I need though is just the 730 part of the changeset. The goal is to be able to write a very simple web service that will return that value - this will then be picked up by another application and displayed in the footer to give a quick reference as to which local revision is current. The testing process can then refer to that 'build' which can then be used to identify that.

Simon Martin
  • 4,203
  • 7
  • 56
  • 93
  • 4
    Please make sure you understand that those *local* revision numbers are very local. It's entirely possible that when someone clones the repository that they get the same changesets in a different order and thus the numbers can't uniquely identify a build anywhere except in that specific clone. – Ry4an Brase Jun 13 '12 at 20:46
  • 1
    Absolutely, I recognise this is an edge case but I explicitly need a readable revision (hence the title). The repo that will be used is a deployed site, **nothing will ever** clone from it and there is only 1 path to it - from a master repo via a build server. – Simon Martin Jun 13 '12 at 21:24

2 Answers2

54

You can show the local revision number of the working copy’s current parent using:

hg identify --num

Note that this outputs a + suffix when there are local changes. Add an -r . option to avoid this.

You can use the -r option to get the local revision number for other revisions too. For example, to retrieve the ID of the last tagged ancestor:

hg id -n -r "ancestors(.) and tag()"
Laurens Holst
  • 20,156
  • 2
  • 29
  • 33
15

You can use a custom template for the hg parent command.

This should get what you want:

hg parent --template "{rev}"
Steve Kaye
  • 6,262
  • 2
  • 23
  • 27
  • How and where do I set up the custom template? – Simon Martin Jun 13 '12 at 10:56
  • It’s specified right there on the command line, no need to set anything up. Another template example: `--template "Local revision number: {rev}\n"`. – Laurens Holst Jun 13 '12 at 11:21
  • Some documentation describing this feature is available here: http://hgbook.red-bean.com/read/customizing-the-output-of-mercurial.html – wip Jun 13 '17 at 05:58