Show More
@@ -1,837 +1,843 | |||||
1 | # wireproto.py - generic wire protocol support functions |
|
1 | # wireproto.py - generic wire protocol support functions | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2010 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005-2010 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | import urllib, tempfile, os, sys |
|
8 | import urllib, tempfile, os, sys | |
9 | from i18n import _ |
|
9 | from i18n import _ | |
10 | from node import bin, hex |
|
10 | from node import bin, hex | |
11 | import changegroup as changegroupmod, bundle2 |
|
11 | import changegroup as changegroupmod, bundle2 | |
12 | import peer, error, encoding, util, store, exchange |
|
12 | import peer, error, encoding, util, store, exchange | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | class abstractserverproto(object): |
|
15 | class abstractserverproto(object): | |
16 | """abstract class that summarizes the protocol API |
|
16 | """abstract class that summarizes the protocol API | |
17 |
|
17 | |||
18 | Used as reference and documentation. |
|
18 | Used as reference and documentation. | |
19 | """ |
|
19 | """ | |
20 |
|
20 | |||
21 | def getargs(self, args): |
|
21 | def getargs(self, args): | |
22 | """return the value for arguments in <args> |
|
22 | """return the value for arguments in <args> | |
23 |
|
23 | |||
24 | returns a list of values (same order as <args>)""" |
|
24 | returns a list of values (same order as <args>)""" | |
25 | raise NotImplementedError() |
|
25 | raise NotImplementedError() | |
26 |
|
26 | |||
27 | def getfile(self, fp): |
|
27 | def getfile(self, fp): | |
28 | """write the whole content of a file into a file like object |
|
28 | """write the whole content of a file into a file like object | |
29 |
|
29 | |||
30 | The file is in the form:: |
|
30 | The file is in the form:: | |
31 |
|
31 | |||
32 | (<chunk-size>\n<chunk>)+0\n |
|
32 | (<chunk-size>\n<chunk>)+0\n | |
33 |
|
33 | |||
34 | chunk size is the ascii version of the int. |
|
34 | chunk size is the ascii version of the int. | |
35 | """ |
|
35 | """ | |
36 | raise NotImplementedError() |
|
36 | raise NotImplementedError() | |
37 |
|
37 | |||
38 | def redirect(self): |
|
38 | def redirect(self): | |
39 | """may setup interception for stdout and stderr |
|
39 | """may setup interception for stdout and stderr | |
40 |
|
40 | |||
41 | See also the `restore` method.""" |
|
41 | See also the `restore` method.""" | |
42 | raise NotImplementedError() |
|
42 | raise NotImplementedError() | |
43 |
|
43 | |||
44 | # If the `redirect` function does install interception, the `restore` |
|
44 | # If the `redirect` function does install interception, the `restore` | |
45 | # function MUST be defined. If interception is not used, this function |
|
45 | # function MUST be defined. If interception is not used, this function | |
46 | # MUST NOT be defined. |
|
46 | # MUST NOT be defined. | |
47 | # |
|
47 | # | |
48 | # left commented here on purpose |
|
48 | # left commented here on purpose | |
49 | # |
|
49 | # | |
50 | #def restore(self): |
|
50 | #def restore(self): | |
51 | # """reinstall previous stdout and stderr and return intercepted stdout |
|
51 | # """reinstall previous stdout and stderr and return intercepted stdout | |
52 | # """ |
|
52 | # """ | |
53 | # raise NotImplementedError() |
|
53 | # raise NotImplementedError() | |
54 |
|
54 | |||
55 | def groupchunks(self, cg): |
|
55 | def groupchunks(self, cg): | |
56 | """return 4096 chunks from a changegroup object |
|
56 | """return 4096 chunks from a changegroup object | |
57 |
|
57 | |||
58 | Some protocols may have compressed the contents.""" |
|
58 | Some protocols may have compressed the contents.""" | |
59 | raise NotImplementedError() |
|
59 | raise NotImplementedError() | |
60 |
|
60 | |||
61 | # abstract batching support |
|
61 | # abstract batching support | |
62 |
|
62 | |||
63 | class future(object): |
|
63 | class future(object): | |
64 | '''placeholder for a value to be set later''' |
|
64 | '''placeholder for a value to be set later''' | |
65 | def set(self, value): |
|
65 | def set(self, value): | |
66 | if util.safehasattr(self, 'value'): |
|
66 | if util.safehasattr(self, 'value'): | |
67 | raise error.RepoError("future is already set") |
|
67 | raise error.RepoError("future is already set") | |
68 | self.value = value |
|
68 | self.value = value | |
69 |
|
69 | |||
70 | class batcher(object): |
|
70 | class batcher(object): | |
71 | '''base class for batches of commands submittable in a single request |
|
71 | '''base class for batches of commands submittable in a single request | |
72 |
|
72 | |||
73 | All methods invoked on instances of this class are simply queued and |
|
73 | All methods invoked on instances of this class are simply queued and | |
74 | return a a future for the result. Once you call submit(), all the queued |
|
74 | return a a future for the result. Once you call submit(), all the queued | |
75 | calls are performed and the results set in their respective futures. |
|
75 | calls are performed and the results set in their respective futures. | |
76 | ''' |
|
76 | ''' | |
77 | def __init__(self): |
|
77 | def __init__(self): | |
78 | self.calls = [] |
|
78 | self.calls = [] | |
79 | def __getattr__(self, name): |
|
79 | def __getattr__(self, name): | |
80 | def call(*args, **opts): |
|
80 | def call(*args, **opts): | |
81 | resref = future() |
|
81 | resref = future() | |
82 | self.calls.append((name, args, opts, resref,)) |
|
82 | self.calls.append((name, args, opts, resref,)) | |
83 | return resref |
|
83 | return resref | |
84 | return call |
|
84 | return call | |
85 | def submit(self): |
|
85 | def submit(self): | |
86 | pass |
|
86 | pass | |
87 |
|
87 | |||
88 | class localbatch(batcher): |
|
88 | class localbatch(batcher): | |
89 | '''performs the queued calls directly''' |
|
89 | '''performs the queued calls directly''' | |
90 | def __init__(self, local): |
|
90 | def __init__(self, local): | |
91 | batcher.__init__(self) |
|
91 | batcher.__init__(self) | |
92 | self.local = local |
|
92 | self.local = local | |
93 | def submit(self): |
|
93 | def submit(self): | |
94 | for name, args, opts, resref in self.calls: |
|
94 | for name, args, opts, resref in self.calls: | |
95 | resref.set(getattr(self.local, name)(*args, **opts)) |
|
95 | resref.set(getattr(self.local, name)(*args, **opts)) | |
96 |
|
96 | |||
97 | class remotebatch(batcher): |
|
97 | class remotebatch(batcher): | |
98 | '''batches the queued calls; uses as few roundtrips as possible''' |
|
98 | '''batches the queued calls; uses as few roundtrips as possible''' | |
99 | def __init__(self, remote): |
|
99 | def __init__(self, remote): | |
100 | '''remote must support _submitbatch(encbatch) and |
|
100 | '''remote must support _submitbatch(encbatch) and | |
101 | _submitone(op, encargs)''' |
|
101 | _submitone(op, encargs)''' | |
102 | batcher.__init__(self) |
|
102 | batcher.__init__(self) | |
103 | self.remote = remote |
|
103 | self.remote = remote | |
104 | def submit(self): |
|
104 | def submit(self): | |
105 | req, rsp = [], [] |
|
105 | req, rsp = [], [] | |
106 | for name, args, opts, resref in self.calls: |
|
106 | for name, args, opts, resref in self.calls: | |
107 | mtd = getattr(self.remote, name) |
|
107 | mtd = getattr(self.remote, name) | |
108 | batchablefn = getattr(mtd, 'batchable', None) |
|
108 | batchablefn = getattr(mtd, 'batchable', None) | |
109 | if batchablefn is not None: |
|
109 | if batchablefn is not None: | |
110 | batchable = batchablefn(mtd.im_self, *args, **opts) |
|
110 | batchable = batchablefn(mtd.im_self, *args, **opts) | |
111 | encargsorres, encresref = batchable.next() |
|
111 | encargsorres, encresref = batchable.next() | |
112 | if encresref: |
|
112 | if encresref: | |
113 | req.append((name, encargsorres,)) |
|
113 | req.append((name, encargsorres,)) | |
114 | rsp.append((batchable, encresref, resref,)) |
|
114 | rsp.append((batchable, encresref, resref,)) | |
115 | else: |
|
115 | else: | |
116 | resref.set(encargsorres) |
|
116 | resref.set(encargsorres) | |
117 | else: |
|
117 | else: | |
118 | if req: |
|
118 | if req: | |
119 | self._submitreq(req, rsp) |
|
119 | self._submitreq(req, rsp) | |
120 | req, rsp = [], [] |
|
120 | req, rsp = [], [] | |
121 | resref.set(mtd(*args, **opts)) |
|
121 | resref.set(mtd(*args, **opts)) | |
122 | if req: |
|
122 | if req: | |
123 | self._submitreq(req, rsp) |
|
123 | self._submitreq(req, rsp) | |
124 | def _submitreq(self, req, rsp): |
|
124 | def _submitreq(self, req, rsp): | |
125 | encresults = self.remote._submitbatch(req) |
|
125 | encresults = self.remote._submitbatch(req) | |
126 | for encres, r in zip(encresults, rsp): |
|
126 | for encres, r in zip(encresults, rsp): | |
127 | batchable, encresref, resref = r |
|
127 | batchable, encresref, resref = r | |
128 | encresref.set(encres) |
|
128 | encresref.set(encres) | |
129 | resref.set(batchable.next()) |
|
129 | resref.set(batchable.next()) | |
130 |
|
130 | |||
131 | def batchable(f): |
|
131 | def batchable(f): | |
132 | '''annotation for batchable methods |
|
132 | '''annotation for batchable methods | |
133 |
|
133 | |||
134 | Such methods must implement a coroutine as follows: |
|
134 | Such methods must implement a coroutine as follows: | |
135 |
|
135 | |||
136 | @batchable |
|
136 | @batchable | |
137 | def sample(self, one, two=None): |
|
137 | def sample(self, one, two=None): | |
138 | # Handle locally computable results first: |
|
138 | # Handle locally computable results first: | |
139 | if not one: |
|
139 | if not one: | |
140 | yield "a local result", None |
|
140 | yield "a local result", None | |
141 | # Build list of encoded arguments suitable for your wire protocol: |
|
141 | # Build list of encoded arguments suitable for your wire protocol: | |
142 | encargs = [('one', encode(one),), ('two', encode(two),)] |
|
142 | encargs = [('one', encode(one),), ('two', encode(two),)] | |
143 | # Create future for injection of encoded result: |
|
143 | # Create future for injection of encoded result: | |
144 | encresref = future() |
|
144 | encresref = future() | |
145 | # Return encoded arguments and future: |
|
145 | # Return encoded arguments and future: | |
146 | yield encargs, encresref |
|
146 | yield encargs, encresref | |
147 | # Assuming the future to be filled with the result from the batched |
|
147 | # Assuming the future to be filled with the result from the batched | |
148 | # request now. Decode it: |
|
148 | # request now. Decode it: | |
149 | yield decode(encresref.value) |
|
149 | yield decode(encresref.value) | |
150 |
|
150 | |||
151 | The decorator returns a function which wraps this coroutine as a plain |
|
151 | The decorator returns a function which wraps this coroutine as a plain | |
152 | method, but adds the original method as an attribute called "batchable", |
|
152 | method, but adds the original method as an attribute called "batchable", | |
153 | which is used by remotebatch to split the call into separate encoding and |
|
153 | which is used by remotebatch to split the call into separate encoding and | |
154 | decoding phases. |
|
154 | decoding phases. | |
155 | ''' |
|
155 | ''' | |
156 | def plain(*args, **opts): |
|
156 | def plain(*args, **opts): | |
157 | batchable = f(*args, **opts) |
|
157 | batchable = f(*args, **opts) | |
158 | encargsorres, encresref = batchable.next() |
|
158 | encargsorres, encresref = batchable.next() | |
159 | if not encresref: |
|
159 | if not encresref: | |
160 | return encargsorres # a local result in this case |
|
160 | return encargsorres # a local result in this case | |
161 | self = args[0] |
|
161 | self = args[0] | |
162 | encresref.set(self._submitone(f.func_name, encargsorres)) |
|
162 | encresref.set(self._submitone(f.func_name, encargsorres)) | |
163 | return batchable.next() |
|
163 | return batchable.next() | |
164 | setattr(plain, 'batchable', f) |
|
164 | setattr(plain, 'batchable', f) | |
165 | return plain |
|
165 | return plain | |
166 |
|
166 | |||
167 | # list of nodes encoding / decoding |
|
167 | # list of nodes encoding / decoding | |
168 |
|
168 | |||
169 | def decodelist(l, sep=' '): |
|
169 | def decodelist(l, sep=' '): | |
170 | if l: |
|
170 | if l: | |
171 | return map(bin, l.split(sep)) |
|
171 | return map(bin, l.split(sep)) | |
172 | return [] |
|
172 | return [] | |
173 |
|
173 | |||
174 | def encodelist(l, sep=' '): |
|
174 | def encodelist(l, sep=' '): | |
175 | return sep.join(map(hex, l)) |
|
175 | return sep.join(map(hex, l)) | |
176 |
|
176 | |||
177 | # batched call argument encoding |
|
177 | # batched call argument encoding | |
178 |
|
178 | |||
179 | def escapearg(plain): |
|
179 | def escapearg(plain): | |
180 | return (plain |
|
180 | return (plain | |
181 | .replace(':', '::') |
|
181 | .replace(':', '::') | |
182 | .replace(',', ':,') |
|
182 | .replace(',', ':,') | |
183 | .replace(';', ':;') |
|
183 | .replace(';', ':;') | |
184 | .replace('=', ':=')) |
|
184 | .replace('=', ':=')) | |
185 |
|
185 | |||
186 | def unescapearg(escaped): |
|
186 | def unescapearg(escaped): | |
187 | return (escaped |
|
187 | return (escaped | |
188 | .replace(':=', '=') |
|
188 | .replace(':=', '=') | |
189 | .replace(':;', ';') |
|
189 | .replace(':;', ';') | |
190 | .replace(':,', ',') |
|
190 | .replace(':,', ',') | |
191 | .replace('::', ':')) |
|
191 | .replace('::', ':')) | |
192 |
|
192 | |||
193 | # client side |
|
193 | # client side | |
194 |
|
194 | |||
195 | class wirepeer(peer.peerrepository): |
|
195 | class wirepeer(peer.peerrepository): | |
196 |
|
196 | |||
197 | def batch(self): |
|
197 | def batch(self): | |
198 | return remotebatch(self) |
|
198 | return remotebatch(self) | |
199 | def _submitbatch(self, req): |
|
199 | def _submitbatch(self, req): | |
200 | cmds = [] |
|
200 | cmds = [] | |
201 | for op, argsdict in req: |
|
201 | for op, argsdict in req: | |
202 | args = ','.join('%s=%s' % p for p in argsdict.iteritems()) |
|
202 | args = ','.join('%s=%s' % p for p in argsdict.iteritems()) | |
203 | cmds.append('%s %s' % (op, args)) |
|
203 | cmds.append('%s %s' % (op, args)) | |
204 | rsp = self._call("batch", cmds=';'.join(cmds)) |
|
204 | rsp = self._call("batch", cmds=';'.join(cmds)) | |
205 | return rsp.split(';') |
|
205 | return rsp.split(';') | |
206 | def _submitone(self, op, args): |
|
206 | def _submitone(self, op, args): | |
207 | return self._call(op, **args) |
|
207 | return self._call(op, **args) | |
208 |
|
208 | |||
209 | @batchable |
|
209 | @batchable | |
210 | def lookup(self, key): |
|
210 | def lookup(self, key): | |
211 | self.requirecap('lookup', _('look up remote revision')) |
|
211 | self.requirecap('lookup', _('look up remote revision')) | |
212 | f = future() |
|
212 | f = future() | |
213 | yield {'key': encoding.fromlocal(key)}, f |
|
213 | yield {'key': encoding.fromlocal(key)}, f | |
214 | d = f.value |
|
214 | d = f.value | |
215 | success, data = d[:-1].split(" ", 1) |
|
215 | success, data = d[:-1].split(" ", 1) | |
216 | if int(success): |
|
216 | if int(success): | |
217 | yield bin(data) |
|
217 | yield bin(data) | |
218 | self._abort(error.RepoError(data)) |
|
218 | self._abort(error.RepoError(data)) | |
219 |
|
219 | |||
220 | @batchable |
|
220 | @batchable | |
221 | def heads(self): |
|
221 | def heads(self): | |
222 | f = future() |
|
222 | f = future() | |
223 | yield {}, f |
|
223 | yield {}, f | |
224 | d = f.value |
|
224 | d = f.value | |
225 | try: |
|
225 | try: | |
226 | yield decodelist(d[:-1]) |
|
226 | yield decodelist(d[:-1]) | |
227 | except ValueError: |
|
227 | except ValueError: | |
228 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
228 | self._abort(error.ResponseError(_("unexpected response:"), d)) | |
229 |
|
229 | |||
230 | @batchable |
|
230 | @batchable | |
231 | def known(self, nodes): |
|
231 | def known(self, nodes): | |
232 | f = future() |
|
232 | f = future() | |
233 | yield {'nodes': encodelist(nodes)}, f |
|
233 | yield {'nodes': encodelist(nodes)}, f | |
234 | d = f.value |
|
234 | d = f.value | |
235 | try: |
|
235 | try: | |
236 | yield [bool(int(f)) for f in d] |
|
236 | yield [bool(int(f)) for f in d] | |
237 | except ValueError: |
|
237 | except ValueError: | |
238 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
238 | self._abort(error.ResponseError(_("unexpected response:"), d)) | |
239 |
|
239 | |||
240 | @batchable |
|
240 | @batchable | |
241 | def branchmap(self): |
|
241 | def branchmap(self): | |
242 | f = future() |
|
242 | f = future() | |
243 | yield {}, f |
|
243 | yield {}, f | |
244 | d = f.value |
|
244 | d = f.value | |
245 | try: |
|
245 | try: | |
246 | branchmap = {} |
|
246 | branchmap = {} | |
247 | for branchpart in d.splitlines(): |
|
247 | for branchpart in d.splitlines(): | |
248 | branchname, branchheads = branchpart.split(' ', 1) |
|
248 | branchname, branchheads = branchpart.split(' ', 1) | |
249 | branchname = encoding.tolocal(urllib.unquote(branchname)) |
|
249 | branchname = encoding.tolocal(urllib.unquote(branchname)) | |
250 | branchheads = decodelist(branchheads) |
|
250 | branchheads = decodelist(branchheads) | |
251 | branchmap[branchname] = branchheads |
|
251 | branchmap[branchname] = branchheads | |
252 | yield branchmap |
|
252 | yield branchmap | |
253 | except TypeError: |
|
253 | except TypeError: | |
254 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
254 | self._abort(error.ResponseError(_("unexpected response:"), d)) | |
255 |
|
255 | |||
256 | def branches(self, nodes): |
|
256 | def branches(self, nodes): | |
257 | n = encodelist(nodes) |
|
257 | n = encodelist(nodes) | |
258 | d = self._call("branches", nodes=n) |
|
258 | d = self._call("branches", nodes=n) | |
259 | try: |
|
259 | try: | |
260 | br = [tuple(decodelist(b)) for b in d.splitlines()] |
|
260 | br = [tuple(decodelist(b)) for b in d.splitlines()] | |
261 | return br |
|
261 | return br | |
262 | except ValueError: |
|
262 | except ValueError: | |
263 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
263 | self._abort(error.ResponseError(_("unexpected response:"), d)) | |
264 |
|
264 | |||
265 | def between(self, pairs): |
|
265 | def between(self, pairs): | |
266 | batch = 8 # avoid giant requests |
|
266 | batch = 8 # avoid giant requests | |
267 | r = [] |
|
267 | r = [] | |
268 | for i in xrange(0, len(pairs), batch): |
|
268 | for i in xrange(0, len(pairs), batch): | |
269 | n = " ".join([encodelist(p, '-') for p in pairs[i:i + batch]]) |
|
269 | n = " ".join([encodelist(p, '-') for p in pairs[i:i + batch]]) | |
270 | d = self._call("between", pairs=n) |
|
270 | d = self._call("between", pairs=n) | |
271 | try: |
|
271 | try: | |
272 | r.extend(l and decodelist(l) or [] for l in d.splitlines()) |
|
272 | r.extend(l and decodelist(l) or [] for l in d.splitlines()) | |
273 | except ValueError: |
|
273 | except ValueError: | |
274 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
274 | self._abort(error.ResponseError(_("unexpected response:"), d)) | |
275 | return r |
|
275 | return r | |
276 |
|
276 | |||
277 | @batchable |
|
277 | @batchable | |
278 | def pushkey(self, namespace, key, old, new): |
|
278 | def pushkey(self, namespace, key, old, new): | |
279 | if not self.capable('pushkey'): |
|
279 | if not self.capable('pushkey'): | |
280 | yield False, None |
|
280 | yield False, None | |
281 | f = future() |
|
281 | f = future() | |
282 | self.ui.debug('preparing pushkey for "%s:%s"\n' % (namespace, key)) |
|
282 | self.ui.debug('preparing pushkey for "%s:%s"\n' % (namespace, key)) | |
283 | yield {'namespace': encoding.fromlocal(namespace), |
|
283 | yield {'namespace': encoding.fromlocal(namespace), | |
284 | 'key': encoding.fromlocal(key), |
|
284 | 'key': encoding.fromlocal(key), | |
285 | 'old': encoding.fromlocal(old), |
|
285 | 'old': encoding.fromlocal(old), | |
286 | 'new': encoding.fromlocal(new)}, f |
|
286 | 'new': encoding.fromlocal(new)}, f | |
287 | d = f.value |
|
287 | d = f.value | |
288 | d, output = d.split('\n', 1) |
|
288 | d, output = d.split('\n', 1) | |
289 | try: |
|
289 | try: | |
290 | d = bool(int(d)) |
|
290 | d = bool(int(d)) | |
291 | except ValueError: |
|
291 | except ValueError: | |
292 | raise error.ResponseError( |
|
292 | raise error.ResponseError( | |
293 | _('push failed (unexpected response):'), d) |
|
293 | _('push failed (unexpected response):'), d) | |
294 | for l in output.splitlines(True): |
|
294 | for l in output.splitlines(True): | |
295 | self.ui.status(_('remote: '), l) |
|
295 | self.ui.status(_('remote: '), l) | |
296 | yield d |
|
296 | yield d | |
297 |
|
297 | |||
298 | @batchable |
|
298 | @batchable | |
299 | def listkeys(self, namespace): |
|
299 | def listkeys(self, namespace): | |
300 | if not self.capable('pushkey'): |
|
300 | if not self.capable('pushkey'): | |
301 | yield {}, None |
|
301 | yield {}, None | |
302 | f = future() |
|
302 | f = future() | |
303 | self.ui.debug('preparing listkeys for "%s"\n' % namespace) |
|
303 | self.ui.debug('preparing listkeys for "%s"\n' % namespace) | |
304 | yield {'namespace': encoding.fromlocal(namespace)}, f |
|
304 | yield {'namespace': encoding.fromlocal(namespace)}, f | |
305 | d = f.value |
|
305 | d = f.value | |
306 | r = {} |
|
306 | r = {} | |
307 | for l in d.splitlines(): |
|
307 | for l in d.splitlines(): | |
308 | k, v = l.split('\t') |
|
308 | k, v = l.split('\t') | |
309 | r[encoding.tolocal(k)] = encoding.tolocal(v) |
|
309 | r[encoding.tolocal(k)] = encoding.tolocal(v) | |
310 | yield r |
|
310 | yield r | |
311 |
|
311 | |||
312 | def stream_out(self): |
|
312 | def stream_out(self): | |
313 | return self._callstream('stream_out') |
|
313 | return self._callstream('stream_out') | |
314 |
|
314 | |||
315 | def changegroup(self, nodes, kind): |
|
315 | def changegroup(self, nodes, kind): | |
316 | n = encodelist(nodes) |
|
316 | n = encodelist(nodes) | |
317 | f = self._callcompressable("changegroup", roots=n) |
|
317 | f = self._callcompressable("changegroup", roots=n) | |
318 | return changegroupmod.unbundle10(f, 'UN') |
|
318 | return changegroupmod.unbundle10(f, 'UN') | |
319 |
|
319 | |||
320 | def changegroupsubset(self, bases, heads, kind): |
|
320 | def changegroupsubset(self, bases, heads, kind): | |
321 | self.requirecap('changegroupsubset', _('look up remote changes')) |
|
321 | self.requirecap('changegroupsubset', _('look up remote changes')) | |
322 | bases = encodelist(bases) |
|
322 | bases = encodelist(bases) | |
323 | heads = encodelist(heads) |
|
323 | heads = encodelist(heads) | |
324 | f = self._callcompressable("changegroupsubset", |
|
324 | f = self._callcompressable("changegroupsubset", | |
325 | bases=bases, heads=heads) |
|
325 | bases=bases, heads=heads) | |
326 | return changegroupmod.unbundle10(f, 'UN') |
|
326 | return changegroupmod.unbundle10(f, 'UN') | |
327 |
|
327 | |||
328 | def getbundle(self, source, heads=None, common=None, bundlecaps=None, |
|
328 | def getbundle(self, source, heads=None, common=None, bundlecaps=None, | |
329 | **kwargs): |
|
329 | **kwargs): | |
330 | self.requirecap('getbundle', _('look up remote changes')) |
|
330 | self.requirecap('getbundle', _('look up remote changes')) | |
331 | opts = {} |
|
331 | opts = {} | |
332 | if heads is not None: |
|
332 | if heads is not None: | |
333 | opts['heads'] = encodelist(heads) |
|
333 | opts['heads'] = encodelist(heads) | |
334 | if common is not None: |
|
334 | if common is not None: | |
335 | opts['common'] = encodelist(common) |
|
335 | opts['common'] = encodelist(common) | |
336 | if bundlecaps is not None: |
|
336 | if bundlecaps is not None: | |
337 | opts['bundlecaps'] = ','.join(bundlecaps) |
|
337 | opts['bundlecaps'] = ','.join(bundlecaps) | |
338 | opts.update(kwargs) |
|
338 | opts.update(kwargs) | |
339 | f = self._callcompressable("getbundle", **opts) |
|
339 | f = self._callcompressable("getbundle", **opts) | |
340 | if bundlecaps is not None and 'HG2X' in bundlecaps: |
|
340 | if bundlecaps is not None and 'HG2X' in bundlecaps: | |
341 | return bundle2.unbundle20(self.ui, f) |
|
341 | return bundle2.unbundle20(self.ui, f) | |
342 | else: |
|
342 | else: | |
343 | return changegroupmod.unbundle10(f, 'UN') |
|
343 | return changegroupmod.unbundle10(f, 'UN') | |
344 |
|
344 | |||
345 | def unbundle(self, cg, heads, source): |
|
345 | def unbundle(self, cg, heads, source): | |
346 | '''Send cg (a readable file-like object representing the |
|
346 | '''Send cg (a readable file-like object representing the | |
347 | changegroup to push, typically a chunkbuffer object) to the |
|
347 | changegroup to push, typically a chunkbuffer object) to the | |
348 | remote server as a bundle. |
|
348 | remote server as a bundle. | |
349 |
|
349 | |||
350 | When pushing a bundle10 stream, return an integer indicating the |
|
350 | When pushing a bundle10 stream, return an integer indicating the | |
351 | result of the push (see localrepository.addchangegroup()). |
|
351 | result of the push (see localrepository.addchangegroup()). | |
352 |
|
352 | |||
353 | When pushing a bundle20 stream, return a bundle20 stream.''' |
|
353 | When pushing a bundle20 stream, return a bundle20 stream.''' | |
354 |
|
354 | |||
355 | if heads != ['force'] and self.capable('unbundlehash'): |
|
355 | if heads != ['force'] and self.capable('unbundlehash'): | |
356 | heads = encodelist(['hashed', |
|
356 | heads = encodelist(['hashed', | |
357 | util.sha1(''.join(sorted(heads))).digest()]) |
|
357 | util.sha1(''.join(sorted(heads))).digest()]) | |
358 | else: |
|
358 | else: | |
359 | heads = encodelist(heads) |
|
359 | heads = encodelist(heads) | |
360 |
|
360 | |||
361 | if util.safehasattr(cg, 'deltaheader'): |
|
361 | if util.safehasattr(cg, 'deltaheader'): | |
362 | # this a bundle10, do the old style call sequence |
|
362 | # this a bundle10, do the old style call sequence | |
363 | ret, output = self._callpush("unbundle", cg, heads=heads) |
|
363 | ret, output = self._callpush("unbundle", cg, heads=heads) | |
364 | if ret == "": |
|
364 | if ret == "": | |
365 | raise error.ResponseError( |
|
365 | raise error.ResponseError( | |
366 | _('push failed:'), output) |
|
366 | _('push failed:'), output) | |
367 | try: |
|
367 | try: | |
368 | ret = int(ret) |
|
368 | ret = int(ret) | |
369 | except ValueError: |
|
369 | except ValueError: | |
370 | raise error.ResponseError( |
|
370 | raise error.ResponseError( | |
371 | _('push failed (unexpected response):'), ret) |
|
371 | _('push failed (unexpected response):'), ret) | |
372 |
|
372 | |||
373 | for l in output.splitlines(True): |
|
373 | for l in output.splitlines(True): | |
374 | self.ui.status(_('remote: '), l) |
|
374 | self.ui.status(_('remote: '), l) | |
375 | else: |
|
375 | else: | |
376 | # bundle2 push. Send a stream, fetch a stream. |
|
376 | # bundle2 push. Send a stream, fetch a stream. | |
377 | stream = self._calltwowaystream('unbundle', cg, heads=heads) |
|
377 | stream = self._calltwowaystream('unbundle', cg, heads=heads) | |
378 | ret = bundle2.unbundle20(self.ui, stream) |
|
378 | ret = bundle2.unbundle20(self.ui, stream) | |
379 | return ret |
|
379 | return ret | |
380 |
|
380 | |||
381 | def debugwireargs(self, one, two, three=None, four=None, five=None): |
|
381 | def debugwireargs(self, one, two, three=None, four=None, five=None): | |
382 | # don't pass optional arguments left at their default value |
|
382 | # don't pass optional arguments left at their default value | |
383 | opts = {} |
|
383 | opts = {} | |
384 | if three is not None: |
|
384 | if three is not None: | |
385 | opts['three'] = three |
|
385 | opts['three'] = three | |
386 | if four is not None: |
|
386 | if four is not None: | |
387 | opts['four'] = four |
|
387 | opts['four'] = four | |
388 | return self._call('debugwireargs', one=one, two=two, **opts) |
|
388 | return self._call('debugwireargs', one=one, two=two, **opts) | |
389 |
|
389 | |||
390 | def _call(self, cmd, **args): |
|
390 | def _call(self, cmd, **args): | |
391 | """execute <cmd> on the server |
|
391 | """execute <cmd> on the server | |
392 |
|
392 | |||
393 | The command is expected to return a simple string. |
|
393 | The command is expected to return a simple string. | |
394 |
|
394 | |||
395 | returns the server reply as a string.""" |
|
395 | returns the server reply as a string.""" | |
396 | raise NotImplementedError() |
|
396 | raise NotImplementedError() | |
397 |
|
397 | |||
398 | def _callstream(self, cmd, **args): |
|
398 | def _callstream(self, cmd, **args): | |
399 | """execute <cmd> on the server |
|
399 | """execute <cmd> on the server | |
400 |
|
400 | |||
401 | The command is expected to return a stream. |
|
401 | The command is expected to return a stream. | |
402 |
|
402 | |||
403 | returns the server reply as a file like object.""" |
|
403 | returns the server reply as a file like object.""" | |
404 | raise NotImplementedError() |
|
404 | raise NotImplementedError() | |
405 |
|
405 | |||
406 | def _callcompressable(self, cmd, **args): |
|
406 | def _callcompressable(self, cmd, **args): | |
407 | """execute <cmd> on the server |
|
407 | """execute <cmd> on the server | |
408 |
|
408 | |||
409 | The command is expected to return a stream. |
|
409 | The command is expected to return a stream. | |
410 |
|
410 | |||
411 | The stream may have been compressed in some implementations. This |
|
411 | The stream may have been compressed in some implementations. This | |
412 | function takes care of the decompression. This is the only difference |
|
412 | function takes care of the decompression. This is the only difference | |
413 | with _callstream. |
|
413 | with _callstream. | |
414 |
|
414 | |||
415 | returns the server reply as a file like object. |
|
415 | returns the server reply as a file like object. | |
416 | """ |
|
416 | """ | |
417 | raise NotImplementedError() |
|
417 | raise NotImplementedError() | |
418 |
|
418 | |||
419 | def _callpush(self, cmd, fp, **args): |
|
419 | def _callpush(self, cmd, fp, **args): | |
420 | """execute a <cmd> on server |
|
420 | """execute a <cmd> on server | |
421 |
|
421 | |||
422 | The command is expected to be related to a push. Push has a special |
|
422 | The command is expected to be related to a push. Push has a special | |
423 | return method. |
|
423 | return method. | |
424 |
|
424 | |||
425 | returns the server reply as a (ret, output) tuple. ret is either |
|
425 | returns the server reply as a (ret, output) tuple. ret is either | |
426 | empty (error) or a stringified int. |
|
426 | empty (error) or a stringified int. | |
427 | """ |
|
427 | """ | |
428 | raise NotImplementedError() |
|
428 | raise NotImplementedError() | |
429 |
|
429 | |||
430 | def _calltwowaystream(self, cmd, fp, **args): |
|
430 | def _calltwowaystream(self, cmd, fp, **args): | |
431 | """execute <cmd> on server |
|
431 | """execute <cmd> on server | |
432 |
|
432 | |||
433 | The command will send a stream to the server and get a stream in reply. |
|
433 | The command will send a stream to the server and get a stream in reply. | |
434 | """ |
|
434 | """ | |
435 | raise NotImplementedError() |
|
435 | raise NotImplementedError() | |
436 |
|
436 | |||
437 | def _abort(self, exception): |
|
437 | def _abort(self, exception): | |
438 | """clearly abort the wire protocol connection and raise the exception |
|
438 | """clearly abort the wire protocol connection and raise the exception | |
439 | """ |
|
439 | """ | |
440 | raise NotImplementedError() |
|
440 | raise NotImplementedError() | |
441 |
|
441 | |||
442 | # server side |
|
442 | # server side | |
443 |
|
443 | |||
444 | # wire protocol command can either return a string or one of these classes. |
|
444 | # wire protocol command can either return a string or one of these classes. | |
445 | class streamres(object): |
|
445 | class streamres(object): | |
446 | """wireproto reply: binary stream |
|
446 | """wireproto reply: binary stream | |
447 |
|
447 | |||
448 | The call was successful and the result is a stream. |
|
448 | The call was successful and the result is a stream. | |
449 | Iterate on the `self.gen` attribute to retrieve chunks. |
|
449 | Iterate on the `self.gen` attribute to retrieve chunks. | |
450 | """ |
|
450 | """ | |
451 | def __init__(self, gen): |
|
451 | def __init__(self, gen): | |
452 | self.gen = gen |
|
452 | self.gen = gen | |
453 |
|
453 | |||
454 | class pushres(object): |
|
454 | class pushres(object): | |
455 | """wireproto reply: success with simple integer return |
|
455 | """wireproto reply: success with simple integer return | |
456 |
|
456 | |||
457 | The call was successful and returned an integer contained in `self.res`. |
|
457 | The call was successful and returned an integer contained in `self.res`. | |
458 | """ |
|
458 | """ | |
459 | def __init__(self, res): |
|
459 | def __init__(self, res): | |
460 | self.res = res |
|
460 | self.res = res | |
461 |
|
461 | |||
462 | class pusherr(object): |
|
462 | class pusherr(object): | |
463 | """wireproto reply: failure |
|
463 | """wireproto reply: failure | |
464 |
|
464 | |||
465 | The call failed. The `self.res` attribute contains the error message. |
|
465 | The call failed. The `self.res` attribute contains the error message. | |
466 | """ |
|
466 | """ | |
467 | def __init__(self, res): |
|
467 | def __init__(self, res): | |
468 | self.res = res |
|
468 | self.res = res | |
469 |
|
469 | |||
470 | class ooberror(object): |
|
470 | class ooberror(object): | |
471 | """wireproto reply: failure of a batch of operation |
|
471 | """wireproto reply: failure of a batch of operation | |
472 |
|
472 | |||
473 | Something failed during a batch call. The error message is stored in |
|
473 | Something failed during a batch call. The error message is stored in | |
474 | `self.message`. |
|
474 | `self.message`. | |
475 | """ |
|
475 | """ | |
476 | def __init__(self, message): |
|
476 | def __init__(self, message): | |
477 | self.message = message |
|
477 | self.message = message | |
478 |
|
478 | |||
479 | def dispatch(repo, proto, command): |
|
479 | def dispatch(repo, proto, command): | |
480 | repo = repo.filtered("served") |
|
480 | repo = repo.filtered("served") | |
481 | func, spec = commands[command] |
|
481 | func, spec = commands[command] | |
482 | args = proto.getargs(spec) |
|
482 | args = proto.getargs(spec) | |
483 | return func(repo, proto, *args) |
|
483 | return func(repo, proto, *args) | |
484 |
|
484 | |||
485 | def options(cmd, keys, others): |
|
485 | def options(cmd, keys, others): | |
486 | opts = {} |
|
486 | opts = {} | |
487 | for k in keys: |
|
487 | for k in keys: | |
488 | if k in others: |
|
488 | if k in others: | |
489 | opts[k] = others[k] |
|
489 | opts[k] = others[k] | |
490 | del others[k] |
|
490 | del others[k] | |
491 | if others: |
|
491 | if others: | |
492 | sys.stderr.write("abort: %s got unexpected arguments %s\n" |
|
492 | sys.stderr.write("abort: %s got unexpected arguments %s\n" | |
493 | % (cmd, ",".join(others))) |
|
493 | % (cmd, ",".join(others))) | |
494 | return opts |
|
494 | return opts | |
495 |
|
495 | |||
496 | # list of commands |
|
496 | # list of commands | |
497 | commands = {} |
|
497 | commands = {} | |
498 |
|
498 | |||
499 | def wireprotocommand(name, args=''): |
|
499 | def wireprotocommand(name, args=''): | |
500 | """decorator for wire protocol command""" |
|
500 | """decorator for wire protocol command""" | |
501 | def register(func): |
|
501 | def register(func): | |
502 | commands[name] = (func, args) |
|
502 | commands[name] = (func, args) | |
503 | return func |
|
503 | return func | |
504 | return register |
|
504 | return register | |
505 |
|
505 | |||
506 | @wireprotocommand('batch', 'cmds *') |
|
506 | @wireprotocommand('batch', 'cmds *') | |
507 | def batch(repo, proto, cmds, others): |
|
507 | def batch(repo, proto, cmds, others): | |
508 | repo = repo.filtered("served") |
|
508 | repo = repo.filtered("served") | |
509 | res = [] |
|
509 | res = [] | |
510 | for pair in cmds.split(';'): |
|
510 | for pair in cmds.split(';'): | |
511 | op, args = pair.split(' ', 1) |
|
511 | op, args = pair.split(' ', 1) | |
512 | vals = {} |
|
512 | vals = {} | |
513 | for a in args.split(','): |
|
513 | for a in args.split(','): | |
514 | if a: |
|
514 | if a: | |
515 | n, v = a.split('=') |
|
515 | n, v = a.split('=') | |
516 | vals[n] = unescapearg(v) |
|
516 | vals[n] = unescapearg(v) | |
517 | func, spec = commands[op] |
|
517 | func, spec = commands[op] | |
518 | if spec: |
|
518 | if spec: | |
519 | keys = spec.split() |
|
519 | keys = spec.split() | |
520 | data = {} |
|
520 | data = {} | |
521 | for k in keys: |
|
521 | for k in keys: | |
522 | if k == '*': |
|
522 | if k == '*': | |
523 | star = {} |
|
523 | star = {} | |
524 | for key in vals.keys(): |
|
524 | for key in vals.keys(): | |
525 | if key not in keys: |
|
525 | if key not in keys: | |
526 | star[key] = vals[key] |
|
526 | star[key] = vals[key] | |
527 | data['*'] = star |
|
527 | data['*'] = star | |
528 | else: |
|
528 | else: | |
529 | data[k] = vals[k] |
|
529 | data[k] = vals[k] | |
530 | result = func(repo, proto, *[data[k] for k in keys]) |
|
530 | result = func(repo, proto, *[data[k] for k in keys]) | |
531 | else: |
|
531 | else: | |
532 | result = func(repo, proto) |
|
532 | result = func(repo, proto) | |
533 | if isinstance(result, ooberror): |
|
533 | if isinstance(result, ooberror): | |
534 | return result |
|
534 | return result | |
535 | res.append(escapearg(result)) |
|
535 | res.append(escapearg(result)) | |
536 | return ';'.join(res) |
|
536 | return ';'.join(res) | |
537 |
|
537 | |||
538 | @wireprotocommand('between', 'pairs') |
|
538 | @wireprotocommand('between', 'pairs') | |
539 | def between(repo, proto, pairs): |
|
539 | def between(repo, proto, pairs): | |
540 | pairs = [decodelist(p, '-') for p in pairs.split(" ")] |
|
540 | pairs = [decodelist(p, '-') for p in pairs.split(" ")] | |
541 | r = [] |
|
541 | r = [] | |
542 | for b in repo.between(pairs): |
|
542 | for b in repo.between(pairs): | |
543 | r.append(encodelist(b) + "\n") |
|
543 | r.append(encodelist(b) + "\n") | |
544 | return "".join(r) |
|
544 | return "".join(r) | |
545 |
|
545 | |||
546 | @wireprotocommand('branchmap') |
|
546 | @wireprotocommand('branchmap') | |
547 | def branchmap(repo, proto): |
|
547 | def branchmap(repo, proto): | |
548 | branchmap = repo.branchmap() |
|
548 | branchmap = repo.branchmap() | |
549 | heads = [] |
|
549 | heads = [] | |
550 | for branch, nodes in branchmap.iteritems(): |
|
550 | for branch, nodes in branchmap.iteritems(): | |
551 | branchname = urllib.quote(encoding.fromlocal(branch)) |
|
551 | branchname = urllib.quote(encoding.fromlocal(branch)) | |
552 | branchnodes = encodelist(nodes) |
|
552 | branchnodes = encodelist(nodes) | |
553 | heads.append('%s %s' % (branchname, branchnodes)) |
|
553 | heads.append('%s %s' % (branchname, branchnodes)) | |
554 | return '\n'.join(heads) |
|
554 | return '\n'.join(heads) | |
555 |
|
555 | |||
556 | @wireprotocommand('branches', 'nodes') |
|
556 | @wireprotocommand('branches', 'nodes') | |
557 | def branches(repo, proto, nodes): |
|
557 | def branches(repo, proto, nodes): | |
558 | nodes = decodelist(nodes) |
|
558 | nodes = decodelist(nodes) | |
559 | r = [] |
|
559 | r = [] | |
560 | for b in repo.branches(nodes): |
|
560 | for b in repo.branches(nodes): | |
561 | r.append(encodelist(b) + "\n") |
|
561 | r.append(encodelist(b) + "\n") | |
562 | return "".join(r) |
|
562 | return "".join(r) | |
563 |
|
563 | |||
564 |
|
564 | |||
565 | wireprotocaps = ['lookup', 'changegroupsubset', 'branchmap', 'pushkey', |
|
565 | wireprotocaps = ['lookup', 'changegroupsubset', 'branchmap', 'pushkey', | |
566 | 'known', 'getbundle', 'unbundlehash', 'batch'] |
|
566 | 'known', 'getbundle', 'unbundlehash', 'batch'] | |
567 |
|
567 | |||
568 | def _capabilities(repo, proto): |
|
568 | def _capabilities(repo, proto): | |
569 | """return a list of capabilities for a repo |
|
569 | """return a list of capabilities for a repo | |
570 |
|
570 | |||
571 | This function exists to allow extensions to easily wrap capabilities |
|
571 | This function exists to allow extensions to easily wrap capabilities | |
572 | computation |
|
572 | computation | |
573 |
|
573 | |||
574 | - returns a lists: easy to alter |
|
574 | - returns a lists: easy to alter | |
575 | - change done here will be propagated to both `capabilities` and `hello` |
|
575 | - change done here will be propagated to both `capabilities` and `hello` | |
576 | command without any other action needed. |
|
576 | command without any other action needed. | |
577 | """ |
|
577 | """ | |
578 | # copy to prevent modification of the global list |
|
578 | # copy to prevent modification of the global list | |
579 | caps = list(wireprotocaps) |
|
579 | caps = list(wireprotocaps) | |
580 | if _allowstream(repo.ui): |
|
580 | if _allowstream(repo.ui): | |
581 | if repo.ui.configbool('server', 'preferuncompressed', False): |
|
581 | if repo.ui.configbool('server', 'preferuncompressed', False): | |
582 | caps.append('stream-preferred') |
|
582 | caps.append('stream-preferred') | |
583 | requiredformats = repo.requirements & repo.supportedformats |
|
583 | requiredformats = repo.requirements & repo.supportedformats | |
584 | # if our local revlogs are just revlogv1, add 'stream' cap |
|
584 | # if our local revlogs are just revlogv1, add 'stream' cap | |
585 | if not requiredformats - set(('revlogv1',)): |
|
585 | if not requiredformats - set(('revlogv1',)): | |
586 | caps.append('stream') |
|
586 | caps.append('stream') | |
587 | # otherwise, add 'streamreqs' detailing our local revlog format |
|
587 | # otherwise, add 'streamreqs' detailing our local revlog format | |
588 | else: |
|
588 | else: | |
589 | caps.append('streamreqs=%s' % ','.join(requiredformats)) |
|
589 | caps.append('streamreqs=%s' % ','.join(requiredformats)) | |
590 | if repo.ui.configbool('experimental', 'bundle2-exp', False): |
|
590 | if repo.ui.configbool('experimental', 'bundle2-exp', False): | |
591 | capsblob = bundle2.encodecaps(repo.bundle2caps) |
|
591 | capsblob = bundle2.encodecaps(repo.bundle2caps) | |
592 | caps.append('bundle2-exp=' + urllib.quote(capsblob)) |
|
592 | caps.append('bundle2-exp=' + urllib.quote(capsblob)) | |
593 | caps.append('unbundle=%s' % ','.join(changegroupmod.bundlepriority)) |
|
593 | caps.append('unbundle=%s' % ','.join(changegroupmod.bundlepriority)) | |
594 | caps.append('httpheader=1024') |
|
594 | caps.append('httpheader=1024') | |
595 | return caps |
|
595 | return caps | |
596 |
|
596 | |||
597 | # If you are writing an extension and consider wrapping this function. Wrap |
|
597 | # If you are writing an extension and consider wrapping this function. Wrap | |
598 | # `_capabilities` instead. |
|
598 | # `_capabilities` instead. | |
599 | @wireprotocommand('capabilities') |
|
599 | @wireprotocommand('capabilities') | |
600 | def capabilities(repo, proto): |
|
600 | def capabilities(repo, proto): | |
601 | return ' '.join(_capabilities(repo, proto)) |
|
601 | return ' '.join(_capabilities(repo, proto)) | |
602 |
|
602 | |||
603 | @wireprotocommand('changegroup', 'roots') |
|
603 | @wireprotocommand('changegroup', 'roots') | |
604 | def changegroup(repo, proto, roots): |
|
604 | def changegroup(repo, proto, roots): | |
605 | nodes = decodelist(roots) |
|
605 | nodes = decodelist(roots) | |
606 | cg = changegroupmod.changegroup(repo, nodes, 'serve') |
|
606 | cg = changegroupmod.changegroup(repo, nodes, 'serve') | |
607 | return streamres(proto.groupchunks(cg)) |
|
607 | return streamres(proto.groupchunks(cg)) | |
608 |
|
608 | |||
609 | @wireprotocommand('changegroupsubset', 'bases heads') |
|
609 | @wireprotocommand('changegroupsubset', 'bases heads') | |
610 | def changegroupsubset(repo, proto, bases, heads): |
|
610 | def changegroupsubset(repo, proto, bases, heads): | |
611 | bases = decodelist(bases) |
|
611 | bases = decodelist(bases) | |
612 | heads = decodelist(heads) |
|
612 | heads = decodelist(heads) | |
613 | cg = changegroupmod.changegroupsubset(repo, bases, heads, 'serve') |
|
613 | cg = changegroupmod.changegroupsubset(repo, bases, heads, 'serve') | |
614 | return streamres(proto.groupchunks(cg)) |
|
614 | return streamres(proto.groupchunks(cg)) | |
615 |
|
615 | |||
616 | @wireprotocommand('debugwireargs', 'one two *') |
|
616 | @wireprotocommand('debugwireargs', 'one two *') | |
617 | def debugwireargs(repo, proto, one, two, others): |
|
617 | def debugwireargs(repo, proto, one, two, others): | |
618 | # only accept optional args from the known set |
|
618 | # only accept optional args from the known set | |
619 | opts = options('debugwireargs', ['three', 'four'], others) |
|
619 | opts = options('debugwireargs', ['three', 'four'], others) | |
620 | return repo.debugwireargs(one, two, **opts) |
|
620 | return repo.debugwireargs(one, two, **opts) | |
621 |
|
621 | |||
|
622 | # List of options accepted by getbundle. | |||
|
623 | # | |||
|
624 | # Meant to be extended by extensions. It is the extension's responsibility to | |||
|
625 | # ensure such options are properly processed in exchange.getbundle. | |||
|
626 | gboptslist = ['heads', 'common', 'bundlecaps'] | |||
|
627 | ||||
622 | @wireprotocommand('getbundle', '*') |
|
628 | @wireprotocommand('getbundle', '*') | |
623 | def getbundle(repo, proto, others): |
|
629 | def getbundle(repo, proto, others): | |
624 |
opts = options('getbundle', |
|
630 | opts = options('getbundle', gboptslist, others) | |
625 | for k, v in opts.iteritems(): |
|
631 | for k, v in opts.iteritems(): | |
626 | if k in ('heads', 'common'): |
|
632 | if k in ('heads', 'common'): | |
627 | opts[k] = decodelist(v) |
|
633 | opts[k] = decodelist(v) | |
628 | elif k == 'bundlecaps': |
|
634 | elif k == 'bundlecaps': | |
629 | opts[k] = set(v.split(',')) |
|
635 | opts[k] = set(v.split(',')) | |
630 | cg = exchange.getbundle(repo, 'serve', **opts) |
|
636 | cg = exchange.getbundle(repo, 'serve', **opts) | |
631 | return streamres(proto.groupchunks(cg)) |
|
637 | return streamres(proto.groupchunks(cg)) | |
632 |
|
638 | |||
633 | @wireprotocommand('heads') |
|
639 | @wireprotocommand('heads') | |
634 | def heads(repo, proto): |
|
640 | def heads(repo, proto): | |
635 | h = repo.heads() |
|
641 | h = repo.heads() | |
636 | return encodelist(h) + "\n" |
|
642 | return encodelist(h) + "\n" | |
637 |
|
643 | |||
638 | @wireprotocommand('hello') |
|
644 | @wireprotocommand('hello') | |
639 | def hello(repo, proto): |
|
645 | def hello(repo, proto): | |
640 | '''the hello command returns a set of lines describing various |
|
646 | '''the hello command returns a set of lines describing various | |
641 | interesting things about the server, in an RFC822-like format. |
|
647 | interesting things about the server, in an RFC822-like format. | |
642 | Currently the only one defined is "capabilities", which |
|
648 | Currently the only one defined is "capabilities", which | |
643 | consists of a line in the form: |
|
649 | consists of a line in the form: | |
644 |
|
650 | |||
645 | capabilities: space separated list of tokens |
|
651 | capabilities: space separated list of tokens | |
646 | ''' |
|
652 | ''' | |
647 | return "capabilities: %s\n" % (capabilities(repo, proto)) |
|
653 | return "capabilities: %s\n" % (capabilities(repo, proto)) | |
648 |
|
654 | |||
649 | @wireprotocommand('listkeys', 'namespace') |
|
655 | @wireprotocommand('listkeys', 'namespace') | |
650 | def listkeys(repo, proto, namespace): |
|
656 | def listkeys(repo, proto, namespace): | |
651 | d = repo.listkeys(encoding.tolocal(namespace)).items() |
|
657 | d = repo.listkeys(encoding.tolocal(namespace)).items() | |
652 | t = '\n'.join(['%s\t%s' % (encoding.fromlocal(k), encoding.fromlocal(v)) |
|
658 | t = '\n'.join(['%s\t%s' % (encoding.fromlocal(k), encoding.fromlocal(v)) | |
653 | for k, v in d]) |
|
659 | for k, v in d]) | |
654 | return t |
|
660 | return t | |
655 |
|
661 | |||
656 | @wireprotocommand('lookup', 'key') |
|
662 | @wireprotocommand('lookup', 'key') | |
657 | def lookup(repo, proto, key): |
|
663 | def lookup(repo, proto, key): | |
658 | try: |
|
664 | try: | |
659 | k = encoding.tolocal(key) |
|
665 | k = encoding.tolocal(key) | |
660 | c = repo[k] |
|
666 | c = repo[k] | |
661 | r = c.hex() |
|
667 | r = c.hex() | |
662 | success = 1 |
|
668 | success = 1 | |
663 | except Exception, inst: |
|
669 | except Exception, inst: | |
664 | r = str(inst) |
|
670 | r = str(inst) | |
665 | success = 0 |
|
671 | success = 0 | |
666 | return "%s %s\n" % (success, r) |
|
672 | return "%s %s\n" % (success, r) | |
667 |
|
673 | |||
668 | @wireprotocommand('known', 'nodes *') |
|
674 | @wireprotocommand('known', 'nodes *') | |
669 | def known(repo, proto, nodes, others): |
|
675 | def known(repo, proto, nodes, others): | |
670 | return ''.join(b and "1" or "0" for b in repo.known(decodelist(nodes))) |
|
676 | return ''.join(b and "1" or "0" for b in repo.known(decodelist(nodes))) | |
671 |
|
677 | |||
672 | @wireprotocommand('pushkey', 'namespace key old new') |
|
678 | @wireprotocommand('pushkey', 'namespace key old new') | |
673 | def pushkey(repo, proto, namespace, key, old, new): |
|
679 | def pushkey(repo, proto, namespace, key, old, new): | |
674 | # compatibility with pre-1.8 clients which were accidentally |
|
680 | # compatibility with pre-1.8 clients which were accidentally | |
675 | # sending raw binary nodes rather than utf-8-encoded hex |
|
681 | # sending raw binary nodes rather than utf-8-encoded hex | |
676 | if len(new) == 20 and new.encode('string-escape') != new: |
|
682 | if len(new) == 20 and new.encode('string-escape') != new: | |
677 | # looks like it could be a binary node |
|
683 | # looks like it could be a binary node | |
678 | try: |
|
684 | try: | |
679 | new.decode('utf-8') |
|
685 | new.decode('utf-8') | |
680 | new = encoding.tolocal(new) # but cleanly decodes as UTF-8 |
|
686 | new = encoding.tolocal(new) # but cleanly decodes as UTF-8 | |
681 | except UnicodeDecodeError: |
|
687 | except UnicodeDecodeError: | |
682 | pass # binary, leave unmodified |
|
688 | pass # binary, leave unmodified | |
683 | else: |
|
689 | else: | |
684 | new = encoding.tolocal(new) # normal path |
|
690 | new = encoding.tolocal(new) # normal path | |
685 |
|
691 | |||
686 | if util.safehasattr(proto, 'restore'): |
|
692 | if util.safehasattr(proto, 'restore'): | |
687 |
|
693 | |||
688 | proto.redirect() |
|
694 | proto.redirect() | |
689 |
|
695 | |||
690 | try: |
|
696 | try: | |
691 | r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key), |
|
697 | r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key), | |
692 | encoding.tolocal(old), new) or False |
|
698 | encoding.tolocal(old), new) or False | |
693 | except util.Abort: |
|
699 | except util.Abort: | |
694 | r = False |
|
700 | r = False | |
695 |
|
701 | |||
696 | output = proto.restore() |
|
702 | output = proto.restore() | |
697 |
|
703 | |||
698 | return '%s\n%s' % (int(r), output) |
|
704 | return '%s\n%s' % (int(r), output) | |
699 |
|
705 | |||
700 | r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key), |
|
706 | r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key), | |
701 | encoding.tolocal(old), new) |
|
707 | encoding.tolocal(old), new) | |
702 | return '%s\n' % int(r) |
|
708 | return '%s\n' % int(r) | |
703 |
|
709 | |||
704 | def _allowstream(ui): |
|
710 | def _allowstream(ui): | |
705 | return ui.configbool('server', 'uncompressed', True, untrusted=True) |
|
711 | return ui.configbool('server', 'uncompressed', True, untrusted=True) | |
706 |
|
712 | |||
707 | def _walkstreamfiles(repo): |
|
713 | def _walkstreamfiles(repo): | |
708 | # this is it's own function so extensions can override it |
|
714 | # this is it's own function so extensions can override it | |
709 | return repo.store.walk() |
|
715 | return repo.store.walk() | |
710 |
|
716 | |||
711 | @wireprotocommand('stream_out') |
|
717 | @wireprotocommand('stream_out') | |
712 | def stream(repo, proto): |
|
718 | def stream(repo, proto): | |
713 | '''If the server supports streaming clone, it advertises the "stream" |
|
719 | '''If the server supports streaming clone, it advertises the "stream" | |
714 | capability with a value representing the version and flags of the repo |
|
720 | capability with a value representing the version and flags of the repo | |
715 | it is serving. Client checks to see if it understands the format. |
|
721 | it is serving. Client checks to see if it understands the format. | |
716 |
|
722 | |||
717 | The format is simple: the server writes out a line with the amount |
|
723 | The format is simple: the server writes out a line with the amount | |
718 | of files, then the total amount of bytes to be transferred (separated |
|
724 | of files, then the total amount of bytes to be transferred (separated | |
719 | by a space). Then, for each file, the server first writes the filename |
|
725 | by a space). Then, for each file, the server first writes the filename | |
720 | and file size (separated by the null character), then the file contents. |
|
726 | and file size (separated by the null character), then the file contents. | |
721 | ''' |
|
727 | ''' | |
722 |
|
728 | |||
723 | if not _allowstream(repo.ui): |
|
729 | if not _allowstream(repo.ui): | |
724 | return '1\n' |
|
730 | return '1\n' | |
725 |
|
731 | |||
726 | entries = [] |
|
732 | entries = [] | |
727 | total_bytes = 0 |
|
733 | total_bytes = 0 | |
728 | try: |
|
734 | try: | |
729 | # get consistent snapshot of repo, lock during scan |
|
735 | # get consistent snapshot of repo, lock during scan | |
730 | lock = repo.lock() |
|
736 | lock = repo.lock() | |
731 | try: |
|
737 | try: | |
732 | repo.ui.debug('scanning\n') |
|
738 | repo.ui.debug('scanning\n') | |
733 | for name, ename, size in _walkstreamfiles(repo): |
|
739 | for name, ename, size in _walkstreamfiles(repo): | |
734 | if size: |
|
740 | if size: | |
735 | entries.append((name, size)) |
|
741 | entries.append((name, size)) | |
736 | total_bytes += size |
|
742 | total_bytes += size | |
737 | finally: |
|
743 | finally: | |
738 | lock.release() |
|
744 | lock.release() | |
739 | except error.LockError: |
|
745 | except error.LockError: | |
740 | return '2\n' # error: 2 |
|
746 | return '2\n' # error: 2 | |
741 |
|
747 | |||
742 | def streamer(repo, entries, total): |
|
748 | def streamer(repo, entries, total): | |
743 | '''stream out all metadata files in repository.''' |
|
749 | '''stream out all metadata files in repository.''' | |
744 | yield '0\n' # success |
|
750 | yield '0\n' # success | |
745 | repo.ui.debug('%d files, %d bytes to transfer\n' % |
|
751 | repo.ui.debug('%d files, %d bytes to transfer\n' % | |
746 | (len(entries), total_bytes)) |
|
752 | (len(entries), total_bytes)) | |
747 | yield '%d %d\n' % (len(entries), total_bytes) |
|
753 | yield '%d %d\n' % (len(entries), total_bytes) | |
748 |
|
754 | |||
749 | sopener = repo.sopener |
|
755 | sopener = repo.sopener | |
750 | oldaudit = sopener.mustaudit |
|
756 | oldaudit = sopener.mustaudit | |
751 | debugflag = repo.ui.debugflag |
|
757 | debugflag = repo.ui.debugflag | |
752 | sopener.mustaudit = False |
|
758 | sopener.mustaudit = False | |
753 |
|
759 | |||
754 | try: |
|
760 | try: | |
755 | for name, size in entries: |
|
761 | for name, size in entries: | |
756 | if debugflag: |
|
762 | if debugflag: | |
757 | repo.ui.debug('sending %s (%d bytes)\n' % (name, size)) |
|
763 | repo.ui.debug('sending %s (%d bytes)\n' % (name, size)) | |
758 | # partially encode name over the wire for backwards compat |
|
764 | # partially encode name over the wire for backwards compat | |
759 | yield '%s\0%d\n' % (store.encodedir(name), size) |
|
765 | yield '%s\0%d\n' % (store.encodedir(name), size) | |
760 | if size <= 65536: |
|
766 | if size <= 65536: | |
761 | fp = sopener(name) |
|
767 | fp = sopener(name) | |
762 | try: |
|
768 | try: | |
763 | data = fp.read(size) |
|
769 | data = fp.read(size) | |
764 | finally: |
|
770 | finally: | |
765 | fp.close() |
|
771 | fp.close() | |
766 | yield data |
|
772 | yield data | |
767 | else: |
|
773 | else: | |
768 | for chunk in util.filechunkiter(sopener(name), limit=size): |
|
774 | for chunk in util.filechunkiter(sopener(name), limit=size): | |
769 | yield chunk |
|
775 | yield chunk | |
770 | # replace with "finally:" when support for python 2.4 has been dropped |
|
776 | # replace with "finally:" when support for python 2.4 has been dropped | |
771 | except Exception: |
|
777 | except Exception: | |
772 | sopener.mustaudit = oldaudit |
|
778 | sopener.mustaudit = oldaudit | |
773 | raise |
|
779 | raise | |
774 | sopener.mustaudit = oldaudit |
|
780 | sopener.mustaudit = oldaudit | |
775 |
|
781 | |||
776 | return streamres(streamer(repo, entries, total_bytes)) |
|
782 | return streamres(streamer(repo, entries, total_bytes)) | |
777 |
|
783 | |||
778 | @wireprotocommand('unbundle', 'heads') |
|
784 | @wireprotocommand('unbundle', 'heads') | |
779 | def unbundle(repo, proto, heads): |
|
785 | def unbundle(repo, proto, heads): | |
780 | their_heads = decodelist(heads) |
|
786 | their_heads = decodelist(heads) | |
781 |
|
787 | |||
782 | try: |
|
788 | try: | |
783 | proto.redirect() |
|
789 | proto.redirect() | |
784 |
|
790 | |||
785 | exchange.check_heads(repo, their_heads, 'preparing changes') |
|
791 | exchange.check_heads(repo, their_heads, 'preparing changes') | |
786 |
|
792 | |||
787 | # write bundle data to temporary file because it can be big |
|
793 | # write bundle data to temporary file because it can be big | |
788 | fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-') |
|
794 | fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-') | |
789 | fp = os.fdopen(fd, 'wb+') |
|
795 | fp = os.fdopen(fd, 'wb+') | |
790 | r = 0 |
|
796 | r = 0 | |
791 | try: |
|
797 | try: | |
792 | proto.getfile(fp) |
|
798 | proto.getfile(fp) | |
793 | fp.seek(0) |
|
799 | fp.seek(0) | |
794 | gen = exchange.readbundle(repo.ui, fp, None) |
|
800 | gen = exchange.readbundle(repo.ui, fp, None) | |
795 | r = exchange.unbundle(repo, gen, their_heads, 'serve', |
|
801 | r = exchange.unbundle(repo, gen, their_heads, 'serve', | |
796 | proto._client()) |
|
802 | proto._client()) | |
797 | if util.safehasattr(r, 'addpart'): |
|
803 | if util.safehasattr(r, 'addpart'): | |
798 | # The return looks streameable, we are in the bundle2 case and |
|
804 | # The return looks streameable, we are in the bundle2 case and | |
799 | # should return a stream. |
|
805 | # should return a stream. | |
800 | return streamres(r.getchunks()) |
|
806 | return streamres(r.getchunks()) | |
801 | return pushres(r) |
|
807 | return pushres(r) | |
802 |
|
808 | |||
803 | finally: |
|
809 | finally: | |
804 | fp.close() |
|
810 | fp.close() | |
805 | os.unlink(tempname) |
|
811 | os.unlink(tempname) | |
806 | except bundle2.UnknownPartError, exc: |
|
812 | except bundle2.UnknownPartError, exc: | |
807 | bundler = bundle2.bundle20(repo.ui) |
|
813 | bundler = bundle2.bundle20(repo.ui) | |
808 | part = bundle2.bundlepart('B2X:ERROR:UNKNOWNPART', |
|
814 | part = bundle2.bundlepart('B2X:ERROR:UNKNOWNPART', | |
809 | [('parttype', str(exc))]) |
|
815 | [('parttype', str(exc))]) | |
810 | bundler.addpart(part) |
|
816 | bundler.addpart(part) | |
811 | return streamres(bundler.getchunks()) |
|
817 | return streamres(bundler.getchunks()) | |
812 | except util.Abort, inst: |
|
818 | except util.Abort, inst: | |
813 | # The old code we moved used sys.stderr directly. |
|
819 | # The old code we moved used sys.stderr directly. | |
814 | # We did not change it to minimise code change. |
|
820 | # We did not change it to minimise code change. | |
815 | # This need to be moved to something proper. |
|
821 | # This need to be moved to something proper. | |
816 | # Feel free to do it. |
|
822 | # Feel free to do it. | |
817 | if getattr(inst, 'duringunbundle2', False): |
|
823 | if getattr(inst, 'duringunbundle2', False): | |
818 | bundler = bundle2.bundle20(repo.ui) |
|
824 | bundler = bundle2.bundle20(repo.ui) | |
819 | manargs = [('message', str(inst))] |
|
825 | manargs = [('message', str(inst))] | |
820 | advargs = [] |
|
826 | advargs = [] | |
821 | if inst.hint is not None: |
|
827 | if inst.hint is not None: | |
822 | advargs.append(('hint', inst.hint)) |
|
828 | advargs.append(('hint', inst.hint)) | |
823 | bundler.addpart(bundle2.bundlepart('B2X:ERROR:ABORT', |
|
829 | bundler.addpart(bundle2.bundlepart('B2X:ERROR:ABORT', | |
824 | manargs, advargs)) |
|
830 | manargs, advargs)) | |
825 | return streamres(bundler.getchunks()) |
|
831 | return streamres(bundler.getchunks()) | |
826 | else: |
|
832 | else: | |
827 | sys.stderr.write("abort: %s\n" % inst) |
|
833 | sys.stderr.write("abort: %s\n" % inst) | |
828 | return pushres(0) |
|
834 | return pushres(0) | |
829 | except error.PushRaced, exc: |
|
835 | except error.PushRaced, exc: | |
830 | if getattr(exc, 'duringunbundle2', False): |
|
836 | if getattr(exc, 'duringunbundle2', False): | |
831 | bundler = bundle2.bundle20(repo.ui) |
|
837 | bundler = bundle2.bundle20(repo.ui) | |
832 | part = bundle2.bundlepart('B2X:ERROR:PUSHRACED', |
|
838 | part = bundle2.bundlepart('B2X:ERROR:PUSHRACED', | |
833 | [('message', str(exc))]) |
|
839 | [('message', str(exc))]) | |
834 | bundler.addpart(part) |
|
840 | bundler.addpart(part) | |
835 | return streamres(bundler.getchunks()) |
|
841 | return streamres(bundler.getchunks()) | |
836 | else: |
|
842 | else: | |
837 | return pusherr(str(exc)) |
|
843 | return pusherr(str(exc)) |
General Comments 0
You need to be logged in to leave comments.
Login now