diff --git a/nosetests b/nosetests index dbf9cf3..2f5271b 100755 --- a/nosetests +++ b/nosetests @@ -1,4 +1,4 @@ -#!/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 diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html index af3837b..16ee4c4 100644 --- a/pagure/templates/pull_request.html +++ b/pagure/templates/pull_request.html @@ -107,6 +107,11 @@ (tree) + + {% if origin == 'compare_commits' %} +
{{ commit1 }} .. {{ commit2 }}
+ {% endif %} + {% endif %} {% if form and (repo_admin or remote_git) %} diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index c668258..152b170 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -379,6 +379,57 @@ def view_commits(repo, branchname=None, username=None): ) +@APP.route('//c/../') +@APP.route('//c/..') +@APP.route('/fork///c/../') +@APP.route('/fork///c/..') +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('//blob//f/') @APP.route('/fork///blob//f/') def view_file(repo, identifier, filename, username=None): diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 3e49701..c2ffc34 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -1125,6 +1125,90 @@ class PagureFlaskRepotests(tests.Modeltests): 'test project #3 ', 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( + '
%s .. %s
' % (c1.oid.hex, c2.oid.hex), + output.data) + self.assertIn( + '@@ -0,0 +1,3 @@', + output.data) + self.assertIn('+ foo', output.data) + self.assertIn('+ bar', output.data) + self.assertIn('+ baz ', output.data) + + # View inverse commits comparison + output = self.app.get( + '/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex)) + self.assertIn( + '
%s .. %s
' % (c2.oid.hex, c1.oid.hex), + output.data) + self.assertIn( + '@@ -1,3 +0,0 @@', + output.data) + self.assertIn('- foo', output.data) + self.assertIn('- bar', output.data) + self.assertIn('- baz ', 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( + '
%s .. %s
' % (c1.oid.hex, c2.oid.hex), + output.data) + self.assertIn( + '@@ -0,0 +1,3 @@', + output.data) + self.assertIn('+ foo', output.data) + self.assertIn('+ bar', output.data) + self.assertIn('+ baz ', output.data) + + # View inverse commits comparison + output = self.app.get( + '/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex)) + self.assertIn( + '
%s .. %s
' % (c2.oid.hex, c1.oid.hex), + output.data) + self.assertIn( + '@@ -1,3 +0,0 @@', + output.data) + self.assertIn('- foo', output.data) + self.assertIn('- bar', output.data) + self.assertIn('- baz ', output.data) + def test_view_file(self): """ Test the view_file endpoint. """ output = self.app.get('/foo/blob/foo/f/sources')