~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Vincent Ladeuil
  • Date: 2012-03-13 16:42:20 UTC
  • mto: This revision was merged to the branch mainline in revision 6512.
  • Revision ID: v.ladeuil+lp@free.fr-20120313164220-atkou2zprhlspmwg
Mention that a given config option cannot be safely handled via both APIs at the same time.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
23
23
# But those depend on its position within a particular inventory, and
24
24
# it would be nice not to need to hold the backpointer here.
25
25
 
 
26
from __future__ import absolute_import
 
27
 
26
28
# This should really be an id randomly assigned when the tree is
27
29
# created, but it's not for now.
28
30
ROOT_ID = "TREE_ROOT"
31
33
lazy_import(globals(), """
32
34
import collections
33
35
import copy
34
 
import os
35
36
import re
36
37
import tarfile
37
38
 
38
 
import bzrlib
39
39
from bzrlib import (
40
40
    chk_map,
41
41
    errors,
42
42
    generate_ids,
43
43
    osutils,
44
 
    symbol_versioning,
45
44
    )
46
45
""")
47
46
 
48
 
from bzrlib.errors import (
49
 
    BzrCheckError,
50
 
    BzrError,
 
47
from bzrlib import (
 
48
    lazy_regex,
 
49
    trace,
51
50
    )
52
 
from bzrlib.symbol_versioning import deprecated_in, deprecated_method
53
 
from bzrlib.trace import mutter
 
51
 
54
52
from bzrlib.static_tuple import StaticTuple
 
53
from bzrlib.symbol_versioning import (
 
54
    deprecated_in,
 
55
    deprecated_method,
 
56
    )
55
57
 
56
58
 
57
59
class InventoryEntry(object):
104
106
    InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
105
107
    >>> i.path2id('src/wibble')
106
108
    '2325'
107
 
    >>> '2325' in i
108
 
    True
109
109
    >>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
110
110
    InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
111
111
    >>> i['2326']
131
131
    RENAMED = 'renamed'
132
132
    MODIFIED_AND_RENAMED = 'modified and renamed'
133
133
 
134
 
    __slots__ = []
 
134
    __slots__ = ['file_id', 'revision', 'parent_id', 'name']
 
135
 
 
136
    # Attributes that all InventoryEntry instances are expected to have, but
 
137
    # that don't vary for all kinds of entry.  (e.g. symlink_target is only
 
138
    # relevant to InventoryLink, so there's no reason to make every
 
139
    # InventoryFile instance allocate space to hold a value for it.)
 
140
    # Attributes that only vary for files: executable, text_sha1, text_size,
 
141
    # text_id
 
142
    executable = False
 
143
    text_sha1 = None
 
144
    text_size = None
 
145
    text_id = None
 
146
    # Attributes that only vary for symlinks: symlink_target
 
147
    symlink_target = None
 
148
    # Attributes that only vary for tree-references: reference_revision
 
149
    reference_revision = None
 
150
 
135
151
 
136
152
    def detect_changes(self, old_entry):
137
153
        """Return a (text_modified, meta_modified) from this to old_entry.
158
174
        candidates = {}
159
175
        # identify candidate head revision ids.
160
176
        for inv in previous_inventories:
161
 
            if self.file_id in inv:
 
177
            if inv.has_id(self.file_id):
162
178
                ie = inv[self.file_id]
163
179
                if ie.revision in candidates:
164
180
                    # same revision value in two different inventories:
176
192
                    candidates[ie.revision] = ie
177
193
        return candidates
178
194
 
179
 
    @deprecated_method(deprecated_in((1, 6, 0)))
180
 
    def get_tar_item(self, root, dp, now, tree):
181
 
        """Get a tarfile item and a file stream for its content."""
182
 
        item = tarfile.TarInfo(osutils.pathjoin(root, dp).encode('utf8'))
183
 
        # TODO: would be cool to actually set it to the timestamp of the
184
 
        # revision it was last changed
185
 
        item.mtime = now
186
 
        fileobj = self._put_in_tar(item, tree)
187
 
        return item, fileobj
188
 
 
189
195
    def has_text(self):
190
196
        """Return true if the object this entry represents has textual data.
191
197
 
197
203
        """
198
204
        return False
199
205
 
200
 
    def __init__(self, file_id, name, parent_id, text_id=None):
 
206
    def __init__(self, file_id, name, parent_id):
201
207
        """Create an InventoryEntry
202
208
 
203
209
        The filename must be a single component, relative to the
214
220
        """
215
221
        if '/' in name or '\\' in name:
216
222
            raise errors.InvalidEntryName(name=name)
217
 
        self.executable = False
 
223
        self.file_id = file_id
218
224
        self.revision = None
219
 
        self.text_sha1 = None
220
 
        self.text_size = None
221
 
        self.file_id = file_id
222
225
        self.name = name
223
 
        self.text_id = text_id
224
226
        self.parent_id = parent_id
225
 
        self.symlink_target = None
226
 
        self.reference_revision = None
227
227
 
228
228
    def kind_character(self):
229
229
        """Return a short kind indicator useful for appending to names."""
230
 
        raise BzrError('unknown kind %r' % self.kind)
 
230
        raise errors.BzrError('unknown kind %r' % self.kind)
231
231
 
232
232
    known_kinds = ('file', 'directory', 'symlink')
233
233
 
234
 
    def _put_in_tar(self, item, tree):
235
 
        """populate item for stashing in a tar, and return the content stream.
236
 
 
237
 
        If no content is available, return None.
238
 
        """
239
 
        raise BzrError("don't know how to export {%s} of kind %r" %
240
 
                       (self.file_id, self.kind))
241
 
 
242
 
    @deprecated_method(deprecated_in((1, 6, 0)))
243
 
    def put_on_disk(self, dest, dp, tree):
244
 
        """Create a representation of self on disk in the prefix dest.
245
 
 
246
 
        This is a template method - implement _put_on_disk in subclasses.
247
 
        """
248
 
        fullpath = osutils.pathjoin(dest, dp)
249
 
        self._put_on_disk(fullpath, tree)
250
 
        # mutter("  export {%s} kind %s to %s", self.file_id,
251
 
        #         self.kind, fullpath)
252
 
 
253
 
    def _put_on_disk(self, fullpath, tree):
254
 
        """Put this entry onto disk at fullpath, from tree tree."""
255
 
        raise BzrError("don't know how to export {%s} of kind %r" % (self.file_id, self.kind))
256
 
 
257
234
    def sorted_children(self):
258
235
        return sorted(self.children.items())
259
236
 
276
253
        """
277
254
        if self.parent_id is not None:
278
255
            if not inv.has_id(self.parent_id):
279
 
                raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
280
 
                        % (self.parent_id, rev_id))
 
