[PATCH 3 of 3 v2] repos: introduce '-R readonly:PATH' for doing local operations "read only"

Yuya Nishihara yuya at tcha.org
Sun Oct 16 09:54:45 EDT 2016


On Fri, 14 Oct 2016 03:17:30 +0200, Mads Kiilerich wrote:
> # HG changeset patch
> # User Mads Kiilerich <madski at unity3d.com>
> # Date 1476402795 -7200
> #      Fri Oct 14 01:53:15 2016 +0200
> # Node ID 491da143c2f310da732c2e1005e7f8394134a51d
> # Parent  24a7ccfb932b134da24e58817991943c8bbd63fa
> repos: introduce '-R readonly:PATH' for doing local operations "read only"

> The existing "API" for repository types could use some cleanup - it requires
> modules with special undefined duck typing. This patch seems to do what is
> needed.

I don't know which is better, but you can use a class to provide the API.
That's what schemes.py is doing.

> +# created, not thrown yet
> +readonlyexception = IOError(errno.EACCES, _('this is a "readonly" repository'))
> +
> +# this seems to be a necessary part of the repository type API
> +islocal = localrepo.islocal
> +
> +class readonlyvfs(scmutil.vfs):
> +    """A VFS that only can be called with read modes - writing will fail with
> +    an IO error as if the user didn't have write access"""
> +
> +    def __call__(self, path, mode='r', *args, **kw):
> +        if mode not in ('r', 'rb'):
> +            raise readonlyexception
> +        return super(readonlyvfs, self).__call__(path, mode, *args, **kw)
> +
> +class readonlyrepo(localrepo.localrepository):
> +    """A repository that is local but read only, as if the user didn't have
> +    file system write access."""
> +
> +    def __init__(self, baseui, path=None, create=False):
> +        # we know the "scheme" for path is "readonly" but do not want to extend
> +        # the file/bundle hack in the "url" parser - just strip it here
> +        assert path.startswith('readonly:'), path
> +        path = path[len('readonly:'):]
> +
> +        super(readonlyrepo, self).__init__(baseui, path=path, create=False)
> +
> +        assert self.vfs.__class__ is scmutil.vfs
> +        self.vfs.__class__ = readonlyvfs
> +        assert self.wvfs.__class__ is scmutil.vfs
> +        self.wvfs.__class__ = readonlyvfs

Don't we need to wrap svfs as well?

> +    def lock(self, wait=True):
> +        raise readonlyexception
> +
> +    def wlock(self, wait=True):
> +        raise readonlyexception

Some operations (e.g status) "try" to take a lock to update dirstate or cache
files. In which case, LockUnavailable should be raised.


More information about the Mercurial-devel mailing list