forked from rbock/sqlpp11
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
Latest commit
executable file
·70 lines (54 loc) · 2.41 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·70 lines (54 loc) · 2.41 KB
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
importfunctools
importos
frompprintimportpprint
importre
importsubprocess
importsys
classValidationError(Exception): pass
defcheck_output(cmd, expect_process_error=False):
try:
returnsubprocess.check_output(cmd)
exceptExceptionase:
ifnotexpect_process_errorornotisinstance(e, subprocess.CalledProcessError):
print('Exception while running cmd %s'% (cmd,))
raise
defchanges():
returncheck_output(('git', 'diff', '--no-ext-diff', '--cached', '-U0'))
deftoplevel():
returncheck_output(('git', 'rev-parse', '--show-toplevel')).strip(b'\n').decode('utf-8')
deftoList(fn):
returnfunctools.wraps(fn)(lambda*args, **kwargs: list(fn(*args, **kwargs)))
top=toplevel()
clangformat="/usr/local/clang_3_6_0/bin/clang-format"
### CLANG-FORMAT BEGIN
ifos.path.exists(os.path.join(top, '.clang-format')):
reformatted_files= []
files_had_unstaged_edits= []
forfilenameincheck_output(['git', 'diff-index', '--cached', '--name-only', 'HEAD']).decode('utf-8').splitlines():
ifany(filename.endswith(ext) forextin ('.cpp', '.h', '.hpp')):
file_path=os.path.join(top, filename)
withopen(file_path, 'rb') asf:
staged=f.read()
formatted=check_output([clangformat, file_path])
ifstaged==formatted:
continue
try:
check_output(['git', 'diff', '--exit-code', file_path], expect_process_error=True)
exceptsubprocess.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)
exceptsubprocess.CalledProcessError:
reformatted_files.append(filename)
ifreformatted_files:
print('These files were reformatted with clang-format, please stage the changes first:\n- %s'
%'\n- '.join(reformatted_files), file=sys.stderr)
iffiles_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)
ifreformatted_filesorfiles_had_unstaged_edits:
exit(1)
### CLANG-FORMAT END
exit(0)