forked from teufelaudio/mynd-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteufel-pre-push
More file actions
executable file
·121 lines (88 loc) · 3.92 KB
/
Copy pathteufel-pre-push
File metadata and controls
executable file
·121 lines (88 loc) · 3.92 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python3
import os
import shutil
import subprocess
import sys
def empty_revision():
return '0000000000000000000000000000000000000000'
def is_file_formatted_correctly(filename, repo_file):
with open('{0}-formatted'.format(filename), 'w') as formatted_contents_file, open(filename, 'r') as in_contents:
subprocess.call(['clang-format', '-style=file', '--assume-filename={0}'.format(repo_file)], stdin=in_contents,stdout=formatted_contents_file)
diff = subprocess.call(['diff', '-u', filename, formatted_contents_file.name])
is_correct = not bool(diff)
return is_correct
def get_old_revision_locally():
parent = subprocess.check_output(["git", "describe", "--abbrev=0", "--all", "HEAD~1"]).decode().strip()
revisions = subprocess.check_output(["git", "rev-list", "--reverse", "{0}..HEAD".format(parent)]).decode()
if len(revisions) == 0:
print("Hook warning: There is no revision to push; pushing anyway...")
sys.exit(0)
old_revision = subprocess.check_output(["git", "rev-parse", "{0}~1".format(revisions.split()[0])]).decode().strip()
return old_revision
def get_filenames_between_revisions(old_revision, new_revision):
filenames = []
try:
FNULL = open(os.devnull, 'w')
filenames = subprocess.check_output(
['git', 'diff-tree', '--no-commit-id', '--name-only', '-r', new_revision, old_revision], stderr=FNULL)
except subprocess.CalledProcessError:
print("Hook warning: it seems that the remote branch is not related to the branch you want to push")
old_revision = get_old_revision_locally()
print("old revision=", old_revision, "new revision=", new_revision)
filenames = subprocess.check_output(
['git', 'diff-tree', '--no-commit-id', '--name-only', '-r', new_revision, old_revision])
if len(filenames) == 0:
print("Hook warning: there is no difference in content between new and old revisions; pushing anyway...")
sys.exit(0)
source_filenames = []
for f in filenames.split():
f = f.decode()
if f.endswith('.c') or f.endswith('.cpp') or f.endswith('.h') or f.endswith('.hpp'):
source_filenames.append(f.strip())
return source_filenames
def hook(old_revision, new_revision):
if old_revision == empty_revision():
old_revision = get_old_revision_locally()
filenames_between_revisions = get_filenames_between_revisions(
old_revision, new_revision)
result = True
for f in filenames_between_revisions:
if not os.path.isfile(f):
continue # File has been deleted from the repository
tmp_filename = '.clang-format-check/{0}'.format(f)
dirname = os.path.dirname(tmp_filename)
try:
os.makedirs(dirname)
except:
pass
with open(tmp_filename, 'w') as temp_file:
subprocess.check_call(['git', 'show', '{0}:{1}'.format(new_revision, f)], stdout=temp_file)
if not is_file_formatted_correctly(tmp_filename, f):
result = False
return result
if __name__ == '__main__':
line = ''
for l in sys.stdin:
line = l
new_revision = ''
old_revision = ''
if line == '':
new_revision = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
old_revision = empty_revision()
else:
new_revision = line.split()[1]
old_revision = line.split()[3]
if new_revision == empty_revision():
print("new revision is empty, you might be deleting a remote branch; pushing anyway...")
sys.exit(0)
result = False
try:
result = hook(old_revision, new_revision)
except OSError as e:
print('OSError reports: {} (exit code {}); is clang-format installed?'.format(e.strerror, e.errno))
if os.path.isdir('.clang-format-check'):
shutil.rmtree('.clang-format-check')
if result:
sys.exit(0)
else:
sys.exit(1)