views:

100

answers:

2

I'd like to add a --version command line option to my python application that will show the right version depending on the tagged status of the command:

If the file comes from a version whose short hex ID was abcdef01 that was tagged TAG, --version should show this:

MyApp Version TAG (abcdef01)

If the file comes from the tip, --version should show this:

MyApp (tip)

If the file comes from an arbitrary, untagged revision abcdef02, --version should show this:

MyApp (development, abcdef02)

Is this possible? If so, how?

+2  A: 

Once you activate the keyword extension, you can have it in a variable which can be carved up for the hash.

Ignacio Vazquez-Abrams
I can't see how to get the tag name out of that, just the short version string.
Chris R
+1  A: 

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.

Ry4an