256
                raise errors.BzrCheckError(
 
257
                    'missing parent {%s} in inventory for revision {%s}' % (
 
258
                        self.parent_id, rev_id))
281
259
        checker._add_entry_to_text_key_references(inv, self)
282
260
        self._check(checker, rev_id)
283
261
 
397
375
        pass
398
376
 
399
377
 
400
 
class RootEntry(InventoryEntry):
401
 
 
402
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
403
 
                 'text_id', 'parent_id', 'children', 'executable',
404
 
                 'revision', 'symlink_target', 'reference_revision']
405
 
 
406
 
    def _check(self, checker, rev_id):
407
 
        """See InventoryEntry._check"""
408
 
 
409
 
    def __init__(self, file_id):
410
 
        self.file_id = file_id
411
 
        self.children = {}
412
 
        self.kind = 'directory'
413
 
        self.parent_id = None
414
 
        self.name = u''
415
 
        self.revision = None
416
 
        symbol_versioning.warn('RootEntry is deprecated as of bzr 0.10.'
417
 
                               '  Please use InventoryDirectory instead.',
418
 
                               DeprecationWarning, stacklevel=2)
419
 
 
420
 
    def __eq__(self, other):
421
 
        if not isinstance(other, RootEntry):
422
 
            return NotImplemented
