~bzr-pqm/bzr/bzr.dev

4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
1
# Copyright (C) 2008, 2009 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Inventory delta serialisation.
18
4205.5.6 by Andrew Bennetts
Garden module docstring a little, cleanup unused imports and inaccurate __all__.
19
See doc/developers/inventory.txt for the description of the format.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
20
21
In this module the interesting classes are:
4205.5.6 by Andrew Bennetts
Garden module docstring a little, cleanup unused imports and inaccurate __all__.
22
 - InventoryDeltaSerializer - object to read/write inventory deltas.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
23
"""
24
4205.5.6 by Andrew Bennetts
Garden module docstring a little, cleanup unused imports and inaccurate __all__.
25
__all__ = ['InventoryDeltaSerializer']
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
26
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
27
from bzrlib import errors
4205.5.6 by Andrew Bennetts
Garden module docstring a little, cleanup unused imports and inaccurate __all__.
28
from bzrlib.osutils import basename
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
29
from bzrlib import inventory
30
from bzrlib.revision import NULL_REVISION
31
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
32
FORMAT_1 = 'bzr inventory delta v1 (bzr 1.14)'
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
33
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
34
35
class InventoryDeltaError(errors.BzrError):
36
    """An error when serializing or deserializing an inventory delta."""
37
    
38
    # Most errors when serializing and deserializing are due to bugs, although
39
    # damaged input (i.e. a bug in a different process) could cause
40
    # deserialization errors too.
41
    internal_error = True
42
43
44
class IncompatibleInventoryDelta(errors.BzrError):
45
    """The delta could not be deserialised because its contents conflict with
46
    the allow_versioned_root or allow_tree_references flags of the
47
    deserializer.
48
    """
49
    internal_error = False
50
51
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
52
def _directory_content(entry):
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
53
    """Serialize the content component of entry which is a directory.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
54
    
55
    :param entry: An InventoryDirectory.
56
    """
57
    return "dir"
58
59
60
def _file_content(entry):
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
61
    """Serialize the content component of entry which is a file.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
62
    
63
    :param entry: An InventoryFile.
64
    """
65
    if entry.executable:
66
        exec_bytes = 'Y'
67
    else:
68
        exec_bytes = ''
69
    size_exec_sha = (entry.text_size, exec_bytes, entry.text_sha1)
70
    if None in size_exec_sha:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
71
        raise InventoryDeltaError('Missing size or sha for %s' % entry.file_id)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
72
    return "file\x00%d\x00%s\x00%s" % size_exec_sha
73
74
75
def _link_content(entry):
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
76
    """Serialize the content component of entry which is a symlink.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
77
    
78
    :param entry: An InventoryLink.
79
    """
80
    target = entry.symlink_target
81
    if target is None:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
82
        raise InventoryDeltaError('Missing target for %s' % entry.file_id)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
83
    return "link\x00%s" % target.encode('utf8')
84
85
86
def _reference_content(entry):
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
87
    """Serialize the content component of entry which is a tree-reference.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
88
    
89
    :param entry: A TreeReference.
90
    """
91
    tree_revision = entry.reference_revision
92
    if tree_revision is None:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
93
        raise InventoryDeltaError(
94
            'Missing reference revision for %s' % entry.file_id)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
95
    return "tree\x00%s" % tree_revision
96
97
98
def _dir_to_entry(content, name, parent_id, file_id, last_modified,
99
    _type=inventory.InventoryDirectory):
100
    """Convert a dir content record to an InventoryDirectory."""
101
    result = _type(file_id, name, parent_id)
102
    result.revision = last_modified
103
    return result
104
105
106
def _file_to_entry(content, name, parent_id, file_id, last_modified,
107
    _type=inventory.InventoryFile):
108
    """Convert a dir content record to an InventoryFile."""
109
    result = _type(file_id, name, parent_id)
110
    result.revision = last_modified
111
    result.text_size = int(content[1])
112
    result.text_sha1 = content[3]
113
    if content[2]:
114
        result.executable = True
115
    else:
116
        result.executable = False
117
    return result
118
119
120
def _link_to_entry(content, name, parent_id, file_id, last_modified,
121
    _type=inventory.InventoryLink):
122
    """Convert a link content record to an InventoryLink."""
123
    result = _type(file_id, name, parent_id)
124
    result.revision = last_modified
125
    result.symlink_target = content[1].decode('utf8')
126
    return result
127
128
129
def _tree_to_entry(content, name, parent_id, file_id, last_modified,
130
    _type=inventory.TreeReference):
131
    """Convert a tree content record to a TreeReference."""
132
    result = _type(file_id, name, parent_id)
133
    result.revision = last_modified
134
    result.reference_revision = content[1]
135
    return result
136
137
138
class InventoryDeltaSerializer(object):
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
139
    """Serialize inventory deltas."""
140
141
    def __init__(self, versioned_root, tree_references):
142
        """Create an InventoryDeltaSerializer.
143
144
        :param versioned_root: If True, any root entry that is seen is expected
145
            to be versioned, and root entries can have any fileid.
146
        :param tree_references: If True support tree-reference entries.
147
        """
148
        self._versioned_root = versioned_root
149
        self._tree_references = tree_references
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
150
        self._entry_to_content = {
151
            'directory': _directory_content,
152
            'file': _file_content,
153
            'symlink': _link_content,
154
        }
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
155
        if tree_references:
156
            self._entry_to_content['tree-reference'] = _reference_content
157
158
    def delta_to_lines(self, old_name, new_name, delta_to_new):
159
        """Return a line sequence for delta_to_new.
160
4476.3.12 by Andrew Bennetts
Add some more comments and docstring text to inventory_delta.py.
161
        Both the versioned_root and tree_references flags must be set via
162
        require_flags before calling this.
163
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
164
        :param old_name: A UTF8 revision id for the old inventory.  May be
165
            NULL_REVISION if there is no older inventory and delta_to_new
166
            includes the entire inventory contents.
167
        :param new_name: The version name of the inventory we create with this
168
            delta.
169
        :param delta_to_new: An inventory delta such as Inventory.apply_delta
170
            takes.
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
171
        :return: The serialized delta as lines.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
172
        """
4476.3.48 by Andrew Bennetts
Trap mistakes like passing key tuples (rather than id strs) into delta_to_lines sooner.
173
        if type(old_name) is not str:
174
            raise TypeError('old_name should be str, got %r' % (old_name,))
175
        if type(new_name) is not str:
176
            raise TypeError('new_name should be str, got %r' % (new_name,))
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
177
        lines = ['', '', '', '', '']
178
        to_line = self._delta_item_to_line
179
        for delta_item in delta_to_new:
4476.3.46 by Andrew Bennetts
Make actual non-rich-root inventories (which have implicit root entries with non-None revisions) roundtrip through inventory-deltas correctly.
180
            line = to_line(delta_item, new_name)
181
            if line.__class__ != str:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
182
                raise InventoryDeltaError(
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
183
                    'to_line generated non-str output %r' % lines[-1])
4476.3.46 by Andrew Bennetts
Make actual non-rich-root inventories (which have implicit root entries with non-None revisions) roundtrip through inventory-deltas correctly.
184
            lines.append(line)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
185
        lines.sort()
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
186
        lines[0] = "format: %s\n" % FORMAT_1
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
187
        lines[1] = "parent: %s\n" % old_name
188
        lines[2] = "version: %s\n" % new_name
189
        lines[3] = "versioned_root: %s\n" % self._serialize_bool(
190
            self._versioned_root)
191
        lines[4] = "tree_references: %s\n" % self._serialize_bool(
192
            self._tree_references)
193
        return lines
194
195
    def _serialize_bool(self, value):
196
        if value:
197
            return "true"
198
        else:
199
            return "false"
200
4476.3.46 by Andrew Bennetts
Make actual non-rich-root inventories (which have implicit root entries with non-None revisions) roundtrip through inventory-deltas correctly.
201
    def _delta_item_to_line(self, delta_item, new_version):
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
202
        """Convert delta_item to a line."""
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
203
        oldpath, newpath, file_id, entry = delta_item
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
204
        if newpath is None:
205
            # delete
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
206
            oldpath_utf8 = '/' + oldpath.encode('utf8')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
207
            newpath_utf8 = 'None'
208
            parent_id = ''
209
            last_modified = NULL_REVISION
210
            content = 'deleted\x00\x00'
211
        else:
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
212
            if oldpath is None:
213
                oldpath_utf8 = 'None'
214
            else:
215
                oldpath_utf8 = '/' + oldpath.encode('utf8')
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
216
            if newpath == '/':
217
                raise AssertionError(
218
                    "Bad inventory delta: '/' is not a valid newpath "
219
                    "(should be '') in delta item %r" % (delta_item,))
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
220
            # TODO: Test real-world utf8 cache hit rate. It may be a win.
221
            newpath_utf8 = '/' + newpath.encode('utf8')
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
222
            # Serialize None as ''
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
223
            parent_id = entry.parent_id or ''
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
224
            # Serialize unknown revisions as NULL_REVISION
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
225
            last_modified = entry.revision
226
            # special cases for /
227
            if newpath_utf8 == '/' and not self._versioned_root:
4476.3.46 by Andrew Bennetts
Make actual non-rich-root inventories (which have implicit root entries with non-None revisions) roundtrip through inventory-deltas correctly.
228
                # This is an entry for the root, this inventory does not
229
                # support versioned roots.  So this must be an unversioned
4476.3.51 by Andrew Bennetts
Fix deserialization of deletes in inventory deltas, don't give up on serialized deltas from a supports_tree_refs repo unless the delta contains tree refs, and experiment to add automatic_root param to branchbuilder.
230
                # root, i.e. last_modified == new revision.  Otherwise, this
231
                # delta is invalid.
232
                # Note: the non-rich-root repositories *can* have roots with
233
                # file-ids other than TREE_ROOT, e.g. repo formats that use the
234
                # xml5 serializer.
4476.3.46 by Andrew Bennetts
Make actual non-rich-root inventories (which have implicit root entries with non-None revisions) roundtrip through inventory-deltas correctly.
235
                if last_modified != new_version:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
236
                    raise InventoryDeltaError(
4476.3.46 by Andrew Bennetts
Make actual non-rich-root inventories (which have implicit root entries with non-None revisions) roundtrip through inventory-deltas correctly.
237
                        'Version present for / in %s (%s != %s)'
238
                        % (file_id, last_modified, new_version))
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
239
            if last_modified is None:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
240
                raise InventoryDeltaError("no version for fileid %s" % file_id)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
241
            content = self._entry_to_content[entry.kind](entry)
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
242
        return ("%s\x00%s\x00%s\x00%s\x00%s\x00%s\n" %
243
            (oldpath_utf8, newpath_utf8, file_id, parent_id, last_modified,
244
                content))
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
245
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
246
247
class InventoryDeltaDeserializer(object):
248
    """Deserialize inventory deltas."""
249
4476.3.77 by Andrew Bennetts
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.
250
    def __init__(self, allow_versioned_root=True, allow_tree_references=True):
251
        """Create an InventoryDeltaDeserializer.
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
252
253
        :param versioned_root: If True, any root entry that is seen is expected
254
            to be versioned, and root entries can have any fileid.
255
        :param tree_references: If True support tree-reference entries.
256
        """
4476.3.77 by Andrew Bennetts
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.
257
        self._allow_versioned_root = allow_versioned_root
258
        self._allow_tree_references = allow_tree_references
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
259
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
260
    def _deserialize_bool(self, value):
261
        if value == "true":
262
            return True
263
        elif value == "false":
264
            return False
265
        else:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
266
            raise InventoryDeltaError("value %r is not a bool" % (value,))
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
267
268
    def parse_text_bytes(self, bytes):
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
269
        """Parse the text bytes of a serialized inventory delta.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
270
4476.3.12 by Andrew Bennetts
Add some more comments and docstring text to inventory_delta.py.
271
        If versioned_root and/or tree_references flags were set via
272
        require_flags, then the parsed flags must match or a BzrError will be
273
        raised.
274
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
275
        :param bytes: The bytes to parse. This can be obtained by calling
276
            delta_to_lines and then doing ''.join(delta_lines).
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
277
        :return: (parent_id, new_id, versioned_root, tree_references,
278
            inventory_delta)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
279
        """
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
280
        if bytes[-1:] != '\n':
281
            last_line = bytes.rsplit('\n', 1)[-1]
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
282
            raise InventoryDeltaError('last line not empty: %r' % (last_line,))
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
283
        lines = bytes.split('\n')[:-1] # discard the last empty line
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
284
        if not lines or lines[0] != 'format: %s' % FORMAT_1:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
285
            raise InventoryDeltaError('unknown format %r' % lines[0:1])
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
286
        if len(lines) < 2 or not lines[1].startswith('parent: '):
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
287
            raise InventoryDeltaError('missing parent: marker')
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
288
        delta_parent_id = lines[1][8:]
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
289
        if len(lines) < 3 or not lines[2].startswith('version: '):
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
290
            raise InventoryDeltaError('missing version: marker')
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
291
        delta_version_id = lines[2][9:]
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
292
        if len(lines) < 4 or not lines[3].startswith('versioned_root: '):
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
293
            raise InventoryDeltaError('missing versioned_root: marker')
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
294
        delta_versioned_root = self._deserialize_bool(lines[3][16:])
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
295
        if len(lines) < 5 or not lines[4].startswith('tree_references: '):
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
296
            raise InventoryDeltaError('missing tree_references: marker')
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
297
        delta_tree_references = self._deserialize_bool(lines[4][17:])
4476.3.77 by Andrew Bennetts
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.
298
        if (not self._allow_versioned_root and delta_versioned_root):
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
299
            raise IncompatibleInventoryDelta("versioned_root not allowed")
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
300
        result = []
301
        seen_ids = set()
302
        line_iter = iter(lines)
303
        for i in range(5):
304
            line_iter.next()
305
        for line in line_iter:
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
306
            (oldpath_utf8, newpath_utf8, file_id, parent_id, last_modified,
307
                content) = line.split('\x00', 5)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
308
            parent_id = parent_id or None
309
            if file_id in seen_ids:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
310
                raise InventoryDeltaError(
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
311
                    "duplicate file id in inventory delta %r" % lines)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
312
            seen_ids.add(file_id)
4476.3.51 by Andrew Bennetts
Fix deserialization of deletes in inventory deltas, don't give up on serialized deltas from a supports_tree_refs repo unless the delta contains tree refs, and experiment to add automatic_root param to branchbuilder.
313
            if (newpath_utf8 == '/' and not delta_versioned_root and
314
                last_modified != delta_version_id):
4476.3.77 by Andrew Bennetts
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.
315
                    # Delta claims to be not have a versioned root, yet here's
316
                    # a root entry with a non-default version.
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
317
                    raise InventoryDeltaError("Versioned root found: %r" % line)
4476.3.51 by Andrew Bennetts
Fix deserialization of deletes in inventory deltas, don't give up on serialized deltas from a supports_tree_refs repo unless the delta contains tree refs, and experiment to add automatic_root param to branchbuilder.
318
            elif newpath_utf8 != 'None' and last_modified[-1] == ':':
319
                # Deletes have a last_modified of null:, but otherwise special
320
                # revision ids should not occur.
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
321
                raise InventoryDeltaError('special revisionid found: %r' % line)
4476.3.77 by Andrew Bennetts
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.
322
            if content.startswith('tree\x00'):
323
                if delta_tree_references is False:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
324
                    raise InventoryDeltaError(
4476.3.77 by Andrew Bennetts
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.
325
                            "Tree reference found (but header said "
326
                            "tree_references: false): %r" % line)
327
                elif not self._allow_tree_references:
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
328
                    raise IncompatibleInventoryDelta(
329
                        "Tree reference not allowed")
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
330
            if oldpath_utf8 == 'None':
331
                oldpath = None
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
332
            elif oldpath_utf8[:1] != '/':
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
333
                raise InventoryDeltaError(
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
334
                    "oldpath invalid (does not start with /): %r"
335
                    % (oldpath_utf8,))
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
336
            else:
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
337
                oldpath_utf8 = oldpath_utf8[1:]
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
338
                oldpath = oldpath_utf8.decode('utf8')
339
            if newpath_utf8 == 'None':
340
                newpath = None
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
341
            elif newpath_utf8[:1] != '/':
4476.3.78 by Andrew Bennetts
Raise InventoryDeltaErrors, not generic BzrErrors, from inventory_delta.py.
342
                raise InventoryDeltaError(
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
343
                    "newpath invalid (does not start with /): %r"
344
                    % (newpath_utf8,))
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
345
            else:
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
346
                # Trim leading slash
347
                newpath_utf8 = newpath_utf8[1:]
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
348
                newpath = newpath_utf8.decode('utf8')
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
349
            content_tuple = tuple(content.split('\x00'))
4476.3.51 by Andrew Bennetts
Fix deserialization of deletes in inventory deltas, don't give up on serialized deltas from a supports_tree_refs repo unless the delta contains tree refs, and experiment to add automatic_root param to branchbuilder.
350
            if content_tuple[0] == 'deleted':
351
                entry = None
352
            else:
353
                entry = _parse_entry(
4476.3.65 by Andrew Bennetts
Cleaner and faster handling of newpath(_utf8) in inventory_delta.py.
354
                    newpath, file_id, parent_id, last_modified, content_tuple)
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
355
            delta_item = (oldpath, newpath, file_id, entry)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
356
            result.append(delta_item)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
357
        return (delta_parent_id, delta_version_id, delta_versioned_root,
358
                delta_tree_references, result)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
359
360
4476.3.65 by Andrew Bennetts
Cleaner and faster handling of newpath(_utf8) in inventory_delta.py.
361
def _parse_entry(path, file_id, parent_id, last_modified, content):
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
362
    entry_factory = {
363
        'dir': _dir_to_entry,
364
        'file': _file_to_entry,
365
        'link': _link_to_entry,
366
        'tree': _tree_to_entry,
367
    }
368
    kind = content[0]
4476.3.65 by Andrew Bennetts
Cleaner and faster handling of newpath(_utf8) in inventory_delta.py.
369
    if path.startswith('/'):
4476.3.49 by Andrew Bennetts
Start reworking inventory-delta streaming to use a separate substream.
370
        raise AssertionError
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
371
    name = basename(path)
372
    return entry_factory[content[0]](
373
            content, name, parent_id, file_id, last_modified)
374
4476.3.77 by Andrew Bennetts
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.
375