tests/testlib/ext-sidedata-5.py
author Martin von Zweigbergk <martinvonz@google.com>
Fri, 12 Feb 2021 16:19:33 -0800
changeset 46831 7d80622fc212
parent 46718 ba8e508a8e69
child 47073 64cd1496bb70
permissions -rw-r--r--
rebase: let _handleskippingobsolete(self) read directly from self The function already has `self` as an argument, so there's no need to pass data from `self` into it. Differential Revision: https://phab.mercurial-scm.org/D10246
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46718
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
# coding: utf8
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
# ext-sidedata-5.py - small extension to test (differently still) the sidedata
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
# logic
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
#
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
# Simulates a server for a simple sidedata exchange.
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
#
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
# Copyright 2021 Raphaël Gomès <rgomes@octobus.net>
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
#
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
# This software may be used and distributed according to the terms of the
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
# GNU General Public License version 2 or any later version.
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    11
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
from __future__ import absolute_import
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
import hashlib
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    15
import struct
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    16
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
from mercurial import (
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
    extensions,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
    revlog,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
from mercurial.revlogutils import sidedata as sidedatamod
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
def compute_sidedata_1(repo, revlog, rev, sidedata, text=None):
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
    sidedata = sidedata.copy()
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
    if text is None:
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
        text = revlog.revision(rev)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
    sidedata[sidedatamod.SD_TEST1] = struct.pack('>I', len(text))
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
    return sidedata
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
def compute_sidedata_2(repo, revlog, rev, sidedata, text=None):
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
    sidedata = sidedata.copy()
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
    if text is None:
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
        text = revlog.revision(rev)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
    sha256 = hashlib.sha256(text).digest()
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
    sidedata[sidedatamod.SD_TEST2] = struct.pack('>32s', sha256)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
    return sidedata
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
def reposetup(ui, repo):
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
    # Sidedata keys happen to be the same as the categories, easier for testing.
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
    for kind in (b'changelog', b'manifest', b'filelog'):
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
        repo.register_sidedata_computer(
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
            kind,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
            sidedatamod.SD_TEST1,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
            (sidedatamod.SD_TEST1,),
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
            compute_sidedata_1,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
        )
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
        repo.register_sidedata_computer(
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
            kind,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
            sidedatamod.SD_TEST2,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
            (sidedatamod.SD_TEST2,),
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
            compute_sidedata_2,
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
        )
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
    # We don't register sidedata computers because we don't care within these
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
    # tests
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
    repo.register_wanted_sidedata(sidedatamod.SD_TEST1)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
    repo.register_wanted_sidedata(sidedatamod.SD_TEST2)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
def wrapaddrevision(
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
    orig, self, text, transaction, link, p1, p2, *args, **kwargs
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
):
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
    if kwargs.get('sidedata') is None:
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
        kwargs['sidedata'] = {}
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
    sd = kwargs['sidedata']
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
    ## let's store some arbitrary data just for testing
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
    # text length
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
    sd[sidedatamod.SD_TEST1] = struct.pack('>I', len(text))
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
    # and sha2 hashes
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
    sha256 = hashlib.sha256(text).digest()
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    sd[sidedatamod.SD_TEST2] = struct.pack('>32s', sha256)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
    return orig(self, text, transaction, link, p1, p2, *args, **kwargs)
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
def extsetup(ui):
ba8e508a8e69 sidedata-exchange: rewrite sidedata on-the-fly whenever possible
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
    extensions.wrapfunction(revlog.revlog, 'addrevision', wrapaddrevision)