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

login
register
mail settings
Submitter phabricator
Date Aug. 15, 2017, 5:51 p.m.
Message ID <b0f400c838dab0916b02221d7dae9765@localhost.localdomain>
Download mbox | patch
Permalink /patch/23028/
State Not Applicable
Headers show

Comments

phabricator - Aug. 15, 2017, 5:51 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGa0aad86b3b6a: repository: implement generic capability methods on peer class (authored by indygreg).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D334?vs=849&id=933

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

AFFECTED FILES
  mercurial/repository.py

CHANGE DETAILS




To: indygreg, #hg-reviewers, durin42
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.
 
@@ -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."""