From 4b90cc8737502bdc7bc9a0ef91b791a618848e24 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 31 2017 15:55:55 +0000 Subject: Consolidate the code to simplify it and add diff view on commits Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index ad5665e..765307a 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -40,7 +40,7 @@ from pagure.lib import tasks _log = logging.getLogger(__name__) -def commit_to_patch(repo_obj, commits): +def commit_to_patch(repo_obj, commits, diff_view=False): ''' For a given commit (PyGit2 commit object) of a specified git repo, returns a string representation of the changes the commit did in a format that allows it to be used as patch. @@ -55,17 +55,21 @@ def commit_to_patch(repo_obj, commits): else: # First commit in the repo diff = commit.tree.diff_to_tree(swap=True) - - subject = message = '' - if '\n' in commit.message: - subject, message = commit.message.split('\n', 1) + if diff_view: + patch.append(diff.patch) else: - subject = commit.message - if len(commits) > 1: - subject = '[PATCH %s/%s] %s' % (cnt + 1, len(commits), subject) + subject = message = '' + if '\n' in commit.message: + subject, message = commit.message.split('\n', 1) + else: + subject = commit.message + + if len(commits) > 1: + subject = '[PATCH %s/%s] %s' % ( + cnt + 1, len(commits), subject) - patch.append(u"""From {commit} Mon Sep 17 00:00:00 2001 + patch.append(u"""From {commit} Mon Sep 17 00:00:00 2001 From: {author_name} <{author_email}> Date: {date} Subject: {subject} @@ -74,14 +78,15 @@ Subject: {subject} --- {patch} -""".format(commit=commit.oid.hex, - author_name=commit.author.name, - author_email=commit.author.email, - date=datetime.datetime.utcfromtimestamp( - commit.commit_time).strftime('%b %d %Y %H:%M:%S +0000'), - subject=subject, - msg=message, - patch=diff.patch)) +""".format( + commit=commit.oid.hex, + author_name=commit.author.name, + author_email=commit.author.email, + date=datetime.datetime.utcfromtimestamp( + commit.commit_time).strftime('%b %d %Y %H:%M:%S +0000'), + subject=subject, + msg=message, + patch=diff.patch)) return ''.join(patch) diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index a81dcd4..39b292d 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -344,21 +344,8 @@ def request_pull_to_diff_or_patch( 'error') diff_commits.reverse() - if not diff: - patch = pagure.lib.git.commit_to_patch(repo_obj, diff_commits) - else: - if not isinstance(diff_commits, list): - diff_commits = [diff_commits] - - patch = [] - for cnt, commit in enumerate(diff_commits): - if commit.parents: - diff = repo_obj.diff(commit.parents[0], commit) - else: - # First commit in the repo - diff = commit.tree.diff_to_tree(swap=True) - patch.append(diff.patch) - patch = '\n'.join(patch) + patch = pagure.lib.git.commit_to_patch( + repo_obj, diff_commits, diff_view=diff) return flask.Response(patch, content_type="text/plain;charset=UTF-8") diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 5134fa6..ae6ab16 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -789,6 +789,25 @@ def view_commit(repo, commitid, username=None, namespace=None): def view_commit_patch(repo, commitid, username=None, namespace=None): """ Render a commit in a repo as patch """ + return view_commit_patch_or_diff( + repo, commitid, username, namespace, diff=False) + + +@APP.route('//c/.diff') +@APP.route('///c/.diff') +@APP.route('/fork///c/.diff') +@APP.route('/fork////c/.diff') +def view_commit_diff(repo, commitid, username=None, namespace=None): + """ Render a commit in a repo as diff + """ + return view_commit_patch_or_diff( + repo, commitid, username, namespace, diff=True) + + +def view_commit_patch_or_diff( + repo, commitid, username=None, namespace=None, diff=False): + """ Renders a commit either as a patch or as a diff. """ + repo_obj = flask.g.repo_obj try: @@ -799,7 +818,8 @@ def view_commit_patch(repo, commitid, username=None, namespace=None): if commit is None: flask.abort(404, 'Commit not found') - patch = pagure.lib.git.commit_to_patch(repo_obj, commit) + patch = pagure.lib.git.commit_to_patch( + repo_obj, commit, diff_view=diff) return flask.Response(patch, content_type="text/plain;charset=UTF-8") diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 64a00d4..0d52365 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -2378,7 +2378,6 @@ class PagureFlaskRepotests(tests.Modeltests): '' in output.data) - def test_view_commit_patch(self): """ Test the view_commit_patch endpoint. """ @@ -2507,6 +2506,56 @@ index 0000000..fb7093d +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-)) ''' in output.data) + def test_view_commit_diff(self): + """ Test the view_commit_diff endpoint. """ + + # No project registered in the DB + output = self.app.get('/foo/c/bar.diff') + self.assertEqual(output.status_code, 404) + + tests.create_projects(self.session) + + output = self.app.get('/test/c/bar.diff') + # No git repo associated + self.assertEqual(output.status_code, 404) + + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + + output = self.app.get('/test/c/bar.diff') + self.assertEqual(output.status_code, 404) + + # 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')) + commit = repo.revparse_single('HEAD') + + # View first commit + output = self.app.get('/test/c/%s.diff' % commit.oid.hex) + self.assertEqual(output.status_code, 200) + self.assertEqual('''diff --git a/README.rst b/README.rst +new file mode 100644 +index 0000000..fb7093d +--- /dev/null ++++ b/README.rst +@@ -0,0 +1,16 @@ ++Pagure ++====== ++ ++:Author: Pierre-Yves Chibon ++ ++ ++Pagure is a light-weight git-centered forge based on pygit2. ++ ++Currently, Pagure offers a web-interface for git repositories, a ticket ++system and possibilities to create new projects, fork existing ones and ++create/merge pull-requests across or within projects. ++ ++ ++Homepage: https://github.com/pypingou/pagure ++ ++Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-)) +''', output.data) + def test_view_tree(self): """ Test the view_tree endpoint. """ output = self.app.get('/foo/tree/')