Patchwork D334: repository: implement generic capability methods on peer class

login
register
mail settings
Submitter phabricator
Date Aug. 11, 2017, 4:20 a.m.
Message ID <differential-rev-PHID-DREV-uil7eww33laljx3c4or7-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/22846/
State Superseded
Headers show

Comments

phabricator - Aug. 11, 2017, 4:20 a.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  These methods are part of the peer interface, are generic, and can
  be implemented in terms of other members of the peer interface. So we
  implement them on the peer base class as a convenience.
  
  The implementation is essentially copied from peer.py. The code
  in peer.py will eventually be deleted.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D334

AFFECTED FILES
  mercurial/repository.py

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/repository.py b/mercurial/repository.py
--- a/mercurial/repository.py
+++ b/mercurial/repository.py
@@ -9,6 +9,11 @@ 
 
 import abc
 
+from .i18n import _
+from . import (
+    error,
+)
+
 class _basepeer(object):
     """Represents a "connection" to a repository.
 
@@ -216,5 +221,36 @@ 
         of results in the order they were called.
         """
 
+    def capable(self, name):
+        """Determine support for a named capability.
+
+        Returns ``False`` if capability not supported.
+
+        Returns ``True`` if boolean capability is supported. Returns a string
+        if capability support is non-boolean.
+        """
+        caps = self.capabilities()
+        if name in caps:
+            return True
+
+        name = '%s=' % name
+        for cap in caps:
+            if cap.startswith(name):
+                return cap[len(name):]
+
+        return False
+
+    def requirecap(self, name, purpose):
+        """Require a capability to be present.
+
+        Raises a ``CapabilityError`` if the capability isn't present.
+        """
+        if self.capable(name):
+            return
+
+        raise error.CapabilityError(
+            _('cannot %s; remote repository does not support the %r '
+              'capability') % (purpose, name))
+
 class legacypeer(peer, _baselegacywirecommands):
     """peer but with support for legacy wire protocol commands."""