public
Created

Mercurial Hook Issues

  • Download Gist
flake8_hooks.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
# -*- coding: utf-8 -*-
from __future__ import with_statement
import os
import sys
from subprocess import Popen, PIPE
try:
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
 
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:
config = DEFAULT_CONFIG
 
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_):
continue
seen.add(file_)
if file_.endswith('.py'):
yield file_
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
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/hook.py", line 68, in _pythonhook
r = obj(ui=ui, repo=repo, hooktype=name, **args)
File "/usr/lib64/python2.6/site-packages/flake8/hooks.py", line 66, in hg_hook
report = flake8_style.check_files(paths)
File "/usr/lib64/python2.6/site-packages/pep8.py", line 1601, in check_files
for path in paths:
File "/usr/lib64/python2.6/site-packages/flake8/hooks.py", line 83, in _get_files
for rev in range(repo[kwargs['node']], len(repo)):
KeyError: 'node'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.