comparison mercurial/revlogutils/flagutil.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 142deb539ccf
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
18 REVIDX_ISCENSORED, 18 REVIDX_ISCENSORED,
19 REVIDX_RAWTEXT_CHANGING_FLAGS, 19 REVIDX_RAWTEXT_CHANGING_FLAGS,
20 REVIDX_SIDEDATA, 20 REVIDX_SIDEDATA,
21 ) 21 )
22 22
23 from .. import ( 23 from .. import error, util
24 error,
25 util
26 )
27 24
28 # blanked usage of all the name to prevent pyflakes constraints 25 # blanked usage of all the name to prevent pyflakes constraints
29 # We need these name available in the module for extensions. 26 # We need these name available in the module for extensions.
30 REVIDX_ISCENSORED 27 REVIDX_ISCENSORED
31 REVIDX_ELLIPSIS 28 REVIDX_ELLIPSIS
39 36
40 # Store flag processors (cf. 'addflagprocessor()' to register) 37 # Store flag processors (cf. 'addflagprocessor()' to register)
41 flagprocessors = { 38 flagprocessors = {
42 REVIDX_ISCENSORED: None, 39 REVIDX_ISCENSORED: None,
43 } 40 }
41
44 42
45 def addflagprocessor(flag, processor): 43 def addflagprocessor(flag, processor):
46 """Register a flag processor on a revision data flag. 44 """Register a flag processor on a revision data flag.
47 45
48 Invariant: 46 Invariant:
67 debug commands. In this case the transform only indicates whether the 65 debug commands. In this case the transform only indicates whether the
68 contents can be used for hash integrity checks. 66 contents can be used for hash integrity checks.
69 """ 67 """
70 insertflagprocessor(flag, processor, flagprocessors) 68 insertflagprocessor(flag, processor, flagprocessors)
71 69
70
72 def insertflagprocessor(flag, processor, flagprocessors): 71 def insertflagprocessor(flag, processor, flagprocessors):
73 if not flag & REVIDX_KNOWN_FLAGS: 72 if not flag & REVIDX_KNOWN_FLAGS:
74 msg = _("cannot register processor on unknown flag '%#x'.") % (flag) 73 msg = _("cannot register processor on unknown flag '%#x'.") % flag
75 raise error.ProgrammingError(msg) 74 raise error.ProgrammingError(msg)
76 if flag not in REVIDX_FLAGS_ORDER: 75 if flag not in REVIDX_FLAGS_ORDER:
77 msg = _("flag '%#x' undefined in REVIDX_FLAGS_ORDER.") % (flag) 76 msg = _("flag '%#x' undefined in REVIDX_FLAGS_ORDER.") % flag
78 raise error.ProgrammingError(msg) 77 raise error.ProgrammingError(msg)
79 if flag in flagprocessors: 78 if flag in flagprocessors:
80 msg = _("cannot register multiple processors on flag '%#x'.") % (flag) 79 msg = _("cannot register multiple processors on flag '%#x'.") % flag
81 raise error.Abort(msg) 80 raise error.Abort(msg)
82 flagprocessors[flag] = processor 81 flagprocessors[flag] = processor
82
83 83
84 def processflagswrite(revlog, text, flags, sidedata): 84 def processflagswrite(revlog, text, flags, sidedata):
85 """Inspect revision data flags and applies write transformations defined 85 """Inspect revision data flags and applies write transformations defined
86 by registered flag processors. 86 by registered flag processors.
87 87
95 95
96 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the 96 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
97 processed text and ``validatehash`` is a bool indicating whether the 97 processed text and ``validatehash`` is a bool indicating whether the
98 returned text should be checked for hash integrity. 98 returned text should be checked for hash integrity.
99 """ 99 """
100 return _processflagsfunc(revlog, text, flags, 'write', 100 return _processflagsfunc(revlog, text, flags, 'write', sidedata=sidedata)[
101 sidedata=sidedata)[:2] 101 :2
102 ]
103
102 104
103 def processflagsread(revlog, text, flags): 105 def processflagsread(revlog, text, flags):
104 """Inspect revision data flags and applies read transformations defined 106 """Inspect revision data flags and applies read transformations defined
105 by registered flag processors. 107 by registered flag processors.
106 108
118 processed text and ``validatehash`` is a bool indicating whether the 120 processed text and ``validatehash`` is a bool indicating whether the
119 returned text should be checked for hash integrity. 121 returned text should be checked for hash integrity.
120 """ 122 """
121 return _processflagsfunc(revlog, text, flags, 'read') 123 return _processflagsfunc(revlog, text, flags, 'read')
122 124
125
123 def processflagsraw(revlog, text, flags): 126 def processflagsraw(revlog, text, flags):
124 """Inspect revision data flags to check is the content hash should be 127 """Inspect revision data flags to check is the content hash should be
125 validated. 128 validated.
126 129
127 ``text`` - the revision data to process 130 ``text`` - the revision data to process
136 processed text and ``validatehash`` is a bool indicating whether the 139 processed text and ``validatehash`` is a bool indicating whether the
137 returned text should be checked for hash integrity. 140 returned text should be checked for hash integrity.
138 """ 141 """
139 return _processflagsfunc(revlog, text, flags, 'raw')[1] 142 return _processflagsfunc(revlog, text, flags, 'raw')[1]
140 143
144
141 def _processflagsfunc(revlog, text, flags, operation, sidedata=None): 145 def _processflagsfunc(revlog, text, flags, operation, sidedata=None):
142 """internal function to process flag on a revlog 146 """internal function to process flag on a revlog
143 147
144 This function is private to this module, code should never needs to call it 148 This function is private to this module, code should never needs to call it
145 directly.""" 149 directly."""
146 # fast path: no flag processors will run 150 # fast path: no flag processors will run
147 if flags == 0: 151 if flags == 0:
148 return text, True, {} 152 return text, True, {}
149 if operation not in ('read', 'write', 'raw'): 153 if operation not in ('read', 'write', 'raw'):
150 raise error.ProgrammingError(_("invalid '%s' operation") % 154 raise error.ProgrammingError(_("invalid '%s' operation") % operation)
151 operation)
152 # Check all flags are known. 155 # Check all flags are known.
153 if flags & ~REVIDX_KNOWN_FLAGS: 156 if flags & ~REVIDX_KNOWN_FLAGS:
154 raise revlog._flagserrorclass(_("incompatible revision flag '%#x'") % 157 raise revlog._flagserrorclass(
155 (flags & ~REVIDX_KNOWN_FLAGS)) 158 _("incompatible revision flag '%#x'")
159 % (flags & ~REVIDX_KNOWN_FLAGS)
160 )
156 validatehash = True 161 validatehash = True
157 # Depending on the operation (read or write), the order might be 162 # Depending on the operation (read or write), the order might be
158 # reversed due to non-commutative transforms. 163 # reversed due to non-commutative transforms.
159 orderedflags = REVIDX_FLAGS_ORDER 164 orderedflags = REVIDX_FLAGS_ORDER
160 if operation == 'write': 165 if operation == 'write':
166 # related operation transform and update result tuple. 171 # related operation transform and update result tuple.
167 if flag & flags: 172 if flag & flags:
168 vhash = True 173 vhash = True
169 174
170 if flag not in revlog._flagprocessors: 175 if flag not in revlog._flagprocessors:
171 message = _("missing processor for flag '%#x'") % (flag) 176 message = _("missing processor for flag '%#x'") % flag
172 raise revlog._flagserrorclass(message) 177 raise revlog._flagserrorclass(message)
173 178
174 processor = revlog._flagprocessors[flag] 179 processor = revlog._flagprocessors[flag]
175 if processor is not None: 180 if processor is not None:
176 readtransform, writetransform, rawtransform = processor 181 readtransform, writetransform, rawtransform = processor
178 if operation == 'raw': 183 if operation == 'raw':
179 vhash = rawtransform(revlog, text) 184 vhash = rawtransform(revlog, text)
180 elif operation == 'read': 185 elif operation == 'read':
181 text, vhash, s = readtransform(revlog, text) 186 text, vhash, s = readtransform(revlog, text)
182 outsidedata.update(s) 187 outsidedata.update(s)
183 else: # write operation 188 else: # write operation
184 text, vhash = writetransform(revlog, text, sidedata) 189 text, vhash = writetransform(revlog, text, sidedata)
185 validatehash = validatehash and vhash 190 validatehash = validatehash and vhash
186 191
187 return text, validatehash, outsidedata 192 return text, validatehash, outsidedata