/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/bundle_data.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-18 18:23:32 UTC
  • mto: This revision was merged to the branch mainline in revision 7197.
  • Revision ID: jelmer@jelmer.uk-20181118182332-viz1qvqese2mo9i6
Fix some more Bazaar references.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2005-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
16
16
 
17
17
"""Read in a bundle stream, and process it into a BundleReader object."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
import base64
20
 
from cStringIO import StringIO
 
22
from io import BytesIO
21
23
import os
22
24
import pprint
23
25
 
24
 
from bzrlib import (
 
26
from .. import (
 
27
    cache_utf8,
25
28
    osutils,
26
29
    timestamp,
27
30
    )
28
 
import bzrlib.errors
29
 
from bzrlib.bundle import apply_bundle
30
 
from bzrlib.errors import (TestamentMismatch, BzrError,
31
 
                           MalformedHeader, MalformedPatches, NotABundle)
32
 
from bzrlib.inventory import (Inventory, InventoryEntry,
33
 
                              InventoryDirectory, InventoryFile,
34
 
                              InventoryLink)
35
 
from bzrlib.osutils import sha_file, sha_string, pathjoin
36
 
from bzrlib.revision import Revision, NULL_REVISION
37
 
from bzrlib.testament import StrictTestament
38
 
from bzrlib.trace import mutter, warning
39
 
import bzrlib.transport
40
 
from bzrlib.tree import Tree
41
 
import bzrlib.urlutils
42
 
from bzrlib.xml5 import serializer_v5
 
31
from . import apply_bundle
 
32
from ..errors import (
 
33
    TestamentMismatch,
 
34
    BzrError,
 
35
    )
 
36
from ..bzr.inventory import (
 
37
    Inventory,
 
38
    InventoryDirectory,
 
39
    InventoryFile,
 
40
    InventoryLink,
 
41
    )
 
42
from ..osutils import sha_string, pathjoin
 
43
from ..revision import Revision, NULL_REVISION
 
44
from ..sixish import (
 
45
    viewitems,
 
46
    )
 
47
from ..testament import StrictTestament
 
48
from ..trace import mutter, warning
 
49
from ..tree import Tree
 
50
from ..bzr.xml5 import serializer_v5
43
51
 
44
52
 
45
53
class RevisionInfo(object):
46
54
    """Gets filled out for each revision object that is read.
47
55
    """
 
56
 
48
57
    def __init__(self, revision_id):
49
58
        self.revision_id = revision_id
50
59
        self.sha1 = None
65
74
 
66
75
    def as_revision(self):
67
76
        rev = Revision(revision_id=self.revision_id,
68
 
            committer=self.committer,
69
 
            timestamp=float(self.timestamp),
70
 
            timezone=int(self.timezone),
71
 
            inventory_sha1=self.inventory_sha1,
72
 
            message='\n'.join(self.message))
 
77
                       committer=self.committer,
 
78
                       timestamp=float(self.timestamp),
 
79
                       timezone=int(self.timezone),
 
80
                       inventory_sha1=self.inventory_sha1,
 
81
                       message='\n'.join(self.message))
73
82
 
74
83
        if self.parent_ids:
75
84
            rev.parent_ids.extend(self.parent_ids)
84
93
                    value = ''
85
94
                else:
86
95
                    key = str(property[:key_end])
87
 
                    value = property[key_end+2:]
 
96
                    value = property[key_end + 2:]
88
97
                rev.properties[key] = value
89
98
 
90
99
        return rev
99
108
        revision_info.timestamp = revision.timestamp
100
109
        revision_info.message = revision.message.split('\n')
101
110
        revision_info.properties = [': '.join(p) for p in
102
 
                                    revision.properties.iteritems()]
 
111
                                    viewitems(revision.properties)]
103
112
        return revision_info
104
113
 
105
114
 
107
116
    """This contains the meta information. Stuff that allows you to
108
117
    recreate the revision or inventory XML.
109
118
    """
 
119
 
110
120
    def __init__(self, bundle_format=None):
111
121
        self.bundle_format = None
112
122
        self.committer = None
136
146
        split up, based on the assumptions that can be made
137
147
        when information is missing.
138
148
        """
