2

I currently have a pre-commit hook in my mercurial project that gives the user the option to update the version number of the project if they wish (e.g. 1.0 to 1.0.1 or 1.1 or 2.0). They select one of these options, and the hook updates the version number in one of the project's files prior to the commit taking place.

When I run hg commit, this hook runs and updates the relevant file with the new version number, and then performs the commit.

I'd like to add to the hook such that it calls hg tag <new_verson_number> as well.

However, I can't add this to the pre-commit hook, because then the tag will be added before the commit is called, causing the tag to be one revision out of date.

I'd like to add the hg tag command to a commit hook (run after the commit), so that the sequence of events is like so:

  • hg commit -m "my commit message"
  • user says yes, I'd like to change the version number
  • version number is updated in the relevant file
  • commit occurs [everything up to here is fine]
  • if the user changed the version number, run a commit hook: hg tag <new_version_number>.

Now, I could add a commit hook that read off the new version number from the file it's stored in and run hg tag <new_version_number>, but what if the user decided not to change the version number? In that case, I don't want a tag added, and if I blindly run hg tag <new_version_number> I'll end up with tags I don't want.

So - is there some way I can tell the pre-commit hook to leave some information for the commit hook (a yes/no of whether to add the tag), and the commit hook can use this to determine whether to add the tag or not?

cheers.

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194

2 Answers2

2

How about a commit hook that checks if the last commit modified the file in which you store the version? Something like:

[hooks]
commit.tagversion = if hg log -r $HG_NODE --template '{files}' | grep -q VERSION_FILE ; then hg tag $(hg cat -r $HG_NODE VERSION_FILE) ; fi

I haven't tested it, but that or something like it should work.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • It would, but I simplified my problem a little - the version file contains other information that may also be modified, but that doesn't contribute to a version change. Perhaps if I set some (temporary) environment variable to be TRUE (1) or FALSE (0) and check that in the commit hook? Except that it'd have to be an environment variable not in use. Or if I wrote to a temporary file in the precommit hook that gets deleted by the commit hook? – mathematical.coffee Mar 08 '12 at 03:38
  • 1
    Yeah, either of those would work fine. More normally this is done by one's CI, continuous integration, server. For example jenkins will watch a repository, build it, run tests, tag it, and post artifacts all w/ no scripting required. Doing stuff like this on commit is failure prone because it's possible to commit on a machine w/o the hooks configured. Having a push-here-to-release repository provides a nice choke point and a lot of additional functionality too. Just my $0.02. – Ry4an Brase Mar 08 '12 at 14:48
  • Cheers, I'll do that then. I knew there were systems that did the testing/making/hosting for you but never knew what they were called - thanks for the pointer, I'll read up about Jenkins. (I am restricted to using bitbucket though). – mathematical.coffee Mar 08 '12 at 23:23
  • I'll go with the environment variable/write a file in the pre-commit hook that gets deleted by the commit hook. Not ideal but will do until I look into things like Jenkins further. – mathematical.coffee Mar 08 '12 at 23:27
1

You could extract the tag name that you would add and then check to see if it already exists in hg tags before you add it. That would also catch the case where the developer amends the version number manually.

Steve Kaye
  • 6,262
  • 2
  • 23
  • 27
  • Thanks - I may end up doing something like this, but it doesn't cover the case where I may update the version number (say 1.6.1 to 1.6.2) in the pre-commit hook but not want to add a tag for it. – mathematical.coffee Mar 08 '12 at 23:26