Tarek Ziadé avatar Tarek Ziadé committed 204a398

added a git hook - fixes #8

Comments (0)

Files changed (2)

 The output of PyFlakes *and* pep8 is merged and returned.
 
 flake8 offers an extra option: --max-complexity, which will emit a warning if the
-McCabe complexityu of a function is higher that the value. By default it's 
+McCabe complexityu of a function is higher that the value. By default it's
 deactivated::
 
     $ bin/flake8 --max-complexity 12 flake8
     coolproject/mod.py:1204:1: 'selftest' is too complex (14)
 
 This feature is quite useful to detect over-complex code. According to McCabe, anything
-that goes beyond 10 is too complex. 
+that goes beyond 10 is too complex.
 See https://en.wikipedia.org/wiki/Cyclomatic_complexity.
 
 
 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.
 
-*complexity* defines the maximum McCabe complexity allowed before a warning 
+*complexity* defines the maximum McCabe complexity allowed before a warning
 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.
 
+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
 =====================
 
 In order to use Flake8 inside a buildout, edit your buildout.cfg and add this::
 
     [buildout]
-    
+
     parts +=
         ...
         flake8
-    
+
     [flake8]
     recipe = zc.recipe.egg
     eggs = flake8
            ${buildout:eggs}
     entry-points =
-        flake8=flake8.run:main 
+        flake8=flake8.run:main
 
 Original projects
 =================
 1.2 - ?
 -------
 
-?
+- added a git hook
+-
+
 
 1.1 - 2012-02-14
 ----------------
 import sys
 import os
 import os.path
+from subprocess import PIPE, Popen
 
 from flake8.util import skip_file
 from flake8 import pep8
     testsuite = ''
     doctest = False
 
-
-def hg_hook(ui, repo, **kwargs):
+def _initpep8():
     # default pep8 setup
     pep8.options = _PEP8Options()
     pep8.options.physical_checks = pep8.find_checks('physical_line')
     pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
     pep8.options.messages = {}
     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)
     warnings = 0
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.