Q:

Mercurial hook fails to execute when its script changed

I have a pretxncommit hook that uses Python script. This Python script is itself under version control in the same repo. Everything worked fine until I added changes to script itself, which led to a bunch of errors when trying to commit or merge with such changes. 
How can I fix this? The best solution I can come up with is to use a subrepo, but I don't really like it.
Error example:
Traceback (most recent call last):
  File "hg", line 43, in <module>
  File "mercurial\dispatch.pyc", line 30, in run
TypeError: unsupported operand type(s) for &: 'str' and 'int'

A:

Running a hook where the script is found in the repository itself can be tricky if a commit means that hook script changes during execution or is erronous - especially as the script itself can be in a somewhat undefined state at some point when it is modified itself.
One solution is to run the hook from another site outside the repository, like ~/bin and use additionally a hook in the post-commit or maybe txnclose which updates the script version as found in ~/bin from the repository, possibly preceeded by a sanity test to make sure you don't update to a broken version.
That's the way I update all scripts which run my compile farm: they all are in a CF-related repository, and hooks to that CF-related repository first trigger test runs with the newly committed versions to ensure the CF will work with them and subsequently only updates the scripts for the CF permanently when those tests pass successfully.

