peer: introduce real peer classes
authorPeter Arrenbrecht <peter.arrenbrecht@gmail.com>
Fri, 13 Jul 2012 21:47:06 +0200
changeset 17192 1ac628cd7113
parent 17191 5884812686f7
child 17193 1d710fe5ee0e
peer: introduce real peer classes This change separates peer implementations from the repository implementation. localpeer currently is a simple pass-through to localrepository, except for legacy calls, which have already been removed from localpeer. This ensures that the local client code only uses the most modern peer API when talking to local repos. Peers have a .local() method which returns either None or the underlying localrepository (or descendant thereof). Repos have a .peer() method to return a freshly constructed localpeer. The latter is used by hg.peer(), and also to allow folks to pass either a peer or a repo to some generic helper methods. We might want to get rid of .peer() eventually. The only user of locallegacypeer is debugdiscovery, which uses it to pose as a pre-setdiscovery client. But we decided to leave the old API defined in locallegacypeer for clarity and maybe for other uses in the future. It might be nice to actually define the peer API directly in peer.py as stub methods. One problem there is, however, that localpeer implements lock/addchangegroup, whereas the true remote peers implement unbundle. It might be desireable to get rid of this distinction eventually.
hgext/largefiles/proto.py
hgext/largefiles/uisetup.py
mercurial/commands.py
mercurial/hg.py
mercurial/httppeer.py
mercurial/httprepo.py
mercurial/localrepo.py
mercurial/peer.py
mercurial/repo.py
mercurial/sshpeer.py
mercurial/sshrepo.py
mercurial/statichttprepo.py
mercurial/wireproto.py
tests/notcapable
tests/test-wireproto.py
--- a/hgext/largefiles/proto.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/hgext/largefiles/proto.py	Fri Jul 13 21:47:06 2012 +0200
@@ -6,7 +6,7 @@
 import os
 import urllib2
 
-from mercurial import error, httprepo, util, wireproto
+from mercurial import error, httppeer, util, wireproto
 from mercurial.wireproto import batchable, future
 from mercurial.i18n import _
 
@@ -82,7 +82,7 @@
             # unfortunately, httprepository._callpush tries to convert its
             # input file-like into a bundle before sending it, so we can't use
             # it ...
