Show More
@@ -1,104 +1,132 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.controllers.compare |
|
3 | rhodecode.controllers.compare | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | compare controller for pylons showoing differences between two |
|
6 | compare controller for pylons showoing differences between two | |
7 | repos, branches, bookmarks or tips |
|
7 | repos, branches, bookmarks or tips | |
8 |
|
8 | |||
9 | :created_on: May 6, 2012 |
|
9 | :created_on: May 6, 2012 | |
10 | :author: marcink |
|
10 | :author: marcink | |
11 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |
12 | :license: GPLv3, see COPYING for more details. |
|
12 | :license: GPLv3, see COPYING for more details. | |
13 | """ |
|
13 | """ | |
14 | # This program is free software: you can redistribute it and/or modify |
|
14 | # This program is free software: you can redistribute it and/or modify | |
15 | # it under the terms of the GNU General Public License as published by |
|
15 | # it under the terms of the GNU General Public License as published by | |
16 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | # the Free Software Foundation, either version 3 of the License, or | |
17 | # (at your option) any later version. |
|
17 | # (at your option) any later version. | |
18 | # |
|
18 | # | |
19 | # This program is distributed in the hope that it will be useful, |
|
19 | # This program is distributed in the hope that it will be useful, | |
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | # GNU General Public License for more details. |
|
22 | # GNU General Public License for more details. | |
23 | # |
|
23 | # | |
24 | # You should have received a copy of the GNU General Public License |
|
24 | # You should have received a copy of the GNU General Public License | |
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
26 | import logging |
|
26 | import logging | |
27 | import traceback |
|
27 | import traceback | |
28 |
|
28 | |||
29 | from webob.exc import HTTPNotFound |
|
29 | from webob.exc import HTTPNotFound | |
30 | from pylons import request, response, session, tmpl_context as c, url |
|
30 | from pylons import request, response, session, tmpl_context as c, url | |
31 | from pylons.controllers.util import abort, redirect |
|
31 | from pylons.controllers.util import abort, redirect | |
|
32 | from pylons.i18n.translation import _ | |||
32 |
|
33 | |||
|
34 | from rhodecode.lib.vcs.exceptions import EmptyRepositoryError, RepositoryError | |||
33 | from rhodecode.lib import helpers as h |
|
35 | from rhodecode.lib import helpers as h | |
34 | from rhodecode.lib.base import BaseRepoController, render |
|
36 | from rhodecode.lib.base import BaseRepoController, render | |
35 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator |
|
37 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator | |
36 | from rhodecode.lib import diffs |
|
38 | from rhodecode.lib import diffs | |
37 |
|
39 | |||
38 | from rhodecode.model.db import Repository |
|
40 | from rhodecode.model.db import Repository | |
39 | from rhodecode.model.pull_request import PullRequestModel |
|
41 | from rhodecode.model.pull_request import PullRequestModel | |
40 |
|
42 | |||
41 | log = logging.getLogger(__name__) |
|
43 | log = logging.getLogger(__name__) | |
42 |
|
44 | |||
43 |
|
45 | |||
44 | class CompareController(BaseRepoController): |
|
46 | class CompareController(BaseRepoController): | |
45 |
|
47 | |||
46 | @LoginRequired() |
|
48 | @LoginRequired() | |
47 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
49 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', | |
48 | 'repository.admin') |
|
50 | 'repository.admin') | |
49 | def __before__(self): |
|
51 | def __before__(self): | |
50 | super(CompareController, self).__before__() |
|
52 | super(CompareController, self).__before__() | |
51 |
|
53 | |||
|
54 | def __get_cs_or_redirect(self, rev, repo, redirect_after=True): | |||
|
55 | """ | |||
|
56 | Safe way to get changeset if error occur it redirects to changeset with | |||
|
57 | proper message | |||
|
58 | ||||
|
59 | :param rev: revision to fetch | |||
|
60 | :param repo: repo instance | |||
|
61 | """ | |||
|
62 | ||||
|
63 | try: | |||
|
64 | type_, rev = rev | |||
|
65 | return repo.scm_instance.get_changeset(rev) | |||
|
66 | except EmptyRepositoryError, e: | |||
|
67 | if not redirect_after: | |||
|
68 | return None | |||
|
69 | h.flash(h.literal(_('There are no changesets yet')), | |||
|
70 | category='warning') | |||
|
71 | redirect(url('summary_home', repo_name=repo.repo_name)) | |||
|
72 | ||||
|
73 | except RepositoryError, e: | |||
|
74 | h.flash(str(e), category='warning') | |||
|
75 | redirect(h.url('summary_home', repo_name=repo.repo_name)) | |||
|
76 | ||||
52 | def index(self, org_ref_type, org_ref, other_ref_type, other_ref): |
|
77 | def index(self, org_ref_type, org_ref, other_ref_type, other_ref): | |
53 |
|
78 | |||
54 | org_repo = c.rhodecode_db_repo.repo_name |
|
79 | org_repo = c.rhodecode_db_repo.repo_name | |
55 | org_ref = (org_ref_type, org_ref) |
|
80 | org_ref = (org_ref_type, org_ref) | |
56 | other_ref = (other_ref_type, other_ref) |
|
81 | other_ref = (other_ref_type, other_ref) | |
57 | other_repo = request.GET.get('repo', org_repo) |
|
82 | other_repo = request.GET.get('repo', org_repo) | |
58 |
|
83 | |||
59 | c.swap_url = h.url('compare_url', repo_name=other_repo, |
|
84 | c.swap_url = h.url('compare_url', repo_name=other_repo, | |
60 | org_ref_type=other_ref[0], org_ref=other_ref[1], |
|
85 | org_ref_type=other_ref[0], org_ref=other_ref[1], | |
61 | other_ref_type=org_ref[0], other_ref=org_ref[1], |
|
86 | other_ref_type=org_ref[0], other_ref=org_ref[1], | |
62 | repo=org_repo) |
|
87 | repo=org_repo) | |
63 |
|
88 | |||
64 | c.org_repo = org_repo = Repository.get_by_repo_name(org_repo) |
|
89 | c.org_repo = org_repo = Repository.get_by_repo_name(org_repo) | |
65 | c.other_repo = other_repo = Repository.get_by_repo_name(other_repo) |
|
90 | c.other_repo = other_repo = Repository.get_by_repo_name(other_repo) | |
66 |
|
91 | |||
67 | if c.org_repo is None or c.other_repo is None: |
|
92 | if c.org_repo is None or c.other_repo is None: | |
68 | log.error('Could not found repo %s or %s' % (org_repo, other_repo)) |
|
93 | log.error('Could not found repo %s or %s' % (org_repo, other_repo)) | |
69 | raise HTTPNotFound |
|
94 | raise HTTPNotFound | |
70 |
|
95 | |||
71 | if c.org_repo.scm_instance.alias != 'hg': |
|
96 | if c.org_repo.scm_instance.alias != 'hg': | |
72 | log.error('Review not available for GIT REPOS') |
|
97 | log.error('Review not available for GIT REPOS') | |
73 | raise HTTPNotFound |
|
98 | raise HTTPNotFound | |
74 |
|
99 | |||
|
100 | self.__get_cs_or_redirect(rev=org_ref, repo=org_repo) | |||
|
101 | self.__get_cs_or_redirect(rev=other_ref, repo=other_repo) | |||
|
102 | ||||
75 | c.cs_ranges, discovery_data = PullRequestModel().get_compare_data( |
|
103 | c.cs_ranges, discovery_data = PullRequestModel().get_compare_data( | |
76 | org_repo, org_ref, other_repo, other_ref |
|
104 | org_repo, org_ref, other_repo, other_ref | |
77 | ) |
|
105 | ) | |
78 |
|
106 | |||
79 | c.statuses = c.rhodecode_db_repo.statuses([x.raw_id for x in |
|
107 | c.statuses = c.rhodecode_db_repo.statuses([x.raw_id for x in | |
80 | c.cs_ranges]) |
|
108 | c.cs_ranges]) | |
81 | c.target_repo = c.repo_name |
|
109 | c.target_repo = c.repo_name | |
82 | # defines that we need hidden inputs with changesets |
|
110 | # defines that we need hidden inputs with changesets | |
83 | c.as_form = request.GET.get('as_form', False) |
|
111 | c.as_form = request.GET.get('as_form', False) | |
84 | if request.environ.get('HTTP_X_PARTIAL_XHR'): |
|
112 | if request.environ.get('HTTP_X_PARTIAL_XHR'): | |
85 | return render('compare/compare_cs.html') |
|
113 | return render('compare/compare_cs.html') | |
86 |
|
114 | |||
87 | c.org_ref = org_ref[1] |
|
115 | c.org_ref = org_ref[1] | |
88 | c.other_ref = other_ref[1] |
|
116 | c.other_ref = other_ref[1] | |
89 | # diff needs to have swapped org with other to generate proper diff |
|
117 | # diff needs to have swapped org with other to generate proper diff | |
90 | _diff = diffs.differ(other_repo, other_ref, org_repo, org_ref, |
|
118 | _diff = diffs.differ(other_repo, other_ref, org_repo, org_ref, | |
91 | discovery_data) |
|
119 | discovery_data) | |
92 | diff_processor = diffs.DiffProcessor(_diff, format='gitdiff') |
|
120 | diff_processor = diffs.DiffProcessor(_diff, format='gitdiff') | |
93 | _parsed = diff_processor.prepare() |
|
121 | _parsed = diff_processor.prepare() | |
94 |
|
122 | |||
95 | c.files = [] |
|
123 | c.files = [] | |
96 | c.changes = {} |
|
124 | c.changes = {} | |
97 |
|
125 | |||
98 | for f in _parsed: |
|
126 | for f in _parsed: | |
99 | fid = h.FID('', f['filename']) |
|
127 | fid = h.FID('', f['filename']) | |
100 | c.files.append([fid, f['operation'], f['filename'], f['stats']]) |
|
128 | c.files.append([fid, f['operation'], f['filename'], f['stats']]) | |
101 | diff = diff_processor.as_html(enable_comments=False, diff_lines=[f]) |
|
129 | diff = diff_processor.as_html(enable_comments=False, diff_lines=[f]) | |
102 | c.changes[fid] = [f['operation'], f['filename'], diff] |
|
130 | c.changes[fid] = [f['operation'], f['filename'], diff] | |
103 |
|
131 | |||
104 | return render('compare/compare_diff.html') |
|
132 | return render('compare/compare_diff.html') |
General Comments 0
You need to be logged in to leave comments.
Login now