~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/filediff_ui.py

  • Committer: Michael Hudson
  • Date: 2009-02-26 20:55:46 UTC
  • Revision ID: michael.hudson@canonical.com-20090226205546-purw3ew11u6t5gt7
clip long lines in sbs diff view
this is still pretty lame, but it's better than it was.
see bug #334837

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from StringIO import StringIO
2
 
import urllib
3
 
 
4
 
from bzrlib import diff
5
 
from bzrlib import errors
6
 
 
7
 
from loggerhead import util
8
 
from loggerhead.controllers import TemplatedBranchView
9
 
 
10
 
def _process_diff(difftext):
11
 
    # doesn't really need to be a method; could be static.
12
 
    chunks = []
13
 
    chunk = None
14
 
    for line in difftext.splitlines():
15
 
        if len(line) == 0:
16
 
            continue
17
 
        if line.startswith('+++ ') or line.startswith('--- '):
18
 
            continue
19
 
        if line.startswith('@@ '):
20
 
            # new chunk
21
 
            if chunk is not None:
22
 
                chunks.append(chunk)
23
 
            chunk = util.Container()
24
 
            chunk.diff = []
25
 
            split_lines = line.split(' ')[1:3]
26
 
            lines = [int(x.split(',')[0][1:]) for x in split_lines]
27
 
            old_lineno = lines[0]
28
 
            new_lineno = lines[1]
29
 
        elif line.startswith(' '):
30
 
            chunk.diff.append(util.Container(old_lineno=old_lineno,
31
 
                                             new_lineno=new_lineno,
32
 
                                             type='context',
33
 
                                             line=line[1:]))
34
 
            old_lineno += 1
35
 
            new_lineno += 1
36
 
        elif line.startswith('+'):
37
 
            chunk.diff.append(util.Container(old_lineno=None,
38
 
                                             new_lineno=new_lineno,
39
 
                                             type='insert', line=line[1:]))
40
 
            new_lineno += 1
41
 
        elif line.startswith('-'):
42
 
            chunk.diff.append(util.Container(old_lineno=old_lineno,
43
 
                                             new_lineno=None,
44
 
                                             type='delete', line=line[1:]))
45
 
            old_lineno += 1
46
 
        else:
47
 
            chunk.diff.append(util.Container(old_lineno=None,
48
 
                                             new_lineno=None,
49
 
                                             type='unknown',
50
 
                                             line=repr(line)))
51
 
    if chunk is not None:
52
 
        chunks.append(chunk)
53
 
    return chunks
54
 
 
55
 
 
56
 
def diff_chunks_for_file(repository, file_id, new_revid, old_revid):
57
 
    old_tree, new_tree = repository.revision_trees([old_revid, new_revid])
58
 
    try:
59
 
        old_lines = old_tree.get_file_lines(file_id)
60
 
    except errors.NoSuchId:
61
 
        old_lines = []
62
 
    try:
63
 
        new_lines = new_tree.get_file_lines(file_id)
64
 
    except errors.NoSuchId:
65
 
        new_lines = []
66
 
    buffer = StringIO()
67
 
    if old_lines != new_lines:
68
 
        try:
69
 
            diff.internal_diff('', old_lines, '', new_lines, buffer)
70
 
        except errors.BinaryFile:
71
 
            difftext = ''
72
 
        else:
73
 
            difftext = buffer.getvalue()
74
 
    else:
75
 
        difftext = ''
76
 
 
77
 
    return _process_diff(difftext)
78
 
 
79
 
 
80
 
class FileDiffUI(TemplatedBranchView):
81
 
 
82
 
    template_path = 'loggerhead.templates.filediff'
83
 
 
84
 
    def get_values(self, path, kwargs, headers):
85
 
        history = self._history
86
 
 
87
 
        revid = urllib.unquote(self.args[0])
88
 
        compare_revid = urllib.unquote(self.args[1])
89
 
        file_id = urllib.unquote(self.args[2])
90
 
 
91
 
        chunks = diff_chunks_for_file(
92
 
            self._history._branch.repository, file_id, revid, compare_revid)
93
 
 
94
 
        return {
95
 
            'util': util,
96
 
            'chunks': chunks,
97
 
        }