forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·162 lines (137 loc) · 4.98 KB
/
pre-commit
File metadata and controls
executable file
·162 lines (137 loc) · 4.98 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
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
#!/usr/bin/env python
"""
An SVN pre-commit hook which requires that commits either are marked as
whitespace cleanup commits, and contain no non-whitespace changes, or
leave whitespace alone on lines without code changes.
"""
import os
import sys
import difflib
#import debug
from pysvn import Client, Transaction
prog = os.path.basename(sys.argv[0])
def die(msg):
sys.stderr.write("\n%s: Error: %s\n" % (prog, msg))
sys.exit(1)
if len(sys.argv) <= 1:
die("Expected arguments: REPOSITORY TRANSACTION, found none")
if len(sys.argv) <= 2:
die( "Expected arguments: REPOSITORY TRANSACTION, found only '%s'" % sys.argv[1])
repo = sys.argv[1]
txname = sys.argv[2]
tx = Transaction(repo, txname)
client = Client()
is_whitespace_cleanup = "whitespace cleanup" in tx.revpropget("svn:log").lower()
def normalize(s):
return s.rstrip().expandtabs()
def normalize_sequence(seq):
o = []
for l in seq:
o.append(normalize(l))
return o
def normalize_file_end(seq):
while True and seq:
if seq[-1].strip() == "":
del seq[-1]
else:
break
return seq
def count(gen):
return sum(1 for _ in gen)
# Function with side effects. Acts on global varaibles
def inc_ab(flag):
global a, b
if flag == ' ':
a += 1; b += 1
elif flag == '-':
a += 1
elif flag == '+':
b += 1
elif flag == '?':
pass
else:
raise ValueError("Unexpected ndiff mark: '%s' in: %s" % (flag, plain_diff[i]))
def get_chunks(unidiff):
if not unidiff:
return [], []
chunks = []
chunk = []
intro = unidiff[0:2]
for line in unidiff[2:]:
if line.startswith("@@"):
if chunk:
chunks.append(chunk)
chunk = [line]
else:
chunk.append(line)
chunks.append(chunk)
return intro, chunks
changes = tx.changed()
issues = {}
context = 3
for path in changes:
action, kind, mod, propmod = changes[path]
# Don't try to diff added or deleted files, on ly changed text files
if not (mod and action == "R"):
continue
# Don't try do diff binary files
mimetype = tx.propget("svn:mime-type", path)
if mimetype and not mimetype.startswith("text/"):
continue
new = tx.cat(path).splitlines()
old = client.cat("file://"+os.path.join(repo,path)).splitlines()
# Added trailing space can mess up the comparison -- eliminate it
new = normalize_file_end(new)
old = normalize_file_end(old)
plain_diff = list(difflib.unified_diff(old, new, "%s (repository)"%path, "%s (commit)"%path, lineterm="" ))
old = normalize_sequence(old)
new = normalize_sequence(new)
white_diff = list(difflib.unified_diff(old, new, "%s (repository)"%path, "%s (commit)"%path, lineterm=""))
plain_count = len(plain_diff)
white_count = len(white_diff)
# for i in range(len(white_diff)):
# sys.stderr.write("%-80s | %-80s\n" % (normalize(plain_diff[i][:80]), normalize(white_diff[i][:80])))
if white_count != plain_count and not is_whitespace_cleanup:
intro, plain_chunks = get_chunks(plain_diff)
intro, white_chunks = get_chunks(white_diff)
deletes = []
for chunk in white_chunks:
for i in range(len(plain_chunks)):
if chunk == plain_chunks[i]:
deletes += [i]
deletes.reverse()
for i in deletes:
del plain_chunks[i]
issue = intro
for chunk in plain_chunks:
issue += chunk
if len(plain_chunks) > 1:
are = "are"; s = "s"; an = ""
else:
are = "is"; s = ""; an = "an "
issues[path] = issue
if white_count != 0 and is_whitespace_cleanup:
intro, white_chunks = get_chunks(white_diff)
if len(white_chunks) > 1:
are = "are"; s = "s"; an = ""
else:
are = "is"; s = ""; an = "an "
issues[path] = white_diff
if issues:
if is_whitespace_cleanup:
die("It looks as if there are non-whitespace changes in\n"
"this commit, but it was marked as a whitespace cleanup commit.\n\n"
"Here %s the diff chunk%s with unexpected change%s:\n\n%s\n\n"
"Declining the commit due to a mix of code and spaces-only changes. Please\n"
"avoid mixing whitespace-only changes with code changes. See details above." %
(are, s, s, '\n\n'.join([ '\n'.join(issues[path]) for path in issues ]))
)
else:
die("It looks as if there are spaces-only changes in this\n"
"commit, but it was not marked as a whitespace cleanup commit.\n\n"
"Here %s the diff chunk%s with unexpected change%s:\n\n%s\n\n"
"Declining the commit due to a mix of code and spaces-only changes. Please\n"
"avoid mixing whitespace-only changes with code changes. See details above." %
(are, s, s, '\n\n'.join([ '\n'.join(issues[path]) for path in issues ]))
)
sys.exit(0)