Debian Codegraph @

tests/testlib/ext-sidedata-3.py

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