mercurial/repository.py
changeset 33802 a0aad86b3b6a
parent 33801 558f5b2ee10e
child 37181 0dfb5672f015
--- a/mercurial/repository.py	Sun Aug 13 11:04:42 2017 -0700
+++ b/mercurial/repository.py	Sun Aug 06 16:47:25 2017 -0700
@@ -9,6 +9,11 @@
 
 import abc
 
+from .i18n import _
+from . import (
+    error,
+)
+
 class _basepeer(object):
     """Represents a "connection" to a repository.
 
@@ -228,5 +233,36 @@
         calls. However, they must all support this API.
         """
 
+    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."""