Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/revlogutils/flagutil.py @ 42877:5bb68fb72df2
flagutil: introduce a flagprocessorsmixin class
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.
Differential Revision: https://phab.mercurial-scm.org/D6796
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Thu, 08 Aug 2019 01:12:48 +0200 |
parents | 6d61be152c55 |
children | 7907008a0bb5 |
comparison
equal
deleted
inserted
replaced
42876:4257c33e24b7 | 42877: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 |