forked from bretmckee/sxASCOM.old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·168 lines (144 loc) · 5.59 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#! /usr/bin/env python
from __future__ import print_function, with_statement
import os, sys, getopt
import subprocess
import errno
import io, codecs
ENDING_UNKNOWN = 0
ENDING_DOS = 1
ENDING_UNIX = 2
ENDING_MIXED = 3
def printList(label, nameList):
if len(nameList) > 0:
first = True
print(label, end="")
for fileName in nameList:
if not first:
print(",", end="")
print("{0}".format(fileName), end="")
first = False
print("\n")
def processFile(f):
dosEnding = None
mixedEnding = False
for line in f:
#print ("dos={0} mixed={1} ".format(dosEnding, mixedEnding), end="")
#if len(line) > 1:
# print("{0} {1} {2} {3}".format(len(line), ord(line[-2]), ord(line[-1]), line.strip()))
#else:
# print("{0} {1}".format(len(line), line))
if len(line) > 0:
if dosEnding == None:
#print(ord(line[-1]), line)
if len(line) > 1 and ord(line[-2]) == 13:
dosEnding = True
else:
dosEnding = False
elif ord(line[-1]) == 10 or ord(line[-1]) == 13:
if dosEnding and (len(line) < 2 or ord(line[-2]) != 13):
mixedEnding = True
break
elif not dosEnding and len(line) > 1 and ord(line[-2]) == 13:
mixedEnding = True
break
if dosEnding == None:
ret = ENDING_UNKNOWN
elif mixedEnding:
ret = ENDING_MIXED
elif dosEnding:
ret = ENDING_DOS
else:
ret = ENDING_UNIX
return ret
def processFileName(fileName):
ret = ENDING_UNKNOWN
try:
with io.open(fileName, "rb") as f:
ret = processFile(f)
except IOError as ex:
if (ex.errno != errno.ENOENT):
print(ex.errno)
#print(fileName, ret)
return ret
def process(startDir):
dos = []
unix = []
mixed = []
for root, dirs, files in os.walk(startDir):
for fileName in files:
if fileName.lower().endswith(".cs"):
fullPath = os.path.join(root, fileName)
ending = processFile(fullPath)
if ending == ENDING_MIXED:
mixed.append(fullPath)
elif ending == ENDING_DOS:
dos.append(fullPath)
else:
unix.append(fullPath)
printList("dos:", dos)
printList("unix:", unix)
printList("mixed:", mixed)
def usage():
print("usage: {0}: [options] <diretory> [<directory>...]".format(sys.argv[0]), file=sys.stderr);
def runCmd(cmd):
try:
#print("running {0}".format(cmd))
cmdProcess = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdoutData, stderrData) = cmdProcess.communicate(None)
return (cmdProcess.returncode, stdoutData, stderrData)
except:
raise
def runCmdOK(cmd):
(returnCode, stdoutData, stderrData) = runCmd(cmd)
if returnCode != 0:
print("{0} returned non-zero status.".format(cmd))
if len(stderrData):
print("stderr output was " + stderrData)
sys.exit(returnCode)
return stdoutData
def runCmdBool(cmd):
(returnCode, stdoutData, stderrData) = runCmd(cmd)
return returnCode == 0
def checkFiles(fileNames):
invalidEnding = False
for fileName in fileNames:
if fileName.lower().endswith(".cs"):
#print("checking file {0}".format(fileName))
(returnCode, stdoutData, stderrData) = runCmd("git show HEAD:{0}".format(fileName))
if returnCode == 0:
if stdoutData.startswith(codecs.BOM_UTF16_LE) or stdoutData.startswith(codecs.BOM_UTF16_BE):
encoding = "utf-16"
elif stdoutData.startswith(codecs.BOM_UTF8):
encoding = "utf-8-sig"
else:
encoding = "ascii"
#print("checking file {0} bytes={1} encoding={2}".format(fileName, stdoutData[:4], encoding))
oldEnding = processFile(io.StringIO(unicode(stdoutData, encoding)))
newEnding = processFileName(fileName)
#print(fileName, oldEnding, newEnding)
if oldEnding == ENDING_UNKNOWN or newEnding == ENDING_UNKNOWN:
continue
if False and oldEnding != newEnding:
print("[Rejected by Policy - mixed newlines]: {0} - line ending mismatch".format(fileName))
invalidEnding = True
if newEnding != ENDING_DOS:
print("[Rejected by Policy - non-dos newlines]: {0}".format(fileName))
invalidEnding = True
return invalidEnding
def checkCommit():
if runCmdBool("git rev-parse --quiet --verify HEAD"):
against = "HEAD"
else:
against = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
stdoutData = runCmdOK("git diff --cached --name-only {0}".format(against))
files = stdoutData.split()
reject = checkFiles(files)
# if not reject:
# reject = not runCmdBool("git diff-index --check --cached {0} --".format(against))
# if reject:
# print("[Rejected by Policy - whitespace issue]: found by git diff-index --check --cached {0} --".format(against))
return reject
def main():
sys.exit(checkCommit())
if __name__ == "__main__":
main()