285583cb0471 — sietse.brouwer default tip 6 years ago
add the ticket-in-message hook
6f37b5604e78 — sietse.brouwer 6 years ago
Fix nested list indenting for Bitbucket's dialect
7be4daa40aa9 — sietse.brouwer 6 years ago
style fixes in pep8_hook.py


browse log



#Sietse’s hooks

Two hooks so far:

  • mergecheck_hook prevents merging the wrong branches into each other. Useful if you want to make sure nobody accidentally merges a feature branch directly into live.

  • pep8_hook prevents commits that introduce pep8 errors, or that don't fix enough of them. In this, it is slightly more sophisticated than than hghooks : that module only checks whether a commit contains files containing pep8 errors, which can be a bit more intrusive if your codebase has legacy. OTOH, hghook lets you customize the behavior of the pep8 checker, which is also nice.


A Mercurial hook that prevents a merge if the (from, to) pair of branchnames matches a (from, to) pair of regexes in a blacklist.


  1. Edit the BLACKLIST constant in mergecheck.py.

  2. Add this to your repository's hgrc:

     preupdate.mergecheck = python:path/to/mergecheck_hook.py:mergecheck


A hook that aborts your commit if it adds pep8 errors, or if it does not fix enough pep8 errors.

If the commit is aborted, the hook will print the list of pep8 errors and tell you how many you need to fix. If the commit is allowed, but the files still contain errors, it will print "committing x files with y pep8 errors" to keep you aware.

The hook does not check merge commits, because you don't want to fix pep8 errors in the middle of a merge. It only scans the files your commit touches.


  1. Add the following to your relevant hgrc:

     pretxncommit.pep8 = python:path/to/pep8_hook.py:pep8_hook
  2. If you want to force people to fix at least 2 pep8 errors on every commit, add the following configuration:

     max_change = -2

    Of course you could also set max_change = 4 to allow people to add up to 4 pep8 errors every commit, but why would you want to do that?

  3. To perform a commit with the hook disabled, override it using the config flag.

     hg --config hooks.pretxncommit.pep8= commit ...


  • rename max_change to fix_at_least

  • Rewrite the main pep8 hook function so that it takes two arguments:

    • a count function that you can point at a temp dir, and that returns a count of errors

    • a rule function that takes arguments (ui, branch, oldest_count, old_count, new_count, old_is_merge, new_is_merge) and returns True to abort, False to allow. It can use ui for reading the config and for printing messages.

  • Once that rewrite is done, create one hook function for every workflow below, so that people may choose which one they want.

  • Once that is done, use this knowledge to add the workflows to Lorenzo Sanchez’s hghooks (PyPI, code), because improving existing packages is better than creating new ones. :-)

  • Get rid of the pep8_hook.py:pep8_hook SmurfNaming?

#pep8 workflows that people might want

  • Abort the commit if it introduces pep8 errors.

  • Abort the commit if it introduces more than n pep8 errors.

  • Abort the commit if it does not fix at least n pep8 errors.

  • Every second commit must fix at least n pep8 errors. This allows you to make your changes in one commit, then fulfil your pep8 quotum in the next commit.

  • Every second commit on default (merge or no) must fix at least n pep8 errors. This lets you keep your pep8 fixes separate from your feature branches.