diff --git a/kallithea/lib/diffs.py b/kallithea/lib/diffs.py
--- a/kallithea/lib/diffs.py
+++ b/kallithea/lib/diffs.py
@@ -445,7 +445,7 @@ class DiffProcessor(object):
return self.adds, self.removes
-_escape_re = re.compile(r'(&)|(<)|(>)|(\t)|(\r)|( $)|(\t$)')
+_escape_re = re.compile(r'(&)|(<)|(>)|(\t)($)?|(\r)|( $)')
def _escaper(diff_line):
@@ -467,7 +467,7 @@ def _escaper(diff_line):
>>> _escaper(' foo\rbar\r')
' foobar'
>>> _escaper(' foo\t')
- ' foo\t'
+ ' foo\t'
>>> _escaper(' foo ')
' foo '
>>> _escaper(' foo ')
@@ -477,15 +477,15 @@ def _escaper(diff_line):
>>> _escaper(' ')
' '
>>> _escaper(' \t')
- ' \t'
+ ' \t'
>>> _escaper(' \t ')
' \t '
>>> _escaper(' \t')
- ' \t'
+ ' \t'
>>> _escaper(' \t\t ')
' \t\t '
>>> _escaper(' \t\t')
- ' \t\t'
+ ' \t\t'
>>> _escaper(' foo&bar ')
' foo&bar<baz> '
"""
@@ -499,15 +499,15 @@ def _escaper(diff_line):
if groups[2]:
return '>'
if groups[3]:
- return '\t' # Note: trailing tabs will get a longer match later
- if groups[4]:
+ if groups[4] is not None: # end of line
+ return '\t'
+ return '\t'
+ if groups[5]:
return ''
- if groups[5]:
+ if groups[6]:
if m.start() == 0:
return ' ' # first column space shouldn't make empty lines show up as trailing space
return ' '
- if groups[6]:
- return '\t'
assert False
return _escape_re.sub(substitute, diff_line)
diff --git a/kallithea/tests/models/test_diff_parsers.py b/kallithea/tests/models/test_diff_parsers.py
--- a/kallithea/tests/models/test_diff_parsers.py
+++ b/kallithea/tests/models/test_diff_parsers.py
@@ -297,13 +297,13 @@ class TestDiffLib(base.TestController):
assert s == r'''
context '@@ -51,6 +51,13 @@'
unmod 51 51 '\tbegin();'
-unmod 52 52 '\t'
+unmod 52 52 '\t'
add 53 '\tint foo;'
add 54 '\tint bar; '
add 55 '\tint baz;\t'
add 56 '\tint space; '
-add 57 '\tint tab;\t'
-add 58 '\t'
+add 57 '\tint tab;\t'
+add 58 '\t'
unmod 59 53 ' '
del 54 '\t#define MAX_STEPS (48)'
add 60 '\t'