Skip to content

Instantly share code, notes, and snippets.

@doloopwhile
Last active December 14, 2015 21:28
Show Gist options
  • Save doloopwhile/5151079 to your computer and use it in GitHub Desktop.
Save doloopwhile/5151079 to your computer and use it in GitHub Desktop.
コミット時にNGワードが含まれていないかチェックする
#!/usr/bin/python3
# コミット時にNGワードが含まれていないかチェックする
import sys
import re
import os.path
import io
import itertools
from subprocess import check_output
from fnmatch import fnmatch
ng_list= [
('navona/MailInspector/mi/resources/*/*.view', r'jquery\.min\.js'),
('**', r'debug')
]
encoding = sys.getfilesystemencoding()
repo_root = check_output(['git', 'rev-parse','--show-toplevel'])
repo_root = repo_root.decode(encoding).strip()
files = check_output(['git', 'diff', '--name-only', '--cached'])
files = files.decode(encoding).splitlines()
for i, (file_pattern, ng_regex) in enumerate(ng_list):
if isinstance(ng_regex, str):
ng_list[i] = (file_pattern, re.compile(ng_regex, re.I))
ng_found = False
for f, (file_pattern, ng_regex) in itertools.product(files, ng_list):
if not fnmatch(f, file_pattern):
continue
try:
with io.open(os.path.join(repo_root, f)) as fp:
for number, line in enumerate(fp, start=1):
if not ng_regex.search(line):
continue
print("{}:{}: {}".format(f, number, line), file=sys.stderr)
ng_found = True
except (IOError, OSError) as e:
print(e, file=sys.stderr)
if ng_found:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment