~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory_delta.py

  • Committer: Andrew Bennetts
  • Date: 2009-08-10 08:10:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4608.
  • Revision ID: andrew.bennetts@canonical.com-20090810081006-hsl2hvsh4awse3kl
Replace require_flags method with allow_versioned_root and allow_tree_references flags on InventoryDeltaSerializer.__init__, and shift some checking of delta compatibility from StreamSink to InventoryDeltaSerializer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
232
232
class InventoryDeltaDeserializer(object):
233
233
    """Deserialize inventory deltas."""
234
234
 
235
 
    def __init__(self):
236
 
        """Create an InventoryDeltaDeserializer."""
237
 
        self._versioned_root = None
238
 
        self._tree_references = None
239
 
 
240
 
    def require_flags(self, versioned_root=None, tree_references=None):
241
 
        """Set the versioned_root and/or tree_references flags for this
242
 
        deserializer.
 
235
    def __init__(self, allow_versioned_root=True, allow_tree_references=True):
 
236
        """Create an InventoryDeltaDeserializer.
243
237
 
244
238
        :param versioned_root: If True, any root entry that is seen is expected
245
239
            to be versioned, and root entries can have any fileid.
246
240
        :param tree_references: If True support tree-reference entries.
247
241
        """
248
 
        if versioned_root is not None and self._versioned_root is not None:
249
 
            raise AssertionError(
250
 
                "require_flags(versioned_root=...) already called.")
251
 
        if tree_references is not None and self._tree_references is not None:
252
 
            raise AssertionError(
253
 
                "require_flags(tree_references=...) already called.")
254
 
        self._versioned_root = versioned_root
255
 
        self._tree_references = tree_references
 
242
        self._allow_versioned_root = allow_versioned_root
 
243
        self._allow_tree_references = allow_tree_references
256
244
 
257
245
    def _deserialize_bool(self, value):
258
246
        if value == "true":
292
280
        if len(lines) < 5 or not lines[4].startswith('tree_references: '):
293
281
            raise errors.BzrError('missing tree_references: marker')
294
282
        delta_tree_references = self._deserialize_bool(lines[4][17:])
295
 
        if (self._versioned_root is not None and
296
 
            delta_versioned_root != self._versioned_root):
297
 
            raise errors.BzrError(
298
 
                "serialized versioned_root flag is wrong: %s" %
299
 
                (delta_versioned_root,))
300
 
        if (self._tree_references is not None
301
 
            and delta_tree_references != self._tree_references):
302
 
            raise errors.BzrError(
303
 
                "serialized tree_references flag is wrong: %s" %
304
 
                (delta_tree_references,))
 
283
        if (not self._allow_versioned_root and delta_versioned_root):
 
284
            raise _IncompatibleDelta("versioned_root not allowed")
305
285
        result = []
306
286
        seen_ids = set()
307
287
        line_iter = iter(lines)
317
297
            seen_ids.add(file_id)
318
298
            if (newpath_utf8 == '/' and not delta_versioned_root and
319
299
                last_modified != delta_version_id):
320
 
                    # Delta claims to be not rich root, yet here's a root entry
321
 
                    # with either non-default version, i.e. it's rich...
 
300
                    # Delta claims to be not have a versioned root, yet here's
 
301
                    # a root entry with a non-default version.
322
302
                    raise errors.BzrError("Versioned root found: %r" % line)
323
303
            elif newpath_utf8 != 'None' and last_modified[-1] == ':':
324
304
                # Deletes have a last_modified of null:, but otherwise special
325
305
                # revision ids should not occur.
326
306
                raise errors.BzrError('special revisionid found: %r' % line)
327
 
            if delta_tree_references is False and content.startswith('tree\x00'):
328
 
                raise errors.BzrError("Tree reference found: %r" % line)
 
307
            if content.startswith('tree\x00'):
 
308
                if delta_tree_references is False:
 
309
                    raise errors.BzrError(
 
310
                            "Tree reference found (but header said "
 
311
                            "tree_references: false): %r" % line)
 
312
                elif not self._allow_tree_references:
 
313
                    raise _IncompatibleDelta("Tree reference not allowed")
329
314
            if oldpath_utf8 == 'None':
330
315
                oldpath = None
331
316
            elif oldpath_utf8[:1] != '/':
371
356
    return entry_factory[content[0]](
372
357
            content, name, parent_id, file_id, last_modified)
373
358
 
 
359
 
 
360
class _IncompatibleDelta(errors.InternalBzrError):
 
361
    """The delta could not be deserialised because its contents conflict with
 
362
    the allow_versioned_root or allow_tree_references flags of the
 
363
    deserializer.
 
364
    """
 
365