139
 
        from bzrlib.timestamp import unpack_highres_date
 
149
        from breezy.timestamp import unpack_highres_date
140
150
        # Put in all of the guessable information.
141
151
        if not self.timestamp and self.date:
142
152
            self.timestamp, self.timezone = unpack_highres_date(self.date)
146
156
            if rev.timestamp is None:
147
157
                if rev.date is not None:
148
158
                    rev.timestamp, rev.timezone = \
149
 
                            unpack_highres_date(rev.date)
 
159
                        unpack_highres_date(rev.date)
150
160
                else:
151
161
                    rev.timestamp = self.timestamp
152
162
                    rev.timezone = self.timezone
201
211
        revision_info = self.get_revision_info(revision_id)
202
212
        inventory_revision_id = revision_id
203
213
        bundle_tree = BundleTree(repository.revision_tree(base),
204
 
                                  inventory_revision_id)
 
214
                                 inventory_revision_id)
205
215
        self._update_tree(bundle_tree, revision_id)
206
216
 
207
217
        inv = bundle_tree.inventory
208
218
        self._validate_inventory(inv, revision_id)
209
 
        self._validate_revision(inv, revision_id)
 
219
        self._validate_revision(bundle_tree, revision_id)
210
220
 
211
221
        return bundle_tree
212
222
 
