Debian Codegraph @

tests/testlib/ext-sidedata-5.py

raw
1 # ext-sidedata-5.py - small extension to test (differently still) the sidedata
2 # logic
3 #
4 # Simulates a server for a simple 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
21 from mercurial.revlogutils import sidedata as sidedatamod
22 from mercurial.revlogutils import constants
23
24 NO_FLAGS = (0, 0)
25
26
27 def compute_sidedata_1(repo, revlog, rev, sidedata, text=None):
28 sidedata = sidedata.copy()
29 if text is None:
30 text = revlog.revision(rev)
31 sidedata[sidedatamod.SD_TEST1] = struct.pack('>I', len(text))
32 return sidedata, NO_FLAGS
33
34
35 def compute_sidedata_2(repo, revlog, rev, sidedata, text=None):
36 sidedata = sidedata.copy()
37 if text is None:
38 text = revlog.revision(rev)
39 sha256 = hashlib.sha256(text).digest()
40 sidedata[sidedatamod.SD_TEST2] = struct.pack('>32s', sha256)
41 return sidedata, NO_FLAGS
42
43
44 def reposetup(ui, repo):
45 # Sidedata keys happen to be the same as the categories, easier for testing.
46 for kind in constants.ALL_KINDS:
47 repo.register_sidedata_computer(
48 kind,
49 sidedatamod.SD_TEST1,
50 (sidedatamod.SD_TEST1,),
51 compute_sidedata_1,
52 0,
53 )
54 repo.register_sidedata_computer(
55 kind,
56 sidedatamod.SD_TEST2,
57 (sidedatamod.SD_TEST2,),
58 compute_sidedata_2,
59 0,
60 )
61
62 # We don't register sidedata computers because we don't care within these
63 # tests
64 repo.register_wanted_sidedata(sidedatamod.SD_TEST1)
65 repo.register_wanted_sidedata(sidedatamod.SD_TEST2)
66
67
68 def wrapaddrevision(
69 orig, self, text, transaction, link, p1, p2, *args, **kwargs
70 ):
71 if kwargs.get('sidedata') is None:
72 kwargs['sidedata'] = {}
73 sd = kwargs['sidedata']
74 ## let's store some arbitrary data just for testing
75 # text length
76 sd[sidedatamod.SD_TEST1] = struct.pack('>I', len(text))
77 # and sha2 hashes
78 sha256 = hashlib.sha256(text).digest()
79 sd[sidedatamod.SD_TEST2] = struct.pack('>32s', sha256)
80 return orig(self, text, transaction, link, p1, p2, *args, **kwargs)
81
82
83 def extsetup(ui):
84 extensions.wrapfunction(revlog.revlog, 'addrevision', wrapaddrevision)