comparison mercurial/revlogutils/flagutil.py @ 42991:50d9de61ce02

flagprocessors: make `_processflagsfunc` a module level function This is the first step toward removing the flag processing mixin. Differential Revision: https://phab.mercurial-scm.org/D6817
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Fri, 06 Sep 2019 23:43:06 +0200
parents f4caf910669e
children a04b2c010d03
comparison
equal deleted inserted replaced
42990:f4caf910669e 42991:50d9de61ce02
116 116
117 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the 117 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
118 processed text and ``validatehash`` is a bool indicating whether the 118 processed text and ``validatehash`` is a bool indicating whether the
119 returned text should be checked for hash integrity. 119 returned text should be checked for hash integrity.
120 """ 120 """
121 return self._processflagsfunc(text, flags, 'read') 121 return _processflagsfunc(self, text, flags, 'read')
122 122
123 def _processflagswrite(self, text, flags, sidedata): 123 def _processflagswrite(self, text, flags, sidedata):
124 """Inspect revision data flags and applies write transformations defined 124 """Inspect revision data flags and applies write transformations defined
125 by registered flag processors. 125 by registered flag processors.
126 126
134 134
135 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the 135 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
136 processed text and ``validatehash`` is a bool indicating whether the 136 processed text and ``validatehash`` is a bool indicating whether the
137 returned text should be checked for hash integrity. 137 returned text should be checked for hash integrity.
138 """ 138 """
139 return self._processflagsfunc(text, flags, 'write', 139 return _processflagsfunc(self, text, flags, 'write',
140 sidedata=sidedata)[:2] 140 sidedata=sidedata)[:2]
141 141
142 def _processflagsraw(self, text, flags): 142 def _processflagsraw(self, text, flags):
143 """Inspect revision data flags to check is the content hash should be 143 """Inspect revision data flags to check is the content hash should be
144 validated. 144 validated.
145 145
153 153
154 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the 154 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
155 processed text and ``validatehash`` is a bool indicating whether the 155 processed text and ``validatehash`` is a bool indicating whether the
156 returned text should be checked for hash integrity. 156 returned text should be checked for hash integrity.
157 """ 157 """
158 return self._processflagsfunc(text, flags, 'raw')[1] 158 return _processflagsfunc(self, text, flags, 'raw')[1]
159 159
160 def _processflagsfunc(self, text, flags, operation, sidedata=None): 160 def _processflagsfunc(revlog, text, flags, operation, sidedata=None):
161 # fast path: no flag processors will run 161 """internal function to process flag on a revlog
162 if flags == 0: 162
163 return text, True, {} 163 This function is private to this module, code should never needs to call it
164 if operation not in ('read', 'write', 'raw'): 164 directly."""
165 raise error.ProgrammingError(_("invalid '%s' operation") % 165 # fast path: no flag processors will run
166 operation) 166 if flags == 0:
167 # Check all flags are known. 167 return text, True, {}
168 if flags & ~REVIDX_KNOWN_FLAGS: 168 if operation not in ('read', 'write', 'raw'):
169 raise self._flagserrorclass(_("incompatible revision flag '%#x'") % 169 raise error.ProgrammingError(_("invalid '%s' operation") %
170 (flags & ~REVIDX_KNOWN_FLAGS)) 170 operation)
171 validatehash = True 171 # Check all flags are known.
172 # Depending on the operation (read or write), the order might be 172 if flags & ~REVIDX_KNOWN_FLAGS:
173 # reversed due to non-commutative transforms. 173 raise revlog._flagserrorclass(_("incompatible revision flag '%#x'") %
174 orderedflags = REVIDX_FLAGS_ORDER 174 (flags & ~REVIDX_KNOWN_FLAGS))
175 if operation == 'write': 175 validatehash = True
176 orderedflags = reversed(orderedflags) 176 # Depending on the operation (read or write), the order might be
177 177 # reversed due to non-commutative transforms.
178 outsidedata = {} 178 orderedflags = REVIDX_FLAGS_ORDER
179 for flag in orderedflags: 179 if operation == 'write':
180 # If a flagprocessor has been registered for a known flag, apply the 180 orderedflags = reversed(orderedflags)
181 # related operation transform and update result tuple. 181
182 if flag & flags: 182 outsidedata = {}
183 vhash = True 183 for flag in orderedflags:
184 184 # If a flagprocessor has been registered for a known flag, apply the
185 if flag not in self._flagprocessors: 185 # related operation transform and update result tuple.
186 message = _("missing processor for flag '%#x'") % (flag) 186 if flag & flags:
187 raise self._flagserrorclass(message) 187 vhash = True
188 188
189 processor = self._flagprocessors[flag] 189 if flag not in revlog._flagprocessors:
190 if processor is not None: 190 message = _("missing processor for flag '%#x'") % (flag)
191 readtransform, writetransform, rawtransform = processor 191 raise revlog._flagserrorclass(message)
192 192
193 if operation == 'raw': 193 processor = revlog._flagprocessors[flag]
194 vhash = rawtransform(self, text) 194 if processor is not None:
195 elif operation == 'read': 195 readtransform, writetransform, rawtransform = processor
196 text, vhash, s = readtransform(self, text) 196
197 outsidedata.update(s) 197 if operation == 'raw':
198 else: # write operation 198 vhash = rawtransform(revlog, text)
199 text, vhash = writetransform(self, text, sidedata) 199 elif operation == 'read':
200 validatehash = validatehash and vhash 200 text, vhash, s = readtransform(revlog, text)
201 201 outsidedata.update(s)
202 return text, validatehash, outsidedata 202 else: # write operation
203 text, vhash = writetransform(revlog, text, sidedata)
204 validatehash = validatehash and vhash
205
206 return text, validatehash, outsidedata