comparison mercurial/obsolete.py @ 45463:145cfe84d3e4

obsstore: refactor v1 logic to fix 32 byte hash support Refactor the v1 logic to determine the node parsing based on the flag. Move the predecessor out of the fixed part and handle it like the other nodes, removing most of the duplicated code for parsing 20/32 bytes hashes. Differential Revision: https://phab.mercurial-scm.org/D8801
author Joerg Sonnenberger <joerg@bec.de>
date Thu, 23 Jul 2020 20:23:44 +0200
parents 9d2b2df2c2ba
children 89a2afe31e82
comparison
equal deleted inserted replaced
45462:c1d6e930ac8a 45463:145cfe84d3e4
326 # 326 #
327 # - M*(uint8, uint8): size of all metadata entries (key and value) 327 # - M*(uint8, uint8): size of all metadata entries (key and value)
328 # 328 #
329 # - remaining bytes: the metadata, each (key, value) pair after the other. 329 # - remaining bytes: the metadata, each (key, value) pair after the other.
330 _fm1version = 1 330 _fm1version = 1
331 _fm1fixed = b'>IdhHBBB20s' 331 _fm1fixed = b'>IdhHBBB'
332 _fm1nodesha1 = b'20s' 332 _fm1nodesha1 = b'20s'
333 _fm1nodesha256 = b'32s' 333 _fm1nodesha256 = b'32s'
334 _fm1nodesha1size = _calcsize(_fm1nodesha1) 334 _fm1nodesha1size = _calcsize(_fm1nodesha1)
335 _fm1nodesha256size = _calcsize(_fm1nodesha256) 335 _fm1nodesha256size = _calcsize(_fm1nodesha256)
336 _fm1fsize = _calcsize(_fm1fixed) 336 _fm1fsize = _calcsize(_fm1fixed)
358 ufixed = struct.Struct(_fm1fixed).unpack 358 ufixed = struct.Struct(_fm1fixed).unpack
359 359
360 while off < stop: 360 while off < stop:
361 # read fixed part 361 # read fixed part
362 o1 = off + fsize 362 o1 = off + fsize
363 t, secs, tz, flags, numsuc, numpar, nummeta, prec = ufixed(data[off:o1]) 363 t, secs, tz, flags, numsuc, numpar, nummeta = ufixed(data[off:o1])
364 364
365 if flags & sha2flag: 365 if flags & sha2flag:
366 # FIXME: prec was read as a SHA1, needs to be amended 366 nodefmt = sha2fmt
367 367 nodesize = sha2size
368 # read 0 or more successors
369 if numsuc == 1:
370 o2 = o1 + sha2size
371 sucs = (data[o1:o2],)
372 else:
373 o2 = o1 + sha2size * numsuc
374 sucs = unpack(sha2fmt * numsuc, data[o1:o2])
375
376 # read parents
377 if numpar == noneflag:
378 o3 = o2
379 parents = None
380 elif numpar == 1:
381 o3 = o2 + sha2size
382 parents = (data[o2:o3],)
383 else:
384 o3 = o2 + sha2size * numpar
385 parents = unpack(sha2fmt * numpar, data[o2:o3])
386 else: 368 else:
387 # read 0 or more successors 369 nodefmt = sha1fmt
388 if numsuc == 1: 370 nodesize = sha1size
389 o2 = o1 + sha1size 371
390 sucs = (data[o1:o2],) 372 (prec,) = unpack(nodefmt, data[o1 : o1 + nodesize])
391 else: 373 o1 += nodesize
392 o2 = o1 + sha1size * numsuc 374
393 sucs = unpack(sha1fmt * numsuc, data[o1:o2]) 375 # read 0 or more successors
394 376 if numsuc == 1:
395 # read parents 377 o2 = o1 + nodesize
396 if numpar == noneflag: 378 sucs = (data[o1:o2],)
397 o3 = o2 379 else:
398 parents = None 380 o2 = o1 + nodesize * numsuc
399 elif numpar == 1: 381 sucs = unpack(nodefmt * numsuc, data[o1:o2])
400 o3 = o2 + sha1size 382
401 parents = (data[o2:o3],) 383 # read parents
402 else: 384 if numpar == noneflag:
403 o3 = o2 + sha1size * numpar 385 o3 = o2
404 parents = unpack(sha1fmt * numpar, data[o2:o3]) 386 parents = None
387 elif numpar == 1:
388 o3 = o2 + nodesize
389 parents = (data[o2:o3],)
390 else:
391 o3 = o2 + nodesize * numpar
392 parents = unpack(nodefmt * numpar, data[o2:o3])
405 393
406 # read metadata 394 # read metadata
407 off = o3 + metasize * nummeta 395 off = o3 + metasize * nummeta
408 metapairsize = unpack(b'>' + (metafmt * nummeta), data[o3:off]) 396 metapairsize = unpack(b'>' + (metafmt * nummeta), data[o3:off])
409 metadata = [] 397 metadata = []
421 # determine node size 409 # determine node size
422 _fm1node = _fm1nodesha1 410 _fm1node = _fm1nodesha1
423 if flags & usingsha256: 411 if flags & usingsha256:
424 _fm1node = _fm1nodesha256 412 _fm1node = _fm1nodesha256
425 numsuc = len(sucs) 413 numsuc = len(sucs)
426 numextranodes = numsuc 414 numextranodes = 1 + numsuc
427 if parents is None: 415 if parents is None:
428 numpar = _fm1parentnone 416 numpar = _fm1parentnone
429 else: 417 else:
430 numpar = len(parents) 418 numpar = len(parents)
431 numextranodes += numpar 419 numextranodes += numpar
622 `createmarkers` function in this module instead. 610 `createmarkers` function in this module instead.
623 611
624 return True if a new marker have been added, False if the markers 612 return True if a new marker have been added, False if the markers
625 already existed (no op). 613 already existed (no op).
626 """ 614 """
615 flag = int(flag)
627 if metadata is None: 616 if metadata is None:
628 metadata = {} 617 metadata = {}
629 if date is None: 618 if date is None:
630 if b'date' in metadata: 619 if b'date' in metadata:
631 # as a courtesy for out-of-tree extensions 620 # as a courtesy for out-of-tree extensions
634 date = ui.configdate(b'devel', b'default-date') 623 date = ui.configdate(b'devel', b'default-date')
635 if date is None: 624 if date is None:
636 date = dateutil.makedate() 625 date = dateutil.makedate()
637 else: 626 else:
638 date = dateutil.makedate() 627 date = dateutil.makedate()
639 if len(prec) != 20: 628 if flag & usingsha256:
640 raise ValueError(prec) 629 if len(prec) != 32:
641 for succ in succs: 630 raise ValueError(prec)
642 if len(succ) != 20: 631 for succ in succs:
643 raise ValueError(succ) 632 if len(succ) != 32:
633 raise ValueError(succ)
634 else:
635 if len(prec) != 20:
636 raise ValueError(prec)
637 for succ in succs:
638 if len(succ) != 20:
639 raise ValueError(succ)
644 if prec in succs: 640 if prec in succs:
645 raise ValueError( 641 raise ValueError(
646 'in-marker cycle with %s' % pycompat.sysstr(node.hex(prec)) 642 'in-marker cycle with %s' % pycompat.sysstr(node.hex(prec))
647 ) 643 )
648 644
657 b'obsstore metadata must be valid UTF-8 sequence ' 653 b'obsstore metadata must be valid UTF-8 sequence '
658 b'(key = %r, value = %r)' 654 b'(key = %r, value = %r)'
659 % (pycompat.bytestr(k), pycompat.bytestr(v)) 655 % (pycompat.bytestr(k), pycompat.bytestr(v))
660 ) 656 )
661 657
662 marker = (bytes(prec), tuple(succs), int(flag), metadata, date, parents) 658 marker = (bytes(prec), tuple(succs), flag, metadata, date, parents)
663 return bool(self.add(transaction, [marker])) 659 return bool(self.add(transaction, [marker]))
664 660
665 def add(self, transaction, markers): 661 def add(self, transaction, markers):
666 """Add new markers to the store 662 """Add new markers to the store
667 663