##// END OF EJS Templates
bundle2: add generic debug output regarding generated interruption...
Pierre-Yves David -
r25324:deed9c6b default
parent child Browse files
Show More
@@ -1,1316 +1,1318 b''
1 # bundle2.py - generic container format to transmit arbitrary data.
1 # bundle2.py - generic container format to transmit arbitrary data.
2 #
2 #
3 # Copyright 2013 Facebook, Inc.
3 # Copyright 2013 Facebook, Inc.
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7 """Handling of the new bundle2 format
7 """Handling of the new bundle2 format
8
8
9 The goal of bundle2 is to act as an atomically packet to transmit a set of
9 The goal of bundle2 is to act as an atomically packet to transmit a set of
10 payloads in an application agnostic way. It consist in a sequence of "parts"
10 payloads in an application agnostic way. It consist in a sequence of "parts"
11 that will be handed to and processed by the application layer.
11 that will be handed to and processed by the application layer.
12
12
13
13
14 General format architecture
14 General format architecture
15 ===========================
15 ===========================
16
16
17 The format is architectured as follow
17 The format is architectured as follow
18
18
19 - magic string
19 - magic string
20 - stream level parameters
20 - stream level parameters
21 - payload parts (any number)
21 - payload parts (any number)
22 - end of stream marker.
22 - end of stream marker.
23
23
24 the Binary format
24 the Binary format
25 ============================
25 ============================
26
26
27 All numbers are unsigned and big-endian.
27 All numbers are unsigned and big-endian.
28
28
29 stream level parameters
29 stream level parameters
30 ------------------------
30 ------------------------
31
31
32 Binary format is as follow
32 Binary format is as follow
33
33
34 :params size: int32
34 :params size: int32
35
35
36 The total number of Bytes used by the parameters
36 The total number of Bytes used by the parameters
37
37
38 :params value: arbitrary number of Bytes
38 :params value: arbitrary number of Bytes
39
39
40 A blob of `params size` containing the serialized version of all stream level
40 A blob of `params size` containing the serialized version of all stream level
41 parameters.
41 parameters.
42
42
43 The blob contains a space separated list of parameters. Parameters with value
43 The blob contains a space separated list of parameters. Parameters with value
44 are stored in the form `<name>=<value>`. Both name and value are urlquoted.
44 are stored in the form `<name>=<value>`. Both name and value are urlquoted.
45
45
46 Empty name are obviously forbidden.
46 Empty name are obviously forbidden.
47
47
48 Name MUST start with a letter. If this first letter is lower case, the
48 Name MUST start with a letter. If this first letter is lower case, the
49 parameter is advisory and can be safely ignored. However when the first
49 parameter is advisory and can be safely ignored. However when the first
50 letter is capital, the parameter is mandatory and the bundling process MUST
50 letter is capital, the parameter is mandatory and the bundling process MUST
51 stop if he is not able to proceed it.
51 stop if he is not able to proceed it.
52
52
53 Stream parameters use a simple textual format for two main reasons:
53 Stream parameters use a simple textual format for two main reasons:
54
54
55 - Stream level parameters should remain simple and we want to discourage any
55 - Stream level parameters should remain simple and we want to discourage any
56 crazy usage.
56 crazy usage.
57 - Textual data allow easy human inspection of a bundle2 header in case of
57 - Textual data allow easy human inspection of a bundle2 header in case of
58 troubles.
58 troubles.
59
59
60 Any Applicative level options MUST go into a bundle2 part instead.
60 Any Applicative level options MUST go into a bundle2 part instead.
61
61
62 Payload part
62 Payload part
63 ------------------------
63 ------------------------
64
64
65 Binary format is as follow
65 Binary format is as follow
66
66
67 :header size: int32
67 :header size: int32
68
68
69 The total number of Bytes used by the part headers. When the header is empty
69 The total number of Bytes used by the part headers. When the header is empty
70 (size = 0) this is interpreted as the end of stream marker.
70 (size = 0) this is interpreted as the end of stream marker.
71
71
72 :header:
72 :header:
73
73
74 The header defines how to interpret the part. It contains two piece of
74 The header defines how to interpret the part. It contains two piece of
75 data: the part type, and the part parameters.
75 data: the part type, and the part parameters.
76
76
77 The part type is used to route an application level handler, that can
77 The part type is used to route an application level handler, that can
78 interpret payload.
78 interpret payload.
79
79
80 Part parameters are passed to the application level handler. They are
80 Part parameters are passed to the application level handler. They are
81 meant to convey information that will help the application level object to
81 meant to convey information that will help the application level object to
82 interpret the part payload.
82 interpret the part payload.
83
83
84 The binary format of the header is has follow
84 The binary format of the header is has follow
85
85
86 :typesize: (one byte)
86 :typesize: (one byte)
87
87
88 :parttype: alphanumerical part name (restricted to [a-zA-Z0-9_:-]*)
88 :parttype: alphanumerical part name (restricted to [a-zA-Z0-9_:-]*)
89
89
90 :partid: A 32bits integer (unique in the bundle) that can be used to refer
90 :partid: A 32bits integer (unique in the bundle) that can be used to refer
91 to this part.
91 to this part.
92
92
93 :parameters:
93 :parameters:
94
94
95 Part's parameter may have arbitrary content, the binary structure is::
95 Part's parameter may have arbitrary content, the binary structure is::
96
96
97 <mandatory-count><advisory-count><param-sizes><param-data>
97 <mandatory-count><advisory-count><param-sizes><param-data>
98
98
99 :mandatory-count: 1 byte, number of mandatory parameters
99 :mandatory-count: 1 byte, number of mandatory parameters
100
100
101 :advisory-count: 1 byte, number of advisory parameters
101 :advisory-count: 1 byte, number of advisory parameters
102
102
103 :param-sizes:
103 :param-sizes:
104
104
105 N couple of bytes, where N is the total number of parameters. Each
105 N couple of bytes, where N is the total number of parameters. Each
106 couple contains (<size-of-key>, <size-of-value) for one parameter.
106 couple contains (<size-of-key>, <size-of-value) for one parameter.
107
107
108 :param-data:
108 :param-data:
109
109
110 A blob of bytes from which each parameter key and value can be
110 A blob of bytes from which each parameter key and value can be
111 retrieved using the list of size couples stored in the previous
111 retrieved using the list of size couples stored in the previous
112 field.
112 field.
113
113
114 Mandatory parameters comes first, then the advisory ones.
114 Mandatory parameters comes first, then the advisory ones.
115
115
116 Each parameter's key MUST be unique within the part.
116 Each parameter's key MUST be unique within the part.
117
117
118 :payload:
118 :payload:
119
119
120 payload is a series of `<chunksize><chunkdata>`.
120 payload is a series of `<chunksize><chunkdata>`.
121
121
122 `chunksize` is an int32, `chunkdata` are plain bytes (as much as
122 `chunksize` is an int32, `chunkdata` are plain bytes (as much as
123 `chunksize` says)` The payload part is concluded by a zero size chunk.
123 `chunksize` says)` The payload part is concluded by a zero size chunk.
124
124
125 The current implementation always produces either zero or one chunk.
125 The current implementation always produces either zero or one chunk.
126 This is an implementation limitation that will ultimately be lifted.
126 This is an implementation limitation that will ultimately be lifted.
127
127
128 `chunksize` can be negative to trigger special case processing. No such
128 `chunksize` can be negative to trigger special case processing. No such
129 processing is in place yet.
129 processing is in place yet.
130
130
131 Bundle processing
131 Bundle processing
132 ============================
132 ============================
133
133
134 Each part is processed in order using a "part handler". Handler are registered
134 Each part is processed in order using a "part handler". Handler are registered
135 for a certain part type.
135 for a certain part type.
136
136
137 The matching of a part to its handler is case insensitive. The case of the
137 The matching of a part to its handler is case insensitive. The case of the
138 part type is used to know if a part is mandatory or advisory. If the Part type
138 part type is used to know if a part is mandatory or advisory. If the Part type
139 contains any uppercase char it is considered mandatory. When no handler is
139 contains any uppercase char it is considered mandatory. When no handler is
140 known for a Mandatory part, the process is aborted and an exception is raised.
140 known for a Mandatory part, the process is aborted and an exception is raised.
141 If the part is advisory and no handler is known, the part is ignored. When the
141 If the part is advisory and no handler is known, the part is ignored. When the
142 process is aborted, the full bundle is still read from the stream to keep the
142 process is aborted, the full bundle is still read from the stream to keep the
143 channel usable. But none of the part read from an abort are processed. In the
143 channel usable. But none of the part read from an abort are processed. In the
144 future, dropping the stream may become an option for channel we do not care to
144 future, dropping the stream may become an option for channel we do not care to
145 preserve.
145 preserve.
146 """
146 """
147
147
148 import errno
148 import errno
149 import sys
149 import sys
150 import util
150 import util
151 import struct
151 import struct
152 import urllib
152 import urllib
153 import string
153 import string
154 import obsolete
154 import obsolete
155 import pushkey
155 import pushkey
156 import url
156 import url
157 import re
157 import re
158
158
159 import changegroup, error
159 import changegroup, error
160 from i18n import _
160 from i18n import _
161
161
162 _pack = struct.pack
162 _pack = struct.pack
163 _unpack = struct.unpack
163 _unpack = struct.unpack
164
164
165 _fstreamparamsize = '>i'
165 _fstreamparamsize = '>i'
166 _fpartheadersize = '>i'
166 _fpartheadersize = '>i'
167 _fparttypesize = '>B'
167 _fparttypesize = '>B'
168 _fpartid = '>I'
168 _fpartid = '>I'
169 _fpayloadsize = '>i'
169 _fpayloadsize = '>i'
170 _fpartparamcount = '>BB'
170 _fpartparamcount = '>BB'
171
171
172 preferedchunksize = 4096
172 preferedchunksize = 4096
173
173
174 _parttypeforbidden = re.compile('[^a-zA-Z0-9_:-]')
174 _parttypeforbidden = re.compile('[^a-zA-Z0-9_:-]')
175
175
176 def outdebug(ui, message):
176 def outdebug(ui, message):
177 """debug regarding output stream (bundling)"""
177 """debug regarding output stream (bundling)"""
178 ui.debug('bundle2-output: %s\n' % message)
178 ui.debug('bundle2-output: %s\n' % message)
179
179
180 def indebug(ui, message):
180 def indebug(ui, message):
181 """debug on input stream (unbundling)"""
181 """debug on input stream (unbundling)"""
182 ui.debug('bundle2-input: %s\n' % message)
182 ui.debug('bundle2-input: %s\n' % message)
183
183
184 def validateparttype(parttype):
184 def validateparttype(parttype):
185 """raise ValueError if a parttype contains invalid character"""
185 """raise ValueError if a parttype contains invalid character"""
186 if _parttypeforbidden.search(parttype):
186 if _parttypeforbidden.search(parttype):
187 raise ValueError(parttype)
187 raise ValueError(parttype)
188
188
189 def _makefpartparamsizes(nbparams):
189 def _makefpartparamsizes(nbparams):
190 """return a struct format to read part parameter sizes
190 """return a struct format to read part parameter sizes
191
191
192 The number parameters is variable so we need to build that format
192 The number parameters is variable so we need to build that format
193 dynamically.
193 dynamically.
194 """
194 """
195 return '>'+('BB'*nbparams)
195 return '>'+('BB'*nbparams)
196
196
197 parthandlermapping = {}
197 parthandlermapping = {}
198
198
199 def parthandler(parttype, params=()):
199 def parthandler(parttype, params=()):
200 """decorator that register a function as a bundle2 part handler
200 """decorator that register a function as a bundle2 part handler
201
201
202 eg::
202 eg::
203
203
204 @parthandler('myparttype', ('mandatory', 'param', 'handled'))
204 @parthandler('myparttype', ('mandatory', 'param', 'handled'))
205 def myparttypehandler(...):
205 def myparttypehandler(...):
206 '''process a part of type "my part".'''
206 '''process a part of type "my part".'''
207 ...
207 ...
208 """
208 """
209 validateparttype(parttype)
209 validateparttype(parttype)
210 def _decorator(func):
210 def _decorator(func):
211 lparttype = parttype.lower() # enforce lower case matching.
211 lparttype = parttype.lower() # enforce lower case matching.
212 assert lparttype not in parthandlermapping
212 assert lparttype not in parthandlermapping
213 parthandlermapping[lparttype] = func
213 parthandlermapping[lparttype] = func
214 func.params = frozenset(params)
214 func.params = frozenset(params)
215 return func
215 return func
216 return _decorator
216 return _decorator
217
217
218 class unbundlerecords(object):
218 class unbundlerecords(object):
219 """keep record of what happens during and unbundle
219 """keep record of what happens during and unbundle
220
220
221 New records are added using `records.add('cat', obj)`. Where 'cat' is a
221 New records are added using `records.add('cat', obj)`. Where 'cat' is a
222 category of record and obj is an arbitrary object.
222 category of record and obj is an arbitrary object.
223
223
224 `records['cat']` will return all entries of this category 'cat'.
224 `records['cat']` will return all entries of this category 'cat'.
225
225
226 Iterating on the object itself will yield `('category', obj)` tuples
226 Iterating on the object itself will yield `('category', obj)` tuples
227 for all entries.
227 for all entries.
228
228
229 All iterations happens in chronological order.
229 All iterations happens in chronological order.
230 """
230 """
231
231
232 def __init__(self):
232 def __init__(self):
233 self._categories = {}
233 self._categories = {}
234 self._sequences = []
234 self._sequences = []
235 self._replies = {}
235 self._replies = {}
236
236
237 def add(self, category, entry, inreplyto=None):
237 def add(self, category, entry, inreplyto=None):
238 """add a new record of a given category.
238 """add a new record of a given category.
239
239
240 The entry can then be retrieved in the list returned by
240 The entry can then be retrieved in the list returned by
241 self['category']."""
241 self['category']."""
242 self._categories.setdefault(category, []).append(entry)
242 self._categories.setdefault(category, []).append(entry)
243 self._sequences.append((category, entry))
243 self._sequences.append((category, entry))
244 if inreplyto is not None:
244 if inreplyto is not None:
245 self.getreplies(inreplyto).add(category, entry)
245 self.getreplies(inreplyto).add(category, entry)
246
246
247 def getreplies(self, partid):
247 def getreplies(self, partid):
248 """get the records that are replies to a specific part"""
248 """get the records that are replies to a specific part"""
249 return self._replies.setdefault(partid, unbundlerecords())
249 return self._replies.setdefault(partid, unbundlerecords())
250
250
251 def __getitem__(self, cat):
251 def __getitem__(self, cat):
252 return tuple(self._categories.get(cat, ()))
252 return tuple(self._categories.get(cat, ()))
253
253
254 def __iter__(self):
254 def __iter__(self):
255 return iter(self._sequences)
255 return iter(self._sequences)
256
256
257 def __len__(self):
257 def __len__(self):
258 return len(self._sequences)
258 return len(self._sequences)
259
259
260 def __nonzero__(self):
260 def __nonzero__(self):
261 return bool(self._sequences)
261 return bool(self._sequences)
262
262
263 class bundleoperation(object):
263 class bundleoperation(object):
264 """an object that represents a single bundling process
264 """an object that represents a single bundling process
265
265
266 Its purpose is to carry unbundle-related objects and states.
266 Its purpose is to carry unbundle-related objects and states.
267
267
268 A new object should be created at the beginning of each bundle processing.
268 A new object should be created at the beginning of each bundle processing.
269 The object is to be returned by the processing function.
269 The object is to be returned by the processing function.
270
270
271 The object has very little content now it will ultimately contain:
271 The object has very little content now it will ultimately contain:
272 * an access to the repo the bundle is applied to,
272 * an access to the repo the bundle is applied to,
273 * a ui object,
273 * a ui object,
274 * a way to retrieve a transaction to add changes to the repo,
274 * a way to retrieve a transaction to add changes to the repo,
275 * a way to record the result of processing each part,
275 * a way to record the result of processing each part,
276 * a way to construct a bundle response when applicable.
276 * a way to construct a bundle response when applicable.
277 """
277 """
278
278
279 def __init__(self, repo, transactiongetter, captureoutput=True):
279 def __init__(self, repo, transactiongetter, captureoutput=True):
280 self.repo = repo
280 self.repo = repo
281 self.ui = repo.ui
281 self.ui = repo.ui
282 self.records = unbundlerecords()
282 self.records = unbundlerecords()
283 self.gettransaction = transactiongetter
283 self.gettransaction = transactiongetter
284 self.reply = None
284 self.reply = None
285 self.captureoutput = captureoutput
285 self.captureoutput = captureoutput
286
286
287 class TransactionUnavailable(RuntimeError):
287 class TransactionUnavailable(RuntimeError):
288 pass
288 pass
289
289
290 def _notransaction():
290 def _notransaction():
291 """default method to get a transaction while processing a bundle
291 """default method to get a transaction while processing a bundle
292
292
293 Raise an exception to highlight the fact that no transaction was expected
293 Raise an exception to highlight the fact that no transaction was expected
294 to be created"""
294 to be created"""
295 raise TransactionUnavailable()
295 raise TransactionUnavailable()
296
296
297 def processbundle(repo, unbundler, transactiongetter=None, op=None):
297 def processbundle(repo, unbundler, transactiongetter=None, op=None):
298 """This function process a bundle, apply effect to/from a repo
298 """This function process a bundle, apply effect to/from a repo
299
299
300 It iterates over each part then searches for and uses the proper handling
300 It iterates over each part then searches for and uses the proper handling
301 code to process the part. Parts are processed in order.
301 code to process the part. Parts are processed in order.
302
302
303 This is very early version of this function that will be strongly reworked
303 This is very early version of this function that will be strongly reworked
304 before final usage.
304 before final usage.
305
305
306 Unknown Mandatory part will abort the process.
306 Unknown Mandatory part will abort the process.
307
307
308 It is temporarily possible to provide a prebuilt bundleoperation to the
308 It is temporarily possible to provide a prebuilt bundleoperation to the
309 function. This is used to ensure output is properly propagated in case of
309 function. This is used to ensure output is properly propagated in case of
310 an error during the unbundling. This output capturing part will likely be
310 an error during the unbundling. This output capturing part will likely be
311 reworked and this ability will probably go away in the process.
311 reworked and this ability will probably go away in the process.
312 """
312 """
313 if op is None:
313 if op is None:
314 if transactiongetter is None:
314 if transactiongetter is None:
315 transactiongetter = _notransaction
315 transactiongetter = _notransaction
316 op = bundleoperation(repo, transactiongetter)
316 op = bundleoperation(repo, transactiongetter)
317 # todo:
317 # todo:
318 # - replace this is a init function soon.
318 # - replace this is a init function soon.
319 # - exception catching
319 # - exception catching
320 unbundler.params
320 unbundler.params
321 iterparts = unbundler.iterparts()
321 iterparts = unbundler.iterparts()
322 part = None
322 part = None
323 try:
323 try:
324 for part in iterparts:
324 for part in iterparts:
325 _processpart(op, part)
325 _processpart(op, part)
326 except BaseException, exc:
326 except BaseException, exc:
327 for part in iterparts:
327 for part in iterparts:
328 # consume the bundle content
328 # consume the bundle content
329 part.seek(0, 2)
329 part.seek(0, 2)
330 # Small hack to let caller code distinguish exceptions from bundle2
330 # Small hack to let caller code distinguish exceptions from bundle2
331 # processing from processing the old format. This is mostly
331 # processing from processing the old format. This is mostly
332 # needed to handle different return codes to unbundle according to the
332 # needed to handle different return codes to unbundle according to the
333 # type of bundle. We should probably clean up or drop this return code
333 # type of bundle. We should probably clean up or drop this return code
334 # craziness in a future version.
334 # craziness in a future version.
335 exc.duringunbundle2 = True
335 exc.duringunbundle2 = True
336 salvaged = []
336 salvaged = []
337 if op.reply is not None:
337 if op.reply is not None:
338 salvaged = op.reply.salvageoutput()
338 salvaged = op.reply.salvageoutput()
339 exc._bundle2salvagedoutput = salvaged
339 exc._bundle2salvagedoutput = salvaged
340 raise
340 raise
341 return op
341 return op
342
342
343 def _processpart(op, part):
343 def _processpart(op, part):
344 """process a single part from a bundle
344 """process a single part from a bundle
345
345
346 The part is guaranteed to have been fully consumed when the function exits
346 The part is guaranteed to have been fully consumed when the function exits
347 (even if an exception is raised)."""
347 (even if an exception is raised)."""
348 try:
348 try:
349 try:
349 try:
350 handler = parthandlermapping.get(part.type)
350 handler = parthandlermapping.get(part.type)
351 if handler is None:
351 if handler is None:
352 raise error.UnsupportedPartError(parttype=part.type)
352 raise error.UnsupportedPartError(parttype=part.type)
353 indebug(op.ui, 'found a handler for part %r' % part.type)
353 indebug(op.ui, 'found a handler for part %r' % part.type)
354 unknownparams = part.mandatorykeys - handler.params
354 unknownparams = part.mandatorykeys - handler.params
355 if unknownparams:
355 if unknownparams:
356 unknownparams = list(unknownparams)
356 unknownparams = list(unknownparams)
357 unknownparams.sort()
357 unknownparams.sort()
358 raise error.UnsupportedPartError(parttype=part.type,
358 raise error.UnsupportedPartError(parttype=part.type,
359 params=unknownparams)
359 params=unknownparams)
360 except error.UnsupportedPartError, exc:
360 except error.UnsupportedPartError, exc:
361 if part.mandatory: # mandatory parts
361 if part.mandatory: # mandatory parts
362 raise
362 raise
363 indebug(op.ui, 'ignoring unsupported advisory part %s' % exc)
363 indebug(op.ui, 'ignoring unsupported advisory part %s' % exc)
364 return # skip to part processing
364 return # skip to part processing
365
365
366 # handler is called outside the above try block so that we don't
366 # handler is called outside the above try block so that we don't
367 # risk catching KeyErrors from anything other than the
367 # risk catching KeyErrors from anything other than the
368 # parthandlermapping lookup (any KeyError raised by handler()
368 # parthandlermapping lookup (any KeyError raised by handler()
369 # itself represents a defect of a different variety).
369 # itself represents a defect of a different variety).
370 output = None
370 output = None
371 if op.captureoutput and op.reply is not None:
371 if op.captureoutput and op.reply is not None:
372 op.ui.pushbuffer(error=True, subproc=True)
372 op.ui.pushbuffer(error=True, subproc=True)
373 output = ''
373 output = ''
374 try:
374 try:
375 handler(op, part)
375 handler(op, part)
376 finally:
376 finally:
377 if output is not None:
377 if output is not None:
378 output = op.ui.popbuffer()
378 output = op.ui.popbuffer()
379 if output:
379 if output:
380 outpart = op.reply.newpart('output', data=output,
380 outpart = op.reply.newpart('output', data=output,
381 mandatory=False)
381 mandatory=False)
382 outpart.addparam('in-reply-to', str(part.id), mandatory=False)
382 outpart.addparam('in-reply-to', str(part.id), mandatory=False)
383 finally:
383 finally:
384 # consume the part content to not corrupt the stream.
384 # consume the part content to not corrupt the stream.
385 part.seek(0, 2)
385 part.seek(0, 2)
386
386
387
387
388 def decodecaps(blob):
388 def decodecaps(blob):
389 """decode a bundle2 caps bytes blob into a dictionary
389 """decode a bundle2 caps bytes blob into a dictionary
390
390
391 The blob is a list of capabilities (one per line)
391 The blob is a list of capabilities (one per line)
392 Capabilities may have values using a line of the form::
392 Capabilities may have values using a line of the form::
393
393
394 capability=value1,value2,value3
394 capability=value1,value2,value3
395
395
396 The values are always a list."""
396 The values are always a list."""
397 caps = {}
397 caps = {}
398 for line in blob.splitlines():
398 for line in blob.splitlines():
399 if not line:
399 if not line:
400 continue
400 continue
401 if '=' not in line:
401 if '=' not in line:
402 key, vals = line, ()
402 key, vals = line, ()
403 else:
403 else:
404 key, vals = line.split('=', 1)
404 key, vals = line.split('=', 1)
405 vals = vals.split(',')
405 vals = vals.split(',')
406 key = urllib.unquote(key)
406 key = urllib.unquote(key)
407 vals = [urllib.unquote(v) for v in vals]
407 vals = [urllib.unquote(v) for v in vals]
408 caps[key] = vals
408 caps[key] = vals
409 return caps
409 return caps
410
410
411 def encodecaps(caps):
411 def encodecaps(caps):
412 """encode a bundle2 caps dictionary into a bytes blob"""
412 """encode a bundle2 caps dictionary into a bytes blob"""
413 chunks = []
413 chunks = []
414 for ca in sorted(caps):
414 for ca in sorted(caps):
415 vals = caps[ca]
415 vals = caps[ca]
416 ca = urllib.quote(ca)
416 ca = urllib.quote(ca)
417 vals = [urllib.quote(v) for v in vals]
417 vals = [urllib.quote(v) for v in vals]
418 if vals:
418 if vals:
419 ca = "%s=%s" % (ca, ','.join(vals))
419 ca = "%s=%s" % (ca, ','.join(vals))
420 chunks.append(ca)
420 chunks.append(ca)
421 return '\n'.join(chunks)
421 return '\n'.join(chunks)
422
422
423 class bundle20(object):
423 class bundle20(object):
424 """represent an outgoing bundle2 container
424 """represent an outgoing bundle2 container
425
425
426 Use the `addparam` method to add stream level parameter. and `newpart` to
426 Use the `addparam` method to add stream level parameter. and `newpart` to
427 populate it. Then call `getchunks` to retrieve all the binary chunks of
427 populate it. Then call `getchunks` to retrieve all the binary chunks of
428 data that compose the bundle2 container."""
428 data that compose the bundle2 container."""
429
429
430 _magicstring = 'HG20'
430 _magicstring = 'HG20'
431
431
432 def __init__(self, ui, capabilities=()):
432 def __init__(self, ui, capabilities=()):
433 self.ui = ui
433 self.ui = ui
434 self._params = []
434 self._params = []
435 self._parts = []
435 self._parts = []
436 self.capabilities = dict(capabilities)
436 self.capabilities = dict(capabilities)
437
437
438 @property
438 @property
439 def nbparts(self):
439 def nbparts(self):
440 """total number of parts added to the bundler"""
440 """total number of parts added to the bundler"""
441 return len(self._parts)
441 return len(self._parts)
442
442
443 # methods used to defines the bundle2 content
443 # methods used to defines the bundle2 content
444 def addparam(self, name, value=None):
444 def addparam(self, name, value=None):
445 """add a stream level parameter"""
445 """add a stream level parameter"""
446 if not name:
446 if not name:
447 raise ValueError('empty parameter name')
447 raise ValueError('empty parameter name')
448 if name[0] not in string.letters:
448 if name[0] not in string.letters:
449 raise ValueError('non letter first character: %r' % name)
449 raise ValueError('non letter first character: %r' % name)
450 self._params.append((name, value))
450 self._params.append((name, value))
451
451
452 def addpart(self, part):
452 def addpart(self, part):
453 """add a new part to the bundle2 container
453 """add a new part to the bundle2 container
454
454
455 Parts contains the actual applicative payload."""
455 Parts contains the actual applicative payload."""
456 assert part.id is None
456 assert part.id is None
457 part.id = len(self._parts) # very cheap counter
457 part.id = len(self._parts) # very cheap counter
458 self._parts.append(part)
458 self._parts.append(part)
459
459
460 def newpart(self, typeid, *args, **kwargs):
460 def newpart(self, typeid, *args, **kwargs):
461 """create a new part and add it to the containers
461 """create a new part and add it to the containers
462
462
463 As the part is directly added to the containers. For now, this means
463 As the part is directly added to the containers. For now, this means
464 that any failure to properly initialize the part after calling
464 that any failure to properly initialize the part after calling
465 ``newpart`` should result in a failure of the whole bundling process.
465 ``newpart`` should result in a failure of the whole bundling process.
466
466
467 You can still fall back to manually create and add if you need better
467 You can still fall back to manually create and add if you need better
468 control."""
468 control."""
469 part = bundlepart(typeid, *args, **kwargs)
469 part = bundlepart(typeid, *args, **kwargs)
470 self.addpart(part)
470 self.addpart(part)
471 return part
471 return part
472
472
473 # methods used to generate the bundle2 stream
473 # methods used to generate the bundle2 stream
474 def getchunks(self):
474 def getchunks(self):
475 if self.ui.debugflag:
475 if self.ui.debugflag:
476 msg = ['bundle2-output-bundle: "%s",' % self._magicstring]
476 msg = ['bundle2-output-bundle: "%s",' % self._magicstring]
477 if self._params:
477 if self._params:
478 msg.append(' (%i params)' % len(self._params))
478 msg.append(' (%i params)' % len(self._params))
479 msg.append(' %i parts total\n' % len(self._parts))
479 msg.append(' %i parts total\n' % len(self._parts))
480 self.ui.debug(''.join(msg))
480 self.ui.debug(''.join(msg))
481 outdebug(self.ui, 'start emission of %s stream' % self._magicstring)
481 outdebug(self.ui, 'start emission of %s stream' % self._magicstring)
482 yield self._magicstring
482 yield self._magicstring
483 param = self._paramchunk()
483 param = self._paramchunk()
484 outdebug(self.ui, 'bundle parameter: %s' % param)
484 outdebug(self.ui, 'bundle parameter: %s' % param)
485 yield _pack(_fstreamparamsize, len(param))
485 yield _pack(_fstreamparamsize, len(param))
486 if param:
486 if param:
487 yield param
487 yield param
488
488
489 outdebug(self.ui, 'start of parts')
489 outdebug(self.ui, 'start of parts')
490 for part in self._parts:
490 for part in self._parts:
491 outdebug(self.ui, 'bundle part: "%s"' % part.type)
491 outdebug(self.ui, 'bundle part: "%s"' % part.type)
492 for chunk in part.getchunks(ui=self.ui):
492 for chunk in part.getchunks(ui=self.ui):
493 yield chunk
493 yield chunk
494 outdebug(self.ui, 'end of bundle')
494 outdebug(self.ui, 'end of bundle')
495 yield _pack(_fpartheadersize, 0)
495 yield _pack(_fpartheadersize, 0)
496
496
497 def _paramchunk(self):
497 def _paramchunk(self):
498 """return a encoded version of all stream parameters"""
498 """return a encoded version of all stream parameters"""
499 blocks = []
499 blocks = []
500 for par, value in self._params:
500 for par, value in self._params:
501 par = urllib.quote(par)
501 par = urllib.quote(par)
502 if value is not None:
502 if value is not None:
503 value = urllib.quote(value)
503 value = urllib.quote(value)
504 par = '%s=%s' % (par, value)
504 par = '%s=%s' % (par, value)
505 blocks.append(par)
505 blocks.append(par)
506 return ' '.join(blocks)
506 return ' '.join(blocks)
507
507
508 def salvageoutput(self):
508 def salvageoutput(self):
509 """return a list with a copy of all output parts in the bundle
509 """return a list with a copy of all output parts in the bundle
510
510
511 This is meant to be used during error handling to make sure we preserve
511 This is meant to be used during error handling to make sure we preserve
512 server output"""
512 server output"""
513 salvaged = []
513 salvaged = []
514 for part in self._parts:
514 for part in self._parts:
515 if part.type.startswith('output'):
515 if part.type.startswith('output'):
516 salvaged.append(part.copy())
516 salvaged.append(part.copy())
517 return salvaged
517 return salvaged
518
518
519
519
520 class unpackermixin(object):
520 class unpackermixin(object):
521 """A mixin to extract bytes and struct data from a stream"""
521 """A mixin to extract bytes and struct data from a stream"""
522
522
523 def __init__(self, fp):
523 def __init__(self, fp):
524 self._fp = fp
524 self._fp = fp
525 self._seekable = (util.safehasattr(fp, 'seek') and
525 self._seekable = (util.safehasattr(fp, 'seek') and
526 util.safehasattr(fp, 'tell'))
526 util.safehasattr(fp, 'tell'))
527
527
528 def _unpack(self, format):
528 def _unpack(self, format):
529 """unpack this struct format from the stream"""
529 """unpack this struct format from the stream"""
530 data = self._readexact(struct.calcsize(format))
530 data = self._readexact(struct.calcsize(format))
531 return _unpack(format, data)
531 return _unpack(format, data)
532
532
533 def _readexact(self, size):
533 def _readexact(self, size):
534 """read exactly <size> bytes from the stream"""
534 """read exactly <size> bytes from the stream"""
535 return changegroup.readexactly(self._fp, size)
535 return changegroup.readexactly(self._fp, size)
536
536
537 def seek(self, offset, whence=0):
537 def seek(self, offset, whence=0):
538 """move the underlying file pointer"""
538 """move the underlying file pointer"""
539 if self._seekable:
539 if self._seekable:
540 return self._fp.seek(offset, whence)
540 return self._fp.seek(offset, whence)
541 else:
541 else:
542 raise NotImplementedError(_('File pointer is not seekable'))
542 raise NotImplementedError(_('File pointer is not seekable'))
543
543
544 def tell(self):
544 def tell(self):
545 """return the file offset, or None if file is not seekable"""
545 """return the file offset, or None if file is not seekable"""
546 if self._seekable:
546 if self._seekable:
547 try:
547 try:
548 return self._fp.tell()
548 return self._fp.tell()
549 except IOError, e:
549 except IOError, e:
550 if e.errno == errno.ESPIPE:
550 if e.errno == errno.ESPIPE:
551 self._seekable = False
551 self._seekable = False
552 else:
552 else:
553 raise
553 raise
554 return None
554 return None
555
555
556 def close(self):
556 def close(self):
557 """close underlying file"""
557 """close underlying file"""
558 if util.safehasattr(self._fp, 'close'):
558 if util.safehasattr(self._fp, 'close'):
559 return self._fp.close()
559 return self._fp.close()
560
560
561 def getunbundler(ui, fp, header=None):
561 def getunbundler(ui, fp, header=None):
562 """return a valid unbundler object for a given header"""
562 """return a valid unbundler object for a given header"""
563 if header is None:
563 if header is None:
564 header = changegroup.readexactly(fp, 4)
564 header = changegroup.readexactly(fp, 4)
565 magic, version = header[0:2], header[2:4]
565 magic, version = header[0:2], header[2:4]
566 if magic != 'HG':
566 if magic != 'HG':
567 raise util.Abort(_('not a Mercurial bundle'))
567 raise util.Abort(_('not a Mercurial bundle'))
568 unbundlerclass = formatmap.get(version)
568 unbundlerclass = formatmap.get(version)
569 if unbundlerclass is None:
569 if unbundlerclass is None:
570 raise util.Abort(_('unknown bundle version %s') % version)
570 raise util.Abort(_('unknown bundle version %s') % version)
571 unbundler = unbundlerclass(ui, fp)
571 unbundler = unbundlerclass(ui, fp)
572 indebug(ui, 'start processing of %s stream' % header)
572 indebug(ui, 'start processing of %s stream' % header)
573 return unbundler
573 return unbundler
574
574
575 class unbundle20(unpackermixin):
575 class unbundle20(unpackermixin):
576 """interpret a bundle2 stream
576 """interpret a bundle2 stream
577
577
578 This class is fed with a binary stream and yields parts through its
578 This class is fed with a binary stream and yields parts through its
579 `iterparts` methods."""
579 `iterparts` methods."""
580
580
581 def __init__(self, ui, fp):
581 def __init__(self, ui, fp):
582 """If header is specified, we do not read it out of the stream."""
582 """If header is specified, we do not read it out of the stream."""
583 self.ui = ui
583 self.ui = ui
584 super(unbundle20, self).__init__(fp)
584 super(unbundle20, self).__init__(fp)
585
585
586 @util.propertycache
586 @util.propertycache
587 def params(self):
587 def params(self):
588 """dictionary of stream level parameters"""
588 """dictionary of stream level parameters"""
589 indebug(self.ui, 'reading bundle2 stream parameters')
589 indebug(self.ui, 'reading bundle2 stream parameters')
590 params = {}
590 params = {}
591 paramssize = self._unpack(_fstreamparamsize)[0]
591 paramssize = self._unpack(_fstreamparamsize)[0]
592 if paramssize < 0:
592 if paramssize < 0:
593 raise error.BundleValueError('negative bundle param size: %i'
593 raise error.BundleValueError('negative bundle param size: %i'
594 % paramssize)
594 % paramssize)
595 if paramssize:
595 if paramssize:
596 for p in self._readexact(paramssize).split(' '):
596 for p in self._readexact(paramssize).split(' '):
597 p = p.split('=', 1)
597 p = p.split('=', 1)
598 p = [urllib.unquote(i) for i in p]
598 p = [urllib.unquote(i) for i in p]
599 if len(p) < 2:
599 if len(p) < 2:
600 p.append(None)
600 p.append(None)
601 self._processparam(*p)
601 self._processparam(*p)
602 params[p[0]] = p[1]
602 params[p[0]] = p[1]
603 return params
603 return params
604
604
605 def _processparam(self, name, value):
605 def _processparam(self, name, value):
606 """process a parameter, applying its effect if needed
606 """process a parameter, applying its effect if needed
607
607
608 Parameter starting with a lower case letter are advisory and will be
608 Parameter starting with a lower case letter are advisory and will be
609 ignored when unknown. Those starting with an upper case letter are
609 ignored when unknown. Those starting with an upper case letter are
610 mandatory and will this function will raise a KeyError when unknown.
610 mandatory and will this function will raise a KeyError when unknown.
611
611
612 Note: no option are currently supported. Any input will be either
612 Note: no option are currently supported. Any input will be either
613 ignored or failing.
613 ignored or failing.
614 """
614 """
615 if not name:
615 if not name:
616 raise ValueError('empty parameter name')
616 raise ValueError('empty parameter name')
617 if name[0] not in string.letters:
617 if name[0] not in string.letters:
618 raise ValueError('non letter first character: %r' % name)
618 raise ValueError('non letter first character: %r' % name)
619 # Some logic will be later added here to try to process the option for
619 # Some logic will be later added here to try to process the option for
620 # a dict of known parameter.
620 # a dict of known parameter.
621 if name[0].islower():
621 if name[0].islower():
622 indebug(self.ui, "ignoring unknown parameter %r" % name)
622 indebug(self.ui, "ignoring unknown parameter %r" % name)
623 else:
623 else:
624 raise error.UnsupportedPartError(params=(name,))
624 raise error.UnsupportedPartError(params=(name,))
625
625
626
626
627 def iterparts(self):
627 def iterparts(self):
628 """yield all parts contained in the stream"""
628 """yield all parts contained in the stream"""
629 # make sure param have been loaded
629 # make sure param have been loaded
630 self.params
630 self.params
631 indebug(self.ui, 'start extraction of bundle2 parts')
631 indebug(self.ui, 'start extraction of bundle2 parts')
632 headerblock = self._readpartheader()
632 headerblock = self._readpartheader()
633 while headerblock is not None:
633 while headerblock is not None:
634 part = unbundlepart(self.ui, headerblock, self._fp)
634 part = unbundlepart(self.ui, headerblock, self._fp)
635 yield part
635 yield part
636 part.seek(0, 2)
636 part.seek(0, 2)
637 headerblock = self._readpartheader()
637 headerblock = self._readpartheader()
638 indebug(self.ui, 'end of bundle2 stream')
638 indebug(self.ui, 'end of bundle2 stream')
639
639
640 def _readpartheader(self):
640 def _readpartheader(self):
641 """reads a part header size and return the bytes blob
641 """reads a part header size and return the bytes blob
642
642
643 returns None if empty"""
643 returns None if empty"""
644 headersize = self._unpack(_fpartheadersize)[0]
644 headersize = self._unpack(_fpartheadersize)[0]
645 if headersize < 0:
645 if headersize < 0:
646 raise error.BundleValueError('negative part header size: %i'
646 raise error.BundleValueError('negative part header size: %i'
647 % headersize)
647 % headersize)
648 indebug(self.ui, 'part header size: %i' % headersize)
648 indebug(self.ui, 'part header size: %i' % headersize)
649 if headersize:
649 if headersize:
650 return self._readexact(headersize)
650 return self._readexact(headersize)
651 return None
651 return None
652
652
653 def compressed(self):
653 def compressed(self):
654 return False
654 return False
655
655
656 formatmap = {'20': unbundle20}
656 formatmap = {'20': unbundle20}
657
657
658 class bundlepart(object):
658 class bundlepart(object):
659 """A bundle2 part contains application level payload
659 """A bundle2 part contains application level payload
660
660
661 The part `type` is used to route the part to the application level
661 The part `type` is used to route the part to the application level
662 handler.
662 handler.
663
663
664 The part payload is contained in ``part.data``. It could be raw bytes or a
664 The part payload is contained in ``part.data``. It could be raw bytes or a
665 generator of byte chunks.
665 generator of byte chunks.
666
666
667 You can add parameters to the part using the ``addparam`` method.
667 You can add parameters to the part using the ``addparam`` method.
668 Parameters can be either mandatory (default) or advisory. Remote side
668 Parameters can be either mandatory (default) or advisory. Remote side
669 should be able to safely ignore the advisory ones.
669 should be able to safely ignore the advisory ones.
670
670
671 Both data and parameters cannot be modified after the generation has begun.
671 Both data and parameters cannot be modified after the generation has begun.
672 """
672 """
673
673
674 def __init__(self, parttype, mandatoryparams=(), advisoryparams=(),
674 def __init__(self, parttype, mandatoryparams=(), advisoryparams=(),
675 data='', mandatory=True):
675 data='', mandatory=True):
676 validateparttype(parttype)
676 validateparttype(parttype)
677 self.id = None
677 self.id = None
678 self.type = parttype
678 self.type = parttype
679 self._data = data
679 self._data = data
680 self._mandatoryparams = list(mandatoryparams)
680 self._mandatoryparams = list(mandatoryparams)
681 self._advisoryparams = list(advisoryparams)
681 self._advisoryparams = list(advisoryparams)
682 # checking for duplicated entries
682 # checking for duplicated entries
683 self._seenparams = set()
683 self._seenparams = set()
684 for pname, __ in self._mandatoryparams + self._advisoryparams:
684 for pname, __ in self._mandatoryparams + self._advisoryparams:
685 if pname in self._seenparams:
685 if pname in self._seenparams:
686 raise RuntimeError('duplicated params: %s' % pname)
686 raise RuntimeError('duplicated params: %s' % pname)
687 self._seenparams.add(pname)
687 self._seenparams.add(pname)
688 # status of the part's generation:
688 # status of the part's generation:
689 # - None: not started,
689 # - None: not started,
690 # - False: currently generated,
690 # - False: currently generated,
691 # - True: generation done.
691 # - True: generation done.
692 self._generated = None
692 self._generated = None
693 self.mandatory = mandatory
693 self.mandatory = mandatory
694
694
695 def copy(self):
695 def copy(self):
696 """return a copy of the part
696 """return a copy of the part
697
697
698 The new part have the very same content but no partid assigned yet.
698 The new part have the very same content but no partid assigned yet.
699 Parts with generated data cannot be copied."""
699 Parts with generated data cannot be copied."""
700 assert not util.safehasattr(self.data, 'next')
700 assert not util.safehasattr(self.data, 'next')
701 return self.__class__(self.type, self._mandatoryparams,
701 return self.__class__(self.type, self._mandatoryparams,
702 self._advisoryparams, self._data, self.mandatory)
702 self._advisoryparams, self._data, self.mandatory)
703
703
704 # methods used to defines the part content
704 # methods used to defines the part content
705 def __setdata(self, data):
705 def __setdata(self, data):
706 if self._generated is not None:
706 if self._generated is not None:
707 raise error.ReadOnlyPartError('part is being generated')
707 raise error.ReadOnlyPartError('part is being generated')
708 self._data = data
708 self._data = data
709 def __getdata(self):
709 def __getdata(self):
710 return self._data
710 return self._data
711 data = property(__getdata, __setdata)
711 data = property(__getdata, __setdata)
712
712
713 @property
713 @property
714 def mandatoryparams(self):
714 def mandatoryparams(self):
715 # make it an immutable tuple to force people through ``addparam``
715 # make it an immutable tuple to force people through ``addparam``
716 return tuple(self._mandatoryparams)
716 return tuple(self._mandatoryparams)
717
717
718 @property
718 @property
719 def advisoryparams(self):
719 def advisoryparams(self):
720 # make it an immutable tuple to force people through ``addparam``
720 # make it an immutable tuple to force people through ``addparam``
721 return tuple(self._advisoryparams)
721 return tuple(self._advisoryparams)
722
722
723 def addparam(self, name, value='', mandatory=True):
723 def addparam(self, name, value='', mandatory=True):
724 if self._generated is not None:
724 if self._generated is not None:
725 raise error.ReadOnlyPartError('part is being generated')
725 raise error.ReadOnlyPartError('part is being generated')
726 if name in self._seenparams:
726 if name in self._seenparams:
727 raise ValueError('duplicated params: %s' % name)
727 raise ValueError('duplicated params: %s' % name)
728 self._seenparams.add(name)
728 self._seenparams.add(name)
729 params = self._advisoryparams
729 params = self._advisoryparams
730 if mandatory:
730 if mandatory:
731 params = self._mandatoryparams
731 params = self._mandatoryparams
732 params.append((name, value))
732 params.append((name, value))
733
733
734 # methods used to generates the bundle2 stream
734 # methods used to generates the bundle2 stream
735 def getchunks(self, ui):
735 def getchunks(self, ui):
736 if self._generated is not None:
736 if self._generated is not None:
737 raise RuntimeError('part can only be consumed once')
737 raise RuntimeError('part can only be consumed once')
738 self._generated = False
738 self._generated = False
739
739
740 if ui.debugflag:
740 if ui.debugflag:
741 msg = ['bundle2-output-part: "%s"' % self.type]
741 msg = ['bundle2-output-part: "%s"' % self.type]
742 if not self.mandatory:
742 if not self.mandatory:
743 msg.append(' (advisory)')
743 msg.append(' (advisory)')
744 nbmp = len(self.mandatoryparams)
744 nbmp = len(self.mandatoryparams)
745 nbap = len(self.advisoryparams)
745 nbap = len(self.advisoryparams)
746 if nbmp or nbap:
746 if nbmp or nbap:
747 msg.append(' (params:')
747 msg.append(' (params:')
748 if nbmp:
748 if nbmp:
749 msg.append(' %i mandatory' % nbmp)
749 msg.append(' %i mandatory' % nbmp)
750 if nbap:
750 if nbap:
751 msg.append(' %i advisory' % nbmp)
751 msg.append(' %i advisory' % nbmp)
752 msg.append(')')
752 msg.append(')')
753 if not self.data:
753 if not self.data:
754 msg.append(' empty payload')
754 msg.append(' empty payload')
755 elif util.safehasattr(self.data, 'next'):
755 elif util.safehasattr(self.data, 'next'):
756 msg.append(' streamed payload')
756 msg.append(' streamed payload')
757 else:
757 else:
758 msg.append(' %i bytes payload' % len(self.data))
758 msg.append(' %i bytes payload' % len(self.data))
759 msg.append('\n')
759 msg.append('\n')
760 ui.debug(''.join(msg))
760 ui.debug(''.join(msg))
761
761
762 #### header
762 #### header
763 if self.mandatory:
763 if self.mandatory:
764 parttype = self.type.upper()
764 parttype = self.type.upper()
765 else:
765 else:
766 parttype = self.type.lower()
766 parttype = self.type.lower()
767 outdebug(ui, 'part %s: "%s"' % (self.id, parttype))
767 outdebug(ui, 'part %s: "%s"' % (self.id, parttype))
768 ## parttype
768 ## parttype
769 header = [_pack(_fparttypesize, len(parttype)),
769 header = [_pack(_fparttypesize, len(parttype)),
770 parttype, _pack(_fpartid, self.id),
770 parttype, _pack(_fpartid, self.id),
771 ]
771 ]
772 ## parameters
772 ## parameters
773 # count
773 # count
774 manpar = self.mandatoryparams
774 manpar = self.mandatoryparams
775 advpar = self.advisoryparams
775 advpar = self.advisoryparams
776 header.append(_pack(_fpartparamcount, len(manpar), len(advpar)))
776 header.append(_pack(_fpartparamcount, len(manpar), len(advpar)))
777 # size
777 # size
778 parsizes = []
778 parsizes = []
779 for key, value in manpar:
779 for key, value in manpar:
780 parsizes.append(len(key))
780 parsizes.append(len(key))
781 parsizes.append(len(value))
781 parsizes.append(len(value))
782 for key, value in advpar:
782 for key, value in advpar:
783 parsizes.append(len(key))
783 parsizes.append(len(key))
784 parsizes.append(len(value))
784 parsizes.append(len(value))
785 paramsizes = _pack(_makefpartparamsizes(len(parsizes) / 2), *parsizes)
785 paramsizes = _pack(_makefpartparamsizes(len(parsizes) / 2), *parsizes)
786 header.append(paramsizes)
786 header.append(paramsizes)
787 # key, value
787 # key, value
788 for key, value in manpar:
788 for key, value in manpar:
789 header.append(key)
789 header.append(key)
790 header.append(value)
790 header.append(value)
791 for key, value in advpar:
791 for key, value in advpar:
792 header.append(key)
792 header.append(key)
793 header.append(value)
793 header.append(value)
794 ## finalize header
794 ## finalize header
795 headerchunk = ''.join(header)
795 headerchunk = ''.join(header)
796 outdebug(ui, 'header chunk size: %i' % len(headerchunk))
796 outdebug(ui, 'header chunk size: %i' % len(headerchunk))
797 yield _pack(_fpartheadersize, len(headerchunk))
797 yield _pack(_fpartheadersize, len(headerchunk))
798 yield headerchunk
798 yield headerchunk
799 ## payload
799 ## payload
800 try:
800 try:
801 for chunk in self._payloadchunks():
801 for chunk in self._payloadchunks():
802 outdebug(ui, 'payload chunk size: %i' % len(chunk))
802 outdebug(ui, 'payload chunk size: %i' % len(chunk))
803 yield _pack(_fpayloadsize, len(chunk))
803 yield _pack(_fpayloadsize, len(chunk))
804 yield chunk
804 yield chunk
805 except BaseException, exc:
805 except BaseException, exc:
806 # backup exception data for later
806 # backup exception data for later
807 ui.debug('bundle2-input-stream-interrupt: encoding exception %s'
808 % exc)
807 exc_info = sys.exc_info()
809 exc_info = sys.exc_info()
808 msg = 'unexpected error: %s' % exc
810 msg = 'unexpected error: %s' % exc
809 interpart = bundlepart('error:abort', [('message', msg)],
811 interpart = bundlepart('error:abort', [('message', msg)],
810 mandatory=False)
812 mandatory=False)
811 interpart.id = 0
813 interpart.id = 0
812 yield _pack(_fpayloadsize, -1)
814 yield _pack(_fpayloadsize, -1)
813 for chunk in interpart.getchunks(ui=ui):
815 for chunk in interpart.getchunks(ui=ui):
814 yield chunk
816 yield chunk
815 outdebug(ui, 'closing payload chunk')
817 outdebug(ui, 'closing payload chunk')
816 # abort current part payload
818 # abort current part payload
817 yield _pack(_fpayloadsize, 0)
819 yield _pack(_fpayloadsize, 0)
818 raise exc_info[0], exc_info[1], exc_info[2]
820 raise exc_info[0], exc_info[1], exc_info[2]
819 # end of payload
821 # end of payload
820 outdebug(ui, 'closing payload chunk')
822 outdebug(ui, 'closing payload chunk')
821 yield _pack(_fpayloadsize, 0)
823 yield _pack(_fpayloadsize, 0)
822 self._generated = True
824 self._generated = True
823
825
824 def _payloadchunks(self):
826 def _payloadchunks(self):
825 """yield chunks of a the part payload
827 """yield chunks of a the part payload
826
828
827 Exists to handle the different methods to provide data to a part."""
829 Exists to handle the different methods to provide data to a part."""
828 # we only support fixed size data now.
830 # we only support fixed size data now.
829 # This will be improved in the future.
831 # This will be improved in the future.
830 if util.safehasattr(self.data, 'next'):
832 if util.safehasattr(self.data, 'next'):
831 buff = util.chunkbuffer(self.data)
833 buff = util.chunkbuffer(self.data)
832 chunk = buff.read(preferedchunksize)
834 chunk = buff.read(preferedchunksize)
833 while chunk:
835 while chunk:
834 yield chunk
836 yield chunk
835 chunk = buff.read(preferedchunksize)
837 chunk = buff.read(preferedchunksize)
836 elif len(self.data):
838 elif len(self.data):
837 yield self.data
839 yield self.data
838
840
839
841
840 flaginterrupt = -1
842 flaginterrupt = -1
841
843
842 class interrupthandler(unpackermixin):
844 class interrupthandler(unpackermixin):
843 """read one part and process it with restricted capability
845 """read one part and process it with restricted capability
844
846
845 This allows to transmit exception raised on the producer size during part
847 This allows to transmit exception raised on the producer size during part
846 iteration while the consumer is reading a part.
848 iteration while the consumer is reading a part.
847
849
848 Part processed in this manner only have access to a ui object,"""
850 Part processed in this manner only have access to a ui object,"""
849
851
850 def __init__(self, ui, fp):
852 def __init__(self, ui, fp):
851 super(interrupthandler, self).__init__(fp)
853 super(interrupthandler, self).__init__(fp)
852 self.ui = ui
854 self.ui = ui
853
855
854 def _readpartheader(self):
856 def _readpartheader(self):
855 """reads a part header size and return the bytes blob
857 """reads a part header size and return the bytes blob
856
858
857 returns None if empty"""
859 returns None if empty"""
858 headersize = self._unpack(_fpartheadersize)[0]
860 headersize = self._unpack(_fpartheadersize)[0]
859 if headersize < 0:
861 if headersize < 0:
860 raise error.BundleValueError('negative part header size: %i'
862 raise error.BundleValueError('negative part header size: %i'
861 % headersize)
863 % headersize)
862 indebug(self.ui, 'part header size: %i\n' % headersize)
864 indebug(self.ui, 'part header size: %i\n' % headersize)
863 if headersize:
865 if headersize:
864 return self._readexact(headersize)
866 return self._readexact(headersize)
865 return None
867 return None
866
868
867 def __call__(self):
869 def __call__(self):
868 indebug(self.ui, 'bundle2 stream interruption, looking for a part.')
870 indebug(self.ui, 'bundle2 stream interruption, looking for a part.')
869 headerblock = self._readpartheader()
871 headerblock = self._readpartheader()
870 if headerblock is None:
872 if headerblock is None:
871 indebug(self.ui, 'no part found during interruption.')
873 indebug(self.ui, 'no part found during interruption.')
872 return
874 return
873 part = unbundlepart(self.ui, headerblock, self._fp)
875 part = unbundlepart(self.ui, headerblock, self._fp)
874 op = interruptoperation(self.ui)
876 op = interruptoperation(self.ui)
875 _processpart(op, part)
877 _processpart(op, part)
876
878
877 class interruptoperation(object):
879 class interruptoperation(object):
878 """A limited operation to be use by part handler during interruption
880 """A limited operation to be use by part handler during interruption
879
881
880 It only have access to an ui object.
882 It only have access to an ui object.
881 """
883 """
882
884
883 def __init__(self, ui):
885 def __init__(self, ui):
884 self.ui = ui
886 self.ui = ui
885 self.reply = None
887 self.reply = None
886 self.captureoutput = False
888 self.captureoutput = False
887
889
888 @property
890 @property
889 def repo(self):
891 def repo(self):
890 raise RuntimeError('no repo access from stream interruption')
892 raise RuntimeError('no repo access from stream interruption')
891
893
892 def gettransaction(self):
894 def gettransaction(self):
893 raise TransactionUnavailable('no repo access from stream interruption')
895 raise TransactionUnavailable('no repo access from stream interruption')
894
896
895 class unbundlepart(unpackermixin):
897 class unbundlepart(unpackermixin):
896 """a bundle part read from a bundle"""
898 """a bundle part read from a bundle"""
897
899
898 def __init__(self, ui, header, fp):
900 def __init__(self, ui, header, fp):
899 super(unbundlepart, self).__init__(fp)
901 super(unbundlepart, self).__init__(fp)
900 self.ui = ui
902 self.ui = ui
901 # unbundle state attr
903 # unbundle state attr
902 self._headerdata = header
904 self._headerdata = header
903 self._headeroffset = 0
905 self._headeroffset = 0
904 self._initialized = False
906 self._initialized = False
905 self.consumed = False
907 self.consumed = False
906 # part data
908 # part data
907 self.id = None
909 self.id = None
908 self.type = None
910 self.type = None
909 self.mandatoryparams = None
911 self.mandatoryparams = None
910 self.advisoryparams = None
912 self.advisoryparams = None
911 self.params = None
913 self.params = None
912 self.mandatorykeys = ()
914 self.mandatorykeys = ()
913 self._payloadstream = None
915 self._payloadstream = None
914 self._readheader()
916 self._readheader()
915 self._mandatory = None
917 self._mandatory = None
916 self._chunkindex = [] #(payload, file) position tuples for chunk starts
918 self._chunkindex = [] #(payload, file) position tuples for chunk starts
917 self._pos = 0
919 self._pos = 0
918
920
919 def _fromheader(self, size):
921 def _fromheader(self, size):
920 """return the next <size> byte from the header"""
922 """return the next <size> byte from the header"""
921 offset = self._headeroffset
923 offset = self._headeroffset
922 data = self._headerdata[offset:(offset + size)]
924 data = self._headerdata[offset:(offset + size)]
923 self._headeroffset = offset + size
925 self._headeroffset = offset + size
924 return data
926 return data
925
927
926 def _unpackheader(self, format):
928 def _unpackheader(self, format):
927 """read given format from header
929 """read given format from header
928
930
929 This automatically compute the size of the format to read."""
931 This automatically compute the size of the format to read."""
930 data = self._fromheader(struct.calcsize(format))
932 data = self._fromheader(struct.calcsize(format))
931 return _unpack(format, data)
933 return _unpack(format, data)
932
934
933 def _initparams(self, mandatoryparams, advisoryparams):
935 def _initparams(self, mandatoryparams, advisoryparams):
934 """internal function to setup all logic related parameters"""
936 """internal function to setup all logic related parameters"""
935 # make it read only to prevent people touching it by mistake.
937 # make it read only to prevent people touching it by mistake.
936 self.mandatoryparams = tuple(mandatoryparams)
938 self.mandatoryparams = tuple(mandatoryparams)
937 self.advisoryparams = tuple(advisoryparams)
939 self.advisoryparams = tuple(advisoryparams)
938 # user friendly UI
940 # user friendly UI
939 self.params = dict(self.mandatoryparams)
941 self.params = dict(self.mandatoryparams)
940 self.params.update(dict(self.advisoryparams))
942 self.params.update(dict(self.advisoryparams))
941 self.mandatorykeys = frozenset(p[0] for p in mandatoryparams)
943 self.mandatorykeys = frozenset(p[0] for p in mandatoryparams)
942
944
943 def _payloadchunks(self, chunknum=0):
945 def _payloadchunks(self, chunknum=0):
944 '''seek to specified chunk and start yielding data'''
946 '''seek to specified chunk and start yielding data'''
945 if len(self._chunkindex) == 0:
947 if len(self._chunkindex) == 0:
946 assert chunknum == 0, 'Must start with chunk 0'
948 assert chunknum == 0, 'Must start with chunk 0'
947 self._chunkindex.append((0, super(unbundlepart, self).tell()))
949 self._chunkindex.append((0, super(unbundlepart, self).tell()))
948 else:
950 else:
949 assert chunknum < len(self._chunkindex), \
951 assert chunknum < len(self._chunkindex), \
950 'Unknown chunk %d' % chunknum
952 'Unknown chunk %d' % chunknum
951 super(unbundlepart, self).seek(self._chunkindex[chunknum][1])
953 super(unbundlepart, self).seek(self._chunkindex[chunknum][1])
952
954
953 pos = self._chunkindex[chunknum][0]
955 pos = self._chunkindex[chunknum][0]
954 payloadsize = self._unpack(_fpayloadsize)[0]
956 payloadsize = self._unpack(_fpayloadsize)[0]
955 indebug(self.ui, 'payload chunk size: %i' % payloadsize)
957 indebug(self.ui, 'payload chunk size: %i' % payloadsize)
956 while payloadsize:
958 while payloadsize:
957 if payloadsize == flaginterrupt:
959 if payloadsize == flaginterrupt:
958 # interruption detection, the handler will now read a
960 # interruption detection, the handler will now read a
959 # single part and process it.
961 # single part and process it.
960 interrupthandler(self.ui, self._fp)()
962 interrupthandler(self.ui, self._fp)()
961 elif payloadsize < 0:
963 elif payloadsize < 0:
962 msg = 'negative payload chunk size: %i' % payloadsize
964 msg = 'negative payload chunk size: %i' % payloadsize
963 raise error.BundleValueError(msg)
965 raise error.BundleValueError(msg)
964 else:
966 else:
965 result = self._readexact(payloadsize)
967 result = self._readexact(payloadsize)
966 chunknum += 1
968 chunknum += 1
967 pos += payloadsize
969 pos += payloadsize
968 if chunknum == len(self._chunkindex):
970 if chunknum == len(self._chunkindex):
969 self._chunkindex.append((pos,
971 self._chunkindex.append((pos,
970 super(unbundlepart, self).tell()))
972 super(unbundlepart, self).tell()))
971 yield result
973 yield result
972 payloadsize = self._unpack(_fpayloadsize)[0]
974 payloadsize = self._unpack(_fpayloadsize)[0]
973 indebug(self.ui, 'payload chunk size: %i' % payloadsize)
975 indebug(self.ui, 'payload chunk size: %i' % payloadsize)
974
976
975 def _findchunk(self, pos):
977 def _findchunk(self, pos):
976 '''for a given payload position, return a chunk number and offset'''
978 '''for a given payload position, return a chunk number and offset'''
977 for chunk, (ppos, fpos) in enumerate(self._chunkindex):
979 for chunk, (ppos, fpos) in enumerate(self._chunkindex):
978 if ppos == pos:
980 if ppos == pos:
979 return chunk, 0
981 return chunk, 0
980 elif ppos > pos:
982 elif ppos > pos:
981 return chunk - 1, pos - self._chunkindex[chunk - 1][0]
983 return chunk - 1, pos - self._chunkindex[chunk - 1][0]
982 raise ValueError('Unknown chunk')
984 raise ValueError('Unknown chunk')
983
985
984 def _readheader(self):
986 def _readheader(self):
985 """read the header and setup the object"""
987 """read the header and setup the object"""
986 typesize = self._unpackheader(_fparttypesize)[0]
988 typesize = self._unpackheader(_fparttypesize)[0]
987 self.type = self._fromheader(typesize)
989 self.type = self._fromheader(typesize)
988 indebug(self.ui, 'part type: "%s"' % self.type)
990 indebug(self.ui, 'part type: "%s"' % self.type)
989 self.id = self._unpackheader(_fpartid)[0]
991 self.id = self._unpackheader(_fpartid)[0]
990 indebug(self.ui, 'part id: "%s"' % self.id)
992 indebug(self.ui, 'part id: "%s"' % self.id)
991 # extract mandatory bit from type
993 # extract mandatory bit from type
992 self.mandatory = (self.type != self.type.lower())
994 self.mandatory = (self.type != self.type.lower())
993 self.type = self.type.lower()
995 self.type = self.type.lower()
994 ## reading parameters
996 ## reading parameters
995 # param count
997 # param count
996 mancount, advcount = self._unpackheader(_fpartparamcount)
998 mancount, advcount = self._unpackheader(_fpartparamcount)
997 indebug(self.ui, 'part parameters: %i' % (mancount + advcount))
999 indebug(self.ui, 'part parameters: %i' % (mancount + advcount))
998 # param size
1000 # param size
999 fparamsizes = _makefpartparamsizes(mancount + advcount)
1001 fparamsizes = _makefpartparamsizes(mancount + advcount)
1000 paramsizes = self._unpackheader(fparamsizes)
1002 paramsizes = self._unpackheader(fparamsizes)
1001 # make it a list of couple again
1003 # make it a list of couple again
1002 paramsizes = zip(paramsizes[::2], paramsizes[1::2])
1004 paramsizes = zip(paramsizes[::2], paramsizes[1::2])
1003 # split mandatory from advisory
1005 # split mandatory from advisory
1004 mansizes = paramsizes[:mancount]
1006 mansizes = paramsizes[:mancount]
1005 advsizes = paramsizes[mancount:]
1007 advsizes = paramsizes[mancount:]
1006 # retrieve param value
1008 # retrieve param value
1007 manparams = []
1009 manparams = []
1008 for key, value in mansizes:
1010 for key, value in mansizes:
1009 manparams.append((self._fromheader(key), self._fromheader(value)))
1011 manparams.append((self._fromheader(key), self._fromheader(value)))
1010 advparams = []
1012 advparams = []
1011 for key, value in advsizes:
1013 for key, value in advsizes:
1012 advparams.append((self._fromheader(key), self._fromheader(value)))
1014 advparams.append((self._fromheader(key), self._fromheader(value)))
1013 self._initparams(manparams, advparams)
1015 self._initparams(manparams, advparams)
1014 ## part payload
1016 ## part payload
1015 self._payloadstream = util.chunkbuffer(self._payloadchunks())
1017 self._payloadstream = util.chunkbuffer(self._payloadchunks())
1016 # we read the data, tell it
1018 # we read the data, tell it
1017 self._initialized = True
1019 self._initialized = True
1018
1020
1019 def read(self, size=None):
1021 def read(self, size=None):
1020 """read payload data"""
1022 """read payload data"""
1021 if not self._initialized:
1023 if not self._initialized:
1022 self._readheader()
1024 self._readheader()
1023 if size is None:
1025 if size is None:
1024 data = self._payloadstream.read()
1026 data = self._payloadstream.read()
1025 else:
1027 else:
1026 data = self._payloadstream.read(size)
1028 data = self._payloadstream.read(size)
1027 if size is None or len(data) < size:
1029 if size is None or len(data) < size:
1028 self.consumed = True
1030 self.consumed = True
1029 self._pos += len(data)
1031 self._pos += len(data)
1030 return data
1032 return data
1031
1033
1032 def tell(self):
1034 def tell(self):
1033 return self._pos
1035 return self._pos
1034
1036
1035 def seek(self, offset, whence=0):
1037 def seek(self, offset, whence=0):
1036 if whence == 0:
1038 if whence == 0:
1037 newpos = offset
1039 newpos = offset
1038 elif whence == 1:
1040 elif whence == 1:
1039 newpos = self._pos + offset
1041 newpos = self._pos + offset
1040 elif whence == 2:
1042 elif whence == 2:
1041 if not self.consumed:
1043 if not self.consumed:
1042 self.read()
1044 self.read()
1043 newpos = self._chunkindex[-1][0] - offset
1045 newpos = self._chunkindex[-1][0] - offset
1044 else:
1046 else:
1045 raise ValueError('Unknown whence value: %r' % (whence,))
1047 raise ValueError('Unknown whence value: %r' % (whence,))
1046
1048
1047 if newpos > self._chunkindex[-1][0] and not self.consumed:
1049 if newpos > self._chunkindex[-1][0] and not self.consumed:
1048 self.read()
1050 self.read()
1049 if not 0 <= newpos <= self._chunkindex[-1][0]:
1051 if not 0 <= newpos <= self._chunkindex[-1][0]:
1050 raise ValueError('Offset out of range')
1052 raise ValueError('Offset out of range')
1051
1053
1052 if self._pos != newpos:
1054 if self._pos != newpos:
1053 chunk, internaloffset = self._findchunk(newpos)
1055 chunk, internaloffset = self._findchunk(newpos)
1054 self._payloadstream = util.chunkbuffer(self._payloadchunks(chunk))
1056 self._payloadstream = util.chunkbuffer(self._payloadchunks(chunk))
1055 adjust = self.read(internaloffset)
1057 adjust = self.read(internaloffset)
1056 if len(adjust) != internaloffset:
1058 if len(adjust) != internaloffset:
1057 raise util.Abort(_('Seek failed\n'))
1059 raise util.Abort(_('Seek failed\n'))
1058 self._pos = newpos
1060 self._pos = newpos
1059
1061
1060 # These are only the static capabilities.
1062 # These are only the static capabilities.
1061 # Check the 'getrepocaps' function for the rest.
1063 # Check the 'getrepocaps' function for the rest.
1062 capabilities = {'HG20': (),
1064 capabilities = {'HG20': (),
1063 'listkeys': (),
1065 'listkeys': (),
1064 'pushkey': (),
1066 'pushkey': (),
1065 'digests': tuple(sorted(util.DIGESTS.keys())),
1067 'digests': tuple(sorted(util.DIGESTS.keys())),
1066 'remote-changegroup': ('http', 'https'),
1068 'remote-changegroup': ('http', 'https'),
1067 }
1069 }
1068
1070
1069 def getrepocaps(repo, allowpushback=False):
1071 def getrepocaps(repo, allowpushback=False):
1070 """return the bundle2 capabilities for a given repo
1072 """return the bundle2 capabilities for a given repo
1071
1073
1072 Exists to allow extensions (like evolution) to mutate the capabilities.
1074 Exists to allow extensions (like evolution) to mutate the capabilities.
1073 """
1075 """
1074 caps = capabilities.copy()
1076 caps = capabilities.copy()
1075 caps['changegroup'] = tuple(sorted(changegroup.packermap.keys()))
1077 caps['changegroup'] = tuple(sorted(changegroup.packermap.keys()))
1076 if obsolete.isenabled(repo, obsolete.exchangeopt):
1078 if obsolete.isenabled(repo, obsolete.exchangeopt):
1077 supportedformat = tuple('V%i' % v for v in obsolete.formats)
1079 supportedformat = tuple('V%i' % v for v in obsolete.formats)
1078 caps['obsmarkers'] = supportedformat
1080 caps['obsmarkers'] = supportedformat
1079 if allowpushback:
1081 if allowpushback:
1080 caps['pushback'] = ()
1082 caps['pushback'] = ()
1081 return caps
1083 return caps
1082
1084
1083 def bundle2caps(remote):
1085 def bundle2caps(remote):
1084 """return the bundle capabilities of a peer as dict"""
1086 """return the bundle capabilities of a peer as dict"""
1085 raw = remote.capable('bundle2')
1087 raw = remote.capable('bundle2')
1086 if not raw and raw != '':
1088 if not raw and raw != '':
1087 return {}
1089 return {}
1088 capsblob = urllib.unquote(remote.capable('bundle2'))
1090 capsblob = urllib.unquote(remote.capable('bundle2'))
1089 return decodecaps(capsblob)
1091 return decodecaps(capsblob)
1090
1092
1091 def obsmarkersversion(caps):
1093 def obsmarkersversion(caps):
1092 """extract the list of supported obsmarkers versions from a bundle2caps dict
1094 """extract the list of supported obsmarkers versions from a bundle2caps dict
1093 """
1095 """
1094 obscaps = caps.get('obsmarkers', ())
1096 obscaps = caps.get('obsmarkers', ())
1095 return [int(c[1:]) for c in obscaps if c.startswith('V')]
1097 return [int(c[1:]) for c in obscaps if c.startswith('V')]
1096
1098
1097 @parthandler('changegroup', ('version',))
1099 @parthandler('changegroup', ('version',))
1098 def handlechangegroup(op, inpart):
1100 def handlechangegroup(op, inpart):
1099 """apply a changegroup part on the repo
1101 """apply a changegroup part on the repo
1100
1102
1101 This is a very early implementation that will massive rework before being
1103 This is a very early implementation that will massive rework before being
1102 inflicted to any end-user.
1104 inflicted to any end-user.
1103 """
1105 """
1104 # Make sure we trigger a transaction creation
1106 # Make sure we trigger a transaction creation
1105 #
1107 #
1106 # The addchangegroup function will get a transaction object by itself, but
1108 # The addchangegroup function will get a transaction object by itself, but
1107 # we need to make sure we trigger the creation of a transaction object used
1109 # we need to make sure we trigger the creation of a transaction object used
1108 # for the whole processing scope.
1110 # for the whole processing scope.
1109 op.gettransaction()
1111 op.gettransaction()
1110 unpackerversion = inpart.params.get('version', '01')
1112 unpackerversion = inpart.params.get('version', '01')
1111 # We should raise an appropriate exception here
1113 # We should raise an appropriate exception here
1112 unpacker = changegroup.packermap[unpackerversion][1]
1114 unpacker = changegroup.packermap[unpackerversion][1]
1113 cg = unpacker(inpart, 'UN')
1115 cg = unpacker(inpart, 'UN')
1114 # the source and url passed here are overwritten by the one contained in
1116 # the source and url passed here are overwritten by the one contained in
1115 # the transaction.hookargs argument. So 'bundle2' is a placeholder
1117 # the transaction.hookargs argument. So 'bundle2' is a placeholder
1116 ret = changegroup.addchangegroup(op.repo, cg, 'bundle2', 'bundle2')
1118 ret = changegroup.addchangegroup(op.repo, cg, 'bundle2', 'bundle2')
1117 op.records.add('changegroup', {'return': ret})
1119 op.records.add('changegroup', {'return': ret})
1118 if op.reply is not None:
1120 if op.reply is not None:
1119 # This is definitely not the final form of this
1121 # This is definitely not the final form of this
1120 # return. But one need to start somewhere.
1122 # return. But one need to start somewhere.
1121 part = op.reply.newpart('reply:changegroup', mandatory=False)
1123 part = op.reply.newpart('reply:changegroup', mandatory=False)
1122 part.addparam('in-reply-to', str(inpart.id), mandatory=False)
1124 part.addparam('in-reply-to', str(inpart.id), mandatory=False)
1123 part.addparam('return', '%i' % ret, mandatory=False)
1125 part.addparam('return', '%i' % ret, mandatory=False)
1124 assert not inpart.read()
1126 assert not inpart.read()
1125
1127
1126 _remotechangegroupparams = tuple(['url', 'size', 'digests'] +
1128 _remotechangegroupparams = tuple(['url', 'size', 'digests'] +
1127 ['digest:%s' % k for k in util.DIGESTS.keys()])
1129 ['digest:%s' % k for k in util.DIGESTS.keys()])
1128 @parthandler('remote-changegroup', _remotechangegroupparams)
1130 @parthandler('remote-changegroup', _remotechangegroupparams)
1129 def handleremotechangegroup(op, inpart):
1131 def handleremotechangegroup(op, inpart):
1130 """apply a bundle10 on the repo, given an url and validation information
1132 """apply a bundle10 on the repo, given an url and validation information
1131
1133
1132 All the information about the remote bundle to import are given as
1134 All the information about the remote bundle to import are given as
1133 parameters. The parameters include:
1135 parameters. The parameters include:
1134 - url: the url to the bundle10.
1136 - url: the url to the bundle10.
1135 - size: the bundle10 file size. It is used to validate what was
1137 - size: the bundle10 file size. It is used to validate what was
1136 retrieved by the client matches the server knowledge about the bundle.
1138 retrieved by the client matches the server knowledge about the bundle.
1137 - digests: a space separated list of the digest types provided as
1139 - digests: a space separated list of the digest types provided as
1138 parameters.
1140 parameters.
1139 - digest:<digest-type>: the hexadecimal representation of the digest with
1141 - digest:<digest-type>: the hexadecimal representation of the digest with
1140 that name. Like the size, it is used to validate what was retrieved by
1142 that name. Like the size, it is used to validate what was retrieved by
1141 the client matches what the server knows about the bundle.
1143 the client matches what the server knows about the bundle.
1142
1144
1143 When multiple digest types are given, all of them are checked.
1145 When multiple digest types are given, all of them are checked.
1144 """
1146 """
1145 try:
1147 try:
1146 raw_url = inpart.params['url']
1148 raw_url = inpart.params['url']
1147 except KeyError:
1149 except KeyError:
1148 raise util.Abort(_('remote-changegroup: missing "%s" param') % 'url')
1150 raise util.Abort(_('remote-changegroup: missing "%s" param') % 'url')
1149 parsed_url = util.url(raw_url)
1151 parsed_url = util.url(raw_url)
1150 if parsed_url.scheme not in capabilities['remote-changegroup']:
1152 if parsed_url.scheme not in capabilities['remote-changegroup']:
1151 raise util.Abort(_('remote-changegroup does not support %s urls') %
1153 raise util.Abort(_('remote-changegroup does not support %s urls') %
1152 parsed_url.scheme)
1154 parsed_url.scheme)
1153
1155
1154 try:
1156 try:
1155 size = int(inpart.params['size'])
1157 size = int(inpart.params['size'])
1156 except ValueError:
1158 except ValueError:
1157 raise util.Abort(_('remote-changegroup: invalid value for param "%s"')
1159 raise util.Abort(_('remote-changegroup: invalid value for param "%s"')
1158 % 'size')
1160 % 'size')
1159 except KeyError:
1161 except KeyError:
1160 raise util.Abort(_('remote-changegroup: missing "%s" param') % 'size')
1162 raise util.Abort(_('remote-changegroup: missing "%s" param') % 'size')
1161
1163
1162 digests = {}
1164 digests = {}
1163 for typ in inpart.params.get('digests', '').split():
1165 for typ in inpart.params.get('digests', '').split():
1164 param = 'digest:%s' % typ
1166 param = 'digest:%s' % typ
1165 try:
1167 try:
1166 value = inpart.params[param]
1168 value = inpart.params[param]
1167 except KeyError:
1169 except KeyError:
1168 raise util.Abort(_('remote-changegroup: missing "%s" param') %
1170 raise util.Abort(_('remote-changegroup: missing "%s" param') %
1169 param)
1171 param)
1170 digests[typ] = value
1172 digests[typ] = value
1171
1173
1172 real_part = util.digestchecker(url.open(op.ui, raw_url), size, digests)
1174 real_part = util.digestchecker(url.open(op.ui, raw_url), size, digests)
1173
1175
1174 # Make sure we trigger a transaction creation
1176 # Make sure we trigger a transaction creation
1175 #
1177 #
1176 # The addchangegroup function will get a transaction object by itself, but
1178 # The addchangegroup function will get a transaction object by itself, but
1177 # we need to make sure we trigger the creation of a transaction object used
1179 # we need to make sure we trigger the creation of a transaction object used
1178 # for the whole processing scope.
1180 # for the whole processing scope.
1179 op.gettransaction()
1181 op.gettransaction()
1180 import exchange
1182 import exchange
1181 cg = exchange.readbundle(op.repo.ui, real_part, raw_url)
1183 cg = exchange.readbundle(op.repo.ui, real_part, raw_url)
1182 if not isinstance(cg, changegroup.cg1unpacker):
1184 if not isinstance(cg, changegroup.cg1unpacker):
1183 raise util.Abort(_('%s: not a bundle version 1.0') %
1185 raise util.Abort(_('%s: not a bundle version 1.0') %
1184 util.hidepassword(raw_url))
1186 util.hidepassword(raw_url))
1185 ret = changegroup.addchangegroup(op.repo, cg, 'bundle2', 'bundle2')
1187 ret = changegroup.addchangegroup(op.repo, cg, 'bundle2', 'bundle2')
1186 op.records.add('changegroup', {'return': ret})
1188 op.records.add('changegroup', {'return': ret})
1187 if op.reply is not None:
1189 if op.reply is not None:
1188 # This is definitely not the final form of this
1190 # This is definitely not the final form of this
1189 # return. But one need to start somewhere.
1191 # return. But one need to start somewhere.
1190 part = op.reply.newpart('reply:changegroup')
1192 part = op.reply.newpart('reply:changegroup')
1191 part.addparam('in-reply-to', str(inpart.id), mandatory=False)
1193 part.addparam('in-reply-to', str(inpart.id), mandatory=False)
1192 part.addparam('return', '%i' % ret, mandatory=False)
1194 part.addparam('return', '%i' % ret, mandatory=False)
1193 try:
1195 try:
1194 real_part.validate()
1196 real_part.validate()
1195 except util.Abort, e:
1197 except util.Abort, e:
1196 raise util.Abort(_('bundle at %s is corrupted:\n%s') %
1198 raise util.Abort(_('bundle at %s is corrupted:\n%s') %
1197 (util.hidepassword(raw_url), str(e)))
1199 (util.hidepassword(raw_url), str(e)))
1198 assert not inpart.read()
1200 assert not inpart.read()
1199
1201
1200 @parthandler('reply:changegroup', ('return', 'in-reply-to'))
1202 @parthandler('reply:changegroup', ('return', 'in-reply-to'))
1201 def handlereplychangegroup(op, inpart):
1203 def handlereplychangegroup(op, inpart):
1202 ret = int(inpart.params['return'])
1204 ret = int(inpart.params['return'])
1203 replyto = int(inpart.params['in-reply-to'])
1205 replyto = int(inpart.params['in-reply-to'])
1204 op.records.add('changegroup', {'return': ret}, replyto)
1206 op.records.add('changegroup', {'return': ret}, replyto)
1205
1207
1206 @parthandler('check:heads')
1208 @parthandler('check:heads')
1207 def handlecheckheads(op, inpart):
1209 def handlecheckheads(op, inpart):
1208 """check that head of the repo did not change
1210 """check that head of the repo did not change
1209
1211
1210 This is used to detect a push race when using unbundle.
1212 This is used to detect a push race when using unbundle.
1211 This replaces the "heads" argument of unbundle."""
1213 This replaces the "heads" argument of unbundle."""
1212 h = inpart.read(20)
1214 h = inpart.read(20)
1213 heads = []
1215 heads = []
1214 while len(h) == 20:
1216 while len(h) == 20:
1215 heads.append(h)
1217 heads.append(h)
1216 h = inpart.read(20)
1218 h = inpart.read(20)
1217 assert not h
1219 assert not h
1218 if heads != op.repo.heads():
1220 if heads != op.repo.heads():
1219 raise error.PushRaced('repository changed while pushing - '
1221 raise error.PushRaced('repository changed while pushing - '
1220 'please try again')
1222 'please try again')
1221
1223
1222 @parthandler('output')
1224 @parthandler('output')
1223 def handleoutput(op, inpart):
1225 def handleoutput(op, inpart):
1224 """forward output captured on the server to the client"""
1226 """forward output captured on the server to the client"""
1225 for line in inpart.read().splitlines():
1227 for line in inpart.read().splitlines():
1226 op.ui.status(('remote: %s\n' % line))
1228 op.ui.status(('remote: %s\n' % line))
1227
1229
1228 @parthandler('replycaps')
1230 @parthandler('replycaps')
1229 def handlereplycaps(op, inpart):
1231 def handlereplycaps(op, inpart):
1230 """Notify that a reply bundle should be created
1232 """Notify that a reply bundle should be created
1231
1233
1232 The payload contains the capabilities information for the reply"""
1234 The payload contains the capabilities information for the reply"""
1233 caps = decodecaps(inpart.read())
1235 caps = decodecaps(inpart.read())
1234 if op.reply is None:
1236 if op.reply is None:
1235 op.reply = bundle20(op.ui, caps)
1237 op.reply = bundle20(op.ui, caps)
1236
1238
1237 @parthandler('error:abort', ('message', 'hint'))
1239 @parthandler('error:abort', ('message', 'hint'))
1238 def handleerrorabort(op, inpart):
1240 def handleerrorabort(op, inpart):
1239 """Used to transmit abort error over the wire"""
1241 """Used to transmit abort error over the wire"""
1240 raise util.Abort(inpart.params['message'], hint=inpart.params.get('hint'))
1242 raise util.Abort(inpart.params['message'], hint=inpart.params.get('hint'))
1241
1243
1242 @parthandler('error:unsupportedcontent', ('parttype', 'params'))
1244 @parthandler('error:unsupportedcontent', ('parttype', 'params'))
1243 def handleerrorunsupportedcontent(op, inpart):
1245 def handleerrorunsupportedcontent(op, inpart):
1244 """Used to transmit unknown content error over the wire"""
1246 """Used to transmit unknown content error over the wire"""
1245 kwargs = {}
1247 kwargs = {}
1246 parttype = inpart.params.get('parttype')
1248 parttype = inpart.params.get('parttype')
1247 if parttype is not None:
1249 if parttype is not None:
1248 kwargs['parttype'] = parttype
1250 kwargs['parttype'] = parttype
1249 params = inpart.params.get('params')
1251 params = inpart.params.get('params')
1250 if params is not None:
1252 if params is not None:
1251 kwargs['params'] = params.split('\0')
1253 kwargs['params'] = params.split('\0')
1252
1254
1253 raise error.UnsupportedPartError(**kwargs)
1255 raise error.UnsupportedPartError(**kwargs)
1254
1256
1255 @parthandler('error:pushraced', ('message',))
1257 @parthandler('error:pushraced', ('message',))
1256 def handleerrorpushraced(op, inpart):
1258 def handleerrorpushraced(op, inpart):
1257 """Used to transmit push race error over the wire"""
1259 """Used to transmit push race error over the wire"""
1258 raise error.ResponseError(_('push failed:'), inpart.params['message'])
1260 raise error.ResponseError(_('push failed:'), inpart.params['message'])
1259
1261
1260 @parthandler('listkeys', ('namespace',))
1262 @parthandler('listkeys', ('namespace',))
1261 def handlelistkeys(op, inpart):
1263 def handlelistkeys(op, inpart):
1262 """retrieve pushkey namespace content stored in a bundle2"""
1264 """retrieve pushkey namespace content stored in a bundle2"""
1263 namespace = inpart.params['namespace']
1265 namespace = inpart.params['namespace']
1264 r = pushkey.decodekeys(inpart.read())
1266 r = pushkey.decodekeys(inpart.read())
1265 op.records.add('listkeys', (namespace, r))
1267 op.records.add('listkeys', (namespace, r))
1266
1268
1267 @parthandler('pushkey', ('namespace', 'key', 'old', 'new'))
1269 @parthandler('pushkey', ('namespace', 'key', 'old', 'new'))
1268 def handlepushkey(op, inpart):
1270 def handlepushkey(op, inpart):
1269 """process a pushkey request"""
1271 """process a pushkey request"""
1270 dec = pushkey.decode
1272 dec = pushkey.decode
1271 namespace = dec(inpart.params['namespace'])
1273 namespace = dec(inpart.params['namespace'])
1272 key = dec(inpart.params['key'])
1274 key = dec(inpart.params['key'])
1273 old = dec(inpart.params['old'])
1275 old = dec(inpart.params['old'])
1274 new = dec(inpart.params['new'])
1276 new = dec(inpart.params['new'])
1275 ret = op.repo.pushkey(namespace, key, old, new)
1277 ret = op.repo.pushkey(namespace, key, old, new)
1276 record = {'namespace': namespace,
1278 record = {'namespace': namespace,
1277 'key': key,
1279 'key': key,
1278 'old': old,
1280 'old': old,
1279 'new': new}
1281 'new': new}
1280 op.records.add('pushkey', record)
1282 op.records.add('pushkey', record)
1281 if op.reply is not None:
1283 if op.reply is not None:
1282 rpart = op.reply.newpart('reply:pushkey')
1284 rpart = op.reply.newpart('reply:pushkey')
1283 rpart.addparam('in-reply-to', str(inpart.id), mandatory=False)
1285 rpart.addparam('in-reply-to', str(inpart.id), mandatory=False)
1284 rpart.addparam('return', '%i' % ret, mandatory=False)
1286 rpart.addparam('return', '%i' % ret, mandatory=False)
1285
1287
1286 @parthandler('reply:pushkey', ('return', 'in-reply-to'))
1288 @parthandler('reply:pushkey', ('return', 'in-reply-to'))
1287 def handlepushkeyreply(op, inpart):
1289 def handlepushkeyreply(op, inpart):
1288 """retrieve the result of a pushkey request"""
1290 """retrieve the result of a pushkey request"""
1289 ret = int(inpart.params['return'])
1291 ret = int(inpart.params['return'])
1290 partid = int(inpart.params['in-reply-to'])
1292 partid = int(inpart.params['in-reply-to'])
1291 op.records.add('pushkey', {'return': ret}, partid)
1293 op.records.add('pushkey', {'return': ret}, partid)
1292
1294
1293 @parthandler('obsmarkers')
1295 @parthandler('obsmarkers')
1294 def handleobsmarker(op, inpart):
1296 def handleobsmarker(op, inpart):
1295 """add a stream of obsmarkers to the repo"""
1297 """add a stream of obsmarkers to the repo"""
1296 tr = op.gettransaction()
1298 tr = op.gettransaction()
1297 markerdata = inpart.read()
1299 markerdata = inpart.read()
1298 if op.ui.config('experimental', 'obsmarkers-exchange-debug', False):
1300 if op.ui.config('experimental', 'obsmarkers-exchange-debug', False):
1299 op.ui.write(('obsmarker-exchange: %i bytes received\n')
1301 op.ui.write(('obsmarker-exchange: %i bytes received\n')
1300 % len(markerdata))
1302 % len(markerdata))
1301 new = op.repo.obsstore.mergemarkers(tr, markerdata)
1303 new = op.repo.obsstore.mergemarkers(tr, markerdata)
1302 if new:
1304 if new:
1303 op.repo.ui.status(_('%i new obsolescence markers\n') % new)
1305 op.repo.ui.status(_('%i new obsolescence markers\n') % new)
1304 op.records.add('obsmarkers', {'new': new})
1306 op.records.add('obsmarkers', {'new': new})
1305 if op.reply is not None:
1307 if op.reply is not None:
1306 rpart = op.reply.newpart('reply:obsmarkers')
1308 rpart = op.reply.newpart('reply:obsmarkers')
1307 rpart.addparam('in-reply-to', str(inpart.id), mandatory=False)
1309 rpart.addparam('in-reply-to', str(inpart.id), mandatory=False)
1308 rpart.addparam('new', '%i' % new, mandatory=False)
1310 rpart.addparam('new', '%i' % new, mandatory=False)
1309
1311
1310
1312
1311 @parthandler('reply:obsmarkers', ('new', 'in-reply-to'))
1313 @parthandler('reply:obsmarkers', ('new', 'in-reply-to'))
1312 def handlepushkeyreply(op, inpart):
1314 def handlepushkeyreply(op, inpart):
1313 """retrieve the result of a pushkey request"""
1315 """retrieve the result of a pushkey request"""
1314 ret = int(inpart.params['new'])
1316 ret = int(inpart.params['new'])
1315 partid = int(inpart.params['in-reply-to'])
1317 partid = int(inpart.params['in-reply-to'])
1316 op.records.add('obsmarkers', {'new': ret}, partid)
1318 op.records.add('obsmarkers', {'new': ret}, partid)
General Comments 0
You need to be logged in to leave comments. Login now