cachevfs: use a compatibility later for all access
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 16 Jan 2018 02:21:44 +0100
changeset 3408 f4ea9652661d
parent 3407 b96568036837
child 3409 740e1267be6f
child 3415 fb18c54b4d49
cachevfs: use a compatibility later for all access
hgext3rd/evolve/compat.py
hgext3rd/evolve/depthcache.py
hgext3rd/evolve/firstmergecache.py
hgext3rd/evolve/obscache.py
hgext3rd/evolve/stablesort.py
--- a/hgext3rd/evolve/compat.py	Tue Jan 16 01:11:47 2018 +0100
+++ b/hgext3rd/evolve/compat.py	Tue Jan 16 02:21:44 2018 +0100
@@ -15,6 +15,13 @@
     util,
 )
 
+# hg < 4.2 compat
+try:
+    from mercurial import vfs as vfsmod
+    vfsmod.vfs
+except ImportError:
+    from mercurial import scmutil as vfsmod
+
 try:
     from mercurial import obsutil
     obsutil.closestpredecessors
@@ -212,3 +219,10 @@
                                   isexec='x' in flags,
                                   copied=copied.get(path))
     return mctx
+
+def getcachevfs(repo):
+    cachevfs = getattr(repo, 'cachevfs', None)
+    if cachevfs is None:
+        cachevfs = vfsmod.vfs(repo.vfs.join('cache'))
+        cachevfs.createmode = repo.store.createmode
+    return cachevfs
--- a/hgext3rd/evolve/depthcache.py	Tue Jan 16 01:11:47 2018 +0100
+++ b/hgext3rd/evolve/depthcache.py	Tue Jan 16 02:21:44 2018 +0100
@@ -19,6 +19,7 @@
 )
 
 from . import (
+    compat,
     error,
     exthelper,
     genericcaches,
@@ -197,10 +198,8 @@
         """load data from disk"""
         assert repo.filtername is None
 
-        if util.safehasattr(repo, 'cachevfs'):
-            data = repo.cachevfs.tryread(self._filepath)
-        else:
-            data = repo.vfs.tryread('cache/' + self._filepath)
+        cachevfs = compat.getcachevfs(repo)
+        data = cachevfs.tryread(self._filepath)
         self._data = array.array('l')
         if not data:
             self._cachekey = self.emptykey
@@ -219,10 +218,8 @@
         if self._cachekey is None or self._cachekey == self._ondiskkey:
             return
 
-        if util.safehasattr(repo, 'cachevfs'):
-            cachefile = repo.cachevfs(self._filepath, 'w', atomictemp=True)
-        else:
-            cachefile = repo.vfs('cache/' + self._filepath, 'w', atomictemp=True)
+        cachevfs = compat.getcachevfs(repo)
+        cachefile = cachevfs(self._filepath, 'w', atomictemp=True)
         headerdata = self._serializecachekey()
         cachefile.write(headerdata)
         cachefile.write(self._data.tostring())
--- a/hgext3rd/evolve/firstmergecache.py	Tue Jan 16 01:11:47 2018 +0100
+++ b/hgext3rd/evolve/firstmergecache.py	Tue Jan 16 02:21:44 2018 +0100
@@ -19,6 +19,7 @@
 )
 
 from . import (
+    compat,
     error,
     exthelper,
     genericcaches,
@@ -134,10 +135,8 @@
         """load data from disk"""
         assert repo.filtername is None
 
-        if util.safehasattr(repo, 'cachevfs'):
-            data = repo.cachevfs.tryread(self._filepath)
-        else:
-            data = repo.vfs.tryread('cache/' + self._filepath)
+        cachevfs = compat.getcachevfs(repo)
+        data = cachevfs.tryread(self._filepath)
         self._data = array.array('l')
         if not data:
             self._cachekey = self.emptykey
@@ -156,10 +155,8 @@
         if self._cachekey is None or self._cachekey == self._ondiskkey:
             return
 
-        if util.safehasattr(repo, 'cachevfs'):
-            cachefile = repo.cachevfs(self._filepath, 'w', atomictemp=True)
-        else:
-            cachefile = repo.vfs('cache/' + self._filepath, 'w', atomictemp=True)
+        cachevfs = compat.getcachevfs(repo)
+        cachefile = cachevfs(self._filepath, 'w', atomictemp=True)
         headerdata = self._serializecachekey()
         cachefile.write(headerdata)
         cachefile.write(self._data.tostring())
--- a/hgext3rd/evolve/obscache.py	Tue Jan 16 01:11:47 2018 +0100
+++ b/hgext3rd/evolve/obscache.py	Tue Jan 16 02:21:44 2018 +0100
@@ -27,6 +27,7 @@
 from mercurial.i18n import _
 
 from . import (
+    compat,
     exthelper,
 )
 
@@ -42,12 +43,6 @@
 else:
     timer = time.time
 
-# hg < 4.2 compat
-try:
-    from mercurial import vfs as vfsmod
-    vfsmod.vfs
-except ImportError:
-    from mercurial import scmutil as vfsmod
 
 obsstorefilecache = localrepo.localrepository.obsstore
 
@@ -326,13 +321,6 @@
 
         return reset, revs, markers, (obssize, obskey)
 
-def getcachevfs(repo):
-    cachevfs = getattr(repo, 'cachevfs', None)
-    if cachevfs is None:
-        cachevfs = vfsmod.vfs(repo.vfs.join('cache'))
-        cachevfs.createmode = repo.store.createmode
-    return cachevfs
-
 class obscache(dualsourcecache):
     """cache the "does a rev" is the precursors of some obsmarkers data
 
@@ -375,7 +363,7 @@
     def __init__(self, repo):
         super(obscache, self).__init__()
         self._ondiskkey = None
-        self._vfs = getcachevfs(repo)
+        self._vfs = compat.getcachevfs(repo)
 
     @util.propertycache
     def get(self):
--- a/hgext3rd/evolve/stablesort.py	Tue Jan 16 01:11:47 2018 +0100
+++ b/hgext3rd/evolve/stablesort.py	Tue Jan 16 02:21:44 2018 +0100
@@ -25,6 +25,7 @@
 from mercurial.i18n import _
 
 from . import (
+    compat,
     depthcache,
     exthelper,
     utility,
@@ -611,7 +612,8 @@
         """
         assert repo.filtername is None
 
-        data = repo.cachevfs.tryread(self._filepath)
+        cachevfs = compat.getcachevfs(repo)
+        data = cachevfs.tryread(self._filepath)
         self._index = array.array('l')
         self._data = array.array('l')
         if not data:
@@ -636,7 +638,8 @@
         """
         if self._cachekey is None or self._cachekey == self._ondiskkey:
             return
-        cachefile = repo.cachevfs(self._filepath, 'w', atomictemp=True)
+        cachevfs = compat.getcachevfs(repo)
+        cachefile = cachevfs(self._filepath, 'w', atomictemp=True)
 
         # data to write
         headerdata = self._serializecachekey()