217
227
        """
218
228
        rev_to_sha = {}
219
229
        inv_to_sha = {}
 
230
 
220
231
        def add_sha(d, revision_id, sha1):
221
232
            if revision_id is None:
222
233
                if sha1 is not None:
223
234
                    raise BzrError('A Null revision should always'
224
 
                        'have a null sha1 hash')
 
235
                                   'have a null sha1 hash')
225
236
                return
226
237
            if revision_id in d:
227
238
                # This really should have been validated as part
228
239
                # of _validate_revisions but lets do it again
229
240
                if sha1 != d[revision_id]:
230
241
                    raise BzrError('** Revision %r referenced with 2 different'
231
 
                            ' sha hashes %s != %s' % (revision_id,
232
 
                                sha1, d[revision_id]))
 
242
                                   ' sha hashes %s != %s' % (revision_id,
 
243
                                                             sha1, d[revision_id]))
233
244
            else:
234
245
                d[revision_id] = sha1
235
246
 
245
256
 
246
257
        count = 0
247
258
        missing = {}
248
 
        for revision_id, sha1 in rev_to_sha.iteritems():
 
259
        for revision_id, sha1 in viewitems(rev_to_sha):
249
260
            if repository.has_revision(revision_id):
250
261
                testament = StrictTestament.from_revision(repository,
251
262
                                                          revision_id)
253
264
                                                                revision_id)
254
265
                if sha1 != local_sha1:
255
266
                    raise BzrError('sha1 mismatch. For revision id {%s}'
256
 
                            'local: %s, bundle: %s' % (revision_id, local_sha1, sha1))
 
267
                                   'local: %s, bundle: %s' % (revision_id, local_sha1, sha1))
257
268
                else:
258
269
                    count += 1
259
270
            elif revision_id not in checked:
278
289
        if rev.revision_id != revision_id:
279
290
            raise AssertionError()
280
291
        if sha1 != rev.inventory_sha1:
281
 
            open(',,bogus-inv', 'wb').write(s)
 
292
            with open(',,bogus-inv', 'wb') as f:
 
293
                f.write(s)
282
294
            warning('Inventory sha hash mismatch for revision %s. %s'
283
295
                    ' != %s' % (revision_id, sha1, rev.inventory_sha1))
284
296
 
285
 
    def _validate_revision(self, inventory, revision_id):
 
297
    def _validate_revision(self, tree, revision_id):
286
298
        """Make sure all revision entries match their checksum."""
287
299
 
288
 
        # This is a mapping from each revision id to it's sha hash
 
300
        # This is a mapping from each revision id to its sha hash
289
301
        rev_to_sha1 = {}
290
302
 
291
303
        rev = self.get_revision(revision_id)
294
306
            raise AssertionError()
295
307
        if not (rev.revision_id == revision_id):
296
308
            raise AssertionError()
297
 
        sha1 = self._testament_sha1(rev, inventory)
 
309
        sha1 = self._testament_sha1(rev, tree)
298
310
        if sha1 != rev_info.sha1:
299
311
            raise TestamentMismatch(rev.revision_id, rev_info.sha1, sha1)
300
312
        if rev.revision_id in rev_to_sha1:
301
313
            raise BzrError('Revision {%s} given twice in the list'
302
 
                    % (rev.revision_id))
 
314
                           % (rev.revision_id))
303
315
        rev_to_sha1[rev.revision_id] = sha1
304
316
 
305
317
    def _update_tree(self, bundle_tree, revision_id):
313
325
            if last_changed is not None:
314
326
                # last_changed will be a Unicode string because of how it was
315
327
                # read. Convert it back to utf8.
316
 
                changed_revision_id = osutils.safe_revision_id(last_changed,
317
 
                                                               warn=False)
 
328
                changed_revision_id = cache_utf8.encode(last_changed)
318
329
            else:
319
330
                changed_revision_id = revision_id
320
331
            bundle_tree.note_last_changed(path, changed_revision_id)
327
338
                try:
328
339
                    name, value = info_item.split(':', 1)
329
340
                except ValueError:
330
 
                    raise 'Value %r has no colon' % info_item
 
341
                    raise ValueError('Value %r has no colon' % info_item)
331
342
                if name == 'last-changed':
332
343
                    last_changed = value
333
344
                elif name == 'executable':
341
352
 
342
353
        def do_patch(path, lines, encoding):
343
354
            if encoding == 'base64':
344
 
                patch = base64.decodestring(''.join(lines))
 
355
                patch = base64.b64decode(b''.join(lines))
345
356
            elif encoding is None:
346
 
                patch =  ''.join(lines)
 
357
                patch = b''.join(lines)
347
358
            else:
348
359
                raise ValueError(encoding)
349
360
            bundle_tree.note_patch(path, patch)
352
363
            info = extra.split(' // ')
353
364
            if len(info) < 2:
354
365
                raise BzrError('renamed action lines need both a from and to'
355
 
                        ': %r' % extra)
 
366
                               ': %r' % extra)
356
367
            old_path = info[0]
357
368
            if info[1].startswith('=> '):
358
369
                new_path = info[1][3:]
371
382
                # TODO: in the future we might allow file ids to be
372
383
                # given for removed entries
373
384
                raise BzrError('removed action lines should only have the path'
374
 
                        ': %r' % extra)
 
385
                               ': %r' % extra)
375
386
            path = info[0]
376
387
            bundle_tree.note_deletion(path)
377
388
 
379
390
            info = extra.split(' // ')
380
391
            if len(info) <= 1:
381
392
                raise BzrError('add action lines require the path and file id'
382
 
                        ': %r' % extra)
 
393
                               ': %r' % extra)
383
394
            elif len(info) > 5:
384
395
                raise BzrError('add action lines have fewer than 5 entries.'
385
 
                        ': %r' % extra)
 
396
                               ': %r' % extra)
386
397
            path = info[0]
387
398
            if not info[1].startswith('file-id:'):
388
399
                raise BzrError('The file-id should follow the path for an add'
389
 
                        ': %r' % extra)
 
400
                               ': %r' % extra)
390
401
            # This will be Unicode because of how the stream is read. Turn it
391
402
            # back into a utf8 file_id
392
 
            file_id = osutils.safe_file_id(info[1][8:], warn=False)
 
403
            file_id = cache_utf8.encode(info[1][8:])
393
404
 
394
405
            bundle_tree.note_id(file_id, path, kind)
395
406
            # this will be overridden in extra_info if executable is specified.
404
415
            info = extra.split(' // ')
405
416
            if len(info) < 1:
406
417
                raise BzrError('modified action lines have at least'
407
 
                        'the path in them: %r' % extra)
 
418
                               'the path in them: %r' % extra)
408
419
            path = info[0]
409
420
 
410
421
            last_modified, encoding = extra_info(info[1:], path)
413
424
                do_patch(path, lines, encoding)
414
425
 
415
426
        valid_actions = {
416
 
            'renamed':renamed,
417
 
            'removed':removed,
418
 
            'added':added,
419
 
            'modified':modified
 
427
            'renamed': renamed,
 
428
            'removed': removed,
 
429
            'added': added,
 
430
            'modified': modified
420
431
        }
421
432
        for action_line, lines in \
422
 
            self.get_revision_info(revision_id).tree_actions:
 
433
                self.get_revision_info(revision_id).tree_actions:
423
434
            first = action_line.find(' ')
424
435
            if first == -1:
425
436
                raise BzrError('Bogus action line'
426
 
                        ' (no opening space): %r' % action_line)
427
 
            second = action_line.find(' ', first+1)
 
437
                               ' (no opening space): %r' % action_line)
 
438
            second = action_line.find(' ', first + 1)
428
439
            if second == -1:
429
440
                raise BzrError('Bogus action line'
430
 
                        ' (missing second space): %r' % action_line)
 
441
                               ' (missing second space): %r' % action_line)
431
442
            action = action_line[:first]
432
 
            kind = action_line[first+1:second]
 
443
            kind = action_line[first + 1:second]
433
444
            if kind not in ('file', 'directory', 'symlink'):
434
445
                raise BzrError('Bogus action line'
435
 
                        ' (invalid object kind %r): %r' % (kind, action_line))
436
 
            extra = action_line[second+1:]
 
446
                               ' (invalid object kind %r): %r' % (kind, action_line))
 
447
            extra = action_line[second + 1:]
437
448
 
438
449
            if action not in valid_actions:
439
450
                raise BzrError('Bogus action line'
440
 
                        ' (unrecognized action): %r' % action_line)
 
451
                               ' (unrecognized action): %r' % action_line)
441
452
            valid_actions[action](kind, extra, lines)
442
453
 
443
454
    def install_revisions(self, target_repo, stream_input=True):
458
469
 
459
470
 
460
471
class BundleTree(Tree):
 
472
 
461
473
    def __init__(self, base_tree, revision_id):
462
474
        self.base_tree = base_tree
463
 
        self._renamed = {} # Mapping from old_path => new_path
464
 
        self._renamed_r = {} # new_path => old_path
465
 
        self._new_id = {} # new_path => new_id
466
 
        self._new_id_r = {} # new_id => new_path
467
 
        self._kinds = {} # new_id => kind
468
 
        self._last_changed = {} # new_id => revision_id
469
 
        self._executable = {} # new_id => executable value
 
475
        self._renamed = {}  # Mapping from old_path => new_path
 
476
        self._renamed_r = {}  # new_path => old_path
 
477
        self._new_id = {}  # new_path => new_id
 
478
        self._new_id_r = {}  # new_id => new_path
 
479
        self._kinds = {}  # new_path => kind
 
480
        self._last_changed = {}  # new_id => revision_id
 
481
        self._executable = {}  # new_id => executable value
470
482
        self.patches = {}
471
 
        self._targets = {} # new path => new symlink target
 
483
        self._targets = {}  # new path => new symlink target
472
484
        self.deleted = []
473
485
        self.contents_by_id = True
474
486
        self.revision_id = revision_id
490
502
        """Files that don't exist in base need a new id."""
