From d4bfa321235b364a5dcea5b3a7b3719da77793bf Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 07 2018 14:10:30 +0000 Subject: [PATCH 1/2] Support viewing commits from a specific commit hash With this commit we're allowing to view the list of commits from a specified commit instead of enforcing this be a branch name. This allows fixing the workflow: view release, view the corresponding tree, view the commits from there. Fixes https://pagure.io/pagure/issue/3190 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 307adf7..2b5af31 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -299,16 +299,26 @@ def view_commits(repo, branchname=None, username=None, namespace=None): repo = flask.g.repo repo_obj = flask.g.repo_obj - if branchname and branchname not in repo_obj.listall_branches(): - flask.abort(404, 'Branch not found') - - if branchname: + commit = None + branch = None + if branchname and branchname in repo_obj.listall_branches(): branch = repo_obj.lookup_branch(branchname) + commit = branch.get_object() + elif branchname: + try: + commit = repo_obj.get(branchname) + except (ValueError, TypeError): + pass + + # If we're arriving here from the release page, we may have a Tag + # where we expected a commit, in this case, get the actual commit + if isinstance(commit, pygit2.Tag): + commit = commit.get_object() + branchname = commit.oid.hex elif not repo_obj.is_empty and not repo_obj.head_is_unborn: branch = repo_obj.lookup_branch(repo_obj.head.shorthand) + commit = branch.get_object() branchname = branch.branch_name - else: - branch = None if not repo_obj.is_empty and not repo_obj.head_is_unborn: head = repo_obj.head.shorthand @@ -337,9 +347,9 @@ def view_commits(repo, branchname=None, username=None, namespace=None): n_commits = 0 last_commits = [] - if branch: + if commit: for commit in repo_obj.walk( - branch.get_object().hex, pygit2.GIT_SORT_TIME): + commit.hex, pygit2.GIT_SORT_TIME): # Filters the commits for a user if author_obj: @@ -916,6 +926,7 @@ def view_tree(repo, identifier=None, username=None, namespace=None): # where we expected a commit, in this case, get the actual commit if isinstance(commit, pygit2.Tag): commit = commit.get_object() + branchname = commit.oid.hex if commit and not isinstance(commit, pygit2.Blob): content = sorted(commit.tree, key=lambda x: x.filemode) diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 82ba0b9..1b345ab 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -1996,8 +1996,13 @@ class PagureFlaskRepotests(tests.Modeltests): os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'), ncommits=10) + # list is empty output = self.app.get('/fork/pingou/test3/commits/fobranch') - self.assertEqual(output.status_code, 404) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn( + '
\n
', + output_text) output = self.app.get('/fork/pingou/test3/commits') self.assertEqual(output.status_code, 200) From 6868c19e173653a7d075f92e44eb30c1b2f66bdb Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 07 2018 15:39:06 +0000 Subject: [PATCH 2/2] Allow viewing commits from a git tag Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 2b5af31..0b25230 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -310,6 +310,11 @@ def view_commits(repo, branchname=None, username=None, namespace=None): except (ValueError, TypeError): pass + if 'refs/tags/%s' % branchname in list(repo_obj.references): + ref = repo_obj.lookup_reference( + 'refs/tags/%s' % branchname) + commit = ref.get_object() + # If we're arriving here from the release page, we may have a Tag # where we expected a commit, in this case, get the actual commit if isinstance(commit, pygit2.Tag): diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 1b345ab..2cb20a1 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -2013,6 +2013,34 @@ class PagureFlaskRepotests(tests.Modeltests): 'test project #3 ', output_text) self.assertIn('Forked from', output_text) + def test_view_commits_from_tag(self): + """ Test the view_commits endpoint given a tag. """ + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + + # Add a README to the git repo - First commit + tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git')) + repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git')) + first_commit = repo.revparse_single('HEAD') + tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0) + repo.create_tag( + "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger, + "Release 0.0.1") + + tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git')) + repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git')) + latest_commit = repo.revparse_single('HEAD') + + output = self.app.get('/test/commits/0.0.1') + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn(first_commit.oid.hex, output_text) + self.assertNotIn(latest_commit.oid.hex, output_text) + self.assertIn('Commits - test - Pagure', output_text) + self.assertEqual( + output_text.count('class="badge badge-secondary commithash"'), 1) + def test_compare_commits(self): """ Test the compare_commits endpoint. """