From ad5e1902b6723dd2a1ddf9bc8c75bfb3813ba6d4 Mon Sep 17 00:00:00 2001 From: William Snell Date: Wed, 21 Oct 2015 13:02:45 +0100 Subject: [PATCH 1/9] Only check diffs, not whole files. --- pre-commit | 70 +++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/pre-commit b/pre-commit index 7ea3907..08778c8 100755 --- a/pre-commit +++ b/pre-commit @@ -1,61 +1,57 @@ #!/usr/bin/env python -""" -Forked from https://gist.github.com/810399 -""" + from __future__ import with_statement, print_function -import os -import re -import shutil import subprocess import sys -import tempfile # don't fill in both of these select_codes = [] -ignore_codes = ["E121", "E122", "E123", "E124", "E125", "E126", "E127", "E128", - "E129", "E131", "E501"] +ignore_codes = [ + "E121", + "E122", + "E123", + "E124", + "E125", + "E126", + "E127", + "E128", + "E129", + "E131", + "E501", + "W191", + "E101", + "E111", +] # Add things like "--max-line-length=120" below overrides = [] -def system(*args, **kwargs): - kwargs.setdefault('stdout', subprocess.PIPE) - proc = subprocess.Popen(args, **kwargs) - out, err = proc.communicate() - return out - - def main(): - modified = re.compile('^[AM]+\s+(?P.*\.py)', re.MULTILINE) - files = system('git', 'status', '--porcelain').decode("utf-8") - files = modified.findall(files) - - tempdir = tempfile.mkdtemp() - for name in files: - filename = os.path.join(tempdir, name) - filepath = os.path.dirname(filename) - - if not os.path.exists(filepath): - os.makedirs(filepath) - with open(filename, 'w') as f: - system('git', 'show', ':' + name, stdout=f) + git_args = ['git', '--no-pager', 'diff', '--cached'] - args = ['pep8'] + pep8_args = ['pep8', '--diff'] if select_codes and ignore_codes: print(u'Error: select and ignore codes are mutually exclusive') sys.exit(1) elif select_codes: - args.extend(('--select', ','.join(select_codes))) + pep8_args.extend(('--select', ','.join(select_codes))) elif ignore_codes: - args.extend(('--ignore', ','.join(ignore_codes))) - args.extend(overrides) - args.append('.') - output = system(*args, cwd=tempdir) - shutil.rmtree(tempdir) + pep8_args.extend(('--ignore', ','.join(ignore_codes))) + pep8_args.extend(overrides) + pep8_args.append('.') + + cmd = ' '.join(git_args) + ' | ' + ' '.join(pep8_args) + proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) + output, error = proc.communicate() + + if error: + print(u'Error in pep8 checking commit', error.decode("utf-8")) + sys.exit(1) + if output: print(u'PEP8 style violations have been detected. Please fix them\n' 'or force the commit with "git commit --no-verify".\n') - print(output.decode("utf-8"),) + print(output.decode("utf-8")) sys.exit(1) From c0b65e9d8bc7bbc38d09f168fb6f01636e9b2173 Mon Sep 17 00:00:00 2001 From: William Snell Date: Wed, 21 Oct 2015 13:05:02 +0100 Subject: [PATCH 2/9] Updated readme to reflect change. --- README.md | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 9dbd865..8b5beb3 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,24 @@ PEP8 Git Commit Hook ==================== - This is a pre-commit hook for Git that checks the code to be committed for Python PEP8 style compliance. The hook will prevent the commit in -case style violations are detected. - -Installation: +case style violations are detected. The script only checks for violations +in the changes, not the file as a whole. -1. Install the pep8 program: ```$ pip install pep8``` -2. Save pre-commit as your_project/.git/hooks/pre-commit -3. Mark pre-commit executable: ```$ chmod +x your_project/.git/hooks/pre-commit``` +This code was forked from [https://github.com/cbrueffer/pep8-git-hook](https://github.com/cbrueffer/pep8-git-hook). -The hook can be overridden: ```$ git commit --no-verify``` -Currently, the following PEP8 codes are checked for: +Installation +==================== -E111 indentation is not a multiple of four -E125 continuation line does not distinguish itself from next logical line -E203 whitespace before ':' -E261 at least two spaces before inline comment -E262 inline comment should start with '# ' -E301 expected 1 blank line, found 0 -E302 expected 2 blank lines, found 1 -E303 too many blank lines (2) -E502 the backslash is redundant between brackets -E701 multiple statements on one line (colon) -E711 comparison to None should be 'if cond is None:' -W291 trailing whitespace -W293 blank line contains whitespace +1. Install the pep8 program: ```$ pip install "pep8>=1.3"``` +2. Save pre-commit as your_project/.git/hooks/pre-commit +3. Mark pre-commit executable: ```$ chmod +x your_project/.git/hooks/pre-commit``` -In case you want to modify the list of codes to ignore, edit the -```ignore_codes``` list in the pre-commit file. -If you want to select only specific codes to scan for, use the -```select_codes``` list. -Additional arguments to the pep8 program (e.g., ```--max-line-length=120```) can be added to the ```overrides``` list. -This code was forked from [https://gist.github.com/810399](https://gist.github.com/810399). +Usage +==================== +* The hook can be overridden: ```$ git commit --no-verify``` +* If you want to modify the list of codes to ignore, edit the ```ignore_codes``` list in the pre-commit file. +* If you want to select only specific codes to scan for, use the ```select_codes``` list. +* Additional arguments to the pep8 program (e.g., ```--max-line-length=120```) can be added to the ```overrides``` list. From 0fd20f22170dfe903a11f966562b5866cfec7ff0 Mon Sep 17 00:00:00 2001 From: Marco Sirabella Date: Thu, 11 May 2017 13:51:10 -0400 Subject: [PATCH 3/9] - python3 migration --- pre-commit | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pre-commit b/pre-commit index 59f0c62..2c5e8a9 100755 --- a/pre-commit +++ b/pre-commit @@ -1,8 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Forked from https://gist.github.com/810399 """ -from __future__ import with_statement, print_function import os import re import shutil From e11d4fd6925028b2a21280424aa3cdaeb42451b7 Mon Sep 17 00:00:00 2001 From: Marco Sirabella Date: Thu, 11 May 2017 13:51:47 -0400 Subject: [PATCH 4/9] - Added requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..282a93f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pycodestyle From 769656da39e8a2d7a6301b7659f15bc6f8ec57ee Mon Sep 17 00:00:00 2001 From: Marco Sirabella Date: Thu, 11 May 2017 15:36:15 -0400 Subject: [PATCH 5/9] - Use pycodestyle module now rather than shell --- pre-commit | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/pre-commit b/pre-commit index 2c5e8a9..2be9731 100755 --- a/pre-commit +++ b/pre-commit @@ -4,17 +4,18 @@ Forked from https://gist.github.com/810399 """ import os import re -import shutil import subprocess import sys -import tempfile +import pycodestyle + +options = { # don't fill in both of these -select_codes = [] -ignore_codes = ["E121", "E122", "E123", "E124", "E125", "E126", "E127", "E128", - "E129", "E131", "E501"] -# Add things like "--max-line-length=120" below -overrides = [] + 'select': [ + ], + 'ignore': [ + ], + } def system(*args, **kwargs): @@ -29,32 +30,18 @@ def main(): files = system('git', 'status', '--porcelain').decode("utf-8") files = modified.findall(files) - tempdir = tempfile.mkdtemp() - for name in files: - filename = os.path.join(tempdir, name) - filepath = os.path.dirname(filename) - - if not os.path.exists(filepath): - os.makedirs(filepath) - with open(filename, 'w') as f: - system('git', 'show', ':' + name, stdout=f) - - args = ['pycodestyle'] - if select_codes and ignore_codes: + if options['select'] and options['ignore']: print(u'Error: select and ignore codes are mutually exclusive') sys.exit(1) - elif select_codes: - args.extend(('--select', ','.join(select_codes))) - elif ignore_codes: - args.extend(('--ignore', ','.join(ignore_codes))) - args.extend(overrides) - args.append('.') - output = system(*args, cwd=tempdir) - shutil.rmtree(tempdir) - if output: - print(u'PEP8 style violations have been detected. Please fix them\n' - 'or force the commit with "git commit --no-verify".\n') - print(output.decode("utf-8"),) + + checker = pycodestyle.StyleGuide(**options) + + report = checker.check_files(paths=files) + + + if report.total_errors != 0: + print('''PEP8 style violations have been detected. Please fix them + or force the commit with "git commit --no-verify".''') sys.exit(1) From 0c2001067ba49a2abb7f554015d9a17fc4f698bb Mon Sep 17 00:00:00 2001 From: Marco Sirabella Date: Thu, 11 May 2017 15:43:05 -0400 Subject: [PATCH 6/9] - Made pep8-compliant --- pre-commit | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pre-commit b/pre-commit index 2be9731..3ce1317 100755 --- a/pre-commit +++ b/pre-commit @@ -10,7 +10,7 @@ import pycodestyle options = { -# don't fill in both of these + # don't fill in both of these 'select': [ ], 'ignore': [ @@ -38,7 +38,6 @@ def main(): report = checker.check_files(paths=files) - if report.total_errors != 0: print('''PEP8 style violations have been detected. Please fix them or force the commit with "git commit --no-verify".''') From f9aebc253e51c65540e39d4bf1b60fb45c73f4cc Mon Sep 17 00:00:00 2001 From: Marco J Sirabella Date: Fri, 30 Jun 2017 11:00:36 -0400 Subject: [PATCH 7/9] - Removed useless import --- pre-commit | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pre-commit b/pre-commit index 152fb35..f947b73 100755 --- a/pre-commit +++ b/pre-commit @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -import os import re import subprocess import sys @@ -11,6 +10,8 @@ options = { 'select': [ ], 'ignore': [ + 'E221', + 'W191' ], } @@ -22,13 +23,11 @@ def system(cmd, **kwargs): def main(): pycheck = re.compile('.*.py') - #git_args = ['git', '--no-pager', 'diff', '--cached'] - git_args = ['git', '--no-pager', 'diff'] + git_args = ['git', '--no-pager', 'diff', '--cached'] diffs = pycodestyle.parse_udiff(system(git_args).decode("utf-8")) + files = diffs.keys() - print(files) files = [filename for filename in files if pycheck.match(filename)] - print(files) if options['select'] and options['ignore']: print(u'Error: select and ignore codes are mutually exclusive') From 444ca65151ac95cb31d8e0ad3021febe56f144e2 Mon Sep 17 00:00:00 2001 From: Paulo R Date: Fri, 9 Jun 2017 18:14:05 -0300 Subject: [PATCH 8/9] - Added option to check for config file setup.cfg (cherry picked from commit 40e1cc9bab202d0652f96f6b28d31f6160076bfe) --- pre-commit | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pre-commit b/pre-commit index f947b73..ba35d7e 100755 --- a/pre-commit +++ b/pre-commit @@ -1,4 +1,7 @@ #!/usr/bin/env python3 +""" +Forked from https://gist.github.com/810399 +""" import re import subprocess import sys @@ -6,14 +9,10 @@ import pycodestyle options = { - # don't fill in both of these - 'select': [ - ], - 'ignore': [ - 'E221', - 'W191' - ], - } + # don't fill in both of these + 'select': [], + 'ignore': ['E221', 'W191'], +} def system(cmd, **kwargs): kwargs.setdefault('stdout', subprocess.PIPE) @@ -34,6 +33,7 @@ def main(): sys.exit(1) checker = pycodestyle.StyleGuide(**options, + config_file="setup.cfg", reporter=pycodestyle.DiffReport, selected_lines=diffs) report = checker.check_files(paths=files) From e504884dbc7d96ab47655b86d4df1f1a71a9a642 Mon Sep 17 00:00:00 2001 From: Marco J Sirabella Date: Sun, 9 Jul 2017 19:34:19 -0400 Subject: [PATCH 9/9] - Got rid of options due to overriding user/local config --- pre-commit | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pre-commit b/pre-commit index ba35d7e..7cbfe47 100755 --- a/pre-commit +++ b/pre-commit @@ -7,19 +7,11 @@ import subprocess import sys import pycodestyle - -options = { - # don't fill in both of these - 'select': [], - 'ignore': ['E221', 'W191'], -} - def system(cmd, **kwargs): kwargs.setdefault('stdout', subprocess.PIPE) proc = subprocess.Popen(cmd, **kwargs) return proc.communicate()[0] - def main(): pycheck = re.compile('.*.py') git_args = ['git', '--no-pager', 'diff', '--cached'] @@ -28,11 +20,8 @@ def main(): files = diffs.keys() files = [filename for filename in files if pycheck.match(filename)] - if options['select'] and options['ignore']: - print(u'Error: select and ignore codes are mutually exclusive') - sys.exit(1) - - checker = pycodestyle.StyleGuide(**options, + checker = pycodestyle.StyleGuide( + parse_argv=False, config_file="setup.cfg", reporter=pycodestyle.DiffReport, selected_lines=diffs)