491
503
        self._new_id[new_path] = new_id
492
504
        self._new_id_r[new_id] = new_path
493
 
        self._kinds[new_id] = kind
 
505
        self._kinds[new_path] = kind
494
506
 
495
507
    def note_last_changed(self, file_id, revision_id):
496
508
        if (file_id in self._last_changed
497
509
                and self._last_changed[file_id] != revision_id):
498
510
            raise BzrError('Mismatched last-changed revision for file_id {%s}'
499
 
                    ': %s != %s' % (file_id,
500
 
                                    self._last_changed[file_id],
501
 
                                    revision_id))
 
511
                           ': %s != %s' % (file_id,
 
512
                                           self._last_changed[file_id],
 
513
                                           revision_id))
502
514
        self._last_changed[file_id] = revision_id
503
515
 
504
516
    def note_patch(self, new_path, patch):
523
535
        old_path = self._renamed.get(new_path)
524
536
        if old_path is not None:
525
537
            return old_path
526
 
        dirname,basename = os.path.split(new_path)
 
538
        dirname, basename = os.path.split(new_path)
527
539
        # dirname is not '' doesn't work, because
528
540
        # dirname may be a unicode entry, and is
529
541
        # requires the objects to be identical
535
547
                old_path = pathjoin(old_dir, basename)
