2005-08-28 03:28:53 +04:00
|
|
|
# statichttprepo.py - simple http repository class for mercurial
|
|
|
|
#
|
|
|
|
# This provides read-only repo access to repositories exported via static http
|
|
|
|
#
|
2007-06-19 10:51:34 +04:00
|
|
|
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
|
2005-08-28 03:28:53 +04:00
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2005-08-28 03:28:53 +04:00
|
|
|
|
2015-08-09 06:11:08 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import os
|
|
|
|
|
|
|
|
from .i18n import _
|
|
|
|
from . import (
|
|
|
|
byterange,
|
|
|
|
changelog,
|
|
|
|
error,
|
|
|
|
localrepo,
|
|
|
|
manifest,
|
|
|
|
namespaces,
|
|
|
|
scmutil,
|
|
|
|
store,
|
|
|
|
url,
|
|
|
|
util,
|
2017-03-02 16:49:01 +03:00
|
|
|
vfs as vfsmod,
|
2015-08-09 06:11:08 +03:00
|
|
|
)
|
2005-09-23 11:05:16 +04:00
|
|
|
|
2016-04-07 02:22:12 +03:00
|
|
|
urlerr = util.urlerr
|
|
|
|
urlreq = util.urlreq
|
|
|
|
|
2008-10-28 10:26:18 +03:00
|
|
|
class httprangereader(object):
|
|
|
|
def __init__(self, url, opener):
|
|
|
|
# we assume opener has HTTPRangeHandler
|
|
|
|
self.url = url
|
|
|
|
self.pos = 0
|
|
|
|
self.opener = opener
|
2010-04-26 15:13:14 +04:00
|
|
|
self.name = url
|
2016-01-03 02:33:01 +03:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
self.close()
|
|
|
|
|
2008-10-28 10:26:18 +03:00
|
|
|
def seek(self, pos):
|
|
|
|
self.pos = pos
|
|
|
|
def read(self, bytes=None):
|
2016-04-07 02:22:12 +03:00
|
|
|
req = urlreq.request(self.url)
|
2008-10-28 10:26:18 +03:00
|
|
|
end = ''
|
|
|
|
if bytes:
|
|
|
|
end = self.pos + bytes - 1
|
2012-06-10 05:13:24 +04:00
|
|
|
if self.pos or end:
|
|
|
|
req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
|
2008-10-28 10:26:18 +03:00
|
|
|
|
2005-09-23 11:05:16 +04:00
|
|
|
try:
|
2008-10-28 10:26:18 +03:00
|
|
|
f = self.opener.open(req)
|
|
|
|
data = f.read()
|
2015-05-19 05:40:16 +03:00
|
|
|
code = f.code
|
2016-04-07 02:22:12 +03:00
|
|
|
except urlerr.httperror as inst:
|
2008-02-08 13:50:37 +03:00
|
|
|
num = inst.code == 404 and errno.ENOENT or None
|
|
|
|
raise IOError(num, inst)
|
2016-04-07 02:22:12 +03:00
|
|
|
except urlerr.urlerror as inst:
|
2006-03-01 17:29:13 +03:00
|
|
|
raise IOError(None, inst.reason[1])
|
2005-08-28 03:28:53 +04:00
|
|
|
|
2009-05-24 20:31:01 +04:00
|
|
|
if code == 200:
|
|
|
|
# HTTPRangeHandler does nothing if remote does not support
|
|
|
|
# Range headers and returns the full entity. Let's slice it.
|
|
|
|
if bytes:
|
|
|
|
data = data[self.pos:self.pos + bytes]
|
|
|
|
else:
|
|
|
|
data = data[self.pos:]
|
|
|
|
elif bytes:
|
2008-10-28 10:26:18 +03:00
|
|
|
data = data[:bytes]
|
2009-05-24 20:31:01 +04:00
|
|
|
self.pos += len(data)
|
2008-10-28 10:26:18 +03:00
|
|
|
return data
|
2013-11-18 01:31:18 +04:00
|
|
|
def readlines(self):
|
|
|
|
return self.read().splitlines(True)
|
2010-04-26 15:13:14 +04:00
|
|
|
def __iter__(self):
|
2013-11-18 01:31:18 +04:00
|
|
|
return iter(self.readlines())
|
2010-04-26 15:13:14 +04:00
|
|
|
def close(self):
|
|
|
|
pass
|
2008-10-28 10:26:18 +03:00
|
|
|
|
|
|
|
def build_opener(ui, authinfo):
|
|
|
|
# urllib cannot handle URLs with embedded user or passwd
|
|
|
|
urlopener = url.opener(ui, authinfo)
|
|
|
|
urlopener.add_handler(byterange.HTTPRangeHandler())
|
|
|
|
|
2017-03-02 16:49:01 +03:00
|
|
|
class statichttpvfs(vfsmod.abstractvfs):
|
2011-04-30 21:41:25 +04:00
|
|
|
def __init__(self, base):
|
|
|
|
self.base = base
|
|
|
|
|
2014-12-14 21:27:58 +03:00
|
|
|
def __call__(self, path, mode='r', *args, **kw):
|
2011-02-18 03:19:01 +03:00
|
|
|
if mode not in ('r', 'rb'):
|
2010-04-26 15:13:14 +04:00
|
|
|
raise IOError('Permission denied')
|
2016-04-07 02:22:12 +03:00
|
|
|
f = "/".join((self.base, urlreq.quote(path)))
|
2008-10-28 10:26:18 +03:00
|
|
|
return httprangereader(f, urlopener)
|
|
|
|
|
2012-10-08 20:41:55 +04:00
|
|
|
def join(self, path):
|
|
|
|
if path:
|
|
|
|
return os.path.join(self.base, path)
|
|
|
|
else:
|
|
|
|
return self.base
|
|
|
|
|
2012-08-30 21:06:29 +04:00
|
|
|
return statichttpvfs
|
2005-08-28 03:28:53 +04:00
|
|
|
|
2012-07-13 23:47:06 +04:00
|
|
|
class statichttppeer(localrepo.localpeer):
|
|
|
|
def local(self):
|
|
|
|
return None
|
2012-07-13 23:52:28 +04:00
|
|
|
def canpush(self):
|
|
|
|
return False
|
2012-07-13 23:47:06 +04:00
|
|
|
|
2005-08-28 03:28:53 +04:00
|
|
|
class statichttprepository(localrepo.localrepository):
|
localrepo: make supported features manageable in each repositories individually
Before this patch, all localrepositories support same features,
because supported features are managed by the class variable
"supported" of "localrepository".
For example, "largefiles" feature provided by largefiles extension is
recognized as supported, by adding the feature name to "supported" of
"localrepository".
So, commands handling multiple repositories at a time like below
misunderstand that such features are supported also in repositories
not enabling corresponded extensions:
- clone/pull from or push to localhost
- recursive execution in subrepo tree
"reposetup()" can't be used to fix this problem, because it is invoked
after checking whether supported features satisfy ones required in the
target repository.
So, this patch adds the set object named as "featuresetupfuncs" to
"localrepository" to manage hook functions to setup supported features
of each repositories.
If any functions are added to "featuresetupfuncs", they are invoked,
and information about supported features is managed in each
repositories individually.
This patch also adds checking below:
- pull from localhost: whether features supported in the local(= dst)
repository satisfies ones required in the remote(= src)
- push to localhost: whether features supported in the remote(= dst)
repository satisfies ones required in the local(= src)
Managing supported features by the class variable means that there is
no difference of supported features between each instances of
"localrepository" in the same Python process, so such checking is not
needed before this patch.
Even with this patch, if intermediate bundlefile is used as pulling
source, pulling indirectly from the remote repository, which requires
features more than ones supported in the local, can't be prevented,
because bundlefile has no information about "required features" in it.
2013-09-21 16:33:29 +04:00
|
|
|
supported = localrepo.localrepository._basesupported
|
|
|
|
|
2005-08-28 03:28:53 +04:00
|
|
|
def __init__(self, ui, path):
|
2006-07-26 00:50:32 +04:00
|
|
|
self._url = path
|
2005-08-28 03:28:53 +04:00
|
|
|
self.ui = ui
|
2006-12-01 15:34:09 +03:00
|
|
|
|
2010-04-26 15:13:14 +04:00
|
|
|
self.root = path
|
2011-04-30 20:43:20 +04:00
|
|
|
u = util.url(path.rstrip('/') + "/.hg")
|
2011-03-31 07:01:35 +04:00
|
|
|
self.path, authinfo = u.authinfo()
|
2008-10-28 10:26:18 +03:00
|
|
|
|
2017-03-02 05:24:58 +03:00
|
|
|
vfsclass = build_opener(ui, authinfo)
|
|
|
|
self.vfs = vfsclass(self.path)
|
2017-07-16 00:05:04 +03:00
|
|
|
self.cachevfs = vfsclass(self.vfs.join('cache'))
|
2012-01-18 20:23:54 +04:00
|
|
|
self._phasedefaults = []
|
2008-02-08 13:50:37 +03:00
|
|
|
|
2014-12-15 06:11:44 +03:00
|
|
|
self.names = namespaces.namespaces()
|
2017-06-09 09:23:37 +03:00
|
|
|
self.filtername = None
|
2014-12-14 23:53:50 +03:00
|
|
|
|
2006-12-10 02:06:59 +03:00
|
|
|
try:
|
2015-01-16 01:17:12 +03:00
|
|
|
requirements = scmutil.readrequires(self.vfs, self.supported)
|
2015-06-24 08:20:08 +03:00
|
|
|
except IOError as inst:
|
2008-10-19 21:49:56 +04:00
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
2011-05-31 21:16:18 +04:00
|
|
|
requirements = set()
|
|
|
|
|
2008-10-19 21:49:56 +04:00
|
|
|
# check if it is a non-empty old-style repository
|
|
|
|
try:
|
2015-01-16 01:17:12 +03:00
|
|
|
fp = self.vfs("00changelog.i")
|
2010-12-24 17:23:01 +03:00
|
|
|
fp.read(1)
|
|
|
|
fp.close()
|
2015-06-24 08:20:08 +03:00
|
|
|
except IOError as inst:
|
2008-10-19 21:49:56 +04:00
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
# we do not care about empty old-style repositories here
|
2008-02-08 13:50:37 +03:00
|
|
|
msg = _("'%s' does not appear to be an hg repository") % path
|
2009-01-12 19:42:31 +03:00
|
|
|
raise error.RepoError(msg)
|
2006-12-10 02:06:59 +03:00
|
|
|
|
|
|
|
# setup store
|
2017-03-02 05:24:58 +03:00
|
|
|
self.store = store.store(requirements, self.path, vfsclass)
|
2008-08-14 05:18:43 +04:00
|
|
|
self.spath = self.store.path
|
2015-01-11 02:25:54 +03:00
|
|
|
self.svfs = self.store.opener
|
2008-08-14 05:18:43 +04:00
|
|
|
self.sjoin = self.store.join
|
2012-02-15 22:02:35 +04:00
|
|
|
self._filecache = {}
|
2012-07-13 23:47:06 +04:00
|
|
|
self.requirements = requirements
|
2006-12-10 02:06:59 +03:00
|
|
|
|
2016-10-19 03:33:39 +03:00
|
|
|
self.manifestlog = manifest.manifestlog(self.svfs, self)
|
2015-01-11 02:25:54 +03:00
|
|
|
self.changelog = changelog.changelog(self.svfs)
|
2009-07-16 18:39:41 +04:00
|
|
|
self._tags = None
|
2005-08-28 03:28:53 +04:00
|
|
|
self.nodetagscache = None
|
2012-12-24 06:21:15 +04:00
|
|
|
self._branchcaches = {}
|
2015-02-11 06:53:48 +03:00
|
|
|
self._revbranchcache = None
|
2005-12-27 22:09:49 +03:00
|
|
|
self.encodepats = None
|
|
|
|
self.decodepats = None
|
2015-02-11 07:06:12 +03:00
|
|
|
self._transref = None
|
2017-07-02 06:51:19 +03:00
|
|
|
# Cache of types representing filtered repos.
|
|
|
|
self._filteredrepotypes = {}
|
2012-07-13 23:47:06 +04:00
|
|
|
|
|
|
|
def _restrictcapabilities(self, caps):
|
2014-04-07 22:45:50 +04:00
|
|
|
caps = super(statichttprepository, self)._restrictcapabilities(caps)
|
2012-07-13 23:47:06 +04:00
|
|
|
return caps.difference(["pushkey"])
|
2005-08-28 03:28:53 +04:00
|
|
|
|
2006-07-26 00:50:32 +04:00
|
|
|
def url(self):
|
2008-10-23 00:41:32 +04:00
|
|
|
return self._url
|
2006-07-26 00:50:32 +04:00
|
|
|
|
2005-08-28 03:28:53 +04:00
|
|
|
def local(self):
|
|
|
|
return False
|
2006-07-31 18:11:12 +04:00
|
|
|
|
2012-07-13 23:47:06 +04:00
|
|
|
def peer(self):
|
|
|
|
return statichttppeer(self)
|
|
|
|
|
2017-07-29 22:50:56 +03:00
|
|
|
def wlock(self, wait=True):
|
|
|
|
raise error.LockUnavailable(0, _('lock not available'), 'lock',
|
|
|
|
_('cannot lock static-http repository'))
|
|
|
|
|
2008-09-08 13:32:53 +04:00
|
|
|
def lock(self, wait=True):
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_('cannot lock static-http repository'))
|
2008-09-08 13:32:53 +04:00
|
|
|
|
2016-08-05 16:48:09 +03:00
|
|
|
def _writecaches(self):
|
|
|
|
pass # statichttprepository are read only
|
|
|
|
|
2006-07-31 18:11:12 +04:00
|
|
|
def instance(ui, path, create):
|
|
|
|
if create:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_('cannot create new static-http repository'))
|
2007-07-08 11:54:42 +04:00
|
|
|
return statichttprepository(ui, path[7:])
|