~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/revision_ui.py

  • Committer: Michael Hudson
  • Date: 2008-06-20 02:16:39 UTC
  • mfrom: (128.8.15 zpt.cleaner_urls)
  • Revision ID: michael.hudson@canonical.com-20080620021639-iz2738k6jomjue3t
merge zpt.cleaner_urls, which generates urls containing revision numbers rather
than revision ids.

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
 
try:
21
 
    import simplejson
22
 
except ImportError:
23
 
    import json as simplejson
24
 
import urllib
 
20
import time
25
21
 
26
 
from paste.httpexceptions import HTTPServerError
 
22
import turbogears
 
23
from cherrypy import InternalError
27
24
 
28
25
from loggerhead import util
29
 
from loggerhead.controllers import TemplatedBranchView
30
 
from loggerhead.controllers.filediff_ui import diff_chunks_for_file
 
26
from loggerhead.templatefunctions import templatefunctions
31
27
 
32
28
 
33
29
DEFAULT_LINE_COUNT_LIMIT = 3000
34
30
 
35
 
def dq(p):
36
 
    return urllib.quote(urllib.quote(p, safe=''))
37
 
 
38
 
 
39
 
class RevisionUI(TemplatedBranchView):
40
 
 
41
 
    template_path = 'loggerhead.templates.revision'
42
 
 
43
 
    def get_values(self, path, kwargs, headers):
44
 
        h = self._history
45
 
        revid = self.get_revid()
46
 
 
47
 
        filter_file_id = kwargs.get('filter_file_id', None)
48
 
        start_revid = h.fix_revid(kwargs.get('start_revid', None))
49
 
        query = kwargs.get('q', None)
50
 
        remember = h.fix_revid(kwargs.get('remember', None))
51
 
        compare_revid = h.fix_revid(kwargs.get('compare_revid', None))
52
 
 
 
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='zpt: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()
53
48
        try:
54
 
            revid, start_revid, revid_list = h.get_view(revid,
55
 
                                                        start_revid,
56
 
                                                        filter_file_id,
57
 
                                                        query)
58
 
        except:
59
 
            self.log.exception('Exception fetching changes')
60
 
            raise HTTPServerError('Could not fetch changes')
61
 
 
62
 
        navigation = util.Container(
63
 
            revid_list=revid_list, revid=revid, start_revid=start_revid,
64
 
            filter_file_id=filter_file_id, pagesize=1,
65
 
            scan_url='/revision', branch=self._branch, feed=True, history=h)
66
 
        if query is not None:
67
 
            navigation.query = query
68
 
        util.fill_in_navigation(navigation)
69
 
 
70
 
        change = h.get_changes([revid])[0]
71
 
 
72
 
        if compare_revid is None:
73
 
            file_changes = h.get_file_changes(change)
74
 
        else:
75
 
            file_changes = h.file_changes_for_revision_ids(
76
 
                compare_revid, change.revid)
77
 
 
78
 
        if path in ('', '/'):
79
 
            path = None
80
 
 
81
 
        link_data = {}
82
 
        path_to_id = {}
83
 
        if path:
84
 
            item = [x for x in file_changes.text_changes if x.filename == path][0]
85
 
            diff_chunks = diff_chunks_for_file(
86
 
                self._history._branch.repository, item.file_id,
87
 
                item.old_revision, item.new_revision)
88
 
        else:
89
 
            diff_chunks = None
90
 
            for i, item in enumerate(file_changes.text_changes):
91
 
                item.index = i
92
 
                link_data['diff-' + str(i)] = '%s/%s/%s' % (
93
 
                    dq(item.new_revision), dq(item.old_revision), dq(item.file_id))
94
 
                path_to_id[item.filename] = 'diff-' + str(i)
95
 
 
96
 
        h.add_branch_nicks(change)
97
 
 
98
 
        # Directory Breadcrumbs
