[PATCH 2 of 6 RFC] scmutil": add altvfs class

Angel Ezquerra angel.ezquerra at gmail.com
Wed Dec 24 06:05:25 CST 2014


# HG changeset patch
# User Angel Ezquerra <angel.ezquerra at gmail.com>
# Date 1413194278 -7200
#      Mon Oct 13 11:57:58 2014 +0200
# Node ID 1664b472cc83f97257c7914c59bfa1600dc64f1a
# Parent  b213f4439d5427cfff6c7fa28927c25e4422a948
scmutil": add altvfs class

The altvfs class is derived from the regular vfs class. It lets you specify an
alternate base path (altbase) that will be used instead of the regular base path
when working with a list of "alternate paths" (altpaths).

This class will be used in follow up patches which will introduce a "full share"
mode for the share extension. It will make it possible to create a vfs object
that accesses the files on the shared repository path except for the limited set
of files that must be accessed from the share target repository.

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -451,6 +451,34 @@
         else:
             return self.base
 
+class altvfs(vfs):
+    """vfs subclass that uses an alternative base path for a set of paths"""
+    def __init__(self, *args, **kwargs):
+        # the altvfs class __init__ method adds two keyword arguments in
+        # addition to the regular vfs class __init__ method:
+        # - altbase: [mandatory] base path of the alternative vfs
+        # - altpaths: [optional] list of relative paths that should be found on
+        #             the alternative base path
+        self.altbase = kwargs.get('altbase', None)
+        if self.altbase is None:
+            util.Abort(_('missing mandatory argument altbase in altvfs init'))
+        self.altpaths = set([util.normpath(p)
+                             for p in kwargs.get('altpaths', [])])
+        # remove the additional arguments before calling the parent's method
+        del kwargs['altbase']
+        del kwargs['altpaths']
+        super(altvfs, self).__init__(*args, **kwargs)
+
+    def _normpath(self, path):
+        if not path:
+            return path
+        return util.normpath(path)
+
+    def join(self, path):
+        if self._normpath(path) in self.altpaths:
+            return os.path.join(self.altbase, path)
+        return super(altvfs, self).join(path)
+
 opener = vfs
 
 class auditvfs(object):


More information about the Mercurial-devel mailing list