added a git hook - fixes #8

This commit is contained in:
Tarek Ziade 2012-02-14 14:20:51 +01:00
parent 6eb3405dd0
commit e439682c85
2 changed files with 68 additions and 9 deletions

33
README
View file

@ -76,6 +76,35 @@ If *strict* option is set to **1**, any warning will block the commit. When
is emited. If you don't specify it it's just ignored. If specified, must is emited. If you don't specify it it's just ignored. If specified, must
be a positive value. 12 is usually a good value. be a positive value. 12 is usually a good value.
Git hook
========
To use the Git hook on any *commit*, add a **pre-commit** file in the
*.git/hooks* directory containing::
#!/usr/bin/python
import sys
from flake8.run import git_hook
COMPLEXITY = 10
STRICT = False
if __name__ == '__main__':
sys.exit(git_hook(complexity=COMPLEXITY, strict=STRICT))
If *strict* option is set to **True**, any warning will block the commit. When
*strict* is set to **False** or omited, warnings are just displayed in the
standard output.
*complexity* defines the maximum McCabe complexity allowed before a warning
is emited. If you don't specify it or set it to **-1**, it's just ignored.
If specified, it must be a positive value. 12 is usually a good value.
Also, make sure the file is executable and adapt the shebang line so it
point to your python interpreter.
Buildout integration Buildout integration
===================== =====================
@ -111,7 +140,9 @@ CHANGES
1.2 - ? 1.2 - ?
------- -------
? - added a git hook
-
1.1 - 2012-02-14 1.1 - 2012-02-14
---------------- ----------------

View file

@ -5,6 +5,7 @@ Implementation of the command-line I{flake8} tool.
import sys import sys
import os import os
import os.path import os.path
from subprocess import PIPE, Popen
from flake8.util import skip_file from flake8.util import skip_file
from flake8 import pep8 from flake8 import pep8
@ -90,8 +91,7 @@ class _PEP8Options(object):
testsuite = '' testsuite = ''
doctest = False doctest = False
def _initpep8():
def hg_hook(ui, repo, **kwargs):
# default pep8 setup # default pep8 setup
pep8.options = _PEP8Options() pep8.options = _PEP8Options()
pep8.options.physical_checks = pep8.find_checks('physical_line') pep8.options.physical_checks = pep8.find_checks('physical_line')
@ -99,6 +99,34 @@ def hg_hook(ui, repo, **kwargs):
pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0) pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
pep8.options.messages = {} pep8.options.messages = {}
pep8.args = [] pep8.args = []
def run(command):
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
p.wait()
return (p.returncode, [line.strip() for line in p.stdout.readlines()],
[line.strip() for line in p.stderr.readlines()])
def git_hook(complexity=-1, strict=False):
_initpep8()
warnings = 0
_, files_modified, _ = run("git diff-index --name-only HEAD")
for filename in files_modified:
ext = os.path.splitext(filename)[-1]
if ext != '.py':
continue
warnings += check_file(filename, complexity)
if strict:
return warnings
return 0
def hg_hook(ui, repo, **kwargs):
_initpep8()
complexity = ui.configint('flake8', 'complexity', default=-1) complexity = ui.configint('flake8', 'complexity', default=-1)
warnings = 0 warnings = 0