536
548
        else:
537
549
            old_path = new_path
538
 
        #If the new path wasn't in renamed, the old one shouldn't be in
539
 
        #renamed_r
 
550
        # If the new path wasn't in renamed, the old one shouldn't be in
 
551
        # renamed_r
540
552
        if old_path in self._renamed_r:
541
553
            return None
542
554
        return old_path
552
564
            return new_path
553
565
        if new_path in self._renamed:
554
566
            return None
555
 
        dirname,basename = os.path.split(old_path)
 
567
        dirname, basename = os.path.split(old_path)
556
568
        if dirname != '':
557
569
            new_dir = self.new_path(dirname)
558
570
            if new_dir is None:
561
573
                new_path = pathjoin(new_dir, basename)
562
574
        else:
563
575
            new_path = old_path
564
 
        #If the old path wasn't in renamed, the new one shouldn't be in
565
 
        #renamed_r
 
576
        # If the old path wasn't in renamed, the new one shouldn't be in
 
577
        # renamed_r
566
578
        if new_path in self._renamed:
567
579
            return None
568
580
        return new_path
569
581
 
 
582
    def get_root_id(self):
 
583
        return self.path2id('')
 
584
 
570
585
    def path2id(self, path):
571
586
        """Return the id of the file present at path in the target tree."""
572
587
        file_id = self._new_id.get(path)
577
592
            return None
578
593
        if old_path in self.deleted:
579
594
            return None
580
 
        if getattr(self.base_tree, 'path2id', None) is not None:
581
 
            return self.base_tree.path2id(old_path)
582
 
        else:
583
 
            return self.base_tree.inventory.path2id(old_path)
 
595
        return self.base_tree.path2id(old_path)
584
596
 
585
597
    def id2path(self, file_id):
586
598
        """Return the new path in the target tree of the file with id file_id"""
606
618
        new_path = self.id2path(file_id)
607
619
        return self.base_tree.path2id(new_path)
608
620
 
609
 
    def get_file(self, file_id):
 
621
    def get_file(self, path):
610
622
        """Return a file-like object containing the new contents of the
611
623
        file given by file_id.
612
624
 
614
626
                in the text-store, so that the file contents would
615
627
                then be cached.
616
628
        """
 
629
        file_id = self.path2id(path)
617
630
        base_id = self.old_contents_id(file_id)
618
631
        if (base_id is not None and
619
 
            base_id != self.base_tree.inventory.root.file_id):
620
 
            patch_original = self.base_tree.get_file(base_id)
 
632
                base_id != self.base_tree.get_root_id()):
 
633
            old_path = self.base_tree.id2path(base_id)
 
634
            patch_original = self.base_tree.get_file(old_path)
621
635
        else:
622
636
            patch_original = None
623
 
        file_patch = self.patches.get(self.id2path(file_id))
 
637
        file_patch = self.patches.get(path)
624
638
        if file_patch is None:
625
639
            if (patch_original is None and
626
 
                self.get_kind(file_id) == 'directory'):
627
 
                return StringIO()
 
640
                    self.kind(path) == 'directory'):
 
641
                return BytesIO()
628
642
            if patch_original is None:
629
643
                raise AssertionError("None: %s" % file_id)
630
644
            return patch_original
631
645
 
632
 
        if file_patch.startswith('\\'):
 
646
        if file_patch.startswith(b'\\'):
633
647
            raise ValueError(
634
648
                'Malformed patch for %s, %r' % (file_id, file_patch))
