gitfs: Fix use of deprecated pygit2 function

0.27.4 (released 5 days ago) removed pygit2.Reference.get_object()
This commit is contained in:
Erik Johnson 2019-01-24 07:18:11 -06:00
parent aacc047686
commit c02757d70b
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -1410,6 +1410,19 @@ class Pygit2(GitProvider):
override_params, cache_root, role
)
def peel(self, obj):
'''
Compatibility function for pygit2.Reference objects. Older versions of
pygit2 use .get_object() to return the object to which the reference
points, while newer versions use .peel(). In pygit2 0.27.4,
.get_object() was removed. This function will try .peel() first and
fall back to .get_object().
'''
try:
return obj.peel()
except AttributeError:
return obj.get_object()
def checkout(self):
'''
Checkout the configured branch/tag
@ -1428,7 +1441,7 @@ class Pygit2(GitProvider):
return None
try:
head_sha = local_head.get_object().hex
head_sha = self.peel(local_head).hex
except AttributeError:
# Shouldn't happen, but just in case a future pygit2 API change
# breaks things, avoid a traceback and log an error.
@ -1477,7 +1490,7 @@ class Pygit2(GitProvider):
try:
if remote_ref in refs:
# Get commit id for the remote ref
oid = self.repo.lookup_reference(remote_ref).get_object().id
oid = self.peel(self.repo.lookup_reference(remote_ref)).id
if local_ref not in refs:
# No local branch for this remote, so create one and point
# it at the commit id of the remote ref
@ -1485,7 +1498,7 @@ class Pygit2(GitProvider):
try:
target_sha = \
self.repo.lookup_reference(remote_ref).get_object().hex
self.peel(self.repo.lookup_reference(remote_ref)).hex
except KeyError:
log.error(
'pygit2 was unable to get SHA for %s in %s remote '
@ -1857,8 +1870,8 @@ class Pygit2(GitProvider):
refs/remotes/origin/
'''
try:
return self.repo.lookup_reference(
'refs/remotes/origin/{0}'.format(ref)).get_object().tree
return self.peel(self.repo.lookup_reference(
'refs/remotes/origin/{0}'.format(ref))).tree
except KeyError:
return None
@ -1867,8 +1880,8 @@ class Pygit2(GitProvider):
Return a pygit2.Tree object matching a tag ref fetched into refs/tags/
'''
try:
return self.repo.lookup_reference(
'refs/tags/{0}'.format(ref)).get_object().tree
return self.peel(self.repo.lookup_reference(
'refs/tags/{0}'.format(ref))).tree
except KeyError:
return None