1
from StringIO import StringIO
4
from bzrlib import diff
5
from bzrlib import errors
7
from loggerhead import util
8
from loggerhead.controllers import TemplatedBranchView
10
def _process_diff(difftext):
13
for line in difftext.splitlines():
16
if line.startswith('+++ ') or line.startswith('--- '):
18
if line.startswith('@@ '):
22
chunk = util.Container()
24
split_lines = line.split(' ')[1:3]
25
lines = [int(x.split(',')[0][1:]) for x in split_lines]
28
elif line.startswith(' '):
29
chunk.diff.append(util.Container(old_lineno=old_lineno,
30
new_lineno=new_lineno,
35
elif line.startswith('+'):
36
chunk.diff.append(util.Container(old_lineno=None,
37
new_lineno=new_lineno,
38
type='insert', line=line[1:]))
40
elif line.startswith('-'):
41
chunk.diff.append(util.Container(old_lineno=old_lineno,
43
type='delete', line=line[1:]))
46
chunk.diff.append(util.Container(old_lineno=None,
55
def diff_chunks_for_file(file_id, old_tree, new_tree):
57
old_lines = old_tree.get_file_lines(file_id)
58
except errors.NoSuchId:
61
new_lines = new_tree.get_file_lines(file_id)
62
except errors.NoSuchId:
65
if old_lines != new_lines:
67
diff.internal_diff('', old_lines, '', new_lines, buffer)
68
except errors.BinaryFile:
71
difftext = buffer.getvalue()
75
return _process_diff(difftext)
78
class FileDiffUI(TemplatedBranchView):
80
template_path = 'loggerhead.templates.filediff'
82
def get_values(self, path, kwargs, headers):
83
history = self._history
85
revid = urllib.unquote(self.args[0])
86
compare_revid = urllib.unquote(self.args[1])
87
file_id = urllib.unquote(self.args[2])
89
repository = self._history._branch.repository
90
old_tree, new_tree = repository.revision_trees([compare_revid, revid])
92
chunks = diff_chunks_for_file(file_id, old_tree, new_tree)