diff hgext/git/__init__.py @ 44484:ec54b3d2af0b

git: don't fail import when pygit2 is not install `test-duplicateoptions.py` was failing on py2 for be because I didn't have pygit2 installed. It failed because we depend on pygit2 at import time. This patch makes it so we successfully load the git extension even if pygit2 doesn't exist -- we just won't be able to use it in that case. Differential Revision: https://phab.mercurial-scm.org/D8268
author Martin von Zweigbergk <martinvonz@google.com>
date Mon, 09 Mar 2020 11:18:33 -0700
parents ad718271a9eb
children 02c47b74366c
line wrap: on
line diff
--- a/hgext/git/__init__.py	Mon Mar 09 12:53:21 2020 -0700
+++ b/hgext/git/__init__.py	Mon Mar 09 11:18:33 2020 -0700
@@ -8,8 +8,6 @@
 
 import os
 
-import pygit2
-
 from mercurial.i18n import _
 
 from mercurial import (
@@ -29,7 +27,6 @@
     index,
 )
 
-
 # TODO: extract an interface for this in core
 class gitstore(object):  # store.basicstore):
     def __init__(self, path, vfstype):
@@ -39,7 +36,7 @@
         # above lines should go away in favor of:
         # super(gitstore, self).__init__(path, vfstype)
 
-        self.git = pygit2.Repository(
+        self.git = gitutil.get_pygit2().Repository(
             os.path.normpath(os.path.join(path, b'..', b'.git'))
         )
         self._progress_factory = lambda *args, **kwargs: None
@@ -89,6 +86,16 @@
 
 
 def _makestore(orig, requirements, storebasepath, vfstype):
+    # Check for presence of pygit2 only here. The assumption is that we'll
+    # run this code iff we'll later need pygit2.
+    if gitutil.get_pygit2() is None:
+        raise error.Abort(
+            _(
+                b'the git extension requires the Python '
+                b'pygit2 library to be installed'
+            )
+        )
+
     if os.path.exists(
         os.path.join(storebasepath, b'this-is-git')
     ) and os.path.exists(os.path.join(storebasepath, b'..', b'.git')):
@@ -156,7 +163,7 @@
             if k in self:
                 return self[k]
             return default
-        except pygit2.InvalidSpecError:
+        except gitutil.get_pygit2().InvalidSpecError:
             return default
 
     @property