Show More
@@ -1,336 +1,342 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | import logging |
|
23 | 23 | |
|
24 | 24 | from pyramid.httpexceptions import HTTPNotFound, HTTPFound |
|
25 | 25 | from pyramid.view import view_config |
|
26 | 26 | from pyramid.renderers import render |
|
27 | 27 | from pyramid.response import Response |
|
28 | 28 | |
|
29 | 29 | from rhodecode.apps._base import RepoAppView |
|
30 | 30 | import rhodecode.lib.helpers as h |
|
31 | 31 | from rhodecode.lib.auth import ( |
|
32 | 32 | LoginRequired, HasRepoPermissionAnyDecorator) |
|
33 | 33 | |
|
34 | 34 | from rhodecode.lib.ext_json import json |
|
35 | 35 | from rhodecode.lib.graphmod import _colored, _dagwalker |
|
36 | 36 | from rhodecode.lib.helpers import RepoPage |
|
37 | 37 | from rhodecode.lib.utils2 import safe_int, safe_str |
|
38 | 38 | from rhodecode.lib.vcs.exceptions import ( |
|
39 | 39 | RepositoryError, CommitDoesNotExistError, |
|
40 | 40 | CommitError, NodeDoesNotExistError, EmptyRepositoryError) |
|
41 | 41 | |
|
42 | 42 | log = logging.getLogger(__name__) |
|
43 | 43 | |
|
44 | 44 | DEFAULT_CHANGELOG_SIZE = 20 |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | class RepoChangelogView(RepoAppView): |
|
48 | 48 | |
|
49 | 49 | def _get_commit_or_redirect(self, commit_id, redirect_after=True): |
|
50 | 50 | """ |
|
51 | 51 | This is a safe way to get commit. If an error occurs it redirects to |
|
52 | 52 | tip with proper message |
|
53 | 53 | |
|
54 | 54 | :param commit_id: id of commit to fetch |
|
55 | 55 | :param redirect_after: toggle redirection |
|
56 | 56 | """ |
|
57 | 57 | _ = self.request.translate |
|
58 | 58 | |
|
59 | 59 | try: |
|
60 | 60 | return self.rhodecode_vcs_repo.get_commit(commit_id) |
|
61 | 61 | except EmptyRepositoryError: |
|
62 | 62 | if not redirect_after: |
|
63 | 63 | return None |
|
64 | 64 | |
|
65 | 65 | h.flash(h.literal( |
|
66 | 66 | _('There are no commits yet')), category='warning') |
|
67 | 67 | raise HTTPFound( |
|
68 | 68 | h.route_path('repo_summary', repo_name=self.db_repo_name)) |
|
69 | 69 | |
|
70 | 70 | except (CommitDoesNotExistError, LookupError): |
|
71 | 71 | msg = _('No such commit exists for this repository') |
|
72 | 72 | h.flash(msg, category='error') |
|
73 | 73 | raise HTTPNotFound() |
|
74 | 74 | except RepositoryError as e: |
|
75 | 75 | h.flash(safe_str(h.escape(e)), category='error') |
|
76 | 76 | raise HTTPNotFound() |
|
77 | 77 | |
|
78 | 78 | def _graph(self, repo, commits, prev_data=None, next_data=None): |
|
79 | 79 | """ |
|
80 | 80 | Generates a DAG graph for repo |
|
81 | 81 | |
|
82 | 82 | :param repo: repo instance |
|
83 | 83 | :param commits: list of commits |
|
84 | 84 | """ |
|
85 | 85 | if not commits: |
|
86 | 86 | return json.dumps([]) |
|
87 | 87 | |
|
88 | 88 | def serialize(commit, parents=True): |
|
89 | 89 | data = dict( |
|
90 | 90 | raw_id=commit.raw_id, |
|
91 | 91 | idx=commit.idx, |
|
92 | 92 | branch=commit.branch, |
|
93 | 93 | ) |
|
94 | 94 | if parents: |
|
95 | 95 | data['parents'] = [ |
|
96 | 96 | serialize(x, parents=False) for x in commit.parents] |
|
97 | 97 | return data |
|
98 | 98 | |
|
99 | 99 | prev_data = prev_data or [] |
|
100 | 100 | next_data = next_data or [] |
|
101 | 101 | |
|
102 | 102 | current = [serialize(x) for x in commits] |
|
103 | 103 | commits = prev_data + current + next_data |
|
104 | 104 | |
|
105 | 105 | dag = _dagwalker(repo, commits) |
|
106 | 106 | |
|
107 | 107 | data = [[commit_id, vtx, edges, branch] |
|
108 | 108 | for commit_id, vtx, edges, branch in _colored(dag)] |
|
109 | 109 | return json.dumps(data), json.dumps(current) |
|
110 | 110 | |
|
111 | 111 | def _check_if_valid_branch(self, branch_name, repo_name, f_path): |
|
112 | 112 | if branch_name not in self.rhodecode_vcs_repo.branches_all: |
|
113 | 113 | h.flash('Branch {} is not found.'.format(h.escape(branch_name)), |
|
114 | 114 | category='warning') |
|
115 | 115 | redirect_url = h.route_path( |
|
116 | 116 | 'repo_changelog_file', repo_name=repo_name, |
|
117 | 117 | commit_id=branch_name, f_path=f_path or '') |
|
118 | 118 | raise HTTPFound(redirect_url) |
|
119 | 119 | |
|
120 | 120 | def _load_changelog_data( |
|
121 | 121 | self, c, collection, page, chunk_size, branch_name=None, |
|
122 | 122 | dynamic=False, f_path=None, commit_id=None): |
|
123 | 123 | |
|
124 | 124 | def url_generator(**kw): |
|
125 | 125 | query_params = {} |
|
126 | 126 | query_params.update(kw) |
|
127 | 127 | if f_path: |
|
128 | 128 | # changelog for file |
|
129 | 129 | return h.route_path( |
|
130 | 130 | 'repo_changelog_file', |
|
131 | 131 | repo_name=c.rhodecode_db_repo.repo_name, |
|
132 | 132 | commit_id=commit_id, f_path=f_path, |
|
133 | 133 | _query=query_params) |
|
134 | 134 | else: |
|
135 | 135 | return h.route_path( |
|
136 | 136 | 'repo_changelog', |
|
137 | 137 | repo_name=c.rhodecode_db_repo.repo_name, _query=query_params) |
|
138 | 138 | |
|
139 | 139 | c.total_cs = len(collection) |
|
140 | 140 | c.showing_commits = min(chunk_size, c.total_cs) |
|
141 | 141 | c.pagination = RepoPage(collection, page=page, item_count=c.total_cs, |
|
142 | 142 | items_per_page=chunk_size, branch=branch_name, |
|
143 | 143 | url=url_generator) |
|
144 | 144 | |
|
145 | 145 | c.next_page = c.pagination.next_page |
|
146 | 146 | c.prev_page = c.pagination.previous_page |
|
147 | 147 | |
|
148 | 148 | if dynamic: |
|
149 | 149 | if self.request.GET.get('chunk') != 'next': |
|
150 | 150 | c.next_page = None |
|
151 | 151 | if self.request.GET.get('chunk') != 'prev': |
|
152 | 152 | c.prev_page = None |
|
153 | 153 | |
|
154 | 154 | page_commit_ids = [x.raw_id for x in c.pagination] |
|
155 | 155 | c.comments = c.rhodecode_db_repo.get_comments(page_commit_ids) |
|
156 | 156 | c.statuses = c.rhodecode_db_repo.statuses(page_commit_ids) |
|
157 | 157 | |
|
158 | 158 | def load_default_context(self): |
|
159 | 159 | c = self._get_local_tmpl_context(include_app_defaults=True) |
|
160 | 160 | |
|
161 | 161 | c.rhodecode_repo = self.rhodecode_vcs_repo |
|
162 | 162 | self._register_global_c(c) |
|
163 | 163 | return c |
|
164 | 164 | |
|
165 | 165 | def _get_preload_attrs(self): |
|
166 | 166 | pre_load = ['author', 'branch', 'date', 'message', 'parents', |
|
167 | 167 | 'obsolete', 'phase', 'hidden'] |
|
168 | 168 | return pre_load |
|
169 | 169 | |
|
170 | 170 | @LoginRequired() |
|
171 | 171 | @HasRepoPermissionAnyDecorator( |
|
172 | 172 | 'repository.read', 'repository.write', 'repository.admin') |
|
173 | 173 | @view_config( |
|
174 | 174 | route_name='repo_changelog', request_method='GET', |
|
175 | 175 | renderer='rhodecode:templates/changelog/changelog.mako') |
|
176 | 176 | @view_config( |
|
177 | 177 | route_name='repo_changelog_file', request_method='GET', |
|
178 | 178 | renderer='rhodecode:templates/changelog/changelog.mako') |
|
179 | 179 | def repo_changelog(self): |
|
180 | 180 | c = self.load_default_context() |
|
181 | 181 | |
|
182 | 182 | commit_id = self.request.matchdict.get('commit_id') |
|
183 | 183 | f_path = self._get_f_path(self.request.matchdict) |
|
184 | 184 | |
|
185 | 185 | chunk_size = 20 |
|
186 | 186 | |
|
187 | 187 | c.branch_name = branch_name = self.request.GET.get('branch') or '' |
|
188 | 188 | c.book_name = book_name = self.request.GET.get('bookmark') or '' |
|
189 | 189 | c.f_path = f_path |
|
190 | 190 | c.commit_id = commit_id |
|
191 | 191 | hist_limit = safe_int(self.request.GET.get('limit')) or None |
|
192 | 192 | |
|
193 | 193 | p = safe_int(self.request.GET.get('page', 1), 1) |
|
194 | 194 | |
|
195 | 195 | c.selected_name = branch_name or book_name |
|
196 | 196 | if not commit_id and branch_name: |
|
197 | 197 | self._check_if_valid_branch(branch_name, self.db_repo_name, f_path) |
|
198 | 198 | |
|
199 | 199 | c.changelog_for_path = f_path |
|
200 | 200 | pre_load = self._get_preload_attrs() |
|
201 | commit_ids = [] | |
|
202 | 201 | |
|
203 | 202 | partial_xhr = self.request.environ.get('HTTP_X_PARTIAL_XHR') |
|
204 | 203 | |
|
205 | 204 | try: |
|
206 | 205 | if f_path: |
|
207 | 206 | log.debug('generating changelog for path %s', f_path) |
|
208 | 207 | # get the history for the file ! |
|
209 | 208 | base_commit = self.rhodecode_vcs_repo.get_commit(commit_id) |
|
210 | 209 | |
|
211 | 210 | try: |
|
212 | 211 | collection = base_commit.get_file_history( |
|
213 | 212 | f_path, limit=hist_limit, pre_load=pre_load) |
|
214 | 213 | if collection and partial_xhr: |
|
215 | 214 | # for ajax call we remove first one since we're looking |
|
216 | 215 | # at it right now in the context of a file commit |
|
217 | 216 | collection.pop(0) |
|
218 | 217 | except (NodeDoesNotExistError, CommitError): |
|
219 | 218 | # this node is not present at tip! |
|
220 | 219 | try: |
|
221 | 220 | commit = self._get_commit_or_redirect(commit_id) |
|
222 | 221 | collection = commit.get_file_history(f_path) |
|
223 | 222 | except RepositoryError as e: |
|
224 | 223 | h.flash(safe_str(e), category='warning') |
|
225 | 224 | redirect_url = h.route_path( |
|
226 | 225 | 'repo_changelog', repo_name=self.db_repo_name) |
|
227 | 226 | raise HTTPFound(redirect_url) |
|
228 | 227 | collection = list(reversed(collection)) |
|
229 | 228 | else: |
|
230 | 229 | collection = self.rhodecode_vcs_repo.get_commits( |
|
231 | 230 | branch_name=branch_name, pre_load=pre_load) |
|
232 | 231 | |
|
233 | 232 | self._load_changelog_data( |
|
234 | 233 | c, collection, p, chunk_size, c.branch_name, |
|
235 | 234 | f_path=f_path, commit_id=commit_id) |
|
236 | 235 | |
|
237 | 236 | except EmptyRepositoryError as e: |
|
238 | 237 | h.flash(safe_str(h.escape(e)), category='warning') |
|
239 | 238 | raise HTTPFound( |
|
240 | 239 | h.route_path('repo_summary', repo_name=self.db_repo_name)) |
|
241 | 240 | except HTTPFound: |
|
242 | 241 | raise |
|
243 | 242 | except (RepositoryError, CommitDoesNotExistError, Exception) as e: |
|
244 | 243 | log.exception(safe_str(e)) |
|
245 | 244 | h.flash(safe_str(h.escape(e)), category='error') |
|
246 | 245 | raise HTTPFound( |
|
247 | 246 | h.route_path('repo_changelog', repo_name=self.db_repo_name)) |
|
248 | 247 | |
|
249 | 248 | if partial_xhr or self.request.environ.get('HTTP_X_PJAX'): |
|
250 | 249 | # case when loading dynamic file history in file view |
|
251 | 250 | # loading from ajax, we don't want the first result, it's popped |
|
252 | 251 | # in the code above |
|
253 | 252 | html = render( |
|
254 | 253 | 'rhodecode:templates/changelog/changelog_file_history.mako', |
|
255 | 254 | self._get_template_context(c), self.request) |
|
256 | 255 | return Response(html) |
|
257 | 256 | |
|
257 | commit_ids = [] | |
|
258 | 258 | if not f_path: |
|
259 | # only load graph data when not in file history mode | |
|
259 | 260 | commit_ids = c.pagination |
|
260 | 261 | |
|
261 | 262 | c.graph_data, c.graph_commits = self._graph( |
|
262 | 263 | self.rhodecode_vcs_repo, commit_ids) |
|
263 | 264 | |
|
264 | 265 | return self._get_template_context(c) |
|
265 | 266 | |
|
266 | 267 | @LoginRequired() |
|
267 | 268 | @HasRepoPermissionAnyDecorator( |
|
268 | 269 | 'repository.read', 'repository.write', 'repository.admin') |
|
269 | 270 | @view_config( |
|
270 | 271 | route_name='repo_changelog_elements', request_method=('GET', 'POST'), |
|
271 | 272 | renderer='rhodecode:templates/changelog/changelog_elements.mako', |
|
272 | 273 | xhr=True) |
|
273 | 274 | @view_config( |
|
274 | 275 | route_name='repo_changelog_elements_file', request_method=('GET', 'POST'), |
|
275 | 276 | renderer='rhodecode:templates/changelog/changelog_elements.mako', |
|
276 | 277 | xhr=True) |
|
277 | 278 | def repo_changelog_elements(self): |
|
278 | 279 | c = self.load_default_context() |
|
279 | 280 | commit_id = self.request.matchdict.get('commit_id') |
|
280 | 281 | f_path = self._get_f_path(self.request.matchdict) |
|
281 | 282 | chunk_size = 20 |
|
282 | 283 | hist_limit = safe_int(self.request.GET.get('limit')) or None |
|
283 | 284 | |
|
284 | 285 | def wrap_for_error(err): |
|
285 | 286 | html = '<tr>' \ |
|
286 | 287 | '<td colspan="9" class="alert alert-error">ERROR: {}</td>' \ |
|
287 | 288 | '</tr>'.format(err) |
|
288 | 289 | return Response(html) |
|
289 | 290 | |
|
290 | 291 | c.branch_name = branch_name = self.request.GET.get('branch') or '' |
|
291 | 292 | c.book_name = book_name = self.request.GET.get('bookmark') or '' |
|
292 | 293 | c.f_path = f_path |
|
293 | 294 | c.commit_id = commit_id |
|
294 | 295 | |
|
295 | 296 | c.selected_name = branch_name or book_name |
|
296 | 297 | if branch_name and branch_name not in self.rhodecode_vcs_repo.branches_all: |
|
297 | 298 | return wrap_for_error( |
|
298 | 299 | safe_str('Branch: {} is not valid'.format(branch_name))) |
|
299 | 300 | |
|
300 | 301 | pre_load = self._get_preload_attrs() |
|
301 | 302 | |
|
302 | 303 | if f_path: |
|
303 | 304 | base_commit = self.rhodecode_vcs_repo.get_commit(commit_id) |
|
304 | 305 | collection = base_commit.get_file_history( |
|
305 | 306 | f_path, limit=hist_limit, pre_load=pre_load) |
|
306 | 307 | collection = list(reversed(collection)) |
|
307 | 308 | else: |
|
308 | 309 | collection = self.rhodecode_vcs_repo.get_commits( |
|
309 | 310 | branch_name=branch_name, pre_load=pre_load) |
|
310 | 311 | |
|
311 | 312 | p = safe_int(self.request.GET.get('page', 1), 1) |
|
312 | 313 | try: |
|
313 | 314 | self._load_changelog_data( |
|
314 | 315 | c, collection, p, chunk_size, dynamic=True, |
|
315 | 316 | f_path=f_path, commit_id=commit_id) |
|
316 | 317 | except EmptyRepositoryError as e: |
|
317 | 318 | return wrap_for_error(safe_str(e)) |
|
318 | 319 | except (RepositoryError, CommitDoesNotExistError, Exception) as e: |
|
319 | 320 | log.exception('Failed to fetch commits') |
|
320 | 321 | return wrap_for_error(safe_str(e)) |
|
321 | 322 | |
|
322 | 323 | prev_data = None |
|
323 | 324 | next_data = None |
|
324 | 325 | |
|
325 | 326 | prev_graph = json.loads(self.request.POST.get('graph') or '{}') |
|
326 | 327 | |
|
327 | 328 | if self.request.GET.get('chunk') == 'prev': |
|
328 | 329 | next_data = prev_graph |
|
329 | 330 | elif self.request.GET.get('chunk') == 'next': |
|
330 | 331 | prev_data = prev_graph |
|
331 | 332 | |
|
333 | commit_ids = [] | |
|
334 | if not f_path: | |
|
335 | # only load graph data when not in file history mode | |
|
336 | commit_ids = c.pagination | |
|
337 | ||
|
332 | 338 | c.graph_data, c.graph_commits = self._graph( |
|
333 |
self.rhodecode_vcs_repo, c |
|
|
339 | self.rhodecode_vcs_repo, commit_ids, | |
|
334 | 340 | prev_data=prev_data, next_data=next_data) |
|
335 | 341 | |
|
336 | 342 | return self._get_template_context(c) |
General Comments 0
You need to be logged in to leave comments.
Login now