diff mercurial/localrepo.py @ 17192:1ac628cd7113

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.
author Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
date Fri, 13 Jul 2012 21:47:06 +0200
parents 4253cfee08ef
children 1d710fe5ee0e
line wrap: on
line diff
--- 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)