Debian Codegraph @

tests/flagprocessorext.py

raw
1 import base64
2 import zlib
3
4 from mercurial import (
5 bundlecaches,
6 changegroup,
7 extensions,
8 revlog,
9 util,
10 )
11 from mercurial.revlogutils import flagutil
12 from mercurial.interfaces import repository
13
14 # Test only: These flags are defined here only in the context of testing the
15 # behavior of the flag processor. The canonical way to add flags is to get in
16 # touch with the community and make them known in revlog.
17 REVIDX_NOOP = 1 << 3
18 REVIDX_BASE64 = 1 << 2
19 REVIDX_GZIP = 1 << 1
20 REVIDX_FAIL = 1
21
22
23 def validatehash(self, text):
24 return True
25
26
27 def bypass(self, text):
28 return False
29
30
31 def noopdonothing(self, text):
32 return (text, True)
33
34
35 def noopdonothingread(self, text):
36 return (text, True)
37
38
39 def b64encode(self, text):
40 return (base64.b64encode(text), False)
41
42
43 def b64decode(self, text):
44 return (base64.b64decode(text), True)
45
46
47 def gzipcompress(self, text):
48 return (zlib.compress(text), False)
49
50
51 def gzipdecompress(self, text):
52 return (zlib.decompress(text), True)
53
54
55 def supportedoutgoingversions(orig, repo):
56 versions = orig(repo)
57 versions.discard(b'01')
58 versions.discard(b'02')
59 return versions
60
61
62 def allsupportedversions(orig, ui):
63 versions = orig(ui)
64 versions.add(b'03')
65 return versions
66
67
68 def makewrappedfile(obj):
69 class wrappedfile(obj.__class__):
70 def addrevision(
71 self,
72 text,
73 transaction,
74 link,
75 p1,
76 p2,
77 cachedelta=None,
78 node=None,
79 flags=flagutil.REVIDX_DEFAULT_FLAGS,
80 ):
81 if b'[NOOP]' in text:
82 flags |= REVIDX_NOOP
83
84 if b'[BASE64]' in text:
85 flags |= REVIDX_BASE64
86
87 if b'[GZIP]' in text:
88 flags |= REVIDX_GZIP
89
90 # This addrevision wrapper is meant to add a flag we will not have
91 # transforms registered for, ensuring we handle this error case.
92 if b'[FAIL]' in text:
93 flags |= REVIDX_FAIL
94
95 return super().addrevision(
96 text,
97 transaction,
98 link,
99 p1,
100 p2,
101 cachedelta=cachedelta,
102 node=node,
103 flags=flags,
104 )
105
106 obj.__class__ = wrappedfile
107
108
109 def reposetup(ui, repo):
110 class wrappingflagprocessorrepo(repo.__class__):
111 def file(self, f, writable=False):
112 orig = super().file(f, writable=writable)
113 makewrappedfile(orig)
114 return orig
115
116 repo.__class__ = wrappingflagprocessorrepo
117
118
119 def extsetup(ui):
120 # Enable changegroup3 for flags to be sent over the wire
121 wrapfunction = extensions.wrapfunction
122 wrapfunction(
123 changegroup, 'supportedoutgoingversions', supportedoutgoingversions
124 )
125 wrapfunction(changegroup, 'allsupportedversions', allsupportedversions)
126
127 # Teach revlog about our test flags
128 flags = [REVIDX_NOOP, REVIDX_BASE64, REVIDX_GZIP, REVIDX_FAIL]
129 flagutil.REVIDX_KNOWN_FLAGS |= util.bitsfrom(flags)
130 repository.REVISION_FLAGS_KNOWN |= util.bitsfrom(flags)
131 revlog.REVIDX_FLAGS_ORDER.extend(flags)
132
133 # Teach exchange to use changegroup 3
134 for k in bundlecaches._bundlespeccontentopts.keys():
135 bundlecaches._bundlespeccontentopts[k][b"cg.version"] = b"03"
136
137 # Register flag processors for each extension
138 flagutil.addflagprocessor(
139 REVIDX_NOOP,
140 (
141 noopdonothingread,
142 noopdonothing,
143 validatehash,
144 ),
145 )
146 flagutil.addflagprocessor(
147 REVIDX_BASE64,
148 (
149 b64decode,
150 b64encode,
151 bypass,
152 ),
153 )
154 flagutil.addflagprocessor(
155 REVIDX_GZIP, (gzipdecompress, gzipcompress, bypass)
156 )