##// END OF EJS Templates
peer: introduce a limitedarguments attributes...
marmoute -
r42334:69921d02 default
parent child Browse files
Show More
@@ -1,1006 +1,1010
1 1 # httppeer.py - HTTP repository proxy classes for mercurial
2 2 #
3 3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 from __future__ import absolute_import
10 10
11 11 import errno
12 12 import io
13 13 import os
14 14 import socket
15 15 import struct
16 16 import weakref
17 17
18 18 from .i18n import _
19 19 from . import (
20 20 bundle2,
21 21 error,
22 22 httpconnection,
23 23 pycompat,
24 24 repository,
25 25 statichttprepo,
26 26 url as urlmod,
27 27 util,
28 28 wireprotoframing,
29 29 wireprototypes,
30 30 wireprotov1peer,
31 31 wireprotov2peer,
32 32 wireprotov2server,
33 33 )
34 34 from .utils import (
35 35 cborutil,
36 36 interfaceutil,
37 37 stringutil,
38 38 )
39 39
40 40 httplib = util.httplib
41 41 urlerr = util.urlerr
42 42 urlreq = util.urlreq
43 43
44 44 def encodevalueinheaders(value, header, limit):
45 45 """Encode a string value into multiple HTTP headers.
46 46
47 47 ``value`` will be encoded into 1 or more HTTP headers with the names
48 48 ``header-<N>`` where ``<N>`` is an integer starting at 1. Each header
49 49 name + value will be at most ``limit`` bytes long.
50 50
51 51 Returns an iterable of 2-tuples consisting of header names and
52 52 values as native strings.
53 53 """
54 54 # HTTP Headers are ASCII. Python 3 requires them to be unicodes,
55 55 # not bytes. This function always takes bytes in as arguments.
56 56 fmt = pycompat.strurl(header) + r'-%s'
57 57 # Note: it is *NOT* a bug that the last bit here is a bytestring
58 58 # and not a unicode: we're just getting the encoded length anyway,
59 59 # and using an r-string to make it portable between Python 2 and 3
60 60 # doesn't work because then the \r is a literal backslash-r
61 61 # instead of a carriage return.
62 62 valuelen = limit - len(fmt % r'000') - len(': \r\n')
63 63 result = []
64 64
65 65 n = 0
66 66 for i in pycompat.xrange(0, len(value), valuelen):
67 67 n += 1
68 68 result.append((fmt % str(n), pycompat.strurl(value[i:i + valuelen])))
69 69
70 70 return result
71 71
72 72 class _multifile(object):
73 73 def __init__(self, *fileobjs):
74 74 for f in fileobjs:
75 75 if not util.safehasattr(f, 'length'):
76 76 raise ValueError(
77 77 '_multifile only supports file objects that '
78 78 'have a length but this one does not:', type(f), f)
79 79 self._fileobjs = fileobjs
80 80 self._index = 0
81 81
82 82 @property
83 83 def length(self):
84 84 return sum(f.length for f in self._fileobjs)
85 85
86 86 def read(self, amt=None):
87 87 if amt <= 0:
88 88 return ''.join(f.read() for f in self._fileobjs)
89 89 parts = []
90 90 while amt and self._index < len(self._fileobjs):
91 91 parts.append(self._fileobjs[self._index].read(amt))
92 92 got = len(parts[-1])
93 93 if got < amt:
94 94 self._index += 1
95 95 amt -= got
96 96 return ''.join(parts)
97 97
98 98 def seek(self, offset, whence=os.SEEK_SET):
99 99 if whence != os.SEEK_SET:
100 100 raise NotImplementedError(
101 101 '_multifile does not support anything other'
102 102 ' than os.SEEK_SET for whence on seek()')
103 103 if offset != 0:
104 104 raise NotImplementedError(
105 105 '_multifile only supports seeking to start, but that '
106 106 'could be fixed if you need it')
107 107 for f in self._fileobjs:
108 108 f.seek(0)
109 109 self._index = 0
110 110
111 111 def makev1commandrequest(ui, requestbuilder, caps, capablefn,
112 112 repobaseurl, cmd, args):
113 113 """Make an HTTP request to run a command for a version 1 client.
114 114
115 115 ``caps`` is a set of known server capabilities. The value may be
116 116 None if capabilities are not yet known.
117 117
118 118 ``capablefn`` is a function to evaluate a capability.
119 119
120 120 ``cmd``, ``args``, and ``data`` define the command, its arguments, and
121 121 raw data to pass to it.
122 122 """
123 123 if cmd == 'pushkey':
124 124 args['data'] = ''
125 125 data = args.pop('data', None)
126 126 headers = args.pop('headers', {})
127 127
128 128 ui.debug("sending %s command\n" % cmd)
129 129 q = [('cmd', cmd)]
130 130 headersize = 0
131 131 # Important: don't use self.capable() here or else you end up
132 132 # with infinite recursion when trying to look up capabilities
133 133 # for the first time.
134 134 postargsok = caps is not None and 'httppostargs' in caps
135 135
136 136 # Send arguments via POST.
137 137 if postargsok and args:
138 138 strargs = urlreq.urlencode(sorted(args.items()))
139 139 if not data:
140 140 data = strargs
141 141 else:
142 142 if isinstance(data, bytes):
143 143 i = io.BytesIO(data)
144 144 i.length = len(data)
145 145 data = i
146 146 argsio = io.BytesIO(strargs)
147 147 argsio.length = len(strargs)
148 148 data = _multifile(argsio, data)
149 149 headers[r'X-HgArgs-Post'] = len(strargs)
150 150 elif args:
151 151 # Calling self.capable() can infinite loop if we are calling
152 152 # "capabilities". But that command should never accept wire
153 153 # protocol arguments. So this should never happen.
154 154 assert cmd != 'capabilities'
155 155 httpheader = capablefn('httpheader')
156 156 if httpheader:
157 157 headersize = int(httpheader.split(',', 1)[0])
158 158
159 159 # Send arguments via HTTP headers.
160 160 if headersize > 0:
161 161 # The headers can typically carry more data than the URL.
162 162 encargs = urlreq.urlencode(sorted(args.items()))
163 163 for header, value in encodevalueinheaders(encargs, 'X-HgArg',
164 164 headersize):
165 165 headers[header] = value
166 166 # Send arguments via query string (Mercurial <1.9).
167 167 else:
168 168 q += sorted(args.items())
169 169
170 170 qs = '?%s' % urlreq.urlencode(q)
171 171 cu = "%s%s" % (repobaseurl, qs)
172 172 size = 0
173 173 if util.safehasattr(data, 'length'):
174 174 size = data.length
175 175 elif data is not None:
176 176 size = len(data)
177 177 if data is not None and r'Content-Type' not in headers:
178 178 headers[r'Content-Type'] = r'application/mercurial-0.1'
179 179
180 180 # Tell the server we accept application/mercurial-0.2 and multiple
181 181 # compression formats if the server is capable of emitting those
182 182 # payloads.
183 183 # Note: Keep this set empty by default, as client advertisement of
184 184 # protocol parameters should only occur after the handshake.
185 185 protoparams = set()
186 186
187 187 mediatypes = set()
188 188 if caps is not None:
189 189 mt = capablefn('httpmediatype')
190 190 if mt:
191 191 protoparams.add('0.1')
192 192 mediatypes = set(mt.split(','))
193 193
194 194 protoparams.add('partial-pull')
195 195
196 196 if '0.2tx' in mediatypes:
197 197 protoparams.add('0.2')
198 198
199 199 if '0.2tx' in mediatypes and capablefn('compression'):
200 200 # We /could/ compare supported compression formats and prune
201 201 # non-mutually supported or error if nothing is mutually supported.
202 202 # For now, send the full list to the server and have it error.
203 203 comps = [e.wireprotosupport().name for e in
204 204 util.compengines.supportedwireengines(util.CLIENTROLE)]
205 205 protoparams.add('comp=%s' % ','.join(comps))
206 206
207 207 if protoparams:
208 208 protoheaders = encodevalueinheaders(' '.join(sorted(protoparams)),
209 209 'X-HgProto',
210 210 headersize or 1024)
211 211 for header, value in protoheaders:
212 212 headers[header] = value
213 213
214 214 varyheaders = []
215 215 for header in headers:
216 216 if header.lower().startswith(r'x-hg'):
217 217 varyheaders.append(header)
218 218
219 219 if varyheaders:
220 220 headers[r'Vary'] = r','.join(sorted(varyheaders))
221 221
222 222 req = requestbuilder(pycompat.strurl(cu), data, headers)
223 223
224 224 if data is not None:
225 225 ui.debug("sending %d bytes\n" % size)
226 226 req.add_unredirected_header(r'Content-Length', r'%d' % size)
227 227
228 228 return req, cu, qs
229 229
230 230 def _reqdata(req):
231 231 """Get request data, if any. If no data, returns None."""
232 232 if pycompat.ispy3:
233 233 return req.data
234 234 if not req.has_data():
235 235 return None
236 236 return req.get_data()
237 237
238 238 def sendrequest(ui, opener, req):
239 239 """Send a prepared HTTP request.
240 240
241 241 Returns the response object.
242 242 """
243 243 dbg = ui.debug
244 244 if (ui.debugflag
245 245 and ui.configbool('devel', 'debug.peer-request')):
246 246 line = 'devel-peer-request: %s\n'
247 247 dbg(line % '%s %s' % (pycompat.bytesurl(req.get_method()),
248 248 pycompat.bytesurl(req.get_full_url())))
249 249 hgargssize = None
250 250
251 251 for header, value in sorted(req.header_items()):
252 252 header = pycompat.bytesurl(header)
253 253 value = pycompat.bytesurl(value)
254 254 if header.startswith('X-hgarg-'):
255 255 if hgargssize is None:
256 256 hgargssize = 0
257 257 hgargssize += len(value)
258 258 else:
259 259 dbg(line % ' %s %s' % (header, value))
260 260
261 261 if hgargssize is not None:
262 262 dbg(line % ' %d bytes of commands arguments in headers'
263 263 % hgargssize)
264 264 data = _reqdata(req)
265 265 if data is not None:
266 266 length = getattr(data, 'length', None)
267 267 if length is None:
268 268 length = len(data)
269 269 dbg(line % ' %d bytes of data' % length)
270 270
271 271 start = util.timer()
272 272
273 273 res = None
274 274 try:
275 275 res = opener.open(req)
276 276 except urlerr.httperror as inst:
277 277 if inst.code == 401:
278 278 raise error.Abort(_('authorization failed'))
279 279 raise
280 280 except httplib.HTTPException as inst:
281 281 ui.debug('http error requesting %s\n' %
282 282 util.hidepassword(req.get_full_url()))
283 283 ui.traceback()
284 284 raise IOError(None, inst)
285 285 finally:
286 286 if ui.debugflag and ui.configbool('devel', 'debug.peer-request'):
287 287 code = res.code if res else -1
288 288 dbg(line % ' finished in %.4f seconds (%d)'
289 289 % (util.timer() - start, code))
290 290
291 291 # Insert error handlers for common I/O failures.
292 292 urlmod.wrapresponse(res)
293 293
294 294 return res
295 295
296 296 class RedirectedRepoError(error.RepoError):
297 297 def __init__(self, msg, respurl):
298 298 super(RedirectedRepoError, self).__init__(msg)
299 299 self.respurl = respurl
300 300
301 301 def parsev1commandresponse(ui, baseurl, requrl, qs, resp, compressible,
302 302 allowcbor=False):
303 303 # record the url we got redirected to
304 304 redirected = False
305 305 respurl = pycompat.bytesurl(resp.geturl())
306 306 if respurl.endswith(qs):
307 307 respurl = respurl[:-len(qs)]
308 308 qsdropped = False
309 309 else:
310 310 qsdropped = True
311 311
312 312 if baseurl.rstrip('/') != respurl.rstrip('/'):
313 313 redirected = True
314 314 if not ui.quiet:
315 315 ui.warn(_('real URL is %s\n') % respurl)
316 316
317 317 try:
318 318 proto = pycompat.bytesurl(resp.getheader(r'content-type', r''))
319 319 except AttributeError:
320 320 proto = pycompat.bytesurl(resp.headers.get(r'content-type', r''))
321 321
322 322 safeurl = util.hidepassword(baseurl)
323 323 if proto.startswith('application/hg-error'):
324 324 raise error.OutOfBandError(resp.read())
325 325
326 326 # Pre 1.0 versions of Mercurial used text/plain and
327 327 # application/hg-changegroup. We don't support such old servers.
328 328 if not proto.startswith('application/mercurial-'):
329 329 ui.debug("requested URL: '%s'\n" % util.hidepassword(requrl))
330 330 msg = _("'%s' does not appear to be an hg repository:\n"
331 331 "---%%<--- (%s)\n%s\n---%%<---\n") % (
332 332 safeurl, proto or 'no content-type', resp.read(1024))
333 333
334 334 # Some servers may strip the query string from the redirect. We
335 335 # raise a special error type so callers can react to this specially.
336 336 if redirected and qsdropped:
337 337 raise RedirectedRepoError(msg, respurl)
338 338 else:
339 339 raise error.RepoError(msg)
340 340
341 341 try:
342 342 subtype = proto.split('-', 1)[1]
343 343
344 344 # Unless we end up supporting CBOR in the legacy wire protocol,
345 345 # this should ONLY be encountered for the initial capabilities
346 346 # request during handshake.
347 347 if subtype == 'cbor':
348 348 if allowcbor:
349 349 return respurl, proto, resp
350 350 else:
351 351 raise error.RepoError(_('unexpected CBOR response from '
352 352 'server'))
353 353
354 354 version_info = tuple([int(n) for n in subtype.split('.')])
355 355 except ValueError:
356 356 raise error.RepoError(_("'%s' sent a broken Content-Type "
357 357 "header (%s)") % (safeurl, proto))
358 358
359 359 # TODO consider switching to a decompression reader that uses
360 360 # generators.
361 361 if version_info == (0, 1):
362 362 if compressible:
363 363 resp = util.compengines['zlib'].decompressorreader(resp)
364 364
365 365 elif version_info == (0, 2):
366 366 # application/mercurial-0.2 always identifies the compression
367 367 # engine in the payload header.
368 368 elen = struct.unpack('B', util.readexactly(resp, 1))[0]
369 369 ename = util.readexactly(resp, elen)
370 370 engine = util.compengines.forwiretype(ename)
371 371
372 372 resp = engine.decompressorreader(resp)
373 373 else:
374 374 raise error.RepoError(_("'%s' uses newer protocol %s") %
375 375 (safeurl, subtype))
376 376
377 377 return respurl, proto, resp
378 378
379 379 class httppeer(wireprotov1peer.wirepeer):
380 380 def __init__(self, ui, path, url, opener, requestbuilder, caps):
381 381 self.ui = ui
382 382 self._path = path
383 383 self._url = url
384 384 self._caps = caps
385 self.limitedarguments = caps is not None and 'httppostargs' not in caps
385 386 self._urlopener = opener
386 387 self._requestbuilder = requestbuilder
387 388
388 389 def __del__(self):
389 390 for h in self._urlopener.handlers:
390 391 h.close()
391 392 getattr(h, "close_all", lambda: None)()
392 393
393 394 # Begin of ipeerconnection interface.
394 395
395 396 def url(self):
396 397 return self._path
397 398
398 399 def local(self):
399 400 return None
400 401
401 402 def peer(self):
402 403 return self
403 404
404 405 def canpush(self):
405 406 return True
406 407
407 408 def close(self):
408 409 try:
409 410 reqs, sent, recv = (self._urlopener.requestscount,
410 411 self._urlopener.sentbytescount,
411 412 self._urlopener.receivedbytescount)
412 413 except AttributeError:
413 414 return
414 415 self.ui.note(_('(sent %d HTTP requests and %d bytes; '
415 416 'received %d bytes in responses)\n') %
416 417 (reqs, sent, recv))
417 418
418 419 # End of ipeerconnection interface.
419 420
420 421 # Begin of ipeercommands interface.
421 422
422 423 def capabilities(self):
423 424 return self._caps
424 425
425 426 # End of ipeercommands interface.
426 427
427 428 def _callstream(self, cmd, _compressible=False, **args):
428 429 args = pycompat.byteskwargs(args)
429 430
430 431 req, cu, qs = makev1commandrequest(self.ui, self._requestbuilder,
431 432 self._caps, self.capable,
432 433 self._url, cmd, args)
433 434
434 435 resp = sendrequest(self.ui, self._urlopener, req)
435 436
436 437 self._url, ct, resp = parsev1commandresponse(self.ui, self._url, cu, qs,
437 438 resp, _compressible)
438 439
439 440 return resp
440 441
441 442 def _call(self, cmd, **args):
442 443 fp = self._callstream(cmd, **args)
443 444 try:
444 445 return fp.read()
445 446 finally:
446 447 # if using keepalive, allow connection to be reused
447 448 fp.close()
448 449
449 450 def _callpush(self, cmd, cg, **args):
450 451 # have to stream bundle to a temp file because we do not have
451 452 # http 1.1 chunked transfer.
452 453
453 454 types = self.capable('unbundle')
454 455 try:
455 456 types = types.split(',')
456 457 except AttributeError:
457 458 # servers older than d1b16a746db6 will send 'unbundle' as a
458 459 # boolean capability. They only support headerless/uncompressed
459 460 # bundles.
460 461 types = [""]
461 462 for x in types:
462 463 if x in bundle2.bundletypes:
463 464 type = x
464 465 break
465 466
466 467 tempname = bundle2.writebundle(self.ui, cg, None, type)
467 468 fp = httpconnection.httpsendfile(self.ui, tempname, "rb")
468 469 headers = {r'Content-Type': r'application/mercurial-0.1'}
469 470
470 471 try:
471 472 r = self._call(cmd, data=fp, headers=headers, **args)
472 473 vals = r.split('\n', 1)
473 474 if len(vals) < 2:
474 475 raise error.ResponseError(_("unexpected response:"), r)
475 476 return vals
476 477 except urlerr.httperror:
477 478 # Catch and re-raise these so we don't try and treat them
478 479 # like generic socket errors. They lack any values in
479 480 # .args on Python 3 which breaks our socket.error block.
480 481 raise
481 482 except socket.error as err:
482 483 if err.args[0] in (errno.ECONNRESET, errno.EPIPE):
483 484 raise error.Abort(_('push failed: %s') % err.args[1])
484 485 raise error.Abort(err.args[1])
485 486 finally:
486 487 fp.close()
487 488 os.unlink(tempname)
488 489
489 490 def _calltwowaystream(self, cmd, fp, **args):
490 491 fh = None
491 492 fp_ = None
492 493 filename = None
493 494 try:
494 495 # dump bundle to disk
495 496 fd, filename = pycompat.mkstemp(prefix="hg-bundle-", suffix=".hg")
496 497 fh = os.fdopen(fd, r"wb")
497 498 d = fp.read(4096)
498 499 while d:
499 500 fh.write(d)
500 501 d = fp.read(4096)
501 502 fh.close()
502 503 # start http push
503 504 fp_ = httpconnection.httpsendfile(self.ui, filename, "rb")
504 505 headers = {r'Content-Type': r'application/mercurial-0.1'}
505 506 return self._callstream(cmd, data=fp_, headers=headers, **args)
506 507 finally:
507 508 if fp_ is not None:
508 509 fp_.close()
509 510 if fh is not None:
510 511 fh.close()
511 512 os.unlink(filename)
512 513
513 514 def _callcompressable(self, cmd, **args):
514 515 return self._callstream(cmd, _compressible=True, **args)
515 516
516 517 def _abort(self, exception):
517 518 raise exception
518 519
519 520 def sendv2request(ui, opener, requestbuilder, apiurl, permission, requests,
520 521 redirect):
521 522 wireprotoframing.populatestreamencoders()
522 523
523 524 uiencoders = ui.configlist(b'experimental', b'httppeer.v2-encoder-order')
524 525
525 526 if uiencoders:
526 527 encoders = []
527 528
528 529 for encoder in uiencoders:
529 530 if encoder not in wireprotoframing.STREAM_ENCODERS:
530 531 ui.warn(_(b'wire protocol version 2 encoder referenced in '
531 532 b'config (%s) is not known; ignoring\n') % encoder)
532 533 else:
533 534 encoders.append(encoder)
534 535
535 536 else:
536 537 encoders = wireprotoframing.STREAM_ENCODERS_ORDER
537 538
538 539 reactor = wireprotoframing.clientreactor(ui,
539 540 hasmultiplesend=False,
540 541 buffersends=True,
541 542 clientcontentencoders=encoders)
542 543
543 544 handler = wireprotov2peer.clienthandler(ui, reactor,
544 545 opener=opener,
545 546 requestbuilder=requestbuilder)
546 547
547 548 url = '%s/%s' % (apiurl, permission)
548 549
549 550 if len(requests) > 1:
550 551 url += '/multirequest'
551 552 else:
552 553 url += '/%s' % requests[0][0]
553 554
554 555 ui.debug('sending %d commands\n' % len(requests))
555 556 for command, args, f in requests:
556 557 ui.debug('sending command %s: %s\n' % (
557 558 command, stringutil.pprint(args, indent=2)))
558 559 assert not list(handler.callcommand(command, args, f,
559 560 redirect=redirect))
560 561
561 562 # TODO stream this.
562 563 body = b''.join(map(bytes, handler.flushcommands()))
563 564
564 565 # TODO modify user-agent to reflect v2
565 566 headers = {
566 567 r'Accept': wireprotov2server.FRAMINGTYPE,
567 568 r'Content-Type': wireprotov2server.FRAMINGTYPE,
568 569 }
569 570
570 571 req = requestbuilder(pycompat.strurl(url), body, headers)
571 572 req.add_unredirected_header(r'Content-Length', r'%d' % len(body))
572 573
573 574 try:
574 575 res = opener.open(req)
575 576 except urlerr.httperror as e:
576 577 if e.code == 401:
577 578 raise error.Abort(_('authorization failed'))
578 579
579 580 raise
580 581 except httplib.HTTPException as e:
581 582 ui.traceback()
582 583 raise IOError(None, e)
583 584
584 585 return handler, res
585 586
586 587 class queuedcommandfuture(pycompat.futures.Future):
587 588 """Wraps result() on command futures to trigger submission on call."""
588 589
589 590 def result(self, timeout=None):
590 591 if self.done():
591 592 return pycompat.futures.Future.result(self, timeout)
592 593
593 594 self._peerexecutor.sendcommands()
594 595
595 596 # sendcommands() will restore the original __class__ and self.result
596 597 # will resolve to Future.result.
597 598 return self.result(timeout)
598 599
599 600 @interfaceutil.implementer(repository.ipeercommandexecutor)
600 601 class httpv2executor(object):
601 602 def __init__(self, ui, opener, requestbuilder, apiurl, descriptor,
602 603 redirect):
603 604 self._ui = ui
604 605 self._opener = opener
605 606 self._requestbuilder = requestbuilder
606 607 self._apiurl = apiurl
607 608 self._descriptor = descriptor
608 609 self._redirect = redirect
609 610 self._sent = False
610 611 self._closed = False
611 612 self._neededpermissions = set()
612 613 self._calls = []
613 614 self._futures = weakref.WeakSet()
614 615 self._responseexecutor = None
615 616 self._responsef = None
616 617
617 618 def __enter__(self):
618 619 return self
619 620
620 621 def __exit__(self, exctype, excvalue, exctb):
621 622 self.close()
622 623
623 624 def callcommand(self, command, args):
624 625 if self._sent:
625 626 raise error.ProgrammingError('callcommand() cannot be used after '
626 627 'commands are sent')
627 628
628 629 if self._closed:
629 630 raise error.ProgrammingError('callcommand() cannot be used after '
630 631 'close()')
631 632
632 633 # The service advertises which commands are available. So if we attempt
633 634 # to call an unknown command or pass an unknown argument, we can screen
634 635 # for this.
635 636 if command not in self._descriptor['commands']:
636 637 raise error.ProgrammingError(
637 638 'wire protocol command %s is not available' % command)
638 639
639 640 cmdinfo = self._descriptor['commands'][command]
640 641 unknownargs = set(args.keys()) - set(cmdinfo.get('args', {}))
641 642
642 643 if unknownargs:
643 644 raise error.ProgrammingError(
644 645 'wire protocol command %s does not accept argument: %s' % (
645 646 command, ', '.join(sorted(unknownargs))))
646 647
647 648 self._neededpermissions |= set(cmdinfo['permissions'])
648 649
649 650 # TODO we /could/ also validate types here, since the API descriptor
650 651 # includes types...
651 652
652 653 f = pycompat.futures.Future()
653 654
654 655 # Monkeypatch it so result() triggers sendcommands(), otherwise result()
655 656 # could deadlock.
656 657 f.__class__ = queuedcommandfuture
657 658 f._peerexecutor = self
658 659
659 660 self._futures.add(f)
660 661 self._calls.append((command, args, f))
661 662
662 663 return f
663 664
664 665 def sendcommands(self):
665 666 if self._sent:
666 667 return
667 668
668 669 if not self._calls:
669 670 return
670 671
671 672 self._sent = True
672 673
673 674 # Unhack any future types so caller sees a clean type and so we
674 675 # break reference cycle.
675 676 for f in self._futures:
676 677 if isinstance(f, queuedcommandfuture):
677 678 f.__class__ = pycompat.futures.Future
678 679 f._peerexecutor = None
679 680
680 681 # Mark the future as running and filter out cancelled futures.
681 682 calls = [(command, args, f)
682 683 for command, args, f in self._calls
683 684 if f.set_running_or_notify_cancel()]
684 685
685 686 # Clear out references, prevent improper object usage.
686 687 self._calls = None
687 688
688 689 if not calls:
689 690 return
690 691
691 692 permissions = set(self._neededpermissions)
692 693
693 694 if 'push' in permissions and 'pull' in permissions:
694 695 permissions.remove('pull')
695 696
696 697 if len(permissions) > 1:
697 698 raise error.RepoError(_('cannot make request requiring multiple '
698 699 'permissions: %s') %
699 700 _(', ').join(sorted(permissions)))
700 701
701 702 permission = {
702 703 'push': 'rw',
703 704 'pull': 'ro',
704 705 }[permissions.pop()]
705 706
706 707 handler, resp = sendv2request(
707 708 self._ui, self._opener, self._requestbuilder, self._apiurl,
708 709 permission, calls, self._redirect)
709 710
710 711 # TODO we probably want to validate the HTTP code, media type, etc.
711 712
712 713 self._responseexecutor = pycompat.futures.ThreadPoolExecutor(1)
713 714 self._responsef = self._responseexecutor.submit(self._handleresponse,
714 715 handler, resp)
715 716
716 717 def close(self):
717 718 if self._closed:
718 719 return
719 720
720 721 self.sendcommands()
721 722
722 723 self._closed = True
723 724
724 725 if not self._responsef:
725 726 return
726 727
727 728 # TODO ^C here may not result in immediate program termination.
728 729
729 730 try:
730 731 self._responsef.result()
731 732 finally:
732 733 self._responseexecutor.shutdown(wait=True)
733 734 self._responsef = None
734 735 self._responseexecutor = None
735 736
736 737 # If any of our futures are still in progress, mark them as
737 738 # errored, otherwise a result() could wait indefinitely.
738 739 for f in self._futures:
739 740 if not f.done():
740 741 f.set_exception(error.ResponseError(
741 742 _('unfulfilled command response')))
742 743
743 744 self._futures = None
744 745
745 746 def _handleresponse(self, handler, resp):
746 747 # Called in a thread to read the response.
747 748
748 749 while handler.readdata(resp):
749 750 pass
750 751
751 752 @interfaceutil.implementer(repository.ipeerv2)
752 753 class httpv2peer(object):
754
755 limitedarguments = False
756
753 757 def __init__(self, ui, repourl, apipath, opener, requestbuilder,
754 758 apidescriptor):
755 759 self.ui = ui
756 760 self.apidescriptor = apidescriptor
757 761
758 762 if repourl.endswith('/'):
759 763 repourl = repourl[:-1]
760 764
761 765 self._url = repourl
762 766 self._apipath = apipath
763 767 self._apiurl = '%s/%s' % (repourl, apipath)
764 768 self._opener = opener
765 769 self._requestbuilder = requestbuilder
766 770
767 771 self._redirect = wireprotov2peer.supportedredirects(ui, apidescriptor)
768 772
769 773 # Start of ipeerconnection.
770 774
771 775 def url(self):
772 776 return self._url
773 777
774 778 def local(self):
775 779 return None
776 780
777 781 def peer(self):
778 782 return self
779 783
780 784 def canpush(self):
781 785 # TODO change once implemented.
782 786 return False
783 787
784 788 def close(self):
785 789 self.ui.note(_('(sent %d HTTP requests and %d bytes; '
786 790 'received %d bytes in responses)\n') %
787 791 (self._opener.requestscount,
788 792 self._opener.sentbytescount,
789 793 self._opener.receivedbytescount))
790 794
791 795 # End of ipeerconnection.
792 796
793 797 # Start of ipeercapabilities.
794 798
795 799 def capable(self, name):
796 800 # The capabilities used internally historically map to capabilities
797 801 # advertised from the "capabilities" wire protocol command. However,
798 802 # version 2 of that command works differently.
799 803
800 804 # Maps to commands that are available.
801 805 if name in ('branchmap', 'getbundle', 'known', 'lookup', 'pushkey'):
802 806 return True
803 807
804 808 # Other concepts.
805 809 if name in ('bundle2'):
806 810 return True
807 811
808 812 # Alias command-* to presence of command of that name.
809 813 if name.startswith('command-'):
810 814 return name[len('command-'):] in self.apidescriptor['commands']
811 815
812 816 return False
813 817
814 818 def requirecap(self, name, purpose):
815 819 if self.capable(name):
816 820 return
817 821
818 822 raise error.CapabilityError(
819 823 _('cannot %s; client or remote repository does not support the '
820 824 '\'%s\' capability') % (purpose, name))
821 825
822 826 # End of ipeercapabilities.
823 827
824 828 def _call(self, name, **args):
825 829 with self.commandexecutor() as e:
826 830 return e.callcommand(name, args).result()
827 831
828 832 def commandexecutor(self):
829 833 return httpv2executor(self.ui, self._opener, self._requestbuilder,
830 834 self._apiurl, self.apidescriptor, self._redirect)
831 835
832 836 # Registry of API service names to metadata about peers that handle it.
833 837 #
834 838 # The following keys are meaningful:
835 839 #
836 840 # init
837 841 # Callable receiving (ui, repourl, servicepath, opener, requestbuilder,
838 842 # apidescriptor) to create a peer.
839 843 #
840 844 # priority
841 845 # Integer priority for the service. If we could choose from multiple
842 846 # services, we choose the one with the highest priority.
843 847 API_PEERS = {
844 848 wireprototypes.HTTP_WIREPROTO_V2: {
845 849 'init': httpv2peer,
846 850 'priority': 50,
847 851 },
848 852 }
849 853
850 854 def performhandshake(ui, url, opener, requestbuilder):
851 855 # The handshake is a request to the capabilities command.
852 856
853 857 caps = None
854 858 def capable(x):
855 859 raise error.ProgrammingError('should not be called')
856 860
857 861 args = {}
858 862
859 863 # The client advertises support for newer protocols by adding an
860 864 # X-HgUpgrade-* header with a list of supported APIs and an
861 865 # X-HgProto-* header advertising which serializing formats it supports.
862 866 # We only support the HTTP version 2 transport and CBOR responses for
863 867 # now.
864 868 advertisev2 = ui.configbool('experimental', 'httppeer.advertise-v2')
865 869
866 870 if advertisev2:
867 871 args['headers'] = {
868 872 r'X-HgProto-1': r'cbor',
869 873 }
870 874
871 875 args['headers'].update(
872 876 encodevalueinheaders(' '.join(sorted(API_PEERS)),
873 877 'X-HgUpgrade',
874 878 # We don't know the header limit this early.
875 879 # So make it small.
876 880 1024))
877 881
878 882 req, requrl, qs = makev1commandrequest(ui, requestbuilder, caps,
879 883 capable, url, 'capabilities',
880 884 args)
881 885 resp = sendrequest(ui, opener, req)
882 886
883 887 # The server may redirect us to the repo root, stripping the
884 888 # ?cmd=capabilities query string from the URL. The server would likely
885 889 # return HTML in this case and ``parsev1commandresponse()`` would raise.
886 890 # We catch this special case and re-issue the capabilities request against
887 891 # the new URL.
888 892 #
889 893 # We should ideally not do this, as a redirect that drops the query
890 894 # string from the URL is arguably a server bug. (Garbage in, garbage out).
891 895 # However, Mercurial clients for several years appeared to handle this
892 896 # issue without behavior degradation. And according to issue 5860, it may
893 897 # be a longstanding bug in some server implementations. So we allow a
894 898 # redirect that drops the query string to "just work."
895 899 try:
896 900 respurl, ct, resp = parsev1commandresponse(ui, url, requrl, qs, resp,
897 901 compressible=False,
898 902 allowcbor=advertisev2)
899 903 except RedirectedRepoError as e:
900 904 req, requrl, qs = makev1commandrequest(ui, requestbuilder, caps,
901 905 capable, e.respurl,
902 906 'capabilities', args)
903 907 resp = sendrequest(ui, opener, req)
904 908 respurl, ct, resp = parsev1commandresponse(ui, url, requrl, qs, resp,
905 909 compressible=False,
906 910 allowcbor=advertisev2)
907 911
908 912 try:
909 913 rawdata = resp.read()
910 914 finally:
911 915 resp.close()
912 916
913 917 if not ct.startswith('application/mercurial-'):
914 918 raise error.ProgrammingError('unexpected content-type: %s' % ct)
915 919
916 920 if advertisev2:
917 921 if ct == 'application/mercurial-cbor':
918 922 try:
919 923 info = cborutil.decodeall(rawdata)[0]
920 924 except cborutil.CBORDecodeError:
921 925 raise error.Abort(_('error decoding CBOR from remote server'),
922 926 hint=_('try again and consider contacting '
923 927 'the server operator'))
924 928
925 929 # We got a legacy response. That's fine.
926 930 elif ct in ('application/mercurial-0.1', 'application/mercurial-0.2'):
927 931 info = {
928 932 'v1capabilities': set(rawdata.split())
929 933 }
930 934
931 935 else:
932 936 raise error.RepoError(
933 937 _('unexpected response type from server: %s') % ct)
934 938 else:
935 939 info = {
936 940 'v1capabilities': set(rawdata.split())
937 941 }
938 942
939 943 return respurl, info
940 944
941 945 def makepeer(ui, path, opener=None, requestbuilder=urlreq.request):
942 946 """Construct an appropriate HTTP peer instance.
943 947
944 948 ``opener`` is an ``url.opener`` that should be used to establish
945 949 connections, perform HTTP requests.
946 950
947 951 ``requestbuilder`` is the type used for constructing HTTP requests.
948 952 It exists as an argument so extensions can override the default.
949 953 """
950 954 u = util.url(path)
951 955 if u.query or u.fragment:
952 956 raise error.Abort(_('unsupported URL component: "%s"') %
953 957 (u.query or u.fragment))
954 958
955 959 # urllib cannot handle URLs with embedded user or passwd.
956 960 url, authinfo = u.authinfo()
957 961 ui.debug('using %s\n' % url)
958 962
959 963 opener = opener or urlmod.opener(ui, authinfo)
960 964
961 965 respurl, info = performhandshake(ui, url, opener, requestbuilder)
962 966
963 967 # Given the intersection of APIs that both we and the server support,
964 968 # sort by their advertised priority and pick the first one.
965 969 #
966 970 # TODO consider making this request-based and interface driven. For
967 971 # example, the caller could say "I want a peer that does X." It's quite
968 972 # possible that not all peers would do that. Since we know the service
969 973 # capabilities, we could filter out services not meeting the
970 974 # requirements. Possibly by consulting the interfaces defined by the
971 975 # peer type.
972 976 apipeerchoices = set(info.get('apis', {}).keys()) & set(API_PEERS.keys())
973 977
974 978 preferredchoices = sorted(apipeerchoices,
975 979 key=lambda x: API_PEERS[x]['priority'],
976 980 reverse=True)
977 981
978 982 for service in preferredchoices:
979 983 apipath = '%s/%s' % (info['apibase'].rstrip('/'), service)
980 984
981 985 return API_PEERS[service]['init'](ui, respurl, apipath, opener,
982 986 requestbuilder,
983 987 info['apis'][service])
984 988
985 989 # Failed to construct an API peer. Fall back to legacy.
986 990 return httppeer(ui, path, respurl, opener, requestbuilder,
987 991 info['v1capabilities'])
988 992
989 993 def instance(ui, path, create, intents=None, createopts=None):
990 994 if create:
991 995 raise error.Abort(_('cannot create new http repository'))
992 996 try:
993 997 if path.startswith('https:') and not urlmod.has_https:
994 998 raise error.Abort(_('Python support for SSL and HTTPS '
995 999 'is not installed'))
996 1000
997 1001 inst = makepeer(ui, path)
998 1002
999 1003 return inst
1000 1004 except error.RepoError as httpexception:
1001 1005 try:
1002 1006 r = statichttprepo.instance(ui, "static-" + path, create)
1003 1007 ui.note(_('(falling back to static-http)\n'))
1004 1008 return r
1005 1009 except error.RepoError:
1006 1010 raise httpexception # use the original http RepoError instead
@@ -1,1864 +1,1870
1 1 # repository.py - Interfaces and base classes for repositories and peers.
2 2 #
3 3 # Copyright 2017 Gregory Szorc <gregory.szorc@gmail.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 from __future__ import absolute_import
9 9
10 10 from .i18n import _
11 11 from . import (
12 12 error,
13 13 )
14 14 from .utils import (
15 15 interfaceutil,
16 16 )
17 17
18 18 # When narrowing is finalized and no longer subject to format changes,
19 19 # we should move this to just "narrow" or similar.
20 20 NARROW_REQUIREMENT = 'narrowhg-experimental'
21 21
22 22 # Local repository feature string.
23 23
24 24 # Revlogs are being used for file storage.
25 25 REPO_FEATURE_REVLOG_FILE_STORAGE = b'revlogfilestorage'
26 26 # The storage part of the repository is shared from an external source.
27 27 REPO_FEATURE_SHARED_STORAGE = b'sharedstore'
28 28 # LFS supported for backing file storage.
29 29 REPO_FEATURE_LFS = b'lfs'
30 30 # Repository supports being stream cloned.
31 31 REPO_FEATURE_STREAM_CLONE = b'streamclone'
32 32 # Files storage may lack data for all ancestors.
33 33 REPO_FEATURE_SHALLOW_FILE_STORAGE = b'shallowfilestorage'
34 34
35 35 REVISION_FLAG_CENSORED = 1 << 15
36 36 REVISION_FLAG_ELLIPSIS = 1 << 14
37 37 REVISION_FLAG_EXTSTORED = 1 << 13
38 38
39 39 REVISION_FLAGS_KNOWN = (
40 40 REVISION_FLAG_CENSORED | REVISION_FLAG_ELLIPSIS | REVISION_FLAG_EXTSTORED)
41 41
42 42 CG_DELTAMODE_STD = b'default'
43 43 CG_DELTAMODE_PREV = b'previous'
44 44 CG_DELTAMODE_FULL = b'fulltext'
45 45 CG_DELTAMODE_P1 = b'p1'
46 46
47 47 class ipeerconnection(interfaceutil.Interface):
48 48 """Represents a "connection" to a repository.
49 49
50 50 This is the base interface for representing a connection to a repository.
51 51 It holds basic properties and methods applicable to all peer types.
52 52
53 53 This is not a complete interface definition and should not be used
54 54 outside of this module.
55 55 """
56 56 ui = interfaceutil.Attribute("""ui.ui instance""")
57 57
58 58 def url():
59 59 """Returns a URL string representing this peer.
60 60
61 61 Currently, implementations expose the raw URL used to construct the
62 62 instance. It may contain credentials as part of the URL. The
63 63 expectations of the value aren't well-defined and this could lead to
64 64 data leakage.
65 65
66 66 TODO audit/clean consumers and more clearly define the contents of this
67 67 value.
68 68 """
69 69
70 70 def local():
71 71 """Returns a local repository instance.
72 72
73 73 If the peer represents a local repository, returns an object that
74 74 can be used to interface with it. Otherwise returns ``None``.
75 75 """
76 76
77 77 def peer():
78 78 """Returns an object conforming to this interface.
79 79
80 80 Most implementations will ``return self``.
81 81 """
82 82
83 83 def canpush():
84 84 """Returns a boolean indicating if this peer can be pushed to."""
85 85
86 86 def close():
87 87 """Close the connection to this peer.
88 88
89 89 This is called when the peer will no longer be used. Resources
90 90 associated with the peer should be cleaned up.
91 91 """
92 92
93 93 class ipeercapabilities(interfaceutil.Interface):
94 94 """Peer sub-interface related to capabilities."""
95 95
96 96 def capable(name):
97 97 """Determine support for a named capability.
98 98
99 99 Returns ``False`` if capability not supported.
100 100
101 101 Returns ``True`` if boolean capability is supported. Returns a string
102 102 if capability support is non-boolean.
103 103
104 104 Capability strings may or may not map to wire protocol capabilities.
105 105 """
106 106
107 107 def requirecap(name, purpose):
108 108 """Require a capability to be present.
109 109
110 110 Raises a ``CapabilityError`` if the capability isn't present.
111 111 """
112 112
113 113 class ipeercommands(interfaceutil.Interface):
114 114 """Client-side interface for communicating over the wire protocol.
115 115
116 116 This interface is used as a gateway to the Mercurial wire protocol.
117 117 methods commonly call wire protocol commands of the same name.
118 118 """
119 119
120 120 def branchmap():
121 121 """Obtain heads in named branches.
122 122
123 123 Returns a dict mapping branch name to an iterable of nodes that are
124 124 heads on that branch.
125 125 """
126 126
127 127 def capabilities():
128 128 """Obtain capabilities of the peer.
129 129
130 130 Returns a set of string capabilities.
131 131 """
132 132
133 133 def clonebundles():
134 134 """Obtains the clone bundles manifest for the repo.
135 135
136 136 Returns the manifest as unparsed bytes.
137 137 """
138 138
139 139 def debugwireargs(one, two, three=None, four=None, five=None):
140 140 """Used to facilitate debugging of arguments passed over the wire."""
141 141
142 142 def getbundle(source, **kwargs):
143 143 """Obtain remote repository data as a bundle.
144 144
145 145 This command is how the bulk of repository data is transferred from
146 146 the peer to the local repository
147 147
148 148 Returns a generator of bundle data.
149 149 """
150 150
151 151 def heads():
152 152 """Determine all known head revisions in the peer.
153 153
154 154 Returns an iterable of binary nodes.
155 155 """
156 156
157 157 def known(nodes):
158 158 """Determine whether multiple nodes are known.
159 159
160 160 Accepts an iterable of nodes whose presence to check for.
161 161
162 162 Returns an iterable of booleans indicating of the corresponding node
163 163 at that index is known to the peer.
164 164 """
165 165
166 166 def listkeys(namespace):
167 167 """Obtain all keys in a pushkey namespace.
168 168
169 169 Returns an iterable of key names.
170 170 """
171 171
172 172 def lookup(key):
173 173 """Resolve a value to a known revision.
174 174
175 175 Returns a binary node of the resolved revision on success.
176 176 """
177 177
178 178 def pushkey(namespace, key, old, new):
179 179 """Set a value using the ``pushkey`` protocol.
180 180
181 181 Arguments correspond to the pushkey namespace and key to operate on and
182 182 the old and new values for that key.
183 183
184 184 Returns a string with the peer result. The value inside varies by the
185 185 namespace.
186 186 """
187 187
188 188 def stream_out():
189 189 """Obtain streaming clone data.
190 190
191 191 Successful result should be a generator of data chunks.
192 192 """
193 193
194 194 def unbundle(bundle, heads, url):
195 195 """Transfer repository data to the peer.
196 196
197 197 This is how the bulk of data during a push is transferred.
198 198
199 199 Returns the integer number of heads added to the peer.
200 200 """
201 201
202 202 class ipeerlegacycommands(interfaceutil.Interface):
203 203 """Interface for implementing support for legacy wire protocol commands.
204 204
205 205 Wire protocol commands transition to legacy status when they are no longer
206 206 used by modern clients. To facilitate identifying which commands are
207 207 legacy, the interfaces are split.
208 208 """
209 209
210 210 def between(pairs):
211 211 """Obtain nodes between pairs of nodes.
212 212
213 213 ``pairs`` is an iterable of node pairs.
214 214
215 215 Returns an iterable of iterables of nodes corresponding to each
216 216 requested pair.
217 217 """
218 218
219 219 def branches(nodes):
220 220 """Obtain ancestor changesets of specific nodes back to a branch point.
221 221
222 222 For each requested node, the peer finds the first ancestor node that is
223 223 a DAG root or is a merge.
224 224
225 225 Returns an iterable of iterables with the resolved values for each node.
226 226 """
227 227
228 228 def changegroup(nodes, source):
229 229 """Obtain a changegroup with data for descendants of specified nodes."""
230 230
231 231 def changegroupsubset(bases, heads, source):
232 232 pass
233 233
234 234 class ipeercommandexecutor(interfaceutil.Interface):
235 235 """Represents a mechanism to execute remote commands.
236 236
237 237 This is the primary interface for requesting that wire protocol commands
238 238 be executed. Instances of this interface are active in a context manager
239 239 and have a well-defined lifetime. When the context manager exits, all
240 240 outstanding requests are waited on.
241 241 """
242 242
243 243 def callcommand(name, args):
244 244 """Request that a named command be executed.
245 245
246 246 Receives the command name and a dictionary of command arguments.
247 247
248 248 Returns a ``concurrent.futures.Future`` that will resolve to the
249 249 result of that command request. That exact value is left up to
250 250 the implementation and possibly varies by command.
251 251
252 252 Not all commands can coexist with other commands in an executor
253 253 instance: it depends on the underlying wire protocol transport being
254 254 used and the command itself.
255 255
256 256 Implementations MAY call ``sendcommands()`` automatically if the
257 257 requested command can not coexist with other commands in this executor.
258 258
259 259 Implementations MAY call ``sendcommands()`` automatically when the
260 260 future's ``result()`` is called. So, consumers using multiple
261 261 commands with an executor MUST ensure that ``result()`` is not called
262 262 until all command requests have been issued.
263 263 """
264 264
265 265 def sendcommands():
266 266 """Trigger submission of queued command requests.
267 267
268 268 Not all transports submit commands as soon as they are requested to
269 269 run. When called, this method forces queued command requests to be
270 270 issued. It will no-op if all commands have already been sent.
271 271
272 272 When called, no more new commands may be issued with this executor.
273 273 """
274 274
275 275 def close():
276 276 """Signal that this command request is finished.
277 277
278 278 When called, no more new commands may be issued. All outstanding
279 279 commands that have previously been issued are waited on before
280 280 returning. This not only includes waiting for the futures to resolve,
281 281 but also waiting for all response data to arrive. In other words,
282 282 calling this waits for all on-wire state for issued command requests
283 283 to finish.
284 284
285 285 When used as a context manager, this method is called when exiting the
286 286 context manager.
287 287
288 288 This method may call ``sendcommands()`` if there are buffered commands.
289 289 """
290 290
291 291 class ipeerrequests(interfaceutil.Interface):
292 292 """Interface for executing commands on a peer."""
293 293
294 limitedarguments = interfaceutil.Attribute(
295 """True if the peer cannot receive large argument value for commands."""
296 )
297
294 298 def commandexecutor():
295 299 """A context manager that resolves to an ipeercommandexecutor.
296 300
297 301 The object this resolves to can be used to issue command requests
298 302 to the peer.
299 303
300 304 Callers should call its ``callcommand`` method to issue command
301 305 requests.
302 306
303 307 A new executor should be obtained for each distinct set of commands
304 308 (possibly just a single command) that the consumer wants to execute
305 309 as part of a single operation or round trip. This is because some
306 310 peers are half-duplex and/or don't support persistent connections.
307 311 e.g. in the case of HTTP peers, commands sent to an executor represent
308 312 a single HTTP request. While some peers may support multiple command
309 313 sends over the wire per executor, consumers need to code to the least
310 314 capable peer. So it should be assumed that command executors buffer
311 315 called commands until they are told to send them and that each
312 316 command executor could result in a new connection or wire-level request
313 317 being issued.
314 318 """
315 319
316 320 class ipeerbase(ipeerconnection, ipeercapabilities, ipeerrequests):
317 321 """Unified interface for peer repositories.
318 322
319 323 All peer instances must conform to this interface.
320 324 """
321 325
322 326 class ipeerv2(ipeerconnection, ipeercapabilities, ipeerrequests):
323 327 """Unified peer interface for wire protocol version 2 peers."""
324 328
325 329 apidescriptor = interfaceutil.Attribute(
326 330 """Data structure holding description of server API.""")
327 331
328 332 @interfaceutil.implementer(ipeerbase)
329 333 class peer(object):
330 334 """Base class for peer repositories."""
331 335
336 limitedarguments = False
337
332 338 def capable(self, name):
333 339 caps = self.capabilities()
334 340 if name in caps:
335 341 return True
336 342
337 343 name = '%s=' % name
338 344 for cap in caps:
339 345 if cap.startswith(name):
340 346 return cap[len(name):]
341 347
342 348 return False
343 349
344 350 def requirecap(self, name, purpose):
345 351 if self.capable(name):
346 352 return
347 353
348 354 raise error.CapabilityError(
349 355 _('cannot %s; remote repository does not support the '
350 356 '\'%s\' capability') % (purpose, name))
351 357
352 358 class iverifyproblem(interfaceutil.Interface):
353 359 """Represents a problem with the integrity of the repository.
354 360
355 361 Instances of this interface are emitted to describe an integrity issue
356 362 with a repository (e.g. corrupt storage, missing data, etc).
357 363
358 364 Instances are essentially messages associated with severity.
359 365 """
360 366 warning = interfaceutil.Attribute(
361 367 """Message indicating a non-fatal problem.""")
362 368
363 369 error = interfaceutil.Attribute(
364 370 """Message indicating a fatal problem.""")
365 371
366 372 node = interfaceutil.Attribute(
367 373 """Revision encountering the problem.
368 374
369 375 ``None`` means the problem doesn't apply to a single revision.
370 376 """)
371 377
372 378 class irevisiondelta(interfaceutil.Interface):
373 379 """Represents a delta between one revision and another.
374 380
375 381 Instances convey enough information to allow a revision to be exchanged
376 382 with another repository.
377 383
378 384 Instances represent the fulltext revision data or a delta against
379 385 another revision. Therefore the ``revision`` and ``delta`` attributes
380 386 are mutually exclusive.
381 387
382 388 Typically used for changegroup generation.
383 389 """
384 390
385 391 node = interfaceutil.Attribute(
386 392 """20 byte node of this revision.""")
387 393
388 394 p1node = interfaceutil.Attribute(
389 395 """20 byte node of 1st parent of this revision.""")
390 396
391 397 p2node = interfaceutil.Attribute(
392 398 """20 byte node of 2nd parent of this revision.""")
393 399
394 400 linknode = interfaceutil.Attribute(
395 401 """20 byte node of the changelog revision this node is linked to.""")
396 402
397 403 flags = interfaceutil.Attribute(
398 404 """2 bytes of integer flags that apply to this revision.
399 405
400 406 This is a bitwise composition of the ``REVISION_FLAG_*`` constants.
401 407 """)
402 408
403 409 basenode = interfaceutil.Attribute(
404 410 """20 byte node of the revision this data is a delta against.
405 411
406 412 ``nullid`` indicates that the revision is a full revision and not
407 413 a delta.
408 414 """)
409 415
410 416 baserevisionsize = interfaceutil.Attribute(
411 417 """Size of base revision this delta is against.
412 418
413 419 May be ``None`` if ``basenode`` is ``nullid``.
414 420 """)
415 421
416 422 revision = interfaceutil.Attribute(
417 423 """Raw fulltext of revision data for this node.""")
418 424
419 425 delta = interfaceutil.Attribute(
420 426 """Delta between ``basenode`` and ``node``.
421 427
422 428 Stored in the bdiff delta format.
423 429 """)
424 430
425 431 class ifilerevisionssequence(interfaceutil.Interface):
426 432 """Contains index data for all revisions of a file.
427 433
428 434 Types implementing this behave like lists of tuples. The index
429 435 in the list corresponds to the revision number. The values contain
430 436 index metadata.
431 437
432 438 The *null* revision (revision number -1) is always the last item
433 439 in the index.
434 440 """
435 441
436 442 def __len__():
437 443 """The total number of revisions."""
438 444
439 445 def __getitem__(rev):
440 446 """Returns the object having a specific revision number.
441 447
442 448 Returns an 8-tuple with the following fields:
443 449
444 450 offset+flags
445 451 Contains the offset and flags for the revision. 64-bit unsigned
446 452 integer where first 6 bytes are the offset and the next 2 bytes
447 453 are flags. The offset can be 0 if it is not used by the store.
448 454 compressed size
449 455 Size of the revision data in the store. It can be 0 if it isn't
450 456 needed by the store.
451 457 uncompressed size
452 458 Fulltext size. It can be 0 if it isn't needed by the store.
453 459 base revision
454 460 Revision number of revision the delta for storage is encoded
455 461 against. -1 indicates not encoded against a base revision.
456 462 link revision
457 463 Revision number of changelog revision this entry is related to.
458 464 p1 revision
459 465 Revision number of 1st parent. -1 if no 1st parent.
460 466 p2 revision
461 467 Revision number of 2nd parent. -1 if no 1st parent.
462 468 node
463 469 Binary node value for this revision number.
464 470
465 471 Negative values should index off the end of the sequence. ``-1``
466 472 should return the null revision. ``-2`` should return the most
467 473 recent revision.
468 474 """
469 475
470 476 def __contains__(rev):
471 477 """Whether a revision number exists."""
472 478
473 479 def insert(self, i, entry):
474 480 """Add an item to the index at specific revision."""
475 481
476 482 class ifileindex(interfaceutil.Interface):
477 483 """Storage interface for index data of a single file.
478 484
479 485 File storage data is divided into index metadata and data storage.
480 486 This interface defines the index portion of the interface.
481 487
482 488 The index logically consists of:
483 489
484 490 * A mapping between revision numbers and nodes.
485 491 * DAG data (storing and querying the relationship between nodes).
486 492 * Metadata to facilitate storage.
487 493 """
488 494 def __len__():
489 495 """Obtain the number of revisions stored for this file."""
490 496
491 497 def __iter__():
492 498 """Iterate over revision numbers for this file."""
493 499
494 500 def hasnode(node):
495 501 """Returns a bool indicating if a node is known to this store.
496 502
497 503 Implementations must only return True for full, binary node values:
498 504 hex nodes, revision numbers, and partial node matches must be
499 505 rejected.
500 506
501 507 The null node is never present.
502 508 """
503 509
504 510 def revs(start=0, stop=None):
505 511 """Iterate over revision numbers for this file, with control."""
506 512
507 513 def parents(node):
508 514 """Returns a 2-tuple of parent nodes for a revision.
509 515
510 516 Values will be ``nullid`` if the parent is empty.
511 517 """
512 518
513 519 def parentrevs(rev):
514 520 """Like parents() but operates on revision numbers."""
515 521
516 522 def rev(node):
517 523 """Obtain the revision number given a node.
518 524
519 525 Raises ``error.LookupError`` if the node is not known.
520 526 """
521 527
522 528 def node(rev):
523 529 """Obtain the node value given a revision number.
524 530
525 531 Raises ``IndexError`` if the node is not known.
526 532 """
527 533
528 534 def lookup(node):
529 535 """Attempt to resolve a value to a node.
530 536
531 537 Value can be a binary node, hex node, revision number, or a string
532 538 that can be converted to an integer.
533 539
534 540 Raises ``error.LookupError`` if a node could not be resolved.
535 541 """
536 542
537 543 def linkrev(rev):
538 544 """Obtain the changeset revision number a revision is linked to."""
539 545
540 546 def iscensored(rev):
541 547 """Return whether a revision's content has been censored."""
542 548
543 549 def commonancestorsheads(node1, node2):
544 550 """Obtain an iterable of nodes containing heads of common ancestors.
545 551
546 552 See ``ancestor.commonancestorsheads()``.
547 553 """
548 554
549 555 def descendants(revs):
550 556 """Obtain descendant revision numbers for a set of revision numbers.
551 557
552 558 If ``nullrev`` is in the set, this is equivalent to ``revs()``.
553 559 """
554 560
555 561 def heads(start=None, stop=None):
556 562 """Obtain a list of nodes that are DAG heads, with control.
557 563
558 564 The set of revisions examined can be limited by specifying
559 565 ``start`` and ``stop``. ``start`` is a node. ``stop`` is an
560 566 iterable of nodes. DAG traversal starts at earlier revision
561 567 ``start`` and iterates forward until any node in ``stop`` is
562 568 encountered.
563 569 """
564 570
565 571 def children(node):
566 572 """Obtain nodes that are children of a node.
567 573
568 574 Returns a list of nodes.
569 575 """
570 576
571 577 class ifiledata(interfaceutil.Interface):
572 578 """Storage interface for data storage of a specific file.
573 579
574 580 This complements ``ifileindex`` and provides an interface for accessing
575 581 data for a tracked file.
576 582 """
577 583 def size(rev):
578 584 """Obtain the fulltext size of file data.
579 585
580 586 Any metadata is excluded from size measurements.
581 587 """
582 588
583 589 def revision(node, raw=False):
584 590 """"Obtain fulltext data for a node.
585 591
586 592 By default, any storage transformations are applied before the data
587 593 is returned. If ``raw`` is True, non-raw storage transformations
588 594 are not applied.
589 595
590 596 The fulltext data may contain a header containing metadata. Most
591 597 consumers should use ``read()`` to obtain the actual file data.
592 598 """
593 599
594 600 def read(node):
595 601 """Resolve file fulltext data.
596 602
597 603 This is similar to ``revision()`` except any metadata in the data
598 604 headers is stripped.
599 605 """
600 606
601 607 def renamed(node):
602 608 """Obtain copy metadata for a node.
603 609
604 610 Returns ``False`` if no copy metadata is stored or a 2-tuple of
605 611 (path, node) from which this revision was copied.
606 612 """
607 613
608 614 def cmp(node, fulltext):
609 615 """Compare fulltext to another revision.
610 616
611 617 Returns True if the fulltext is different from what is stored.
612 618
613 619 This takes copy metadata into account.
614 620
615 621 TODO better document the copy metadata and censoring logic.
616 622 """
617 623
618 624 def emitrevisions(nodes,
619 625 nodesorder=None,
620 626 revisiondata=False,
621 627 assumehaveparentrevisions=False,
622 628 deltamode=CG_DELTAMODE_STD):
623 629 """Produce ``irevisiondelta`` for revisions.
624 630
625 631 Given an iterable of nodes, emits objects conforming to the
626 632 ``irevisiondelta`` interface that describe revisions in storage.
627 633
628 634 This method is a generator.
629 635
630 636 The input nodes may be unordered. Implementations must ensure that a
631 637 node's parents are emitted before the node itself. Transitively, this
632 638 means that a node may only be emitted once all its ancestors in
633 639 ``nodes`` have also been emitted.
634 640
635 641 By default, emits "index" data (the ``node``, ``p1node``, and
636 642 ``p2node`` attributes). If ``revisiondata`` is set, revision data
637 643 will also be present on the emitted objects.
638 644
639 645 With default argument values, implementations can choose to emit
640 646 either fulltext revision data or a delta. When emitting deltas,
641 647 implementations must consider whether the delta's base revision
642 648 fulltext is available to the receiver.
643 649
644 650 The base revision fulltext is guaranteed to be available if any of
645 651 the following are met:
646 652
647 653 * Its fulltext revision was emitted by this method call.
648 654 * A delta for that revision was emitted by this method call.
649 655 * ``assumehaveparentrevisions`` is True and the base revision is a
650 656 parent of the node.
651 657
652 658 ``nodesorder`` can be used to control the order that revisions are
653 659 emitted. By default, revisions can be reordered as long as they are
654 660 in DAG topological order (see above). If the value is ``nodes``,
655 661 the iteration order from ``nodes`` should be used. If the value is
656 662 ``storage``, then the native order from the backing storage layer
657 663 is used. (Not all storage layers will have strong ordering and behavior
658 664 of this mode is storage-dependent.) ``nodes`` ordering can force
659 665 revisions to be emitted before their ancestors, so consumers should
660 666 use it with care.
661 667
662 668 The ``linknode`` attribute on the returned ``irevisiondelta`` may not
663 669 be set and it is the caller's responsibility to resolve it, if needed.
664 670
665 671 If ``deltamode`` is CG_DELTAMODE_PREV and revision data is requested,
666 672 all revision data should be emitted as deltas against the revision
667 673 emitted just prior. The initial revision should be a delta against its
668 674 1st parent.
669 675 """
670 676
671 677 class ifilemutation(interfaceutil.Interface):
672 678 """Storage interface for mutation events of a tracked file."""
673 679
674 680 def add(filedata, meta, transaction, linkrev, p1, p2):
675 681 """Add a new revision to the store.
676 682
677 683 Takes file data, dictionary of metadata, a transaction, linkrev,
678 684 and parent nodes.
679 685
680 686 Returns the node that was added.
681 687
682 688 May no-op if a revision matching the supplied data is already stored.
683 689 """
684 690
685 691 def addrevision(revisiondata, transaction, linkrev, p1, p2, node=None,
686 692 flags=0, cachedelta=None):
687 693 """Add a new revision to the store.
688 694
689 695 This is similar to ``add()`` except it operates at a lower level.
690 696
691 697 The data passed in already contains a metadata header, if any.
692 698
693 699 ``node`` and ``flags`` can be used to define the expected node and
694 700 the flags to use with storage. ``flags`` is a bitwise value composed
695 701 of the various ``REVISION_FLAG_*`` constants.
696 702
697 703 ``add()`` is usually called when adding files from e.g. the working
698 704 directory. ``addrevision()`` is often called by ``add()`` and for
699 705 scenarios where revision data has already been computed, such as when
700 706 applying raw data from a peer repo.
701 707 """
702 708
703 709 def addgroup(deltas, linkmapper, transaction, addrevisioncb=None,
704 710 maybemissingparents=False):
705 711 """Process a series of deltas for storage.
706 712
707 713 ``deltas`` is an iterable of 7-tuples of
708 714 (node, p1, p2, linknode, deltabase, delta, flags) defining revisions
709 715 to add.
710 716
711 717 The ``delta`` field contains ``mpatch`` data to apply to a base
712 718 revision, identified by ``deltabase``. The base node can be
713 719 ``nullid``, in which case the header from the delta can be ignored
714 720 and the delta used as the fulltext.
715 721
716 722 ``addrevisioncb`` should be called for each node as it is committed.
717 723
718 724 ``maybemissingparents`` is a bool indicating whether the incoming
719 725 data may reference parents/ancestor revisions that aren't present.
720 726 This flag is set when receiving data into a "shallow" store that
721 727 doesn't hold all history.
722 728
723 729 Returns a list of nodes that were processed. A node will be in the list
724 730 even if it existed in the store previously.
725 731 """
726 732
727 733 def censorrevision(tr, node, tombstone=b''):
728 734 """Remove the content of a single revision.
729 735
730 736 The specified ``node`` will have its content purged from storage.
731 737 Future attempts to access the revision data for this node will
732 738 result in failure.
733 739
734 740 A ``tombstone`` message can optionally be stored. This message may be
735 741 displayed to users when they attempt to access the missing revision
736 742 data.
737 743
738 744 Storage backends may have stored deltas against the previous content
739 745 in this revision. As part of censoring a revision, these storage
740 746 backends are expected to rewrite any internally stored deltas such
741 747 that they no longer reference the deleted content.
742 748 """
743 749
744 750 def getstrippoint(minlink):
745 751 """Find the minimum revision that must be stripped to strip a linkrev.
746 752
747 753 Returns a 2-tuple containing the minimum revision number and a set
748 754 of all revisions numbers that would be broken by this strip.
749 755
750 756 TODO this is highly revlog centric and should be abstracted into
751 757 a higher-level deletion API. ``repair.strip()`` relies on this.
752 758 """
753 759
754 760 def strip(minlink, transaction):
755 761 """Remove storage of items starting at a linkrev.
756 762
757 763 This uses ``getstrippoint()`` to determine the first node to remove.
758 764 Then it effectively truncates storage for all revisions after that.
759 765
760 766 TODO this is highly revlog centric and should be abstracted into a
761 767 higher-level deletion API.
762 768 """
763 769
764 770 class ifilestorage(ifileindex, ifiledata, ifilemutation):
765 771 """Complete storage interface for a single tracked file."""
766 772
767 773 def files():
768 774 """Obtain paths that are backing storage for this file.
769 775
770 776 TODO this is used heavily by verify code and there should probably
771 777 be a better API for that.
772 778 """
773 779
774 780 def storageinfo(exclusivefiles=False, sharedfiles=False,
775 781 revisionscount=False, trackedsize=False,
776 782 storedsize=False):
777 783 """Obtain information about storage for this file's data.
778 784
779 785 Returns a dict describing storage for this tracked path. The keys
780 786 in the dict map to arguments of the same. The arguments are bools
781 787 indicating whether to calculate and obtain that data.
782 788
783 789 exclusivefiles
784 790 Iterable of (vfs, path) describing files that are exclusively
785 791 used to back storage for this tracked path.
786 792
787 793 sharedfiles
788 794 Iterable of (vfs, path) describing files that are used to back
789 795 storage for this tracked path. Those files may also provide storage
790 796 for other stored entities.
791 797
792 798 revisionscount
793 799 Number of revisions available for retrieval.
794 800
795 801 trackedsize
796 802 Total size in bytes of all tracked revisions. This is a sum of the
797 803 length of the fulltext of all revisions.
798 804
799 805 storedsize
800 806 Total size in bytes used to store data for all tracked revisions.
801 807 This is commonly less than ``trackedsize`` due to internal usage
802 808 of deltas rather than fulltext revisions.
803 809
804 810 Not all storage backends may support all queries are have a reasonable
805 811 value to use. In that case, the value should be set to ``None`` and
806 812 callers are expected to handle this special value.
807 813 """
808 814
809 815 def verifyintegrity(state):
810 816 """Verifies the integrity of file storage.
811 817
812 818 ``state`` is a dict holding state of the verifier process. It can be
813 819 used to communicate data between invocations of multiple storage
814 820 primitives.
815 821
816 822 If individual revisions cannot have their revision content resolved,
817 823 the method is expected to set the ``skipread`` key to a set of nodes
818 824 that encountered problems.
819 825
820 826 The method yields objects conforming to the ``iverifyproblem``
821 827 interface.
822 828 """
823 829
824 830 class idirs(interfaceutil.Interface):
825 831 """Interface representing a collection of directories from paths.
826 832
827 833 This interface is essentially a derived data structure representing
828 834 directories from a collection of paths.
829 835 """
830 836
831 837 def addpath(path):
832 838 """Add a path to the collection.
833 839
834 840 All directories in the path will be added to the collection.
835 841 """
836 842
837 843 def delpath(path):
838 844 """Remove a path from the collection.
839 845
840 846 If the removal was the last path in a particular directory, the
841 847 directory is removed from the collection.
842 848 """
843 849
844 850 def __iter__():
845 851 """Iterate over the directories in this collection of paths."""
846 852
847 853 def __contains__(path):
848 854 """Whether a specific directory is in this collection."""
849 855
850 856 class imanifestdict(interfaceutil.Interface):
851 857 """Interface representing a manifest data structure.
852 858
853 859 A manifest is effectively a dict mapping paths to entries. Each entry
854 860 consists of a binary node and extra flags affecting that entry.
855 861 """
856 862
857 863 def __getitem__(path):
858 864 """Returns the binary node value for a path in the manifest.
859 865
860 866 Raises ``KeyError`` if the path does not exist in the manifest.
861 867
862 868 Equivalent to ``self.find(path)[0]``.
863 869 """
864 870
865 871 def find(path):
866 872 """Returns the entry for a path in the manifest.
867 873
868 874 Returns a 2-tuple of (node, flags).
869 875
870 876 Raises ``KeyError`` if the path does not exist in the manifest.
871 877 """
872 878
873 879 def __len__():
874 880 """Return the number of entries in the manifest."""
875 881
876 882 def __nonzero__():
877 883 """Returns True if the manifest has entries, False otherwise."""
878 884
879 885 __bool__ = __nonzero__
880 886
881 887 def __setitem__(path, node):
882 888 """Define the node value for a path in the manifest.
883 889
884 890 If the path is already in the manifest, its flags will be copied to
885 891 the new entry.
886 892 """
887 893
888 894 def __contains__(path):
889 895 """Whether a path exists in the manifest."""
890 896
891 897 def __delitem__(path):
892 898 """Remove a path from the manifest.
893 899
894 900 Raises ``KeyError`` if the path is not in the manifest.
895 901 """
896 902
897 903 def __iter__():
898 904 """Iterate over paths in the manifest."""
899 905
900 906 def iterkeys():
901 907 """Iterate over paths in the manifest."""
902 908
903 909 def keys():
904 910 """Obtain a list of paths in the manifest."""
905 911
906 912 def filesnotin(other, match=None):
907 913 """Obtain the set of paths in this manifest but not in another.
908 914
909 915 ``match`` is an optional matcher function to be applied to both
910 916 manifests.
911 917
912 918 Returns a set of paths.
913 919 """
914 920
915 921 def dirs():
916 922 """Returns an object implementing the ``idirs`` interface."""
917 923
918 924 def hasdir(dir):
919 925 """Returns a bool indicating if a directory is in this manifest."""
920 926
921 927 def matches(match):
922 928 """Generate a new manifest filtered through a matcher.
923 929
924 930 Returns an object conforming to the ``imanifestdict`` interface.
925 931 """
926 932
927 933 def walk(match):
928 934 """Generator of paths in manifest satisfying a matcher.
929 935
930 936 This is equivalent to ``self.matches(match).iterkeys()`` except a new
931 937 manifest object is not created.
932 938
933 939 If the matcher has explicit files listed and they don't exist in
934 940 the manifest, ``match.bad()`` is called for each missing file.
935 941 """
936 942
937 943 def diff(other, match=None, clean=False):
938 944 """Find differences between this manifest and another.
939 945
940 946 This manifest is compared to ``other``.
941 947
942 948 If ``match`` is provided, the two manifests are filtered against this
943 949 matcher and only entries satisfying the matcher are compared.
944 950
945 951 If ``clean`` is True, unchanged files are included in the returned
946 952 object.
947 953
948 954 Returns a dict with paths as keys and values of 2-tuples of 2-tuples of
949 955 the form ``((node1, flag1), (node2, flag2))`` where ``(node1, flag1)``
950 956 represents the node and flags for this manifest and ``(node2, flag2)``
951 957 are the same for the other manifest.
952 958 """
953 959
954 960 def setflag(path, flag):
955 961 """Set the flag value for a given path.
956 962
957 963 Raises ``KeyError`` if the path is not already in the manifest.
958 964 """
959 965
960 966 def get(path, default=None):
961 967 """Obtain the node value for a path or a default value if missing."""
962 968
963 969 def flags(path, default=''):
964 970 """Return the flags value for a path or a default value if missing."""
965 971
966 972 def copy():
967 973 """Return a copy of this manifest."""
968 974
969 975 def items():
970 976 """Returns an iterable of (path, node) for items in this manifest."""
971 977
972 978 def iteritems():
973 979 """Identical to items()."""
974 980
975 981 def iterentries():
976 982 """Returns an iterable of (path, node, flags) for this manifest.
977 983
978 984 Similar to ``iteritems()`` except items are a 3-tuple and include
979 985 flags.
980 986 """
981 987
982 988 def text():
983 989 """Obtain the raw data representation for this manifest.
984 990
985 991 Result is used to create a manifest revision.
986 992 """
987 993
988 994 def fastdelta(base, changes):
989 995 """Obtain a delta between this manifest and another given changes.
990 996
991 997 ``base`` in the raw data representation for another manifest.
992 998
993 999 ``changes`` is an iterable of ``(path, to_delete)``.
994 1000
995 1001 Returns a 2-tuple containing ``bytearray(self.text())`` and the
996 1002 delta between ``base`` and this manifest.
997 1003 """
998 1004
999 1005 class imanifestrevisionbase(interfaceutil.Interface):
1000 1006 """Base interface representing a single revision of a manifest.
1001 1007
1002 1008 Should not be used as a primary interface: should always be inherited
1003 1009 as part of a larger interface.
1004 1010 """
1005 1011
1006 1012 def new():
1007 1013 """Obtain a new manifest instance.
1008 1014
1009 1015 Returns an object conforming to the ``imanifestrevisionwritable``
1010 1016 interface. The instance will be associated with the same
1011 1017 ``imanifestlog`` collection as this instance.
1012 1018 """
1013 1019
1014 1020 def copy():
1015 1021 """Obtain a copy of this manifest instance.
1016 1022
1017 1023 Returns an object conforming to the ``imanifestrevisionwritable``
1018 1024 interface. The instance will be associated with the same
1019 1025 ``imanifestlog`` collection as this instance.
1020 1026 """
1021 1027
1022 1028 def read():
1023 1029 """Obtain the parsed manifest data structure.
1024 1030
1025 1031 The returned object conforms to the ``imanifestdict`` interface.
1026 1032 """
1027 1033
1028 1034 class imanifestrevisionstored(imanifestrevisionbase):
1029 1035 """Interface representing a manifest revision committed to storage."""
1030 1036
1031 1037 def node():
1032 1038 """The binary node for this manifest."""
1033 1039
1034 1040 parents = interfaceutil.Attribute(
1035 1041 """List of binary nodes that are parents for this manifest revision."""
1036 1042 )
1037 1043
1038 1044 def readdelta(shallow=False):
1039 1045 """Obtain the manifest data structure representing changes from parent.
1040 1046
1041 1047 This manifest is compared to its 1st parent. A new manifest representing
1042 1048 those differences is constructed.
1043 1049
1044 1050 The returned object conforms to the ``imanifestdict`` interface.
1045 1051 """
1046 1052
1047 1053 def readfast(shallow=False):
1048 1054 """Calls either ``read()`` or ``readdelta()``.
1049 1055
1050 1056 The faster of the two options is called.
1051 1057 """
1052 1058
1053 1059 def find(key):
1054 1060 """Calls self.read().find(key)``.
1055 1061
1056 1062 Returns a 2-tuple of ``(node, flags)`` or raises ``KeyError``.
1057 1063 """
1058 1064
1059 1065 class imanifestrevisionwritable(imanifestrevisionbase):
1060 1066 """Interface representing a manifest revision that can be committed."""
1061 1067
1062 1068 def write(transaction, linkrev, p1node, p2node, added, removed, match=None):
1063 1069 """Add this revision to storage.
1064 1070
1065 1071 Takes a transaction object, the changeset revision number it will
1066 1072 be associated with, its parent nodes, and lists of added and
1067 1073 removed paths.
1068 1074
1069 1075 If match is provided, storage can choose not to inspect or write out
1070 1076 items that do not match. Storage is still required to be able to provide
1071 1077 the full manifest in the future for any directories written (these
1072 1078 manifests should not be "narrowed on disk").
1073 1079
1074 1080 Returns the binary node of the created revision.
1075 1081 """
1076 1082
1077 1083 class imanifeststorage(interfaceutil.Interface):
1078 1084 """Storage interface for manifest data."""
1079 1085
1080 1086 tree = interfaceutil.Attribute(
1081 1087 """The path to the directory this manifest tracks.
1082 1088
1083 1089 The empty bytestring represents the root manifest.
1084 1090 """)
1085 1091
1086 1092 index = interfaceutil.Attribute(
1087 1093 """An ``ifilerevisionssequence`` instance.""")
1088 1094
1089 1095 indexfile = interfaceutil.Attribute(
1090 1096 """Path of revlog index file.
1091 1097
1092 1098 TODO this is revlog specific and should not be exposed.
1093 1099 """)
1094 1100
1095 1101 opener = interfaceutil.Attribute(
1096 1102 """VFS opener to use to access underlying files used for storage.
1097 1103
1098 1104 TODO this is revlog specific and should not be exposed.
1099 1105 """)
1100 1106
1101 1107 version = interfaceutil.Attribute(
1102 1108 """Revlog version number.
1103 1109
1104 1110 TODO this is revlog specific and should not be exposed.
1105 1111 """)
1106 1112
1107 1113 _generaldelta = interfaceutil.Attribute(
1108 1114 """Whether generaldelta storage is being used.
1109 1115
1110 1116 TODO this is revlog specific and should not be exposed.
1111 1117 """)
1112 1118
1113 1119 fulltextcache = interfaceutil.Attribute(
1114 1120 """Dict with cache of fulltexts.
1115 1121
1116 1122 TODO this doesn't feel appropriate for the storage interface.
1117 1123 """)
1118 1124
1119 1125 def __len__():
1120 1126 """Obtain the number of revisions stored for this manifest."""
1121 1127
1122 1128 def __iter__():
1123 1129 """Iterate over revision numbers for this manifest."""
1124 1130
1125 1131 def rev(node):
1126 1132 """Obtain the revision number given a binary node.
1127 1133
1128 1134 Raises ``error.LookupError`` if the node is not known.
1129 1135 """
1130 1136
1131 1137 def node(rev):
1132 1138 """Obtain the node value given a revision number.
1133 1139
1134 1140 Raises ``error.LookupError`` if the revision is not known.
1135 1141 """
1136 1142
1137 1143 def lookup(value):
1138 1144 """Attempt to resolve a value to a node.
1139 1145
1140 1146 Value can be a binary node, hex node, revision number, or a bytes
1141 1147 that can be converted to an integer.
1142 1148
1143 1149 Raises ``error.LookupError`` if a ndoe could not be resolved.
1144 1150 """
1145 1151
1146 1152 def parents(node):
1147 1153 """Returns a 2-tuple of parent nodes for a node.
1148 1154
1149 1155 Values will be ``nullid`` if the parent is empty.
1150 1156 """
1151 1157
1152 1158 def parentrevs(rev):
1153 1159 """Like parents() but operates on revision numbers."""
1154 1160
1155 1161 def linkrev(rev):
1156 1162 """Obtain the changeset revision number a revision is linked to."""
1157 1163
1158 1164 def revision(node, _df=None, raw=False):
1159 1165 """Obtain fulltext data for a node."""
1160 1166
1161 1167 def revdiff(rev1, rev2):
1162 1168 """Obtain a delta between two revision numbers.
1163 1169
1164 1170 The returned data is the result of ``bdiff.bdiff()`` on the raw
1165 1171 revision data.
1166 1172 """
1167 1173
1168 1174 def cmp(node, fulltext):
1169 1175 """Compare fulltext to another revision.
1170 1176
1171 1177 Returns True if the fulltext is different from what is stored.
1172 1178 """
1173 1179
1174 1180 def emitrevisions(nodes,
1175 1181 nodesorder=None,
1176 1182 revisiondata=False,
1177 1183 assumehaveparentrevisions=False):
1178 1184 """Produce ``irevisiondelta`` describing revisions.
1179 1185
1180 1186 See the documentation for ``ifiledata`` for more.
1181 1187 """
1182 1188
1183 1189 def addgroup(deltas, linkmapper, transaction, addrevisioncb=None):
1184 1190 """Process a series of deltas for storage.
1185 1191
1186 1192 See the documentation in ``ifilemutation`` for more.
1187 1193 """
1188 1194
1189 1195 def rawsize(rev):
1190 1196 """Obtain the size of tracked data.
1191 1197
1192 1198 Is equivalent to ``len(m.revision(node, raw=True))``.
1193 1199
1194 1200 TODO this method is only used by upgrade code and may be removed.
1195 1201 """
1196 1202
1197 1203 def getstrippoint(minlink):
1198 1204 """Find minimum revision that must be stripped to strip a linkrev.
1199 1205
1200 1206 See the documentation in ``ifilemutation`` for more.
1201 1207 """
1202 1208
1203 1209 def strip(minlink, transaction):
1204 1210 """Remove storage of items starting at a linkrev.
1205 1211
1206 1212 See the documentation in ``ifilemutation`` for more.
1207 1213 """
1208 1214
1209 1215 def checksize():
1210 1216 """Obtain the expected sizes of backing files.
1211 1217
1212 1218 TODO this is used by verify and it should not be part of the interface.
1213 1219 """
1214 1220
1215 1221 def files():
1216 1222 """Obtain paths that are backing storage for this manifest.
1217 1223
1218 1224 TODO this is used by verify and there should probably be a better API
1219 1225 for this functionality.
1220 1226 """
1221 1227
1222 1228 def deltaparent(rev):
1223 1229 """Obtain the revision that a revision is delta'd against.
1224 1230
1225 1231 TODO delta encoding is an implementation detail of storage and should
1226 1232 not be exposed to the storage interface.
1227 1233 """
1228 1234
1229 1235 def clone(tr, dest, **kwargs):
1230 1236 """Clone this instance to another."""
1231 1237
1232 1238 def clearcaches(clear_persisted_data=False):
1233 1239 """Clear any caches associated with this instance."""
1234 1240
1235 1241 def dirlog(d):
1236 1242 """Obtain a manifest storage instance for a tree."""
1237 1243
1238 1244 def add(m, transaction, link, p1, p2, added, removed, readtree=None,
1239 1245 match=None):
1240 1246 """Add a revision to storage.
1241 1247
1242 1248 ``m`` is an object conforming to ``imanifestdict``.
1243 1249
1244 1250 ``link`` is the linkrev revision number.
1245 1251
1246 1252 ``p1`` and ``p2`` are the parent revision numbers.
1247 1253
1248 1254 ``added`` and ``removed`` are iterables of added and removed paths,
1249 1255 respectively.
1250 1256
1251 1257 ``readtree`` is a function that can be used to read the child tree(s)
1252 1258 when recursively writing the full tree structure when using
1253 1259 treemanifets.
1254 1260
1255 1261 ``match`` is a matcher that can be used to hint to storage that not all
1256 1262 paths must be inspected; this is an optimization and can be safely
1257 1263 ignored. Note that the storage must still be able to reproduce a full
1258 1264 manifest including files that did not match.
1259 1265 """
1260 1266
1261 1267 def storageinfo(exclusivefiles=False, sharedfiles=False,
1262 1268 revisionscount=False, trackedsize=False,
1263 1269 storedsize=False):
1264 1270 """Obtain information about storage for this manifest's data.
1265 1271
1266 1272 See ``ifilestorage.storageinfo()`` for a description of this method.
1267 1273 This one behaves the same way, except for manifest data.
1268 1274 """
1269 1275
1270 1276 class imanifestlog(interfaceutil.Interface):
1271 1277 """Interface representing a collection of manifest snapshots.
1272 1278
1273 1279 Represents the root manifest in a repository.
1274 1280
1275 1281 Also serves as a means to access nested tree manifests and to cache
1276 1282 tree manifests.
1277 1283 """
1278 1284
1279 1285 def __getitem__(node):
1280 1286 """Obtain a manifest instance for a given binary node.
1281 1287
1282 1288 Equivalent to calling ``self.get('', node)``.
1283 1289
1284 1290 The returned object conforms to the ``imanifestrevisionstored``
1285 1291 interface.
1286 1292 """
1287 1293
1288 1294 def get(tree, node, verify=True):
1289 1295 """Retrieve the manifest instance for a given directory and binary node.
1290 1296
1291 1297 ``node`` always refers to the node of the root manifest (which will be
1292 1298 the only manifest if flat manifests are being used).
1293 1299
1294 1300 If ``tree`` is the empty string, the root manifest is returned.
1295 1301 Otherwise the manifest for the specified directory will be returned
1296 1302 (requires tree manifests).
1297 1303
1298 1304 If ``verify`` is True, ``LookupError`` is raised if the node is not
1299 1305 known.
1300 1306
1301 1307 The returned object conforms to the ``imanifestrevisionstored``
1302 1308 interface.
1303 1309 """
1304 1310
1305 1311 def getstorage(tree):
1306 1312 """Retrieve an interface to storage for a particular tree.
1307 1313
1308 1314 If ``tree`` is the empty bytestring, storage for the root manifest will
1309 1315 be returned. Otherwise storage for a tree manifest is returned.
1310 1316
1311 1317 TODO formalize interface for returned object.
1312 1318 """
1313 1319
1314 1320 def clearcaches():
1315 1321 """Clear caches associated with this collection."""
1316 1322
1317 1323 def rev(node):
1318 1324 """Obtain the revision number for a binary node.
1319 1325
1320 1326 Raises ``error.LookupError`` if the node is not known.
1321 1327 """
1322 1328
1323 1329 class ilocalrepositoryfilestorage(interfaceutil.Interface):
1324 1330 """Local repository sub-interface providing access to tracked file storage.
1325 1331
1326 1332 This interface defines how a repository accesses storage for a single
1327 1333 tracked file path.
1328 1334 """
1329 1335
1330 1336 def file(f):
1331 1337 """Obtain a filelog for a tracked path.
1332 1338
1333 1339 The returned type conforms to the ``ifilestorage`` interface.
1334 1340 """
1335 1341
1336 1342 class ilocalrepositorymain(interfaceutil.Interface):
1337 1343 """Main interface for local repositories.
1338 1344
1339 1345 This currently captures the reality of things - not how things should be.
1340 1346 """
1341 1347
1342 1348 supportedformats = interfaceutil.Attribute(
1343 1349 """Set of requirements that apply to stream clone.
1344 1350
1345 1351 This is actually a class attribute and is shared among all instances.
1346 1352 """)
1347 1353
1348 1354 supported = interfaceutil.Attribute(
1349 1355 """Set of requirements that this repo is capable of opening.""")
1350 1356
1351 1357 requirements = interfaceutil.Attribute(
1352 1358 """Set of requirements this repo uses.""")
1353 1359
1354 1360 features = interfaceutil.Attribute(
1355 1361 """Set of "features" this repository supports.
1356 1362
1357 1363 A "feature" is a loosely-defined term. It can refer to a feature
1358 1364 in the classical sense or can describe an implementation detail
1359 1365 of the repository. For example, a ``readonly`` feature may denote
1360 1366 the repository as read-only. Or a ``revlogfilestore`` feature may
1361 1367 denote that the repository is using revlogs for file storage.
1362 1368
1363 1369 The intent of features is to provide a machine-queryable mechanism
1364 1370 for repo consumers to test for various repository characteristics.
1365 1371
1366 1372 Features are similar to ``requirements``. The main difference is that
1367 1373 requirements are stored on-disk and represent requirements to open the
1368 1374 repository. Features are more run-time capabilities of the repository
1369 1375 and more granular capabilities (which may be derived from requirements).
1370 1376 """)
1371 1377
1372 1378 filtername = interfaceutil.Attribute(
1373 1379 """Name of the repoview that is active on this repo.""")
1374 1380
1375 1381 wvfs = interfaceutil.Attribute(
1376 1382 """VFS used to access the working directory.""")
1377 1383
1378 1384 vfs = interfaceutil.Attribute(
1379 1385 """VFS rooted at the .hg directory.
1380 1386
1381 1387 Used to access repository data not in the store.
1382 1388 """)
1383 1389
1384 1390 svfs = interfaceutil.Attribute(
1385 1391 """VFS rooted at the store.
1386 1392
1387 1393 Used to access repository data in the store. Typically .hg/store.
1388 1394 But can point elsewhere if the store is shared.
1389 1395 """)
1390 1396
1391 1397 root = interfaceutil.Attribute(
1392 1398 """Path to the root of the working directory.""")
1393 1399
1394 1400 path = interfaceutil.Attribute(
1395 1401 """Path to the .hg directory.""")
1396 1402
1397 1403 origroot = interfaceutil.Attribute(
1398 1404 """The filesystem path that was used to construct the repo.""")
1399 1405
1400 1406 auditor = interfaceutil.Attribute(
1401 1407 """A pathauditor for the working directory.
1402 1408
1403 1409 This checks if a path refers to a nested repository.
1404 1410
1405 1411 Operates on the filesystem.
1406 1412 """)
1407 1413
1408 1414 nofsauditor = interfaceutil.Attribute(
1409 1415 """A pathauditor for the working directory.
1410 1416
1411 1417 This is like ``auditor`` except it doesn't do filesystem checks.
1412 1418 """)
1413 1419
1414 1420 baseui = interfaceutil.Attribute(
1415 1421 """Original ui instance passed into constructor.""")
1416 1422
1417 1423 ui = interfaceutil.Attribute(
1418 1424 """Main ui instance for this instance.""")
1419 1425
1420 1426 sharedpath = interfaceutil.Attribute(
1421 1427 """Path to the .hg directory of the repo this repo was shared from.""")
1422 1428
1423 1429 store = interfaceutil.Attribute(
1424 1430 """A store instance.""")
1425 1431
1426 1432 spath = interfaceutil.Attribute(
1427 1433 """Path to the store.""")
1428 1434
1429 1435 sjoin = interfaceutil.Attribute(
1430 1436 """Alias to self.store.join.""")
1431 1437
1432 1438 cachevfs = interfaceutil.Attribute(
1433 1439 """A VFS used to access the cache directory.
1434 1440
1435 1441 Typically .hg/cache.
1436 1442 """)
1437 1443
1438 1444 wcachevfs = interfaceutil.Attribute(
1439 1445 """A VFS used to access the cache directory dedicated to working copy
1440 1446
1441 1447 Typically .hg/wcache.
1442 1448 """)
1443 1449
1444 1450 filteredrevcache = interfaceutil.Attribute(
1445 1451 """Holds sets of revisions to be filtered.""")
1446 1452
1447 1453 names = interfaceutil.Attribute(
1448 1454 """A ``namespaces`` instance.""")
1449 1455
1450 1456 def close():
1451 1457 """Close the handle on this repository."""
1452 1458
1453 1459 def peer():
1454 1460 """Obtain an object conforming to the ``peer`` interface."""
1455 1461
1456 1462 def unfiltered():
1457 1463 """Obtain an unfiltered/raw view of this repo."""
1458 1464
1459 1465 def filtered(name, visibilityexceptions=None):
1460 1466 """Obtain a named view of this repository."""
1461 1467
1462 1468 obsstore = interfaceutil.Attribute(
1463 1469 """A store of obsolescence data.""")
1464 1470
1465 1471 changelog = interfaceutil.Attribute(
1466 1472 """A handle on the changelog revlog.""")
1467 1473
1468 1474 manifestlog = interfaceutil.Attribute(
1469 1475 """An instance conforming to the ``imanifestlog`` interface.
1470 1476
1471 1477 Provides access to manifests for the repository.
1472 1478 """)
1473 1479
1474 1480 dirstate = interfaceutil.Attribute(
1475 1481 """Working directory state.""")
1476 1482
1477 1483 narrowpats = interfaceutil.Attribute(
1478 1484 """Matcher patterns for this repository's narrowspec.""")
1479 1485
1480 1486 def narrowmatch(match=None, includeexact=False):
1481 1487 """Obtain a matcher for the narrowspec."""
1482 1488
1483 1489 def setnarrowpats(newincludes, newexcludes):
1484 1490 """Define the narrowspec for this repository."""
1485 1491
1486 1492 def __getitem__(changeid):
1487 1493 """Try to resolve a changectx."""
1488 1494
1489 1495 def __contains__(changeid):
1490 1496 """Whether a changeset exists."""
1491 1497
1492 1498 def __nonzero__():
1493 1499 """Always returns True."""
1494 1500 return True
1495 1501
1496 1502 __bool__ = __nonzero__
1497 1503
1498 1504 def __len__():
1499 1505 """Returns the number of changesets in the repo."""
1500 1506
1501 1507 def __iter__():
1502 1508 """Iterate over revisions in the changelog."""
1503 1509
1504 1510 def revs(expr, *args):
1505 1511 """Evaluate a revset.
1506 1512
1507 1513 Emits revisions.
1508 1514 """
1509 1515
1510 1516 def set(expr, *args):
1511 1517 """Evaluate a revset.
1512 1518
1513 1519 Emits changectx instances.
1514 1520 """
1515 1521
1516 1522 def anyrevs(specs, user=False, localalias=None):
1517 1523 """Find revisions matching one of the given revsets."""
1518 1524
1519 1525 def url():
1520 1526 """Returns a string representing the location of this repo."""
1521 1527
1522 1528 def hook(name, throw=False, **args):
1523 1529 """Call a hook."""
1524 1530
1525 1531 def tags():
1526 1532 """Return a mapping of tag to node."""
1527 1533
1528 1534 def tagtype(tagname):
1529 1535 """Return the type of a given tag."""
1530 1536
1531 1537 def tagslist():
1532 1538 """Return a list of tags ordered by revision."""
1533 1539
1534 1540 def nodetags(node):
1535 1541 """Return the tags associated with a node."""
1536 1542
1537 1543 def nodebookmarks(node):
1538 1544 """Return the list of bookmarks pointing to the specified node."""
1539 1545
1540 1546 def branchmap():
1541 1547 """Return a mapping of branch to heads in that branch."""
1542 1548
1543 1549 def revbranchcache():
1544 1550 pass
1545 1551
1546 1552 def branchtip(branchtip, ignoremissing=False):
1547 1553 """Return the tip node for a given branch."""
1548 1554
1549 1555 def lookup(key):
1550 1556 """Resolve the node for a revision."""
1551 1557
1552 1558 def lookupbranch(key):
1553 1559 """Look up the branch name of the given revision or branch name."""
1554 1560
1555 1561 def known(nodes):
1556 1562 """Determine whether a series of nodes is known.
1557 1563
1558 1564 Returns a list of bools.
1559 1565 """
1560 1566
1561 1567 def local():
1562 1568 """Whether the repository is local."""
1563 1569 return True
1564 1570
1565 1571 def publishing():
1566 1572 """Whether the repository is a publishing repository."""
1567 1573
1568 1574 def cancopy():
1569 1575 pass
1570 1576
1571 1577 def shared():
1572 1578 """The type of shared repository or None."""
1573 1579
1574 1580 def wjoin(f, *insidef):
1575 1581 """Calls self.vfs.reljoin(self.root, f, *insidef)"""
1576 1582
1577 1583 def setparents(p1, p2):
1578 1584 """Set the parent nodes of the working directory."""
1579 1585
1580 1586 def filectx(path, changeid=None, fileid=None):
1581 1587 """Obtain a filectx for the given file revision."""
1582 1588
1583 1589 def getcwd():
1584 1590 """Obtain the current working directory from the dirstate."""
1585 1591
1586 1592 def pathto(f, cwd=None):
1587 1593 """Obtain the relative path to a file."""
1588 1594
1589 1595 def adddatafilter(name, fltr):
1590 1596 pass
1591 1597
1592 1598 def wread(filename):
1593 1599 """Read a file from wvfs, using data filters."""
1594 1600
1595 1601 def wwrite(filename, data, flags, backgroundclose=False, **kwargs):
1596 1602 """Write data to a file in the wvfs, using data filters."""
1597 1603
1598 1604 def wwritedata(filename, data):
1599 1605 """Resolve data for writing to the wvfs, using data filters."""
1600 1606
1601 1607 def currenttransaction():
1602 1608 """Obtain the current transaction instance or None."""
1603 1609
1604 1610 def transaction(desc, report=None):
1605 1611 """Open a new transaction to write to the repository."""
1606 1612
1607 1613 def undofiles():
1608 1614 """Returns a list of (vfs, path) for files to undo transactions."""
1609 1615
1610 1616 def recover():
1611 1617 """Roll back an interrupted transaction."""
1612 1618
1613 1619 def rollback(dryrun=False, force=False):
1614 1620 """Undo the last transaction.
1615 1621
1616 1622 DANGEROUS.
1617 1623 """
1618 1624
1619 1625 def updatecaches(tr=None, full=False):
1620 1626 """Warm repo caches."""
1621 1627
1622 1628 def invalidatecaches():
1623 1629 """Invalidate cached data due to the repository mutating."""
1624 1630
1625 1631 def invalidatevolatilesets():
1626 1632 pass
1627 1633
1628 1634 def invalidatedirstate():
1629 1635 """Invalidate the dirstate."""
1630 1636
1631 1637 def invalidate(clearfilecache=False):
1632 1638 pass
1633 1639
1634 1640 def invalidateall():
1635 1641 pass
1636 1642
1637 1643 def lock(wait=True):
1638 1644 """Lock the repository store and return a lock instance."""
1639 1645
1640 1646 def wlock(wait=True):
1641 1647 """Lock the non-store parts of the repository."""
1642 1648
1643 1649 def currentwlock():
1644 1650 """Return the wlock if it's held or None."""
1645 1651
1646 1652 def checkcommitpatterns(wctx, vdirs, match, status, fail):
1647 1653 pass
1648 1654
1649 1655 def commit(text='', user=None, date=None, match=None, force=False,
1650 1656 editor=False, extra=None):
1651 1657 """Add a new revision to the repository."""
1652 1658
1653 1659 def commitctx(ctx, error=False):
1654 1660 """Commit a commitctx instance to the repository."""
1655 1661
1656 1662 def destroying():
1657 1663 """Inform the repository that nodes are about to be destroyed."""
1658 1664
1659 1665 def destroyed():
1660 1666 """Inform the repository that nodes have been destroyed."""
1661 1667
1662 1668 def status(node1='.', node2=None, match=None, ignored=False,
1663 1669 clean=False, unknown=False, listsubrepos=False):
1664 1670 """Convenience method to call repo[x].status()."""
1665 1671
1666 1672 def addpostdsstatus(ps):
1667 1673 pass
1668 1674
1669 1675 def postdsstatus():
1670 1676 pass
1671 1677
1672 1678 def clearpostdsstatus():
1673 1679 pass
1674 1680
1675 1681 def heads(start=None):
1676 1682 """Obtain list of nodes that are DAG heads."""
1677 1683
1678 1684 def branchheads(branch=None, start=None, closed=False):
1679 1685 pass
1680 1686
1681 1687 def branches(nodes):
1682 1688 pass
1683 1689
1684 1690 def between(pairs):
1685 1691 pass
1686 1692
1687 1693 def checkpush(pushop):
1688 1694 pass
1689 1695
1690 1696 prepushoutgoinghooks = interfaceutil.Attribute(
1691 1697 """util.hooks instance.""")
1692 1698
1693 1699 def pushkey(namespace, key, old, new):
1694 1700 pass
1695 1701
1696 1702 def listkeys(namespace):
1697 1703 pass
1698 1704
1699 1705 def debugwireargs(one, two, three=None, four=None, five=None):
1700 1706 pass
1701 1707
1702 1708 def savecommitmessage(text):
1703 1709 pass
1704 1710
1705 1711 class completelocalrepository(ilocalrepositorymain,
1706 1712 ilocalrepositoryfilestorage):
1707 1713 """Complete interface for a local repository."""
1708 1714
1709 1715 class iwireprotocolcommandcacher(interfaceutil.Interface):
1710 1716 """Represents a caching backend for wire protocol commands.
1711 1717
1712 1718 Wire protocol version 2 supports transparent caching of many commands.
1713 1719 To leverage this caching, servers can activate objects that cache
1714 1720 command responses. Objects handle both cache writing and reading.
1715 1721 This interface defines how that response caching mechanism works.
1716 1722
1717 1723 Wire protocol version 2 commands emit a series of objects that are
1718 1724 serialized and sent to the client. The caching layer exists between
1719 1725 the invocation of the command function and the sending of its output
1720 1726 objects to an output layer.
1721 1727
1722 1728 Instances of this interface represent a binding to a cache that
1723 1729 can serve a response (in place of calling a command function) and/or
1724 1730 write responses to a cache for subsequent use.
1725 1731
1726 1732 When a command request arrives, the following happens with regards
1727 1733 to this interface:
1728 1734
1729 1735 1. The server determines whether the command request is cacheable.
1730 1736 2. If it is, an instance of this interface is spawned.
1731 1737 3. The cacher is activated in a context manager (``__enter__`` is called).
1732 1738 4. A cache *key* for that request is derived. This will call the
1733 1739 instance's ``adjustcachekeystate()`` method so the derivation
1734 1740 can be influenced.
1735 1741 5. The cacher is informed of the derived cache key via a call to
1736 1742 ``setcachekey()``.
1737 1743 6. The cacher's ``lookup()`` method is called to test for presence of
1738 1744 the derived key in the cache.
1739 1745 7. If ``lookup()`` returns a hit, that cached result is used in place
1740 1746 of invoking the command function. ``__exit__`` is called and the instance
1741 1747 is discarded.
1742 1748 8. The command function is invoked.
1743 1749 9. ``onobject()`` is called for each object emitted by the command
1744 1750 function.
1745 1751 10. After the final object is seen, ``onfinished()`` is called.
1746 1752 11. ``__exit__`` is called to signal the end of use of the instance.
1747 1753
1748 1754 Cache *key* derivation can be influenced by the instance.
1749 1755
1750 1756 Cache keys are initially derived by a deterministic representation of
1751 1757 the command request. This includes the command name, arguments, protocol
1752 1758 version, etc. This initial key derivation is performed by CBOR-encoding a
1753 1759 data structure and feeding that output into a hasher.
1754 1760
1755 1761 Instances of this interface can influence this initial key derivation
1756 1762 via ``adjustcachekeystate()``.
1757 1763
1758 1764 The instance is informed of the derived cache key via a call to
1759 1765 ``setcachekey()``. The instance must store the key locally so it can
1760 1766 be consulted on subsequent operations that may require it.
1761 1767
1762 1768 When constructed, the instance has access to a callable that can be used
1763 1769 for encoding response objects. This callable receives as its single
1764 1770 argument an object emitted by a command function. It returns an iterable
1765 1771 of bytes chunks representing the encoded object. Unless the cacher is
1766 1772 caching native Python objects in memory or has a way of reconstructing
1767 1773 the original Python objects, implementations typically call this function
1768 1774 to produce bytes from the output objects and then store those bytes in
1769 1775 the cache. When it comes time to re-emit those bytes, they are wrapped
1770 1776 in a ``wireprototypes.encodedresponse`` instance to tell the output
1771 1777 layer that they are pre-encoded.
1772 1778
1773 1779 When receiving the objects emitted by the command function, instances
1774 1780 can choose what to do with those objects. The simplest thing to do is
1775 1781 re-emit the original objects. They will be forwarded to the output
1776 1782 layer and will be processed as if the cacher did not exist.
1777 1783
1778 1784 Implementations could also choose to not emit objects - instead locally
1779 1785 buffering objects or their encoded representation. They could then emit
1780 1786 a single "coalesced" object when ``onfinished()`` is called. In
1781 1787 this way, the implementation would function as a filtering layer of
1782 1788 sorts.
1783 1789
1784 1790 When caching objects, typically the encoded form of the object will
1785 1791 be stored. Keep in mind that if the original object is forwarded to
1786 1792 the output layer, it will need to be encoded there as well. For large
1787 1793 output, this redundant encoding could add overhead. Implementations
1788 1794 could wrap the encoded object data in ``wireprototypes.encodedresponse``
1789 1795 instances to avoid this overhead.
1790 1796 """
1791 1797 def __enter__():
1792 1798 """Marks the instance as active.
1793 1799
1794 1800 Should return self.
1795 1801 """
1796 1802
1797 1803 def __exit__(exctype, excvalue, exctb):
1798 1804 """Called when cacher is no longer used.
1799 1805
1800 1806 This can be used by implementations to perform cleanup actions (e.g.
1801 1807 disconnecting network sockets, aborting a partially cached response.
1802 1808 """
1803 1809
1804 1810 def adjustcachekeystate(state):
1805 1811 """Influences cache key derivation by adjusting state to derive key.
1806 1812
1807 1813 A dict defining the state used to derive the cache key is passed.
1808 1814
1809 1815 Implementations can modify this dict to record additional state that
1810 1816 is wanted to influence key derivation.
1811 1817
1812 1818 Implementations are *highly* encouraged to not modify or delete
1813 1819 existing keys.
1814 1820 """
1815 1821
1816 1822 def setcachekey(key):
1817 1823 """Record the derived cache key for this request.
1818 1824
1819 1825 Instances may mutate the key for internal usage, as desired. e.g.
1820 1826 instances may wish to prepend the repo name, introduce path
1821 1827 components for filesystem or URL addressing, etc. Behavior is up to
1822 1828 the cache.
1823 1829
1824 1830 Returns a bool indicating if the request is cacheable by this
1825 1831 instance.
1826 1832 """
1827 1833
1828 1834 def lookup():
1829 1835 """Attempt to resolve an entry in the cache.
1830 1836
1831 1837 The instance is instructed to look for the cache key that it was
1832 1838 informed about via the call to ``setcachekey()``.
1833 1839
1834 1840 If there's no cache hit or the cacher doesn't wish to use the cached
1835 1841 entry, ``None`` should be returned.
1836 1842
1837 1843 Else, a dict defining the cached result should be returned. The
1838 1844 dict may have the following keys:
1839 1845
1840 1846 objs
1841 1847 An iterable of objects that should be sent to the client. That
1842 1848 iterable of objects is expected to be what the command function
1843 1849 would return if invoked or an equivalent representation thereof.
1844 1850 """
1845 1851
1846 1852 def onobject(obj):
1847 1853 """Called when a new object is emitted from the command function.
1848 1854
1849 1855 Receives as its argument the object that was emitted from the
1850 1856 command function.
1851 1857
1852 1858 This method returns an iterator of objects to forward to the output
1853 1859 layer. The easiest implementation is a generator that just
1854 1860 ``yield obj``.
1855 1861 """
1856 1862
1857 1863 def onfinished():
1858 1864 """Called after all objects have been emitted from the command function.
1859 1865
1860 1866 Implementations should return an iterator of objects to forward to
1861 1867 the output layer.
1862 1868
1863 1869 This method can be a generator.
1864 1870 """
General Comments 0
You need to be logged in to leave comments. Login now