Skip to content

Commit b55f764

Browse files
committed
Added tolerance for space changes after code changes at the end of a file. This is only a partial fix for space changes next to code changes in general.
- Legacy-Id: 10588
1 parent bb4706e commit b55f764

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

hooks/pre-commit

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ leave whitespace alone on lines without code changes.
99
import os
1010
import sys
1111
import difflib
12-
import debug
12+
#import debug
1313
from pysvn import Client, Transaction
1414

1515
prog = os.path.basename(sys.argv[0])
@@ -40,6 +40,14 @@ def normalize_sequence(seq):
4040
o.append(normalize(l))
4141
return o
4242

43+
def normalize_file_end(seq):
44+
while True and seq:
45+
if seq[-1].strip() == "":
46+
del seq[-1]
47+
else:
48+
break
49+
return seq
50+
4351
def count(gen):
4452
return sum(1 for _ in gen)
4553

@@ -59,7 +67,7 @@ def inc_ab(flag):
5967

6068
def get_chunks(unidiff):
6169
if not unidiff:
62-
return []
70+
return [], []
6371
chunks = []
6472
chunk = []
6573
intro = unidiff[0:2]
@@ -91,6 +99,10 @@ for path in changes:
9199
new = tx.cat(path).splitlines()
92100
old = client.cat("file://"+os.path.join(repo,path)).splitlines()
93101

102+
# Added trailing space can mess up the comparison -- eliminate it
103+
new = normalize_file_end(new)
104+
old = normalize_file_end(old)
105+
94106
plain_diff = list(difflib.unified_diff(old, new, "%s (repository)"%path, "%s (commit)"%path, lineterm="" ))
95107
old = normalize_sequence(old)
96108
new = normalize_sequence(new)
@@ -104,10 +116,14 @@ for path in changes:
104116
if white_count != plain_count and not is_whitespace_cleanup:
105117
intro, plain_chunks = get_chunks(plain_diff)
106118
intro, white_chunks = get_chunks(white_diff)
119+
deletes = []
107120
for chunk in white_chunks:
108121
for i in range(len(plain_chunks)):
109122
if chunk == plain_chunks[i]:
110-
del plain_chunks[i]
123+
deletes += [i]
124+
deletes.reverse()
125+
for i in deletes:
126+
del plain_chunks[i]
111127
issue = intro
112128
for chunk in plain_chunks:
113129
issue += chunk

0 commit comments

Comments
 (0)