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