From 3797b1340c701aee27ef2522c64fc4bd8734c2c4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:41:23 +0000 Subject: [PATCH 1/18] In pygit2 version 0.22.x, remote.push requires a list not a string as before --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index a207e48..822404a 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -198,7 +198,10 @@ def update_git(obj, repo, repofolder, objtype='ticket'): master_ref = new_repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Remove the clone shutil.rmtree(newpath) @@ -264,7 +267,10 @@ def clean_git(obj, repo, repofolder, objtype='ticket'): master_ref = new_repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Remove the clone shutil.rmtree(newpath) @@ -648,7 +654,10 @@ def add_file_to_git(repo, issue, ticketfolder, user, filename, filestream): master_ref = new_repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Remove the clone shutil.rmtree(newpath) From 97a6d19fa28405a406a6cde8d8e972eee1528bc2 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:41:23 +0000 Subject: [PATCH 2/18] Unit-tests also need to be adjusted for pygit2 0.22 --- diff --git a/tests/__init__.py b/tests/__init__.py index 44b2720..f6a2087 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -365,7 +365,10 @@ def add_content_git_repo(folder): master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) shutil.rmtree(newfolder) @@ -434,7 +437,10 @@ Dev instance: http://209.132.184.222/ (/!\\ May change unexpectedly, it's a dev master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) shutil.rmtree(newfolder) @@ -486,7 +492,10 @@ def add_commit_git_repo(folder, ncommits=10): master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) shutil.rmtree(newfolder) @@ -553,7 +562,10 @@ C7Pí^DQeee<84>ÃaÜn·î<98><9e><9e>^^¶oß®<95>ݦM^^T©®®¦®®<8e>©©)� master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) shutil.rmtree(newfolder) diff --git a/tests/test_pagure_flask_docs.py b/tests/test_pagure_flask_docs.py index 0d4aaa1..5c6f0d1 100644 --- a/tests/test_pagure_flask_docs.py +++ b/tests/test_pagure_flask_docs.py @@ -155,7 +155,11 @@ class PagureFlaskDocstests(tests.Modeltests): # Push the changes to the bare repo remote = repo.create_remote( 'origin', os.path.join(tests.HERE, 'docs', 'test.git')) - remote.push('refs/heads/master:refs/heads/master') + + if pygit2.__version__.startswith('0.22'): + remote.push(['refs/heads/master:refs/heads/master']) + else: + remote.push('refs/heads/master:refs/heads/master') # Now check the UI diff --git a/tests/test_progit_flask_ui_fork.py b/tests/test_progit_flask_ui_fork.py index 38f67b0..6a5e41c 100644 --- a/tests/test_progit_flask_ui_fork.py +++ b/tests/test_progit_flask_ui_fork.py @@ -93,7 +93,10 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) first_commit = repo.revparse_single('HEAD') @@ -121,7 +124,10 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) if mtype == 'conflicts': with open(os.path.join(repopath, 'sources'), 'w') as stream: @@ -147,7 +153,10 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Set the second repo @@ -184,7 +193,10 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/%s' % (branch_from) ori_remote = repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Create a PR for these changes project = pagure.lib.get_project(self.session, 'test') @@ -554,7 +566,10 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/feature:refs/heads/feature' ori_remote = new_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Create a PR for these changes project = pagure.lib.get_project(self.session, 'test') @@ -856,7 +871,10 @@ index 9f44358..2a552bb 100644 ) refname = 'refs/heads/feature:refs/heads/feature' ori_remote = new_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Create a PR for these "changes" (there are none, both repos are # empty) diff --git a/tests/test_progit_flask_ui_no_master_branch.py b/tests/test_progit_flask_ui_no_master_branch.py index d5c766b..2b17a66 100644 --- a/tests/test_progit_flask_ui_no_master_branch.py +++ b/tests/test_progit_flask_ui_no_master_branch.py @@ -115,7 +115,10 @@ class PagureFlaskNoMasterBranchtests(tests.Modeltests): refname = 'refs/heads/feature' ori_remote = clone_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) shutil.rmtree(newpath) diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index 688415b..b7374a3 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -583,7 +583,10 @@ class PagureFlaskRepotests(tests.Modeltests): ) refname = 'refs/heads/feature' ori_remote = new_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) output = self.app.get('/test') self.assertEqual(output.status_code, 200) diff --git a/tests/test_progit_flask_ui_slash_branch_name.py b/tests/test_progit_flask_ui_slash_branch_name.py index f8f1ca0..6db5ba2 100644 --- a/tests/test_progit_flask_ui_slash_branch_name.py +++ b/tests/test_progit_flask_ui_slash_branch_name.py @@ -89,7 +89,10 @@ class PagureFlaskSlashInBranchtests(tests.Modeltests): ) refname = 'refs/heads/master' ori_remote = clone_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) master_branch = clone_repo.lookup_branch('master') first_commit = master_branch.get_object().hex @@ -118,7 +121,10 @@ class PagureFlaskSlashInBranchtests(tests.Modeltests): refname = 'refs/heads/maxamilion/feature' ori_remote = clone_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) shutil.rmtree(newpath) From 8807b665e1350f1586b042f56bf3472e87745bcb Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:41:23 +0000 Subject: [PATCH 3/18] And one more fix for pygit 0.22 --- diff --git a/tests/test_progit_flask_api_project.py b/tests/test_progit_flask_api_project.py index 555f5bd..8ad9c94 100644 --- a/tests/test_progit_flask_api_project.py +++ b/tests/test_progit_flask_api_project.py @@ -84,7 +84,10 @@ class PagureFlaskApiProjecttests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Tag our first commit first_commit = repo.revparse_single('HEAD') From 6c6664ac37e76f992b9ddce13f83542b284fc97a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:41:23 +0000 Subject: [PATCH 4/18] Yay, even the commits hash are not strings anymore --- diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index 62c3042..c5e3f62 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -204,7 +204,7 @@ def avatar_url(email, size=64): @APP.template_filter('short') def shorted_commit(cid): """Gets short version of the commit id""" - return cid[:APP.config['SHORT_LENGTH']] + return str(cid)[:APP.config['SHORT_LENGTH']] @APP.template_filter('markdown') From 66df483bea83380450ac087d6a1a316ef0eb1867 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:41:23 +0000 Subject: [PATCH 5/18] And another couple of fixes to remote.push() --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 822404a..4ded463 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -746,7 +746,10 @@ def update_file_in_git( branchto) try: - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) except pygit2.GitError as err: # pragma: no cover shutil.rmtree(newpath) raise pagure.exceptions.PagureException( @@ -958,7 +961,10 @@ def merge_pull_request( elif merge is None and mergecode is not None: branch_ref.set_target(repo_commit.oid.hex) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) else: request.merge_status = 'FFORWARD' session.commit() @@ -990,7 +996,10 @@ def merge_pull_request( 'Merge #%s `%s`' % (request.id, request.title), tree, [head.hex, repo_commit.oid.hex]) - ori_remote.push(refname) + if pygit2.__version__.startswith('0.22'): + ori_remote.push([refname]) + else: + ori_remote.push(refname) # Update status pagure.lib.close_pull_request( From cf88d8e9c7dff32a91019252bea6f3dab1131184 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:41:43 +0000 Subject: [PATCH 6/18] Add a method to try to find out if a given text is a binary file or not --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 139a5c7..4ba145a 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2366,3 +2366,11 @@ def clean_input(text): ], attributes=attrs, ) + + +def is_binary(text): + """ Returns wether we think this text correspond to a binary file or not + """ + textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100)) + is_binary_string = lambda bytes: bool(bytes.translate(None, textchars)) + return is_binary_string(text) From 1f1193f2ba514d3258d7bc00f21f7cd2e09037d3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 11 2015 08:42:22 +0000 Subject: [PATCH 7/18] Rely on both pygit2 and our own method to find out if a file is a binary --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index c447382..aecdaf2 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -1300,9 +1300,9 @@ def edit_file(repo, branchname, filename, username=None): repo_obj, commit.tree, filename.split('/')) if not content or isinstance(content, pygit2.Tree): flask.abort(404, 'File not found') - if content.is_binary: - flask.abort(400, 'Cannot edit binary files') data = repo_obj[content.oid].data + if content.is_binary or pagure.lib.is_binary(data): + flask.abort(400, 'Cannot edit binary files') else: data = form.content.data From 9826234d687b4f61d5ecf573ed878bf73f778f53 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 09:25:30 +0000 Subject: [PATCH 8/18] Look for the null byte in binary file --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 4ba145a..195c37f 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2371,6 +2371,4 @@ def clean_input(text): def is_binary(text): """ Returns wether we think this text correspond to a binary file or not """ - textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100)) - is_binary_string = lambda bytes: bool(bytes.translate(None, textchars)) - return is_binary_string(text) + return '\0' in text From 97c77d86afd5f6258192aa2bcb85c995dfe95c21 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 09:57:00 +0000 Subject: [PATCH 9/18] Another attempt to figure out if the content is binary --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 195c37f..7b1e7c2 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2371,4 +2371,20 @@ def clean_input(text): def is_binary(text): """ Returns wether we think this text correspond to a binary file or not """ - return '\0' in text + btext = bytes(text) + if b'\x00' in btext: + return True + + _printable_extended_ascii = b'\n\r\t\f\b' + if bytes is str: + # Python 2 means we need to invoke chr() explicitly + _printable_extended_ascii += b''.join(map(chr, range(32, 256))) + else: + # Python 3 means bytes accepts integer input directly + _printable_extended_ascii += bytes(range(32, 256)) + + # Now check for a high percentage of ASCII control characters + # Binary if control chars are > 30% of the string + control_chars = btext.translate(None, _printable_extended_ascii) + nontext_ratio = float(len(control_chars)) / float(len(btext)) + return nontext_ratio > 0.3 From fe765eb69492e1ae9cf8c5f75bad7bbfc0c935dc Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 13:51:05 +0000 Subject: [PATCH 10/18] More attempt to figure out if a file is a binary or an image --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 7b1e7c2..a5b21f8 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2372,6 +2372,24 @@ def is_binary(text): """ Returns wether we think this text correspond to a binary file or not """ btext = bytes(text) + + import imghdr + print 'imghdr', imghdr.what('foo', h=btext) + if imghdr.what('foo', h=text): + return True + + import StringIO + from PIL import Image + try: + im = Image.open(text) + im = Image.open(StringIO.StringIO(text)) + if im: + return True + except IOError, err: + print 'ERROR', err + pass + print btext + if b'\x00' in btext: return True From 6f963cab64a7c7e27b14fba0d308b2e5951a611b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 13:55:33 +0000 Subject: [PATCH 11/18] Drop a line that will never work --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index a5b21f8..d147fad 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2381,7 +2381,6 @@ def is_binary(text): import StringIO from PIL import Image try: - im = Image.open(text) im = Image.open(StringIO.StringIO(text)) if im: return True From eae5cce2d4c2a7daeaf3057ec1bc5c3ecb8b68ae Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 15:43:30 +0000 Subject: [PATCH 12/18] Rename is_binary into could_be_text and adjust its logic Finally the easiest way to see if the content of a blob is binary or text is to try to encode it in UTF-8. If that works, we'll assume jinja will render it correctly, otherwise we consider it a binary file and bail out. --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index d147fad..2acbaaf 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2368,40 +2368,10 @@ def clean_input(text): ) -def is_binary(text): - """ Returns wether we think this text correspond to a binary file or not +def could_be_text(text): + """ Returns wether we think this chain of character could be text or not """ - btext = bytes(text) - - import imghdr - print 'imghdr', imghdr.what('foo', h=btext) - if imghdr.what('foo', h=text): - return True - - import StringIO - from PIL import Image try: - im = Image.open(StringIO.StringIO(text)) - if im: - return True - except IOError, err: - print 'ERROR', err - pass - print btext - - if b'\x00' in btext: + text.encode('utf-8') + except: return True - - _printable_extended_ascii = b'\n\r\t\f\b' - if bytes is str: - # Python 2 means we need to invoke chr() explicitly - _printable_extended_ascii += b''.join(map(chr, range(32, 256))) - else: - # Python 3 means bytes accepts integer input directly - _printable_extended_ascii += bytes(range(32, 256)) - - # Now check for a high percentage of ASCII control characters - # Binary if control chars are > 30% of the string - control_chars = btext.translate(None, _printable_extended_ascii) - nontext_ratio = float(len(control_chars)) / float(len(btext)) - return nontext_ratio > 0.3 From 3836a7f2a575dec4696ece877bc080d149615806 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 15:44:30 +0000 Subject: [PATCH 13/18] Check if the content is binary or could be text --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index aecdaf2..e603aa1 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -354,7 +354,7 @@ def view_file(repo, identifier, filename, username=None): content = commit if isinstance(content, pygit2.Blob): - if content.is_binary: + if content.is_binary or pagure.lib.could_be_text(data): ext = filename[filename.rfind('.'):] if ext in ( '.gif', '.png', '.bmp', '.tif', '.tiff', '.jpg', @@ -1300,8 +1300,9 @@ def edit_file(repo, branchname, filename, username=None): repo_obj, commit.tree, filename.split('/')) if not content or isinstance(content, pygit2.Tree): flask.abort(404, 'File not found') + data = repo_obj[content.oid].data - if content.is_binary or pagure.lib.is_binary(data): + if content.is_binary or pagure.lib.could_be_text(data): flask.abort(400, 'Cannot edit binary files') else: data = form.content.data From 7f81dae270b25f007d5eb8760a77875bd7b67002 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 15:45:34 +0000 Subject: [PATCH 14/18] Adjust the boolean returned to the name of the method Return True when the string could be text, otherwise return False --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 2acbaaf..4216207 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2373,5 +2373,6 @@ def could_be_text(text): """ try: text.encode('utf-8') - except: return True + except: + return False diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index e603aa1..da200c6 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -354,7 +354,7 @@ def view_file(repo, identifier, filename, username=None): content = commit if isinstance(content, pygit2.Blob): - if content.is_binary or pagure.lib.could_be_text(data): + if content.is_binary or not pagure.lib.could_be_text(data): ext = filename[filename.rfind('.'):] if ext in ( '.gif', '.png', '.bmp', '.tif', '.tiff', '.jpg', @@ -1302,7 +1302,7 @@ def edit_file(repo, branchname, filename, username=None): flask.abort(404, 'File not found') data = repo_obj[content.oid].data - if content.is_binary or pagure.lib.could_be_text(data): + if content.is_binary or not pagure.lib.could_be_text(data): flask.abort(400, 'Cannot edit binary files') else: data = form.content.data From 7a86e0d90061503de4d3e89e2f45080ac978ec64 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 15 2015 15:48:33 +0000 Subject: [PATCH 15/18] Call the function on the proper variable --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index da200c6..112c364 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -354,7 +354,7 @@ def view_file(repo, identifier, filename, username=None): content = commit if isinstance(content, pygit2.Blob): - if content.is_binary or not pagure.lib.could_be_text(data): + if content.is_binary or not pagure.lib.could_be_text(content.data): ext = filename[filename.rfind('.'):] if ext in ( '.gif', '.png', '.bmp', '.tif', '.tiff', '.jpg', From 6aab3c39927ce8cd1df255c237e67c5c06a4623b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 17 2015 08:39:36 +0000 Subject: [PATCH 16/18] Introduce the PagureRepo class, sub-class of pygit2.Repository The aim is to concentrate in this class all the logic to handle the different breakage of pygit2's API, either overriding methods or using static methods --- diff --git a/pagure/lib/repo.py b/pagure/lib/repo.py new file mode 100644 index 0000000..12dbd66 --- /dev/null +++ b/pagure/lib/repo.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + + +import pygit2 + + +class PagureRepo(pygit2.Repository): + """ An utility class allowing to go around pygit2's inability to be + stable. + + """ + + @staticmethod + def push(remote, refname): + """ Push the given reference to the specified remote. """ + if pygit2.__version__.startswith('0.22'): + remote.push([refname]) + else: + remote.push(refname) From 60996545cae3442030bef633b55c766b00e63872 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 17 2015 08:40:28 +0000 Subject: [PATCH 17/18] Rely on PagureRepo and PagureRepo.push() --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 4ded463..d620061 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -27,6 +27,7 @@ import pagure.exceptions import pagure.lib import pagure.lib.notify from pagure.lib import model +from pagure.lib.repo import PagureRepo # pylint: disable=R0913,E1101,R0914 @@ -198,10 +199,7 @@ def update_git(obj, repo, repofolder, objtype='ticket'): master_ref = new_repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Remove the clone shutil.rmtree(newpath) @@ -267,10 +265,7 @@ def clean_git(obj, repo, repofolder, objtype='ticket'): master_ref = new_repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Remove the clone shutil.rmtree(newpath) @@ -654,10 +649,7 @@ def add_file_to_git(repo, issue, ticketfolder, user, filename, filestream): master_ref = new_repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Remove the clone shutil.rmtree(newpath) @@ -746,10 +738,7 @@ def update_file_in_git( branchto) try: - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) except pygit2.GitError as err: # pragma: no cover shutil.rmtree(newpath) raise pagure.exceptions.PagureException( @@ -867,7 +856,7 @@ def merge_pull_request( ''' # Get the fork repopath = pagure.get_repo_path(request.project_from) - fork_obj = pygit2.Repository(repopath) + fork_obj = PagureRepo(repopath) # Get the original repo parentpath = pagure.get_repo_path(request.project) @@ -878,7 +867,7 @@ def merge_pull_request( # Update the start and stop commits in the DB, one last time diff_commits = diff_pull_request( - session, request, pygit2.Repository(parentpath), fork_obj, + session, request, PagureRepo(parentpath), fork_obj, requestfolder=request_folder, with_diff=False)[0] if request.project.settings.get( @@ -961,10 +950,7 @@ def merge_pull_request( elif merge is None and mergecode is not None: branch_ref.set_target(repo_commit.oid.hex) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) else: request.merge_status = 'FFORWARD' session.commit() @@ -996,10 +982,7 @@ def merge_pull_request( 'Merge #%s `%s`' % (request.id, request.title), tree, [head.hex, repo_commit.oid.hex]) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Update status pagure.lib.close_pull_request( @@ -1100,7 +1083,7 @@ def get_git_tags(project): specified project. """ repopath = pagure.get_repo_path(project) - repo_obj = pygit2.Repository(repopath) + repo_obj = PagureRepo(repopath) tags = [ tag.split('refs/tags/')[1] for tag in repo_obj.listall_references() @@ -1114,7 +1097,7 @@ def get_git_tags_objects(project): repositorie the specified project. """ repopath = pagure.get_repo_path(project) - repo_obj = pygit2.Repository(repopath) + repo_obj = PagureRepo(repopath) tags = [ repo_obj.lookup_reference(tag) for tag in repo_obj.listall_references() From 27a73daf1ead6a227ce0a0122c8824149f5913fc Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 17 2015 08:46:06 +0000 Subject: [PATCH 18/18] Adjust the unit-tests to rely on PagureRepo.push() as well --- diff --git a/tests/__init__.py b/tests/__init__.py index f6a2087..e9872b8 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -35,7 +35,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure import pagure.lib import pagure.lib.model - +from pagure.lib.repo import PagureRepo DB_PATH = 'sqlite:///:memory:' FAITOUT_URL = 'http://209.132.184.152/faitout/' @@ -365,10 +365,7 @@ def add_content_git_repo(folder): master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) shutil.rmtree(newfolder) @@ -437,10 +434,7 @@ Dev instance: http://209.132.184.222/ (/!\\ May change unexpectedly, it's a dev master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) shutil.rmtree(newfolder) @@ -492,10 +486,7 @@ def add_commit_git_repo(folder, ncommits=10): master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) shutil.rmtree(newfolder) @@ -562,10 +553,7 @@ C7Pí^DQeee<84>ÃaÜn·î<98><9e><9e>^^¶oß®<95>ݦM^^T©®®¦®®<8e>©©)� master_ref = repo.lookup_reference('HEAD').resolve() refname = '%s:%s' % (master_ref.name, master_ref.name) - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) shutil.rmtree(newfolder) diff --git a/tests/test_pagure_flask_docs.py b/tests/test_pagure_flask_docs.py index 5c6f0d1..4ab0378 100644 --- a/tests/test_pagure_flask_docs.py +++ b/tests/test_pagure_flask_docs.py @@ -26,6 +26,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure.docs_server import pagure.lib import tests +from pagure.lib.repo import PagureRepo class PagureFlaskDocstests(tests.Modeltests): @@ -156,10 +157,7 @@ class PagureFlaskDocstests(tests.Modeltests): remote = repo.create_remote( 'origin', os.path.join(tests.HERE, 'docs', 'test.git')) - if pygit2.__version__.startswith('0.22'): - remote.push(['refs/heads/master:refs/heads/master']) - else: - remote.push('refs/heads/master:refs/heads/master') + PagureRepo.push(remote, 'refs/heads/master:refs/heads/master') # Now check the UI diff --git a/tests/test_progit_flask_api_project.py b/tests/test_progit_flask_api_project.py index 8ad9c94..c4faf89 100644 --- a/tests/test_progit_flask_api_project.py +++ b/tests/test_progit_flask_api_project.py @@ -28,6 +28,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure.lib import tests +from pagure.lib.repo import PagureRepo class PagureFlaskApiProjecttests(tests.Modeltests): @@ -84,10 +85,7 @@ class PagureFlaskApiProjecttests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Tag our first commit first_commit = repo.revparse_single('HEAD') diff --git a/tests/test_progit_flask_ui_fork.py b/tests/test_progit_flask_ui_fork.py index 6a5e41c..24e499f 100644 --- a/tests/test_progit_flask_ui_fork.py +++ b/tests/test_progit_flask_ui_fork.py @@ -26,6 +26,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure.lib import tests +from pagure.lib.repo import PagureRepo class PagureFlaskForktests(tests.Modeltests): @@ -93,10 +94,7 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) first_commit = repo.revparse_single('HEAD') @@ -124,10 +122,7 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) if mtype == 'conflicts': with open(os.path.join(repopath, 'sources'), 'w') as stream: @@ -153,10 +148,7 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/master:refs/heads/master' ori_remote = clone_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Set the second repo @@ -193,10 +185,7 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/%s' % (branch_from) ori_remote = repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Create a PR for these changes project = pagure.lib.get_project(self.session, 'test') @@ -566,10 +555,7 @@ class PagureFlaskForktests(tests.Modeltests): ) refname = 'refs/heads/feature:refs/heads/feature' ori_remote = new_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Create a PR for these changes project = pagure.lib.get_project(self.session, 'test') @@ -871,10 +857,7 @@ index 9f44358..2a552bb 100644 ) refname = 'refs/heads/feature:refs/heads/feature' ori_remote = new_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) # Create a PR for these "changes" (there are none, both repos are # empty) diff --git a/tests/test_progit_flask_ui_no_master_branch.py b/tests/test_progit_flask_ui_no_master_branch.py index 2b17a66..2addff7 100644 --- a/tests/test_progit_flask_ui_no_master_branch.py +++ b/tests/test_progit_flask_ui_no_master_branch.py @@ -26,6 +26,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure.lib import tests +from pagure.lib.repo import PagureRepo class PagureFlaskNoMasterBranchtests(tests.Modeltests): @@ -115,10 +116,7 @@ class PagureFlaskNoMasterBranchtests(tests.Modeltests): refname = 'refs/heads/feature' ori_remote = clone_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) shutil.rmtree(newpath) diff --git a/tests/test_progit_flask_ui_repo.py b/tests/test_progit_flask_ui_repo.py index b7374a3..0c53945 100644 --- a/tests/test_progit_flask_ui_repo.py +++ b/tests/test_progit_flask_ui_repo.py @@ -26,6 +26,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure.lib import tests +from pagure.lib.repo import PagureRepo class PagureFlaskRepotests(tests.Modeltests): @@ -583,10 +584,7 @@ class PagureFlaskRepotests(tests.Modeltests): ) refname = 'refs/heads/feature' ori_remote = new_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) output = self.app.get('/test') self.assertEqual(output.status_code, 200) diff --git a/tests/test_progit_flask_ui_slash_branch_name.py b/tests/test_progit_flask_ui_slash_branch_name.py index 6db5ba2..02691e1 100644 --- a/tests/test_progit_flask_ui_slash_branch_name.py +++ b/tests/test_progit_flask_ui_slash_branch_name.py @@ -26,6 +26,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure.lib import tests +from pagure.lib.repo import PagureRepo class PagureFlaskSlashInBranchtests(tests.Modeltests): @@ -89,10 +90,7 @@ class PagureFlaskSlashInBranchtests(tests.Modeltests): ) refname = 'refs/heads/master' ori_remote = clone_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) master_branch = clone_repo.lookup_branch('master') first_commit = master_branch.get_object().hex @@ -121,10 +119,7 @@ class PagureFlaskSlashInBranchtests(tests.Modeltests): refname = 'refs/heads/maxamilion/feature' ori_remote = clone_repo.remotes[0] - if pygit2.__version__.startswith('0.22'): - ori_remote.push([refname]) - else: - ori_remote.push(refname) + PagureRepo.push(ori_remote, refname) shutil.rmtree(newpath)