diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html index 16ee4c4..e8ca6a0 100644 --- a/pagure/templates/pull_request.html +++ b/pagure/templates/pull_request.html @@ -6,6 +6,8 @@ PR#{{ requestid }}: {{ pull_request.title | noJS(ignore="img")}} - {{ repo.name }} {% elif form and (repo_admin or remote_git) %} Create new Pull Request for {{ branch_to }} - {{ repo.name }} + {%- elif origin == 'compare_commits' -%} + Diff from {{ commit1 }} to {{ commit2 }} - {{ repo.name }} {%- else -%} Diff from {{ branch_from }} to {{ branch_to }} - {{ repo.name }} {%- endif -%} diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 152b170..eadb3b4 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -406,12 +406,34 @@ def compare_commits(repo, commit1, commit2, username=None): 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') + flask.abort(404, 'Last commit does not exist') - # Get commit1 and commit2 diff data - diff_commits = [commit1_obj, commit2_obj] + # Get commits diff data diff = repo_obj.diff(commit1, commit2) + # Get commits list + diff_commits = [] + order = pygit2.GIT_SORT_TIME + first_commit = commit1 + last_commit = commit2 + + commits = map(lambda x: x.oid.hex[:len(first_commit)], + repo_obj.walk(last_commit, pygit2.GIT_SORT_TIME)) + + if first_commit not in commits: + first_commit = commit2 + last_commit = commit1 + + for commit in repo_obj.walk(last_commit, order): + diff_commits.append(commit) + + if commit.oid.hex == first_commit \ + or commit.oid.hex.startswith(first_commit): + break + + if first_commit == commit2: + diff_commits.reverse() + return flask.render_template( 'pull_request.html', select='logs', @@ -421,8 +443,6 @@ def compare_commits(repo, commit1, commit2, username=None): 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()), diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index c2ffc34..d674d59 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -1127,6 +1127,111 @@ class PagureFlaskRepotests(tests.Modeltests): def test_compare_commits(self): """ Test the compare_commits endpoint. """ + + # First two commits comparison + def compare_first_two(c1, c2): + # View commits comparison + output = self.app.get('/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex)) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Diff from %s to %s - test - Pagure' + % (c2.oid.hex, c1.oid.hex), + output.data) + self.assertIn( + '
%s .. %s
' + % (c2.oid.hex, c1.oid.hex), + output.data) + self.assertIn( + 'Commits \n ' + + '' + + '\n 2\n ', + output.data) + self.assertIn( + '' + + '@@ -1,2 +1,1 @@', + output.data) + self.assertIn( + '- Row 0', output.data) + # View inverse commits comparison + output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c2.oid.hex)) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Diff from %s to %s - test - Pagure' % + (c1.oid.hex, c2.oid.hex), + output.data) + self.assertIn( + 'Commits \n ' + + '' + + '\n 2\n ', + output.data) + self.assertIn( + '
%s .. %s
' % + (c1.oid.hex, c2.oid.hex), + output.data) + self.assertIn( + '' + + '@@ -1,1 +1,2 @@', + output.data) + self.assertIn( + '+ Row 0', + output.data) + + def compare_all(c1, c3): + # View commits comparison + output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c3.oid.hex)) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Diff from %s to %s - test - Pagure' % + (c1.oid.hex, c3.oid.hex), output.data) + self.assertIn( + '
%s .. %s
' % + (c1.oid.hex, c3.oid.hex), + output.data) + self.assertIn( + '' + + '@@ -1,1 +1,3 @@', + output.data) + self.assertIn( + '+ Row 0', output.data) + self.assertEqual( + output.data.count( + '+ Row 0'), 2) + self.assertIn( + 'Commits \n ' + + '' + + '\n 3\n ', + output.data) + # View inverse commits comparison + output = self.app.get( + '/test/c/%s..%s' % (c3.oid.hex, c1.oid.hex)) + self.assertEqual(output.status_code, 200) + self.assertIn( + 'Diff from %s to %s - test - Pagure' % + (c3.oid.hex, c1.oid.hex), output.data) + self.assertIn( + '
%s .. %s
' % + (c3.oid.hex, c1.oid.hex), + output.data) + self.assertIn( + '' + + '@@ -1,3 +1,1 @@', + output.data) + self.assertIn( + '- Row 0', + output.data) + self.assertEqual( + output.data.count( + '- Row 0'), 2) + self.assertIn( + 'Commits \n ' + + '' + + '\n 3\n ', + output.data) + output = self.app.get('/foo/bar') # No project registered in the DB self.assertEqual(output.status_code, 404) @@ -1142,72 +1247,31 @@ class PagureFlaskRepotests(tests.Modeltests): 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')) + # Add one commit to git repo + tests.add_commit_git_repo( + os.path.join(tests.HERE, 'test.git'), ncommits=1) + c1 = repo.revparse_single('HEAD') - repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git')) + # Add another commit to git repo + tests.add_commit_git_repo( + os.path.join(tests.HERE, 'test.git'), ncommits=1) 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) + # Add one more commit to git repo + tests.add_commit_git_repo( + os.path.join(tests.HERE, 'test.git'), ncommits=1) + c3 = repo.revparse_single('HEAD') - # 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) + compare_first_two(c1, c2) + compare_all(c1, c3) 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) + compare_first_two(c1, c2) + compare_all(c1, c3) def test_view_file(self): """ Test the view_file endpoint. """