mercurial/revlogutils/flagutil.py
changeset 42871 5bb68fb72df2
parent 42732 6d61be152c55
child 42873 7907008a0bb5
equal deleted inserted replaced
42870:4257c33e24b7 42871:5bb68fb72df2
    76         raise error.ProgrammingError(msg)
    76         raise error.ProgrammingError(msg)
    77     if flag in flagprocessors:
    77     if flag in flagprocessors:
    78         msg = _("cannot register multiple processors on flag '%#x'.") % (flag)
    78         msg = _("cannot register multiple processors on flag '%#x'.") % (flag)
    79         raise error.Abort(msg)
    79         raise error.Abort(msg)
    80     flagprocessors[flag] = processor
    80     flagprocessors[flag] = processor
       
    81 
       
    82 class flagprocessorsmixin(object):
       
    83     """basic mixin to support revlog flag processing
       
    84 
       
    85     Make sure the `_flagprocessors` attribute is set at ``__init__`` time.
       
    86 
       
    87     See the documentation of the ``_processflags`` method for details.
       
    88     """
       
    89 
       
    90     def _processflags(self, text, flags, operation, raw=False):
       
    91         """Inspect revision data flags and applies transforms defined by
       
    92         registered flag processors.
       
    93 
       
    94         ``text`` - the revision data to process
       
    95         ``flags`` - the revision flags
       
    96         ``operation`` - the operation being performed (read or write)
       
    97         ``raw`` - an optional argument describing if the raw transform should be
       
    98         applied.
       
    99 
       
   100         This method processes the flags in the order (or reverse order if
       
   101         ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
       
   102         flag processors registered for present flags. The order of flags defined
       
   103         in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
       
   104 
       
   105         Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
       
   106         processed text and ``validatehash`` is a bool indicating whether the
       
   107         returned text should be checked for hash integrity.
       
   108 
       
   109         Note: If the ``raw`` argument is set, it has precedence over the
       
   110         operation and will only update the value of ``validatehash``.
       
   111         """
       
   112         # fast path: no flag processors will run
       
   113         if flags == 0:
       
   114             return text, True
       
   115         if not operation in ('read', 'write'):
       
   116             raise error.ProgrammingError(_("invalid '%s' operation") %
       
   117                                          operation)
       
   118         # Check all flags are known.
       
   119         if flags & ~REVIDX_KNOWN_FLAGS:
       
   120             raise error.RevlogError(_("incompatible revision flag '%#x'") %
       
   121                                     (flags & ~REVIDX_KNOWN_FLAGS))
       
   122         validatehash = True
       
   123         # Depending on the operation (read or write), the order might be
       
   124         # reversed due to non-commutative transforms.
       
   125         orderedflags = REVIDX_FLAGS_ORDER
       
   126         if operation == 'write':
       
   127             orderedflags = reversed(orderedflags)
       
   128 
       
   129         for flag in orderedflags:
       
   130             # If a flagprocessor has been registered for a known flag, apply the
       
   131             # related operation transform and update result tuple.
       
   132             if flag & flags:
       
   133                 vhash = True
       
   134 
       
   135                 if flag not in self._flagprocessors:
       
   136                     message = _("missing processor for flag '%#x'") % (flag)
       
   137                     raise error.RevlogError(message)
       
   138 
       
   139                 processor = self._flagprocessors[flag]
       
   140                 if processor is not None:
       
   141                     readtransform, writetransform, rawtransform = processor
       
   142 
       
   143                     if raw:
       
   144                         vhash = rawtransform(self, text)
       
   145                     elif operation == 'read':
       
   146                         text, vhash = readtransform(self, text)
       
   147                     else: # write operation
       
   148                         text, vhash = writetransform(self, text)
       
   149                 validatehash = validatehash and vhash
       
   150 
       
   151         return text, validatehash