99
 
        directory_breadcrumbs = (
100
 
            util.directory_breadcrumbs(
101
 
                self._branch.friendly_name,
102
 
                self._branch.is_root,
103
 
                'changes'))
104
 
 
105
 
        return {
106
 
            'branch': self._branch,
107
 
            'revid': revid,
108
 
            'change': change,
109
 
            'file_changes': file_changes,
110
 
            'diff_chunks': diff_chunks,
111
 
            'link_data': simplejson.dumps(link_data),
112
 
            'specific_path': path,
113
 
            'json_specific_path': simplejson.dumps(path),
114
 
            'path_to_id': simplejson.dumps(path_to_id),
115
 
            'start_revid': start_revid,
116
 
            'filter_file_id': filter_file_id,
117
 
            'util': util,
118
 
            'history': h,
119
 
            'navigation': navigation,
120
 
            'query': query,
121
 
            'remember': remember,
122
 
            'compare_revid': compare_revid,
123
 
            'url': self._branch.context_url,
124
 
            'directory_breadcrumbs': directory_breadcrumbs,
125
 
        }
 
49
            if len(args) > 0:
 
50
                revid = h.fix_revid(args[0])
 
51
            else:
 
52
                revid = None
 
53
 
 
54
            filter_file_id = kw.get('filter_file_id', None)
 
55
            start_revid = h.fix_revid(kw.get('start_revid', None))
 
56
            query = kw.get('q', None)
 
57
            remember = h.fix_revid(kw.get('remember', None))
 
58
            compare_revid = h.fix_revid(kw.get('compare_revid', None))
 
59
 
 
60
            try:
 
61
                revid, start_revid, revid_list = h.get_view(revid, start_revid, filter_file_id, query)
 
62
            except:
 
63
                self.log.exception('Exception fetching changes')
 
64
                raise InternalError('Could not fetch changes')
 
65
 
 
66
            navigation = util.Container(
 
67
                revid_list=revid_list, revid=revid, start_revid=start_revid,
 
68
                filter_file_id=filter_file_id, pagesize=1,
 
69
                scan_url='/revision', branch=self._branch, feed=True)
 
70
            if query is not None:
 
71
                navigation.query = query
 
72
            util.fill_in_navigation(navigation)
 
73
 
 
74
            change = h.get_change_with_diff(revid, compare_revid)
 
75
            # add parent & merge-point branch-nick info, in case it's useful
 
76
            h.get_branch_nicks([ change ])
 
77
 
 
78
            line_count_limit = int(self._branch.get_config_item(
 
79
                'line_count_limit', DEFAULT_LINE_COUNT_LIMIT))
 
80
            line_count = 0
 
81
            for file in change.changes.modified:
 
82
                for chunk in file.chunks:
 
83
                    line_count += len(chunk.diff)
 
84
 
 
85
            # let's make side-by-side diff be the default
 
86
            side_by_side = not kw.get('unified', False)
 
87
            if side_by_side:
 
88
                h.add_side_by_side([ change ])
 
89
 
 
90
            vals = {
 
91
                'branch': self._branch,
 
92
                'revid': revid,
 
93
                'change': change,
 
94
                'start_revid': start_revid,
 
95
                'filter_file_id': filter_file_id,
 
96
                'util': util,
 
97
                'history': h,
 
98
                'navigation': navigation,
 
99
                'query': query,
 
100
                'remember': remember,
 
101
                'compare_revid': compare_revid,
 
102
                'side_by_side': side_by_side,
 
103
                'url': self._branch.context_url,
 
104
                'line_count': line_count,
 
105
                'line_count_limit': line_count_limit,
 
106
                'show_plain_diffs': line_count > line_count_limit,
 
107
            }
 
108
            vals.update(templatefunctions)
 
109
            self.log.info('/revision: %r seconds' % (time.time() - z,))
 
110
            return vals
 
111
        finally:
 
112
            h._branch.unlock()