Submit Blog  RSS Feeds

Wednesday, March 7, 2012

Logging mercurial (hg) update/merge history

Greetings fellow readers!

I finally managed to get my blog running... and no, this ain't another fashion blog... and this ain't another blog about cooking... Is it about voyages? Nice try... but it's not. This blog is about old-school programming :-)


Considering that this is my first post I'll start with something simple, yet very helpful -- a hook for logging the hg udpate and hg merge commands.

Why should this feature be helpful? Imagine a production server dependant on a large code repository (many programmers, many branches, you can hardly commit some changes without merging changesets). Having an update/merge history on such a server could save a lot of time and nerves when something goes wrong -- we have a list of previous stable versions.

In order to make log the mentioned activities you have to insert two lines in the [hooks] section of the .hgrc file from your home directory (or .hg/hgrc in your project directory):

~ cat ~/.hgrc


[hooks]
preupdate.pre_up = echo $(date +%D\ %T) "update [$(hg branch)]" $(hg id -i) >> .hg_update.log
update.post_up =  if [ $HG_ERROR -eq 0 ] ; then echo "$(date +%D\ %T) success: [$(hg branch)]" $HG_PARENT1 $HG_PARENT2; else echo "Errors occured" ; fi >> .hg_update.log

 These hooks log a mesage containing the current date, time, branch and changeset before and after the update/merge is performed. The history is stored in .hg_update.log (current directory). And a test:

~ hg update -C new_feature
~ cat .hg_update.log

03/07/12 22:22:12 update [default] 19478f194c18
03/07/12 22:22:12 success: [new_feature] 2c050459a85f

You can now easily revert those changes by executing:

~ hg update -C  19478f194c18

 
Hope you find my solution useful.

No comments:

Post a Comment

free counters