##// END OF EJS Templates
flagprocessors: directly duplicate the deprecated layer back into revlog...
marmoute -
r43263:01304095 default
parent child Browse files
Show More
@@ -1,446 +1,458 b''
1 1 # remotefilelog.py - filelog implementation where filelog history is stored
2 2 # remotely
3 3 #
4 4 # Copyright 2013 Facebook, Inc.
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8 from __future__ import absolute_import
9 9
10 10 import collections
11 11 import os
12 12
13 13 from mercurial.node import (
14 14 bin,
15 15 nullid,
16 16 wdirfilenodeids,
17 17 wdirid,
18 18 )
19 19 from mercurial.i18n import _
20 20 from mercurial import (
21 21 ancestor,
22 22 error,
23 23 mdiff,
24 24 revlog,
25 util,
25 26 )
26 27 from mercurial.utils import storageutil
27 28 from mercurial.revlogutils import flagutil
28 29
29 30 from . import (
30 31 constants,
31 32 fileserverclient,
32 33 shallowutil,
33 34 )
34 35
35 36 class remotefilelognodemap(object):
36 37 def __init__(self, filename, store):
37 38 self._filename = filename
38 39 self._store = store
39 40
40 41 def __contains__(self, node):
41 42 missing = self._store.getmissing([(self._filename, node)])
42 43 return not bool(missing)
43 44
44 45 def __get__(self, node):
45 46 if node not in self:
46 47 raise KeyError(node)
47 48 return node
48 49
49 50 class remotefilelog(flagutil.flagprocessorsmixin):
50 51
51 52 _generaldelta = True
52 53
53 54 def __init__(self, opener, path, repo):
54 55 self.opener = opener
55 56 self.filename = path
56 57 self.repo = repo
57 58 self.nodemap = remotefilelognodemap(self.filename, repo.contentstore)
58 59
59 60 self.version = 1
60 61
61 62 self._flagprocessors = dict(flagutil.flagprocessors)
62 63
63 64 def read(self, node):
64 65 """returns the file contents at this node"""
65 66 t = self.revision(node)
66 67 if not t.startswith('\1\n'):
67 68 return t
68 69 s = t.index('\1\n', 2)
69 70 return t[s + 2:]
70 71
71 72 def add(self, text, meta, transaction, linknode, p1=None, p2=None):
72 73 # hash with the metadata, like in vanilla filelogs
73 74 hashtext = shallowutil.createrevlogtext(text, meta.get('copy'),
74 75 meta.get('copyrev'))
75 76 node = storageutil.hashrevisionsha1(hashtext, p1, p2)
76 77 return self.addrevision(hashtext, transaction, linknode, p1, p2,
77 78 node=node)
78 79
79 80 def _createfileblob(self, text, meta, flags, p1, p2, node, linknode):
80 81 # text passed to "_createfileblob" does not include filelog metadata
81 82 header = shallowutil.buildfileblobheader(len(text), flags)
82 83 data = "%s\0%s" % (header, text)
83 84
84 85 realp1 = p1
85 86 copyfrom = ""
86 87 if meta and 'copy' in meta:
87 88 copyfrom = meta['copy']
88 89 realp1 = bin(meta['copyrev'])
89 90
90 91 data += "%s%s%s%s%s\0" % (node, realp1, p2, linknode, copyfrom)
91 92
92 93 visited = set()
93 94
94 95 pancestors = {}
95 96 queue = []
96 97 if realp1 != nullid:
97 98 p1flog = self
98 99 if copyfrom:
99 100 p1flog = remotefilelog(self.opener, copyfrom, self.repo)
100 101
101 102 pancestors.update(p1flog.ancestormap(realp1))
102 103 queue.append(realp1)
103 104 visited.add(realp1)
104 105 if p2 != nullid:
105 106 pancestors.update(self.ancestormap(p2))
106 107 queue.append(p2)
107 108 visited.add(p2)
108 109
109 110 ancestortext = ""
110 111
111 112 # add the ancestors in topological order
112 113 while queue:
113 114 c = queue.pop(0)
114 115 pa1, pa2, ancestorlinknode, pacopyfrom = pancestors[c]
115 116
116 117 pacopyfrom = pacopyfrom or ''
117 118 ancestortext += "%s%s%s%s%s\0" % (
118 119 c, pa1, pa2, ancestorlinknode, pacopyfrom)
119 120
120 121 if pa1 != nullid and pa1 not in visited:
121 122 queue.append(pa1)
122 123 visited.add(pa1)
123 124 if pa2 != nullid and pa2 not in visited:
124 125 queue.append(pa2)
125 126 visited.add(pa2)
126 127
127 128 data += ancestortext
128 129
129 130 return data
130 131
131 132 def addrevision(self, text, transaction, linknode, p1, p2, cachedelta=None,
132 133 node=None, flags=revlog.REVIDX_DEFAULT_FLAGS,
133 134 sidedata=None):
134 135 # text passed to "addrevision" includes hg filelog metadata header
135 136 if node is None:
136 137 node = storageutil.hashrevisionsha1(text, p1, p2)
137 138 if sidedata is None:
138 139 sidedata = {}
139 140
140 141 meta, metaoffset = storageutil.parsemeta(text)
141 142 rawtext, validatehash = flagutil.processflagswrite(self, text, flags,
142 143 sidedata=sidedata)
143 144 return self.addrawrevision(rawtext, transaction, linknode, p1, p2,
144 145 node, flags, cachedelta,
145 146 _metatuple=(meta, metaoffset))
146 147
147 148 def addrawrevision(self, rawtext, transaction, linknode, p1, p2, node,
148 149 flags, cachedelta=None, _metatuple=None):
149 150 if _metatuple:
150 151 # _metatuple: used by "addrevision" internally by remotefilelog
151 152 # meta was parsed confidently
152 153 meta, metaoffset = _metatuple
153 154 else:
154 155 # not from self.addrevision, but something else (repo._filecommit)
155 156 # calls addrawrevision directly. remotefilelog needs to get and
156 157 # strip filelog metadata.
157 158 # we don't have confidence about whether rawtext contains filelog
158 159 # metadata or not (flag processor could replace it), so we just
159 160 # parse it as best-effort.
160 161 # in LFS (flags != 0)'s case, the best way is to call LFS code to
161 162 # get the meta information, instead of storageutil.parsemeta.
162 163 meta, metaoffset = storageutil.parsemeta(rawtext)
163 164 if flags != 0:
164 165 # when flags != 0, be conservative and do not mangle rawtext, since
165 166 # a read flag processor expects the text not being mangled at all.
166 167 metaoffset = 0
167 168 if metaoffset:
168 169 # remotefilelog fileblob stores copy metadata in its ancestortext,
169 170 # not its main blob. so we need to remove filelog metadata
170 171 # (containing copy information) from text.
171 172 blobtext = rawtext[metaoffset:]
172 173 else:
173 174 blobtext = rawtext
174 175 data = self._createfileblob(blobtext, meta, flags, p1, p2, node,
175 176 linknode)
176 177 self.repo.contentstore.addremotefilelognode(self.filename, node, data)
177 178
178 179 return node
179 180
180 181 def renamed(self, node):
181 182 ancestors = self.repo.metadatastore.getancestors(self.filename, node)
182 183 p1, p2, linknode, copyfrom = ancestors[node]
183 184 if copyfrom:
184 185 return (copyfrom, p1)
185 186
186 187 return False
187 188
188 189 def size(self, node):
189 190 """return the size of a given revision"""
190 191 return len(self.read(node))
191 192
192 193 rawsize = size
193 194
194 195 def cmp(self, node, text):
195 196 """compare text with a given file revision
196 197
197 198 returns True if text is different than what is stored.
198 199 """
199 200
200 201 if node == nullid:
201 202 return True
202 203
203 204 nodetext = self.read(node)
204 205 return nodetext != text
205 206
206 207 def __nonzero__(self):
207 208 return True
208 209
209 210 __bool__ = __nonzero__
210 211
211 212 def __len__(self):
212 213 if self.filename == '.hgtags':
213 214 # The length of .hgtags is used to fast path tag checking.
214 215 # remotefilelog doesn't support .hgtags since the entire .hgtags
215 216 # history is needed. Use the excludepattern setting to make
216 217 # .hgtags a normal filelog.
217 218 return 0
218 219
219 220 raise RuntimeError("len not supported")
220 221
221 222 def empty(self):
222 223 return False
223 224
224 225 def flags(self, node):
225 226 if isinstance(node, int):
226 227 raise error.ProgrammingError(
227 228 'remotefilelog does not accept integer rev for flags')
228 229 store = self.repo.contentstore
229 230 return store.getmeta(self.filename, node).get(constants.METAKEYFLAG, 0)
230 231
231 232 def parents(self, node):
232 233 if node == nullid:
233 234 return nullid, nullid
234 235
235 236 ancestormap = self.repo.metadatastore.getancestors(self.filename, node)
236 237 p1, p2, linknode, copyfrom = ancestormap[node]
237 238 if copyfrom:
238 239 p1 = nullid
239 240
240 241 return p1, p2
241 242
242 243 def parentrevs(self, rev):
243 244 # TODO(augie): this is a node and should be a rev, but for now
244 245 # nothing in core seems to actually break.
245 246 return self.parents(rev)
246 247
247 248 def linknode(self, node):
248 249 ancestormap = self.repo.metadatastore.getancestors(self.filename, node)
249 250 p1, p2, linknode, copyfrom = ancestormap[node]
250 251 return linknode
251 252
252 253 def linkrev(self, node):
253 254 return self.repo.unfiltered().changelog.rev(self.linknode(node))
254 255
255 256 def emitrevisions(self, nodes, nodesorder=None, revisiondata=False,
256 257 assumehaveparentrevisions=False, deltaprevious=False,
257 258 deltamode=None):
258 259 # we don't use any of these parameters here
259 260 del nodesorder, revisiondata, assumehaveparentrevisions, deltaprevious
260 261 del deltamode
261 262 prevnode = None
262 263 for node in nodes:
263 264 p1, p2 = self.parents(node)
264 265 if prevnode is None:
265 266 basenode = prevnode = p1
266 267 if basenode == node:
267 268 basenode = nullid
268 269 if basenode != nullid:
269 270 revision = None
270 271 delta = self.revdiff(basenode, node)
271 272 else:
272 273 revision = self.rawdata(node)
273 274 delta = None
274 275 yield revlog.revlogrevisiondelta(
275 276 node=node,
276 277 p1node=p1,
277 278 p2node=p2,
278 279 linknode=self.linknode(node),
279 280 basenode=basenode,
280 281 flags=self.flags(node),
281 282 baserevisionsize=None,
282 283 revision=revision,
283 284 delta=delta,
284 285 )
285 286
286 287 def revdiff(self, node1, node2):
287 288 return mdiff.textdiff(self.rawdata(node1),
288 289 self.rawdata(node2))
289 290
290 291 def lookup(self, node):
291 292 if len(node) == 40:
292 293 node = bin(node)
293 294 if len(node) != 20:
294 295 raise error.LookupError(node, self.filename,
295 296 _('invalid lookup input'))
296 297
297 298 return node
298 299
299 300 def rev(self, node):
300 301 # This is a hack to make TortoiseHG work.
301 302 return node
302 303
303 304 def node(self, rev):
304 305 # This is a hack.
305 306 if isinstance(rev, int):
306 307 raise error.ProgrammingError(
307 308 'remotefilelog does not convert integer rev to node')
308 309 return rev
309 310
311 def _processflags(self, text, flags, operation, raw=False):
312 """deprecated entry point to access flag processors"""
313 msg = ('_processflag(...) use the specialized variant')
314 util.nouideprecwarn(msg, '5.2', stacklevel=2)
315 if raw:
316 return text, flagutil.processflagsraw(self, text, flags)
317 elif operation == 'read':
318 return flagutil.processflagsread(self, text, flags)
319 else: # write operation
320 return flagutil.processflagswrite(self, text, flags)
321
310 322 def revision(self, node, raw=False):
311 323 """returns the revlog contents at this node.
312 324 this includes the meta data traditionally included in file revlogs.
313 325 this is generally only used for bundling and communicating with vanilla
314 326 hg clients.
315 327 """
316 328 if node == nullid:
317 329 return ""
318 330 if len(node) != 20:
319 331 raise error.LookupError(node, self.filename,
320 332 _('invalid revision input'))
321 333 if node == wdirid or node in wdirfilenodeids:
322 334 raise error.WdirUnsupported
323 335
324 336 store = self.repo.contentstore
325 337 rawtext = store.get(self.filename, node)
326 338 if raw:
327 339 return rawtext
328 340 flags = store.getmeta(self.filename, node).get(constants.METAKEYFLAG, 0)
329 341 if flags == 0:
330 342 return rawtext
331 343 return flagutil.processflagsread(self, rawtext, flags)[0]
332 344
333 345 def rawdata(self, node):
334 346 return self.revision(node, raw=False)
335 347
336 348 def _read(self, id):
337 349 """reads the raw file blob from disk, cache, or server"""
338 350 fileservice = self.repo.fileservice
339 351 localcache = fileservice.localcache
340 352 cachekey = fileserverclient.getcachekey(self.repo.name, self.filename,
341 353 id)
342 354 try:
343 355 return localcache.read(cachekey)
344 356 except KeyError:
345 357 pass
346 358
347 359 localkey = fileserverclient.getlocalkey(self.filename, id)
348 360 localpath = os.path.join(self.localpath, localkey)
349 361 try:
350 362 return shallowutil.readfile(localpath)
351 363 except IOError:
352 364 pass
353 365
354 366 fileservice.prefetch([(self.filename, id)])
355 367 try:
356 368 return localcache.read(cachekey)
357 369 except KeyError:
358 370 pass
359 371
360 372 raise error.LookupError(id, self.filename, _('no node'))
361 373
362 374 def ancestormap(self, node):
363 375 return self.repo.metadatastore.getancestors(self.filename, node)
364 376
365 377 def ancestor(self, a, b):
366 378 if a == nullid or b == nullid:
367 379 return nullid
368 380
369 381 revmap, parentfunc = self._buildrevgraph(a, b)
370 382 nodemap = dict(((v, k) for (k, v) in revmap.iteritems()))
371 383
372 384 ancs = ancestor.ancestors(parentfunc, revmap[a], revmap[b])
373 385 if ancs:
374 386 # choose a consistent winner when there's a tie
375 387 return min(map(nodemap.__getitem__, ancs))
376 388 return nullid
377 389
378 390 def commonancestorsheads(self, a, b):
379 391 """calculate all the heads of the common ancestors of nodes a and b"""
380 392
381 393 if a == nullid or b == nullid:
382 394 return nullid
383 395
384 396 revmap, parentfunc = self._buildrevgraph(a, b)
385 397 nodemap = dict(((v, k) for (k, v) in revmap.iteritems()))
386 398
387 399 ancs = ancestor.commonancestorsheads(parentfunc, revmap[a], revmap[b])
388 400 return map(nodemap.__getitem__, ancs)
389 401
390 402 def _buildrevgraph(self, a, b):
391 403 """Builds a numeric revision graph for the given two nodes.
392 404 Returns a node->rev map and a rev->[revs] parent function.
393 405 """
394 406 amap = self.ancestormap(a)
395 407 bmap = self.ancestormap(b)
396 408
397 409 # Union the two maps
398 410 parentsmap = collections.defaultdict(list)
399 411 allparents = set()
400 412 for mapping in (amap, bmap):
401 413 for node, pdata in mapping.iteritems():
402 414 parents = parentsmap[node]
403 415 p1, p2, linknode, copyfrom = pdata
404 416 # Don't follow renames (copyfrom).
405 417 # remotefilectx.ancestor does that.
406 418 if p1 != nullid and not copyfrom:
407 419 parents.append(p1)
408 420 allparents.add(p1)
409 421 if p2 != nullid:
410 422 parents.append(p2)
411 423 allparents.add(p2)
412 424
413 425 # Breadth first traversal to build linkrev graph
414 426 parentrevs = collections.defaultdict(list)
415 427 revmap = {}
416 428 queue = collections.deque(((None, n) for n in parentsmap
417 429 if n not in allparents))
418 430 while queue:
419 431 prevrev, current = queue.pop()
420 432 if current in revmap:
421 433 if prevrev:
422 434 parentrevs[prevrev].append(revmap[current])
423 435 continue
424 436
425 437 # Assign linkrevs in reverse order, so start at
426 438 # len(parentsmap) and work backwards.
427 439 currentrev = len(parentsmap) - len(revmap) - 1
428 440 revmap[current] = currentrev
429 441
430 442 if prevrev:
431 443 parentrevs[prevrev].append(currentrev)
432 444
433 445 for parent in parentsmap.get(current):
434 446 queue.appendleft((currentrev, parent))
435 447
436 448 return revmap, parentrevs.__getitem__
437 449
438 450 def strip(self, minlink, transaction):
439 451 pass
440 452
441 453 # misc unused things
442 454 def files(self):
443 455 return []
444 456
445 457 def checksize(self):
446 458 return 0, 0
@@ -1,2639 +1,2650 b''
1 1 # revlog.py - storage back-end for mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 """Storage back-end for Mercurial.
9 9
10 10 This provides efficient delta storage with O(1) retrieve and append
11 11 and O(changes) merge between branches.
12 12 """
13 13
14 14 from __future__ import absolute_import
15 15
16 16 import collections
17 17 import contextlib
18 18 import errno
19 19 import io
20 20 import os
21 21 import struct
22 22 import zlib
23 23
24 24 # import stuff from node for others to import from revlog
25 25 from .node import (
26 26 bin,
27 27 hex,
28 28 nullhex,
29 29 nullid,
30 30 nullrev,
31 31 short,
32 32 wdirfilenodeids,
33 33 wdirhex,
34 34 wdirid,
35 35 wdirrev,
36 36 )
37 37 from .i18n import _
38 38 from .revlogutils.constants import (
39 39 FLAG_GENERALDELTA,
40 40 FLAG_INLINE_DATA,
41 41 REVLOGV0,
42 42 REVLOGV1,
43 43 REVLOGV1_FLAGS,
44 44 REVLOGV2,
45 45 REVLOGV2_FLAGS,
46 46 REVLOG_DEFAULT_FLAGS,
47 47 REVLOG_DEFAULT_FORMAT,
48 48 REVLOG_DEFAULT_VERSION,
49 49 )
50 50 from .revlogutils.flagutil import (
51 51 REVIDX_DEFAULT_FLAGS,
52 52 REVIDX_ELLIPSIS,
53 53 REVIDX_EXTSTORED,
54 54 REVIDX_FLAGS_ORDER,
55 55 REVIDX_ISCENSORED,
56 56 REVIDX_RAWTEXT_CHANGING_FLAGS,
57 57 )
58 58 from .thirdparty import (
59 59 attr,
60 60 )
61 61 from . import (
62 62 ancestor,
63 63 dagop,
64 64 error,
65 65 mdiff,
66 66 policy,
67 67 pycompat,
68 68 templatefilters,
69 69 util,
70 70 )
71 71 from .interfaces import (
72 72 repository,
73 73 util as interfaceutil,
74 74 )
75 75 from .revlogutils import (
76 76 deltas as deltautil,
77 77 flagutil,
78 78 )
79 79 from .utils import (
80 80 storageutil,
81 81 stringutil,
82 82 )
83 83
84 84 # blanked usage of all the name to prevent pyflakes constraints
85 85 # We need these name available in the module for extensions.
86 86 REVLOGV0
87 87 REVLOGV1
88 88 REVLOGV2
89 89 FLAG_INLINE_DATA
90 90 FLAG_GENERALDELTA
91 91 REVLOG_DEFAULT_FLAGS
92 92 REVLOG_DEFAULT_FORMAT
93 93 REVLOG_DEFAULT_VERSION
94 94 REVLOGV1_FLAGS
95 95 REVLOGV2_FLAGS
96 96 REVIDX_ISCENSORED
97 97 REVIDX_ELLIPSIS
98 98 REVIDX_EXTSTORED
99 99 REVIDX_DEFAULT_FLAGS
100 100 REVIDX_FLAGS_ORDER
101 101 REVIDX_RAWTEXT_CHANGING_FLAGS
102 102
103 103 parsers = policy.importmod(r'parsers')
104 104 rustancestor = policy.importrust(r'ancestor')
105 105 rustdagop = policy.importrust(r'dagop')
106 106
107 107 # Aliased for performance.
108 108 _zlibdecompress = zlib.decompress
109 109
110 110 # max size of revlog with inline data
111 111 _maxinline = 131072
112 112 _chunksize = 1048576
113 113
114 114 # Flag processors for REVIDX_ELLIPSIS.
115 115 def ellipsisreadprocessor(rl, text):
116 116 return text, False, {}
117 117
118 118 def ellipsiswriteprocessor(rl, text, sidedata):
119 119 return text, False
120 120
121 121 def ellipsisrawprocessor(rl, text):
122 122 return False
123 123
124 124 ellipsisprocessor = (
125 125 ellipsisreadprocessor,
126 126 ellipsiswriteprocessor,
127 127 ellipsisrawprocessor,
128 128 )
129 129
130 130 def getoffset(q):
131 131 return int(q >> 16)
132 132
133 133 def gettype(q):
134 134 return int(q & 0xFFFF)
135 135
136 136 def offset_type(offset, type):
137 137 if (type & ~flagutil.REVIDX_KNOWN_FLAGS) != 0:
138 138 raise ValueError('unknown revlog index flags')
139 139 return int(int(offset) << 16 | type)
140 140
141 141 @attr.s(slots=True, frozen=True)
142 142 class _revisioninfo(object):
143 143 """Information about a revision that allows building its fulltext
144 144 node: expected hash of the revision
145 145 p1, p2: parent revs of the revision
146 146 btext: built text cache consisting of a one-element list
147 147 cachedelta: (baserev, uncompressed_delta) or None
148 148 flags: flags associated to the revision storage
149 149
150 150 One of btext[0] or cachedelta must be set.
151 151 """
152 152 node = attr.ib()
153 153 p1 = attr.ib()
154 154 p2 = attr.ib()
155 155 btext = attr.ib()
156 156 textlen = attr.ib()
157 157 cachedelta = attr.ib()
158 158 flags = attr.ib()
159 159
160 160 @interfaceutil.implementer(repository.irevisiondelta)
161 161 @attr.s(slots=True)
162 162 class revlogrevisiondelta(object):
163 163 node = attr.ib()
164 164 p1node = attr.ib()
165 165 p2node = attr.ib()
166 166 basenode = attr.ib()
167 167 flags = attr.ib()
168 168 baserevisionsize = attr.ib()
169 169 revision = attr.ib()
170 170 delta = attr.ib()
171 171 linknode = attr.ib(default=None)
172 172
173 173 @interfaceutil.implementer(repository.iverifyproblem)
174 174 @attr.s(frozen=True)
175 175 class revlogproblem(object):
176 176 warning = attr.ib(default=None)
177 177 error = attr.ib(default=None)
178 178 node = attr.ib(default=None)
179 179
180 180 # index v0:
181 181 # 4 bytes: offset
182 182 # 4 bytes: compressed length
183 183 # 4 bytes: base rev
184 184 # 4 bytes: link rev
185 185 # 20 bytes: parent 1 nodeid
186 186 # 20 bytes: parent 2 nodeid
187 187 # 20 bytes: nodeid
188 188 indexformatv0 = struct.Struct(">4l20s20s20s")
189 189 indexformatv0_pack = indexformatv0.pack
190 190 indexformatv0_unpack = indexformatv0.unpack
191 191
192 192 class revlogoldindex(list):
193 193 def __getitem__(self, i):
194 194 if i == -1:
195 195 return (0, 0, 0, -1, -1, -1, -1, nullid)
196 196 return list.__getitem__(self, i)
197 197
198 198 class revlogoldio(object):
199 199 def __init__(self):
200 200 self.size = indexformatv0.size
201 201
202 202 def parseindex(self, data, inline):
203 203 s = self.size
204 204 index = []
205 205 nodemap = {nullid: nullrev}
206 206 n = off = 0
207 207 l = len(data)
208 208 while off + s <= l:
209 209 cur = data[off:off + s]
210 210 off += s
211 211 e = indexformatv0_unpack(cur)
212 212 # transform to revlogv1 format
213 213 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
214 214 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
215 215 index.append(e2)
216 216 nodemap[e[6]] = n
217 217 n += 1
218 218
219 219 return revlogoldindex(index), nodemap, None
220 220
221 221 def packentry(self, entry, node, version, rev):
222 222 if gettype(entry[0]):
223 223 raise error.RevlogError(_('index entry flags need revlog '
224 224 'version 1'))
225 225 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
226 226 node(entry[5]), node(entry[6]), entry[7])
227 227 return indexformatv0_pack(*e2)
228 228
229 229 # index ng:
230 230 # 6 bytes: offset
231 231 # 2 bytes: flags
232 232 # 4 bytes: compressed length
233 233 # 4 bytes: uncompressed length
234 234 # 4 bytes: base rev
235 235 # 4 bytes: link rev
236 236 # 4 bytes: parent 1 rev
237 237 # 4 bytes: parent 2 rev
238 238 # 32 bytes: nodeid
239 239 indexformatng = struct.Struct(">Qiiiiii20s12x")
240 240 indexformatng_pack = indexformatng.pack
241 241 versionformat = struct.Struct(">I")
242 242 versionformat_pack = versionformat.pack
243 243 versionformat_unpack = versionformat.unpack
244 244
245 245 # corresponds to uncompressed length of indexformatng (2 gigs, 4-byte
246 246 # signed integer)
247 247 _maxentrysize = 0x7fffffff
248 248
249 249 class revlogio(object):
250 250 def __init__(self):
251 251 self.size = indexformatng.size
252 252
253 253 def parseindex(self, data, inline):
254 254 # call the C implementation to parse the index data
255 255 index, cache = parsers.parse_index2(data, inline)
256 256 return index, getattr(index, 'nodemap', None), cache
257 257
258 258 def packentry(self, entry, node, version, rev):
259 259 p = indexformatng_pack(*entry)
260 260 if rev == 0:
261 261 p = versionformat_pack(version) + p[4:]
262 262 return p
263 263
264 264 class revlog(flagutil.flagprocessorsmixin):
265 265 """
266 266 the underlying revision storage object
267 267
268 268 A revlog consists of two parts, an index and the revision data.
269 269
270 270 The index is a file with a fixed record size containing
271 271 information on each revision, including its nodeid (hash), the
272 272 nodeids of its parents, the position and offset of its data within
273 273 the data file, and the revision it's based on. Finally, each entry
274 274 contains a linkrev entry that can serve as a pointer to external
275 275 data.
276 276
277 277 The revision data itself is a linear collection of data chunks.
278 278 Each chunk represents a revision and is usually represented as a
279 279 delta against the previous chunk. To bound lookup time, runs of
280 280 deltas are limited to about 2 times the length of the original
281 281 version data. This makes retrieval of a version proportional to
282 282 its size, or O(1) relative to the number of revisions.
283 283
284 284 Both pieces of the revlog are written to in an append-only
285 285 fashion, which means we never need to rewrite a file to insert or
286 286 remove data, and can use some simple techniques to avoid the need
287 287 for locking while reading.
288 288
289 289 If checkambig, indexfile is opened with checkambig=True at
290 290 writing, to avoid file stat ambiguity.
291 291
292 292 If mmaplargeindex is True, and an mmapindexthreshold is set, the
293 293 index will be mmapped rather than read if it is larger than the
294 294 configured threshold.
295 295
296 296 If censorable is True, the revlog can have censored revisions.
297 297
298 298 If `upperboundcomp` is not None, this is the expected maximal gain from
299 299 compression for the data content.
300 300 """
301 301 def __init__(self, opener, indexfile, datafile=None, checkambig=False,
302 302 mmaplargeindex=False, censorable=False,
303 303 upperboundcomp=None):
304 304 """
305 305 create a revlog object
306 306
307 307 opener is a function that abstracts the file opening operation
308 308 and can be used to implement COW semantics or the like.
309 309
310 310 """
311 311 self.upperboundcomp = upperboundcomp
312 312 self.indexfile = indexfile
313 313 self.datafile = datafile or (indexfile[:-2] + ".d")
314 314 self.opener = opener
315 315 # When True, indexfile is opened with checkambig=True at writing, to
316 316 # avoid file stat ambiguity.
317 317 self._checkambig = checkambig
318 318 self._mmaplargeindex = mmaplargeindex
319 319 self._censorable = censorable
320 320 # 3-tuple of (node, rev, text) for a raw revision.
321 321 self._revisioncache = None
322 322 # Maps rev to chain base rev.
323 323 self._chainbasecache = util.lrucachedict(100)
324 324 # 2-tuple of (offset, data) of raw data from the revlog at an offset.
325 325 self._chunkcache = (0, '')
326 326 # How much data to read and cache into the raw revlog data cache.
327 327 self._chunkcachesize = 65536
328 328 self._maxchainlen = None
329 329 self._deltabothparents = True
330 330 self.index = []
331 331 # Mapping of partial identifiers to full nodes.
332 332 self._pcache = {}
333 333 # Mapping of revision integer to full node.
334 334 self._nodecache = {nullid: nullrev}
335 335 self._nodepos = None
336 336 self._compengine = 'zlib'
337 337 self._compengineopts = {}
338 338 self._maxdeltachainspan = -1
339 339 self._withsparseread = False
340 340 self._sparserevlog = False
341 341 self._srdensitythreshold = 0.50
342 342 self._srmingapsize = 262144
343 343
344 344 # Make copy of flag processors so each revlog instance can support
345 345 # custom flags.
346 346 self._flagprocessors = dict(flagutil.flagprocessors)
347 347
348 348 # 2-tuple of file handles being used for active writing.
349 349 self._writinghandles = None
350 350
351 351 self._loadindex()
352 352
353 353 def _loadindex(self):
354 354 mmapindexthreshold = None
355 355 opts = getattr(self.opener, 'options', {}) or {}
356 356
357 357 if 'revlogv2' in opts:
358 358 newversionflags = REVLOGV2 | FLAG_INLINE_DATA
359 359 elif 'revlogv1' in opts:
360 360 newversionflags = REVLOGV1 | FLAG_INLINE_DATA
361 361 if 'generaldelta' in opts:
362 362 newversionflags |= FLAG_GENERALDELTA
363 363 elif getattr(self.opener, 'options', None) is not None:
364 364 # If options provided but no 'revlog*' found, the repository
365 365 # would have no 'requires' file in it, which means we have to
366 366 # stick to the old format.
367 367 newversionflags = REVLOGV0
368 368 else:
369 369 newversionflags = REVLOG_DEFAULT_VERSION
370 370
371 371 if 'chunkcachesize' in opts:
372 372 self._chunkcachesize = opts['chunkcachesize']
373 373 if 'maxchainlen' in opts:
374 374 self._maxchainlen = opts['maxchainlen']
375 375 if 'deltabothparents' in opts:
376 376 self._deltabothparents = opts['deltabothparents']
377 377 self._lazydelta = bool(opts.get('lazydelta', True))
378 378 self._lazydeltabase = False
379 379 if self._lazydelta:
380 380 self._lazydeltabase = bool(opts.get('lazydeltabase', False))
381 381 if 'compengine' in opts:
382 382 self._compengine = opts['compengine']
383 383 if 'zlib.level' in opts:
384 384 self._compengineopts['zlib.level'] = opts['zlib.level']
385 385 if 'zstd.level' in opts:
386 386 self._compengineopts['zstd.level'] = opts['zstd.level']
387 387 if 'maxdeltachainspan' in opts:
388 388 self._maxdeltachainspan = opts['maxdeltachainspan']
389 389 if self._mmaplargeindex and 'mmapindexthreshold' in opts:
390 390 mmapindexthreshold = opts['mmapindexthreshold']
391 391 self._sparserevlog = bool(opts.get('sparse-revlog', False))
392 392 withsparseread = bool(opts.get('with-sparse-read', False))
393 393 # sparse-revlog forces sparse-read
394 394 self._withsparseread = self._sparserevlog or withsparseread
395 395 if 'sparse-read-density-threshold' in opts:
396 396 self._srdensitythreshold = opts['sparse-read-density-threshold']
397 397 if 'sparse-read-min-gap-size' in opts:
398 398 self._srmingapsize = opts['sparse-read-min-gap-size']
399 399 if opts.get('enableellipsis'):
400 400 self._flagprocessors[REVIDX_ELLIPSIS] = ellipsisprocessor
401 401
402 402 # revlog v0 doesn't have flag processors
403 403 for flag, processor in opts.get(b'flagprocessors', {}).iteritems():
404 404 flagutil.insertflagprocessor(flag, processor, self._flagprocessors)
405 405
406 406 if self._chunkcachesize <= 0:
407 407 raise error.RevlogError(_('revlog chunk cache size %r is not '
408 408 'greater than 0') % self._chunkcachesize)
409 409 elif self._chunkcachesize & (self._chunkcachesize - 1):
410 410 raise error.RevlogError(_('revlog chunk cache size %r is not a '
411 411 'power of 2') % self._chunkcachesize)
412 412
413 413 indexdata = ''
414 414 self._initempty = True
415 415 try:
416 416 with self._indexfp() as f:
417 417 if (mmapindexthreshold is not None and
418 418 self.opener.fstat(f).st_size >= mmapindexthreshold):
419 419 # TODO: should .close() to release resources without
420 420 # relying on Python GC
421 421 indexdata = util.buffer(util.mmapread(f))
422 422 else:
423 423 indexdata = f.read()
424 424 if len(indexdata) > 0:
425 425 versionflags = versionformat_unpack(indexdata[:4])[0]
426 426 self._initempty = False
427 427 else:
428 428 versionflags = newversionflags
429 429 except IOError as inst:
430 430 if inst.errno != errno.ENOENT:
431 431 raise
432 432
433 433 versionflags = newversionflags
434 434
435 435 self.version = versionflags
436 436
437 437 flags = versionflags & ~0xFFFF
438 438 fmt = versionflags & 0xFFFF
439 439
440 440 if fmt == REVLOGV0:
441 441 if flags:
442 442 raise error.RevlogError(_('unknown flags (%#04x) in version %d '
443 443 'revlog %s') %
444 444 (flags >> 16, fmt, self.indexfile))
445 445
446 446 self._inline = False
447 447 self._generaldelta = False
448 448
449 449 elif fmt == REVLOGV1:
450 450 if flags & ~REVLOGV1_FLAGS:
451 451 raise error.RevlogError(_('unknown flags (%#04x) in version %d '
452 452 'revlog %s') %
453 453 (flags >> 16, fmt, self.indexfile))
454 454
455 455 self._inline = versionflags & FLAG_INLINE_DATA
456 456 self._generaldelta = versionflags & FLAG_GENERALDELTA
457 457
458 458 elif fmt == REVLOGV2:
459 459 if flags & ~REVLOGV2_FLAGS:
460 460 raise error.RevlogError(_('unknown flags (%#04x) in version %d '
461 461 'revlog %s') %
462 462 (flags >> 16, fmt, self.indexfile))
463 463
464 464 self._inline = versionflags & FLAG_INLINE_DATA
465 465 # generaldelta implied by version 2 revlogs.
466 466 self._generaldelta = True
467 467
468 468 else:
469 469 raise error.RevlogError(_('unknown version (%d) in revlog %s') %
470 470 (fmt, self.indexfile))
471 471 # sparse-revlog can't be on without general-delta (issue6056)
472 472 if not self._generaldelta:
473 473 self._sparserevlog = False
474 474
475 475 self._storedeltachains = True
476 476
477 477 self._io = revlogio()
478 478 if self.version == REVLOGV0:
479 479 self._io = revlogoldio()
480 480 try:
481 481 d = self._io.parseindex(indexdata, self._inline)
482 482 except (ValueError, IndexError):
483 483 raise error.RevlogError(_("index %s is corrupted") %
484 484 self.indexfile)
485 485 self.index, nodemap, self._chunkcache = d
486 486 if nodemap is not None:
487 487 self.nodemap = self._nodecache = nodemap
488 488 if not self._chunkcache:
489 489 self._chunkclear()
490 490 # revnum -> (chain-length, sum-delta-length)
491 491 self._chaininfocache = {}
492 492 # revlog header -> revlog compressor
493 493 self._decompressors = {}
494 494
495 495 @util.propertycache
496 496 def _compressor(self):
497 497 engine = util.compengines[self._compengine]
498 498 return engine.revlogcompressor(self._compengineopts)
499 499
500 500 def _indexfp(self, mode='r'):
501 501 """file object for the revlog's index file"""
502 502 args = {r'mode': mode}
503 503 if mode != 'r':
504 504 args[r'checkambig'] = self._checkambig
505 505 if mode == 'w':
506 506 args[r'atomictemp'] = True
507 507 return self.opener(self.indexfile, **args)
508 508
509 509 def _datafp(self, mode='r'):
510 510 """file object for the revlog's data file"""
511 511 return self.opener(self.datafile, mode=mode)
512 512
513 513 @contextlib.contextmanager
514 514 def _datareadfp(self, existingfp=None):
515 515 """file object suitable to read data"""
516 516 # Use explicit file handle, if given.
517 517 if existingfp is not None:
518 518 yield existingfp
519 519
520 520 # Use a file handle being actively used for writes, if available.
521 521 # There is some danger to doing this because reads will seek the
522 522 # file. However, _writeentry() performs a SEEK_END before all writes,
523 523 # so we should be safe.
524 524 elif self._writinghandles:
525 525 if self._inline:
526 526 yield self._writinghandles[0]
527 527 else:
528 528 yield self._writinghandles[1]
529 529
530 530 # Otherwise open a new file handle.
531 531 else:
532 532 if self._inline:
533 533 func = self._indexfp
534 534 else:
535 535 func = self._datafp
536 536 with func() as fp:
537 537 yield fp
538 538
539 539 def tip(self):
540 540 return self.node(len(self.index) - 1)
541 541 def __contains__(self, rev):
542 542 return 0 <= rev < len(self)
543 543 def __len__(self):
544 544 return len(self.index)
545 545 def __iter__(self):
546 546 return iter(pycompat.xrange(len(self)))
547 547 def revs(self, start=0, stop=None):
548 548 """iterate over all rev in this revlog (from start to stop)"""
549 549 return storageutil.iterrevs(len(self), start=start, stop=stop)
550 550
551 551 @util.propertycache
552 552 def nodemap(self):
553 553 if self.index:
554 554 # populate mapping down to the initial node
555 555 node0 = self.index[0][7] # get around changelog filtering
556 556 self.rev(node0)
557 557 return self._nodecache
558 558
559 559 def hasnode(self, node):
560 560 try:
561 561 self.rev(node)
562 562 return True
563 563 except KeyError:
564 564 return False
565 565
566 566 def candelta(self, baserev, rev):
567 567 """whether two revisions (baserev, rev) can be delta-ed or not"""
568 568 # Disable delta if either rev requires a content-changing flag
569 569 # processor (ex. LFS). This is because such flag processor can alter
570 570 # the rawtext content that the delta will be based on, and two clients
571 571 # could have a same revlog node with different flags (i.e. different
572 572 # rawtext contents) and the delta could be incompatible.
573 573 if ((self.flags(baserev) & REVIDX_RAWTEXT_CHANGING_FLAGS)
574 574 or (self.flags(rev) & REVIDX_RAWTEXT_CHANGING_FLAGS)):
575 575 return False
576 576 return True
577 577
578 578 def clearcaches(self):
579 579 self._revisioncache = None
580 580 self._chainbasecache.clear()
581 581 self._chunkcache = (0, '')
582 582 self._pcache = {}
583 583
584 584 try:
585 585 # If we are using the native C version, you are in a fun case
586 586 # where self.index, self.nodemap and self._nodecaches is the same
587 587 # object.
588 588 self._nodecache.clearcaches()
589 589 except AttributeError:
590 590 self._nodecache = {nullid: nullrev}
591 591 self._nodepos = None
592 592
593 593 def rev(self, node):
594 594 try:
595 595 return self._nodecache[node]
596 596 except TypeError:
597 597 raise
598 598 except error.RevlogError:
599 599 # parsers.c radix tree lookup failed
600 600 if node == wdirid or node in wdirfilenodeids:
601 601 raise error.WdirUnsupported
602 602 raise error.LookupError(node, self.indexfile, _('no node'))
603 603 except KeyError:
604 604 # pure python cache lookup failed
605 605 n = self._nodecache
606 606 i = self.index
607 607 p = self._nodepos
608 608 if p is None:
609 609 p = len(i) - 1
610 610 else:
611 611 assert p < len(i)
612 612 for r in pycompat.xrange(p, -1, -1):
613 613 v = i[r][7]
614 614 n[v] = r
615 615 if v == node:
616 616 self._nodepos = r - 1
617 617 return r
618 618 if node == wdirid or node in wdirfilenodeids:
619 619 raise error.WdirUnsupported
620 620 raise error.LookupError(node, self.indexfile, _('no node'))
621 621
622 622 # Accessors for index entries.
623 623
624 624 # First tuple entry is 8 bytes. First 6 bytes are offset. Last 2 bytes
625 625 # are flags.
626 626 def start(self, rev):
627 627 return int(self.index[rev][0] >> 16)
628 628
629 629 def flags(self, rev):
630 630 return self.index[rev][0] & 0xFFFF
631 631
632 632 def length(self, rev):
633 633 return self.index[rev][1]
634 634
635 635 def rawsize(self, rev):
636 636 """return the length of the uncompressed text for a given revision"""
637 637 l = self.index[rev][2]
638 638 if l >= 0:
639 639 return l
640 640
641 641 t = self.rawdata(rev)
642 642 return len(t)
643 643
644 644 def size(self, rev):
645 645 """length of non-raw text (processed by a "read" flag processor)"""
646 646 # fast path: if no "read" flag processor could change the content,
647 647 # size is rawsize. note: ELLIPSIS is known to not change the content.
648 648 flags = self.flags(rev)
649 649 if flags & (flagutil.REVIDX_KNOWN_FLAGS ^ REVIDX_ELLIPSIS) == 0:
650 650 return self.rawsize(rev)
651 651
652 652 return len(self.revision(rev, raw=False))
653 653
654 654 def chainbase(self, rev):
655 655 base = self._chainbasecache.get(rev)
656 656 if base is not None:
657 657 return base
658 658
659 659 index = self.index
660 660 iterrev = rev
661 661 base = index[iterrev][3]
662 662 while base != iterrev:
663 663 iterrev = base
664 664 base = index[iterrev][3]
665 665
666 666 self._chainbasecache[rev] = base
667 667 return base
668 668
669 669 def linkrev(self, rev):
670 670 return self.index[rev][4]
671 671
672 672 def parentrevs(self, rev):
673 673 try:
674 674 entry = self.index[rev]
675 675 except IndexError:
676 676 if rev == wdirrev:
677 677 raise error.WdirUnsupported
678 678 raise
679 679
680 680 return entry[5], entry[6]
681 681
682 682 # fast parentrevs(rev) where rev isn't filtered
683 683 _uncheckedparentrevs = parentrevs
684 684
685 685 def node(self, rev):
686 686 try:
687 687 return self.index[rev][7]
688 688 except IndexError:
689 689 if rev == wdirrev:
690 690 raise error.WdirUnsupported
691 691 raise
692 692
693 693 # Derived from index values.
694 694
695 695 def end(self, rev):
696 696 return self.start(rev) + self.length(rev)
697 697
698 698 def parents(self, node):
699 699 i = self.index
700 700 d = i[self.rev(node)]
701 701 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
702 702
703 703 def chainlen(self, rev):
704 704 return self._chaininfo(rev)[0]
705 705
706 706 def _chaininfo(self, rev):
707 707 chaininfocache = self._chaininfocache
708 708 if rev in chaininfocache:
709 709 return chaininfocache[rev]
710 710 index = self.index
711 711 generaldelta = self._generaldelta
712 712 iterrev = rev
713 713 e = index[iterrev]
714 714 clen = 0
715 715 compresseddeltalen = 0
716 716 while iterrev != e[3]:
717 717 clen += 1
718 718 compresseddeltalen += e[1]
719 719 if generaldelta:
720 720 iterrev = e[3]
721 721 else:
722 722 iterrev -= 1
723 723 if iterrev in chaininfocache:
724 724 t = chaininfocache[iterrev]
725 725 clen += t[0]
726 726 compresseddeltalen += t[1]
727 727 break
728 728 e = index[iterrev]
729 729 else:
730 730 # Add text length of base since decompressing that also takes
731 731 # work. For cache hits the length is already included.
732 732 compresseddeltalen += e[1]
733 733 r = (clen, compresseddeltalen)
734 734 chaininfocache[rev] = r
735 735 return r
736 736
737 737 def _deltachain(self, rev, stoprev=None):
738 738 """Obtain the delta chain for a revision.
739 739
740 740 ``stoprev`` specifies a revision to stop at. If not specified, we
741 741 stop at the base of the chain.
742 742
743 743 Returns a 2-tuple of (chain, stopped) where ``chain`` is a list of
744 744 revs in ascending order and ``stopped`` is a bool indicating whether
745 745 ``stoprev`` was hit.
746 746 """
747 747 # Try C implementation.
748 748 try:
749 749 return self.index.deltachain(rev, stoprev, self._generaldelta)
750 750 except AttributeError:
751 751 pass
752 752
753 753 chain = []
754 754
755 755 # Alias to prevent attribute lookup in tight loop.
756 756 index = self.index
757 757 generaldelta = self._generaldelta
758 758
759 759 iterrev = rev
760 760 e = index[iterrev]
761 761 while iterrev != e[3] and iterrev != stoprev:
762 762 chain.append(iterrev)
763 763 if generaldelta:
764 764 iterrev = e[3]
765 765 else:
766 766 iterrev -= 1
767 767 e = index[iterrev]
768 768
769 769 if iterrev == stoprev:
770 770 stopped = True
771 771 else:
772 772 chain.append(iterrev)
773 773 stopped = False
774 774
775 775 chain.reverse()
776 776 return chain, stopped
777 777
778 778 def ancestors(self, revs, stoprev=0, inclusive=False):
779 779 """Generate the ancestors of 'revs' in reverse revision order.
780 780 Does not generate revs lower than stoprev.
781 781
782 782 See the documentation for ancestor.lazyancestors for more details."""
783 783
784 784 # first, make sure start revisions aren't filtered
785 785 revs = list(revs)
786 786 checkrev = self.node
787 787 for r in revs:
788 788 checkrev(r)
789 789 # and we're sure ancestors aren't filtered as well
790 790
791 791 if rustancestor is not None:
792 792 lazyancestors = rustancestor.LazyAncestors
793 793 arg = self.index
794 794 elif util.safehasattr(parsers, 'rustlazyancestors'):
795 795 lazyancestors = ancestor.rustlazyancestors
796 796 arg = self.index
797 797 else:
798 798 lazyancestors = ancestor.lazyancestors
799 799 arg = self._uncheckedparentrevs
800 800 return lazyancestors(arg, revs, stoprev=stoprev, inclusive=inclusive)
801 801
802 802 def descendants(self, revs):
803 803 return dagop.descendantrevs(revs, self.revs, self.parentrevs)
804 804
805 805 def findcommonmissing(self, common=None, heads=None):
806 806 """Return a tuple of the ancestors of common and the ancestors of heads
807 807 that are not ancestors of common. In revset terminology, we return the
808 808 tuple:
809 809
810 810 ::common, (::heads) - (::common)
811 811
812 812 The list is sorted by revision number, meaning it is
813 813 topologically sorted.
814 814
815 815 'heads' and 'common' are both lists of node IDs. If heads is
816 816 not supplied, uses all of the revlog's heads. If common is not
817 817 supplied, uses nullid."""
818 818 if common is None:
819 819 common = [nullid]
820 820 if heads is None:
821 821 heads = self.heads()
822 822
823 823 common = [self.rev(n) for n in common]
824 824 heads = [self.rev(n) for n in heads]
825 825
826 826 # we want the ancestors, but inclusive
827 827 class lazyset(object):
828 828 def __init__(self, lazyvalues):
829 829 self.addedvalues = set()
830 830 self.lazyvalues = lazyvalues
831 831
832 832 def __contains__(self, value):
833 833 return value in self.addedvalues or value in self.lazyvalues
834 834
835 835 def __iter__(self):
836 836 added = self.addedvalues
837 837 for r in added:
838 838 yield r
839 839 for r in self.lazyvalues:
840 840 if not r in added:
841 841 yield r
842 842
843 843 def add(self, value):
844 844 self.addedvalues.add(value)
845 845
846 846 def update(self, values):
847 847 self.addedvalues.update(values)
848 848
849 849 has = lazyset(self.ancestors(common))
850 850 has.add(nullrev)
851 851 has.update(common)
852 852
853 853 # take all ancestors from heads that aren't in has
854 854 missing = set()
855 855 visit = collections.deque(r for r in heads if r not in has)
856 856 while visit:
857 857 r = visit.popleft()
858 858 if r in missing:
859 859 continue
860 860 else:
861 861 missing.add(r)
862 862 for p in self.parentrevs(r):
863 863 if p not in has:
864 864 visit.append(p)
865 865 missing = list(missing)
866 866 missing.sort()
867 867 return has, [self.node(miss) for miss in missing]
868 868
869 869 def incrementalmissingrevs(self, common=None):
870 870 """Return an object that can be used to incrementally compute the
871 871 revision numbers of the ancestors of arbitrary sets that are not
872 872 ancestors of common. This is an ancestor.incrementalmissingancestors
873 873 object.
874 874
875 875 'common' is a list of revision numbers. If common is not supplied, uses
876 876 nullrev.
877 877 """
878 878 if common is None:
879 879 common = [nullrev]
880 880
881 881 if rustancestor is not None:
882 882 return rustancestor.MissingAncestors(self.index, common)
883 883 return ancestor.incrementalmissingancestors(self.parentrevs, common)
884 884
885 885 def findmissingrevs(self, common=None, heads=None):
886 886 """Return the revision numbers of the ancestors of heads that
887 887 are not ancestors of common.
888 888
889 889 More specifically, return a list of revision numbers corresponding to
890 890 nodes N such that every N satisfies the following constraints:
891 891
892 892 1. N is an ancestor of some node in 'heads'
893 893 2. N is not an ancestor of any node in 'common'
894 894
895 895 The list is sorted by revision number, meaning it is
896 896 topologically sorted.
897 897
898 898 'heads' and 'common' are both lists of revision numbers. If heads is
899 899 not supplied, uses all of the revlog's heads. If common is not
900 900 supplied, uses nullid."""
901 901 if common is None:
902 902 common = [nullrev]
903 903 if heads is None:
904 904 heads = self.headrevs()
905 905
906 906 inc = self.incrementalmissingrevs(common=common)
907 907 return inc.missingancestors(heads)
908 908
909 909 def findmissing(self, common=None, heads=None):
910 910 """Return the ancestors of heads that are not ancestors of common.
911 911
912 912 More specifically, return a list of nodes N such that every N
913 913 satisfies the following constraints:
914 914
915 915 1. N is an ancestor of some node in 'heads'
916 916 2. N is not an ancestor of any node in 'common'
917 917
918 918 The list is sorted by revision number, meaning it is
919 919 topologically sorted.
920 920
921 921 'heads' and 'common' are both lists of node IDs. If heads is
922 922 not supplied, uses all of the revlog's heads. If common is not
923 923 supplied, uses nullid."""
924 924 if common is None:
925 925 common = [nullid]
926 926 if heads is None:
927 927 heads = self.heads()
928 928
929 929 common = [self.rev(n) for n in common]
930 930 heads = [self.rev(n) for n in heads]
931 931
932 932 inc = self.incrementalmissingrevs(common=common)
933 933 return [self.node(r) for r in inc.missingancestors(heads)]
934 934
935 935 def nodesbetween(self, roots=None, heads=None):
936 936 """Return a topological path from 'roots' to 'heads'.
937 937
938 938 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
939 939 topologically sorted list of all nodes N that satisfy both of
940 940 these constraints:
941 941
942 942 1. N is a descendant of some node in 'roots'
943 943 2. N is an ancestor of some node in 'heads'
944 944
945 945 Every node is considered to be both a descendant and an ancestor
946 946 of itself, so every reachable node in 'roots' and 'heads' will be
947 947 included in 'nodes'.
948 948
949 949 'outroots' is the list of reachable nodes in 'roots', i.e., the
950 950 subset of 'roots' that is returned in 'nodes'. Likewise,
951 951 'outheads' is the subset of 'heads' that is also in 'nodes'.
952 952
953 953 'roots' and 'heads' are both lists of node IDs. If 'roots' is
954 954 unspecified, uses nullid as the only root. If 'heads' is
955 955 unspecified, uses list of all of the revlog's heads."""
956 956 nonodes = ([], [], [])
957 957 if roots is not None:
958 958 roots = list(roots)
959 959 if not roots:
960 960 return nonodes
961 961 lowestrev = min([self.rev(n) for n in roots])
962 962 else:
963 963 roots = [nullid] # Everybody's a descendant of nullid
964 964 lowestrev = nullrev
965 965 if (lowestrev == nullrev) and (heads is None):
966 966 # We want _all_ the nodes!
967 967 return ([self.node(r) for r in self], [nullid], list(self.heads()))
968 968 if heads is None:
969 969 # All nodes are ancestors, so the latest ancestor is the last
970 970 # node.
971 971 highestrev = len(self) - 1
972 972 # Set ancestors to None to signal that every node is an ancestor.
973 973 ancestors = None
974 974 # Set heads to an empty dictionary for later discovery of heads
975 975 heads = {}
976 976 else:
977 977 heads = list(heads)
978 978 if not heads:
979 979 return nonodes
980 980 ancestors = set()
981 981 # Turn heads into a dictionary so we can remove 'fake' heads.
982 982 # Also, later we will be using it to filter out the heads we can't
983 983 # find from roots.
984 984 heads = dict.fromkeys(heads, False)
985 985 # Start at the top and keep marking parents until we're done.
986 986 nodestotag = set(heads)
987 987 # Remember where the top was so we can use it as a limit later.
988 988 highestrev = max([self.rev(n) for n in nodestotag])
989 989 while nodestotag:
990 990 # grab a node to tag
991 991 n = nodestotag.pop()
992 992 # Never tag nullid
993 993 if n == nullid:
994 994 continue
995 995 # A node's revision number represents its place in a
996 996 # topologically sorted list of nodes.
997 997 r = self.rev(n)
998 998 if r >= lowestrev:
999 999 if n not in ancestors:
1000 1000 # If we are possibly a descendant of one of the roots
1001 1001 # and we haven't already been marked as an ancestor
1002 1002 ancestors.add(n) # Mark as ancestor
1003 1003 # Add non-nullid parents to list of nodes to tag.
1004 1004 nodestotag.update([p for p in self.parents(n) if
1005 1005 p != nullid])
1006 1006 elif n in heads: # We've seen it before, is it a fake head?
1007 1007 # So it is, real heads should not be the ancestors of
1008 1008 # any other heads.
1009 1009 heads.pop(n)
1010 1010 if not ancestors:
1011 1011 return nonodes
1012 1012 # Now that we have our set of ancestors, we want to remove any
1013 1013 # roots that are not ancestors.
1014 1014
1015 1015 # If one of the roots was nullid, everything is included anyway.
1016 1016 if lowestrev > nullrev:
1017 1017 # But, since we weren't, let's recompute the lowest rev to not
1018 1018 # include roots that aren't ancestors.
1019 1019
1020 1020 # Filter out roots that aren't ancestors of heads
1021 1021 roots = [root for root in roots if root in ancestors]
1022 1022 # Recompute the lowest revision
1023 1023 if roots:
1024 1024 lowestrev = min([self.rev(root) for root in roots])
1025 1025 else:
1026 1026 # No more roots? Return empty list
1027 1027 return nonodes
1028 1028 else:
1029 1029 # We are descending from nullid, and don't need to care about
1030 1030 # any other roots.
1031 1031 lowestrev = nullrev
1032 1032 roots = [nullid]
1033 1033 # Transform our roots list into a set.
1034 1034 descendants = set(roots)
1035 1035 # Also, keep the original roots so we can filter out roots that aren't
1036 1036 # 'real' roots (i.e. are descended from other roots).
1037 1037 roots = descendants.copy()
1038 1038 # Our topologically sorted list of output nodes.
1039 1039 orderedout = []
1040 1040 # Don't start at nullid since we don't want nullid in our output list,
1041 1041 # and if nullid shows up in descendants, empty parents will look like
1042 1042 # they're descendants.
1043 1043 for r in self.revs(start=max(lowestrev, 0), stop=highestrev + 1):
1044 1044 n = self.node(r)
1045 1045 isdescendant = False
1046 1046 if lowestrev == nullrev: # Everybody is a descendant of nullid
1047 1047 isdescendant = True
1048 1048 elif n in descendants:
1049 1049 # n is already a descendant
1050 1050 isdescendant = True
1051 1051 # This check only needs to be done here because all the roots
1052 1052 # will start being marked is descendants before the loop.
1053 1053 if n in roots:
1054 1054 # If n was a root, check if it's a 'real' root.
1055 1055 p = tuple(self.parents(n))
1056 1056 # If any of its parents are descendants, it's not a root.
1057 1057 if (p[0] in descendants) or (p[1] in descendants):
1058 1058 roots.remove(n)
1059 1059 else:
1060 1060 p = tuple(self.parents(n))
1061 1061 # A node is a descendant if either of its parents are
1062 1062 # descendants. (We seeded the dependents list with the roots
1063 1063 # up there, remember?)
1064 1064 if (p[0] in descendants) or (p[1] in descendants):
1065 1065 descendants.add(n)
1066 1066 isdescendant = True
1067 1067 if isdescendant and ((ancestors is None) or (n in ancestors)):
1068 1068 # Only include nodes that are both descendants and ancestors.
1069 1069 orderedout.append(n)
1070 1070 if (ancestors is not None) and (n in heads):
1071 1071 # We're trying to figure out which heads are reachable
1072 1072 # from roots.
1073 1073 # Mark this head as having been reached
1074 1074 heads[n] = True
1075 1075 elif ancestors is None:
1076 1076 # Otherwise, we're trying to discover the heads.
1077 1077 # Assume this is a head because if it isn't, the next step
1078 1078 # will eventually remove it.
1079 1079 heads[n] = True
1080 1080 # But, obviously its parents aren't.
1081 1081 for p in self.parents(n):
1082 1082 heads.pop(p, None)
1083 1083 heads = [head for head, flag in heads.iteritems() if flag]
1084 1084 roots = list(roots)
1085 1085 assert orderedout
1086 1086 assert roots
1087 1087 assert heads
1088 1088 return (orderedout, roots, heads)
1089 1089
1090 1090 def headrevs(self, revs=None):
1091 1091 if revs is None:
1092 1092 try:
1093 1093 return self.index.headrevs()
1094 1094 except AttributeError:
1095 1095 return self._headrevs()
1096 1096 if rustdagop is not None:
1097 1097 return rustdagop.headrevs(self.index, revs)
1098 1098 return dagop.headrevs(revs, self._uncheckedparentrevs)
1099 1099
1100 1100 def computephases(self, roots):
1101 1101 return self.index.computephasesmapsets(roots)
1102 1102
1103 1103 def _headrevs(self):
1104 1104 count = len(self)
1105 1105 if not count:
1106 1106 return [nullrev]
1107 1107 # we won't iter over filtered rev so nobody is a head at start
1108 1108 ishead = [0] * (count + 1)
1109 1109 index = self.index
1110 1110 for r in self:
1111 1111 ishead[r] = 1 # I may be an head
1112 1112 e = index[r]
1113 1113 ishead[e[5]] = ishead[e[6]] = 0 # my parent are not
1114 1114 return [r for r, val in enumerate(ishead) if val]
1115 1115
1116 1116 def heads(self, start=None, stop=None):
1117 1117 """return the list of all nodes that have no children
1118 1118
1119 1119 if start is specified, only heads that are descendants of
1120 1120 start will be returned
1121 1121 if stop is specified, it will consider all the revs from stop
1122 1122 as if they had no children
1123 1123 """
1124 1124 if start is None and stop is None:
1125 1125 if not len(self):
1126 1126 return [nullid]
1127 1127 return [self.node(r) for r in self.headrevs()]
1128 1128
1129 1129 if start is None:
1130 1130 start = nullrev
1131 1131 else:
1132 1132 start = self.rev(start)
1133 1133
1134 1134 stoprevs = set(self.rev(n) for n in stop or [])
1135 1135
1136 1136 revs = dagop.headrevssubset(self.revs, self.parentrevs, startrev=start,
1137 1137 stoprevs=stoprevs)
1138 1138
1139 1139 return [self.node(rev) for rev in revs]
1140 1140
1141 1141 def children(self, node):
1142 1142 """find the children of a given node"""
1143 1143 c = []
1144 1144 p = self.rev(node)
1145 1145 for r in self.revs(start=p + 1):
1146 1146 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
1147 1147 if prevs:
1148 1148 for pr in prevs:
1149 1149 if pr == p:
1150 1150 c.append(self.node(r))
1151 1151 elif p == nullrev:
1152 1152 c.append(self.node(r))
1153 1153 return c
1154 1154
1155 1155 def commonancestorsheads(self, a, b):
1156 1156 """calculate all the heads of the common ancestors of nodes a and b"""
1157 1157 a, b = self.rev(a), self.rev(b)
1158 1158 ancs = self._commonancestorsheads(a, b)
1159 1159 return pycompat.maplist(self.node, ancs)
1160 1160
1161 1161 def _commonancestorsheads(self, *revs):
1162 1162 """calculate all the heads of the common ancestors of revs"""
1163 1163 try:
1164 1164 ancs = self.index.commonancestorsheads(*revs)
1165 1165 except (AttributeError, OverflowError): # C implementation failed
1166 1166 ancs = ancestor.commonancestorsheads(self.parentrevs, *revs)
1167 1167 return ancs
1168 1168
1169 1169 def isancestor(self, a, b):
1170 1170 """return True if node a is an ancestor of node b
1171 1171
1172 1172 A revision is considered an ancestor of itself."""
1173 1173 a, b = self.rev(a), self.rev(b)
1174 1174 return self.isancestorrev(a, b)
1175 1175
1176 1176 def isancestorrev(self, a, b):
1177 1177 """return True if revision a is an ancestor of revision b
1178 1178
1179 1179 A revision is considered an ancestor of itself.
1180 1180
1181 1181 The implementation of this is trivial but the use of
1182 1182 reachableroots is not."""
1183 1183 if a == nullrev:
1184 1184 return True
1185 1185 elif a == b:
1186 1186 return True
1187 1187 elif a > b:
1188 1188 return False
1189 1189 return bool(self.reachableroots(a, [b], [a], includepath=False))
1190 1190
1191 1191 def reachableroots(self, minroot, heads, roots, includepath=False):
1192 1192 """return (heads(::<roots> and <roots>::<heads>))
1193 1193
1194 1194 If includepath is True, return (<roots>::<heads>)."""
1195 1195 try:
1196 1196 return self.index.reachableroots2(minroot, heads, roots,
1197 1197 includepath)
1198 1198 except AttributeError:
1199 1199 return dagop._reachablerootspure(self.parentrevs,
1200 1200 minroot, roots, heads, includepath)
1201 1201
1202 1202 def ancestor(self, a, b):
1203 1203 """calculate the "best" common ancestor of nodes a and b"""
1204 1204
1205 1205 a, b = self.rev(a), self.rev(b)
1206 1206 try:
1207 1207 ancs = self.index.ancestors(a, b)
1208 1208 except (AttributeError, OverflowError):
1209 1209 ancs = ancestor.ancestors(self.parentrevs, a, b)
1210 1210 if ancs:
1211 1211 # choose a consistent winner when there's a tie
1212 1212 return min(map(self.node, ancs))
1213 1213 return nullid
1214 1214
1215 1215 def _match(self, id):
1216 1216 if isinstance(id, int):
1217 1217 # rev
1218 1218 return self.node(id)
1219 1219 if len(id) == 20:
1220 1220 # possibly a binary node
1221 1221 # odds of a binary node being all hex in ASCII are 1 in 10**25
1222 1222 try:
1223 1223 node = id
1224 1224 self.rev(node) # quick search the index
1225 1225 return node
1226 1226 except error.LookupError:
1227 1227 pass # may be partial hex id
1228 1228 try:
1229 1229 # str(rev)
1230 1230 rev = int(id)
1231 1231 if "%d" % rev != id:
1232 1232 raise ValueError
1233 1233 if rev < 0:
1234 1234 rev = len(self) + rev
1235 1235 if rev < 0 or rev >= len(self):
1236 1236 raise ValueError
1237 1237 return self.node(rev)
1238 1238 except (ValueError, OverflowError):
1239 1239 pass
1240 1240 if len(id) == 40:
1241 1241 try:
1242 1242 # a full hex nodeid?
1243 1243 node = bin(id)
1244 1244 self.rev(node)
1245 1245 return node
1246 1246 except (TypeError, error.LookupError):
1247 1247 pass
1248 1248
1249 1249 def _partialmatch(self, id):
1250 1250 # we don't care wdirfilenodeids as they should be always full hash
1251 1251 maybewdir = wdirhex.startswith(id)
1252 1252 try:
1253 1253 partial = self.index.partialmatch(id)
1254 1254 if partial and self.hasnode(partial):
1255 1255 if maybewdir:
1256 1256 # single 'ff...' match in radix tree, ambiguous with wdir
1257 1257 raise error.RevlogError
1258 1258 return partial
1259 1259 if maybewdir:
1260 1260 # no 'ff...' match in radix tree, wdir identified
1261 1261 raise error.WdirUnsupported
1262 1262 return None
1263 1263 except error.RevlogError:
1264 1264 # parsers.c radix tree lookup gave multiple matches
1265 1265 # fast path: for unfiltered changelog, radix tree is accurate
1266 1266 if not getattr(self, 'filteredrevs', None):
1267 1267 raise error.AmbiguousPrefixLookupError(
1268 1268 id, self.indexfile, _('ambiguous identifier'))
1269 1269 # fall through to slow path that filters hidden revisions
1270 1270 except (AttributeError, ValueError):
1271 1271 # we are pure python, or key was too short to search radix tree
1272 1272 pass
1273 1273
1274 1274 if id in self._pcache:
1275 1275 return self._pcache[id]
1276 1276
1277 1277 if len(id) <= 40:
1278 1278 try:
1279 1279 # hex(node)[:...]
1280 1280 l = len(id) // 2 # grab an even number of digits
1281 1281 prefix = bin(id[:l * 2])
1282 1282 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
1283 1283 nl = [n for n in nl if hex(n).startswith(id) and
1284 1284 self.hasnode(n)]
1285 1285 if nullhex.startswith(id):
1286 1286 nl.append(nullid)
1287 1287 if len(nl) > 0:
1288 1288 if len(nl) == 1 and not maybewdir:
1289 1289 self._pcache[id] = nl[0]
1290 1290 return nl[0]
1291 1291 raise error.AmbiguousPrefixLookupError(
1292 1292 id, self.indexfile, _('ambiguous identifier'))
1293 1293 if maybewdir:
1294 1294 raise error.WdirUnsupported
1295 1295 return None
1296 1296 except TypeError:
1297 1297 pass
1298 1298
1299 1299 def lookup(self, id):
1300 1300 """locate a node based on:
1301 1301 - revision number or str(revision number)
1302 1302 - nodeid or subset of hex nodeid
1303 1303 """
1304 1304 n = self._match(id)
1305 1305 if n is not None:
1306 1306 return n
1307 1307 n = self._partialmatch(id)
1308 1308 if n:
1309 1309 return n
1310 1310
1311 1311 raise error.LookupError(id, self.indexfile, _('no match found'))
1312 1312
1313 1313 def shortest(self, node, minlength=1):
1314 1314 """Find the shortest unambiguous prefix that matches node."""
1315 1315 def isvalid(prefix):
1316 1316 try:
1317 1317 matchednode = self._partialmatch(prefix)
1318 1318 except error.AmbiguousPrefixLookupError:
1319 1319 return False
1320 1320 except error.WdirUnsupported:
1321 1321 # single 'ff...' match
1322 1322 return True
1323 1323 if matchednode is None:
1324 1324 raise error.LookupError(node, self.indexfile, _('no node'))
1325 1325 return True
1326 1326
1327 1327 def maybewdir(prefix):
1328 1328 return all(c == 'f' for c in pycompat.iterbytestr(prefix))
1329 1329
1330 1330 hexnode = hex(node)
1331 1331
1332 1332 def disambiguate(hexnode, minlength):
1333 1333 """Disambiguate against wdirid."""
1334 1334 for length in range(minlength, 41):
1335 1335 prefix = hexnode[:length]
1336 1336 if not maybewdir(prefix):
1337 1337 return prefix
1338 1338
1339 1339 if not getattr(self, 'filteredrevs', None):
1340 1340 try:
1341 1341 length = max(self.index.shortest(node), minlength)
1342 1342 return disambiguate(hexnode, length)
1343 1343 except error.RevlogError:
1344 1344 if node != wdirid:
1345 1345 raise error.LookupError(node, self.indexfile, _('no node'))
1346 1346 except AttributeError:
1347 1347 # Fall through to pure code
1348 1348 pass
1349 1349
1350 1350 if node == wdirid:
1351 1351 for length in range(minlength, 41):
1352 1352 prefix = hexnode[:length]
1353 1353 if isvalid(prefix):
1354 1354 return prefix
1355 1355
1356 1356 for length in range(minlength, 41):
1357 1357 prefix = hexnode[:length]
1358 1358 if isvalid(prefix):
1359 1359 return disambiguate(hexnode, length)
1360 1360
1361 1361 def cmp(self, node, text):
1362 1362 """compare text with a given file revision
1363 1363
1364 1364 returns True if text is different than what is stored.
1365 1365 """
1366 1366 p1, p2 = self.parents(node)
1367 1367 return storageutil.hashrevisionsha1(text, p1, p2) != node
1368 1368
1369 1369 def _cachesegment(self, offset, data):
1370 1370 """Add a segment to the revlog cache.
1371 1371
1372 1372 Accepts an absolute offset and the data that is at that location.
1373 1373 """
1374 1374 o, d = self._chunkcache
1375 1375 # try to add to existing cache
1376 1376 if o + len(d) == offset and len(d) + len(data) < _chunksize:
1377 1377 self._chunkcache = o, d + data
1378 1378 else:
1379 1379 self._chunkcache = offset, data
1380 1380
1381 1381 def _readsegment(self, offset, length, df=None):
1382 1382 """Load a segment of raw data from the revlog.
1383 1383
1384 1384 Accepts an absolute offset, length to read, and an optional existing
1385 1385 file handle to read from.
1386 1386
1387 1387 If an existing file handle is passed, it will be seeked and the
1388 1388 original seek position will NOT be restored.
1389 1389
1390 1390 Returns a str or buffer of raw byte data.
1391 1391
1392 1392 Raises if the requested number of bytes could not be read.
1393 1393 """
1394 1394 # Cache data both forward and backward around the requested
1395 1395 # data, in a fixed size window. This helps speed up operations
1396 1396 # involving reading the revlog backwards.
1397 1397 cachesize = self._chunkcachesize
1398 1398 realoffset = offset & ~(cachesize - 1)
1399 1399 reallength = (((offset + length + cachesize) & ~(cachesize - 1))
1400 1400 - realoffset)
1401 1401 with self._datareadfp(df) as df:
1402 1402 df.seek(realoffset)
1403 1403 d = df.read(reallength)
1404 1404
1405 1405 self._cachesegment(realoffset, d)
1406 1406 if offset != realoffset or reallength != length:
1407 1407 startoffset = offset - realoffset
1408 1408 if len(d) - startoffset < length:
1409 1409 raise error.RevlogError(
1410 1410 _('partial read of revlog %s; expected %d bytes from '
1411 1411 'offset %d, got %d') %
1412 1412 (self.indexfile if self._inline else self.datafile,
1413 1413 length, realoffset, len(d) - startoffset))
1414 1414
1415 1415 return util.buffer(d, startoffset, length)
1416 1416
1417 1417 if len(d) < length:
1418 1418 raise error.RevlogError(
1419 1419 _('partial read of revlog %s; expected %d bytes from offset '
1420 1420 '%d, got %d') %
1421 1421 (self.indexfile if self._inline else self.datafile,
1422 1422 length, offset, len(d)))
1423 1423
1424 1424 return d
1425 1425
1426 1426 def _getsegment(self, offset, length, df=None):
1427 1427 """Obtain a segment of raw data from the revlog.
1428 1428
1429 1429 Accepts an absolute offset, length of bytes to obtain, and an
1430 1430 optional file handle to the already-opened revlog. If the file
1431 1431 handle is used, it's original seek position will not be preserved.
1432 1432
1433 1433 Requests for data may be returned from a cache.
1434 1434
1435 1435 Returns a str or a buffer instance of raw byte data.
1436 1436 """
1437 1437 o, d = self._chunkcache
1438 1438 l = len(d)
1439 1439
1440 1440 # is it in the cache?
1441 1441 cachestart = offset - o
1442 1442 cacheend = cachestart + length
1443 1443 if cachestart >= 0 and cacheend <= l:
1444 1444 if cachestart == 0 and cacheend == l:
1445 1445 return d # avoid a copy
1446 1446 return util.buffer(d, cachestart, cacheend - cachestart)
1447 1447
1448 1448 return self._readsegment(offset, length, df=df)
1449 1449
1450 1450 def _getsegmentforrevs(self, startrev, endrev, df=None):
1451 1451 """Obtain a segment of raw data corresponding to a range of revisions.
1452 1452
1453 1453 Accepts the start and end revisions and an optional already-open
1454 1454 file handle to be used for reading. If the file handle is read, its
1455 1455 seek position will not be preserved.
1456 1456
1457 1457 Requests for data may be satisfied by a cache.
1458 1458
1459 1459 Returns a 2-tuple of (offset, data) for the requested range of
1460 1460 revisions. Offset is the integer offset from the beginning of the
1461 1461 revlog and data is a str or buffer of the raw byte data.
1462 1462
1463 1463 Callers will need to call ``self.start(rev)`` and ``self.length(rev)``
1464 1464 to determine where each revision's data begins and ends.
1465 1465 """
1466 1466 # Inlined self.start(startrev) & self.end(endrev) for perf reasons
1467 1467 # (functions are expensive).
1468 1468 index = self.index
1469 1469 istart = index[startrev]
1470 1470 start = int(istart[0] >> 16)
1471 1471 if startrev == endrev:
1472 1472 end = start + istart[1]
1473 1473 else:
1474 1474 iend = index[endrev]
1475 1475 end = int(iend[0] >> 16) + iend[1]
1476 1476
1477 1477 if self._inline:
1478 1478 start += (startrev + 1) * self._io.size
1479 1479 end += (endrev + 1) * self._io.size
1480 1480 length = end - start
1481 1481
1482 1482 return start, self._getsegment(start, length, df=df)
1483 1483
1484 1484 def _chunk(self, rev, df=None):
1485 1485 """Obtain a single decompressed chunk for a revision.
1486 1486
1487 1487 Accepts an integer revision and an optional already-open file handle
1488 1488 to be used for reading. If used, the seek position of the file will not
1489 1489 be preserved.
1490 1490
1491 1491 Returns a str holding uncompressed data for the requested revision.
1492 1492 """
1493 1493 return self.decompress(self._getsegmentforrevs(rev, rev, df=df)[1])
1494 1494
1495 1495 def _chunks(self, revs, df=None, targetsize=None):
1496 1496 """Obtain decompressed chunks for the specified revisions.
1497 1497
1498 1498 Accepts an iterable of numeric revisions that are assumed to be in
1499 1499 ascending order. Also accepts an optional already-open file handle
1500 1500 to be used for reading. If used, the seek position of the file will
1501 1501 not be preserved.
1502 1502
1503 1503 This function is similar to calling ``self._chunk()`` multiple times,
1504 1504 but is faster.
1505 1505
1506 1506 Returns a list with decompressed data for each requested revision.
1507 1507 """
1508 1508 if not revs:
1509 1509 return []
1510 1510 start = self.start
1511 1511 length = self.length
1512 1512 inline = self._inline
1513 1513 iosize = self._io.size
1514 1514 buffer = util.buffer
1515 1515
1516 1516 l = []
1517 1517 ladd = l.append
1518 1518
1519 1519 if not self._withsparseread:
1520 1520 slicedchunks = (revs,)
1521 1521 else:
1522 1522 slicedchunks = deltautil.slicechunk(self, revs,
1523 1523 targetsize=targetsize)
1524 1524
1525 1525 for revschunk in slicedchunks:
1526 1526 firstrev = revschunk[0]
1527 1527 # Skip trailing revisions with empty diff
1528 1528 for lastrev in revschunk[::-1]:
1529 1529 if length(lastrev) != 0:
1530 1530 break
1531 1531
1532 1532 try:
1533 1533 offset, data = self._getsegmentforrevs(firstrev, lastrev, df=df)
1534 1534 except OverflowError:
1535 1535 # issue4215 - we can't cache a run of chunks greater than
1536 1536 # 2G on Windows
1537 1537 return [self._chunk(rev, df=df) for rev in revschunk]
1538 1538
1539 1539 decomp = self.decompress
1540 1540 for rev in revschunk:
1541 1541 chunkstart = start(rev)
1542 1542 if inline:
1543 1543 chunkstart += (rev + 1) * iosize
1544 1544 chunklength = length(rev)
1545 1545 ladd(decomp(buffer(data, chunkstart - offset, chunklength)))
1546 1546
1547 1547 return l
1548 1548
1549 1549 def _chunkclear(self):
1550 1550 """Clear the raw chunk cache."""
1551 1551 self._chunkcache = (0, '')
1552 1552
1553 1553 def deltaparent(self, rev):
1554 1554 """return deltaparent of the given revision"""
1555 1555 base = self.index[rev][3]
1556 1556 if base == rev:
1557 1557 return nullrev
1558 1558 elif self._generaldelta:
1559 1559 return base
1560 1560 else:
1561 1561 return rev - 1
1562 1562
1563 1563 def issnapshot(self, rev):
1564 1564 """tells whether rev is a snapshot
1565 1565 """
1566 1566 if not self._sparserevlog:
1567 1567 return self.deltaparent(rev) == nullrev
1568 1568 elif util.safehasattr(self.index, 'issnapshot'):
1569 1569 # directly assign the method to cache the testing and access
1570 1570 self.issnapshot = self.index.issnapshot
1571 1571 return self.issnapshot(rev)
1572 1572 if rev == nullrev:
1573 1573 return True
1574 1574 entry = self.index[rev]
1575 1575 base = entry[3]
1576 1576 if base == rev:
1577 1577 return True
1578 1578 if base == nullrev:
1579 1579 return True
1580 1580 p1 = entry[5]
1581 1581 p2 = entry[6]
1582 1582 if base == p1 or base == p2:
1583 1583 return False
1584 1584 return self.issnapshot(base)
1585 1585
1586 1586 def snapshotdepth(self, rev):
1587 1587 """number of snapshot in the chain before this one"""
1588 1588 if not self.issnapshot(rev):
1589 1589 raise error.ProgrammingError('revision %d not a snapshot')
1590 1590 return len(self._deltachain(rev)[0]) - 1
1591 1591
1592 1592 def revdiff(self, rev1, rev2):
1593 1593 """return or calculate a delta between two revisions
1594 1594
1595 1595 The delta calculated is in binary form and is intended to be written to
1596 1596 revlog data directly. So this function needs raw revision data.
1597 1597 """
1598 1598 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
1599 1599 return bytes(self._chunk(rev2))
1600 1600
1601 1601 return mdiff.textdiff(self.rawdata(rev1),
1602 1602 self.rawdata(rev2))
1603 1603
1604 def _processflags(self, text, flags, operation, raw=False):
1605 """deprecated entry point to access flag processors"""
1606 msg = ('_processflag(...) use the specialized variant')
1607 util.nouideprecwarn(msg, '5.2', stacklevel=2)
1608 if raw:
1609 return text, flagutil.processflagsraw(self, text, flags)
1610 elif operation == 'read':
1611 return flagutil.processflagsread(self, text, flags)
1612 else: # write operation
1613 return flagutil.processflagswrite(self, text, flags)
1614
1604 1615 def revision(self, nodeorrev, _df=None, raw=False):
1605 1616 """return an uncompressed revision of a given node or revision
1606 1617 number.
1607 1618
1608 1619 _df - an existing file handle to read from. (internal-only)
1609 1620 raw - an optional argument specifying if the revision data is to be
1610 1621 treated as raw data when applying flag transforms. 'raw' should be set
1611 1622 to True when generating changegroups or in debug commands.
1612 1623 """
1613 1624 if raw:
1614 1625 msg = ('revlog.revision(..., raw=True) is deprecated, '
1615 1626 'use revlog.rawdata(...)')
1616 1627 util.nouideprecwarn(msg, '5.2', stacklevel=2)
1617 1628 return self._revisiondata(nodeorrev, _df, raw=raw)[0]
1618 1629
1619 1630 def sidedata(self, nodeorrev, _df=None):
1620 1631 """a map of extra data related to the changeset but not part of the hash
1621 1632
1622 1633 This function currently return a dictionary. However, more advanced
1623 1634 mapping object will likely be used in the future for a more
1624 1635 efficient/lazy code.
1625 1636 """
1626 1637 return self._revisiondata(nodeorrev, _df)[1]
1627 1638
1628 1639 def _revisiondata(self, nodeorrev, _df=None, raw=False):
1629 1640 # deal with <nodeorrev> argument type
1630 1641 if isinstance(nodeorrev, int):
1631 1642 rev = nodeorrev
1632 1643 node = self.node(rev)
1633 1644 else:
1634 1645 node = nodeorrev
1635 1646 rev = None
1636 1647
1637 1648 # fast path the special `nullid` rev
1638 1649 if node == nullid:
1639 1650 return "", {}
1640 1651
1641 1652 # The text as stored inside the revlog. Might be the revision or might
1642 1653 # need to be processed to retrieve the revision.
1643 1654 rawtext = None
1644 1655
1645 1656 rev, rawtext, validated = self._rawtext(node, rev, _df=_df)
1646 1657
1647 1658 if raw and validated:
1648 1659 # if we don't want to process the raw text and that raw
1649 1660 # text is cached, we can exit early.
1650 1661 return rawtext, {}
1651 1662 if rev is None:
1652 1663 rev = self.rev(node)
1653 1664 # the revlog's flag for this revision
1654 1665 # (usually alter its state or content)
1655 1666 flags = self.flags(rev)
1656 1667
1657 1668 if validated and flags == REVIDX_DEFAULT_FLAGS:
1658 1669 # no extra flags set, no flag processor runs, text = rawtext
1659 1670 return rawtext, {}
1660 1671
1661 1672 sidedata = {}
1662 1673 if raw:
1663 1674 validatehash = flagutil.processflagsraw(self, rawtext, flags)
1664 1675 text = rawtext
1665 1676 else:
1666 1677 r = flagutil.processflagsread(self, rawtext, flags)
1667 1678 text, validatehash, sidedata = r
1668 1679 if validatehash:
1669 1680 self.checkhash(text, node, rev=rev)
1670 1681 if not validated:
1671 1682 self._revisioncache = (node, rev, rawtext)
1672 1683
1673 1684 return text, sidedata
1674 1685
1675 1686 def _rawtext(self, node, rev, _df=None):
1676 1687 """return the possibly unvalidated rawtext for a revision
1677 1688
1678 1689 returns (rev, rawtext, validated)
1679 1690 """
1680 1691
1681 1692 # revision in the cache (could be useful to apply delta)
1682 1693 cachedrev = None
1683 1694 # An intermediate text to apply deltas to
1684 1695 basetext = None
1685 1696
1686 1697 # Check if we have the entry in cache
1687 1698 # The cache entry looks like (node, rev, rawtext)
1688 1699 if self._revisioncache:
1689 1700 if self._revisioncache[0] == node:
1690 1701 return (rev, self._revisioncache[2], True)
1691 1702 cachedrev = self._revisioncache[1]
1692 1703
1693 1704 if rev is None:
1694 1705 rev = self.rev(node)
1695 1706
1696 1707 chain, stopped = self._deltachain(rev, stoprev=cachedrev)
1697 1708 if stopped:
1698 1709 basetext = self._revisioncache[2]
1699 1710
1700 1711 # drop cache to save memory, the caller is expected to
1701 1712 # update self._revisioncache after validating the text
1702 1713 self._revisioncache = None
1703 1714
1704 1715 targetsize = None
1705 1716 rawsize = self.index[rev][2]
1706 1717 if 0 <= rawsize:
1707 1718 targetsize = 4 * rawsize
1708 1719
1709 1720 bins = self._chunks(chain, df=_df, targetsize=targetsize)
1710 1721 if basetext is None:
1711 1722 basetext = bytes(bins[0])
1712 1723 bins = bins[1:]
1713 1724
1714 1725 rawtext = mdiff.patches(basetext, bins)
1715 1726 del basetext # let us have a chance to free memory early
1716 1727 return (rev, rawtext, False)
1717 1728
1718 1729 def rawdata(self, nodeorrev, _df=None):
1719 1730 """return an uncompressed raw data of a given node or revision number.
1720 1731
1721 1732 _df - an existing file handle to read from. (internal-only)
1722 1733 """
1723 1734 return self._revisiondata(nodeorrev, _df, raw=True)[0]
1724 1735
1725 1736 def hash(self, text, p1, p2):
1726 1737 """Compute a node hash.
1727 1738
1728 1739 Available as a function so that subclasses can replace the hash
1729 1740 as needed.
1730 1741 """
1731 1742 return storageutil.hashrevisionsha1(text, p1, p2)
1732 1743
1733 1744 def checkhash(self, text, node, p1=None, p2=None, rev=None):
1734 1745 """Check node hash integrity.
1735 1746
1736 1747 Available as a function so that subclasses can extend hash mismatch
1737 1748 behaviors as needed.
1738 1749 """
1739 1750 try:
1740 1751 if p1 is None and p2 is None:
1741 1752 p1, p2 = self.parents(node)
1742 1753 if node != self.hash(text, p1, p2):
1743 1754 # Clear the revision cache on hash failure. The revision cache
1744 1755 # only stores the raw revision and clearing the cache does have
1745 1756 # the side-effect that we won't have a cache hit when the raw
1746 1757 # revision data is accessed. But this case should be rare and
1747 1758 # it is extra work to teach the cache about the hash
1748 1759 # verification state.
1749 1760 if self._revisioncache and self._revisioncache[0] == node:
1750 1761 self._revisioncache = None
1751 1762
1752 1763 revornode = rev
1753 1764 if revornode is None:
1754 1765 revornode = templatefilters.short(hex(node))
1755 1766 raise error.RevlogError(_("integrity check failed on %s:%s")
1756 1767 % (self.indexfile, pycompat.bytestr(revornode)))
1757 1768 except error.RevlogError:
1758 1769 if self._censorable and storageutil.iscensoredtext(text):
1759 1770 raise error.CensoredNodeError(self.indexfile, node, text)
1760 1771 raise
1761 1772
1762 1773 def _enforceinlinesize(self, tr, fp=None):
1763 1774 """Check if the revlog is too big for inline and convert if so.
1764 1775
1765 1776 This should be called after revisions are added to the revlog. If the
1766 1777 revlog has grown too large to be an inline revlog, it will convert it
1767 1778 to use multiple index and data files.
1768 1779 """
1769 1780 tiprev = len(self) - 1
1770 1781 if (not self._inline or
1771 1782 (self.start(tiprev) + self.length(tiprev)) < _maxinline):
1772 1783 return
1773 1784
1774 1785 trinfo = tr.find(self.indexfile)
1775 1786 if trinfo is None:
1776 1787 raise error.RevlogError(_("%s not found in the transaction")
1777 1788 % self.indexfile)
1778 1789
1779 1790 trindex = trinfo[2]
1780 1791 if trindex is not None:
1781 1792 dataoff = self.start(trindex)
1782 1793 else:
1783 1794 # revlog was stripped at start of transaction, use all leftover data
1784 1795 trindex = len(self) - 1
1785 1796 dataoff = self.end(tiprev)
1786 1797
1787 1798 tr.add(self.datafile, dataoff)
1788 1799
1789 1800 if fp:
1790 1801 fp.flush()
1791 1802 fp.close()
1792 1803 # We can't use the cached file handle after close(). So prevent
1793 1804 # its usage.
1794 1805 self._writinghandles = None
1795 1806
1796 1807 with self._indexfp('r') as ifh, self._datafp('w') as dfh:
1797 1808 for r in self:
1798 1809 dfh.write(self._getsegmentforrevs(r, r, df=ifh)[1])
1799 1810
1800 1811 with self._indexfp('w') as fp:
1801 1812 self.version &= ~FLAG_INLINE_DATA
1802 1813 self._inline = False
1803 1814 io = self._io
1804 1815 for i in self:
1805 1816 e = io.packentry(self.index[i], self.node, self.version, i)
1806 1817 fp.write(e)
1807 1818
1808 1819 # the temp file replace the real index when we exit the context
1809 1820 # manager
1810 1821
1811 1822 tr.replace(self.indexfile, trindex * self._io.size)
1812 1823 self._chunkclear()
1813 1824
1814 1825 def _nodeduplicatecallback(self, transaction, node):
1815 1826 """called when trying to add a node already stored.
1816 1827 """
1817 1828
1818 1829 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None,
1819 1830 node=None, flags=REVIDX_DEFAULT_FLAGS, deltacomputer=None,
1820 1831 sidedata=None):
1821 1832 """add a revision to the log
1822 1833
1823 1834 text - the revision data to add
1824 1835 transaction - the transaction object used for rollback
1825 1836 link - the linkrev data to add
1826 1837 p1, p2 - the parent nodeids of the revision
1827 1838 cachedelta - an optional precomputed delta
1828 1839 node - nodeid of revision; typically node is not specified, and it is
1829 1840 computed by default as hash(text, p1, p2), however subclasses might
1830 1841 use different hashing method (and override checkhash() in such case)
1831 1842 flags - the known flags to set on the revision
1832 1843 deltacomputer - an optional deltacomputer instance shared between
1833 1844 multiple calls
1834 1845 """
1835 1846 if link == nullrev:
1836 1847 raise error.RevlogError(_("attempted to add linkrev -1 to %s")
1837 1848 % self.indexfile)
1838 1849
1839 1850 if sidedata is None:
1840 1851 sidedata = {}
1841 1852
1842 1853 if flags:
1843 1854 node = node or self.hash(text, p1, p2)
1844 1855
1845 1856 rawtext, validatehash = flagutil.processflagswrite(self, text, flags,
1846 1857 sidedata=sidedata)
1847 1858
1848 1859 # If the flag processor modifies the revision data, ignore any provided
1849 1860 # cachedelta.
1850 1861 if rawtext != text:
1851 1862 cachedelta = None
1852 1863
1853 1864 if len(rawtext) > _maxentrysize:
1854 1865 raise error.RevlogError(
1855 1866 _("%s: size of %d bytes exceeds maximum revlog storage of 2GiB")
1856 1867 % (self.indexfile, len(rawtext)))
1857 1868
1858 1869 node = node or self.hash(rawtext, p1, p2)
1859 1870 if node in self.nodemap:
1860 1871 return node
1861 1872
1862 1873 if validatehash:
1863 1874 self.checkhash(rawtext, node, p1=p1, p2=p2)
1864 1875
1865 1876 return self.addrawrevision(rawtext, transaction, link, p1, p2, node,
1866 1877 flags, cachedelta=cachedelta,
1867 1878 deltacomputer=deltacomputer)
1868 1879
1869 1880 def addrawrevision(self, rawtext, transaction, link, p1, p2, node, flags,
1870 1881 cachedelta=None, deltacomputer=None):
1871 1882 """add a raw revision with known flags, node and parents
1872 1883 useful when reusing a revision not stored in this revlog (ex: received
1873 1884 over wire, or read from an external bundle).
1874 1885 """
1875 1886 dfh = None
1876 1887 if not self._inline:
1877 1888 dfh = self._datafp("a+")
1878 1889 ifh = self._indexfp("a+")
1879 1890 try:
1880 1891 return self._addrevision(node, rawtext, transaction, link, p1, p2,
1881 1892 flags, cachedelta, ifh, dfh,
1882 1893 deltacomputer=deltacomputer)
1883 1894 finally:
1884 1895 if dfh:
1885 1896 dfh.close()
1886 1897 ifh.close()
1887 1898
1888 1899 def compress(self, data):
1889 1900 """Generate a possibly-compressed representation of data."""
1890 1901 if not data:
1891 1902 return '', data
1892 1903
1893 1904 compressed = self._compressor.compress(data)
1894 1905
1895 1906 if compressed:
1896 1907 # The revlog compressor added the header in the returned data.
1897 1908 return '', compressed
1898 1909
1899 1910 if data[0:1] == '\0':
1900 1911 return '', data
1901 1912 return 'u', data
1902 1913
1903 1914 def decompress(self, data):
1904 1915 """Decompress a revlog chunk.
1905 1916
1906 1917 The chunk is expected to begin with a header identifying the
1907 1918 format type so it can be routed to an appropriate decompressor.
1908 1919 """
1909 1920 if not data:
1910 1921 return data
1911 1922
1912 1923 # Revlogs are read much more frequently than they are written and many
1913 1924 # chunks only take microseconds to decompress, so performance is
1914 1925 # important here.
1915 1926 #
1916 1927 # We can make a few assumptions about revlogs:
1917 1928 #
1918 1929 # 1) the majority of chunks will be compressed (as opposed to inline
1919 1930 # raw data).
1920 1931 # 2) decompressing *any* data will likely by at least 10x slower than
1921 1932 # returning raw inline data.
1922 1933 # 3) we want to prioritize common and officially supported compression
1923 1934 # engines
1924 1935 #
1925 1936 # It follows that we want to optimize for "decompress compressed data
1926 1937 # when encoded with common and officially supported compression engines"
1927 1938 # case over "raw data" and "data encoded by less common or non-official
1928 1939 # compression engines." That is why we have the inline lookup first
1929 1940 # followed by the compengines lookup.
1930 1941 #
1931 1942 # According to `hg perfrevlogchunks`, this is ~0.5% faster for zlib
1932 1943 # compressed chunks. And this matters for changelog and manifest reads.
1933 1944 t = data[0:1]
1934 1945
1935 1946 if t == 'x':
1936 1947 try:
1937 1948 return _zlibdecompress(data)
1938 1949 except zlib.error as e:
1939 1950 raise error.RevlogError(_('revlog decompress error: %s') %
1940 1951 stringutil.forcebytestr(e))
1941 1952 # '\0' is more common than 'u' so it goes first.
1942 1953 elif t == '\0':
1943 1954 return data
1944 1955 elif t == 'u':
1945 1956 return util.buffer(data, 1)
1946 1957
1947 1958 try:
1948 1959 compressor = self._decompressors[t]
1949 1960 except KeyError:
1950 1961 try:
1951 1962 engine = util.compengines.forrevlogheader(t)
1952 1963 compressor = engine.revlogcompressor(self._compengineopts)
1953 1964 self._decompressors[t] = compressor
1954 1965 except KeyError:
1955 1966 raise error.RevlogError(_('unknown compression type %r') % t)
1956 1967
1957 1968 return compressor.decompress(data)
1958 1969
1959 1970 def _addrevision(self, node, rawtext, transaction, link, p1, p2, flags,
1960 1971 cachedelta, ifh, dfh, alwayscache=False,
1961 1972 deltacomputer=None):
1962 1973 """internal function to add revisions to the log
1963 1974
1964 1975 see addrevision for argument descriptions.
1965 1976
1966 1977 note: "addrevision" takes non-raw text, "_addrevision" takes raw text.
1967 1978
1968 1979 if "deltacomputer" is not provided or None, a defaultdeltacomputer will
1969 1980 be used.
1970 1981
1971 1982 invariants:
1972 1983 - rawtext is optional (can be None); if not set, cachedelta must be set.
1973 1984 if both are set, they must correspond to each other.
1974 1985 """
1975 1986 if node == nullid:
1976 1987 raise error.RevlogError(_("%s: attempt to add null revision") %
1977 1988 self.indexfile)
1978 1989 if node == wdirid or node in wdirfilenodeids:
1979 1990 raise error.RevlogError(_("%s: attempt to add wdir revision") %
1980 1991 self.indexfile)
1981 1992
1982 1993 if self._inline:
1983 1994 fh = ifh
1984 1995 else:
1985 1996 fh = dfh
1986 1997
1987 1998 btext = [rawtext]
1988 1999
1989 2000 curr = len(self)
1990 2001 prev = curr - 1
1991 2002 offset = self.end(prev)
1992 2003 p1r, p2r = self.rev(p1), self.rev(p2)
1993 2004
1994 2005 # full versions are inserted when the needed deltas
1995 2006 # become comparable to the uncompressed text
1996 2007 if rawtext is None:
1997 2008 # need rawtext size, before changed by flag processors, which is
1998 2009 # the non-raw size. use revlog explicitly to avoid filelog's extra
1999 2010 # logic that might remove metadata size.
2000 2011 textlen = mdiff.patchedsize(revlog.size(self, cachedelta[0]),
2001 2012 cachedelta[1])
2002 2013 else:
2003 2014 textlen = len(rawtext)
2004 2015
2005 2016 if deltacomputer is None:
2006 2017 deltacomputer = deltautil.deltacomputer(self)
2007 2018
2008 2019 revinfo = _revisioninfo(node, p1, p2, btext, textlen, cachedelta, flags)
2009 2020
2010 2021 deltainfo = deltacomputer.finddeltainfo(revinfo, fh)
2011 2022
2012 2023 e = (offset_type(offset, flags), deltainfo.deltalen, textlen,
2013 2024 deltainfo.base, link, p1r, p2r, node)
2014 2025 self.index.append(e)
2015 2026 self.nodemap[node] = curr
2016 2027
2017 2028 # Reset the pure node cache start lookup offset to account for new
2018 2029 # revision.
2019 2030 if self._nodepos is not None:
2020 2031 self._nodepos = curr
2021 2032
2022 2033 entry = self._io.packentry(e, self.node, self.version, curr)
2023 2034 self._writeentry(transaction, ifh, dfh, entry, deltainfo.data,
2024 2035 link, offset)
2025 2036
2026 2037 rawtext = btext[0]
2027 2038
2028 2039 if alwayscache and rawtext is None:
2029 2040 rawtext = deltacomputer.buildtext(revinfo, fh)
2030 2041
2031 2042 if type(rawtext) == bytes: # only accept immutable objects
2032 2043 self._revisioncache = (node, curr, rawtext)
2033 2044 self._chainbasecache[curr] = deltainfo.chainbase
2034 2045 return node
2035 2046
2036 2047 def _writeentry(self, transaction, ifh, dfh, entry, data, link, offset):
2037 2048 # Files opened in a+ mode have inconsistent behavior on various
2038 2049 # platforms. Windows requires that a file positioning call be made
2039 2050 # when the file handle transitions between reads and writes. See
2040 2051 # 3686fa2b8eee and the mixedfilemodewrapper in windows.py. On other
2041 2052 # platforms, Python or the platform itself can be buggy. Some versions
2042 2053 # of Solaris have been observed to not append at the end of the file
2043 2054 # if the file was seeked to before the end. See issue4943 for more.
2044 2055 #
2045 2056 # We work around this issue by inserting a seek() before writing.
2046 2057 # Note: This is likely not necessary on Python 3. However, because
2047 2058 # the file handle is reused for reads and may be seeked there, we need
2048 2059 # to be careful before changing this.
2049 2060 ifh.seek(0, os.SEEK_END)
2050 2061 if dfh:
2051 2062 dfh.seek(0, os.SEEK_END)
2052 2063
2053 2064 curr = len(self) - 1
2054 2065 if not self._inline:
2055 2066 transaction.add(self.datafile, offset)
2056 2067 transaction.add(self.indexfile, curr * len(entry))
2057 2068 if data[0]:
2058 2069 dfh.write(data[0])
2059 2070 dfh.write(data[1])
2060 2071 ifh.write(entry)
2061 2072 else:
2062 2073 offset += curr * self._io.size
2063 2074 transaction.add(self.indexfile, offset, curr)
2064 2075 ifh.write(entry)
2065 2076 ifh.write(data[0])
2066 2077 ifh.write(data[1])
2067 2078 self._enforceinlinesize(transaction, ifh)
2068 2079
2069 2080 def addgroup(self, deltas, linkmapper, transaction, addrevisioncb=None):
2070 2081 """
2071 2082 add a delta group
2072 2083
2073 2084 given a set of deltas, add them to the revision log. the
2074 2085 first delta is against its parent, which should be in our
2075 2086 log, the rest are against the previous delta.
2076 2087
2077 2088 If ``addrevisioncb`` is defined, it will be called with arguments of
2078 2089 this revlog and the node that was added.
2079 2090 """
2080 2091
2081 2092 if self._writinghandles:
2082 2093 raise error.ProgrammingError('cannot nest addgroup() calls')
2083 2094
2084 2095 nodes = []
2085 2096
2086 2097 r = len(self)
2087 2098 end = 0
2088 2099 if r:
2089 2100 end = self.end(r - 1)
2090 2101 ifh = self._indexfp("a+")
2091 2102 isize = r * self._io.size
2092 2103 if self._inline:
2093 2104 transaction.add(self.indexfile, end + isize, r)
2094 2105 dfh = None
2095 2106 else:
2096 2107 transaction.add(self.indexfile, isize, r)
2097 2108 transaction.add(self.datafile, end)
2098 2109 dfh = self._datafp("a+")
2099 2110 def flush():
2100 2111 if dfh:
2101 2112 dfh.flush()
2102 2113 ifh.flush()
2103 2114
2104 2115 self._writinghandles = (ifh, dfh)
2105 2116
2106 2117 try:
2107 2118 deltacomputer = deltautil.deltacomputer(self)
2108 2119 # loop through our set of deltas
2109 2120 for data in deltas:
2110 2121 node, p1, p2, linknode, deltabase, delta, flags = data
2111 2122 link = linkmapper(linknode)
2112 2123 flags = flags or REVIDX_DEFAULT_FLAGS
2113 2124
2114 2125 nodes.append(node)
2115 2126
2116 2127 if node in self.nodemap:
2117 2128 self._nodeduplicatecallback(transaction, node)
2118 2129 # this can happen if two branches make the same change
2119 2130 continue
2120 2131
2121 2132 for p in (p1, p2):
2122 2133 if p not in self.nodemap:
2123 2134 raise error.LookupError(p, self.indexfile,
2124 2135 _('unknown parent'))
2125 2136
2126 2137 if deltabase not in self.nodemap:
2127 2138 raise error.LookupError(deltabase, self.indexfile,
2128 2139 _('unknown delta base'))
2129 2140
2130 2141 baserev = self.rev(deltabase)
2131 2142
2132 2143 if baserev != nullrev and self.iscensored(baserev):
2133 2144 # if base is censored, delta must be full replacement in a
2134 2145 # single patch operation
2135 2146 hlen = struct.calcsize(">lll")
2136 2147 oldlen = self.rawsize(baserev)
2137 2148 newlen = len(delta) - hlen
2138 2149 if delta[:hlen] != mdiff.replacediffheader(oldlen, newlen):
2139 2150 raise error.CensoredBaseError(self.indexfile,
2140 2151 self.node(baserev))
2141 2152
2142 2153 if not flags and self._peek_iscensored(baserev, delta, flush):
2143 2154 flags |= REVIDX_ISCENSORED
2144 2155
2145 2156 # We assume consumers of addrevisioncb will want to retrieve
2146 2157 # the added revision, which will require a call to
2147 2158 # revision(). revision() will fast path if there is a cache
2148 2159 # hit. So, we tell _addrevision() to always cache in this case.
2149 2160 # We're only using addgroup() in the context of changegroup
2150 2161 # generation so the revision data can always be handled as raw
2151 2162 # by the flagprocessor.
2152 2163 self._addrevision(node, None, transaction, link,
2153 2164 p1, p2, flags, (baserev, delta),
2154 2165 ifh, dfh,
2155 2166 alwayscache=bool(addrevisioncb),
2156 2167 deltacomputer=deltacomputer)
2157 2168
2158 2169 if addrevisioncb:
2159 2170 addrevisioncb(self, node)
2160 2171
2161 2172 if not dfh and not self._inline:
2162 2173 # addrevision switched from inline to conventional
2163 2174 # reopen the index
2164 2175 ifh.close()
2165 2176 dfh = self._datafp("a+")
2166 2177 ifh = self._indexfp("a+")
2167 2178 self._writinghandles = (ifh, dfh)
2168 2179 finally:
2169 2180 self._writinghandles = None
2170 2181
2171 2182 if dfh:
2172 2183 dfh.close()
2173 2184 ifh.close()
2174 2185
2175 2186 return nodes
2176 2187
2177 2188 def iscensored(self, rev):
2178 2189 """Check if a file revision is censored."""
2179 2190 if not self._censorable:
2180 2191 return False
2181 2192
2182 2193 return self.flags(rev) & REVIDX_ISCENSORED
2183 2194
2184 2195 def _peek_iscensored(self, baserev, delta, flush):
2185 2196 """Quickly check if a delta produces a censored revision."""
2186 2197 if not self._censorable:
2187 2198 return False
2188 2199
2189 2200 return storageutil.deltaiscensored(delta, baserev, self.rawsize)
2190 2201
2191 2202 def getstrippoint(self, minlink):
2192 2203 """find the minimum rev that must be stripped to strip the linkrev
2193 2204
2194 2205 Returns a tuple containing the minimum rev and a set of all revs that
2195 2206 have linkrevs that will be broken by this strip.
2196 2207 """
2197 2208 return storageutil.resolvestripinfo(minlink, len(self) - 1,
2198 2209 self.headrevs(),
2199 2210 self.linkrev, self.parentrevs)
2200 2211
2201 2212 def strip(self, minlink, transaction):
2202 2213 """truncate the revlog on the first revision with a linkrev >= minlink
2203 2214
2204 2215 This function is called when we're stripping revision minlink and
2205 2216 its descendants from the repository.
2206 2217
2207 2218 We have to remove all revisions with linkrev >= minlink, because
2208 2219 the equivalent changelog revisions will be renumbered after the
2209 2220 strip.
2210 2221
2211 2222 So we truncate the revlog on the first of these revisions, and
2212 2223 trust that the caller has saved the revisions that shouldn't be
2213 2224 removed and that it'll re-add them after this truncation.
2214 2225 """
2215 2226 if len(self) == 0:
2216 2227 return
2217 2228
2218 2229 rev, _ = self.getstrippoint(minlink)
2219 2230 if rev == len(self):
2220 2231 return
2221 2232
2222 2233 # first truncate the files on disk
2223 2234 end = self.start(rev)
2224 2235 if not self._inline:
2225 2236 transaction.add(self.datafile, end)
2226 2237 end = rev * self._io.size
2227 2238 else:
2228 2239 end += rev * self._io.size
2229 2240
2230 2241 transaction.add(self.indexfile, end)
2231 2242
2232 2243 # then reset internal state in memory to forget those revisions
2233 2244 self._revisioncache = None
2234 2245 self._chaininfocache = {}
2235 2246 self._chunkclear()
2236 2247 for x in pycompat.xrange(rev, len(self)):
2237 2248 del self.nodemap[self.node(x)]
2238 2249
2239 2250 del self.index[rev:-1]
2240 2251 self._nodepos = None
2241 2252
2242 2253 def checksize(self):
2243 2254 """Check size of index and data files
2244 2255
2245 2256 return a (dd, di) tuple.
2246 2257 - dd: extra bytes for the "data" file
2247 2258 - di: extra bytes for the "index" file
2248 2259
2249 2260 A healthy revlog will return (0, 0).
2250 2261 """
2251 2262 expected = 0
2252 2263 if len(self):
2253 2264 expected = max(0, self.end(len(self) - 1))
2254 2265
2255 2266 try:
2256 2267 with self._datafp() as f:
2257 2268 f.seek(0, io.SEEK_END)
2258 2269 actual = f.tell()
2259 2270 dd = actual - expected
2260 2271 except IOError as inst:
2261 2272 if inst.errno != errno.ENOENT:
2262 2273 raise
2263 2274 dd = 0
2264 2275
2265 2276 try:
2266 2277 f = self.opener(self.indexfile)
2267 2278 f.seek(0, io.SEEK_END)
2268 2279 actual = f.tell()
2269 2280 f.close()
2270 2281 s = self._io.size
2271 2282 i = max(0, actual // s)
2272 2283 di = actual - (i * s)
2273 2284 if self._inline:
2274 2285 databytes = 0
2275 2286 for r in self:
2276 2287 databytes += max(0, self.length(r))
2277 2288 dd = 0
2278 2289 di = actual - len(self) * s - databytes
2279 2290 except IOError as inst:
2280 2291 if inst.errno != errno.ENOENT:
2281 2292 raise
2282 2293 di = 0
2283 2294
2284 2295 return (dd, di)
2285 2296
2286 2297 def files(self):
2287 2298 res = [self.indexfile]
2288 2299 if not self._inline:
2289 2300 res.append(self.datafile)
2290 2301 return res
2291 2302
2292 2303 def emitrevisions(self, nodes, nodesorder=None, revisiondata=False,
2293 2304 assumehaveparentrevisions=False,
2294 2305 deltamode=repository.CG_DELTAMODE_STD):
2295 2306 if nodesorder not in ('nodes', 'storage', 'linear', None):
2296 2307 raise error.ProgrammingError('unhandled value for nodesorder: %s' %
2297 2308 nodesorder)
2298 2309
2299 2310 if nodesorder is None and not self._generaldelta:
2300 2311 nodesorder = 'storage'
2301 2312
2302 2313 if (not self._storedeltachains and
2303 2314 deltamode != repository.CG_DELTAMODE_PREV):
2304 2315 deltamode = repository.CG_DELTAMODE_FULL
2305 2316
2306 2317 return storageutil.emitrevisions(
2307 2318 self, nodes, nodesorder, revlogrevisiondelta,
2308 2319 deltaparentfn=self.deltaparent,
2309 2320 candeltafn=self.candelta,
2310 2321 rawsizefn=self.rawsize,
2311 2322 revdifffn=self.revdiff,
2312 2323 flagsfn=self.flags,
2313 2324 deltamode=deltamode,
2314 2325 revisiondata=revisiondata,
2315 2326 assumehaveparentrevisions=assumehaveparentrevisions)
2316 2327
2317 2328 DELTAREUSEALWAYS = 'always'
2318 2329 DELTAREUSESAMEREVS = 'samerevs'
2319 2330 DELTAREUSENEVER = 'never'
2320 2331
2321 2332 DELTAREUSEFULLADD = 'fulladd'
2322 2333
2323 2334 DELTAREUSEALL = {'always', 'samerevs', 'never', 'fulladd'}
2324 2335
2325 2336 def clone(self, tr, destrevlog, addrevisioncb=None,
2326 2337 deltareuse=DELTAREUSESAMEREVS, forcedeltabothparents=None):
2327 2338 """Copy this revlog to another, possibly with format changes.
2328 2339
2329 2340 The destination revlog will contain the same revisions and nodes.
2330 2341 However, it may not be bit-for-bit identical due to e.g. delta encoding
2331 2342 differences.
2332 2343
2333 2344 The ``deltareuse`` argument control how deltas from the existing revlog
2334 2345 are preserved in the destination revlog. The argument can have the
2335 2346 following values:
2336 2347
2337 2348 DELTAREUSEALWAYS
2338 2349 Deltas will always be reused (if possible), even if the destination
2339 2350 revlog would not select the same revisions for the delta. This is the
2340 2351 fastest mode of operation.
2341 2352 DELTAREUSESAMEREVS
2342 2353 Deltas will be reused if the destination revlog would pick the same
2343 2354 revisions for the delta. This mode strikes a balance between speed
2344 2355 and optimization.
2345 2356 DELTAREUSENEVER
2346 2357 Deltas will never be reused. This is the slowest mode of execution.
2347 2358 This mode can be used to recompute deltas (e.g. if the diff/delta
2348 2359 algorithm changes).
2349 2360
2350 2361 Delta computation can be slow, so the choice of delta reuse policy can
2351 2362 significantly affect run time.
2352 2363
2353 2364 The default policy (``DELTAREUSESAMEREVS``) strikes a balance between
2354 2365 two extremes. Deltas will be reused if they are appropriate. But if the
2355 2366 delta could choose a better revision, it will do so. This means if you
2356 2367 are converting a non-generaldelta revlog to a generaldelta revlog,
2357 2368 deltas will be recomputed if the delta's parent isn't a parent of the
2358 2369 revision.
2359 2370
2360 2371 In addition to the delta policy, the ``forcedeltabothparents``
2361 2372 argument controls whether to force compute deltas against both parents
2362 2373 for merges. By default, the current default is used.
2363 2374 """
2364 2375 if deltareuse not in self.DELTAREUSEALL:
2365 2376 raise ValueError(_('value for deltareuse invalid: %s') % deltareuse)
2366 2377
2367 2378 if len(destrevlog):
2368 2379 raise ValueError(_('destination revlog is not empty'))
2369 2380
2370 2381 if getattr(self, 'filteredrevs', None):
2371 2382 raise ValueError(_('source revlog has filtered revisions'))
2372 2383 if getattr(destrevlog, 'filteredrevs', None):
2373 2384 raise ValueError(_('destination revlog has filtered revisions'))
2374 2385
2375 2386 # lazydelta and lazydeltabase controls whether to reuse a cached delta,
2376 2387 # if possible.
2377 2388 oldlazydelta = destrevlog._lazydelta
2378 2389 oldlazydeltabase = destrevlog._lazydeltabase
2379 2390 oldamd = destrevlog._deltabothparents
2380 2391
2381 2392 try:
2382 2393 if deltareuse == self.DELTAREUSEALWAYS:
2383 2394 destrevlog._lazydeltabase = True
2384 2395 destrevlog._lazydelta = True
2385 2396 elif deltareuse == self.DELTAREUSESAMEREVS:
2386 2397 destrevlog._lazydeltabase = False
2387 2398 destrevlog._lazydelta = True
2388 2399 elif deltareuse == self.DELTAREUSENEVER:
2389 2400 destrevlog._lazydeltabase = False
2390 2401 destrevlog._lazydelta = False
2391 2402
2392 2403 destrevlog._deltabothparents = forcedeltabothparents or oldamd
2393 2404
2394 2405 deltacomputer = deltautil.deltacomputer(destrevlog)
2395 2406 index = self.index
2396 2407 for rev in self:
2397 2408 entry = index[rev]
2398 2409
2399 2410 # Some classes override linkrev to take filtered revs into
2400 2411 # account. Use raw entry from index.
2401 2412 flags = entry[0] & 0xffff
2402 2413 linkrev = entry[4]
2403 2414 p1 = index[entry[5]][7]
2404 2415 p2 = index[entry[6]][7]
2405 2416 node = entry[7]
2406 2417
2407 2418 # (Possibly) reuse the delta from the revlog if allowed and
2408 2419 # the revlog chunk is a delta.
2409 2420 cachedelta = None
2410 2421 rawtext = None
2411 2422 if (deltareuse != self.DELTAREUSEFULLADD
2412 2423 and destrevlog._lazydelta):
2413 2424 dp = self.deltaparent(rev)
2414 2425 if dp != nullrev:
2415 2426 cachedelta = (dp, bytes(self._chunk(rev)))
2416 2427
2417 2428 if not cachedelta:
2418 2429 rawtext = self.rawdata(rev)
2419 2430
2420 2431
2421 2432 if deltareuse == self.DELTAREUSEFULLADD:
2422 2433 destrevlog.addrevision(rawtext, tr, linkrev, p1, p2,
2423 2434 cachedelta=cachedelta,
2424 2435 node=node, flags=flags,
2425 2436 deltacomputer=deltacomputer)
2426 2437 else:
2427 2438 ifh = destrevlog.opener(destrevlog.indexfile, 'a+',
2428 2439 checkambig=False)
2429 2440 dfh = None
2430 2441 if not destrevlog._inline:
2431 2442 dfh = destrevlog.opener(destrevlog.datafile, 'a+')
2432 2443 try:
2433 2444 destrevlog._addrevision(node, rawtext, tr, linkrev, p1,
2434 2445 p2, flags, cachedelta, ifh, dfh,
2435 2446 deltacomputer=deltacomputer)
2436 2447 finally:
2437 2448 if dfh:
2438 2449 dfh.close()
2439 2450 ifh.close()
2440 2451
2441 2452 if addrevisioncb:
2442 2453 addrevisioncb(self, rev, node)
2443 2454 finally:
2444 2455 destrevlog._lazydelta = oldlazydelta
2445 2456 destrevlog._lazydeltabase = oldlazydeltabase
2446 2457 destrevlog._deltabothparents = oldamd
2447 2458
2448 2459 def censorrevision(self, tr, censornode, tombstone=b''):
2449 2460 if (self.version & 0xFFFF) == REVLOGV0:
2450 2461 raise error.RevlogError(_('cannot censor with version %d revlogs') %
2451 2462 self.version)
2452 2463
2453 2464 censorrev = self.rev(censornode)
2454 2465 tombstone = storageutil.packmeta({b'censored': tombstone}, b'')
2455 2466
2456 2467 if len(tombstone) > self.rawsize(censorrev):
2457 2468 raise error.Abort(_('censor tombstone must be no longer than '
2458 2469 'censored data'))
2459 2470
2460 2471 # Rewriting the revlog in place is hard. Our strategy for censoring is
2461 2472 # to create a new revlog, copy all revisions to it, then replace the
2462 2473 # revlogs on transaction close.
2463 2474
2464 2475 newindexfile = self.indexfile + b'.tmpcensored'
2465 2476 newdatafile = self.datafile + b'.tmpcensored'
2466 2477
2467 2478 # This is a bit dangerous. We could easily have a mismatch of state.
2468 2479 newrl = revlog(self.opener, newindexfile, newdatafile,
2469 2480 censorable=True)
2470 2481 newrl.version = self.version
2471 2482 newrl._generaldelta = self._generaldelta
2472 2483 newrl._io = self._io
2473 2484
2474 2485 for rev in self.revs():
2475 2486 node = self.node(rev)
2476 2487 p1, p2 = self.parents(node)
2477 2488
2478 2489 if rev == censorrev:
2479 2490 newrl.addrawrevision(tombstone, tr, self.linkrev(censorrev),
2480 2491 p1, p2, censornode, REVIDX_ISCENSORED)
2481 2492
2482 2493 if newrl.deltaparent(rev) != nullrev:
2483 2494 raise error.Abort(_('censored revision stored as delta; '
2484 2495 'cannot censor'),
2485 2496 hint=_('censoring of revlogs is not '
2486 2497 'fully implemented; please report '
2487 2498 'this bug'))
2488 2499 continue
2489 2500
2490 2501 if self.iscensored(rev):
2491 2502 if self.deltaparent(rev) != nullrev:
2492 2503 raise error.Abort(_('cannot censor due to censored '
2493 2504 'revision having delta stored'))
2494 2505 rawtext = self._chunk(rev)
2495 2506 else:
2496 2507 rawtext = self.rawdata(rev)
2497 2508
2498 2509 newrl.addrawrevision(rawtext, tr, self.linkrev(rev), p1, p2, node,
2499 2510 self.flags(rev))
2500 2511
2501 2512 tr.addbackup(self.indexfile, location='store')
2502 2513 if not self._inline:
2503 2514 tr.addbackup(self.datafile, location='store')
2504 2515
2505 2516 self.opener.rename(newrl.indexfile, self.indexfile)
2506 2517 if not self._inline:
2507 2518 self.opener.rename(newrl.datafile, self.datafile)
2508 2519
2509 2520 self.clearcaches()
2510 2521 self._loadindex()
2511 2522
2512 2523 def verifyintegrity(self, state):
2513 2524 """Verifies the integrity of the revlog.
2514 2525
2515 2526 Yields ``revlogproblem`` instances describing problems that are
2516 2527 found.
2517 2528 """
2518 2529 dd, di = self.checksize()
2519 2530 if dd:
2520 2531 yield revlogproblem(error=_('data length off by %d bytes') % dd)
2521 2532 if di:
2522 2533 yield revlogproblem(error=_('index contains %d extra bytes') % di)
2523 2534
2524 2535 version = self.version & 0xFFFF
2525 2536
2526 2537 # The verifier tells us what version revlog we should be.
2527 2538 if version != state['expectedversion']:
2528 2539 yield revlogproblem(
2529 2540 warning=_("warning: '%s' uses revlog format %d; expected %d") %
2530 2541 (self.indexfile, version, state['expectedversion']))
2531 2542
2532 2543 state['skipread'] = set()
2533 2544
2534 2545 for rev in self:
2535 2546 node = self.node(rev)
2536 2547
2537 2548 # Verify contents. 4 cases to care about:
2538 2549 #
2539 2550 # common: the most common case
2540 2551 # rename: with a rename
2541 2552 # meta: file content starts with b'\1\n', the metadata
2542 2553 # header defined in filelog.py, but without a rename
2543 2554 # ext: content stored externally
2544 2555 #
2545 2556 # More formally, their differences are shown below:
2546 2557 #
2547 2558 # | common | rename | meta | ext
2548 2559 # -------------------------------------------------------
2549 2560 # flags() | 0 | 0 | 0 | not 0
2550 2561 # renamed() | False | True | False | ?
2551 2562 # rawtext[0:2]=='\1\n'| False | True | True | ?
2552 2563 #
2553 2564 # "rawtext" means the raw text stored in revlog data, which
2554 2565 # could be retrieved by "rawdata(rev)". "text"
2555 2566 # mentioned below is "revision(rev)".
2556 2567 #
2557 2568 # There are 3 different lengths stored physically:
2558 2569 # 1. L1: rawsize, stored in revlog index
2559 2570 # 2. L2: len(rawtext), stored in revlog data
2560 2571 # 3. L3: len(text), stored in revlog data if flags==0, or
2561 2572 # possibly somewhere else if flags!=0
2562 2573 #
2563 2574 # L1 should be equal to L2. L3 could be different from them.
2564 2575 # "text" may or may not affect commit hash depending on flag
2565 2576 # processors (see flagutil.addflagprocessor).
2566 2577 #
2567 2578 # | common | rename | meta | ext
2568 2579 # -------------------------------------------------
2569 2580 # rawsize() | L1 | L1 | L1 | L1
2570 2581 # size() | L1 | L2-LM | L1(*) | L1 (?)
2571 2582 # len(rawtext) | L2 | L2 | L2 | L2
2572 2583 # len(text) | L2 | L2 | L2 | L3
2573 2584 # len(read()) | L2 | L2-LM | L2-LM | L3 (?)
2574 2585 #
2575 2586 # LM: length of metadata, depending on rawtext
2576 2587 # (*): not ideal, see comment in filelog.size
2577 2588 # (?): could be "- len(meta)" if the resolved content has
2578 2589 # rename metadata
2579 2590 #
2580 2591 # Checks needed to be done:
2581 2592 # 1. length check: L1 == L2, in all cases.
2582 2593 # 2. hash check: depending on flag processor, we may need to
2583 2594 # use either "text" (external), or "rawtext" (in revlog).
2584 2595
2585 2596 try:
2586 2597 skipflags = state.get('skipflags', 0)
2587 2598 if skipflags:
2588 2599 skipflags &= self.flags(rev)
2589 2600
2590 2601 if skipflags:
2591 2602 state['skipread'].add(node)
2592 2603 else:
2593 2604 # Side-effect: read content and verify hash.
2594 2605 self.revision(node)
2595 2606
2596 2607 l1 = self.rawsize(rev)
2597 2608 l2 = len(self.rawdata(node))
2598 2609
2599 2610 if l1 != l2:
2600 2611 yield revlogproblem(
2601 2612 error=_('unpacked size is %d, %d expected') % (l2, l1),
2602 2613 node=node)
2603 2614
2604 2615 except error.CensoredNodeError:
2605 2616 if state['erroroncensored']:
2606 2617 yield revlogproblem(error=_('censored file data'),
2607 2618 node=node)
2608 2619 state['skipread'].add(node)
2609 2620 except Exception as e:
2610 2621 yield revlogproblem(
2611 2622 error=_('unpacking %s: %s') % (short(node),
2612 2623 stringutil.forcebytestr(e)),
2613 2624 node=node)
2614 2625 state['skipread'].add(node)
2615 2626
2616 2627 def storageinfo(self, exclusivefiles=False, sharedfiles=False,
2617 2628 revisionscount=False, trackedsize=False,
2618 2629 storedsize=False):
2619 2630 d = {}
2620 2631
2621 2632 if exclusivefiles:
2622 2633 d['exclusivefiles'] = [(self.opener, self.indexfile)]
2623 2634 if not self._inline:
2624 2635 d['exclusivefiles'].append((self.opener, self.datafile))
2625 2636
2626 2637 if sharedfiles:
2627 2638 d['sharedfiles'] = []
2628 2639
2629 2640 if revisionscount:
2630 2641 d['revisionscount'] = len(self)
2631 2642
2632 2643 if trackedsize:
2633 2644 d['trackedsize'] = sum(map(self.rawsize, iter(self)))
2634 2645
2635 2646 if storedsize:
2636 2647 d['storedsize'] = sum(self.opener.stat(path).st_size
2637 2648 for path in self.files())
2638 2649
2639 2650 return d
@@ -1,206 +1,195 b''
1 1 # flagutils.py - code to deal with revlog flags and their processors
2 2 #
3 3 # Copyright 2016 Remi Chaintron <remi@fb.com>
4 4 # Copyright 2016-2019 Pierre-Yves David <pierre-yves.david@ens-lyon.org>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 from __future__ import absolute_import
10 10
11 11 from ..i18n import _
12 12
13 13 from .constants import (
14 14 REVIDX_DEFAULT_FLAGS,
15 15 REVIDX_ELLIPSIS,
16 16 REVIDX_EXTSTORED,
17 17 REVIDX_FLAGS_ORDER,
18 18 REVIDX_ISCENSORED,
19 19 REVIDX_RAWTEXT_CHANGING_FLAGS,
20 20 )
21 21
22 22 from .. import (
23 23 error,
24 24 util
25 25 )
26 26
27 27 # blanked usage of all the name to prevent pyflakes constraints
28 28 # We need these name available in the module for extensions.
29 29 REVIDX_ISCENSORED
30 30 REVIDX_ELLIPSIS
31 31 REVIDX_EXTSTORED
32 32 REVIDX_DEFAULT_FLAGS
33 33 REVIDX_FLAGS_ORDER
34 34 REVIDX_RAWTEXT_CHANGING_FLAGS
35 35
36 36 REVIDX_KNOWN_FLAGS = util.bitsfrom(REVIDX_FLAGS_ORDER)
37 37
38 38 # Store flag processors (cf. 'addflagprocessor()' to register)
39 39 flagprocessors = {
40 40 REVIDX_ISCENSORED: None,
41 41 }
42 42
43 43 def addflagprocessor(flag, processor):
44 44 """Register a flag processor on a revision data flag.
45 45
46 46 Invariant:
47 47 - Flags need to be defined in REVIDX_KNOWN_FLAGS and REVIDX_FLAGS_ORDER,
48 48 and REVIDX_RAWTEXT_CHANGING_FLAGS if they can alter rawtext.
49 49 - Only one flag processor can be registered on a specific flag.
50 50 - flagprocessors must be 3-tuples of functions (read, write, raw) with the
51 51 following signatures:
52 52 - (read) f(self, rawtext) -> text, bool
53 53 - (write) f(self, text) -> rawtext, bool
54 54 - (raw) f(self, rawtext) -> bool
55 55 "text" is presented to the user. "rawtext" is stored in revlog data, not
56 56 directly visible to the user.
57 57 The boolean returned by these transforms is used to determine whether
58 58 the returned text can be used for hash integrity checking. For example,
59 59 if "write" returns False, then "text" is used to generate hash. If
60 60 "write" returns True, that basically means "rawtext" returned by "write"
61 61 should be used to generate hash. Usually, "write" and "read" return
62 62 different booleans. And "raw" returns a same boolean as "write".
63 63
64 64 Note: The 'raw' transform is used for changegroup generation and in some
65 65 debug commands. In this case the transform only indicates whether the
66 66 contents can be used for hash integrity checks.
67 67 """
68 68 insertflagprocessor(flag, processor, flagprocessors)
69 69
70 70 def insertflagprocessor(flag, processor, flagprocessors):
71 71 if not flag & REVIDX_KNOWN_FLAGS:
72 72 msg = _("cannot register processor on unknown flag '%#x'.") % (flag)
73 73 raise error.ProgrammingError(msg)
74 74 if flag not in REVIDX_FLAGS_ORDER:
75 75 msg = _("flag '%#x' undefined in REVIDX_FLAGS_ORDER.") % (flag)
76 76 raise error.ProgrammingError(msg)
77 77 if flag in flagprocessors:
78 78 msg = _("cannot register multiple processors on flag '%#x'.") % (flag)
79 79 raise error.Abort(msg)
80 80 flagprocessors[flag] = processor
81 81
82 82 class flagprocessorsmixin(object):
83 83 """basic mixin to support revlog flag processing
84 84
85 85 Make sure the `_flagprocessors` attribute is set at ``__init__`` time.
86 86
87 87 See the documentation of the ``_processflags`` method for details.
88 88 """
89 89
90 90 _flagserrorclass = error.RevlogError
91 91
92 def _processflags(self, text, flags, operation, raw=False):
93 """deprecated entry point to access flag processors"""
94 msg = ('_processflag(...) use the specialized variant')
95 util.nouideprecwarn(msg, '5.2', stacklevel=2)
96 if raw:
97 return text, processflagsraw(self, text, flags)
98 elif operation == 'read':
99 return processflagsread(self, text, flags)
100 else: # write operation
101 return processflagswrite(self, text, flags)
102
103 92 def processflagswrite(revlog, text, flags, sidedata):
104 93 """Inspect revision data flags and applies write transformations defined
105 94 by registered flag processors.
106 95
107 96 ``text`` - the revision data to process
108 97 ``flags`` - the revision flags
109 98
110 99 This method processes the flags in the order (or reverse order if
111 100 ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
112 101 flag processors registered for present flags. The order of flags defined
113 102 in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
114 103
115 104 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
116 105 processed text and ``validatehash`` is a bool indicating whether the
117 106 returned text should be checked for hash integrity.
118 107 """
119 108 return _processflagsfunc(revlog, text, flags, 'write',
120 109 sidedata=sidedata)[:2]
121 110
122 111 def processflagsread(revlog, text, flags):
123 112 """Inspect revision data flags and applies read transformations defined
124 113 by registered flag processors.
125 114
126 115 ``text`` - the revision data to process
127 116 ``flags`` - the revision flags
128 117 ``raw`` - an optional argument describing if the raw transform should be
129 118 applied.
130 119
131 120 This method processes the flags in the order (or reverse order if
132 121 ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
133 122 flag processors registered for present flags. The order of flags defined
134 123 in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
135 124
136 125 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
137 126 processed text and ``validatehash`` is a bool indicating whether the
138 127 returned text should be checked for hash integrity.
139 128 """
140 129 return _processflagsfunc(revlog, text, flags, 'read')
141 130
142 131 def processflagsraw(revlog, text, flags):
143 132 """Inspect revision data flags to check is the content hash should be
144 133 validated.
145 134
146 135 ``text`` - the revision data to process
147 136 ``flags`` - the revision flags
148 137
149 138 This method processes the flags in the order (or reverse order if
150 139 ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
151 140 flag processors registered for present flags. The order of flags defined
152 141 in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
153 142
154 143 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
155 144 processed text and ``validatehash`` is a bool indicating whether the
156 145 returned text should be checked for hash integrity.
157 146 """
158 147 return _processflagsfunc(revlog, text, flags, 'raw')[1]
159 148
160 149 def _processflagsfunc(revlog, text, flags, operation, sidedata=None):
161 150 """internal function to process flag on a revlog
162 151
163 152 This function is private to this module, code should never needs to call it
164 153 directly."""
165 154 # fast path: no flag processors will run
166 155 if flags == 0:
167 156 return text, True, {}
168 157 if operation not in ('read', 'write', 'raw'):
169 158 raise error.ProgrammingError(_("invalid '%s' operation") %
170 159 operation)
171 160 # Check all flags are known.
172 161 if flags & ~REVIDX_KNOWN_FLAGS:
173 162 raise revlog._flagserrorclass(_("incompatible revision flag '%#x'") %
174 163 (flags & ~REVIDX_KNOWN_FLAGS))
175 164 validatehash = True
176 165 # Depending on the operation (read or write), the order might be
177 166 # reversed due to non-commutative transforms.
178 167 orderedflags = REVIDX_FLAGS_ORDER
179 168 if operation == 'write':
180 169 orderedflags = reversed(orderedflags)
181 170
182 171 outsidedata = {}
183 172 for flag in orderedflags:
184 173 # If a flagprocessor has been registered for a known flag, apply the
185 174 # related operation transform and update result tuple.
186 175 if flag & flags:
187 176 vhash = True
188 177
189 178 if flag not in revlog._flagprocessors:
190 179 message = _("missing processor for flag '%#x'") % (flag)
191 180 raise revlog._flagserrorclass(message)
192 181
193 182 processor = revlog._flagprocessors[flag]
194 183 if processor is not None:
195 184 readtransform, writetransform, rawtransform = processor
196 185
197 186 if operation == 'raw':
198 187 vhash = rawtransform(revlog, text)
199 188 elif operation == 'read':
200 189 text, vhash, s = readtransform(revlog, text)
201 190 outsidedata.update(s)
202 191 else: # write operation
203 192 text, vhash = writetransform(revlog, text, sidedata)
204 193 validatehash = validatehash and vhash
205 194
206 195 return text, validatehash, outsidedata
General Comments 0
You need to be logged in to leave comments. Login now