flagprocessorext.py
149 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
/ tests / flagprocessorext.py
Remi Chaintron
|
r30745 | # coding=UTF-8 | ||
from __future__ import absolute_import | ||||
import base64 | ||||
import zlib | ||||
from mercurial import ( | ||||
changegroup, | ||||
Jun Wu
|
r31832 | exchange, | ||
Remi Chaintron
|
r30745 | extensions, | ||
revlog, | ||||
util, | ||||
) | ||||
Augie Fackler
|
r43346 | from mercurial.revlogutils import flagutil | ||
Remi Chaintron
|
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. | ||||
Augie Fackler
|
r43346 | REVIDX_NOOP = 1 << 3 | ||
REVIDX_BASE64 = 1 << 2 | ||||
REVIDX_GZIP = 1 << 1 | ||||
Remi Chaintron
|
r30745 | REVIDX_FAIL = 1 | ||
Augie Fackler
|
r43346 | |||
Remi Chaintron
|
r30745 | def validatehash(self, text): | ||
return True | ||||
Augie Fackler
|
r43346 | |||
Remi Chaintron
|
r30745 | def bypass(self, text): | ||
return False | ||||
Augie Fackler
|
r43346 | |||
r43258 | def noopdonothing(self, text, sidedata): | |||
Remi Chaintron
|
r30745 | return (text, True) | ||
Augie Fackler
|
r43346 | |||
r43255 | def noopdonothingread(self, text): | |||
return (text, True, {}) | ||||
Augie Fackler
|
r43346 | |||
r43258 | def b64encode(self, text, sidedata): | |||
Remi Chaintron
|
r30745 | return (base64.b64encode(text), False) | ||
Augie Fackler
|
r43346 | |||
Remi Chaintron
|
r30745 | def b64decode(self, text): | ||
r43255 | return (base64.b64decode(text), True, {}) | |||
Remi Chaintron
|
r30745 | |||
Augie Fackler
|
r43346 | |||
r43258 | def gzipcompress(self, text, sidedata): | |||
Remi Chaintron
|
r30745 | return (zlib.compress(text), False) | ||
Augie Fackler
|
r43346 | |||
Remi Chaintron
|
r30745 | def gzipdecompress(self, text): | ||
r43255 | return (zlib.decompress(text), True, {}) | |||
Remi Chaintron
|
r30745 | |||
Augie Fackler
|
r43346 | |||
Remi Chaintron
|
r30745 | def supportedoutgoingversions(orig, repo): | ||
versions = orig(repo) | ||||
Gregory Szorc
|
r36131 | versions.discard(b'01') | ||
versions.discard(b'02') | ||||
versions.add(b'03') | ||||
Remi Chaintron
|
r30745 | return versions | ||
Augie Fackler
|
r43346 | |||
Remi Chaintron
|
r30745 | def allsupportedversions(orig, ui): | ||
versions = orig(ui) | ||||
Gregory Szorc
|
r36131 | versions.add(b'03') | ||
Remi Chaintron
|
r30745 | return versions | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r37454 | def makewrappedfile(obj): | ||
class wrappedfile(obj.__class__): | ||||
Augie Fackler
|
r43346 | def addrevision( | ||
self, | ||||
text, | ||||
transaction, | ||||
link, | ||||
p1, | ||||
p2, | ||||
cachedelta=None, | ||||
node=None, | ||||
flags=flagutil.REVIDX_DEFAULT_FLAGS, | ||||
): | ||||
Gregory Szorc
|
r37454 | if b'[NOOP]' in text: | ||
flags |= REVIDX_NOOP | ||||
Remi Chaintron
|
r30745 | |||
Gregory Szorc
|
r37454 | if b'[BASE64]' in text: | ||
flags |= REVIDX_BASE64 | ||||
if b'[GZIP]' in text: | ||||
flags |= REVIDX_GZIP | ||||
Remi Chaintron
|
r30745 | |||
Gregory Szorc
|
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 | ||||
Augie Fackler
|
r43346 | return super(wrappedfile, self).addrevision( | ||
text, | ||||
transaction, | ||||
link, | ||||
p1, | ||||
p2, | ||||
cachedelta=cachedelta, | ||||
node=node, | ||||
flags=flags, | ||||
) | ||||
Remi Chaintron
|
r30745 | |||
Gregory Szorc
|
r37454 | obj.__class__ = wrappedfile | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
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 | ||||
Remi Chaintron
|
r30745 | |||
Augie Fackler
|
r43346 | |||
Remi Chaintron
|
r30745 | def extsetup(ui): | ||
# Enable changegroup3 for flags to be sent over the wire | ||||
wrapfunction = extensions.wrapfunction | ||||
Augie Fackler
|
r43346 | wrapfunction( | ||
changegroup, 'supportedoutgoingversions', supportedoutgoingversions | ||||
) | ||||
wrapfunction(changegroup, 'allsupportedversions', allsupportedversions) | ||||
Remi Chaintron
|
r30745 | |||
# Teach revlog about our test flags | ||||
flags = [REVIDX_NOOP, REVIDX_BASE64, REVIDX_GZIP, REVIDX_FAIL] | ||||
r42956 | flagutil.REVIDX_KNOWN_FLAGS |= util.bitsfrom(flags) | |||
Remi Chaintron
|
r30745 | revlog.REVIDX_FLAGS_ORDER.extend(flags) | ||
Jun Wu
|
r31832 | # Teach exchange to use changegroup 3 | ||
Boris Feld
|
r37182 | for k in exchange._bundlespeccontentopts.keys(): | ||
Gregory Szorc
|
r41458 | exchange._bundlespeccontentopts[k][b"cg.version"] = b"03" | ||
Jun Wu
|
r31832 | |||
Remi Chaintron
|
r30745 | # Register flag processors for each extension | ||
r42958 | flagutil.addflagprocessor( | |||
Augie Fackler
|
r43346 | REVIDX_NOOP, (noopdonothingread, noopdonothing, validatehash,) | ||
Remi Chaintron
|
r30745 | ) | ||
r42958 | flagutil.addflagprocessor( | |||
Augie Fackler
|
r43346 | REVIDX_BASE64, (b64decode, b64encode, bypass,), | ||
Remi Chaintron
|
r30745 | ) | ||
r42958 | flagutil.addflagprocessor( | |||
Augie Fackler
|
r43346 | REVIDX_GZIP, (gzipdecompress, gzipcompress, bypass) | ||
Remi Chaintron
|
r30745 | ) | ||