Skip to content

Commit

Permalink
gitfs: Fix use of deprecated pygit2 function
Browse files Browse the repository at this point in the history
0.27.4 (released 5 days ago) removed pygit2.Reference.get_object()
  • Loading branch information
terminalmage committed Jan 24, 2019
1 parent aacc047 commit c02757d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions salt/utils/gitfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,19 @@ def __init__(self, opts, remote, per_remote_defaults, per_remote_only,
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
Expand All @@ -1428,7 +1441,7 @@ def checkout(self):
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.
Expand Down Expand Up @@ -1477,15 +1490,15 @@ def _perform_checkout(checkout_ref, branch=True):
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
self.repo.create_reference(local_ref, oid)

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 '
Expand Down Expand Up @@ -1857,8 +1870,8 @@ def get_tree_from_branch(self, ref):
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

Expand All @@ -1867,8 +1880,8 @@ def get_tree_from_tag(self, ref):
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

Expand Down

0 comments on commit c02757d

Please sign in to comment.