635
649
        return patched_file(file_patch, patch_original)
636
650
 
637
 
    def get_symlink_target(self, file_id):
638
 
        new_path = self.id2path(file_id)
639
 
        try:
640
 
            return self._targets[new_path]
641
 
        except KeyError:
642
 
            return self.base_tree.get_symlink_target(file_id)
643
 
 
644
 
    def get_kind(self, file_id):
645
 
        if file_id in self._kinds:
646
 
            return self._kinds[file_id]
647
 
        return self.base_tree.inventory[file_id].kind
648
 
 
649
 
    def is_executable(self, file_id):
650
 
        path = self.id2path(file_id)
 
651
    def get_symlink_target(self, path):
 
652
        try:
 
653
            return self._targets[path]
 
654
        except KeyError:
 
655
            old_path = self.old_path(path)
 
656
            return self.base_tree.get_symlink_target(old_path)
 
657
 
 
658
    def kind(self, path):
 
659
        try:
 
660
            return self._kinds[path]
 
661
        except KeyError:
 
662
            old_path = self.old_path(path)
 
663
            return self.base_tree.kind(old_path)
 
664
 
 
665
    def get_file_revision(self, path):
 
666
        if path in self._last_changed:
 
667
            return self._last_changed[path]
 
668
        else:
 
669
            old_path = self.old_path(path)
 
670
            return self.base_tree.get_file_revision(old_path)
 
671
 
 
672
    def is_executable(self, path):
651
673
        if path in self._executable:
652
674
            return self._executable[path]
653
675
        else:
654
 
            return self.base_tree.inventory[file_id].executable
 
676
            old_path = self.old_path(path)
 
677
            return self.base_tree.is_executable(old_path)
655
678
 
656
 
    def get_last_changed(self, file_id):
657
 
        path = self.id2path(file_id)
 
679
    def get_last_changed(self, path):
658
680
        if path in self._last_changed:
659
681
            return self._last_changed[path]
660
 
        return self.base_tree.inventory[file_id].revision
 
682
        old_path = self.old_path(path)
 
683
        return self.base_tree.get_file_revision(old_path)
661
684
 
662
 
    def get_size_and_sha1(self, file_id):
 
685
    def get_size_and_sha1(self, new_path, file_id=None):
663
686
        """Return the size and sha1 hash of the given file id.
664
687
        If the file was not locally modified, this is extracted
665
688
        from the base_tree. Rather than re-reading the file.
666
689
        """
667
 
        new_path = self.id2path(file_id)
668
690
        if new_path is None:
669
691
            return None, None
670
692
        if new_path not in self.patches:
671
693
            # If the entry does not have a patch, then the
672
694
            # contents must be the same as in the base_tree
673
 
            ie = self.base_tree.inventory[file_id]
674
 
            if ie.text_size is None:
675
 
                return ie.text_size, ie.text_sha1
676
 
            return int(ie.text_size), ie.text_sha1
677
 
        fileobj = self.get_file(file_id)
 
695
            base_path = self.old_path(new_path)
 
696
            text_size = self.base_tree.get_file_size(base_path)
 
697
            text_sha1 = self.base_tree.get_file_sha1(base_path)
 
698
            return text_size, text_sha1
 
699
        fileobj = self.get_file(new_path)
678
700
        content = fileobj.read()
679
701
        return len(content), sha_string(content)
680
702
 
684
706
        This need to be called before ever accessing self.inventory
