This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
commit-msg-hook
executable file
·65 lines (50 loc) · 2.24 KB
/
commit-msg-hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
"""Commit hook for git that does lint testing.
It runs the lint checker (runlint.py) on all the open files in
the current commit.
"""
import os
import subprocess
import sys
try:
from third_party.rmc_linter import hook_lib
except ImportError, why:
sys.exit('FATAL ERROR: %s. Initiaize the linter repo with:\n'
' git submodule update --init --recursive\n'
' (cd third_party/rmc_linter && npm install)' % why)
def _normalized_commit_message(text):
"""Remove lines starting with '#' and other stuff that git ignores."""
lines = text.splitlines(True)
lines = [l for l in lines if not l.startswith('#')]
return ''.join(lines).strip()
def main():
"""Run a git pre-commit lint-check."""
# If we're a merge, don't try to do a lint-check.
git_root = subprocess.check_output(['git', 'rev-parse', '--git-dir'])
if os.path.exists(os.path.join(git_root.strip(), 'MERGE_HEAD')):
print "Skipping lint on merge..."
return 0
commit_message = open(sys.argv[1]).read()
# Get rid of the comment lines, and leading and trailing whitespace.
commit_message = _normalized_commit_message(commit_message)
# Go through all modified or added files.
try:
subprocess.check_output(['git', 'rev-parse', '--verify', 'HEAD'],
stderr=subprocess.STDOUT)
parent = 'HEAD'
except subprocess.CalledProcessError:
parent = '4b825dc642cb6eb9a060e54bf8d69288fbee4904' # empty repo
# Look at Added, Modified, and Renamed files.
files = subprocess.check_output(['git', 'diff', '--cached', '--name-only',
'--diff-filter=AMR', '-z', parent])
files_to_lint = files.strip('\0').split('\0') # that's what -z is for
if not files_to_lint or files_to_lint == ['']:
return 0
num_errors = hook_lib.lint_files(files_to_lint)
# Report what we found, and exit with the proper status code.
hook_lib.report_errors_and_exit(num_errors, commit_message,
os.path.join('.git', 'commit.save'))
if __name__ == '__main__':
suppress_lint = os.getenv('FORCE_COMMIT', '')
if suppress_lint.lower() not in ('1', 'true'):
sys.exit(main())