| 1 |
# ext-sidedata.py - small extension to test the sidedata logic |
| 2 |
# |
| 3 |
# Copyright 2019 Pierre-Yves David <[email protected]> |
| 4 |
# |
| 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. |
| 7 |
|
| 8 |
|
| 9 |
import hashlib |
| 10 |
import struct |
| 11 |
|
| 12 |
from mercurial.node import nullrev |
| 13 |
from mercurial import ( |
| 14 |
extensions, |
| 15 |
requirements, |
| 16 |
revlog, |
| 17 |
) |
| 18 |
|
| 19 |
from mercurial.upgrade_utils import engine as upgrade_engine |
| 20 |
|
| 21 |
from mercurial.revlogutils import constants |
| 22 |
from mercurial.revlogutils import sidedata |
| 23 |
|
| 24 |
|
| 25 |
def wrapaddrevision( |
| 26 |
orig, self, text, transaction, link, p1, p2, *args, **kwargs |
| 27 |
): |
| 28 |
if kwargs.get('sidedata') is None: |
| 29 |
kwargs['sidedata'] = {} |
| 30 |
sd = kwargs['sidedata'] |
| 31 |
## let's store some arbitrary data just for testing |
| 32 |
# text length |
| 33 |
sd[sidedata.SD_TEST1] = struct.pack('>I', len(text)) |
| 34 |
# and sha2 hashes |
| 35 |
sha256 = hashlib.sha256(text).digest() |
| 36 |
sd[sidedata.SD_TEST2] = struct.pack('>32s', sha256) |
| 37 |
return orig(self, text, transaction, link, p1, p2, *args, **kwargs) |
| 38 |
|
| 39 |
|
| 40 |
def wrap_revisiondata(orig, self, nodeorrev, *args, **kwargs): |
| 41 |
text = orig(self, nodeorrev, *args, **kwargs) |
| 42 |
sd = self.sidedata(nodeorrev) |
| 43 |
if getattr(self, 'sidedatanocheck', False): |
| 44 |
return text |
| 45 |
if self.feature_config.has_side_data: |
| 46 |
return text |
| 47 |
if nodeorrev != nullrev and nodeorrev != self.nullid: |
| 48 |
cat1 = sd.get(sidedata.SD_TEST1) |
| 49 |
if cat1 is not None and len(text) != struct.unpack('>I', cat1)[0]: |
| 50 |
raise RuntimeError('text size mismatch') |
| 51 |
expected = sd.get(sidedata.SD_TEST2) |
| 52 |
got = hashlib.sha256(text).digest() |
| 53 |
if expected is not None and got != expected: |
| 54 |
raise RuntimeError('sha256 mismatch') |
| 55 |
return text |
| 56 |
|
| 57 |
|
| 58 |
def wrapget_sidedata_helpers(orig, srcrepo, dstrepo): |
| 59 |
repo, computers, removers = orig(srcrepo, dstrepo) |
| 60 |
assert not computers and not removers # deal with composition later |
| 61 |
addedreqs = dstrepo.requirements - srcrepo.requirements |
| 62 |
|
| 63 |
if requirements.REVLOGV2_REQUIREMENT in addedreqs: |
| 64 |
|
| 65 |
def computer(repo, revlog, rev, old_sidedata): |
| 66 |
assert not old_sidedata # not supported yet |
| 67 |
update = {} |
| 68 |
revlog.sidedatanocheck = True |
| 69 |
try: |
| 70 |
text = revlog.revision(rev) |
| 71 |
finally: |
| 72 |
del revlog.sidedatanocheck |
| 73 |
## let's store some arbitrary data just for testing |
| 74 |
# text length |
| 75 |
update[sidedata.SD_TEST1] = struct.pack('>I', len(text)) |
| 76 |
# and sha2 hashes |
| 77 |
sha256 = hashlib.sha256(text).digest() |
| 78 |
update[sidedata.SD_TEST2] = struct.pack('>32s', sha256) |
| 79 |
return update, (0, 0) |
| 80 |
|
| 81 |
srcrepo.register_sidedata_computer( |
| 82 |
constants.KIND_CHANGELOG, |
| 83 |
b"whatever", |
| 84 |
(sidedata.SD_TEST1, sidedata.SD_TEST2), |
| 85 |
computer, |
| 86 |
0, |
| 87 |
) |
| 88 |
dstrepo.register_wanted_sidedata(b"whatever") |
| 89 |
|
| 90 |
return sidedata.get_sidedata_helpers(srcrepo, dstrepo._wanted_sidedata) |
| 91 |
|
| 92 |
|
| 93 |
def extsetup(ui): |
| 94 |
extensions.wrapfunction(revlog.revlog, 'addrevision', wrapaddrevision) |
| 95 |
extensions.wrapfunction(revlog.revlog, '_revisiondata', wrap_revisiondata) |
| 96 |
extensions.wrapfunction( |
| 97 |
upgrade_engine, 'get_sidedata_helpers', wrapget_sidedata_helpers |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
def reposetup(ui, repo): |
| 102 |
# We don't register sidedata computers because we don't care within these |
| 103 |
# tests |
| 104 |
repo.register_wanted_sidedata(sidedata.SD_TEST1) |
| 105 |
repo.register_wanted_sidedata(sidedata.SD_TEST2) |