685
707
        """
686
708
        from os.path import dirname, basename
687
 
        base_inv = self.base_tree.inventory
688
709
        inv = Inventory(None, self.revision_id)
689
710
 
690
 
        def add_entry(file_id):
691
 
            path = self.id2path(file_id)
692
 
            if path is None:
693
 
                return
 
711
        def add_entry(path, file_id):
694
712
            if path == '':
695
713
                parent_id = None
696
714
            else:
697
715
                parent_path = dirname(path)
698
716
                parent_id = self.path2id(parent_path)
699
717
 
700
 
            kind = self.get_kind(file_id)
701
 
            revision_id = self.get_last_changed(file_id)
 
718
            kind = self.kind(path)
 
719
            revision_id = self.get_last_changed(path)
702
720
 
703
721
            name = basename(path)
704
722
            if kind == 'directory':
705
723
                ie = InventoryDirectory(file_id, name, parent_id)
706
724
            elif kind == 'file':
707
725
                ie = InventoryFile(file_id, name, parent_id)
708
 
                ie.executable = self.is_executable(file_id)
 
726
                ie.executable = self.is_executable(path)
709
727
            elif kind == 'symlink':
710
728
                ie = InventoryLink(file_id, name, parent_id)
711
 
                ie.symlink_target = self.get_symlink_target(file_id)
 
729
                ie.symlink_target = self.get_symlink_target(path)
712
730
            ie.revision = revision_id
713
731
 
714
 
            if kind in ('directory', 'symlink'):
715
 
                ie.text_size, ie.text_sha1 = None, None
716
 
            else:
717
 
                ie.text_size, ie.text_sha1 = self.get_size_and_sha1(file_id)
718
 
            if (ie.text_size is None) and (kind == 'file'):
719
 
                raise BzrError('Got a text_size of None for file_id %r' % file_id)
 
732
            if kind == 'file':
 
733
                ie.text_size, ie.text_sha1 = self.get_size_and_sha1(path)
 
734
                if ie.text_size is None:
 
735
                    raise BzrError(
 
736
                        'Got a text_size of None for file_id %r' % file_id)
720
737
            inv.add(ie)
721
738
 
722
739
        sorted_entries = self.sorted_path_id()
723
740
        for path, file_id in sorted_entries:
724
 
            add_entry(file_id)
 
741
            add_entry(path, file_id)
725
742
 
726
743
        return inv
727
744
 
732
749
    # at that instant
733
750
    inventory = property(_get_inventory)
734
751
 
735
 
    def __iter__(self):
736
 
        for path, entry in self.inventory.iter_entries():
737
 
            yield entry.file_id
 
752
    root_inventory = property(_get_inventory)
 
753
 
 
754
    def all_file_ids(self):
 
755
        return {entry.file_id for path, entry in self.inventory.iter_entries()}
 
756
 
 
757
    def all_versioned_paths(self):
 
758
        return {path for path, entry in self.inventory.iter_entries()}
 
759
 
 
760
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
761
        # The only files returned by this are those from the version
 
762
        inv = self.inventory
 
763
        if from_dir is None:
 
764
            from_dir_id = None
 
765
        else:
 
766
            from_dir_id = inv.path2id(from_dir)
 
767
            if from_dir_id is None:
 
768
                # Directory not versioned
 
769
                return
 
770
        entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
 
771
        if inv.root is not None and not include_root and from_dir is None:
 
772
            # skip the root for compatability with the current apis.
 
773
            next(entries)
 
774
        for path, entry in entries:
 
775
            yield path, 'V', entry.kind, entry.file_id, entry
738
776
 
739
777
    def sorted_path_id(self):
740
778
        paths = []
741
 
        for result in self._new_id.iteritems():
 
779
        for result in viewitems(self._new_id):
742
780
            paths.append(result)
743
 
        for id in self.base_tree:
 
781
        for id in self.base_tree.all_file_ids():
744
782
            path = self.id2path(id)
745
783
            if path is None:
746
784
                continue
751
789
 
752
790
def patched_file(file_patch, original):
753
791
    """Produce a file-like object with the patched version of a text"""
754
 
    from bzrlib.patches import iter_patched
755
 
    from bzrlib.iterablefile import IterableFile
756
 
    if file_patch == "":
 
792
    from breezy.patches import iter_patched
 
793
    from breezy.iterablefile import IterableFile
 
794
    if file_patch == b"":
757
795
        return IterableFile(())
758
796
    # string.splitlines(True) also splits on '\r', but the iter_patched code
759
797
    # only expects to iterate over '\n' style lines
760
798
    return IterableFile(iter_patched(original,
761
 
                StringIO(file_patch).readlines()))
 
799
                                     BytesIO(file_patch).readlines()))