Skip to content

Got mercurial hook working, with improvements

username-removed-894687 requested to merge dhandy/flake8:bug/283 into master

This commit fixes a problem that caused "flake8 --install-hook mercurial" to die with this message:

Could not find the mercurial directory

Also fixes two usages of configparser (can't supply a non-text value when calling .set(), and need to use .getboolean() to convert the value to a boolean.)

This commit converts the hook itself from a function hook to a script hook. This enables the hook to run even when flake8 is installed in a virtual environment and Mercurial is installed globally (a common case.) This also makes it possible for flake8 to be installed for Python 3; Mercurial is Python 2.x only.

Also convert the hook event from "commit" to "pretxncommit" so that strict mode would actually work. A regular commit hook runs only after the commit is completed; a pretxncommit hook will cause the transaction to be rolled back if it exits with a non-zero code.

Finally, put extensions on the hook names: pretxncommit.flake8 and qrefresh.flake8. This allows the flake8 hook to coexist with other hooks the user might have put in place.

I tested flake8 mercurial hook installation and execution on both Python 2.7 and Python 3.5. I did not test the qrefresh.flake8 hook. I could not find documentation on qrefresh hook event; I presume it has something to do with the Mercurial MQ extension, which I have never used. Probably this commit does not make it any worse than it was before.

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

Closes #283

Merge request reports