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