Skip to content

Instantly share code, notes, and snippets.

@sigmavirus24
Created February 26, 2013 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigmavirus24/5040167 to your computer and use it in GitHub Desktop.
Save sigmavirus24/5040167 to your computer and use it in GitHub Desktop.
Mercurial Hook Issues
# -*- 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_
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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment