~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/revision_ui.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-09-10 17:30:17 UTC
  • mfrom: (138.1.1 mwhudson-sucks)
  • Revision ID: pqm@pqm.ubuntu.com-20070910173017-teattztsb8ep0aer
[trivial] so i cleaned up the tests, but also broke them :( need to look at getting PQM to run the loggerhead tests...

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
#
19
19
 
20
 
import simplejson
21
 
import urllib
 
20
import datetime
 
21
import logging
 
22
import os
 
23
import textwrap
 
24
import time
22
25
 
23
 
from paste.httpexceptions import HTTPServerError
 
26
import turbogears
 
27
from cherrypy import InternalError, session
24
28
 
25
29
from loggerhead import util
26
 
from loggerhead.controllers import TemplatedBranchView
27
 
from loggerhead.controllers.filediff_ui import diff_chunks_for_file
28
 
 
29
 
 
30
 
DEFAULT_LINE_COUNT_LIMIT = 3000
31
 
 
32
 
def dq(p):
33
 
    return urllib.quote(urllib.quote(p, safe=''))
34
 
 
35
 
 
36
 
class RevisionUI(TemplatedBranchView):
37
 
 
38
 
    template_path = 'loggerhead.templates.revision'
39
 
 
40
 
    def get_values(self, path, kwargs, headers):
41
 
        h = self._history
42
 
        revid = self.get_revid()
43
 
 
44
 
        filter_file_id = kwargs.get('filter_file_id', None)
45
 
        start_revid = h.fix_revid(kwargs.get('start_revid', None))
46
 
        query = kwargs.get('q', None)
47
 
        remember = h.fix_revid(kwargs.get('remember', None))
48
 
        compare_revid = h.fix_revid(kwargs.get('compare_revid', None))
49
 
 
 
30
 
 
31
 
 
32
class RevisionUI (object):
 
33
 
 
34
    def __init__(self, branch):
 
35
        # BranchView object
 
36
        self._branch = branch
 
37
        self.log = branch.log
 
38
    
 
39
#    @util.lsprof
 
40
    @util.strip_whitespace
 
41
    @turbogears.expose(html='loggerhead.templates.revision')
 
42
    def default(self, *args, **kw):
 
43
        z = time.time()
 
44
        h = self._branch.get_history()
 
45
        util.set_context(kw)
 
46
        
 
47
        h._branch.lock_read()
50
48
        try:
51
 
            revid, start_revid, revid_list = h.get_view(revid,
52
 
                                                        start_revid,
53
 
                                                        filter_file_id,
54
 
                                                        query)
55
 
        except:
56
 
            self.log.exception('Exception fetching changes')
57
 
            raise HTTPServerError('Could not fetch changes')
58
 
 
59
 
        navigation = util.Container(
60
 
            revid_list=revid_list, revid=revid, start_revid=start_revid,
61
 
            filter_file_id=filter_file_id, pagesize=1,
62
 
            scan_url='/revision', branch=self._branch, feed=True, history=h)
63
 
        if query is not None:
64
 
            navigation.query = query
65
 
        util.fill_in_navigation(navigation)
66
 
 
67
 
        change = h.get_changes([revid])[0]
68
 
 
69
 
        if compare_revid is None:
70
 
            file_changes = h.get_file_changes(change)
71
 
        else:
72
 
            file_changes = h.file_changes_for_revision_ids(
73
 
                compare_revid, change.revid)
74
 
 
75
 
        if path in ('', '/'):
76
 
            path = None
77
 
 
78
 
        link_data = {}
79
 
        path_to_id = {}
80
 
        if path:
81
 
            item = [x for x in file_changes.text_changes if x.filename == path][0]
82
 
            diff_chunks = diff_chunks_for_file(
83
 
                self._history._branch.repository, item.file_id,
84
 
                item.old_revision, item.new_revision)
85
 
        else:
86
 
            diff_chunks = None
87
 
            for i, item in enumerate(file_changes.text_changes):
88
 
                item.index = i
89
 
                link_data['diff-' + str(i)] = '%s/%s/%s' % (
90
 
                    dq(item.new_revision), dq(item.old_revision), dq(item.file_id))
91
 
                path_to_id[item.filename] = 'diff-' + str(i)
92
 
 
93
 
        h.add_branch_nicks(change)
94
 
 
95
 
        if '.' in change.revno:
96
 
            # Walk "up" though the merge-sorted graph until we find a
97
 
            # revision with merge depth 0: this is the revision that merged
98
 
            # this one to mainline.
99
 
            ri = self._history._rev_info
100
 
            i = self._history._rev_indices[change.revid]
101
 
            while ri[i][0][2] > 0:
102
 
                i -= 1
103
 
            merged_in = ri[i][0][3]
104
 
        else:
105
 
            merged_in = None
106
 
 
107
 
        # Directory Breadcrumbs
108
 
        directory_breadcrumbs = (
109
 
            util.directory_breadcrumbs(
110
 
                self._branch.friendly_name,
111
 
                self._branch.is_root,
112
 
                'changes'))
113
 
 
114
 
        return {
115
 
            'branch': self._branch,
116
 
            'revid': revid,
117
 
            'change': change,
118
 
            'file_changes': file_changes,
119
 
            'diff_chunks': diff_chunks,
120
 
            'link_data': simplejson.dumps(link_data),
121
 
            'specific_path': path,
122
 
            'json_specific_path': simplejson.dumps(path),
123
 
            'path_to_id': simplejson.dumps(path_to_id),
124
 
            'start_revid': start_revid,
125
 
            'filter_file_id': filter_file_id,
126
 
            'util': util,
127
 
            'history': h,
128
 
            'merged_in': merged_in,
129
 
            'navigation': navigation,
130
 
            'query': query,
131
 
            'remember': remember,
132
 
            'compare_revid': compare_revid,
133
 
            'url': self._branch.context_url,
134
 
            'directory_breadcrumbs': directory_breadcrumbs,
135
 
        }
 
49
            if len(args) > 0:
 
50
                revid = h.fix_revid(args[0])
 
51
            else:
 
52
                revid = None
 
53
 
 
54
            file_id = kw.get('file_id', None)
 
55
            start_revid = h.fix_revid(kw.get('start_revid', None))
 
56
            query = kw.get('q', None)
 
57
            remember = kw.get('remember', None)
 
58
            compare_revid = kw.get('compare_revid', None)
 
59
 
 
60
            try:
 
61
                revid, start_revid, revid_list = h.get_view(revid, start_revid, file_id, query)
 
62
            except:
 
63
                self.log.exception('Exception fetching changes')
 
64
                raise InternalError('Could not fetch changes')
 
65
 
 
66
            navigation = util.Container(revid_list=revid_list, revid=revid, start_revid=start_revid, file_id=file_id,
 
67
                                        pagesize=1, scan_url='/revision', branch=self._branch, feed=True)
 
68
            if query is not None:
 
69
                navigation.query = query
 
70
            util.fill_in_navigation(navigation)
 
71
 
 
72
            change = h.get_change_with_diff(revid, compare_revid)
 
73
            # add parent & merge-point branch-nick info, in case it's useful
 
74
            h.get_branch_nicks([ change ])
 
75
 
 
76
            # let's make side-by-side diff be the default
 
77
            side_by_side = not kw.get('unified', False)
 
78
            if side_by_side:
 
79
                h.add_side_by_side([ change ])
 
80
 
 
81
            vals = {
 
82
                'branch': self._branch,
 
83
                'revid': revid,
 
84
                'change': change,
 
85
                'start_revid': start_revid,
 
86
                'file_id': file_id,
 
87
                'util': util,
 
88
                'history': h,
 
89
                'navigation': navigation,
 
90
                'query': query,
 
91
                'remember': remember,
 
92
                'compare_revid': compare_revid,
 
93
                'side_by_side': side_by_side,
 
94
            }
 
95
            h.flush_cache()
 
96
            self.log.info('/revision: %r seconds' % (time.time() - z,))
 
97
            return vals
 
98
        finally:
 
99
            h._branch.unlock()