Skip to content

Instantly share code, notes, and snippets.

@aborilov
Created March 20, 2015 11:52
Show Gist options
  • Select an option

  • Save aborilov/c231ee6b8fac76a7814e to your computer and use it in GitHub Desktop.

Select an option

Save aborilov/c231ee6b8fac76a7814e to your computer and use it in GitHub Desktop.
git update hook
#!/usr/bin/env python
import re
import subprocess
import sys
refname = sys.argv[1]
oldrev = sys.argv[2]
newrev = sys.argv[3]
modified = re.compile('^(?:M|A)(\s+)(?P<name>.*)')
CHECKS = [
{
'output': 'Checking for pdbs...',
'command': '"import pdb"',
'print_filename': True,
},
{
'output': 'Checking for ipdbs...',
'command': '"import ipdb"',
'print_filename': True,
},
{
'output': 'Checking for print statements...',
'command': 'print',
'match_files': ['.*\.py$'],
'print_filename': True,
},
# {
# 'output': 'Checking for console.log()...',
# 'command': 'grep -n console.log %s',
# 'match_files': ['.*yipit/.*\.js$'],
# 'print_filename': True,
# },
{
'output': 'Checking for XXX comment',
'command': 'XXX',
'match_files': ['.*\.py$'],
'print_filename': True,
},
]
def matches_file(file_name, match_files):
return any(re.compile(match_file).match(file_name)
for match_file in match_files)
def check_files(files, check):
result = 0
print check['output']
for file_name in files:
if not 'match_files' in check or matches_file(file_name, check['match_files']):
process = subprocess.Popen(
'git diff -U0 --diff-filter=AM ' +
oldrev +
' ' +
newrev +
' -- ' +
file_name +
' | ' +
'grep ' +
check['command'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
out, err = process.communicate()
# print out
if out or err:
if check['print_filename']:
prefix = '\t%s:' % file_name
else:
prefix = '\t'
output_lines = [
'%s%s' % (prefix, line) for line in out.splitlines()]
print '\n'.join(output_lines)
if err:
print err
result = 1
return result
def main():
files = []
p = subprocess.Popen(
['git', 'diff', '--name-only', oldrev, newrev], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
files.append(line)
result = 0
for check in CHECKS:
result = check_files(files, check) or result
sys.exit(result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment