This repository was archived by the owner on Feb 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·107 lines (94 loc) · 2.91 KB
/
pre-commit
File metadata and controls
executable file
·107 lines (94 loc) · 2.91 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/env python
import os
import subprocess
import sys
LICENSE = '''\
// Copyright 2020 - developers of the `grammers` project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
'''
def warn(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def error(*args, **kwargs):
warn(*args, **kwargs)
exit(1)
def check_fmt():
return subprocess.run(('cargo', '+nightly', 'fmt', '--', '--check')).returncode == 0
def check_fmt_extra():
ok = True
for root, _dirs, files in os.walk('.'):
root = root.replace(os.path.sep, '/')
if not root.startswith('./grammers'):
continue
for file in files:
if file.lower().endswith('.rs'):
file = os.path.join(root, file)
with open(file, 'r', encoding='utf-8') as fd:
expecting = 0
was_blank = True
for line in map(str.strip, fd):
if not line:
if was_blank:
warn(file, 'a single blank lines must be used between groups')
ok = False
break
was_blank = True
elif was_blank:
was_blank = False
if line.startswith('//!'):
new = 2
elif line.startswith('///'):
break
elif line.startswith('//'):
new = 1
elif line.startswith('#!['):
new = 3
else:
break
if new <= expecting:
warn(file, 'order must be license comment, doc-comment, cfg, mods/uses')
break
expecting = new
elif expecting == 1:
if not line.startswith('//'):
ok = False
warn(file, 'license comment separated by a single blank line must come first')
break
elif expecting == 2:
if not line.startswith('//!'):
ok = False
warn(file, 'doc-comment separated by a single blank line, if any, must come second')
break
elif expecting == 3:
if not line.startswith('#!['):
ok = False
warn(file, 'cfg separated by a single blank line, if any, must come third')
break
return ok
def check_license():
ok = True
for root, _dirs, files in os.walk('.'):
root = root.replace(os.path.sep, '/')
if not root.startswith('./grammers'):
continue
for file in files:
if file.lower().endswith('.rs') and not root.endswith('/examples'):
file = os.path.join(root, file)
with open(file, 'r', encoding='utf-8') as fd:
if fd.read(len(LICENSE)) != LICENSE:
ok = False
warn(file, 'missing license')
return ok
def main():
if not check_fmt():
error("please run 'cargo +nightly fmt' before commit")
if not check_license():
error("please make sure all files have the license header")
if not check_fmt_extra():
error("please make sure all files separate their imports")
if __name__ == '__main__':
main()