-
Notifications
You must be signed in to change notification settings - Fork 340
/
pre-commit
executable file
·70 lines (54 loc) · 2.41 KB
/
pre-commit
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
66
67
68
69
#!/usr/bin/env python3
import functools
import os
from pprint import pprint
import re
import subprocess
import sys
class ValidationError(Exception): pass
def check_output(cmd, expect_process_error=False):
try:
return subprocess.check_output(cmd)
except Exception as e:
if not expect_process_error or not isinstance(e, subprocess.CalledProcessError):
print('Exception while running cmd %s' % (cmd,))
raise
def changes():
return check_output(('git', 'diff', '--no-ext-diff', '--cached', '-U0'))
def toplevel():
return check_output(('git', 'rev-parse', '--show-toplevel')).strip(b'\n').decode('utf-8')
def toList(fn):
return functools.wraps(fn)(lambda *args, **kwargs: list(fn(*args, **kwargs)))
top = toplevel()
clangformat = "/usr/local/clang_3_6_0/bin/clang-format"
### CLANG-FORMAT BEGIN
if os.path.exists(os.path.join(top, '.clang-format')):
reformatted_files = []
files_had_unstaged_edits = []
for filename in check_output(['git', 'diff-index', '--cached', '--name-only', 'HEAD']).decode('utf-8').splitlines():
if any(filename.endswith(ext) for ext in ('.cpp', '.h', '.hpp')):
file_path = os.path.join(top, filename)
with open(file_path, 'rb') as f:
staged = f.read()
formatted = check_output([clangformat, file_path])
if staged == formatted:
continue
try:
check_output(['git', 'diff', '--exit-code', file_path], expect_process_error=True)
except subprocess.CalledProcessError:
files_had_unstaged_edits.append(filename)
check_output([clangformat, '-i', file_path])
try:
check_output(['git', 'diff', '--exit-code', file_path], expect_process_error=True)
except subprocess.CalledProcessError:
reformatted_files.append(filename)
if reformatted_files:
print('These files were reformatted with clang-format, please stage the changes first:\n- %s'
% '\n- '.join(reformatted_files), file=sys.stderr)
if files_had_unstaged_edits:
print('\nThese files had unstaged edits before clang-format reformatting. Please stage desired parts and confirm the commit again.\n- %s'
% '\n- '.join(files_had_unstaged_edits), file=sys.stderr)
if reformatted_files or files_had_unstaged_edits:
exit(1)
### CLANG-FORMAT END
exit(0)