Show More
@@ -1,36 +1,50 b'' | |||
|
1 | 1 | # ext-sidedata.py - small extension to test the sidedata logic |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net) |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import hashlib |
|
11 | 11 | import struct |
|
12 | 12 | |
|
13 | 13 | from mercurial import ( |
|
14 | 14 | extensions, |
|
15 | node, | |
|
15 | 16 | revlog, |
|
16 | 17 | ) |
|
17 | 18 | |
|
18 | 19 | from mercurial.revlogutils import ( |
|
19 | 20 | sidedata, |
|
20 | 21 | ) |
|
21 | 22 | |
|
22 | 23 | def wrapaddrevision(orig, self, text, transaction, link, p1, p2, *args, |
|
23 | 24 | **kwargs): |
|
24 | 25 | if kwargs.get('sidedata') is None: |
|
25 | 26 | kwargs['sidedata'] = {} |
|
26 | 27 | sd = kwargs['sidedata'] |
|
27 | 28 | ## let's store some arbitrary data just for testing |
|
28 | 29 | # text length |
|
29 | 30 | sd[sidedata.SD_TEST1] = struct.pack('>I', len(text)) |
|
30 | 31 | # and sha2 hashes |
|
31 | 32 | sha256 = hashlib.sha256(text).digest() |
|
32 | 33 | sd[sidedata.SD_TEST2] = struct.pack('>32s', sha256) |
|
33 | 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 | 48 | def extsetup(ui): |
|
36 | 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