Mercurial > kallithea
comparison rhodecode/controllers/changeset.py @ 547:1e757ac98988
renamed project to rhodecode
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 06 Oct 2010 03:18:16 +0200 |
parents | pylons_app/controllers/changeset.py@d8778cde98f0 |
children | 7fcc539e20b1 |
comparison
equal
deleted
inserted
replaced
546:7c2f5e4d7bbf | 547:1e757ac98988 |
---|---|
1 #!/usr/bin/env python | |
2 # encoding: utf-8 | |
3 # changeset controller for pylons | |
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 # This program is free software; you can redistribute it and/or | |
6 # modify it under the terms of the GNU General Public License | |
7 # as published by the Free Software Foundation; version 2 | |
8 # of the License or (at your opinion) any later version of the license. | |
9 # | |
10 # This program is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with this program; if not, write to the Free Software | |
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
18 # MA 02110-1301, USA. | |
19 """ | |
20 Created on April 25, 2010 | |
21 changeset controller for pylons | |
22 @author: marcink | |
23 """ | |
24 from pylons import tmpl_context as c, url, request, response | |
25 from pylons.i18n.translation import _ | |
26 from pylons.controllers.util import redirect | |
27 from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator | |
28 from rhodecode.lib.base import BaseController, render | |
29 from rhodecode.model.hg_model import HgModel | |
30 from vcs.exceptions import RepositoryError, ChangesetError | |
31 from vcs.nodes import FileNode | |
32 from vcs.utils import diffs as differ | |
33 import logging | |
34 import traceback | |
35 | |
36 log = logging.getLogger(__name__) | |
37 | |
38 class ChangesetController(BaseController): | |
39 | |
40 @LoginRequired() | |
41 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', | |
42 'repository.admin') | |
43 def __before__(self): | |
44 super(ChangesetController, self).__before__() | |
45 | |
46 def index(self, revision): | |
47 hg_model = HgModel() | |
48 cut_off_limit = 1024 * 100 | |
49 | |
50 def wrap_to_table(str): | |
51 | |
52 return '''<table class="code-difftable"> | |
53 <tr class="line"> | |
54 <td class="lineno new"></td> | |
55 <td class="code"><pre>%s</pre></td> | |
56 </tr> | |
57 </table>''' % str | |
58 | |
59 try: | |
60 c.changeset = hg_model.get_repo(c.repo_name).get_changeset(revision) | |
61 except RepositoryError: | |
62 log.error(traceback.format_exc()) | |
63 return redirect(url('hg_home')) | |
64 else: | |
65 try: | |
66 c.changeset_old = c.changeset.parents[0] | |
67 except IndexError: | |
68 c.changeset_old = None | |
69 c.changes = [] | |
70 | |
71 #=================================================================== | |
72 # ADDED FILES | |
73 #=================================================================== | |
74 c.sum_added = 0 | |
75 for node in c.changeset.added: | |
76 | |
77 filenode_old = FileNode(node.path, '') | |
78 if filenode_old.is_binary or node.is_binary: | |
79 diff = wrap_to_table(_('binary file')) | |
80 else: | |
81 c.sum_added += node.size | |
82 if c.sum_added < cut_off_limit: | |
83 f_udiff = differ.get_udiff(filenode_old, node) | |
84 diff = differ.DiffProcessor(f_udiff).as_html() | |
85 else: | |
86 diff = wrap_to_table(_('Changeset is to big and was cut' | |
87 ' off, see raw changeset instead')) | |
88 | |
89 cs1 = None | |
90 cs2 = node.last_changeset.short_id | |
91 c.changes.append(('added', node, diff, cs1, cs2)) | |
92 | |
93 #=================================================================== | |
94 # CHANGED FILES | |
95 #=================================================================== | |
96 c.sum_removed = 0 | |
97 for node in c.changeset.changed: | |
98 try: | |
99 filenode_old = c.changeset_old.get_node(node.path) | |
100 except ChangesetError: | |
101 filenode_old = FileNode(node.path, '') | |
102 | |
103 if filenode_old.is_binary or node.is_binary: | |
104 diff = wrap_to_table(_('binary file')) | |
105 else: | |
106 c.sum_removed += node.size | |
107 if c.sum_removed < cut_off_limit: | |
108 f_udiff = differ.get_udiff(filenode_old, node) | |
109 diff = differ.DiffProcessor(f_udiff).as_html() | |
110 else: | |
111 diff = wrap_to_table(_('Changeset is to big and was cut' | |
112 ' off, see raw changeset instead')) | |
113 | |
114 cs1 = filenode_old.last_changeset.short_id | |
115 cs2 = node.last_changeset.short_id | |
116 c.changes.append(('changed', node, diff, cs1, cs2)) | |
117 | |
118 #=================================================================== | |
119 # REMOVED FILES | |
120 #=================================================================== | |
121 for node in c.changeset.removed: | |
122 c.changes.append(('removed', node, None, None, None)) | |
123 | |
124 return render('changeset/changeset.html') | |
125 | |
126 def raw_changeset(self, revision): | |
127 | |
128 hg_model = HgModel() | |
129 method = request.GET.get('diff', 'show') | |
130 try: | |
131 c.changeset = hg_model.get_repo(c.repo_name).get_changeset(revision) | |
132 except RepositoryError: | |
133 log.error(traceback.format_exc()) | |
134 return redirect(url('hg_home')) | |
135 else: | |
136 try: | |
137 c.changeset_old = c.changeset.parents[0] | |
138 except IndexError: | |
139 c.changeset_old = None | |
140 c.changes = [] | |
141 | |
142 for node in c.changeset.added: | |
143 filenode_old = FileNode(node.path, '') | |
144 if filenode_old.is_binary or node.is_binary: | |
145 diff = _('binary file') | |
146 else: | |
147 f_udiff = differ.get_udiff(filenode_old, node) | |
148 diff = differ.DiffProcessor(f_udiff).raw_diff() | |
149 | |
150 cs1 = None | |
151 cs2 = node.last_changeset.short_id | |
152 c.changes.append(('added', node, diff, cs1, cs2)) | |
153 | |
154 for node in c.changeset.changed: | |
155 filenode_old = c.changeset_old.get_node(node.path) | |
156 if filenode_old.is_binary or node.is_binary: | |
157 diff = _('binary file') | |
158 else: | |
159 f_udiff = differ.get_udiff(filenode_old, node) | |
160 diff = differ.DiffProcessor(f_udiff).raw_diff() | |
161 | |
162 cs1 = filenode_old.last_changeset.short_id | |
163 cs2 = node.last_changeset.short_id | |
164 c.changes.append(('changed', node, diff, cs1, cs2)) | |
165 | |
166 response.content_type = 'text/plain' | |
167 if method == 'download': | |
168 response.content_disposition = 'attachment; filename=%s.patch' % revision | |
169 parent = True if len(c.changeset.parents) > 0 else False | |
170 c.parent_tmpl = 'Parent %s' % c.changeset.parents[0].raw_id if parent else '' | |
171 | |
172 c.diffs = '' | |
173 for x in c.changes: | |
174 c.diffs += x[2] | |
175 | |
176 return render('changeset/raw_changeset.html') |