tests/testlib/ext-sidedata-3.py
changeset 46718 ba8e508a8e69
parent 46709 3d740058b467
child 47073 64cd1496bb70
equal deleted inserted replaced
46717:502e795b55ac 46718:ba8e508a8e69
       
     1 # coding: utf8
       
     2 # ext-sidedata-3.py - small extension to test (differently still) the sidedata
       
     3 # logic
       
     4 #
       
     5 # Simulates a client for a complex sidedata exchange.
       
     6 #
       
     7 # Copyright 2021 Raphaël Gomès <rgomes@octobus.net>
       
     8 #
       
     9 # This software may be used and distributed according to the terms of the
       
    10 # GNU General Public License version 2 or any later version.
       
    11 
       
    12 from __future__ import absolute_import
       
    13 
       
    14 import hashlib
       
    15 import struct
       
    16 
       
    17 from mercurial import (
       
    18     extensions,
       
    19     revlog,
       
    20 )
       
    21 
       
    22 from mercurial.revlogutils import sidedata as sidedatamod
       
    23 
       
    24 
       
    25 def compute_sidedata_1(repo, revlog, rev, sidedata, text=None):
       
    26     sidedata = sidedata.copy()
       
    27     if text is None:
       
    28         text = revlog.revision(rev)
       
    29     sidedata[sidedatamod.SD_TEST1] = struct.pack('>I', len(text))
       
    30     return sidedata
       
    31 
       
    32 
       
    33 def compute_sidedata_2(repo, revlog, rev, sidedata, text=None):
       
    34     sidedata = sidedata.copy()
       
    35     if text is None:
       
    36         text = revlog.revision(rev)
       
    37     sha256 = hashlib.sha256(text).digest()
       
    38     sidedata[sidedatamod.SD_TEST2] = struct.pack('>32s', sha256)
       
    39     return sidedata
       
    40 
       
    41 
       
    42 def compute_sidedata_3(repo, revlog, rev, sidedata, text=None):
       
    43     sidedata = sidedata.copy()
       
    44     if text is None:
       
    45         text = revlog.revision(rev)
       
    46     sha384 = hashlib.sha384(text).digest()
       
    47     sidedata[sidedatamod.SD_TEST3] = struct.pack('>48s', sha384)
       
    48     return sidedata
       
    49 
       
    50 
       
    51 def wrapaddrevision(
       
    52     orig, self, text, transaction, link, p1, p2, *args, **kwargs
       
    53 ):
       
    54     if kwargs.get('sidedata') is None:
       
    55         kwargs['sidedata'] = {}
       
    56     sd = kwargs['sidedata']
       
    57     sd = compute_sidedata_1(None, self, None, sd, text=text)
       
    58     kwargs['sidedata'] = compute_sidedata_2(None, self, None, sd, text=text)
       
    59     return orig(self, text, transaction, link, p1, p2, *args, **kwargs)
       
    60 
       
    61 
       
    62 def extsetup(ui):
       
    63     extensions.wrapfunction(revlog.revlog, 'addrevision', wrapaddrevision)
       
    64 
       
    65 
       
    66 def reposetup(ui, repo):
       
    67     # Sidedata keys happen to be the same as the categories, easier for testing.
       
    68     for kind in (b'changelog', b'manifest', b'filelog'):
       
    69         repo.register_sidedata_computer(
       
    70             kind,
       
    71             sidedatamod.SD_TEST1,
       
    72             (sidedatamod.SD_TEST1,),
       
    73             compute_sidedata_1,
       
    74         )
       
    75         repo.register_sidedata_computer(
       
    76             kind,
       
    77             sidedatamod.SD_TEST2,
       
    78             (sidedatamod.SD_TEST2,),
       
    79             compute_sidedata_2,
       
    80         )
       
    81         repo.register_sidedata_computer(
       
    82             kind,
       
    83             sidedatamod.SD_TEST3,
       
    84             (sidedatamod.SD_TEST3,),
       
    85             compute_sidedata_3,
       
    86         )
       
    87     repo.register_wanted_sidedata(sidedatamod.SD_TEST1)
       
    88     repo.register_wanted_sidedata(sidedatamod.SD_TEST2)