423
 
 
424
 
        return (self.file_id == other.file_id) \
425
 
               and (self.children == other.children)
426
 
 
427
 
 
428
378
class InventoryDirectory(InventoryEntry):
429
379
    """A directory in an inventory."""
430
380
 
431
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
432
 
                 'text_id', 'parent_id', 'children', 'executable',
433
 
                 'revision', 'symlink_target', 'reference_revision']
 
381
    __slots__ = ['children']
 
382
 
 
383
    kind = 'directory'
434
384
 
435
385
    def _check(self, checker, rev_id):
436
386
        """See InventoryEntry._check"""
437
 
        if (self.text_sha1 is not None or self.text_size is not None or
438
 
            self.text_id is not None):
439
 
            checker._report_items.append('directory {%s} has text in revision {%s}'
440
 
                                % (self.file_id, rev_id))
441
387
        # In non rich root repositories we do not expect a file graph for the
442
388
        # root.
443
389
        if self.name == '' and not checker.rich_roots:
459
405
    def __init__(self, file_id, name, parent_id):
460
406
        super(InventoryDirectory, self).__init__(file_id, name, parent_id)
461
407
        self.children = {}
462
 
        self.kind = 'directory'
463
408
 
464
409
    def kind_character(self):
465
410
        """See InventoryEntry.kind_character."""
466
411
        return '/'
467
412
 
468
 
    def _put_in_tar(self, item, tree):
469
 
        """See InventoryEntry._put_in_tar."""
470
 
        item.type = tarfile.DIRTYPE
471
 
        fileobj = None
472
 
        item.name += '/'
473
 
        item.size = 0
474
 
        item.mode = 0755
475
 
        return fileobj
476
 
 
477
 
    def _put_on_disk(self, fullpath, tree):
478
 
        """See InventoryEntry._put_on_disk."""
479
 
        os.mkdir(fullpath)
480
 
 
481
413
 
482
414
class InventoryFile(InventoryEntry):
483
415
    """A file in an inventory."""
484
416
 
485
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
486
 
                 'text_id', 'parent_id', 'children', 'executable',
487
 
                 'revision', 'symlink_target', 'reference_revision']
 
417
    __slots__ = ['text_sha1', 'text_size', 'text_id', 'executable']
 
418
 
 
419
    kind = 'file'
 
420
 
 
421
    def __init__(self, file_id, name, parent_id):
 
422
        super(InventoryFile, self).__init__(file_id, name, parent_id)
 
423
        self.text_sha1 = None
 
424
        self.text_size = None
 
425
        self.text_id = None
 
426
        self.executable = False
488
427
 
489
428
    def _check(self, checker, tree_revision_id):
490
429
        """See InventoryEntry._check"""
533
472
        """See InventoryEntry.has_text."""
534
473
        return True
535
474
 
536
 
    def __init__(self, file_id, name, parent_id):
537
 
        super(InventoryFile, self).__init__(file_id, name, parent_id)
538
 
        self.kind = 'file'
539
 
 
540
475
    def kind_character(self):
541
476
        """See InventoryEntry.kind_character."""
542
477
        return ''
543
478
 
544
 
    def _put_in_tar(self, item, tree):
545
 
        """See InventoryEntry._put_in_tar."""
546
 
        item.type = tarfile.REGTYPE
547
 
        fileobj = tree.get_file(self.file_id)
548
 
        item.size = self.text_size
549
 
        if tree.is_executable(self.file_id):
550
 
            item.mode = 0755
551
 
        else:
552
 
            item.mode = 0644
553
 
        return fileobj
554
 
 
555
 
    def _put_on_disk(self, fullpath, tree):
556
 
        """See InventoryEntry._put_on_disk."""
557
 
        osutils.pumpfile(tree.get_file(self.file_id), file(fullpath, 'wb'))
558
 
        if tree.is_executable(self.file_id):
559
 
            os.chmod(fullpath, 0755)
