tests/flagprocessorext.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 12 Sep 2018 11:02:16 -0700
changeset 39703 bfeab472e3c0
parent 37436 9d4f09bfe3ec
child 41435 fad627d2047c
permissions -rw-r--r--
localrepo: create new function for instantiating a local repo object Today, there is a single local repository class - localrepository. Its __init__ is responsible for loading the .hg/requires file and taking different actions depending on what is present. In addition, extensions may define a "reposetup" function that monkeypatches constructed repository instances, often by implementing a derived type and changing the __class__ of the repo instance. Work around alternate storage backends and partial clone has made it clear to me that shoehorning all this logic into __init__ and operating on an existing instance is too convoluted. For example, localrepository assumes revlog storage and swapping in non-revlog storage requires overriding e.g. file() to return something that isn't a revlog. I've authored various patches that either: a) teach various methods (like file()) about different states and taking the appropriate code path at run-time b) create methods/attributes/callables used for instantiating things and populating these in __init__ "a" incurs run-time performance penalties and makes code more complicated since various functions have a bunch of "if storage is X" branches. "b" makes localrepository quickly explode in complexity. My plan for tackling this problem is to make the local repository type more dynamic. Instead of a static localrepository class/type that supports all of the local repository configurations (revlogs vs other, revlogs with ellipsis, revlog v1 versus revlog v2, etc), we'll dynamically construct a type providing the implementations that are needed for the repository on disk, derived from the .hg/requires file and configuration options. The constructed repository type will be specialized and methods won't need to be taught about different implementations nor overloaded. We may also leverage this functionality for building types that don't implement all attributes. For example, the "intents" feature allows commands to declare that they are read only. By dynamically constructing a repository type, we could return a repository instance with no attributes related to mutating the repository. This could include things like a "changelog" property implementation that doesn't check whether it needs to invalidate the hidden revisions set on every access. This commit establishes a function for building a local repository instance. Future commits will start moving functionality from localrepository.__init__ to this function. Then we'll start dynamically changing the returned type depending on options that are present. This change may seem radical. But it should be fully compatible with the reposetup() model - at least for now. Differential Revision: https://phab.mercurial-scm.org/D4563
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     1
# coding=UTF-8
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     2
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     3
from __future__ import absolute_import
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     4
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     5
import base64
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     6
import zlib
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     7
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     8
from mercurial import (
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
     9
    changegroup,
31832
77f746e5383a test-flagprocessor: use changegroup3 in bundle2
Jun Wu <quark@fb.com>
parents: 30745
diff changeset
    10
    exchange,
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    11
    extensions,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    12
    revlog,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    13
    util,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    14
)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    15
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    16
# Test only: These flags are defined here only in the context of testing the
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    17
# behavior of the flag processor. The canonical way to add flags is to get in
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    18
# touch with the community and make them known in revlog.
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    19
REVIDX_NOOP = (1 << 3)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    20
REVIDX_BASE64 = (1 << 2)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    21
REVIDX_GZIP = (1 << 1)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    22
REVIDX_FAIL = 1
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    23
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    24
def validatehash(self, text):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    25
    return True
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    26
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    27
def bypass(self, text):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    28
    return False
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    29
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    30
def noopdonothing(self, text):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    31
    return (text, True)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    32
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    33
def b64encode(self, text):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    34
    return (base64.b64encode(text), False)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    35
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    36
def b64decode(self, text):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    37
    return (base64.b64decode(text), True)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    38
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    39
def gzipcompress(self, text):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    40
    return (zlib.compress(text), False)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    41
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    42
def gzipdecompress(self, text):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    43
    return (zlib.decompress(text), True)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    44
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    45
def supportedoutgoingversions(orig, repo):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    46
    versions = orig(repo)
36152
83246d6920f2 py3: use b'' for changegroup version literals
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35570
diff changeset
    47
    versions.discard(b'01')
83246d6920f2 py3: use b'' for changegroup version literals
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35570
diff changeset
    48
    versions.discard(b'02')
83246d6920f2 py3: use b'' for changegroup version literals
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35570
diff changeset
    49
    versions.add(b'03')
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    50
    return versions
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    51
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    52
def allsupportedversions(orig, ui):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    53
    versions = orig(ui)
