views:

146

answers:

1

Hi -

where could I place code to be run after every commit I make with mercurial? Specifically, I would like to maintain a file called latest inside the .hg folder in the root of my project - that file will hold the revision number and hash code for the most recent commit. On that same topic, how can I get those in python?

# get mercurial version hash
ver = ?

# get mercurial revision number
rev = ?

# is there a shortcut to this folder through mercurial?
f = open('/path/to/.hg/latest', 'w')
f.write('ver=%s\nrev=%d' % ( str(ver), int(rev) ) )
f.close

EDIT: I was able to accomplish the above with hooks (in .hg/hgrc):

[hooks]
precommit= echo node=`hg tip --template {node}` > tip && echo rev=`hg tip --template {rev}` >> tip && hg add tip

The file with the tip info is created successfully, but I would also like to add it to the current commit with hg add tip, which is where the mercurial process gets stuck waiting for the lock apparently held by the pending commit. Is there a way to work around it such that the file created during/pre commit is added to it? thanks.

+4  A: 

http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html

specifically you seem to want the commit hook which there is a tutorial for

of course it sounds like what you really want is hg tip

jk
hg tip isn't really the latest commit (if you pull) but it should suffice for the op
Marcus Lindblom
yes its not clear whether they want tip or not but it might be an option so i mentioned it
jk