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

login
register
mail settings
Submitter Angel Ezquerra
Date Dec. 24, 2014, 12:05 p.m.
Message ID <1664b472cc83f97257c7.1419422725@108.1.168.192.in-addr.arpa>
Download mbox | patch
Permalink /patch/7228/
State Superseded
Headers show

Comments

Angel Ezquerra - Dec. 24, 2014, 12:05 p.m.
# HG changeset patch
# User Angel Ezquerra <angel.ezquerra@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.
Matt Mackall - Dec. 24, 2014, 7:43 p.m.
On Wed, 2014-12-24 at 13:05 +0100, Angel Ezquerra wrote:
> # HG changeset patch
> # User Angel Ezquerra <angel.ezquerra@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 is going to complicate things when we get to the next step in the
VFS roadmap where we choose between two classes to use.

http://mercurial.selenic.com/wiki/WindowsUTF8Plan

So I think we want to instead have something like a wrapper class that
takes two VFS objects and presents a union.

Patch

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):