Last active
November 16, 2015 18:26
-
-
Save andersontep/20d717a87488ea12c7c8 to your computer and use it in GitHub Desktop.
Automatically run flake8 on only your changes before each commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
To use this hook, copy this file to REPOSITORY_NAME/.git/hooks/pre-commit | |
This pre-commit hook relies mainly on the diff_cover package to check for style | |
violations only in the lines of code you are about to commit. It's been pretty | |
awesome for me so far and it's definitely useful when working on legacy code! | |
In use, it might not be much different than git diff HEAD~1 | flake8 --diff | |
but I prefer the readability of the diff-quality output. | |
Check it out here: https://github.com/Bachmann1234/diff-cover | |
I'm using flake8 here, but diff_cover also supports pep8, pyflakes, and pylint | |
If you already have a flake8 configuration, then great! If not, it's quite easy | |
See here: https://flake8.readthedocs.org/en/latest/config.html | |
""" | |
import importlib | |
import subprocess | |
import sys | |
requirements = ['diff_cover', 'flake8'] | |
for package in requirements: | |
try: | |
importlib.import_module(package) | |
except ImportError: | |
print('Please install {} to use this pre-commit hook'.format(package)) | |
sys.exit(1) | |
def main(): | |
cmd = [ | |
'diff-quality', | |
'--fail-under=100', | |
'--violations=flake8', | |
'--ignore-unstaged', | |
] | |
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out, _ = proc.communicate() | |
if proc.returncode: | |
print(out) | |
print("Style violations detected. Please fix them or force the commit with --no-verify") | |
sys.exit(proc.returncode) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment