-
Notifications
You must be signed in to change notification settings - Fork 3
/
pre-commit
executable file
·40 lines (31 loc) · 1.34 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
#!/usr/bin/env python3
# (C) 2019 The Johns Hopkins University Applied Physics Laboratory LLC
import os
import subprocess
import re
import sys
COPYRIGHT = "2019 The Johns Hopkins University Applied Physics Laboratory LLC"
def is_text(filename):
msg = subprocess.Popen(["file", filename], stdout=subprocess.PIPE).communicate()[0].decode()
return re.search('text|empty', msg) != None
def main():
foundUncopyrighted = False
p = subprocess.Popen(["git", "diff", "-z", "--cached", "--name-status"], stdout = subprocess.PIPE)
lines = p.communicate()[0].strip().decode('utf8').split(u'\x00')
for i in range(0, len(lines) - 1, 2):
if lines[i].upper() == "A":
filename = lines[i + 1]
if is_text(filename):
p = subprocess.Popen(["grep", COPYRIGHT, filename], stdout = subprocess.DEVNULL)
exitCode = p.wait()
if exitCode != 0:
print("New file '" + lines[i + 1] + "' has no copyright header.")
foundUncopyrighted = True
if foundUncopyrighted:
sys.stdin = open("/dev/tty", "r")
answer = input("\nAbort commit to add copyright? [Yn] ").strip()
if answer == "" or answer.upper() == "Y" or answer.upper() == "yes":
return 1
return 0
if __name__ == "__main__":
sys.exit(main())