@@ -125,24 +125,25 @@ def get_gitdiff(filenode_old, filenode_n
vcs_gitdiff = repo.get_diff(old_raw_id, new_raw_id, filenode_new.path,
ignore_whitespace, context)
return vcs_gitdiff
class DiffProcessor(object):
"""
Give it a unified diff and it returns a list of the files that were
mentioned in the diff together with a dict of meta information that
can be used to render it in a HTML template.
_chunk_re = re.compile(r'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)')
_newline_marker = '\\ No newline at end of file\n'
def __init__(self, diff, differ='diff', format='gitdiff'):
:param diff: a text in diff format or generator
:param format: format of diff passed, `udiff` or `gitdiff`
if isinstance(diff, basestring):
diff = [diff]
self.__udiff = diff
self.__format = format
self.adds = 0
@@ -336,65 +337,72 @@ class DiffProcessor(object):
if context:
# skip context only if it's first line
if int(gr[0]) > 1:
lines.append({
'old_lineno': '...',
'new_lineno': '...',
'action': 'context',
'line': line,
})
line = lineiter.next()
while old_line < old_end or new_line < new_end:
if line:
command, line = line[0], line[1:]
command = line[0]
if command in ['+', '-', ' ']:
#only modify the line if it's actually a diff
# thing
line = line[1:]
else:
command = ' '
affects_old = affects_new = False
# ignore those if we don't expect them
if command in '#@':
continue
elif command == '+':
affects_new = True
action = 'add'
stats[0] += 1
elif command == '-':
affects_old = True
action = 'del'
stats[1] += 1
affects_old = affects_new = True
action = 'unmod'
if line.find('No newline at end of file') != -1:
'line': line
if line != self._newline_marker:
old_line += affects_old
new_line += affects_new
'old_lineno': affects_old and old_line or '',
'new_lineno': affects_new and new_line or '',
'action': action,
if line == self._newline_marker:
# we need to append to lines, since this is not
# counted in the line specs of diff
except StopIteration:
pass
sorter = lambda info: {'A': 0, 'M': 1, 'D': 2}.get(info['operation'])
if inline_diff is False:
return sorted(files, key=sorter)
# highlight inline changes
for diff_data in files:
for chunk in diff_data['chunks']:
lineiter = iter(chunk)
Status change: