Mercurial > public > mercurial-scm > hg
comparison hgext/fastannotate/revmap.py @ 43077:687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Done with
python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py')
black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**')
# skip-blame mass-reformatting only
Differential Revision: https://phab.mercurial-scm.org/D6972
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:48:39 -0400 |
parents | 2372284d9457 |
children | eef9a2d67051 |
comparison
equal
deleted
inserted
replaced
43076:2372284d9457 | 43077:687b865b95ad |
---|---|
68 self._rev2flag = [None] | 68 self._rev2flag = [None] |
69 self._hsh2rev = {} | 69 self._hsh2rev = {} |
70 # since rename does not happen frequently, do not store path for every | 70 # since rename does not happen frequently, do not store path for every |
71 # revision. self._renamerevs can be used for bisecting. | 71 # revision. self._renamerevs can be used for bisecting. |
72 self._renamerevs = [0] | 72 self._renamerevs = [0] |
73 self._renamepaths = [''] | 73 self._renamepaths = [b''] |
74 self._lastmaxrev = -1 | 74 self._lastmaxrev = -1 |
75 if path: | 75 if path: |
76 if os.path.exists(path): | 76 if os.path.exists(path): |
77 self._load() | 77 self._load() |
78 else: | 78 else: |
96 def append(self, hsh, sidebranch=False, path=None, flush=False): | 96 def append(self, hsh, sidebranch=False, path=None, flush=False): |
97 """add a binary hg hash and return the mapped linelog revision. | 97 """add a binary hg hash and return the mapped linelog revision. |
98 if flush is True, incrementally update the file. | 98 if flush is True, incrementally update the file. |
99 """ | 99 """ |
100 if hsh in self._hsh2rev: | 100 if hsh in self._hsh2rev: |
101 raise error.CorruptedFileError('%r is in revmap already' % hex(hsh)) | 101 raise error.CorruptedFileError( |
102 b'%r is in revmap already' % hex(hsh) | |
103 ) | |
102 if len(hsh) != _hshlen: | 104 if len(hsh) != _hshlen: |
103 raise hgerror.ProgrammingError('hsh must be %d-char long' % _hshlen) | 105 raise hgerror.ProgrammingError( |
106 b'hsh must be %d-char long' % _hshlen | |
107 ) | |
104 idx = len(self._rev2hsh) | 108 idx = len(self._rev2hsh) |
105 flag = 0 | 109 flag = 0 |
106 if sidebranch: | 110 if sidebranch: |
107 flag |= sidebranchflag | 111 flag |= sidebranchflag |
108 if path is not None and path != self._renamepaths[-1]: | 112 if path is not None and path != self._renamepaths[-1]: |
147 """make the map empty. if flush is True, write to disk""" | 151 """make the map empty. if flush is True, write to disk""" |
148 # rev 0 is reserved, real rev starts from 1 | 152 # rev 0 is reserved, real rev starts from 1 |
149 self._rev2hsh = [None] | 153 self._rev2hsh = [None] |
150 self._rev2flag = [None] | 154 self._rev2flag = [None] |
151 self._hsh2rev = {} | 155 self._hsh2rev = {} |
152 self._rev2path = [''] | 156 self._rev2path = [b''] |
153 self._lastmaxrev = -1 | 157 self._lastmaxrev = -1 |
154 if flush: | 158 if flush: |
155 self.flush() | 159 self.flush() |
156 | 160 |
157 def flush(self): | 161 def flush(self): |
158 """write the state down to the file""" | 162 """write the state down to the file""" |
159 if not self.path: | 163 if not self.path: |
160 return | 164 return |
161 if self._lastmaxrev == -1: # write the entire file | 165 if self._lastmaxrev == -1: # write the entire file |
162 with open(self.path, 'wb') as f: | 166 with open(self.path, b'wb') as f: |
163 f.write(self.HEADER) | 167 f.write(self.HEADER) |
164 for i in pycompat.xrange(1, len(self._rev2hsh)): | 168 for i in pycompat.xrange(1, len(self._rev2hsh)): |
165 self._writerev(i, f) | 169 self._writerev(i, f) |
166 else: # append incrementally | 170 else: # append incrementally |
167 with open(self.path, 'ab') as f: | 171 with open(self.path, b'ab') as f: |
168 for i in pycompat.xrange( | 172 for i in pycompat.xrange( |
169 self._lastmaxrev + 1, len(self._rev2hsh) | 173 self._lastmaxrev + 1, len(self._rev2hsh) |
170 ): | 174 ): |
171 self._writerev(i, f) | 175 self._writerev(i, f) |
172 self._lastmaxrev = self.maxrev | 176 self._lastmaxrev = self.maxrev |
177 return | 181 return |
178 # use local variables in a loop. CPython uses LOAD_FAST for them, | 182 # use local variables in a loop. CPython uses LOAD_FAST for them, |
179 # which is faster than both LOAD_CONST and LOAD_GLOBAL. | 183 # which is faster than both LOAD_CONST and LOAD_GLOBAL. |
180 flaglen = 1 | 184 flaglen = 1 |
181 hshlen = _hshlen | 185 hshlen = _hshlen |
182 with open(self.path, 'rb') as f: | 186 with open(self.path, b'rb') as f: |
183 if f.read(len(self.HEADER)) != self.HEADER: | 187 if f.read(len(self.HEADER)) != self.HEADER: |
184 raise error.CorruptedFileError() | 188 raise error.CorruptedFileError() |
185 self.clear(flush=False) | 189 self.clear(flush=False) |
186 while True: | 190 while True: |
187 buf = f.read(flaglen) | 191 buf = f.read(flaglen) |
203 | 207 |
204 def _writerev(self, rev, f): | 208 def _writerev(self, rev, f): |
205 """append a revision data to file""" | 209 """append a revision data to file""" |
206 flag = self._rev2flag[rev] | 210 flag = self._rev2flag[rev] |
207 hsh = self._rev2hsh[rev] | 211 hsh = self._rev2hsh[rev] |
208 f.write(struct.pack('B', flag)) | 212 f.write(struct.pack(b'B', flag)) |
209 if flag & renameflag: | 213 if flag & renameflag: |
210 path = self.rev2path(rev) | 214 path = self.rev2path(rev) |
211 if path is None: | 215 if path is None: |
212 raise error.CorruptedFileError('cannot find path for %s' % rev) | 216 raise error.CorruptedFileError(b'cannot find path for %s' % rev) |
213 f.write(path + b'\0') | 217 f.write(path + b'\0') |
214 f.write(hsh) | 218 f.write(hsh) |
215 | 219 |
216 @staticmethod | 220 @staticmethod |
217 def _readcstr(f): | 221 def _readcstr(f): |
218 """read a C-language-like '\0'-terminated string""" | 222 """read a C-language-like '\0'-terminated string""" |
219 buf = '' | 223 buf = b'' |
220 while True: | 224 while True: |
221 ch = f.read(1) | 225 ch = f.read(1) |
222 if not ch: # unexpected eof | 226 if not ch: # unexpected eof |
223 raise error.CorruptedFileError() | 227 raise error.CorruptedFileError() |
224 if ch == '\0': | 228 if ch == b'\0': |
225 break | 229 break |
226 buf += ch | 230 buf += ch |
227 return buf | 231 return buf |
228 | 232 |
229 def __contains__(self, f): | 233 def __contains__(self, f): |
247 """return the last hash in a revmap, without loading its full content. | 251 """return the last hash in a revmap, without loading its full content. |
248 this is equivalent to `m = revmap(path); m.rev2hsh(m.maxrev)`, but faster. | 252 this is equivalent to `m = revmap(path); m.rev2hsh(m.maxrev)`, but faster. |
249 """ | 253 """ |
250 hsh = None | 254 hsh = None |
251 try: | 255 try: |
252 with open(path, 'rb') as f: | 256 with open(path, b'rb') as f: |
253 f.seek(-_hshlen, io.SEEK_END) | 257 f.seek(-_hshlen, io.SEEK_END) |
254 if f.tell() > len(revmap.HEADER): | 258 if f.tell() > len(revmap.HEADER): |
255 hsh = f.read(_hshlen) | 259 hsh = f.read(_hshlen) |
256 except IOError: | 260 except IOError: |
257 pass | 261 pass |