Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mercurial Hook Issues
# -*- coding: utf-8 -*-
from __future__ import with_statement
import os
import sys
from subprocess import Popen, PIPE
from configparser import ConfigParser
except ImportError: # Python 2
from ConfigParser import ConfigParser
from flake8.engine import get_parser, get_style_guide
from flake8.main import DEFAULT_CONFIG
from mercurial.demandimport import disable
# ...
def hg_hook(ui, repo, **kwargs):
"""This is the function executed directly by Mercurial as part of the
hook. This is never called directly by the user, so the parameters are
undocumented. If you would like to learn more about them, please feel free
to read the official Mercurial documentation.
complexity = ui.config('flake8', 'complexity', default=-1)
strict = ui.configbool('flake8', 'strict', default=True)
config = ui.config('flake8', 'config', default=True)
if config is True:
paths = _get_files(repo, **kwargs)
flake8_style = get_style_guide(
config_file=config, max_complexity=complexity)
report = flake8_style.check_files(paths)
if strict:
return report.total_errors
return 0
# ...
def _get_files(repo, **kwargs):
seen = set()
for rev in range(repo[kwargs['node']], len(repo)):
for file_ in repo[rev].files():
file_ = os.path.join(repo.root, file_)
if file_ in seen or not os.path.exists(file_):
if file_.endswith('.py'):
yield file_
iapetus:~/sandbox/flake8 (default) hg ci --traceback
calling hook pre-commit: flake8.hooks.hg_hook
error: pre-commit hook raised an exception: 'node'
Traceback (most recent call last):
File "/usr/lib64/python2.6/site-packages/mercurial/", line 68, in _pythonhook
r = obj(ui=ui, repo=repo, hooktype=name, **args)
File "/usr/lib64/python2.6/site-packages/flake8/", line 66, in hg_hook
report = flake8_style.check_files(paths)
File "/usr/lib64/python2.6/site-packages/", line 1601, in check_files
for path in paths:
File "/usr/lib64/python2.6/site-packages/flake8/", line 83, in _get_files
for rev in range(repo[kwargs['node']], len(repo)):
KeyError: 'node'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment