From c02757d70b38813e566a47954fc1118a55c8116d Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Thu, 24 Jan 2019 07:18:11 -0600 Subject: [PATCH] gitfs: Fix use of deprecated pygit2 function 0.27.4 (released 5 days ago) removed pygit2.Reference.get_object() --- salt/utils/gitfs.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/salt/utils/gitfs.py b/salt/utils/gitfs.py index 1cc06fcb09cd..36b825944750 100644 --- a/salt/utils/gitfs.py +++ b/salt/utils/gitfs.py @@ -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 @@ -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. @@ -1477,7 +1490,7 @@ 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 @@ -1485,7 +1498,7 @@ def _perform_checkout(checkout_ref, branch=True): 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 @@ 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 @@ -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