-            if issubclass(self.__class__, httprepo.httprepository):
+            if issubclass(self.__class__, httppeer.httppeer):
                 res = None
                 try:
                     res = self._call('putlfile', data=fd, sha=sha,
--- a/hgext/largefiles/uisetup.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/hgext/largefiles/uisetup.py	Fri Jul 13 21:47:06 2012 +0200
@@ -9,7 +9,7 @@
 '''setup for largefiles extension: uisetup'''
 
 from mercurial import archival, cmdutil, commands, extensions, filemerge, hg, \
-    httprepo, localrepo, merge, sshrepo, sshserver, wireproto
+    httppeer, localrepo, merge, sshpeer, sshserver, wireproto
 from mercurial.i18n import _
 from mercurial.hgweb import hgweb_mod, protocol, webcommands
 from mercurial.subrepo import hgsubrepo
@@ -143,10 +143,10 @@
 
     # can't do this in reposetup because it needs to have happened before
     # wirerepo.__init__ is called
-    proto.ssholdcallstream = sshrepo.sshrepository._callstream
-    proto.httpoldcallstream = httprepo.httprepository._callstream
-    sshrepo.sshrepository._callstream = proto.sshrepocallstream
-    httprepo.httprepository._callstream = proto.httprepocallstream
+    proto.ssholdcallstream = sshpeer.sshpeer._callstream
+    proto.httpoldcallstream = httppeer.httppeer._callstream
+    sshpeer.sshpeer._callstream = proto.sshrepocallstream
+    httppeer.httppeer._callstream = proto.httprepocallstream
 
     # don't die on seeing a repo with the largefiles requirement
     localrepo.localrepository.supported |= set(['largefiles'])
--- a/mercurial/commands.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/mercurial/commands.py	Fri Jul 13 21:47:06 2012 +0200
@@ -16,7 +16,7 @@
 import merge as mergemod
 import minirst, revset, fileset
 import dagparser, context, simplemerge, graphmod
-import random, setdiscovery, treediscovery, dagutil, pvec
+import random, setdiscovery, treediscovery, dagutil, pvec, localrepo
 import phases, obsolete
 
 table = {}
@@ -1789,6 +1789,9 @@
             if localheads:
                 raise util.Abort('cannot use localheads with old style '
                                  'discovery')
+            if not util.safehasattr(remote, 'branches'):
+                # enable in-client legacy support
+                remote = localrepo.locallegacypeer(remote.local())
             common, _in, hds = treediscovery.findcommonincoming(repo, remote,
                                                                 force=True)
             common = set(common)
--- a/mercurial/hg.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/mercurial/hg.py	Fri Jul 13 21:47:06 2012 +0200
@@ -9,7 +9,7 @@
 from i18n import _
 from lock import release
 from node import hex, nullid
-import localrepo, bundlerepo, httprepo, sshrepo, statichttprepo, bookmarks
+import localrepo, bundlerepo, httppeer, sshpeer, statichttprepo, bookmarks
 import lock, util, extensions, error, node, scmutil
 import cmdutil, discovery
 import merge as mergemod
@@ -65,9 +65,9 @@
 schemes = {
     'bundle': bundlerepo,
     'file': _local,
-    'http': httprepo,
-    'https': httprepo,
-    'ssh': sshrepo,
+    'http': httppeer,
+    'https': httppeer,
+    'ssh': sshpeer,
     'static-http': statichttprepo,
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/httppeer.py	Fri Jul 13 21:47:06 2012 +0200
@@ -0,0 +1,245 @@
+# httppeer.py - HTTP repository proxy classes for mercurial
+#
+# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
+# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from node import nullid
+from i18n import _
+import changegroup, statichttprepo, error, httpconnection, url, util, wireproto
+import os, urllib, urllib2, zlib, httplib
+import errno, socket
+
+def zgenerator(f):
+    zd = zlib.decompressobj()
+    try:
+        for chunk in util.filechunkiter(f):
+            while chunk:
+                yield zd.decompress(chunk, 2**18)
+                chunk = zd.unconsumed_tail
+    except httplib.HTTPException:
+        raise IOError(None, _('connection ended unexpectedly'))
+    yield zd.flush()
+
+class httppeer(wireproto.wirepeer):
+    def __init__(self, ui, path):
+        self.path = path
+        self.caps = None
+        self.handler = None
+        self.urlopener = None
+        u = util.url(path)
+        if u.query or u.fragment:
+            raise util.Abort(_('unsupported URL component: "%s"') %
+                             (u.query or u.fragment))
+
+        # urllib cannot handle URLs with embedded user or passwd
+        self._url, authinfo = u.authinfo()
+
+        self.ui = ui
+        self.ui.debug('using %s\n' % self._url)
+
+        self.urlopener = url.opener(ui, authinfo)
+
+    def __del__(self):
+        if self.urlopener:
+            for h in self.urlopener.handlers:
+                h.close()
+                getattr(h, "close_all", lambda : None)()
+
+    def url(self):
+        return self.path
+
+    # look up capabilities only when needed
+
+    def _fetchcaps(self):
+        self.caps = set(self._call('capabilities').split())
+
+    def _capabilities(self):
+        if self.caps is None:
+            try:
+                self._fetchcaps()
+            except error.RepoError:
+                self.caps = set()
+            self.ui.debug('capabilities: %s\n' %
+                          (' '.join(self.caps or ['none'])))
+        return self.caps
+
+    def lock(self):
+        raise util.Abort(_('operation not supported over http'))
+
+    def _callstream(self, cmd, **args):
+        if cmd == 'pushkey':
+            args['data'] = ''
+        data = args.pop('data', None)
+        size = 0
+        if util.safehasattr(data, 'length'):
+            size = data.length
+        elif data is not None:
+            size = len(data)
+        headers = args.pop('headers', {})
+
+        if size and self.ui.configbool('ui', 'usehttp2', False):
+            headers['Expect'] = '100-Continue'
+            headers['X-HgHttp2'] = '1'
+
+        self.ui.debug("sending %s command\n" % cmd)
+        q = [('cmd', cmd)]
+        headersize = 0
+        if len(args) > 0:
+            httpheader = self.capable('httpheader')
+            if httpheader:
+                headersize = int(httpheader.split(',')[0])
+        if headersize > 0:
+            # The headers can typically carry more data than the URL.
+            encargs = urllib.urlencode(sorted(args.items()))
+            headerfmt = 'X-HgArg-%s'
+            contentlen = headersize - len(headerfmt % '000' + ': \r\n')
+            headernum = 0
+            for i in xrange(0, len(encargs), contentlen):
+                headernum += 1
+                header = headerfmt % str(headernum)
+                headers[header] = encargs[i:i + contentlen]
+            varyheaders = [headerfmt % str(h) for h in range(1, headernum + 1)]
+            headers['Vary'] = ','.join(varyheaders)
+        else:
+            q += sorted(args.items())
+        qs = '?%s' % urllib.urlencode(q)
+        cu = "%s%s" % (self._url, qs)
+        req = urllib2.Request(cu, data, headers)
+        if data is not None:
+            self.ui.debug("sending %s bytes\n" % size)
+            req.add_unredirected_header('Content-Length', '%d' % size)
+        try:
+            resp = self.urlopener.open(req)
+        except urllib2.HTTPError, inst:
+            if inst.code == 401:
+                raise util.Abort(_('authorization failed'))
+            raise
+        except httplib.HTTPException, inst:
+            self.ui.debug('http error while sending %s command\n' % cmd)
+            self.ui.traceback()
+            raise IOError(None, inst)
+        except IndexError:
+            # this only happens with Python 2.3, later versions raise URLError
+            raise util.Abort(_('http error, possibly caused by proxy setting'))
+        # record the url we got redirected to
+        resp_url = resp.geturl()
+        if resp_url.endswith(qs):
+            resp_url = resp_url[:-len(qs)]
+        if self._url.rstrip('/') != resp_url.rstrip('/'):
+            if not self.ui.quiet:
+                self.ui.warn(_('real URL is %s\n') % resp_url)
+        self._url = resp_url
+        try:
+            proto = resp.getheader('content-type')
+        except AttributeError:
+            proto = resp.headers.get('content-type', '')
+
+        safeurl = util.hidepassword(self._url)
+        if proto.startswith('application/hg-error'):
+            raise error.OutOfBandError(resp.read())
+        # accept old "text/plain" and "application/hg-changegroup" for now
+        if not (proto.startswith('application/mercurial-') or
+                proto.startswith('text/plain') or
+                proto.startswith('application/hg-changegroup')):
+            self.ui.debug("requested URL: '%s'\n" % util.hidepassword(cu))
+            raise error.RepoError(
+                _("'%s' does not appear to be an hg repository:\n"
+                  "---%%<--- (%s)\n%s\n---%%<---\n")
+                % (safeurl, proto or 'no content-type', resp.read()))
+
+        if proto.startswith('application/mercurial-'):
+            try:
+                version = proto.split('-', 1)[1]
+                version_info = tuple([int(n) for n in version.split('.')])
+            except ValueError:
+                raise error.RepoError(_("'%s' sent a broken Content-Type "
+                                        "header (%s)") % (safeurl, proto))
+            if version_info > (0, 1):
+                raise error.RepoError(_("'%s' uses newer protocol %s") %
+                                      (safeurl, version))
+
+        return resp
+
+    def _call(self, cmd, **args):
+        fp = self._callstream(cmd, **args)
+        try:
+            return fp.read()
+        finally:
+            # if using keepalive, allow connection to be reused
+            fp.close()
+
+    def _callpush(self, cmd, cg, **args):
+        # have to stream bundle to a temp file because we do not have
+        # http 1.1 chunked transfer.
+
+        types = self.capable('unbundle')
+        try:
+            types = types.split(',')
+        except AttributeError:
+            # servers older than d1b16a746db6 will send 'unbundle' as a
+            # boolean capability. They only support headerless/uncompressed
+            # bundles.
+            types = [""]
+        for x in types:
+            if x in changegroup.bundletypes:
+                type = x
+                break
+
+        tempname = changegroup.writebundle(cg, None, type)
+        fp = httpconnection.httpsendfile(self.ui, tempname, "rb")
+        headers = {'Content-Type': 'application/mercurial-0.1'}
+
+        try:
+            try:
+                r = self._call(cmd, data=fp, headers=headers, **args)
+                vals = r.split('\n', 1)
+                if len(vals) < 2:
+                    raise error.ResponseError(_("unexpected response:"), r)
+                return vals
+            except socket.error, err:
+                if err.args[0] in (errno.ECONNRESET, errno.EPIPE):
+                    raise util.Abort(_('push failed: %s') % err.args[1])
+                raise util.Abort(err.args[1])
+        finally:
+            fp.close()
+            os.unlink(tempname)
+
+    def _abort(self, exception):
+        raise exception
+
+    def _decompress(self, stream):
+        return util.chunkbuffer(zgenerator(stream))
+
+class httpspeer(httppeer):
+    def __init__(self, ui, path):
+        if not url.has_https:
+            raise util.Abort(_('Python support for SSL and HTTPS '
+                               'is not installed'))
+        httppeer.__init__(self, ui, path)
+
+def instance(ui, path, create):
+    if create:
+        raise util.Abort(_('cannot create new http repository'))
+    try:
+        if path.startswith('https:'):
+            inst = httpspeer(ui, path)
+        else:
+            inst = httppeer(ui, path)
+        try:
+            # Try to do useful work when checking compatibility.
+            # Usually saves a roundtrip since we want the caps anyway.
+            inst._fetchcaps()
+        except error.RepoError:
+            # No luck, try older compatibility check.
+            inst.between([(nullid, nullid)])
+        return inst
+    except error.RepoError, httpexception:
+        try:
+            r = statichttprepo.instance(ui, "static-" + path, create)
+            ui.note('(falling back to static-http)\n')
+            return r
+        except error.RepoError:
+            raise httpexception # use the original http RepoError instead
--- a/mercurial/httprepo.py	Fri Jul 13 21:46:53 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,247 +0,0 @@
-# httprepo.py - HTTP repository proxy classes for mercurial
-#
-# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
-# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
-#
-# This software may be used and distributed according to the terms of the
-# GNU General Public License version 2 or any later version.
-
-from node import nullid
-from i18n import _
-import changegroup, statichttprepo, error, httpconnection, url, util, wireproto
-import os, urllib, urllib2, zlib, httplib
-import errno, socket
-
-def zgenerator(f):
-    zd = zlib.decompressobj()
-    try:
-        for chunk in util.filechunkiter(f):
-            while chunk:
-                yield zd.decompress(chunk, 2**18)
-                chunk = zd.unconsumed_tail
-    except httplib.HTTPException:
-        raise IOError(None, _('connection ended unexpectedly'))
-    yield zd.flush()
-
-class httprepository(wireproto.wirerepository):
-    def __init__(self, ui, path):
-        self.path = path
-        self.caps = None
-        self.handler = None
-        self.urlopener = None
-        u = util.url(path)
-        if u.query or u.fragment:
-            raise util.Abort(_('unsupported URL component: "%s"') %
-                             (u.query or u.fragment))
-
-        # urllib cannot handle URLs with embedded user or passwd
-        self._url, authinfo = u.authinfo()
-
-        self.ui = ui
-        self.ui.debug('using %s\n' % self._url)
-
-        self.urlopener = url.opener(ui, authinfo)
-
-    def __del__(self):
-        if self.urlopener:
-            for h in self.urlopener.handlers:
-                h.close()
-                getattr(h, "close_all", lambda : None)()
-
-    def url(self):
-        return self.path
-
-    # look up capabilities only when needed
-
-    def _fetchcaps(self):
-        self.caps = set(self._call('capabilities').split())
-
-    def get_caps(self):
-        if self.caps is None:
-            try:
-                self._fetchcaps()
-            except error.RepoError:
-                self.caps = set()
-            self.ui.debug('capabilities: %s\n' %
-                          (' '.join(self.caps or ['none'])))
-        return self.caps
-
-    capabilities = property(get_caps)
-
-    def lock(self):
-        raise util.Abort(_('operation not supported over http'))
-
-    def _callstream(self, cmd, **args):
-        if cmd == 'pushkey':
-            args['data'] = ''
-        data = args.pop('data', None)
-        size = 0
-        if util.safehasattr(data, 'length'):
-            size = data.length
-        elif data is not None:
-            size = len(data)
-        headers = args.pop('headers', {})
-
-        if size and self.ui.configbool('ui', 'usehttp2', False):
-            headers['Expect'] = '100-Continue'
-            headers['X-HgHttp2'] = '1'
-
-        self.ui.debug("sending %s command\n" % cmd)
-        q = [('cmd', cmd)]
-        headersize = 0
-        if len(args) > 0:
-            httpheader = self.capable('httpheader')
-            if httpheader:
-                headersize = int(httpheader.split(',')[0])
-        if headersize > 0:
-            # The headers can typically carry more data than the URL.
-            encargs = urllib.urlencode(sorted(args.items()))
-            headerfmt = 'X-HgArg-%s'
-            contentlen = headersize - len(headerfmt % '000' + ': \r\n')
-            headernum = 0
-            for i in xrange(0, len(encargs), contentlen):
-                headernum += 1
-                header = headerfmt % str(headernum)
-                headers[header] = encargs[i:i + contentlen]
-            varyheaders = [headerfmt % str(h) for h in range(1, headernum + 1)]
-            headers['Vary'] = ','.join(varyheaders)
-        else:
-            q += sorted(args.items())
-        qs = '?%s' % urllib.urlencode(q)
-        cu = "%s%s" % (self._url, qs)
-        req = urllib2.Request(cu, data, headers)
-        if data is not None:
-            self.ui.debug("sending %s bytes\n" % size)
-            req.add_unredirected_header('Content-Length', '%d' % size)
-        try:
-            resp = self.urlopener.open(req)
-        except urllib2.HTTPError, inst:
-            if inst.code == 401:
-                raise util.Abort(_('authorization failed'))
-            raise
-        except httplib.HTTPException, inst:
-            self.ui.debug('http error while sending %s command\n' % cmd)
-            self.ui.traceback()
-            raise IOError(None, inst)
-        except IndexError:
-            # this only happens with Python 2.3, later versions raise URLError
-            raise util.Abort(_('http error, possibly caused by proxy setting'))
-        # record the url we got redirected to
-        resp_url = resp.geturl()
-        if resp_url.endswith(qs):
-            resp_url = resp_url[:-len(qs)]
-        if self._url.rstrip('/') != resp_url.rstrip('/'):
-            if not self.ui.quiet:
-                self.ui.warn(_('real URL is %s\n') % resp_url)
-        self._url = resp_url
-        try:
-            proto = resp.getheader('content-type')
-        except AttributeError:
-            proto = resp.headers.get('content-type', '')
-
-        safeurl = util.hidepassword(self._url)
-        if proto.startswith('application/hg-error'):
-            raise error.OutOfBandError(resp.read())
-        # accept old "text/plain" and "application/hg-changegroup" for now
-        if not (proto.startswith('application/mercurial-') or
-                proto.startswith('text/plain') or
-                proto.startswith('application/hg-changegroup')):
-            self.ui.debug("requested URL: '%s'\n" % util.hidepassword(cu))
-            raise error.RepoError(
-                _("'%s' does not appear to be an hg repository:\n"
-                  "---%%<--- (%s)\n%s\n---%%<---\n")
-                % (safeurl, proto or 'no content-type', resp.read()))
-
-        if proto.startswith('application/mercurial-'):
-            try:
-                version = proto.split('-', 1)[1]
-                version_info = tuple([int(n) for n in version.split('.')])
-            except ValueError:
-                raise error.RepoError(_("'%s' sent a broken Content-Type "
-                                        "header (%s)") % (safeurl, proto))
-            if version_info > (0, 1):
-                raise error.RepoError(_("'%s' uses newer protocol %s") %
-                                      (safeurl, version))
-
-        return resp
-
-    def _call(self, cmd, **args):
-        fp = self._callstream(cmd, **args)
-        try:
-            return fp.read()
-        finally:
-            # if using keepalive, allow connection to be reused
-            fp.close()
-
-    def _callpush(self, cmd, cg, **args):
-        # have to stream bundle to a temp file because we do not have
-        # http 1.1 chunked transfer.
-
-        types = self.capable('unbundle')
-        try:
-            types = types.split(',')
-        except AttributeError:
-            # servers older than d1b16a746db6 will send 'unbundle' as a
-            # boolean capability. They only support headerless/uncompressed
-            # bundles.
-            types = [""]
-        for x in types:
-            if x in changegroup.bundletypes:
-                type = x
-                break
-
-        tempname = changegroup.writebundle(cg, None, type)
-        fp = httpconnection.httpsendfile(self.ui, tempname, "rb")
-        headers = {'Content-Type': 'application/mercurial-0.1'}
-
-        try:
-            try:
-                r = self._call(cmd, data=fp, headers=headers, **args)
-                vals = r.split('\n', 1)
-                if len(vals) < 2:
-                    raise error.ResponseError(_("unexpected response:"), r)
-                return vals
-            except socket.error, err:
-                if err.args[0] in (errno.ECONNRESET, errno.EPIPE):
-                    raise util.Abort(_('push failed: %s') % err.args[1])
-                raise util.Abort(err.args[1])
-        finally:
-            fp.close()
-            os.unlink(tempname)
-
-    def _abort(self, exception):
-        raise exception
-
-    def _decompress(self, stream):
-        return util.chunkbuffer(zgenerator(stream))
-
-class httpsrepository(httprepository):
-    def __init__(self, ui, path):
-        if not url.has_https:
-            raise util.Abort(_('Python support for SSL and HTTPS '
-                               'is not installed'))
-        httprepository.__init__(self, ui, path)
-
-def instance(ui, path, create):
-    if create:
-        raise util.Abort(_('cannot create new http repository'))
-    try:
-        if path.startswith('https:'):
-            inst = httpsrepository(ui, path)
-        else:
-            inst = httprepository(ui, path)
-        try:
-            # Try to do useful work when checking compatibility.
-            # Usually saves a roundtrip since we want the caps anyway.
-            inst._fetchcaps()
-        except error.RepoError:
-            # No luck, try older compatibility check.
-            inst.between([(nullid, nullid)])
-        return inst
-    except error.RepoError, httpexception:
-        try:
-            r = statichttprepo.instance(ui, "static-" + path, create)
-            ui.note('(falling back to static-http)\n')
-            return r
-        except error.RepoError:
-            raise httpexception # use the original http RepoError instead
--- a/mercurial/localrepo.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/mercurial/localrepo.py	Fri Jul 13 21:47:06 2012 +0200
@@ -6,7 +6,7 @@
 # GNU General Public License version 2 or any later version.
 from node import bin, hex, nullid, nullrev, short
 from i18n import _
-import repo, changegroup, subrepo, discovery, pushkey, obsolete
+import peer, changegroup, subrepo, discovery, pushkey, obsolete
 import changelog, dirstate, filelog, manifest, context, bookmarks, phases
 import lock, transaction, store, encoding, base85
 import scmutil, util, extensions, hook, error, revset
@@ -23,9 +23,90 @@
     def join(self, obj, fname):
         return obj.sjoin(fname)
 
-class localrepository(repo.repository):
-    capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey',
-                        'known', 'getbundle'))
+MODERNCAPS = set(('lookup', 'branchmap', 'pushkey', 'known', 'getbundle'))
+LEGACYCAPS = MODERNCAPS.union(set(['changegroupsubset']))
+
+class localpeer(peer.peerrepository):
+    '''peer for a local repo; reflects only the most recent API'''
+
+    def __init__(self, repo, caps=MODERNCAPS):
+        peer.peerrepository.__init__(self)
+        self._repo = repo
+        self.ui = repo.ui
+        self._caps = repo._restrictcapabilities(caps)
+        self.requirements = repo.requirements
+        self.supportedformats = repo.supportedformats
+
+    def close(self):
+        self._repo.close()
+
+    def _capabilities(self):
+        return self._caps
+
+    def local(self):
+        return self._repo
+
+    def cancopy(self):
+        return self._repo.cancopy() # so bundlerepo can override
+
+    def url(self):
+        return self._repo.url()
+
+    def lookup(self, key):
+        return self._repo.lookup(key)
+
+    def branchmap(self):
+        return self._repo.branchmap()
+
+    def heads(self):
+        return self._repo.heads()
+
+    def known(self, nodes):
+        return self._repo.known(nodes)
+
+    def getbundle(self, source, heads=None, common=None):
+        return self._repo.getbundle(source, heads=heads, common=common)
+
+    # TODO We might want to move the next two calls into legacypeer and add
+    # unbundle instead.
+
+    def lock(self):
+        return self._repo.lock()
+
+    def addchangegroup(self, cg, source, url):
+        return self._repo.addchangegroup(cg, source, url)
+
+    def pushkey(self, namespace, key, old, new):
+        return self._repo.pushkey(namespace, key, old, new)
+
+    def listkeys(self, namespace):
+        return self._repo.listkeys(namespace)
+
+    def debugwireargs(self, one, two, three=None, four=None, five=None):
+        '''used to test argument passing over the wire'''
+        return "%s %s %s %s %s" % (one, two, three, four, five)
+
+class locallegacypeer(localpeer):
+    '''peer extension which implements legacy methods too; used for tests with
+    restricted capabilities'''
+
+    def __init__(self, repo):
+        localpeer.__init__(self, repo, caps=LEGACYCAPS)
+
+    def branches(self, nodes):
+        return self._repo.branches(nodes)
+
+    def between(self, pairs):
+        return self._repo.between(pairs)
+
+    def changegroup(self, basenodes, source):
+        return self._repo.changegroup(basenodes, source)
+
+    def changegroupsubset(self, bases, heads, source):
+        return self._repo.changegroupsubset(bases, heads, source)
+
+class localrepository(object):
+
     supportedformats = set(('revlogv1', 'generaldelta'))
     supported = supportedformats | set(('store', 'fncache', 'shared',
                                         'dotencode'))