36152
83246d6920f2 py3: use b'' for changegroup version literals
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35570
diff changeset
    54
    versions.add(b'03')
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    55
    return versions
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    56
37436
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    57
def makewrappedfile(obj):
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    58
    class wrappedfile(obj.__class__):
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    59
        def addrevision(self, text, transaction, link, p1, p2,
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    60
                        cachedelta=None, node=None,
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    61
                        flags=revlog.REVIDX_DEFAULT_FLAGS):
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    62
            if b'[NOOP]' in text:
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    63
                flags |= REVIDX_NOOP
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    64
37436
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    65
            if b'[BASE64]' in text:
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    66
                flags |= REVIDX_BASE64
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    67
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    68
            if b'[GZIP]' in text:
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    69
                flags |= REVIDX_GZIP
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    70
37436
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    71
            # This addrevision wrapper is meant to add a flag we will not have
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    72
            # transforms registered for, ensuring we handle this error case.
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    73
            if b'[FAIL]' in text:
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    74
                flags |= REVIDX_FAIL
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    75
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    76
            return super(wrappedfile, self).addrevision(text, transaction, link,
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    77
                                                        p1, p2,
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    78
                                                        cachedelta=cachedelta,
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    79
                                                        node=node,
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    80
                                                        flags=flags)
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    81
37436
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    82
    obj.__class__ = wrappedfile
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    83
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    84
def reposetup(ui, repo):
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    85
    class wrappingflagprocessorrepo(repo.__class__):
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    86
        def file(self, f):
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    87
            orig = super(wrappingflagprocessorrepo, self).file(f)
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    88
            makewrappedfile(orig)
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    89
            return orig
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    90
9d4f09bfe3ec simplestore: correctly implement flag processors
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37167
diff changeset
    91
    repo.__class__ = wrappingflagprocessorrepo
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    92
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    93
def extsetup(ui):
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    94
    # Enable changegroup3 for flags to be sent over the wire
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    95
    wrapfunction = extensions.wrapfunction
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    96
    wrapfunction(changegroup,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    97
                 'supportedoutgoingversions',
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    98
                 supportedoutgoingversions)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
    99
    wrapfunction(changegroup,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   100
                 'allsupportedversions',
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   101
                 allsupportedversions)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   102
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   103
    # Teach revlog about our test flags
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   104
    flags = [REVIDX_NOOP, REVIDX_BASE64, REVIDX_GZIP, REVIDX_FAIL]
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   105
    revlog.REVIDX_KNOWN_FLAGS |= util.bitsfrom(flags)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   106
    revlog.REVIDX_FLAGS_ORDER.extend(flags)
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   107
31832
77f746e5383a test-flagprocessor: use changegroup3 in bundle2
Jun Wu <quark@fb.com>
parents: 30745
diff changeset
   108
    # Teach exchange to use changegroup 3
37167
6c7a6b04b274 bundlespec: move computing the bundle contentops in parsebundlespec
Boris Feld <boris.feld@octobus.net>
parents: 36152
diff changeset
   109
    for k in exchange._bundlespeccontentopts.keys():
6c7a6b04b274 bundlespec: move computing the bundle contentops in parsebundlespec
Boris Feld <boris.feld@octobus.net>
parents: 36152
diff changeset
   110
        exchange._bundlespeccontentopts[k]["cg.version"] = "03"
31832
77f746e5383a test-flagprocessor: use changegroup3 in bundle2
Jun Wu <quark@fb.com>
parents: 30745
diff changeset
   111
30745
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   112
    # Register flag processors for each extension
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   113
    revlog.addflagprocessor(
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   114
        REVIDX_NOOP,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   115
        (
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   116
            noopdonothing,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   117
            noopdonothing,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   118
            validatehash,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   119
        )
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   120
    )
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   121
    revlog.addflagprocessor(
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   122
        REVIDX_BASE64,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   123
        (
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   124
            b64decode,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   125
            b64encode,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   126
            bypass,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   127
        ),
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   128
    )
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   129
    revlog.addflagprocessor(
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   130
        REVIDX_GZIP,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   131
        (
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   132
            gzipdecompress,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   133
            gzipcompress,
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   134
            bypass
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   135
        )
c1b7b2285522 revlog: flag processor
Remi Chaintron <remi@fb.com>
parents:
diff changeset
   136
    )