Simplify @mpenkov's fix for exclude in git hooks

The simpler fix is to ensure that the filename isn't the full path to
the temporary file before we check to see if it should be excluded or if
it should even match.

This also fixes a bug I found while testing the pull request in which no
files are checked.
This commit is contained in:
Ian Cordasco 2015-03-06 19:47:55 -06:00 committed by Ian Cordasco
parent de9fd7d6a2
commit 84c8dd5e8d
3 changed files with 46 additions and 111 deletions

View file

@ -6,7 +6,7 @@ import sys
import stat
from subprocess import Popen, PIPE
import shutil
from tempfile import mkdtemp
import tempfile
try:
from configparser import ConfigParser
except ImportError: # Python 2
@ -16,11 +16,6 @@ from flake8.engine import get_parser, get_style_guide
from flake8.main import DEFAULT_CONFIG
def fix_exclude(flake8_style, tmpdir):
flake8_style.options.exclude = [tmpdir + x if "/" in x else x
for x in flake8_style.options.exclude]
def git_hook(complexity=-1, strict=False, ignore=None, lazy=False):
"""This is the function used by the git hook.
@ -53,11 +48,10 @@ def git_hook(complexity=-1, strict=False, ignore=None, lazy=False):
if complexity > -1:
options['max_complexity'] = complexity
tmpdir = mkdtemp()
tmpdir = tempfile.mkdtemp()
flake8_style = get_style_guide(config_file=DEFAULT_CONFIG, paths=['.'],
**options)
fix_exclude(flake8_style, tmpdir)
filepatterns = flake8_style.options.filename
# Copy staged versions to temporary directory
@ -75,15 +69,16 @@ def git_hook(complexity=-1, strict=False, ignore=None, lazy=False):
dirname = os.path.join(tmpdir, dirname)
if not os.path.isdir(dirname):
os.makedirs(dirname)
filename = os.path.join(dirname, filename)
# write staged version of file to temporary directory
with open(filename, "wb") as fh:
fh.write(out)
# check_files() only does this check if passed a dir; so we do it
if ((pep8.filename_match(filename, filepatterns) and
not flake8_style.excluded(filename))):
if ((pep8.filename_match(file_, filepatterns) and
not flake8_style.excluded(file_))):
filename = os.path.join(dirname, filename)
files_to_check.append(filename)
# write staged version of file to temporary directory
with open(filename, "wb") as fh:
fh.write(out)
# Run the checks
report = flake8_style.check_files(files_to_check)