@@ -36,7 +117,6 @@
         return self.requirements[:]
 
     def __init__(self, baseui, path=None, create=False):
-        repo.repository.__init__(self)
         self.wopener = scmutil.opener(path, expand=True)
         self.wvfs = self.wopener
         self.root = self.wvfs.base
@@ -126,6 +206,12 @@
         # Maps a property name to its util.filecacheentry
         self._filecache = {}
 
+    def close(self):
+        pass
+
+    def _restrictcapabilities(self, caps):
+        return caps
+
     def _applyrequirements(self, requirements):
         self.requirements = requirements
         self.sopener.options = dict((r, 1) for r in requirements
@@ -175,6 +261,9 @@
                 parts.pop()
         return False
 
+    def peer(self):
+        return localpeer(self) # not cached to avoid reference cycle
+
     @filecache('bookmarks')
     def _bookmarks(self):
         return bookmarks.read(self)
@@ -668,6 +757,9 @@
     def local(self):
         return self
 
+    def cancopy(self):
+        return self.local() # so statichttprepo's override of local() works
+
     def join(self, f):
         return os.path.join(self.path, f)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/peer.py	Fri Jul 13 21:47:06 2012 +0200
@@ -0,0 +1,49 @@
+# peer.py - repository base classes for mercurial
+#
+# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
+# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from i18n import _
+import error
+
+class peerrepository(object):
+
+    def capable(self, name):
+        '''tell whether repo supports named capability.
+        return False if not supported.
+        if boolean capability, return True.
+        if string capability, return string.'''
+        caps = self._capabilities()
+        if name in caps:
+            return True
+        name_eq = name + '='
+        for cap in caps:
+            if cap.startswith(name_eq):
+                return cap[len(name_eq):]
+        return False
+
+    def requirecap(self, name, purpose):
+        '''raise an exception if the given capability is not present'''
+        if not self.capable(name):
+            raise error.CapabilityError(
+                _('cannot %s; remote repository does not '
+                  'support the %r capability') % (purpose, name))
+
+    def local(self):
+        '''return peer as a localrepo, or None'''
+        return None
+
+    def peer(self):
+        return self
+
+    def peer(self):
+        return self
+
+    def cancopy(self):
+        return False
+
+    def close(self):
+        pass
--- a/mercurial/repo.py	Fri Jul 13 21:46:53 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-# repo.py - repository base classes for mercurial
-#
-# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
-# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
-#
-# This software may be used and distributed according to the terms of the
-# GNU General Public License version 2 or any later version.
-
-from i18n import _
-import error
-
-class repository(object):
-    def capable(self, name):
-        '''tell whether repo supports named capability.
-        return False if not supported.
-        if boolean capability, return True.
-        if string capability, return string.'''
-        if name in self.capabilities:
-            return True
-        name_eq = name + '='
-        for cap in self.capabilities:
-            if cap.startswith(name_eq):
-                return cap[len(name_eq):]
-        return False
-
-    def requirecap(self, name, purpose):
-        '''raise an exception if the given capability is not present'''
-        if not self.capable(name):
-            raise error.CapabilityError(
-                _('cannot %s; remote repository does not '
-                  'support the %r capability') % (purpose, name))
-
-    def local(self):
-        return False
-
-    def peer(self):
-        return self
-
-    def cancopy(self):
-        return self.local()
-
-    def close(self):
-        pass
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/sshpeer.py	Fri Jul 13 21:47:06 2012 +0200
@@ -0,0 +1,239 @@
+# sshpeer.py - ssh repository proxy class for mercurial
+#
+# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+import re
+from i18n import _
+import util, error, wireproto
+
+class remotelock(object):
+    def __init__(self, repo):
+        self.repo = repo
+    def release(self):
+        self.repo.unlock()
+        self.repo = None
+    def __del__(self):
+        if self.repo:
+            self.release()
+
+def _serverquote(s):
+    '''quote a string for the remote shell ... which we assume is sh'''
+    if re.match('[a-zA-Z0-9@%_+=:,./-]*$', s):
+        return s
+    return "'%s'" % s.replace("'", "'\\''")
+
+class sshpeer(wireproto.wirepeer):
+    def __init__(self, ui, path, create=False):
+        self._url = path
+        self.ui = ui
+        self.pipeo = self.pipei = self.pipee = None
+
+        u = util.url(path, parsequery=False, parsefragment=False)
+        if u.scheme != 'ssh' or not u.host or u.path is None:
+            self._abort(error.RepoError(_("couldn't parse location %s") % path))
+
+        self.user = u.user
+        if u.passwd is not None:
+            self._abort(error.RepoError(_("password in URL not supported")))
+        self.host = u.host
+        self.port = u.port
+        self.path = u.path or "."
+
+        sshcmd = self.ui.config("ui", "ssh", "ssh")
+        remotecmd = self.ui.config("ui", "remotecmd", "hg")
+
+        args = util.sshargs(sshcmd, self.host, self.user, self.port)
+
+        if create:
+            cmd = '%s %s %s' % (sshcmd, args,
+                util.shellquote("%s init %s" %
+                    (_serverquote(remotecmd), _serverquote(self.path))))
+            ui.note(_('running %s\n') % cmd)
+            res = util.system(cmd)
+            if res != 0:
+                self._abort(error.RepoError(_("could not create remote repo")))
+
+        self.validate_repo(ui, sshcmd, args, remotecmd)
+
+    def url(self):
+        return self._url
+
+    def validate_repo(self, ui, sshcmd, args, remotecmd):
+        # cleanup up previous run
+        self.cleanup()
+
+        cmd = '%s %s %s' % (sshcmd, args,
+            util.shellquote("%s -R %s serve --stdio" %
+                (_serverquote(remotecmd), _serverquote(self.path))))
+        ui.note(_('running %s\n') % cmd)
+        cmd = util.quotecommand(cmd)
+        self.pipeo, self.pipei, self.pipee = util.popen3(cmd)
+
+        # skip any noise generated by remote shell
+        self._callstream("hello")
+        r = self._callstream("between", pairs=("%s-%s" % ("0"*40, "0"*40)))
+        lines = ["", "dummy"]
+        max_noise = 500
+        while lines[-1] and max_noise:
+            l = r.readline()
+            self.readerr()
+            if lines[-1] == "1\n" and l == "\n":
+                break
+            if l:
+                ui.debug("remote: ", l)
+            lines.append(l)
+            max_noise -= 1
+        else:
+            self._abort(error.RepoError(_('no suitable response from '
+                                          'remote hg')))
+
+        self._caps = set()
+        for l in reversed(lines):
+            if l.startswith("capabilities:"):
+                self._caps.update(l[:-1].split(":")[1].split())
+                break
+
+    def _capabilities(self):
+        return self._caps
+
+    def readerr(self):
+        while True:
+            size = util.fstat(self.pipee).st_size
+            if size == 0:
+                break
+            s = self.pipee.read(size)
+            if not s:
+                break
+            for l in s.splitlines():
+                self.ui.status(_("remote: "), l, '\n')
+
+    def _abort(self, exception):
+        self.cleanup()
+        raise exception
+
+    def cleanup(self):
+        if self.pipeo is None:
+            return
+        self.pipeo.close()
+        self.pipei.close()
+        try:
+            # read the error descriptor until EOF
+            for l in self.pipee:
+                self.ui.status(_("remote: "), l)
+        except (IOError, ValueError):
+            pass
+        self.pipee.close()
+
+    __del__ = cleanup
+
+    def _callstream(self, cmd, **args):
+        self.ui.debug("sending %s command\n" % cmd)
+        self.pipeo.write("%s\n" % cmd)
+        _func, names = wireproto.commands[cmd]
+        keys = names.split()
+        wireargs = {}
+        for k in keys:
+            if k == '*':
+                wireargs['*'] = args
+                break
+            else:
+                wireargs[k] = args[k]
+                del args[k]
+        for k, v in sorted(wireargs.iteritems()):
+            self.pipeo.write("%s %d\n" % (k, len(v)))
+            if isinstance(v, dict):
+                for dk, dv in v.iteritems():
+                    self.pipeo.write("%s %d\n" % (dk, len(dv)))
+                    self.pipeo.write(dv)
+            else:
+                self.pipeo.write(v)
+        self.pipeo.flush()
+
+        return self.pipei
+
+    def _call(self, cmd, **args):
+        self._callstream(cmd, **args)
+        return self._recv()
+
+    def _callpush(self, cmd, fp, **args):
+        r = self._call(cmd, **args)
+        if r:
+            return '', r
+        while True:
+            d = fp.read(4096)
+            if not d:
+                break
+            self._send(d)
+        self._send("", flush=True)
+        r = self._recv()
+        if r:
+            return '', r
+        return self._recv(), ''
+
+    def _decompress(self, stream):
+        return stream
+
+    def _recv(self):
+        l = self.pipei.readline()
+        if l == '\n':
+            err = []
+            while True:
+                line = self.pipee.readline()
+                if line == '-\n':
+                    break
+                err.extend([line])
+            if len(err) > 0:
+                # strip the trailing newline added to the last line server-side
+                err[-1] = err[-1][:-1]
+            self._abort(error.OutOfBandError(*err))
+        self.readerr()
+        try:
+            l = int(l)
+        except ValueError:
+            self._abort(error.ResponseError(_("unexpected response:"), l))
+        return self.pipei.read(l)
+
+    def _send(self, data, flush=False):
+        self.pipeo.write("%d\n" % len(data))
+        if data:
+            self.pipeo.write(data)
+        if flush:
+            self.pipeo.flush()
+        self.readerr()
+
+    def lock(self):
+        self._call("lock")
+        return remotelock(self)
+
+    def unlock(self):
+        self._call("unlock")
+
+    def addchangegroup(self, cg, source, url, lock=None):
+        '''Send a changegroup to the remote server.  Return an integer
+        similar to unbundle(). DEPRECATED, since it requires locking the
+        remote.'''
+        d = self._call("addchangegroup")
+        if d:
+            self._abort(error.RepoError(_("push refused: %s") % d))
+        while True:
+            d = cg.read(4096)
+            if not d:
+                break
+            self.pipeo.write(d)
+            self.readerr()
+
+        self.pipeo.flush()
+
+        self.readerr()
+        r = self._recv()
+        if not r:
+            return 1
+        try:
+            return int(r)
+        except ValueError:
+            self._abort(error.ResponseError(_("unexpected response:"), r))
+
+instance = sshpeer
--- a/mercurial/sshrepo.py	Fri Jul 13 21:46:53 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,236 +0,0 @@
-# sshrepo.py - ssh repository proxy class for mercurial
-#
-# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
-#
-# This software may be used and distributed according to the terms of the
-# GNU General Public License version 2 or any later version.
-
-import re
-from i18n import _
-import util, error, wireproto
-
-class remotelock(object):
-    def __init__(self, repo):
-        self.repo = repo
-    def release(self):
-        self.repo.unlock()
-        self.repo = None
-    def __del__(self):
-        if self.repo:
-            self.release()
-
-def _serverquote(s):
-    '''quote a string for the remote shell ... which we assume is sh'''
-    if re.match('[a-zA-Z0-9@%_+=:,./-]*$', s):
-        return s
-    return "'%s'" % s.replace("'", "'\\''")
-
-class sshrepository(wireproto.wirerepository):
-    def __init__(self, ui, path, create=False):
-        self._url = path
-        self.ui = ui
-        self.pipeo = self.pipei = self.pipee = None
-
-        u = util.url(path, parsequery=False, parsefragment=False)
-        if u.scheme != 'ssh' or not u.host or u.path is None:
-            self._abort(error.RepoError(_("couldn't parse location %s") % path))
-
-        self.user = u.user
-        if u.passwd is not None:
-            self._abort(error.RepoError(_("password in URL not supported")))
-        self.host = u.host
-        self.port = u.port
-        self.path = u.path or "."
-
-        sshcmd = self.ui.config("ui", "ssh", "ssh")
-        remotecmd = self.ui.config("ui", "remotecmd", "hg")
-
-        args = util.sshargs(sshcmd, self.host, self.user, self.port)
-
-        if create:
-            cmd = '%s %s %s' % (sshcmd, args,
-                util.shellquote("%s init %s" %
-                    (_serverquote(remotecmd), _serverquote(self.path))))
-            ui.note(_('running %s\n') % cmd)
-            res = util.system(cmd)
-            if res != 0:
-                self._abort(error.RepoError(_("could not create remote repo")))
-
-        self.validate_repo(ui, sshcmd, args, remotecmd)
-
-    def url(self):
-        return self._url
-
-    def validate_repo(self, ui, sshcmd, args, remotecmd):
-        # cleanup up previous run
-        self.cleanup()
-
-        cmd = '%s %s %s' % (sshcmd, args,
-            util.shellquote("%s -R %s serve --stdio" %
-                (_serverquote(remotecmd), _serverquote(self.path))))
-        ui.note(_('running %s\n') % cmd)
-        cmd = util.quotecommand(cmd)
-        self.pipeo, self.pipei, self.pipee = util.popen3(cmd)
-
-        # skip any noise generated by remote shell
-        self._callstream("hello")
-        r = self._callstream("between", pairs=("%s-%s" % ("0"*40, "0"*40)))
-        lines = ["", "dummy"]
-        max_noise = 500
-        while lines[-1] and max_noise:
-            l = r.readline()
-            self.readerr()
-            if lines[-1] == "1\n" and l == "\n":
-                break
-            if l:
-                ui.debug("remote: ", l)
-            lines.append(l)
-            max_noise -= 1
-        else:
-            self._abort(error.RepoError(_('no suitable response from '
-                                          'remote hg')))
-
-        self.capabilities = set()
-        for l in reversed(lines):
-            if l.startswith("capabilities:"):
-                self.capabilities.update(l[:-1].split(":")[1].split())
-                break
-
-    def readerr(self):
-        while True:
-            size = util.fstat(self.pipee).st_size
-            if size == 0:
-                break
-            s = self.pipee.read(size)
-            if not s:
-                break
-            for l in s.splitlines():
-                self.ui.status(_("remote: "), l, '\n')
-
-    def _abort(self, exception):
-        self.cleanup()
-        raise exception
-
-    def cleanup(self):
-        if self.pipeo is None:
-            return
-        self.pipeo.close()
-        self.pipei.close()
-        try:
-            # read the error descriptor until EOF
-            for l in self.pipee:
-                self.ui.status(_("remote: "), l)
-        except (IOError, ValueError):
-            pass
-        self.pipee.close()
-
-    __del__ = cleanup
-
-    def _callstream(self, cmd, **args):
-        self.ui.debug("sending %s command\n" % cmd)
-        self.pipeo.write("%s\n" % cmd)
-        _func, names = wireproto.commands[cmd]
-        keys = names.split()
-        wireargs = {}
-        for k in keys:
-            if k == '*':
-                wireargs['*'] = args
-                break
-            else:
-                wireargs[k] = args[k]
-                del args[k]
-        for k, v in sorted(wireargs.iteritems()):
-            self.pipeo.write("%s %d\n" % (k, len(v)))
-            if isinstance(v, dict):
-                for dk, dv in v.iteritems():
-                    self.pipeo.write("%s %d\n" % (dk, len(dv)))
-                    self.pipeo.write(dv)
-            else:
-                self.pipeo.write(v)
-        self.pipeo.flush()
-
-        return self.pipei
-
-    def _call(self, cmd, **args):
-        self._callstream(cmd, **args)
-        return self._recv()
-
-    def _callpush(self, cmd, fp, **args):
-        r = self._call(cmd, **args)
-        if r:
-            return '', r
-        while True:
-            d = fp.read(4096)
-            if not d:
-                break
-            self._send(d)
-        self._send("", flush=True)
-        r = self._recv()
-        if r:
-            return '', r
-        return self._recv(), ''
-
-    def _decompress(self, stream):
-        return stream
-
-    def _recv(self):
-        l = self.pipei.readline()
-        if l == '\n':
-            err = []
-            while True:
-                line = self.pipee.readline()
-                if line == '-\n':
-                    break
-                err.extend([line])
-            if len(err) > 0:
-                # strip the trailing newline added to the last line server-side
-                err[-1] = err[-1][:-1]
-            self._abort(error.OutOfBandError(*err))
-        self.readerr()
-        try:
-            l = int(l)
-        except ValueError:
-            self._abort(error.ResponseError(_("unexpected response:"), l))
-        return self.pipei.read(l)
-
-    def _send(self, data, flush=False):
-        self.pipeo.write("%d\n" % len(data))
-        if data:
-            self.pipeo.write(data)
-        if flush:
-            self.pipeo.flush()
-        self.readerr()
-
-    def lock(self):
-        self._call("lock")
-        return remotelock(self)
-
-    def unlock(self):
-        self._call("unlock")
-
-    def addchangegroup(self, cg, source, url, lock=None):
-        '''Send a changegroup to the remote server.  Return an integer
-        similar to unbundle(). DEPRECATED, since it requires locking the
-        remote.'''
-        d = self._call("addchangegroup")
-        if d:
-            self._abort(error.RepoError(_("push refused: %s") % d))
-        while True:
-            d = cg.read(4096)
-            if not d:
-                break
-            self.pipeo.write(d)
-            self.readerr()
-
-        self.pipeo.flush()
-
-        self.readerr()
-        r = self._recv()
-        if not r:
-            return 1
-        try:
-            return int(r)
-        except ValueError:
-            self._abort(error.ResponseError(_("unexpected response:"), r))
-
-instance = sshrepository
--- a/mercurial/statichttprepo.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/mercurial/statichttprepo.py	Fri Jul 13 21:47:06 2012 +0200
@@ -76,6 +76,10 @@
 
     return statichttpopener
 
+class statichttppeer(localrepo.localpeer):
+    def local(self):
+        return None
+
 class statichttprepository(localrepo.localrepository):
     def __init__(self, ui, path):
         self._url = path
@@ -116,6 +120,7 @@
         self.svfs = self.sopener
         self.sjoin = self.store.join
         self._filecache = {}
+        self.requirements = requirements
 
         self.manifest = manifest.manifest(self.sopener)
         self.changelog = changelog.changelog(self.sopener)
@@ -125,7 +130,9 @@
         self._branchcachetip = None
         self.encodepats = None
         self.decodepats = None
-        self.capabilities.difference_update(["pushkey"])
+
+    def _restrictcapabilities(self, caps):
+        return caps.difference(["pushkey"])
 
     def url(self):
         return self._url
@@ -133,6 +140,9 @@
     def local(self):
         return False
 
+    def peer(self):
+        return statichttppeer(self)
+
     def lock(self, wait=True):
         raise util.Abort(_('cannot lock static-http repository'))
 
--- a/mercurial/wireproto.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/mercurial/wireproto.py	Fri Jul 13 21:47:06 2012 +0200
@@ -9,7 +9,7 @@
 from i18n import _
 from node import bin, hex
 import changegroup as changegroupmod
-import repo, error, encoding, util, store
+import peer, error, encoding, util, store
 import phases
 
 # abstract batching support
@@ -149,7 +149,7 @@
 def todict(**args):
     return args
 
-class wirerepository(repo.repository):
+class wirepeer(peer.peerrepository):
 
     def batch(self):
         return remotebatch(self)
--- a/tests/notcapable	Fri Jul 13 21:46:53 2012 +0200
+++ b/tests/notcapable	Fri Jul 13 21:47:06 2012 +0200
@@ -6,13 +6,18 @@
 fi
 
 cat > notcapable-$CAP.py << EOF
-from mercurial import extensions, repo
+from mercurial import extensions, peer, localrepo
 def extsetup():
-    extensions.wrapfunction(repo.repository, 'capable', wrapper)
-def wrapper(orig, self, name, *args, **kwargs):
+    extensions.wrapfunction(peer.peerrepository, 'capable', wrapcapable)
+    extensions.wrapfunction(localrepo.localrepository, 'peer', wrappeer)
+def wrapcapable(orig, self, name, *args, **kwargs):
     if name in '$CAP'.split(' '):
         return False
     return orig(self, name, *args, **kwargs)
+def wrappeer(orig, self):
+    # Since we're disabling some newer features, we need to make sure local
+    # repos add in the legacy features again.
+    return localrepo.locallegacypeer(self)
 EOF
 
 echo '[extensions]' >> $HGRCPATH
--- a/tests/test-wireproto.py	Fri Jul 13 21:46:53 2012 +0200
+++ b/tests/test-wireproto.py	Fri Jul 13 21:47:06 2012 +0200
@@ -9,7 +9,7 @@
         names = spec.split()
         return [args[n] for n in names]
 
-class clientrepo(wireproto.wirerepository):
+class clientpeer(wireproto.wirepeer):
     def __init__(self, serverrepo):
         self.serverrepo = serverrepo
     def _call(self, cmd, **args):
@@ -36,7 +36,7 @@
 wireproto.commands['greet'] = (greet, 'name',)
 
 srv = serverrepo()
-clt = clientrepo(srv)
+clt = clientpeer(srv)
 
 print clt.greet("Foobar")
 b = clt.batch()