##// END OF EJS Templates
sidedata: check that the sidedata safely roundtrip...
marmoute -
r43310:03e76927 default
parent child Browse files
Show More
@@ -1,36 +1,50 b''
1 # ext-sidedata.py - small extension to test the sidedata logic
1 # ext-sidedata.py - small extension to test the sidedata logic
2 #
2 #
3 # Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net)
3 # Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net)
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import hashlib
10 import hashlib
11 import struct
11 import struct
12
12
13 from mercurial import (
13 from mercurial import (
14 extensions,
14 extensions,
15 node,
15 revlog,
16 revlog,
16 )
17 )
17
18
18 from mercurial.revlogutils import (
19 from mercurial.revlogutils import (
19 sidedata,
20 sidedata,
20 )
21 )
21
22
22 def wrapaddrevision(orig, self, text, transaction, link, p1, p2, *args,
23 def wrapaddrevision(orig, self, text, transaction, link, p1, p2, *args,
23 **kwargs):
24 **kwargs):
24 if kwargs.get('sidedata') is None:
25 if kwargs.get('sidedata') is None:
25 kwargs['sidedata'] = {}
26 kwargs['sidedata'] = {}
26 sd = kwargs['sidedata']
27 sd = kwargs['sidedata']
27 ## let's store some arbitrary data just for testing
28 ## let's store some arbitrary data just for testing
28 # text length
29 # text length
29 sd[sidedata.SD_TEST1] = struct.pack('>I', len(text))
30 sd[sidedata.SD_TEST1] = struct.pack('>I', len(text))
30 # and sha2 hashes
31 # and sha2 hashes
31 sha256 = hashlib.sha256(text).digest()
32 sha256 = hashlib.sha256(text).digest()
32 sd[sidedata.SD_TEST2] = struct.pack('>32s', sha256)
33 sd[sidedata.SD_TEST2] = struct.pack('>32s', sha256)
33 return orig(self, text, transaction, link, p1, p2, *args, **kwargs)
34 return orig(self, text, transaction, link, p1, p2, *args, **kwargs)
34
35
36 def wraprevision(orig, self, nodeorrev, *args, **kwargs):
37 text = orig(self, nodeorrev, *args, **kwargs)
38 if nodeorrev != node.nullrev and nodeorrev != node.nullid:
39 sd = self.sidedata(nodeorrev)
40 if len(text) != struct.unpack('>I', sd[sidedata.SD_TEST1])[0]:
41 raise RuntimeError('text size mismatch')
42 expected = sd[sidedata.SD_TEST2]
43 got = hashlib.sha256(text).digest()
44 if got != expected:
45 raise RuntimeError('sha256 mismatch')
46 return text
47
35 def extsetup(ui):
48 def extsetup(ui):
36 extensions.wrapfunction(revlog.revlog, 'addrevision', wrapaddrevision)
49 extensions.wrapfunction(revlog.revlog, 'addrevision', wrapaddrevision)
50 extensions.wrapfunction(revlog.revlog, 'revision', wraprevision)
General Comments 0
You need to be logged in to leave comments. Login now