560
 
 
561
479
    def _read_tree_state(self, path, work_tree):
562
480
        """See InventoryEntry._read_tree_state."""
563
481
        self.text_sha1 = work_tree.get_file_sha1(self.file_id, path=path)
595
513
class InventoryLink(InventoryEntry):
596
514
    """A file in an inventory."""
597
515
 
598
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
599
 
                 'text_id', 'parent_id', 'children', 'executable',
600
 
                 'revision', 'symlink_target', 'reference_revision']
 
516
    __slots__ = ['symlink_target']
 
517
 
 
518
    kind = 'symlink'
 
519
 
 
520
    def __init__(self, file_id, name, parent_id):
 
521
        super(InventoryLink, self).__init__(file_id, name, parent_id)
 
522
        self.symlink_target = None
601
523
 
602
524
    def _check(self, checker, tree_revision_id):
603
525
        """See InventoryEntry._check"""
604
 
        if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
605
 
            checker._report_items.append(
606
 
               'symlink {%s} has text in revision {%s}'
607
 
                    % (self.file_id, tree_revision_id))
608
526
        if self.symlink_target is None:
609
527
            checker._report_items.append(
610
528
                'symlink {%s} has no target in revision {%s}'
625
543
        # FIXME: which _modified field should we use ? RBC 20051003
626
544
        text_modified = (self.symlink_target != old_entry.symlink_target)
627
545
        if text_modified:
628
 
            mutter("    symlink target changed")
 
546
            trace.mutter("    symlink target changed")
629
547
        meta_modified = False
630
548
        return text_modified, meta_modified
631
549
 
648
566
        differ = DiffSymlink(old_tree, new_tree, output_to)
649
567
        return differ.diff_symlink(old_target, new_target)
650
568
 
651
 
    def __init__(self, file_id, name, parent_id):
652
 
        super(InventoryLink, self).__init__(file_id, name, parent_id)
653
 
        self.kind = 'symlink'
654
 
 
655
569
    def kind_character(self):
656
570
        """See InventoryEntry.kind_character."""
657
571
        return ''
658
572
 
659
 
    def _put_in_tar(self, item, tree):
660
 
        """See InventoryEntry._put_in_tar."""
661
 
        item.type = tarfile.SYMTYPE
662
 
        fileobj = None
663
 
        item.size = 0
664
 
        item.mode = 0755
665
 
        item.linkname = self.symlink_target
666
 
        return fileobj
667
 
 
668
 
    def _put_on_disk(self, fullpath, tree):
669
 
        """See InventoryEntry._put_on_disk."""
670
 
        try:
671
 
            os.symlink(self.symlink_target, fullpath)
672
 
        except OSError,e:
673
 
            raise BzrError("Failed to create symlink %r -> %r, error: %s" % (fullpath, self.symlink_target, e))
674
 
 
675
573
    def _read_tree_state(self, path, work_tree):
676
574
        """See InventoryEntry._read_tree_state."""
677
575
        self.symlink_target = work_tree.get_symlink_target(self.file_id)
689
587
 
690
588
class TreeReference(InventoryEntry):
691
589
 
 
590
    __slots__ = ['reference_revision']
 
591
 
692
592
    kind = 'tree-reference'
693
593
 
694
594
    def __init__(self, file_id, name, parent_id, revision=None,
733
633
    inserted, other than through the Inventory API.
734
634
    """
735
635
 
 
636
    @deprecated_method(deprecated_in((2, 4, 0)))
736
637
    def __contains__(self, file_id):
737
638
        """True if this entry contains a file with given id.
738
639
 
739
640
        >>> inv = Inventory()
740
641
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
741
642
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
742
 
        >>> '123' in inv
 
643
        >>> inv.has_id('123')
743
644
        True
744
 
        >>> '456' in inv
 
645
        >>> inv.has_id('456')
745
646
        False
746
647
 
747
648
        Note that this method along with __iter__ are not encouraged for use as
822
723
                # if we finished all children, pop it off the stack
823
724
                stack.pop()
824
725
 
 
726
    def _preload_cache(self):
 
727
        """Populate any caches, we are about to access all items.
 
728
        
 
729
        The default implementation does nothing, because CommonInventory doesn't
 
730
        have a cache.
 
731
        """
 
732
        pass
 
733
    
825
734
    def iter_entries_by_dir(self, from_dir=None, specific_file_ids=None,
826
735
        yield_parents=False):
827
736
        """Iterate over the entries in a directory first order.
840
749
            specific_file_ids = set(specific_file_ids)
841
750
        # TODO? Perhaps this should return the from_dir so that the root is
842
751
        # yielded? or maybe an option?
 
752
        if from_dir is None and specific_file_ids is None:
 
753
            # They are iterating from the root, and have not specified any
 
754
            # specific entries to look at. All current callers fully consume the
 
755
            # iterator, so we can safely assume we are accessing all entries
 
756
            self._preload_cache()
843
757
        if from_dir is None:
844
758
            if self.root is None:
845
759
                return
847
761
            if (not yield_parents and specific_file_ids is not None and
848
762
                len(specific_file_ids) == 1):
849
763
                file_id = list(specific_file_ids)[0]
850
 
                if file_id in self:
 
764
                if self.has_id(file_id):
851
765
                    yield self.id2path(file_id), self[file_id]
852
766
                return
853
767
            from_dir = self.root
863
777
            parents = set()
864
778
            byid = self
865
779
            def add_ancestors(file_id):
866
 
                if file_id not in byid:
 
780
                if not byid.has_id(file_id):
867
781
                    return
868
782
                parent_id = byid[file_id].parent_id
869
783
                if parent_id is None:
913
827
                    file_id, self[file_id]))
914
828
        return delta
915
829
 
916
 
    def _get_mutable_inventory(self):
917
 
        """Returns a mutable copy of the object.
918
 
 
919
 
        Some inventories are immutable, yet working trees, for example, needs
920
 
        to mutate exisiting inventories instead of creating a new one.
921
 
        """
922
 
        raise NotImplementedError(self._get_mutable_inventory)
923
 
 
924
830
    def make_entry(self, kind, name, parent_id, file_id=None):
925
831
        """Simple thunk to bzrlib.inventory.make_entry."""
926
832
        return make_entry(kind, name, parent_id, file_id)
940
846
                if ie.kind == 'directory':
941
847
                    descend(ie, child_path)
942
848
 
943
 
        descend(self.root, u'')
944
 
        return accum
945
 
 
946
 
    def directories(self):
947
 
        """Return (path, entry) pairs for all directories, including the root.
948
 
        """
949
 
        accum = []
950
 
        def descend(parent_ie, parent_path):
951
 
            accum.append((parent_path, parent_ie))
952
 
 
953
 
            kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
954
 
            kids.sort()
955
 
 
956
 
            for name, child_ie in kids:
957
 
                child_path = osutils.pathjoin(parent_path, name)
958
 
                descend(child_ie, child_path)
959
 
        descend(self.root, u'')
 
849
        if self.root is not None:
 
850
            descend(self.root, u'')
960
851
        return accum
961
852
 
962
853
    def path2id(self, relpath):
1060
951
 
1061
952
    >>> inv.path2id('hello.c')
1062
953
    '123-123'
1063
 
    >>> '123-123' in inv
 
954
    >>> inv.has_id('123-123')
1064
955
    True
1065
956
 
1066
957
    There are iterators over the contents:
1223
1114
            other.add(entry.copy())
1224
1115
        return other
1225
1116
 
1226
 
    def _get_mutable_inventory(self):
1227
 
        """See CommonInventory._get_mutable_inventory."""
1228
 
        return copy.deepcopy(self)
1229
 
 
1230
1117
    def __iter__(self):
1231
1118
        """Iterate over all file-ids."""
1232
1119
        return iter(self._byid)
1272
1159
    def _add_child(self, entry):
1273
1160
        """Add an entry to the inventory, without adding it to its parent"""
1274
1161
        if entry.file_id in self._byid:
1275
 
            raise BzrError("inventory already contains entry with id {%s}" %
1276
 
                           entry.file_id)
 
1162
            raise errors.BzrError(
 
1163
                "inventory already contains entry with id {%s}" %
 
1164
                entry.file_id)
1277
1165
        self._byid[entry.file_id] = entry
1278
1166
        for child in getattr(entry, 'children', {}).itervalues():
1279
1167
            self._add_child(child)
1282
1170
    def add(self, entry):
1283
1171
        """Add entry to inventory.
1284
1172
 
1285
 
        To add  a file to a branch ready to be committed, use Branch.add,
1286
 
        which calls this.
1287
 
 
1288
1173
        :return: entry
1289
1174
        """
1290
1175
        if entry.file_id in self._byid:
1335
1220
        >>> inv = Inventory()
1336
1221
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
1337
1222
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
1338
 
        >>> '123' in inv
 
1223
        >>> inv.has_id('123')
1339
1224
        True
1340
1225
        >>> del inv['123']
1341
 
        >>> '123' in inv
 
1226
        >>> inv.has_id('123')
1342
1227
        False
1343
1228
        """
1344
1229
        ie = self[file_id]
1446
1331
        """
1447
1332
        new_name = ensure_normalized_name(new_name)
1448
1333
        if not is_valid_name(new_name):
1449
 
            raise BzrError("not an acceptable filename: %r" % new_name)
 
1334
            raise errors.BzrError("not an acceptable filename: %r" % new_name)
1450
1335
 
1451
1336
        new_parent = self._byid[new_parent_id]
1452
1337
        if new_name in new_parent.children:
1453
 
            raise BzrError("%r already exists in %r" % (new_name, self.id2path(new_parent_id)))
 
1338
            raise errors.BzrError("%r already exists in %r" %
 
1339
                (new_name, self.id2path(new_parent_id)))
1454
1340
 
1455
1341
        new_parent_idpath = self.get_idpath(new_parent_id)
1456
1342
        if file_id in new_parent_idpath:
1457
 
            raise BzrError("cannot move directory %r into a subdirectory of itself, %r"
 
1343
            raise errors.BzrError(
 
1344
                "cannot move directory %r into a subdirectory of itself, %r"
1458
1345
                    % (self.id2path(file_id), self.id2path(new_parent_id)))
1459
1346
 
1460
1347
        file_ie = self._byid[file_id]
1496
1383
    def __init__(self, search_key_name):
1497
1384
        CommonInventory.__init__(self)
1498
1385
        self._fileid_to_entry_cache = {}
 
1386
        self._fully_cached = False
1499
1387
        self._path_to_fileid_cache = {}
1500
1388
        self._search_key_name = search_key_name
1501
1389
        self.root_id = None
1588
1476
            if entry.kind == 'directory':
1589
1477
                directories_to_expand.add(entry.file_id)
1590
1478
            interesting.add(entry.parent_id)
1591
 
            children_of_parent_id.setdefault(entry.parent_id, []
1592
 
                                             ).append(entry.file_id)
 
1479
            children_of_parent_id.setdefault(entry.parent_id, set()
 
1480
                                             ).add(entry.file_id)
1593
1481
 
1594
1482
        # Now, interesting has all of the direct parents, but not the
1595
1483
        # parents of those parents. It also may have some duplicates with
1603
1491
            next_parents = set()
1604
1492
            for entry in self._getitems(remaining_parents):
1605
1493
                next_parents.add(entry.parent_id)
1606
 
                children_of_parent_id.setdefault(entry.parent_id, []
1607
 
                                                 ).append(entry.file_id)
 
1494
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1495
                                                 ).add(entry.file_id)
1608
1496
            # Remove any search tips we've already processed
1609
1497
            remaining_parents = next_parents.difference(interesting)
1610
1498
            interesting.update(remaining_parents)
1623
1511
            for entry in self._getitems(next_file_ids):
1624
1512
                if entry.kind == 'directory':
1625
1513
                    directories_to_expand.add(entry.file_id)
1626
 
                children_of_parent_id.setdefault(entry.parent_id, []
1627
 
                                                 ).append(entry.file_id)
 
1514
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1515
                                                 ).add(entry.file_id)
1628
1516
        return interesting, children_of_parent_id
1629
1517
 
1630
1518
    def filter(self, specific_fileids):
1652
1540
            # parent_to_children with at least the tree root.)
1653
1541
            return other
1654
1542
        cache = self._fileid_to_entry_cache
1655
 
        try:
1656
 
            remaining_children = collections.deque(parent_to_children[self.root_id])
1657
 
        except:
1658
 
            import pdb; pdb.set_trace()
1659
 
            raise
 
1543
        remaining_children = collections.deque(parent_to_children[self.root_id])
1660
1544
        while remaining_children:
1661
1545
            file_id = remaining_children.popleft()
1662
1546
            ie = cache[file_id]
1712
1596
        self._fileid_to_entry_cache[result.file_id] = result
1713
1597
        return result
1714
1598
 
1715
 
    def _get_mutable_inventory(self):
1716
 
        """See CommonInventory._get_mutable_inventory."""
1717
 
        entries = self.iter_entries()
1718
 
        inv = Inventory(None, self.revision_id)
1719
 
        for path, inv_entry in entries:
1720
 
            inv.add(inv_entry.copy())
1721
 
        return inv
1722
 
 
1723
1599
    def create_by_apply_delta(self, inventory_delta, new_revision_id,
1724
1600
        propagate_caches=False):
1725
1601
        """Create a new CHKInventory by applying inventory_delta to this one.
2066
1942
 
2067
1943
    def iter_just_entries(self):
2068
1944
        """Iterate over all entries.
2069
 
        
 
1945
 
2070
1946
        Unlike iter_entries(), just the entries are returned (not (path, ie))
2071
1947
        and the order of entries is undefined.
2072
1948
 
2080
1956
                self._fileid_to_entry_cache[file_id] = ie
2081
1957
            yield ie
2082
1958
 
 
1959
    def _preload_cache(self):
 
1960
        """Make sure all file-ids are in _fileid_to_entry_cache"""
 
1961
        if self._fully_cached:
 
1962
            return # No need to do it again
 
1963
        # The optimal sort order is to use iteritems() directly
 
1964
        cache = self._fileid_to_entry_cache
 
1965
        for key, entry in self.id_to_entry.iteritems():
 
1966
            file_id = key[0]
 
1967
            if file_id not in cache:
 
1968
                ie = self._bytes_to_entry(entry)
 
1969
                cache[file_id] = ie
 
1970
            else:
 
1971
                ie = cache[file_id]
 
1972
        last_parent_id = last_parent_ie = None
 
1973
        pid_items = self.parent_id_basename_to_file_id.iteritems()
 
1974
        for key, child_file_id in pid_items:
 
1975
            if key == ('', ''): # This is the root
 
1976
                if child_file_id != self.root_id:
 
1977
                    raise ValueError('Data inconsistency detected.'
 
1978
                        ' We expected data with key ("","") to match'
 
1979
                        ' the root id, but %s != %s'
 
1980
                        % (child_file_id, self.root_id))
 
1981
                continue
 
1982
            parent_id, basename = key
 
1983
            ie = cache[child_file_id]
 
1984
            if parent_id == last_parent_id:
 
1985
                parent_ie = last_parent_ie
 
1986
            else:
 
1987
                parent_ie = cache[parent_id]
 
1988
            if parent_ie.kind != 'directory':
 
1989
                raise ValueError('Data inconsistency detected.'
 
1990
                    ' An entry in the parent_id_basename_to_file_id map'
 
1991
                    ' has parent_id {%s} but the kind of that object'
 
1992
                    ' is %r not "directory"' % (parent_id, parent_ie.kind))
 
1993
            if parent_ie._children is None:
 
1994
                parent_ie._children = {}
 
1995
            basename = basename.decode('utf-8')
 
1996
            if basename in parent_ie._children:
 
1997
                existing_ie = parent_ie._children[basename]
 
1998
                if existing_ie != ie:
 
1999
                    raise ValueError('Data inconsistency detected.'
 
2000
                        ' Two entries with basename %r were found'
 
2001
                        ' in the parent entry {%s}'
 
2002
                        % (basename, parent_id))
 
2003
            if basename != ie.name:
 
2004
                raise ValueError('Data inconsistency detected.'
 
2005
                    ' In the parent_id_basename_to_file_id map, file_id'
 
2006
                    ' {%s} is listed as having basename %r, but in the'
 
2007
                    ' id_to_entry map it is %r'
 
2008
                    % (child_file_id, basename, ie.name))
 
2009
            parent_ie._children[basename] = ie
 
2010
        self._fully_cached = True
 
2011
 
2083
2012
    def iter_changes(self, basis):
2084
2013
        """Generate a Tree.iter_changes change list between this and basis.
2085
2014
 
2182
2111
    def path2id(self, relpath):
2183
2112
        """See CommonInventory.path2id()."""
2184
2113
        # TODO: perhaps support negative hits?
 
2114
        if isinstance(relpath, basestring):
 
2115
            names = osutils.splitpath(relpath)
 
2116
        else:
 
2117
            names = relpath
 
2118
            if relpath == []:
 
2119
                relpath = [""]
 
2120
            relpath = osutils.pathjoin(*relpath)
2185
2121
        result = self._path_to_fileid_cache.get(relpath, None)
2186
2122
        if result is not None:
2187
2123
            return result
2188
 
        if isinstance(relpath, basestring):
2189
 
            names = osutils.splitpath(relpath)
2190
 
        else:
2191
 
            names = relpath
2192
2124
        current_id = self.root_id
2193
2125
        if current_id is None:
2194
2126
            return None
2245
2177
class CHKInventoryDirectory(InventoryDirectory):
2246
2178
    """A directory in an inventory."""
2247
2179
 
2248
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
2249
 
                 'text_id', 'parent_id', '_children', 'executable',
2250
 
                 'revision', 'symlink_target', 'reference_revision',
2251
 
                 '_chk_inventory']
 
2180
    __slots__ = ['_children', '_chk_inventory']
2252
2181
 
2253
2182
    def __init__(self, file_id, name, parent_id, chk_inventory):
2254
2183
        # Don't call InventoryDirectory.__init__ - it isn't right for this
2255
2184
        # class.
2256
2185
        InventoryEntry.__init__(self, file_id, name, parent_id)
2257
2186
        self._children = None
2258
 
        self.kind = 'directory'
2259
2187
        self._chk_inventory = chk_inventory
2260
2188
 
2261
2189
    @property
2346
2274
    return name
2347
2275
 
2348
2276
 
2349
 
_NAME_RE = None
 
2277
_NAME_RE = lazy_regex.lazy_compile(r'^[^/\\]+$')
2350
2278
 
2351
2279
def is_valid_name(name):
2352
 
    global _NAME_RE
2353
 
    if _NAME_RE is None:
2354
 
        _NAME_RE = re.compile(r'^[^/\\]+$')
2355
 
 
2356
2280
    return bool(_NAME_RE.match(name))
2357
2281
 
2358
2282
 
2448
2372
            raise errors.InconsistentDelta(new_path, item[1],
2449
2373
                "new_path with no entry")
2450
2374
        yield item
 
2375
 
 
2376
 
 
2377
def mutable_inventory_from_tree(tree):
 
2378
    """Create a new inventory that has the same contents as a specified tree.
 
2379
 
 
2380
    :param tree: Revision tree to create inventory from
 
2381
    """
 
2382
    entries = tree.iter_entries_by_dir()
 
2383
    inv = Inventory(None, tree.get_revision_id())
 
2384
    for path, inv_entry in entries:
 
2385
        inv.add(inv_entry.copy())
 
2386
    return inv