flagprocessorext.py
161 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
/ tests / flagprocessorext.py
|
r30745 | # coding=UTF-8 | ||
from __future__ import absolute_import | ||||
import base64 | ||||
import zlib | ||||
from mercurial import ( | ||||
r46369 | bundlecaches, | |||
|
r30745 | changegroup, | ||
extensions, | ||||
revlog, | ||||
util, | ||||
) | ||||
|
r43346 | from mercurial.revlogutils import flagutil | ||
r48186 | from mercurial.interfaces import repository | |||
|
r30745 | |||
# Test only: These flags are defined here only in the context of testing the | ||||
# behavior of the flag processor. The canonical way to add flags is to get in | ||||
# touch with the community and make them known in revlog. | ||||
|
r43346 | REVIDX_NOOP = 1 << 3 | ||
REVIDX_BASE64 = 1 << 2 | ||||
REVIDX_GZIP = 1 << 1 | ||||
|
r30745 | REVIDX_FAIL = 1 | ||
|
r43346 | |||
|
r30745 | def validatehash(self, text): | ||
return True | ||||
|
r43346 | |||
|
r30745 | def bypass(self, text): | ||
return False | ||||
|
r43346 | |||
|
r47443 | def noopdonothing(self, text): | ||
|
r30745 | return (text, True) | ||
|
r43346 | |||
r43255 | def noopdonothingread(self, text): | |||
|
r47443 | return (text, True) | ||
r43255 | ||||
|
r43346 | |||
|
r47443 | def b64encode(self, text): | ||
|
r30745 | return (base64.b64encode(text), False) | ||
|
r43346 | |||
|
r30745 | def b64decode(self, text): | ||
|
r47443 | return (base64.b64decode(text), True) | ||
|
r30745 | |||
|
r43346 | |||
|
r47443 | def gzipcompress(self, text): | ||
|
r30745 | return (zlib.compress(text), False) | ||
|
r43346 | |||
|
r30745 | def gzipdecompress(self, text): | ||
|
r47443 | return (zlib.decompress(text), True) | ||
|
r30745 | |||
|
r43346 | |||
|
r30745 | def supportedoutgoingversions(orig, repo): | ||
versions = orig(repo) | ||||
|
r36131 | versions.discard(b'01') | ||
versions.discard(b'02') | ||||
versions.add(b'03') | ||||
|
r30745 | return versions | ||
|
r43346 | |||
|
r30745 | def allsupportedversions(orig, ui): | ||
versions = orig(ui) | ||||
|
r36131 | versions.add(b'03') | ||
|
r30745 | return versions | ||
|
r43346 | |||
|
r37454 | def makewrappedfile(obj): | ||
class wrappedfile(obj.__class__): | ||||
|
r43346 | def addrevision( | ||
self, | ||||
text, | ||||
transaction, | ||||
link, | ||||
p1, | ||||
p2, | ||||
cachedelta=None, | ||||
node=None, | ||||
flags=flagutil.REVIDX_DEFAULT_FLAGS, | ||||
): | ||||
|
r37454 | if b'[NOOP]' in text: | ||
flags |= REVIDX_NOOP | ||||
|
r30745 | |||
|
r37454 | if b'[BASE64]' in text: | ||
flags |= REVIDX_BASE64 | ||||
if b'[GZIP]' in text: | ||||
flags |= REVIDX_GZIP | ||||
|
r30745 | |||
|
r37454 | # This addrevision wrapper is meant to add a flag we will not have | ||
# transforms registered for, ensuring we handle this error case. | ||||
if b'[FAIL]' in text: | ||||
flags |= REVIDX_FAIL | ||||
|
r43346 | return super(wrappedfile, self).addrevision( | ||
text, | ||||
transaction, | ||||
link, | ||||
p1, | ||||
p2, | ||||
cachedelta=cachedelta, | ||||
node=node, | ||||
flags=flags, | ||||
) | ||||
|
r30745 | |||
|
r37454 | obj.__class__ = wrappedfile | ||
|
r43346 | |||
|
r37454 | def reposetup(ui, repo): | ||
class wrappingflagprocessorrepo(repo.__class__): | ||||
def file(self, f): | ||||
orig = super(wrappingflagprocessorrepo, self).file(f) | ||||
makewrappedfile(orig) | ||||
return orig | ||||
repo.__class__ = wrappingflagprocessorrepo | ||||
|
r30745 | |||
|
r43346 | |||
|
r30745 | def extsetup(ui): | ||
# Enable changegroup3 for flags to be sent over the wire | ||||
wrapfunction = extensions.wrapfunction | ||||
|
r43346 | wrapfunction( | ||
changegroup, 'supportedoutgoingversions', supportedoutgoingversions | ||||
) | ||||
wrapfunction(changegroup, 'allsupportedversions', allsupportedversions) | ||||
|
r30745 | |||
# Teach revlog about our test flags | ||||
flags = [REVIDX_NOOP, REVIDX_BASE64, REVIDX_GZIP, REVIDX_FAIL] | ||||
r42956 | flagutil.REVIDX_KNOWN_FLAGS |= util.bitsfrom(flags) | |||
r48186 | repository.REVISION_FLAGS_KNOWN |= util.bitsfrom(flags) | |||
|
r30745 | revlog.REVIDX_FLAGS_ORDER.extend(flags) | ||
|
r31832 | # Teach exchange to use changegroup 3 | ||
r46369 | for k in bundlecaches._bundlespeccontentopts.keys(): | |||
bundlecaches._bundlespeccontentopts[k][b"cg.version"] = b"03" | ||||
|
r31832 | |||
|
r30745 | # Register flag processors for each extension | ||
r42958 | flagutil.addflagprocessor( | |||
|
r46554 | REVIDX_NOOP, | ||
( | ||||
noopdonothingread, | ||||
noopdonothing, | ||||
validatehash, | ||||
), | ||||
|
r30745 | ) | ||
r42958 | flagutil.addflagprocessor( | |||
|
r46554 | REVIDX_BASE64, | ||
( | ||||
b64decode, | ||||
b64encode, | ||||
bypass, | ||||
), | ||||
|
r30745 | ) | ||
r42958 | flagutil.addflagprocessor( | |||
|
r43346 | REVIDX_GZIP, (gzipdecompress, gzipcompress, bypass) | ||
|
r30745 | ) | ||