1
from StringIO import StringIO
4
from bzrlib import diff
5
from bzrlib import errors
6
from bzrlib import osutils
8
from loggerhead import util
9
from loggerhead.controllers import TemplatedBranchView
11
def _process_diff(difftext):
14
for line in difftext.splitlines():
17
if line.startswith('+++ ') or line.startswith('--- '):
19
if line.startswith('@@ '):
23
chunk = util.Container()
25
split_lines = line.split(' ')[1:3]
26
lines = [int(x.split(',')[0][1:]) for x in split_lines]
29
elif line.startswith(' '):
30
chunk.diff.append(util.Container(old_lineno=old_lineno,
31
new_lineno=new_lineno,
36
elif line.startswith('+'):
37
chunk.diff.append(util.Container(old_lineno=None,
38
new_lineno=new_lineno,
39
type='insert', line=line[1:]))
41
elif line.startswith('-'):
42
chunk.diff.append(util.Container(old_lineno=old_lineno,
44
type='delete', line=line[1:]))
47
chunk.diff.append(util.Container(old_lineno=None,
56
def diff_chunks_for_file(repository, file_id, compare_revid, revid):
59
for r in (compare_revid, revid):
63
args.append((file_id, r, r))
64
for r, bytes_iter in repository.iter_files_bytes(args):
65
lines[r] = osutils.split_lines(''.join(bytes_iter))
68
diff.internal_diff('', lines[compare_revid], '', lines[revid], buffer)
69
except errors.BinaryFile:
72
difftext = buffer.getvalue()
74
return _process_diff(difftext)
77
class FileDiffUI(TemplatedBranchView):
79
template_path = 'loggerhead.templates.filediff'
81
def get_values(self, path, kwargs, headers):
82
history = self._history
84
revid = urllib.unquote(self.args[0])
85
compare_revid = urllib.unquote(self.args[1])
86
file_id = urllib.unquote(self.args[2])
88
repository = self._history._branch.repository
89
chunks = diff_chunks_for_file(
90
self._history._branch.repository, file_id, compare_revid, revid)