~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/revision_ui.py

  • Committer: Michael Hudson
  • Date: 2009-03-19 18:48:15 UTC
  • mfrom: (305.1.16 iter_changes-forevar)
  • Revision ID: michael.hudson@canonical.com-20090319184815-wnj0kvcnzx31n1lq
simplify the internal handling of differences between revisions, and in
particular use iter_changes to find said changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from loggerhead import util
29
29
from loggerhead.controllers import TemplatedBranchView
30
30
from loggerhead.controllers.filediff_ui import diff_chunks_for_file
31
 
from loggerhead.history import rich_filename
32
31
 
33
32
 
34
33
DEFAULT_LINE_COUNT_LIMIT = 3000
36
35
def dq(p):
37
36
    return urllib.quote(urllib.quote(p, safe=''))
38
37
 
 
38
 
39
39
class RevisionUI(TemplatedBranchView):
40
40
 
41
41
    template_path = 'loggerhead.templates.revision'
42
42
 
43
 
    def _parse_diffs(self, old_tree, new_tree, delta, specific_path):
44
 
        """
45
 
        Return a list of processed diffs, in the format::
46
 
 
47
 
            list(
48
 
                filename: str,
49
 
                file_id: str,
50
 
                chunks: list(
51
 
                    diff: list(
52
 
                        old_lineno: int,
53
 
                        new_lineno: int,
54
 
                        type: str('context', 'delete', or 'insert'),
55
 
                        line: str,
56
 
                    ),
57
 
                ),
58
 
            )
59
 
        """
60
 
        if specific_path:
61
 
            fid = new_tree.path2id(specific_path)
62
 
            kind = new_tree.kind(fid)
63
 
            chunks=diff_chunks_for_file(fid, old_tree, new_tree)
64
 
            return [util.Container(
65
 
                filename=rich_filename(specific_path, kind), file_id=fid,
66
 
                chunks=chunks)]
67
 
 
68
 
        process = []
69
 
        out = []
70
 
 
71
 
        for old_path, new_path, fid, \
72
 
            kind, text_modified, meta_modified in delta.renamed:
73
 
            if text_modified:
74
 
                process.append((new_path, fid, kind))
75
 
        for path, fid, kind, text_modified, meta_modified in delta.modified:
76
 
            process.append((path, fid, kind))
77
 
        for path, fid, kind in delta.added:
78
 
            if kind == 'file':
79
 
                process.append((path, fid, kind))
80
 
        for path, fid, kind in delta.removed:
81
 
            if kind == 'file':
82
 
                process.append((path, fid, kind))
83
 
 
84
 
        process.sort()
85
 
 
86
 
        for new_path, fid, kind in process:
87
 
            out.append(util.Container(
88
 
                filename=rich_filename(new_path, kind), file_id=fid,
89
 
                chunks=[]))
90
 
 
91
 
        return out
92
 
 
93
 
    def get_changes_with_diff(self, change, compare_revid, specific_path):
94
 
        h = self._history
95
 
        if compare_revid is None:
96
 
            if change.parents:
97
 
                compare_revid = change.parents[0].revid
98
 
            else:
99
 
                compare_revid = 'null:'
100
 
 
101
 
        rev_tree1 = h._branch.repository.revision_tree(compare_revid)
102
 
        rev_tree2 = h._branch.repository.revision_tree(change.revid)
103
 
        delta = rev_tree2.changes_from(rev_tree1)
104
 
 
105
 
        changes = h.parse_delta(delta)
106
 
 
107
 
        return changes, self._parse_diffs(
108
 
            rev_tree1, rev_tree2, delta, specific_path)
109
 
 
110
43
    def get_values(self, path, kwargs, headers):
111
44
        h = self._history
112
45
        revid = self.get_revid()
135
68
        util.fill_in_navigation(navigation)
136
69
 
137
70
        change = h.get_changes([revid])[0]
 
71
 
 
72
        if compare_revid is None:
 
73
            if change.parents:
 
74
                cr = change.parents[0].revid
 
75
            else:
 
76
                cr = 'null:'
 
77
        else:
 
78
            cr = compare_revid
 
79
 
138
80
        if path in ('', '/'):
139
81
            path = None
140
 
        change.changes, diffs = self.get_changes_with_diff(change, compare_revid, path)
 
82
 
 
83
        old_tree = h._branch.repository.revision_tree(cr)
 
84
        new_tree = h._branch.repository.revision_tree(change.revid)
 
85
        file_changes = h.file_changes_from_revision_trees(
 
86
            old_tree, new_tree)
 
87
 
141
88
        link_data = {}
142
89
        path_to_id = {}
143
 
        if compare_revid is None:
144
 
            if change.parents:
145
 
                cr = change.parents[0].revid
146
 
            else:
147
 
                cr = 'null:'
 
90
        if path:
 
91
            diff_chunks = diff_chunks_for_file(
 
92
                new_tree.path2id(path), old_tree, new_tree)
148
93
        else:
149
 
            cr = compare_revid
150
 
        for i, item in enumerate(diffs):
151
 
            item.index = i
152
 
            link_data['diff-' + str(i)] = '%s/%s/%s' % (
153
 
                dq(revid), dq(cr), dq(item.file_id))
154
 
            path_to_id[item.filename] = 'diff-' + str(i)
155
 
        # add parent & merge-point branch-nick info, in case it's useful
156
 
        h.get_branch_nicks([change])
 
94
            diff_chunks = None
 
95
            for i, item in enumerate(file_changes.text_changes):
 
96
                item.index = i
 
97
                link_data['diff-' + str(i)] = '%s/%s/%s' % (
 
98
                    dq(revid), dq(cr), dq(item.file_id))
 
99
                path_to_id[item.filename] = 'diff-' + str(i)
 
100
 
 
101
        h.add_branch_nicks(change)
157
102
 
158
103
        # Directory Breadcrumbs
159
104
        directory_breadcrumbs = (
166
111
            'branch': self._branch,
167
112
            'revid': revid,
168
113
            'change': change,
169
 
            'diffs': diffs,
 
114
            'file_changes': file_changes,
 
115
            'diff_chunks': diff_chunks,
170
116
            'link_data': simplejson.dumps(link_data),
171
117
            'specific_path': path,
172
118
            'json_specific_path': simplejson.dumps(path),