~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-16 22:00:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1942.
  • Revision ID: john@arbash-meinel.com-20060816220019-541cb90093258ac3
Using real utf8 and cache_utf8 has similar performance, 272ms, and 363ms

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import sys
35
35
import tarfile
36
36
import types
 
37
from warnings import warn
37
38
 
38
39
import bzrlib
39
40
from bzrlib import errors, osutils
81
82
    InventoryDirectory('123', 'src', parent_id='TREE_ROOT', revision=None)
82
83
    >>> i.add(InventoryFile('2323', 'hello.c', parent_id='123'))
83
84
    InventoryFile('2323', 'hello.c', parent_id='123', sha1=None, len=None)
84
 
    >>> shouldbe = {0: 'src', 1: pathjoin('src','hello.c')}
 
85
    >>> shouldbe = {0: '', 1: 'src', 2: pathjoin('src','hello.c')}
85
86
    >>> for ix, j in enumerate(i.iter_entries()):
86
87
    ...   print (j[0] == shouldbe[ix], j[1])
87
88
    ... 
 
89
    (True, InventoryDirectory('TREE_ROOT', '', parent_id=None, revision=None))
88
90
    (True, InventoryDirectory('123', 'src', parent_id='TREE_ROOT', revision=None))
89
91
    (True, InventoryFile('2323', 'hello.c', parent_id='123', sha1=None, len=None))
90
92
    >>> i.add(InventoryFile('2323', 'bye.c', '123'))
107
109
    ...     print path
108
110
    ...     assert i.path2id(path)
109
111
    ... 
 
112
    <BLANKLINE>
110
113
    src
111
114
    src/bye.c
112
115
    src/hello.c
293
296
        """Return a short kind indicator useful for appending to names."""
294
297
        raise BzrError('unknown kind %r' % self.kind)
295
298
 
296
 
    known_kinds = ('file', 'directory', 'symlink', 'root_directory')
 
299
    known_kinds = ('file', 'directory', 'symlink')
297
300
 
298
301
    def _put_in_tar(self, item, tree):
299
302
        """populate item for stashing in a tar, and return the content stream.
310
313
        """
311
314
        fullpath = pathjoin(dest, dp)
312
315
        self._put_on_disk(fullpath, tree)
313
 
        mutter("  export {%s} kind %s to %s", self.file_id,
314
 
                self.kind, fullpath)
 
316
        # mutter("  export {%s} kind %s to %s", self.file_id,
 
317
        #         self.kind, fullpath)
315
318
 
316
319
    def _put_on_disk(self, fullpath, tree):
317
320
        """Put this entry onto disk at fullpath, from tree tree."""
408
411
        This means that all its fields are populated, that it has its
409
412
        text stored in the text store or weave.
410
413
        """
411
 
        mutter('new parents of %s are %r', path, previous_entries)
 
414
        # mutter('new parents of %s are %r', path, previous_entries)
412
415
        self._read_tree_state(path, work_tree)
413
416
        # TODO: Where should we determine whether to reuse a
414
417
        # previous revision id or create a new revision? 20060606
416
419
            # cannot be unchanged unless there is only one parent file rev.
417
420
            parent_ie = previous_entries.values()[0]
418
421
            if self._unchanged(parent_ie):
419
 
                mutter("found unchanged entry")
 
422
                # mutter("found unchanged entry")
420
423
                self.revision = parent_ie.revision
421
424
                return "unchanged"
422
425
        return self._snapshot_into_revision(revision, previous_entries, 
433
436
 
434
437
        :returns: String description of the commit (e.g. "merged", "modified"), etc.
435
438
        """
436
 
        mutter('new revision {%s} for {%s}', revision, self.file_id)
 
439
        # mutter('new revision {%s} for {%s}', revision, self.file_id)
437
440
        self.revision = revision
438
441
        self._snapshot_text(previous_entries, work_tree, commit_builder)
439
442
 
507
510
    def __init__(self, file_id):
508
511
        self.file_id = file_id
509
512
        self.children = {}
510
 
        self.kind = 'root_directory'
 
513
        self.kind = 'directory'
511
514
        self.parent_id = None
512
515
        self.name = u''
513
516
        self.revision = None
 
517
        warn('RootEntry is deprecated as of bzr 0.10.  Please use '
 
518
             'InventoryDirectory instead.',
 
519
            DeprecationWarning, stacklevel=2)
514
520
 
515
521
    def __eq__(self, other):
516
522
        if not isinstance(other, RootEntry):
840
846
    May also look up by name:
841
847
 
842
848
    >>> [x[0] for x in inv.iter_entries()]
843
 
    [u'hello.c']
 
849
    ['', u'hello.c']
844
850
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
845
851
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
846
852
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT-12345678-12345678', sha1=None, len=None)
859
865
        # root id. Rather than generating a random one here.
860
866
        #if root_id is None:
861
867
        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
862
 
        self.root = RootEntry(root_id)
 
868
        if root_id is not None:
 
869
            self._set_root(InventoryDirectory(root_id, '', None))
 
870
        else:
 
871
            self.root = None
 
872
            self._byid = {}
863
873
        # FIXME: this isn't ever used, changing it to self.revision may break
864
874
        # things. TODO make everything use self.revision_id
865
875
        self.revision_id = revision_id
 
876
 
 
877
    def _set_root(self, ie):
 
878
        self.root = ie
866
879
        self._byid = {self.root.file_id: self.root}
867
880
 
868
881
    def copy(self):
869
882
        # TODO: jam 20051218 Should copy also copy the revision_id?
870
 
        other = Inventory(self.root.file_id)
 
883
        entries = self.iter_entries()
 
884
        other = Inventory(entries.next()[1].file_id)
871
885
        # copy recursively so we know directories will be added before
872
886
        # their children.  There are more efficient ways than this...
873
 
        for path, entry in self.iter_entries():
874
 
            if entry == self.root:
875
 
                continue
 
887
        for path, entry in entries():
876
888
            other.add(entry.copy())
877
889
        return other
878
890
 
888
900
        if from_dir is None:
889
901
            assert self.root
890
902
            from_dir = self.root
 
903
            yield '', self.root
891
904
        elif isinstance(from_dir, basestring):
892
905
            from_dir = self._byid[from_dir]
893
906
            
940
953
        if from_dir is None:
941
954
            assert self.root
942
955
            from_dir = self.root
 
956
            yield '', self.root
943
957
        elif isinstance(from_dir, basestring):
944
958
            from_dir = self._byid[from_dir]
945
959
            
1039
1053
        if entry.file_id in self._byid:
1040
1054
            raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
1041
1055
 
1042
 
        if entry.parent_id == ROOT_ID or entry.parent_id is None:
 
1056
        if entry.parent_id is None:
 
1057
            assert self.root is None and len(self._byid) == 0
 
1058
            self._set_root(entry)
 
1059
            return entry
 
1060
        if entry.parent_id == ROOT_ID:
 
1061
            assert self.root is not None, self
1043
1062
            entry.parent_id = self.root.file_id
1044
1063
 
1045
1064
        try:
1067
1086
        if len(parts) == 0:
1068
1087
            if file_id is None:
1069
1088
                file_id = bzrlib.workingtree.gen_root_id()
1070
 
            self.root = RootEntry(file_id)
 
1089
            self.root = InventoryDirectory(file_id, '', None)
1071
1090
            self._byid = {self.root.file_id: self.root}
1072
1091
            return
1073
1092
        else: