D6796: flagutil: introduce a flagprocessorsmixin class

marmoute (Pierre-Yves David) phabricator at mercurial-scm.org
Sat Sep 7 05:26:54 EDT 2019


marmoute created this revision.
marmoute added reviewers: yuja, durin42.
Herald added a reviewer: indygreg.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  To avoid code duplication, we will provide a simple "ready to use" mixin that
  carry the appropriate logic. First we use it in standard revlog, we'll remove
  code duplication in later changesets.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/revlog.py
  mercurial/revlogutils/flagutil.py

CHANGE DETAILS

diff --git a/mercurial/revlogutils/flagutil.py b/mercurial/revlogutils/flagutil.py
--- a/mercurial/revlogutils/flagutil.py
+++ b/mercurial/revlogutils/flagutil.py
@@ -78,3 +78,74 @@
         msg = _("cannot register multiple processors on flag '%#x'.") % (flag)
         raise error.Abort(msg)
     flagprocessors[flag] = processor
+
+class flagprocessorsmixin(object):
+    """basic mixin to support revlog flag processing
+
+    Make sure the `_flagprocessors` attribute is set at ``__init__`` time.
+
+    See the documentation of the ``_processflags`` method for details.
+    """
+
+    def _processflags(self, text, flags, operation, raw=False):
+        """Inspect revision data flags and applies transforms defined by
+        registered flag processors.
+
+        ``text`` - the revision data to process
+        ``flags`` - the revision flags
+        ``operation`` - the operation being performed (read or write)
+        ``raw`` - an optional argument describing if the raw transform should be
+        applied.
+
+        This method processes the flags in the order (or reverse order if
+        ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
+        flag processors registered for present flags. The order of flags defined
+        in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
+
+        Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
+        processed text and ``validatehash`` is a bool indicating whether the
+        returned text should be checked for hash integrity.
+
+        Note: If the ``raw`` argument is set, it has precedence over the
+        operation and will only update the value of ``validatehash``.
+        """
+        # fast path: no flag processors will run
+        if flags == 0:
+            return text, True
+        if not operation in ('read', 'write'):
+            raise error.ProgrammingError(_("invalid '%s' operation") %
+                                         operation)
+        # Check all flags are known.
+        if flags & ~REVIDX_KNOWN_FLAGS:
+            raise error.RevlogError(_("incompatible revision flag '%#x'") %
+                                    (flags & ~REVIDX_KNOWN_FLAGS))
+        validatehash = True
+        # Depending on the operation (read or write), the order might be
+        # reversed due to non-commutative transforms.
+        orderedflags = REVIDX_FLAGS_ORDER
+        if operation == 'write':
+            orderedflags = reversed(orderedflags)
+
+        for flag in orderedflags:
+            # If a flagprocessor has been registered for a known flag, apply the
+            # related operation transform and update result tuple.
+            if flag & flags:
+                vhash = True
+
+                if flag not in self._flagprocessors:
+                    message = _("missing processor for flag '%#x'") % (flag)
+                    raise error.RevlogError(message)
+
+                processor = self._flagprocessors[flag]
+                if processor is not None:
+                    readtransform, writetransform, rawtransform = processor
+
+                    if raw:
+                        vhash = rawtransform(self, text)
+                    elif operation == 'read':
+                        text, vhash = readtransform(self, text)
+                    else: # write operation
+                        text, vhash = writetransform(self, text)
+                validatehash = validatehash and vhash
+
+        return text, validatehash
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -261,7 +261,7 @@
             p = versionformat_pack(version) + p[4:]
         return p
 
-class revlog(object):
+class revlog(flagutil.flagprocessorsmixin):
     """
     the underlying revision storage object
 
@@ -1711,69 +1711,6 @@
         """
         return storageutil.hashrevisionsha1(text, p1, p2)
 
-    def _processflags(self, text, flags, operation, raw=False):
-        """Inspect revision data flags and applies transforms defined by
-        registered flag processors.
-
-        ``text`` - the revision data to process
-        ``flags`` - the revision flags
-        ``operation`` - the operation being performed (read or write)
-        ``raw`` - an optional argument describing if the raw transform should be
-        applied.
-
-        This method processes the flags in the order (or reverse order if
-        ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
-        flag processors registered for present flags. The order of flags defined
-        in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
-
-        Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
-        processed text and ``validatehash`` is a bool indicating whether the
-        returned text should be checked for hash integrity.
-
-        Note: If the ``raw`` argument is set, it has precedence over the
-        operation and will only update the value of ``validatehash``.
-        """
-        # fast path: no flag processors will run
-        if flags == 0:
-            return text, True
-        if not operation in ('read', 'write'):
-            raise error.ProgrammingError(_("invalid '%s' operation") %
-                                         operation)
-        # Check all flags are known.
-        if flags & ~flagutil.REVIDX_KNOWN_FLAGS:
-            raise error.RevlogError(_("incompatible revision flag '%#x'") %
-                                    (flags & ~flagutil.REVIDX_KNOWN_FLAGS))
-        validatehash = True
-        # Depending on the operation (read or write), the order might be
-        # reversed due to non-commutative transforms.
-        orderedflags = REVIDX_FLAGS_ORDER
-        if operation == 'write':
-            orderedflags = reversed(orderedflags)
-
-        for flag in orderedflags:
-            # If a flagprocessor has been registered for a known flag, apply the
-            # related operation transform and update result tuple.
-            if flag & flags:
-                vhash = True
-
-                if flag not in self._flagprocessors:
-                    message = _("missing processor for flag '%#x'") % (flag)
-                    raise error.RevlogError(message)
-
-                processor = self._flagprocessors[flag]
-                if processor is not None:
-                    readtransform, writetransform, rawtransform = processor
-
-                    if raw:
-                        vhash = rawtransform(self, text)
-                    elif operation == 'read':
-                        text, vhash = readtransform(self, text)
-                    else: # write operation
-                        text, vhash = writetransform(self, text)
-                validatehash = validatehash and vhash
-
-        return text, validatehash
-
     def checkhash(self, text, node, p1=None, p2=None, rev=None):
         """Check node hash integrity.
 



To: marmoute, yuja, durin42, indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list