diff --git a/README b/README index 27b900d..9412a4b 100644 --- a/README +++ b/README @@ -12,6 +12,7 @@ It also adds a few features: - lines that contains a "# NOQA" comment at the end will not issue a warning - merging pep8 and pyflakes options +- a Mercurial hook - more things to come.. Original projects: @@ -19,3 +20,20 @@ Original projects: - pep8: http://github.com/jcrocholl/pep8/ - PyFlakes: http://divmod.org/trac/wiki/DivmodPyflakes + +Mercurial hook +============== + +To use the Mercurial hook on any *commit* or *qrefresh*, change your .hg/rc file +like this:: + + [hooks] + commit = python:flake8.hg_hook + qrefresh = python:flake8.hg_hook + + [flake8] + strict = 0 + + +If *strict* option is set to **1**, any warning will block the commit. When +*strict* is set to **0**, warnings are just displayed in the standard output. diff --git a/flake8/__init__.py b/flake8/__init__.py index 7eed0aa..f3830b5 100644 --- a/flake8/__init__.py +++ b/flake8/__init__.py @@ -109,3 +109,25 @@ def main(): warnings += check(stdin, '') raise SystemExit(warnings > 0) + +def hg_hook(ui, repo, **kwargs): + pep8.process_options() + warnings = 0 + files = [] + for rev in xrange(repo[kwargs['node']], len(repo)): + for file_ in repo[rev].files(): + if file_ not in files: + files.append(file_) + + for file_ in files: + warnings += checkPath(file_) + warnings += pep8.input_file(file_) + + strict = ui.config('flake8', 'strict') + if strict is None: + strict = True + + if strict.lower() in ('1', 'true'): + return warnings + + return 0