~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/diff_ui.py

  • Committer: Robey Pointer
  • Date: 2007-01-24 18:43:51 UTC
  • Revision ID: robey@lag.net-20070124184351-waw1sy4hjtpewpp1
bump up version number to 1.1 and change push site.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008  Canonical Ltd.
2
 
#                     (Authored by Martin Albisetti <argentina@gmail.com>)
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
#
18
 
 
19
 
from cStringIO import StringIO
20
 
import logging
21
 
import time
22
 
 
23
 
from paste.request import path_info_pop
24
 
 
25
 
from bzrlib.diff import show_diff_trees
26
 
 
27
 
 
28
 
log = logging.getLogger("loggerhead.controllers")
29
 
 
30
 
 
31
 
class DiffUI(object):
32
 
    """Class to output a diff for a single file or revisions."""
33
 
 
34
 
    def __init__(self, branch, history):
35
 
        self._branch = branch
36
 
        self._history = history
37
 
        self.log = history.log
38
 
 
39
 
    def __call__(self, environ, start_response):
40
 
        # /diff/<rev_id>/<rev_id>
41
 
        """Default method called from /diff URL."""
42
 
        z = time.time()
43
 
 
44
 
        args = []
45
 
        while 1:
46
 
            arg = path_info_pop(environ)
47
 
            if arg is None:
48
 
                break
49
 
            args.append(arg)
50
 
 
51
 
        revid_from = args[0]
52
 
        # Convert a revno to a revid if we get a revno
53
 
        revid_from = self._history.fix_revid(revid_from)
54
 
        change = self._history.get_changes([revid_from])[0]
55
 
 
56
 
        if len(args) is 2:
57
 
            revid_to = self._history.fix_revid(args[1])
58
 
        else:
59
 
            revid_to = change.parents[0].revid
60
 
 
61
 
        repo = self._branch.branch.repository
62
 
        revtree1 = repo.revision_tree(revid_to)
63
 
        revtree2 = repo.revision_tree(revid_from)
64
 
 
65
 
        diff_content_stream = StringIO()
66
 
        show_diff_trees(revtree1, revtree2, diff_content_stream,
67
 
                        old_label='', new_label='')
68
 
 
69
 
        content = diff_content_stream.getvalue()
70
 
 
71
 
        self.log.info('/diff %r:%r in %r secs' % (revid_from, revid_to,
72
 
                                                  time.time() - z))
73
 
 
74
 
        revno1 = self._history.get_revno(revid_from)
75
 
        revno2 = self._history.get_revno(revid_to)
76
 
        filename = '%s_%s.diff' % (revno1, revno2)
77
 
        headers = [
78
 
            ('Content-Type', 'application/octet-stream'),
79
 
            ('Content-Length', len(content)),
80
 
            ('Content-Disposition', 'attachment; filename=%s' % filename),
81
 
            ]
82
 
        start_response('200 OK', headers)
83
 
        return [content]