Someone pointed out the KeywordExtension, and that's definitely one route to go.
For a little more control you can create an 'update' writes what you want into a version
file which you don't add to the repository itself. Something like this in your repo's hgrc:
[hooks]
update = hg id > version.txt
Where version.txt
exists in your .hgignore because you don't want to track changes to it. Then you have your version code read that file.
The advantage to using a hook vs. the KeywordExtension is the ability to use more complex tag trolling logic.
In fact the nearest extension might get you pretty much exactly what you want:
The goal of the nearest mercurial
extension is to find the nearest
tag(s) from a given changeset, either
backward or forward in the changesets
history tree.
By default, tags are searched backward
in history, but using the --contains
option will make it search forward. It
answers the following questions:
On which tag this changeset is based
on ? (without --contains) Which tag
will include / contain this changeset
? (with --contains) The tags are
searched by date, so that the nearest
tag in time will be reported. However,
the --all option will make the
extension search the first tag on all
possible branches.