Automatic version numbering with Mercurial hook

Assuming you put your version number setting in a version.py file as VERSION = NNN, you can add the following to the project .hg/hgrc to automatically increment the version number upon each commit. The version number is actually the local revision number before the commit plus 1, although not exactly robust but is generally increasingly upward. Any conflict should be resolved manually.
# .hg/hgrc
pre-commit.version = echo VERSION = $((`hg id -n | tr -d +` + 1)) > ./path/version.py
We use Mercurial pre-commit hook to update the version number in the file version.py. When the hook is run, the current directory is set to the repo's root directory. We get the local revision number with "hg id -n", remove the trailing "+" if any, and then add one to it as the version number. Please note that the hook is pre-commit not precommit. Both hooks exist, but if you use the precommit hook, you will not get what you want. See this post for details.
EDITED 2015-07-06: See Part 2 for a different approach.

No comments: