/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/bundle/serializer/v4.py

  • Committer: Jelmer Vernooij
  • Date: 2019-02-03 01:42:11 UTC
  • mto: This revision was merged to the branch mainline in revision 7267.
  • Revision ID: jelmer@jelmer.uk-20190203014211-poj1fv922sejfsb4
Don't require that short git shas have an even number of characters.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from cStringIO import StringIO
 
17
from __future__ import absolute_import
 
18
 
18
19
import bz2
19
20
import re
20
21
 
21
 
from bzrlib import (
22
 
    diff,
 
22
from ... import (
 
23
    bencode,
23
24
    errors,
24
25
    iterablefile,
25
26
    lru_cache,
26
27
    multiparent,
27
28
    osutils,
28
 
    pack,
29
29
    revision as _mod_revision,
30
 
    serializer,
31
30
    trace,
32
31
    ui,
33
32
    )
34
 
from bzrlib.bundle import bundle_data, serializer as bundle_serializer
35
 
from bzrlib import bencode
 
33
from ...bzr import (
 
34
    pack,
 
35
    serializer,
 
36
    versionedfile as _mod_versionedfile,
 
37
    )
 
38
from ...bundle import bundle_data, serializer as bundle_serializer
 
39
from ...i18n import ngettext
 
40
from ...sixish import (
 
41
    BytesIO,
 
42
    viewitems,
 
43
    )
 
44
 
 
45
 
 
46
class _MPDiffInventoryGenerator(_mod_versionedfile._MPDiffGenerator):
 
47
    """Generate Inventory diffs serialized inventories."""
 
48
 
 
49
    def __init__(self, repo, inventory_keys):
 
50
        super(_MPDiffInventoryGenerator, self).__init__(repo.inventories,
 
51
                                                        inventory_keys)
 
52
        self.repo = repo
 
53
        self.sha1s = {}
 
54
 
 
55
    def iter_diffs(self):
 
56
        """Compute the diffs one at a time."""
 
57
        # This is instead of compute_diffs() since we guarantee our ordering of
 
58
        # inventories, we don't have to do any buffering
 
59
        self._find_needed_keys()
 
60
        # We actually use a slightly different ordering. We grab all of the
 
61
        # parents first, and then grab the ordered requests.
 
62
        needed_ids = [k[-1] for k in self.present_parents]
 
63
        needed_ids.extend([k[-1] for k in self.ordered_keys])
 
64
        inv_to_bytes = self.repo._serializer.write_inventory_to_string
 
65
        for inv in self.repo.iter_inventories(needed_ids):
 
66
            revision_id = inv.revision_id
 
67
            key = (revision_id,)
 
68
            if key in self.present_parents:
 
69
                # Not a key we will transmit, which is a shame, since because
 
70
                # of that bundles don't work with stacked branches
 
71
                parent_ids = None
 
72
            else:
 
73
                parent_ids = [k[-1] for k in self.parent_map[key]]
 
74
            as_bytes = inv_to_bytes(inv)
 
75
            self._process_one_record(key, (as_bytes,))
 
76
            if parent_ids is None:
 
77
                continue
 
78
            diff = self.diffs.pop(key)
 
79
            sha1 = osutils.sha_string(as_bytes)
 
80
            yield revision_id, parent_ids, sha1, diff
36
81
 
37
82
 
38
83
class BundleWriter(object):
56
101
 
57
102
    def begin(self):
58
103
        """Start writing the bundle"""
59
 
        self._fileobj.write(bundle_serializer._get_bundle_header(
60
 
            bundle_serializer.v4_string))
61
 
        self._fileobj.write('#\n')
 
104
        self._fileobj.write(bundle_serializer._get_bundle_header('4'))
 
105
        self._fileobj.write(b'#\n')
62
106
        self._container.begin()
63
107
 
64
108
    def end(self):
78
122
        :revision_id: The revision id of the mpdiff being added.
79
123
        :file_id: The file-id of the file, or None for inventories.
80
124
        """
81
 
        metadata = {'parents': parents,
82
 
                    'storage_kind': 'mpdiff',
83
 
                    'sha1': sha1}
 
125
        metadata = {b'parents': parents,
 
126
                    b'storage_kind': b'mpdiff',
 
127
                    b'sha1': sha1}
84
128
        self._add_record(mp_bytes, metadata, repo_kind, revision_id, file_id)
85
129
 
86
130
    def add_fulltext_record(self, bytes, parents, repo_kind, revision_id):
92
136
            'signature'
93
137
        :revision_id: The revision id of the fulltext being added.
94
138
        """
95
 
        metadata = {'parents': parents,
96
 
                    'storage_kind': 'mpdiff'}
97
 
        self._add_record(bytes, {'parents': parents,
98
 
            'storage_kind': 'fulltext'}, repo_kind, revision_id, None)
 
139
        metadata = {b'parents': parents,
 
140
                    b'storage_kind': b'mpdiff'}
 
141
        self._add_record(bytes, {b'parents': parents,
 
142
                                 b'storage_kind': b'fulltext'}, repo_kind, revision_id, None)
99
143
 
100
 
    def add_info_record(self, **kwargs):
 
144
    def add_info_record(self, kwargs):
101
145
        """Add an info record to the bundle
102
146
 
103
147
        Any parameters may be supplied, except 'self' and 'storage_kind'.
104
148
        Values must be lists, strings, integers, dicts, or a combination.
105
149
        """
106
 
        kwargs['storage_kind'] = 'header'
 
150
        kwargs[b'storage_kind'] = b'header'
107
151
        self._add_record(None, kwargs, 'info', None, None)
108
152
 
109
153
    @staticmethod
110
154
    def encode_name(content_kind, revision_id, file_id=None):
111
155
        """Encode semantic ids as a container name"""
112
156
        if content_kind not in ('revision', 'file', 'inventory', 'signature',
113
 
                'info'):
 
157
                                'info'):
114
158
            raise ValueError(content_kind)
115
159
        if content_kind == 'file':
116
160
            if file_id is None:
123
167
                raise AssertionError()
124
168
        elif revision_id is None:
125
169
            raise AssertionError()
126
 
        names = [n.replace('/', '//') for n in
127
 
                 (content_kind, revision_id, file_id) if n is not None]
128
 
        return '/'.join(names)
 
170
        names = [n.replace(b'/', b'//') for n in
 
171
                 (content_kind.encode('ascii'), revision_id, file_id) if n is not None]
 
172
        return b'/'.join(names)
129
173
 
130
174
    def _add_record(self, bytes, metadata, repo_kind, revision_id, file_id):
131
175
        """Add a bundle record to the container.
137
181
        name = self.encode_name(repo_kind, revision_id, file_id)
138
182
        encoded_metadata = bencode.bencode(metadata)
139
183
        self._container.add_bytes_record(encoded_metadata, [(name, )])
140
 
        if metadata['storage_kind'] != 'header':
 
184
        if metadata[b'storage_kind'] != b'header':
141
185
            self._container.add_bytes_record(bytes, [])
142
186
 
143
187
 
164
208
        if stream_input:
165
209
            source_file = iterablefile.IterableFile(self.iter_decode(fileobj))
166
210
        else:
167
 
            source_file = StringIO(bz2.decompress(fileobj.read()))
 
211
            source_file = BytesIO(bz2.decompress(fileobj.read()))
168
212
        self._container_file = source_file
169
213
 
170
214
    @staticmethod
183
227
 
184
228
        :retval: content_kind, revision_id, file_id
185
229
        """
186
 
        segments = re.split('(//?)', name)
187
 
        names = ['']
 
230
        segments = re.split(b'(//?)', name)
 
231
        names = [b'']
188
232
        for segment in segments:
189
 
            if segment == '//':
190
 
                names[-1] += '/'
191
 
            elif segment == '/':
192
 
                names.append('')
 
233
            if segment == b'//':
 
234
                names[-1] += b'/'
 
235
            elif segment == b'/':
 
236
                names.append(b'')
193
237
            else:
194
238
                names[-1] += segment
195
239
        content_kind = names[0]
199
243
            revision_id = names[1]
200
244
        if len(names) > 2:
201
245
            file_id = names[2]
202
 
        return content_kind, revision_id, file_id
 
246
        return content_kind.decode('ascii'), revision_id, file_id
203
247
 
204
248
    def iter_records(self):
205
249
        """Iterate through bundle records
213
257
                raise errors.BadBundle('Record has %d names instead of 1'
214
258
                                       % len(names))
215
259
            metadata = bencode.bdecode(bytes)
216
 
            if metadata['storage_kind'] == 'header':
 
260
            if metadata[b'storage_kind'] == b'header':
217
261
                bytes = None
218
262
            else:
219
 
                _unused, bytes = iterator.next()
 
263
                _unused, bytes = next(iterator)
220
264
            yield (bytes, metadata) + self.decode_name(names[0][0])
221
265
 
222
266
 
223
267
class BundleSerializerV4(bundle_serializer.BundleSerializer):
224
268
    """Implement the high-level bundle interface"""
225
269
 
226
 
    def write(self, repository, revision_ids, forced_bases, fileobj):
227
 
        """Write a bundle to a file-like object
228
 
 
229
 
        For backwards-compatibility only
230
 
        """
231
 
        write_op = BundleWriteOperation.from_old_args(repository, revision_ids,
232
 
                                                      forced_bases, fileobj)
233
 
        return write_op.do_write()
234
 
 
235
270
    def write_bundle(self, repository, target, base, fileobj):
236
271
        """Write a bundle to a file object
237
272
 
241
276
            at.
242
277
        :param fileobj: The file-like object to write to
243
278
        """
244
 
        write_op =  BundleWriteOperation(base, target, repository, fileobj)
 
279
        write_op = BundleWriteOperation(base, target, repository, fileobj)
245
280
        return write_op.do_write()
246
281
 
247
282
    def read(self, file):
252
287
    @staticmethod
253
288
    def get_source_serializer(info):
254
289
        """Retrieve the serializer for a given info object"""
255
 
        return serializer.format_registry.get(info['serializer'])
 
290
        return serializer.format_registry.get(info[b'serializer'].decode('ascii'))
256
291
 
257
292
 
258
293
class BundleWriteOperation(object):
259
294
    """Perform the operation of writing revisions to a bundle"""
260
295
 
261
 
    @classmethod
262
 
    def from_old_args(cls, repository, revision_ids, forced_bases, fileobj):
263
 
        """Create a BundleWriteOperation from old-style arguments"""
264
 
        base, target = cls.get_base_target(revision_ids, forced_bases,
265
 
                                           repository)
266
 
        return BundleWriteOperation(base, target, repository, fileobj,
267
 
                                    revision_ids)
268
 
 
269
296
    def __init__(self, base, target, repository, fileobj, revision_ids=None):
270
297
        self.base = base
271
298
        self.target = target
280
307
            # Strip ghosts
281
308
            parents = graph.get_parent_map(revision_ids)
282
309
            self.revision_ids = [r for r in revision_ids if r in parents]
283
 
        self.revision_keys = set([(revid,) for revid in self.revision_ids])
 
310
        self.revision_keys = {(revid,) for revid in self.revision_ids}
284
311
 
285
312
    def do_write(self):
286
313
        """Write all data to the bundle"""
287
 
        trace.note('Bundling %d revision(s).', len(self.revision_ids))
288
 
        self.repository.lock_read()
289
 
        try:
 
314
        trace.note(ngettext('Bundling %d revision.', 'Bundling %d revisions.',
 
315
                            len(self.revision_ids)), len(self.revision_ids))
 
316
        with self.repository.lock_read():
290
317
            self.bundle.begin()
291
318
            self.write_info()
292
319
            self.write_files()
293
320
            self.write_revisions()
294
321
            self.bundle.end()
295
 
        finally:
296
 
            self.repository.unlock()
297
322
        return self.revision_ids
298
323
 
299
324
    def write_info(self):
301
326
        serializer_format = self.repository.get_serializer_format()
302
327
        supports_rich_root = {True: 1, False: 0}[
303
328
            self.repository.supports_rich_root()]
304
 
        self.bundle.add_info_record(serializer=serializer_format,
305
 
                                    supports_rich_root=supports_rich_root)
 
329
        self.bundle.add_info_record({b'serializer': serializer_format,
 
330
                                     b'supports_rich_root': supports_rich_root})
306
331
 
307
332
    def write_files(self):
308
333
        """Write bundle records for all revisions of all files"""
309
334
        text_keys = []
310
335
        altered_fileids = self.repository.fileids_altered_by_revision_ids(
311
 
                self.revision_ids)
312
 
        for file_id, revision_ids in altered_fileids.iteritems():
 
336
            self.revision_ids)
 
337
        for file_id, revision_ids in viewitems(altered_fileids):
313
338
            for revision_id in revision_ids:
314
339
                text_keys.append((file_id, revision_id))
315
340
        self._add_mp_records_keys('file', self.repository.texts, text_keys)
318
343
        """Write bundle records for all revisions and signatures"""
319
344
        inv_vf = self.repository.inventories
320
345
        topological_order = [key[-1] for key in multiparent.topo_iter_keys(
321
 
                                inv_vf, self.revision_keys)]
 
346
            inv_vf, self.revision_keys)]
322
347
        revision_order = topological_order
323
348
        if self.target is not None and self.target in self.revision_ids:
324
349
            # Make sure the target revision is always the last entry
348
373
        the other side.
349
374
        """
350
375
        inventory_key_order = [(r,) for r in revision_order]
351
 
        parent_map = self.repository.inventories.get_parent_map(
352
 
                            inventory_key_order)
353
 
        missing_keys = set(inventory_key_order).difference(parent_map)
354
 
        if missing_keys:
355
 
            raise errors.RevisionNotPresent(list(missing_keys)[0],
356
 
                                            self.repository.inventories)
357
 
        inv_to_str = self.repository._serializer.write_inventory_to_string
358
 
        # Make sure that we grab the parent texts first
359
 
        just_parents = set()
360
 
        map(just_parents.update, parent_map.itervalues())
361
 
        just_parents.difference_update(parent_map)
362
 
        # Ignore ghost parents
363
 
        present_parents = self.repository.inventories.get_parent_map(
364
 
                            just_parents)
365
 
        ghost_keys = just_parents.difference(present_parents)
366
 
        needed_inventories = list(present_parents) + inventory_key_order
367
 
        needed_inventories = [k[-1] for k in needed_inventories]
368
 
        all_lines = {}
369
 
        for inv in self.repository.iter_inventories(needed_inventories):
370
 
            revision_id = inv.revision_id
371
 
            key = (revision_id,)
372
 
            as_bytes = inv_to_str(inv)
373
 
            # The sha1 is validated as the xml/textual form, not as the
374
 
            # form-in-the-repository
375
 
            sha1 = osutils.sha_string(as_bytes)
376
 
            as_lines = osutils.split_lines(as_bytes)
377
 
            del as_bytes
378
 
            all_lines[key] = as_lines
379
 
            if key in just_parents:
380
 
                # We don't transmit those entries
381
 
                continue
382
 
            # Create an mpdiff for this text, and add it to the output
383
 
            parent_keys = parent_map[key]
384
 
            # See the comment in VF.make_mpdiffs about how this effects
385
 
            # ordering when there are ghosts present. I think we have a latent
386
 
            # bug
387
 
            parent_lines = [all_lines[p_key] for p_key in parent_keys
388
 
                            if p_key not in ghost_keys]
389
 
            diff = multiparent.MultiParent.from_lines(
390
 
                as_lines, parent_lines)
391
 
            text = ''.join(diff.to_patch())
392
 
            parent_ids = [k[-1] for k in parent_keys]
 
376
        generator = _MPDiffInventoryGenerator(self.repository,
 
377
                                              inventory_key_order)
 
378
        for revision_id, parent_ids, sha1, diff in generator.iter_diffs():
 
379
            text = b''.join(diff.to_patch())
393
380
            self.bundle.add_multiparent_record(text, sha1, parent_ids,
394
381
                                               'inventory', revision_id, None)
395
382
 
396
383
    def _add_revision_texts(self, revision_order):
397
384
        parent_map = self.repository.get_parent_map(revision_order)
398
 
        revision_to_str = self.repository._serializer.write_revision_to_string
 
385
        revision_to_bytes = self.repository._serializer.write_revision_to_string
399
386
        revisions = self.repository.get_revisions(revision_order)
400
387
        for revision in revisions:
401
388
            revision_id = revision.revision_id
402
389
            parents = parent_map.get(revision_id, None)
403
 
            revision_text = revision_to_str(revision)
 
390
            revision_text = revision_to_bytes(revision)
404
391
            self.bundle.add_fulltext_record(revision_text, parents,
405
 
                                       'revision', revision_id)
 
392
                                            'revision', revision_id)
406
393
            try:
407
394
                self.bundle.add_fulltext_record(
408
395
                    self.repository.get_signature_text(
409
 
                    revision_id), parents, 'signature', revision_id)
 
396
                        revision_id), parents, 'signature', revision_id)
410
397
            except errors.NoSuchRevision:
411
398
                pass
412
399
 
434
421
        for mpdiff, item_key, in zip(mpdiffs, ordered_keys):
435
422
            sha1 = sha1s[item_key]
436
423
            parents = [key[-1] for key in parent_map[item_key]]
437
 
            text = ''.join(mpdiff.to_patch())
 
424
            text = b''.join(mpdiff.to_patch())
438
425
            # Infer file id records as appropriate.
439
426
            if len(item_key) == 2:
440
427
                file_id = item_key[0]
447
434
class BundleInfoV4(object):
448
435
 
