diff hgext/git/dirstate.py @ 44496: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 eb061d272af4
line wrap: on
line diff
--- a/hgext/git/dirstate.py	Mon Mar 09 12:53:21 2020 -0700
+++ b/hgext/git/dirstate.py	Mon Mar 09 11:18:33 2020 -0700
@@ -4,8 +4,6 @@
 import errno
 import os
 
-import pygit2
-
 from mercurial import (
     error,
     extensions,
@@ -22,6 +20,8 @@
 
 from . import gitutil
 
+pygit2 = gitutil.get_pygit2()
+
 
 def readpatternfile(orig, filepath, warn, sourceinfo=False):
     if not (b'info/exclude' in filepath or filepath.endswith(b'.gitignore')):
@@ -46,23 +46,25 @@
 extensions.wrapfunction(matchmod, b'readpatternfile', readpatternfile)
 
 
-_STATUS_MAP = {
-    pygit2.GIT_STATUS_CONFLICTED: b'm',
-    pygit2.GIT_STATUS_CURRENT: b'n',
-    pygit2.GIT_STATUS_IGNORED: b'?',
-    pygit2.GIT_STATUS_INDEX_DELETED: b'r',
-    pygit2.GIT_STATUS_INDEX_MODIFIED: b'n',
-    pygit2.GIT_STATUS_INDEX_NEW: b'a',
-    pygit2.GIT_STATUS_INDEX_RENAMED: b'a',
-    pygit2.GIT_STATUS_INDEX_TYPECHANGE: b'n',
-    pygit2.GIT_STATUS_WT_DELETED: b'r',
-    pygit2.GIT_STATUS_WT_MODIFIED: b'n',
-    pygit2.GIT_STATUS_WT_NEW: b'?',
-    pygit2.GIT_STATUS_WT_RENAMED: b'a',
-    pygit2.GIT_STATUS_WT_TYPECHANGE: b'n',
-    pygit2.GIT_STATUS_WT_UNREADABLE: b'?',
-    pygit2.GIT_STATUS_INDEX_MODIFIED | pygit2.GIT_STATUS_WT_MODIFIED: 'm',
-}
+_STATUS_MAP = {}
+if pygit2:
+    _STATUS_MAP = {
+        pygit2.GIT_STATUS_CONFLICTED: b'm',
+        pygit2.GIT_STATUS_CURRENT: b'n',
+        pygit2.GIT_STATUS_IGNORED: b'?',
+        pygit2.GIT_STATUS_INDEX_DELETED: b'r',
+        pygit2.GIT_STATUS_INDEX_MODIFIED: b'n',
+        pygit2.GIT_STATUS_INDEX_NEW: b'a',
+        pygit2.GIT_STATUS_INDEX_RENAMED: b'a',
+        pygit2.GIT_STATUS_INDEX_TYPECHANGE: b'n',
+        pygit2.GIT_STATUS_WT_DELETED: b'r',
+        pygit2.GIT_STATUS_WT_MODIFIED: b'n',
+        pygit2.GIT_STATUS_WT_NEW: b'?',
+        pygit2.GIT_STATUS_WT_RENAMED: b'a',
+        pygit2.GIT_STATUS_WT_TYPECHANGE: b'n',
+        pygit2.GIT_STATUS_WT_UNREADABLE: b'?',
+        pygit2.GIT_STATUS_INDEX_MODIFIED | pygit2.GIT_STATUS_WT_MODIFIED: 'm',
+    }
 
 
 @interfaceutil.implementer(intdirstate.idirstate)