~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/diff_ui.py

  • Committer: Marius Kruger
  • Date: 2008-09-06 20:17:56 UTC
  • mto: This revision was merged to the branch mainline in revision 219.
  • Revision ID: amanic@gmail.com-20080906201756-ocqnga9vu1uj425t
Add alternative insallation notes and mention the --prefix option in README.txt

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
import sys
 
23
 
 
24
from paste import httpexceptions
 
25
from paste.request import path_info_pop
 
26
 
 
27
from loggerhead import history
 
28
from loggerhead import util
 
29
from loggerhead.templatefunctions import templatefunctions
 
30
 
 
31
import bzrlib
 
32
from bzrlib import branch
 
33
from bzrlib.diff import show_diff_trees
 
34
 
 
35
 
 
36
log = logging.getLogger("loggerhead.controllers")
 
37
 
 
38
class DiffUI(object):
 
39
    """
 
40
 
 
41
    Class to output a diff for a single file or revisions.
 
42
    """
 
43
    
 
44
    def __init__(self, branch, history):
 
45
        self._branch = branch
 
46
        self._history = history
 
47
        self.log = history.log
 
48
 
 
49
    
 
50
    def __call__(self, environ, start_response):
 
51
        # /diff/<rev_id>/<rev_id>
 
52
        """
 
53
        Default method called from /diff URL.
 
54
        """
 
55
 
 
56
        z = time.time()
 
57
        
 
58
        args = []
 
59
        while 1:
 
60
            arg = path_info_pop(environ)
 
61
            if arg is None:
 
62
                break
 
63
            args.append(arg)
 
64
 
 
65
        revid_from = args[0]
 
66
        # Convert a revno to a revid if we get a revno
 
67
        revid_from = self._history.fix_revid(revid_from)
 
68
        change = self._history.get_changes([revid_from])[0]
 
69
 
 
70
        if len(args) is 2:
 
71
            revid_to = self._history.fix_revid(args[1])
 
72
        else:
 
73
            revid_to = change.parents[0].revid 
 
74
 
 
75
 
 
76
        repo = self._branch.branch.repository
 
77
        revtree1 = repo.revision_tree(revid_from)
 
78
        revtree2 = repo.revision_tree(revid_to)
 
79
        
 
80
        diff_content_stream = StringIO()
 
81
        show_diff_trees(revtree1, revtree2, diff_content_stream, 
 
82
                        old_label='', new_label='')
 
83
 
 
84
        content = diff_content_stream.getvalue()
 
85
 
 
86
        self.log.info('/diff %r:%r in %r secs' % (revid_from, revid_to, 
 
87
                                                  time.time() - z))
 
88
 
 
89
        revno1 = self._history.get_revno(revid_from)
 
90
        revno2 = self._history.get_revno(revid_to)
 
91
        filename = '%s_%s.diff' % (revno1, revno2)
 
92
        headers = [
 
93
            ('Content-Type', 'application/octet-stream'),
 
94
            ('Content-Length', len(content)),
 
95
            ('Content-Disposition', 'attachment; filename=%s'%(filename,)),
 
96
            ]
 
97
        start_response('200 OK', headers)
 
98
        return [content]
 
99