449
436
    """Provide (most of) the BundleInfo interface"""
 
437
 
450
438
    def __init__(self, fileobj, serializer):
451
439
        self._fileobj = fileobj
452
440
        self._serializer = serializer
464
452
            all into memory at once.  Reading it into memory all at once is
465
453
            (currently) faster.
466
454
        """
467
 
        repository.lock_write()
468
 
        try:
 
455
        with repository.lock_write():
469
456
            ri = RevisionInstaller(self.get_bundle_reader(stream_input),
470
457
                                   self._serializer, repository)
471
458
            return ri.install()
472
 
        finally:
473
 
            repository.unlock()
474
459
 
475
460
    def get_merge_request(self, target_repo):
476
461
        """Provide data for performing a merge
494
479
            self.__real_revisions = []
495
480
            bundle_reader = self.get_bundle_reader()
496
481
            for bytes, metadata, repo_kind, revision_id, file_id in \
497
 
                bundle_reader.iter_records():
 
482
                    bundle_reader.iter_records():
498
483
                if repo_kind == 'info':
499
484
                    serializer =\
500
485
                        self._serializer.get_source_serializer(metadata)
552
537
        added_inv = set()
553
538
        target_revision = None
554
539
        for bytes, metadata, repo_kind, revision_id, file_id in\
555
 
            self._container.iter_records():
 
540
                self._container.iter_records():
556
541
            if repo_kind == 'info':
557
542
                if self._info is not None:
558
543
                    raise AssertionError()
559
544
                self._handle_info(metadata)
560
545
            if (pending_file_records and
561
 
                (repo_kind, file_id) != ('file', current_file)):
 
546
                    (repo_kind, file_id) != ('file', current_file)):
562
547
                # Flush the data for a single file - prevents memory
563
548
                # spiking due to buffering all files in memory.
564
549
                self._install_mp_records_keys(self._repository.texts,
565
 
                    pending_file_records)
 
550
                                              pending_file_records)
566
551
                current_file = None
567
552
                del pending_file_records[:]
568
553
            if len(pending_inventory_records) > 0 and repo_kind != 'inventory':
569
554
                self._install_inventory_records(pending_inventory_records)
570
555
                pending_inventory_records = []
571
556
            if repo_kind == 'inventory':
572
 
                pending_inventory_records.append(((revision_id,), metadata, bytes))
 
557
                pending_inventory_records.append(
 
558
                    ((revision_id,), metadata, bytes))
573
559
            if repo_kind == 'revision':
574
560
                target_revision = revision_id
575
561
                self._install_revision(revision_id, metadata, bytes)
577
563
                self._install_signature(revision_id, metadata, bytes)
578
564
            if repo_kind == 'file':
579
565
                current_file = file_id
580
 
                pending_file_records.append(((file_id, revision_id), metadata, bytes))
581
 
        self._install_mp_records_keys(self._repository.texts, pending_file_records)
 
566
                pending_file_records.append(
 
567
                    ((file_id, revision_id), metadata, bytes))
 
568
        self._install_mp_records_keys(
 
569
            self._repository.texts, pending_file_records)
582
570
        return target_revision
583
571
 
584
572
    def _handle_info(self, info):
585
573
        """Extract data from an info record"""
586
574
        self._info = info
587
575
        self._source_serializer = self._serializer.get_source_serializer(info)
588
 
        if (info['supports_rich_root'] == 0 and
589
 
            self._repository.supports_rich_root()):
 
576
        if (info[b'supports_rich_root'] == 0 and
 
577
                self._repository.supports_rich_root()):
590
578
            self.update_root = True
591
579
        else:
592
580
            self.update_root = False
612
600
                prefix = key[:1]
613
601
            else:
614
602
                prefix = ()
615
 
            parents = [prefix + (parent,) for parent in meta['parents']]
616
 
            vf_records.append((key, parents, meta['sha1'], d_func(text)))
 
603
            parents = [prefix + (parent,) for parent in meta[b'parents']]
 
604
            vf_records.append((key, parents, meta[b'sha1'], d_func(text)))
617
605
        versionedfile.add_mpdiffs(vf_records)
618
606
 
619
607
    def _get_parent_inventory_texts(self, inventory_text_cache,
635
623
            # installed yet.)
636
624
            parent_keys = [(r,) for r in remaining_parent_ids]
637
625
            present_parent_map = self._repository.inventories.get_parent_map(
638
 
                                        parent_keys)
 
626
                parent_keys)
639
627
            present_parent_ids = []
640
628
            ghosts = set()
641
629
            for p_id in remaining_parent_ids:
645
633
                    ghosts.add(p_id)
646
634
            to_string = self._source_serializer.write_inventory_to_string
647
635
            for parent_inv in self._repository.iter_inventories(
648
 
                                    present_parent_ids):
 
636
                    present_parent_ids):
649
637
                p_text = to_string(parent_inv)
650
638
                inventory_cache[parent_inv.revision_id] = parent_inv
651
639
                cached_parent_texts[parent_inv.revision_id] = p_text
653
641
 
654
642
        parent_texts = [cached_parent_texts[parent_id]
655
643
                        for parent_id in parent_ids
656
 
                         if parent_id not in ghosts]
 
644
                        if parent_id not in ghosts]
657
645
        return parent_texts
658
646
 
659
647
    def _install_inventory_records(self, records):
660
 
        if (self._info['serializer'] == self._repository._serializer.format_num
661
 
            and self._repository._serializer.support_altered_by_hack):
 
648
        if (self._info[b'serializer'] == self._repository._serializer.format_num
 
649
                and self._repository._serializer.support_altered_by_hack):
662
650
            return self._install_mp_records_keys(self._repository.inventories,
663
 
                records)
 
651
                                                 records)
664
652
        # Use a 10MB text cache, since these are string xml inventories. Note
665
653
        # that 10MB is fairly small for large projects (a single inventory can
666
654
        # be >5MB). Another possibility is to cache 10-20 inventory texts
667
655
        # instead
668
 
        inventory_text_cache = lru_cache.LRUSizeCache(10*1024*1024)
 
656
        inventory_text_cache = lru_cache.LRUSizeCache(10 * 1024 * 1024)
669
657
        # Also cache the in-memory representation. This allows us to create
670
658
        # inventory deltas to apply rather than calling add_inventory from
671
659
        # scratch each time.
672
660
        inventory_cache = lru_cache.LRUCache(10)
673
 
        pb = ui.ui_factory.nested_progress_bar()
674
 
        try:
 
661
        with ui.ui_factory.nested_progress_bar() as pb:
675
662
            num_records = len(records)
676
663
            for idx, (key, metadata, bytes) in enumerate(records):
677
664
                pb.update('installing inventory', idx, num_records)
678
665
                revision_id = key[-1]
679
 
                parent_ids = metadata['parents']
 
666
                parent_ids = metadata[b'parents']
680
667
                # Note: This assumes the local ghosts are identical to the
681
668
                #       ghosts in the source, as the Bundle serialization
682
669
                #       format doesn't record ghosts.
687
674
                # it would have to cast to a list of lines, which we get back
688
675
                # as lines and then cast back to a string.
689
676
                target_lines = multiparent.MultiParent.from_patch(bytes
690
 
                            ).to_lines(p_texts)
691
 
                inv_text = ''.join(target_lines)
 
677
                                                                  ).to_lines(p_texts)
 
678
                inv_text = b''.join(target_lines)
692
679
                del target_lines
693
680
                sha1 = osutils.sha_string(inv_text)
694
 
                if sha1 != metadata['sha1']:
 
681
                if sha1 != metadata[b'sha1']:
695
682
                    raise errors.BadBundle("Can't convert to target format")
696
683
                # Add this to the cache so we don't have to extract it again.
697
684
                inventory_text_cache[revision_id] = inv_text
708
695
                    else:
709
696
                        delta = target_inv._make_delta(parent_inv)
710
697
                        self._repository.add_inventory_by_delta(parent_ids[0],
711
 
                            delta, revision_id, parent_ids)
 
698
                                                                delta, revision_id, parent_ids)
712
699
                except errors.UnsupportedInventoryKind:
713
700
                    raise errors.IncompatibleRevision(repr(self._repository))
714
701
                inventory_cache[revision_id] = target_inv
715
 
        finally:
716
 
            pb.finished()
717
702
 
718
703
    def _handle_root(self, target_inv, parent_ids):
719
704
        revision_id = target_inv.revision_id
720
705
        if self.update_root:
721
706
            text_key = (target_inv.root.file_id, revision_id)
722
707
            parent_keys = [(target_inv.root.file_id, parent) for
723
 
                parent in parent_ids]
 
708
                           parent in parent_ids]
724
709
            self._repository.texts.add_lines(text_key, parent_keys, [])
725
710
        elif not self._repository.supports_rich_root():
726
711
            if target_inv.root.revision != revision_id: