Commit 38f51010 authored by Oliver Gutierrez's avatar Oliver Gutierrez

Added commit comparison view

parent 0adca76b
#!/usr/bin/python
#!/usr/bin/env python
# EASY-INSTALL-ENTRY-SCRIPT: 'nose==0.10.4','console_scripts','nosetests'
__requires__ = ['nose>=0.10.4', 'SQLAlchemy >= 0.7', 'jinja2 >= 2.4']
import sys
......
......@@ -107,6 +107,11 @@
(<a href="{{ url_for('view_tree', username=username,
repo=repo.name, identifier=commitid) }}"
>tree</a>)</h2>
{% if origin == 'compare_commits' %}
<h5 class="text-muted">{{ commit1 }} .. {{ commit2 }}</h5>
{% endif %}
{% endif %}
{% if form and (repo_admin or remote_git) %}
......
......@@ -379,6 +379,57 @@ def view_commits(repo, branchname=None, username=None):
)
@APP.route('/<repo>/c/<commit1>..<commit2>/')
@APP.route('/<repo>/c/<commit1>..<commit2>')
@APP.route('/fork/<username>/<repo>/c/<commit1>..<commit2>/')
@APP.route('/fork/<username>/<repo>/c/<commit1>..<commit2>')
def compare_commits(repo, commit1, commit2, username=None):
""" Compares two commits for specified repo
"""
repo = pagure.lib.get_project(SESSION, repo, user=username)
if not repo:
flask.abort(404, 'Project not found')
reponame = pagure.get_repo_path(repo)
repo_obj = pygit2.Repository(reponame)
if not repo_obj.is_empty and not repo_obj.head_is_unborn:
head = repo_obj.head.shorthand
else:
head = None
# Check commit1 and commit2 existence
commit1_obj = repo_obj.get(commit1)
commit2_obj = repo_obj.get(commit2)
if commit1_obj is None:
flask.abort(404, 'First commit does not exist')
if commit2_obj is None:
flask.abort(404, 'Second commit does not exist')
# Get commit1 and commit2 diff data
diff_commits = [commit1_obj, commit2_obj]
diff = repo_obj.diff(commit1, commit2)
return flask.render_template(
'pull_request.html',
select='logs',
origin='compare_commits',
repo=repo,
username=username,
head=head,
commit1=commit1,
commit2=commit2,
commit1_obj=commit1_obj,
commit2_obj=commit2_obj,
diff=diff,
diff_commits=diff_commits,
branches=sorted(repo_obj.listall_branches()),
repo_admin=is_repo_admin(repo),
)
@APP.route('/<repo:repo>/blob/<path:identifier>/f/<path:filename>')
@APP.route('/fork/<username>/<repo:repo>/blob/<path:identifier>/f/<path:filename>')
def view_file(repo, identifier, filename, username=None):
......
......@@ -1125,6 +1125,90 @@ class PagureFlaskRepotests(tests.Modeltests):
'test project #3 </div>', output.data)
self.assertIn('Forked from', output.data)
def test_compare_commits(self):
""" Test the compare_commits endpoint. """
output = self.app.get('/foo/bar')
# No project registered in the DB
self.assertEqual(output.status_code, 404)
tests.create_projects(self.session)
output = self.app.get('/test/bar')
# No git repo associated
self.assertEqual(output.status_code, 404)
tests.create_projects_git(tests.HERE, bare=True)
output = self.app.get('/test/bar')
self.assertEqual(output.status_code, 404)
# Add a README to the git repo - First commit
tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
c1 = repo.revparse_single('HEAD')
# Add some content to the git repo
tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
c2 = repo.revparse_single('HEAD')
# View commits comparison
output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c2.oid.hex))
self.assertEqual(output.status_code, 200)
self.assertIn(
'<h5 class="text-muted">%s .. %s</h5>' % (c1.oid.hex, c2.oid.hex),
output.data)
self.assertIn(
'<span style="color: #800080; font-weight: bold">@@ -0,0 +1,3 @@</span>',
output.data)
self.assertIn('<span style="color: #00A000">+ foo</span>', output.data)
self.assertIn('<span style="color: #00A000">+ bar</span>', output.data)
self.assertIn('<span style="color: #00A000">+ baz </span>', output.data)
# View inverse commits comparison
output = self.app.get(
'/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex))
self.assertIn(
'<h5 class="text-muted">%s .. %s</h5>' % (c2.oid.hex, c1.oid.hex),
output.data)
self.assertIn(
'<span style="color: #800080; font-weight: bold">@@ -1,3 +0,0 @@</span>',
output.data)
self.assertIn('<span style="color: #a40000">- foo</span>', output.data)
self.assertIn('<span style="color: #a40000">- bar</span>', output.data)
self.assertIn('<span style="color: #a40000">- baz </span>', output.data)
user = tests.FakeUser()
# Set user logged in
with tests.user_set(pagure.APP, user):
# View commits comparison
output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c2.oid.hex))
self.assertEqual(output.status_code, 200)
self.assertIn(
'<h5 class="text-muted">%s .. %s</h5>' % (c1.oid.hex, c2.oid.hex),
output.data)
self.assertIn(
'<span style="color: #800080; font-weight: bold">@@ -0,0 +1,3 @@</span>',
output.data)
self.assertIn('<span style="color: #00A000">+ foo</span>', output.data)
self.assertIn('<span style="color: #00A000">+ bar</span>', output.data)
self.assertIn('<span style="color: #00A000">+ baz </span>', output.data)
# View inverse commits comparison
output = self.app.get(
'/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex))
self.assertIn(
'<h5 class="text-muted">%s .. %s</h5>' % (c2.oid.hex, c1.oid.hex),
output.data)
self.assertIn(
'<span style="color: #800080; font-weight: bold">@@ -1,3 +0,0 @@</span>',
output.data)
self.assertIn('<span style="color: #a40000">- foo</span>', output.data)
self.assertIn('<span style="color: #a40000">- bar</span>', output.data)
self.assertIn('<span style="color: #a40000">- baz </span>', output.data)
def test_view_file(self):
""" Test the view_file endpoint. """
output = self.app.get('/foo/blob/foo/f/sources')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment