Show More
@@ -1,656 +1,653 b'' | |||
|
1 | 1 | # wireproto.py - generic wire protocol support functions |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2010 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import urllib, tempfile, os, sys |
|
9 | 9 | from i18n import _ |
|
10 | 10 | from node import bin, hex |
|
11 | 11 | import changegroup as changegroupmod |
|
12 | 12 | import peer, error, encoding, util, store |
|
13 | import discovery, phases | |
|
14 | 13 | |
|
15 | 14 | # abstract batching support |
|
16 | 15 | |
|
17 | 16 | class future(object): |
|
18 | 17 | '''placeholder for a value to be set later''' |
|
19 | 18 | def set(self, value): |
|
20 | 19 | if util.safehasattr(self, 'value'): |
|
21 | 20 | raise error.RepoError("future is already set") |
|
22 | 21 | self.value = value |
|
23 | 22 | |
|
24 | 23 | class batcher(object): |
|
25 | 24 | '''base class for batches of commands submittable in a single request |
|
26 | 25 | |
|
27 | 26 | All methods invoked on instances of this class are simply queued and |
|
28 | 27 | return a a future for the result. Once you call submit(), all the queued |
|
29 | 28 | calls are performed and the results set in their respective futures. |
|
30 | 29 | ''' |
|
31 | 30 | def __init__(self): |
|
32 | 31 | self.calls = [] |
|
33 | 32 | def __getattr__(self, name): |
|
34 | 33 | def call(*args, **opts): |
|
35 | 34 | resref = future() |
|
36 | 35 | self.calls.append((name, args, opts, resref,)) |
|
37 | 36 | return resref |
|
38 | 37 | return call |
|
39 | 38 | def submit(self): |
|
40 | 39 | pass |
|
41 | 40 | |
|
42 | 41 | class localbatch(batcher): |
|
43 | 42 | '''performs the queued calls directly''' |
|
44 | 43 | def __init__(self, local): |
|
45 | 44 | batcher.__init__(self) |
|
46 | 45 | self.local = local |
|
47 | 46 | def submit(self): |
|
48 | 47 | for name, args, opts, resref in self.calls: |
|
49 | 48 | resref.set(getattr(self.local, name)(*args, **opts)) |
|
50 | 49 | |
|
51 | 50 | class remotebatch(batcher): |
|
52 | 51 | '''batches the queued calls; uses as few roundtrips as possible''' |
|
53 | 52 | def __init__(self, remote): |
|
54 | 53 | '''remote must support _submitbatch(encbatch) and |
|
55 | 54 | _submitone(op, encargs)''' |
|
56 | 55 | batcher.__init__(self) |
|
57 | 56 | self.remote = remote |
|
58 | 57 | def submit(self): |
|
59 | 58 | req, rsp = [], [] |
|
60 | 59 | for name, args, opts, resref in self.calls: |
|
61 | 60 | mtd = getattr(self.remote, name) |
|
62 | 61 | batchablefn = getattr(mtd, 'batchable', None) |
|
63 | 62 | if batchablefn is not None: |
|
64 | 63 | batchable = batchablefn(mtd.im_self, *args, **opts) |
|
65 | 64 | encargsorres, encresref = batchable.next() |
|
66 | 65 | if encresref: |
|
67 | 66 | req.append((name, encargsorres,)) |
|
68 | 67 | rsp.append((batchable, encresref, resref,)) |
|
69 | 68 | else: |
|
70 | 69 | resref.set(encargsorres) |
|
71 | 70 | else: |
|
72 | 71 | if req: |
|
73 | 72 | self._submitreq(req, rsp) |
|
74 | 73 | req, rsp = [], [] |
|
75 | 74 | resref.set(mtd(*args, **opts)) |
|
76 | 75 | if req: |
|
77 | 76 | self._submitreq(req, rsp) |
|
78 | 77 | def _submitreq(self, req, rsp): |
|
79 | 78 | encresults = self.remote._submitbatch(req) |
|
80 | 79 | for encres, r in zip(encresults, rsp): |
|
81 | 80 | batchable, encresref, resref = r |
|
82 | 81 | encresref.set(encres) |
|
83 | 82 | resref.set(batchable.next()) |
|
84 | 83 | |
|
85 | 84 | def batchable(f): |
|
86 | 85 | '''annotation for batchable methods |
|
87 | 86 | |
|
88 | 87 | Such methods must implement a coroutine as follows: |
|
89 | 88 | |
|
90 | 89 | @batchable |
|
91 | 90 | def sample(self, one, two=None): |
|
92 | 91 | # Handle locally computable results first: |
|
93 | 92 | if not one: |
|
94 | 93 | yield "a local result", None |
|
95 | 94 | # Build list of encoded arguments suitable for your wire protocol: |
|
96 | 95 | encargs = [('one', encode(one),), ('two', encode(two),)] |
|
97 | 96 | # Create future for injection of encoded result: |
|
98 | 97 | encresref = future() |
|
99 | 98 | # Return encoded arguments and future: |
|
100 | 99 | yield encargs, encresref |
|
101 | 100 | # Assuming the future to be filled with the result from the batched |
|
102 | 101 | # request now. Decode it: |
|
103 | 102 | yield decode(encresref.value) |
|
104 | 103 | |
|
105 | 104 | The decorator returns a function which wraps this coroutine as a plain |
|
106 | 105 | method, but adds the original method as an attribute called "batchable", |
|
107 | 106 | which is used by remotebatch to split the call into separate encoding and |
|
108 | 107 | decoding phases. |
|
109 | 108 | ''' |
|
110 | 109 | def plain(*args, **opts): |
|
111 | 110 | batchable = f(*args, **opts) |
|
112 | 111 | encargsorres, encresref = batchable.next() |
|
113 | 112 | if not encresref: |
|
114 | 113 | return encargsorres # a local result in this case |
|
115 | 114 | self = args[0] |
|
116 | 115 | encresref.set(self._submitone(f.func_name, encargsorres)) |
|
117 | 116 | return batchable.next() |
|
118 | 117 | setattr(plain, 'batchable', f) |
|
119 | 118 | return plain |
|
120 | 119 | |
|
121 | 120 | # list of nodes encoding / decoding |
|
122 | 121 | |
|
123 | 122 | def decodelist(l, sep=' '): |
|
124 | 123 | if l: |
|
125 | 124 | return map(bin, l.split(sep)) |
|
126 | 125 | return [] |
|
127 | 126 | |
|
128 | 127 | def encodelist(l, sep=' '): |
|
129 | 128 | return sep.join(map(hex, l)) |
|
130 | 129 | |
|
131 | 130 | # batched call argument encoding |
|
132 | 131 | |
|
133 | 132 | def escapearg(plain): |
|
134 | 133 | return (plain |
|
135 | 134 | .replace(':', '::') |
|
136 | 135 | .replace(',', ':,') |
|
137 | 136 | .replace(';', ':;') |
|
138 | 137 | .replace('=', ':=')) |
|
139 | 138 | |
|
140 | 139 | def unescapearg(escaped): |
|
141 | 140 | return (escaped |
|
142 | 141 | .replace(':=', '=') |
|
143 | 142 | .replace(':;', ';') |
|
144 | 143 | .replace(':,', ',') |
|
145 | 144 | .replace('::', ':')) |
|
146 | 145 | |
|
147 | 146 | # client side |
|
148 | 147 | |
|
149 | 148 | def todict(**args): |
|
150 | 149 | return args |
|
151 | 150 | |
|
152 | 151 | class wirepeer(peer.peerrepository): |
|
153 | 152 | |
|
154 | 153 | def batch(self): |
|
155 | 154 | return remotebatch(self) |
|
156 | 155 | def _submitbatch(self, req): |
|
157 | 156 | cmds = [] |
|
158 | 157 | for op, argsdict in req: |
|
159 | 158 | args = ','.join('%s=%s' % p for p in argsdict.iteritems()) |
|
160 | 159 | cmds.append('%s %s' % (op, args)) |
|
161 | 160 | rsp = self._call("batch", cmds=';'.join(cmds)) |
|
162 | 161 | return rsp.split(';') |
|
163 | 162 | def _submitone(self, op, args): |
|
164 | 163 | return self._call(op, **args) |
|
165 | 164 | |
|
166 | 165 | @batchable |
|
167 | 166 | def lookup(self, key): |
|
168 | 167 | self.requirecap('lookup', _('look up remote revision')) |
|
169 | 168 | f = future() |
|
170 | 169 | yield todict(key=encoding.fromlocal(key)), f |
|
171 | 170 | d = f.value |
|
172 | 171 | success, data = d[:-1].split(" ", 1) |
|
173 | 172 | if int(success): |
|
174 | 173 | yield bin(data) |
|
175 | 174 | self._abort(error.RepoError(data)) |
|
176 | 175 | |
|
177 | 176 | @batchable |
|
178 | 177 | def heads(self): |
|
179 | 178 | f = future() |
|
180 | 179 | yield {}, f |
|
181 | 180 | d = f.value |
|
182 | 181 | try: |
|
183 | 182 | yield decodelist(d[:-1]) |
|
184 | 183 | except ValueError: |
|
185 | 184 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
186 | 185 | |
|
187 | 186 | @batchable |
|
188 | 187 | def known(self, nodes): |
|
189 | 188 | f = future() |
|
190 | 189 | yield todict(nodes=encodelist(nodes)), f |
|
191 | 190 | d = f.value |
|
192 | 191 | try: |
|
193 | 192 | yield [bool(int(f)) for f in d] |
|
194 | 193 | except ValueError: |
|
195 | 194 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
196 | 195 | |
|
197 | 196 | @batchable |
|
198 | 197 | def branchmap(self): |
|
199 | 198 | f = future() |
|
200 | 199 | yield {}, f |
|
201 | 200 | d = f.value |
|
202 | 201 | try: |
|
203 | 202 | branchmap = {} |
|
204 | 203 | for branchpart in d.splitlines(): |
|
205 | 204 | branchname, branchheads = branchpart.split(' ', 1) |
|
206 | 205 | branchname = encoding.tolocal(urllib.unquote(branchname)) |
|
207 | 206 | branchheads = decodelist(branchheads) |
|
208 | 207 | branchmap[branchname] = branchheads |
|
209 | 208 | yield branchmap |
|
210 | 209 | except TypeError: |
|
211 | 210 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
212 | 211 | |
|
213 | 212 | def branches(self, nodes): |
|
214 | 213 | n = encodelist(nodes) |
|
215 | 214 | d = self._call("branches", nodes=n) |
|
216 | 215 | try: |
|
217 | 216 | br = [tuple(decodelist(b)) for b in d.splitlines()] |
|
218 | 217 | return br |
|
219 | 218 | except ValueError: |
|
220 | 219 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
221 | 220 | |
|
222 | 221 | def between(self, pairs): |
|
223 | 222 | batch = 8 # avoid giant requests |
|
224 | 223 | r = [] |
|
225 | 224 | for i in xrange(0, len(pairs), batch): |
|
226 | 225 | n = " ".join([encodelist(p, '-') for p in pairs[i:i + batch]]) |
|
227 | 226 | d = self._call("between", pairs=n) |
|
228 | 227 | try: |
|
229 | 228 | r.extend(l and decodelist(l) or [] for l in d.splitlines()) |
|
230 | 229 | except ValueError: |
|
231 | 230 | self._abort(error.ResponseError(_("unexpected response:"), d)) |
|
232 | 231 | return r |
|
233 | 232 | |
|
234 | 233 | @batchable |
|
235 | 234 | def pushkey(self, namespace, key, old, new): |
|
236 | 235 | if not self.capable('pushkey'): |
|
237 | 236 | yield False, None |
|
238 | 237 | f = future() |
|
239 | 238 | self.ui.debug('preparing pushkey for "%s:%s"\n' % (namespace, key)) |
|
240 | 239 | yield todict(namespace=encoding.fromlocal(namespace), |
|
241 | 240 | key=encoding.fromlocal(key), |
|
242 | 241 | old=encoding.fromlocal(old), |
|
243 | 242 | new=encoding.fromlocal(new)), f |
|
244 | 243 | d = f.value |
|
245 | 244 | d, output = d.split('\n', 1) |
|
246 | 245 | try: |
|
247 | 246 | d = bool(int(d)) |
|
248 | 247 | except ValueError: |
|
249 | 248 | raise error.ResponseError( |
|
250 | 249 | _('push failed (unexpected response):'), d) |
|
251 | 250 | for l in output.splitlines(True): |
|
252 | 251 | self.ui.status(_('remote: '), l) |
|
253 | 252 | yield d |
|
254 | 253 | |
|
255 | 254 | @batchable |
|
256 | 255 | def listkeys(self, namespace): |
|
257 | 256 | if not self.capable('pushkey'): |
|
258 | 257 | yield {}, None |
|
259 | 258 | f = future() |
|
260 | 259 | self.ui.debug('preparing listkeys for "%s"\n' % namespace) |
|
261 | 260 | yield todict(namespace=encoding.fromlocal(namespace)), f |
|
262 | 261 | d = f.value |
|
263 | 262 | r = {} |
|
264 | 263 | for l in d.splitlines(): |
|
265 | 264 | k, v = l.split('\t') |
|
266 | 265 | r[encoding.tolocal(k)] = encoding.tolocal(v) |
|
267 | 266 | yield r |
|
268 | 267 | |
|
269 | 268 | def stream_out(self): |
|
270 | 269 | return self._callstream('stream_out') |
|
271 | 270 | |
|
272 | 271 | def changegroup(self, nodes, kind): |
|
273 | 272 | n = encodelist(nodes) |
|
274 | 273 | f = self._callstream("changegroup", roots=n) |
|
275 | 274 | return changegroupmod.unbundle10(self._decompress(f), 'UN') |
|
276 | 275 | |
|
277 | 276 | def changegroupsubset(self, bases, heads, kind): |
|
278 | 277 | self.requirecap('changegroupsubset', _('look up remote changes')) |
|
279 | 278 | bases = encodelist(bases) |
|
280 | 279 | heads = encodelist(heads) |
|
281 | 280 | f = self._callstream("changegroupsubset", |
|
282 | 281 | bases=bases, heads=heads) |
|
283 | 282 | return changegroupmod.unbundle10(self._decompress(f), 'UN') |
|
284 | 283 | |
|
285 | 284 | def getbundle(self, source, heads=None, common=None): |
|
286 | 285 | self.requirecap('getbundle', _('look up remote changes')) |
|
287 | 286 | opts = {} |
|
288 | 287 | if heads is not None: |
|
289 | 288 | opts['heads'] = encodelist(heads) |
|
290 | 289 | if common is not None: |
|
291 | 290 | opts['common'] = encodelist(common) |
|
292 | 291 | f = self._callstream("getbundle", **opts) |
|
293 | 292 | return changegroupmod.unbundle10(self._decompress(f), 'UN') |
|
294 | 293 | |
|
295 | 294 | def unbundle(self, cg, heads, source): |
|
296 | 295 | '''Send cg (a readable file-like object representing the |
|
297 | 296 | changegroup to push, typically a chunkbuffer object) to the |
|
298 | 297 | remote server as a bundle. Return an integer indicating the |
|
299 | 298 | result of the push (see localrepository.addchangegroup()).''' |
|
300 | 299 | |
|
301 | 300 | if heads != ['force'] and self.capable('unbundlehash'): |
|
302 | 301 | heads = encodelist(['hashed', |
|
303 | 302 | util.sha1(''.join(sorted(heads))).digest()]) |
|
304 | 303 | else: |
|
305 | 304 | heads = encodelist(heads) |
|
306 | 305 | |
|
307 | 306 | ret, output = self._callpush("unbundle", cg, heads=heads) |
|
308 | 307 | if ret == "": |
|
309 | 308 | raise error.ResponseError( |
|
310 | 309 | _('push failed:'), output) |
|
311 | 310 | try: |
|
312 | 311 | ret = int(ret) |
|
313 | 312 | except ValueError: |
|
314 | 313 | raise error.ResponseError( |
|
315 | 314 | _('push failed (unexpected response):'), ret) |
|
316 | 315 | |
|
317 | 316 | for l in output.splitlines(True): |
|
318 | 317 | self.ui.status(_('remote: '), l) |
|
319 | 318 | return ret |
|
320 | 319 | |
|
321 | 320 | def debugwireargs(self, one, two, three=None, four=None, five=None): |
|
322 | 321 | # don't pass optional arguments left at their default value |
|
323 | 322 | opts = {} |
|
324 | 323 | if three is not None: |
|
325 | 324 | opts['three'] = three |
|
326 | 325 | if four is not None: |
|
327 | 326 | opts['four'] = four |
|
328 | 327 | return self._call('debugwireargs', one=one, two=two, **opts) |
|
329 | 328 | |
|
330 | 329 | # server side |
|
331 | 330 | |
|
332 | 331 | class streamres(object): |
|
333 | 332 | def __init__(self, gen): |
|
334 | 333 | self.gen = gen |
|
335 | 334 | |
|
336 | 335 | class pushres(object): |
|
337 | 336 | def __init__(self, res): |
|
338 | 337 | self.res = res |
|
339 | 338 | |
|
340 | 339 | class pusherr(object): |
|
341 | 340 | def __init__(self, res): |
|
342 | 341 | self.res = res |
|
343 | 342 | |
|
344 | 343 | class ooberror(object): |
|
345 | 344 | def __init__(self, message): |
|
346 | 345 | self.message = message |
|
347 | 346 | |
|
348 | 347 | def dispatch(repo, proto, command): |
|
349 | 348 | repo = repo.filtered("unserved") |
|
350 | 349 | func, spec = commands[command] |
|
351 | 350 | args = proto.getargs(spec) |
|
352 | 351 | return func(repo, proto, *args) |
|
353 | 352 | |
|
354 | 353 | def options(cmd, keys, others): |
|
355 | 354 | opts = {} |
|
356 | 355 | for k in keys: |
|
357 | 356 | if k in others: |
|
358 | 357 | opts[k] = others[k] |
|
359 | 358 | del others[k] |
|
360 | 359 | if others: |
|
361 | 360 | sys.stderr.write("abort: %s got unexpected arguments %s\n" |
|
362 | 361 | % (cmd, ",".join(others))) |
|
363 | 362 | return opts |
|
364 | 363 | |
|
365 | 364 | def batch(repo, proto, cmds, others): |
|
366 | 365 | repo = repo.filtered("unserved") |
|
367 | 366 | res = [] |
|
368 | 367 | for pair in cmds.split(';'): |
|
369 | 368 | op, args = pair.split(' ', 1) |
|
370 | 369 | vals = {} |
|
371 | 370 | for a in args.split(','): |
|
372 | 371 | if a: |
|
373 | 372 | n, v = a.split('=') |
|
374 | 373 | vals[n] = unescapearg(v) |
|
375 | 374 | func, spec = commands[op] |
|
376 | 375 | if spec: |
|
377 | 376 | keys = spec.split() |
|
378 | 377 | data = {} |
|
379 | 378 | for k in keys: |
|
380 | 379 | if k == '*': |
|
381 | 380 | star = {} |
|
382 | 381 | for key in vals.keys(): |
|
383 | 382 | if key not in keys: |
|
384 | 383 | star[key] = vals[key] |
|
385 | 384 | data['*'] = star |
|
386 | 385 | else: |
|
387 | 386 | data[k] = vals[k] |
|
388 | 387 | result = func(repo, proto, *[data[k] for k in keys]) |
|
389 | 388 | else: |
|
390 | 389 | result = func(repo, proto) |
|
391 | 390 | if isinstance(result, ooberror): |
|
392 | 391 | return result |
|
393 | 392 | res.append(escapearg(result)) |
|
394 | 393 | return ';'.join(res) |
|
395 | 394 | |
|
396 | 395 | def between(repo, proto, pairs): |
|
397 | 396 | pairs = [decodelist(p, '-') for p in pairs.split(" ")] |
|
398 | 397 | r = [] |
|
399 | 398 | for b in repo.between(pairs): |
|
400 | 399 | r.append(encodelist(b) + "\n") |
|
401 | 400 | return "".join(r) |
|
402 | 401 | |
|
403 | 402 | def branchmap(repo, proto): |
|
404 |
branchmap = |
|
|
403 | branchmap = repo.branchmap() | |
|
405 | 404 | heads = [] |
|
406 | 405 | for branch, nodes in branchmap.iteritems(): |
|
407 | 406 | branchname = urllib.quote(encoding.fromlocal(branch)) |
|
408 | 407 | branchnodes = encodelist(nodes) |
|
409 | 408 | heads.append('%s %s' % (branchname, branchnodes)) |
|
410 | 409 | return '\n'.join(heads) |
|
411 | 410 | |
|
412 | 411 | def branches(repo, proto, nodes): |
|
413 | 412 | nodes = decodelist(nodes) |
|
414 | 413 | r = [] |
|
415 | 414 | for b in repo.branches(nodes): |
|
416 | 415 | r.append(encodelist(b) + "\n") |
|
417 | 416 | return "".join(r) |
|
418 | 417 | |
|
419 | 418 | def capabilities(repo, proto): |
|
420 | 419 | caps = ('lookup changegroupsubset branchmap pushkey known getbundle ' |
|
421 | 420 | 'unbundlehash batch').split() |
|
422 | 421 | if _allowstream(repo.ui): |
|
423 | 422 | if repo.ui.configbool('server', 'preferuncompressed', False): |
|
424 | 423 | caps.append('stream-preferred') |
|
425 | 424 | requiredformats = repo.requirements & repo.supportedformats |
|
426 | 425 | # if our local revlogs are just revlogv1, add 'stream' cap |
|
427 | 426 | if not requiredformats - set(('revlogv1',)): |
|
428 | 427 | caps.append('stream') |
|
429 | 428 | # otherwise, add 'streamreqs' detailing our local revlog format |
|
430 | 429 | else: |
|
431 | 430 | caps.append('streamreqs=%s' % ','.join(requiredformats)) |
|
432 | 431 | caps.append('unbundle=%s' % ','.join(changegroupmod.bundlepriority)) |
|
433 | 432 | caps.append('httpheader=1024') |
|
434 | 433 | return ' '.join(caps) |
|
435 | 434 | |
|
436 | 435 | def changegroup(repo, proto, roots): |
|
437 | 436 | nodes = decodelist(roots) |
|
438 | 437 | cg = repo.changegroup(nodes, 'serve') |
|
439 | 438 | return streamres(proto.groupchunks(cg)) |
|
440 | 439 | |
|
441 | 440 | def changegroupsubset(repo, proto, bases, heads): |
|
442 | 441 | bases = decodelist(bases) |
|
443 | 442 | heads = decodelist(heads) |
|
444 | 443 | cg = repo.changegroupsubset(bases, heads, 'serve') |
|
445 | 444 | return streamres(proto.groupchunks(cg)) |
|
446 | 445 | |
|
447 | 446 | def debugwireargs(repo, proto, one, two, others): |
|
448 | 447 | # only accept optional args from the known set |
|
449 | 448 | opts = options('debugwireargs', ['three', 'four'], others) |
|
450 | 449 | return repo.debugwireargs(one, two, **opts) |
|
451 | 450 | |
|
452 | 451 | def getbundle(repo, proto, others): |
|
453 | 452 | opts = options('getbundle', ['heads', 'common'], others) |
|
454 | 453 | for k, v in opts.iteritems(): |
|
455 | 454 | opts[k] = decodelist(v) |
|
456 | 455 | cg = repo.getbundle('serve', **opts) |
|
457 | 456 | return streamres(proto.groupchunks(cg)) |
|
458 | 457 | |
|
459 | 458 | def heads(repo, proto): |
|
460 |
h = |
|
|
459 | h = repo.heads() | |
|
461 | 460 | return encodelist(h) + "\n" |
|
462 | 461 | |
|
463 | 462 | def hello(repo, proto): |
|
464 | 463 | '''the hello command returns a set of lines describing various |
|
465 | 464 | interesting things about the server, in an RFC822-like format. |
|
466 | 465 | Currently the only one defined is "capabilities", which |
|
467 | 466 | consists of a line in the form: |
|
468 | 467 | |
|
469 | 468 | capabilities: space separated list of tokens |
|
470 | 469 | ''' |
|
471 | 470 | return "capabilities: %s\n" % (capabilities(repo, proto)) |
|
472 | 471 | |
|
473 | 472 | def listkeys(repo, proto, namespace): |
|
474 | 473 | d = repo.listkeys(encoding.tolocal(namespace)).items() |
|
475 | 474 | t = '\n'.join(['%s\t%s' % (encoding.fromlocal(k), encoding.fromlocal(v)) |
|
476 | 475 | for k, v in d]) |
|
477 | 476 | return t |
|
478 | 477 | |
|
479 | 478 | def lookup(repo, proto, key): |
|
480 | 479 | try: |
|
481 | 480 | k = encoding.tolocal(key) |
|
482 | 481 | c = repo[k] |
|
483 | if c.phase() == phases.secret: | |
|
484 | raise error.RepoLookupError(_("unknown revision '%s'") % k) | |
|
485 | 482 | r = c.hex() |
|
486 | 483 | success = 1 |
|
487 | 484 | except Exception, inst: |
|
488 | 485 | r = str(inst) |
|
489 | 486 | success = 0 |
|
490 | 487 | return "%s %s\n" % (success, r) |
|
491 | 488 | |
|
492 | 489 | def known(repo, proto, nodes, others): |
|
493 | 490 | return ''.join(b and "1" or "0" for b in repo.known(decodelist(nodes))) |
|
494 | 491 | |
|
495 | 492 | def pushkey(repo, proto, namespace, key, old, new): |
|
496 | 493 | # compatibility with pre-1.8 clients which were accidentally |
|
497 | 494 | # sending raw binary nodes rather than utf-8-encoded hex |
|
498 | 495 | if len(new) == 20 and new.encode('string-escape') != new: |
|
499 | 496 | # looks like it could be a binary node |
|
500 | 497 | try: |
|
501 | 498 | new.decode('utf-8') |
|
502 | 499 | new = encoding.tolocal(new) # but cleanly decodes as UTF-8 |
|
503 | 500 | except UnicodeDecodeError: |
|
504 | 501 | pass # binary, leave unmodified |
|
505 | 502 | else: |
|
506 | 503 | new = encoding.tolocal(new) # normal path |
|
507 | 504 | |
|
508 | 505 | if util.safehasattr(proto, 'restore'): |
|
509 | 506 | |
|
510 | 507 | proto.redirect() |
|
511 | 508 | |
|
512 | 509 | try: |
|
513 | 510 | r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key), |
|
514 | 511 | encoding.tolocal(old), new) or False |
|
515 | 512 | except util.Abort: |
|
516 | 513 | r = False |
|
517 | 514 | |
|
518 | 515 | output = proto.restore() |
|
519 | 516 | |
|
520 | 517 | return '%s\n%s' % (int(r), output) |
|
521 | 518 | |
|
522 | 519 | r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key), |
|
523 | 520 | encoding.tolocal(old), new) |
|
524 | 521 | return '%s\n' % int(r) |
|
525 | 522 | |
|
526 | 523 | def _allowstream(ui): |
|
527 | 524 | return ui.configbool('server', 'uncompressed', True, untrusted=True) |
|
528 | 525 | |
|
529 | 526 | def stream(repo, proto): |
|
530 | 527 | '''If the server supports streaming clone, it advertises the "stream" |
|
531 | 528 | capability with a value representing the version and flags of the repo |
|
532 | 529 | it is serving. Client checks to see if it understands the format. |
|
533 | 530 | |
|
534 | 531 | The format is simple: the server writes out a line with the amount |
|
535 | 532 | of files, then the total amount of bytes to be transferred (separated |
|
536 | 533 | by a space). Then, for each file, the server first writes the filename |
|
537 | 534 | and filesize (separated by the null character), then the file contents. |
|
538 | 535 | ''' |
|
539 | 536 | |
|
540 | 537 | if not _allowstream(repo.ui): |
|
541 | 538 | return '1\n' |
|
542 | 539 | |
|
543 | 540 | entries = [] |
|
544 | 541 | total_bytes = 0 |
|
545 | 542 | try: |
|
546 | 543 | # get consistent snapshot of repo, lock during scan |
|
547 | 544 | lock = repo.lock() |
|
548 | 545 | try: |
|
549 | 546 | repo.ui.debug('scanning\n') |
|
550 | 547 | for name, ename, size in repo.store.walk(): |
|
551 | 548 | entries.append((name, size)) |
|
552 | 549 | total_bytes += size |
|
553 | 550 | finally: |
|
554 | 551 | lock.release() |
|
555 | 552 | except error.LockError: |
|
556 | 553 | return '2\n' # error: 2 |
|
557 | 554 | |
|
558 | 555 | def streamer(repo, entries, total): |
|
559 | 556 | '''stream out all metadata files in repository.''' |
|
560 | 557 | yield '0\n' # success |
|
561 | 558 | repo.ui.debug('%d files, %d bytes to transfer\n' % |
|
562 | 559 | (len(entries), total_bytes)) |
|
563 | 560 | yield '%d %d\n' % (len(entries), total_bytes) |
|
564 | 561 | |
|
565 | 562 | sopener = repo.sopener |
|
566 | 563 | oldaudit = sopener.mustaudit |
|
567 | 564 | debugflag = repo.ui.debugflag |
|
568 | 565 | sopener.mustaudit = False |
|
569 | 566 | |
|
570 | 567 | try: |
|
571 | 568 | for name, size in entries: |
|
572 | 569 | if debugflag: |
|
573 | 570 | repo.ui.debug('sending %s (%d bytes)\n' % (name, size)) |
|
574 | 571 | # partially encode name over the wire for backwards compat |
|
575 | 572 | yield '%s\0%d\n' % (store.encodedir(name), size) |
|
576 | 573 | if size <= 65536: |
|
577 | 574 | fp = sopener(name) |
|
578 | 575 | try: |
|
579 | 576 | data = fp.read(size) |
|
580 | 577 | finally: |
|
581 | 578 | fp.close() |
|
582 | 579 | yield data |
|
583 | 580 | else: |
|
584 | 581 | for chunk in util.filechunkiter(sopener(name), limit=size): |
|
585 | 582 | yield chunk |
|
586 | 583 | # replace with "finally:" when support for python 2.4 has been dropped |
|
587 | 584 | except Exception: |
|
588 | 585 | sopener.mustaudit = oldaudit |
|
589 | 586 | raise |
|
590 | 587 | sopener.mustaudit = oldaudit |
|
591 | 588 | |
|
592 | 589 | return streamres(streamer(repo, entries, total_bytes)) |
|
593 | 590 | |
|
594 | 591 | def unbundle(repo, proto, heads): |
|
595 | 592 | their_heads = decodelist(heads) |
|
596 | 593 | |
|
597 | 594 | def check_heads(): |
|
598 |
heads = |
|
|
595 | heads = repo.heads() | |
|
599 | 596 | heads_hash = util.sha1(''.join(sorted(heads))).digest() |
|
600 | 597 | return (their_heads == ['force'] or their_heads == heads or |
|
601 | 598 | their_heads == ['hashed', heads_hash]) |
|
602 | 599 | |
|
603 | 600 | proto.redirect() |
|
604 | 601 | |
|
605 | 602 | # fail early if possible |
|
606 | 603 | if not check_heads(): |
|
607 | 604 | return pusherr('unsynced changes') |
|
608 | 605 | |
|
609 | 606 | # write bundle data to temporary file because it can be big |
|
610 | 607 | fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-') |
|
611 | 608 | fp = os.fdopen(fd, 'wb+') |
|
612 | 609 | r = 0 |
|
613 | 610 | try: |
|
614 | 611 | proto.getfile(fp) |
|
615 | 612 | lock = repo.lock() |
|
616 | 613 | try: |
|
617 | 614 | if not check_heads(): |
|
618 | 615 | # someone else committed/pushed/unbundled while we |
|
619 | 616 | # were transferring data |
|
620 | 617 | return pusherr('unsynced changes') |
|
621 | 618 | |
|
622 | 619 | # push can proceed |
|
623 | 620 | fp.seek(0) |
|
624 | 621 | gen = changegroupmod.readbundle(fp, None) |
|
625 | 622 | |
|
626 | 623 | try: |
|
627 | 624 | r = repo.addchangegroup(gen, 'serve', proto._client()) |
|
628 | 625 | except util.Abort, inst: |
|
629 | 626 | sys.stderr.write("abort: %s\n" % inst) |
|
630 | 627 | finally: |
|
631 | 628 | lock.release() |
|
632 | 629 | return pushres(r) |
|
633 | 630 | |
|
634 | 631 | finally: |
|
635 | 632 | fp.close() |
|
636 | 633 | os.unlink(tempname) |
|
637 | 634 | |
|
638 | 635 | commands = { |
|
639 | 636 | 'batch': (batch, 'cmds *'), |
|
640 | 637 | 'between': (between, 'pairs'), |
|
641 | 638 | 'branchmap': (branchmap, ''), |
|
642 | 639 | 'branches': (branches, 'nodes'), |
|
643 | 640 | 'capabilities': (capabilities, ''), |
|
644 | 641 | 'changegroup': (changegroup, 'roots'), |
|
645 | 642 | 'changegroupsubset': (changegroupsubset, 'bases heads'), |
|
646 | 643 | 'debugwireargs': (debugwireargs, 'one two *'), |
|
647 | 644 | 'getbundle': (getbundle, '*'), |
|
648 | 645 | 'heads': (heads, ''), |
|
649 | 646 | 'hello': (hello, ''), |
|
650 | 647 | 'known': (known, 'nodes *'), |
|
651 | 648 | 'listkeys': (listkeys, 'namespace'), |
|
652 | 649 | 'lookup': (lookup, 'key'), |
|
653 | 650 | 'pushkey': (pushkey, 'namespace key old new'), |
|
654 | 651 | 'stream_out': (stream, ''), |
|
655 | 652 | 'unbundle': (unbundle, 'heads'), |
|
656 | 653 | } |
General Comments 0
You need to be logged in to leave comments.
Login now