/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 bzrlib/inventory.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-18 18:56:23 UTC
  • mfrom: (1790 +trunk)
  • mto: (1711.7.2 win32)
  • mto: This revision was merged to the branch mainline in revision 1796.
  • Revision ID: john@arbash-meinel.com-20060618185623-854bdd0fbc4f230f
[merge] bzr.dev 1790

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
    >>> i.path2id('')
78
78
    'TREE_ROOT'
79
79
    >>> i.add(InventoryDirectory('123', 'src', ROOT_ID))
80
 
    InventoryDirectory('123', 'src', parent_id='TREE_ROOT')
 
80
    InventoryDirectory('123', 'src', parent_id='TREE_ROOT', revision=None)
81
81
    >>> i.add(InventoryFile('2323', 'hello.c', parent_id='123'))
82
 
    InventoryFile('2323', 'hello.c', parent_id='123')
 
82
    InventoryFile('2323', 'hello.c', parent_id='123', sha1=None, len=None)
83
83
    >>> shouldbe = {0: 'src', 1: pathjoin('src','hello.c')}
84
84
    >>> for ix, j in enumerate(i.iter_entries()):
85
85
    ...   print (j[0] == shouldbe[ix], j[1])
86
86
    ... 
87
 
    (True, InventoryDirectory('123', 'src', parent_id='TREE_ROOT'))
88
 
    (True, InventoryFile('2323', 'hello.c', parent_id='123'))
 
87
    (True, InventoryDirectory('123', 'src', parent_id='TREE_ROOT', revision=None))
 
88
    (True, InventoryFile('2323', 'hello.c', parent_id='123', sha1=None, len=None))
89
89
    >>> i.add(InventoryFile('2323', 'bye.c', '123'))
90
90
    Traceback (most recent call last):
91
91
    ...
92
92
    BzrError: inventory already contains entry with id {2323}
93
93
    >>> i.add(InventoryFile('2324', 'bye.c', '123'))
94
 
    InventoryFile('2324', 'bye.c', parent_id='123')
 
94
    InventoryFile('2324', 'bye.c', parent_id='123', sha1=None, len=None)
95
95
    >>> i.add(InventoryDirectory('2325', 'wibble', '123'))
96
 
    InventoryDirectory('2325', 'wibble', parent_id='123')
 
96
    InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
97
97
    >>> i.path2id('src/wibble')
98
98
    '2325'
99
99
    >>> '2325' in i
100
100
    True
101
101
    >>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
102
 
    InventoryFile('2326', 'wibble.c', parent_id='2325')
 
102
    InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None)
103
103
    >>> i['2326']
104
 
    InventoryFile('2326', 'wibble.c', parent_id='2325')
 
104
    InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None)
105
105
    >>> for path, entry in i.iter_entries():
106
106
    ...     print path
107
107
    ...     assert i.path2id(path)
123
123
    RENAMED = 'renamed'
124
124
    MODIFIED_AND_RENAMED = 'modified and renamed'
125
125
    
126
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
127
 
                 'text_id', 'parent_id', 'children', 'executable', 
128
 
                 'revision']
 
126
    __slots__ = []
129
127
 
130
128
    def detect_changes(self, old_entry):
131
129
        """Return a (text_modified, meta_modified) from this to old_entry.
160
158
                            versioned_file_store,
161
159
                            transaction,
162
160
                            entry_vf=None):
163
 
        """Return the revisions and entries that directly preceed this.
 
161
        """Return the revisions and entries that directly precede this.
164
162
 
165
163
        Returned as a map from revision to inventory entry.
166
164
 
319
317
        raise BzrError("don't know how to export {%s} of kind %r" % (self.file_id, self.kind))
320
318
 
321
319
    def sorted_children(self):
322
 
        l = self.children.items()
323
 
        l.sort()
324
 
        return l
 
320
        return sorted(self.children.items())
325
321
 
326
322
    @staticmethod
327
323
    def versionable_kind(kind):
341
337
        :param inv: Inventory from which the entry was loaded.
342
338
        :param tree: RevisionTree for this entry.
343
339
        """
344
 
        if self.parent_id != None:
 
340
        if self.parent_id is not None:
345
341
            if not inv.has_id(self.parent_id):
346
342
                raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
347
343
                        % (self.parent_id, rev_id))
397
393
        return 'unchanged'
398
394
 
399
395
    def __repr__(self):
400
 
        return ("%s(%r, %r, parent_id=%r)"
 
396
        return ("%s(%r, %r, parent_id=%r, revision=%r)"
401
397
                % (self.__class__.__name__,
402
398
                   self.file_id,
403
399
                   self.name,
404
 
                   self.parent_id))
 
400
                   self.parent_id,
 
401
                   self.revision))
405
402
 
406
403
    def snapshot(self, revision, path, previous_entries,
407
404
                 work_tree, commit_builder):
471
468
    def _unchanged(self, previous_ie):
472
469
        """Has this entry changed relative to previous_ie.
473
470
 
474
 
        This method should be overriden in child classes.
 
471
        This method should be overridden in child classes.
475
472
        """
476
473
        compatible = True
477
474
        # different inv parent
499
496
 
500
497
class RootEntry(InventoryEntry):
501
498
 
 
499
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
500
                 'text_id', 'parent_id', 'children', 'executable', 
 
501
                 'revision', 'symlink_target']
 
502
 
502
503
    def _check(self, checker, rev_id, tree):
503
504
        """See InventoryEntry._check"""
504
505
 
508
509
        self.kind = 'root_directory'
509
510
        self.parent_id = None
510
511
        self.name = u''
 
512
        self.revision = None
511
513
 
512
514
    def __eq__(self, other):
513
515
        if not isinstance(other, RootEntry):
520
522
class InventoryDirectory(InventoryEntry):
521
523
    """A directory in an inventory."""
522
524
 
 
525
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
526
                 'text_id', 'parent_id', 'children', 'executable', 
 
527
                 'revision', 'symlink_target']
 
528
 
523
529
    def _check(self, checker, rev_id, tree):
524
530
        """See InventoryEntry._check"""
525
 
        if self.text_sha1 != None or self.text_size != None or self.text_id != None:
 
531
        if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
526
532
            raise BzrCheckError('directory {%s} has text in revision {%s}'
527
533
                                % (self.file_id, rev_id))
528
534
 
563
569
class InventoryFile(InventoryEntry):
564
570
    """A file in an inventory."""
565
571
 
 
572
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
573
                 'text_id', 'parent_id', 'children', 'executable', 
 
574
                 'revision', 'symlink_target']
 
575
 
566
576
    def _check(self, checker, tree_revision_id, tree):
567
577
        """See InventoryEntry._check"""
568
578
        t = (self.file_id, self.revision)
607
617
 
608
618
    def detect_changes(self, old_entry):
609
619
        """See InventoryEntry.detect_changes."""
610
 
        assert self.text_sha1 != None
611
 
        assert old_entry.text_sha1 != None
 
620
        assert self.text_sha1 is not None
 
621
        assert old_entry.text_sha1 is not None
612
622
        text_modified = (self.text_sha1 != old_entry.text_sha1)
613
623
        meta_modified = (self.executable != old_entry.executable)
614
624
        return text_modified, meta_modified
671
681
        # in _read_tree_state
672
682
        self.executable = work_tree.is_executable(self.file_id, path=path)
673
683
 
 
684
    def __repr__(self):
 
685
        return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s)"
 
686
                % (self.__class__.__name__,
 
687
                   self.file_id,
 
688
                   self.name,
 
689
                   self.parent_id,
 
690
                   self.text_sha1,
 
691
                   self.text_size))
 
692
 
674
693
    def _forget_tree_state(self):
675
694
        self.text_sha1 = None
676
695
        self.executable = None
699
718
class InventoryLink(InventoryEntry):
700
719
    """A file in an inventory."""
701
720
 
702
 
    __slots__ = ['symlink_target']
 
721
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
722
                 'text_id', 'parent_id', 'children', 'executable', 
 
723
                 'revision', 'symlink_target']
703
724
 
704
725
    def _check(self, checker, rev_id, tree):
705
726
        """See InventoryEntry._check"""
706
 
        if self.text_sha1 != None or self.text_size != None or self.text_id != None:
 
727
        if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
707
728
            raise BzrCheckError('symlink {%s} has text in revision {%s}'
708
729
                    % (self.file_id, rev_id))
709
730
        if self.symlink_target is None:
805
826
 
806
827
    >>> inv = Inventory()
807
828
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
808
 
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT')
 
829
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT', sha1=None, len=None)
809
830
    >>> inv['123-123'].name
810
831
    'hello.c'
811
832
 
822
843
    [u'hello.c']
823
844
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
824
845
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
825
 
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT-12345678-12345678')
 
846
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT-12345678-12345678', sha1=None, len=None)
826
847
    """
827
848
    def __init__(self, root_id=ROOT_ID, revision_id=None):
828
849
        """Create or read an inventory.
839
860
        #if root_id is None:
840
861
        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
841
862
        self.root = RootEntry(root_id)
 
863
        # FIXME: this isn't ever used, changing it to self.revision may break
 
864
        # things. TODO make everything use self.revision_id
842
865
        self.revision_id = revision_id
843
866
        self._byid = {self.root.file_id: self.root}
844
867
 
974
997
 
975
998
        >>> inv = Inventory()
976
999
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
977
 
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT')
 
1000
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None)
978
1001
        >>> '123' in inv
979
1002
        True
980
1003
        >>> '456' in inv
987
1010
 
988
1011
        >>> inv = Inventory()
989
1012
        >>> inv.add(InventoryFile('123123', 'hello.c', ROOT_ID))
990
 
        InventoryFile('123123', 'hello.c', parent_id='TREE_ROOT')
 
1013
        InventoryFile('123123', 'hello.c', parent_id='TREE_ROOT', sha1=None, len=None)
991
1014
        >>> inv['123123'].name
992
1015
        'hello.c'
993
1016
        """
1024
1047
        except KeyError:
1025
1048
            raise BzrError("parent_id {%s} not in inventory" % entry.parent_id)
1026
1049
 
1027
 
        if parent.children.has_key(entry.name):
 
1050
        if entry.name in parent.children:
1028
1051
            raise BzrError("%s is already versioned" %
1029
1052
                    pathjoin(self.id2path(parent.file_id), entry.name))
1030
1053
 
1060
1083
 
1061
1084
        >>> inv = Inventory()
1062
1085
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
1063
 
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT')
 
1086
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None)
1064
1087
        >>> '123' in inv
1065
1088
        True
1066
1089
        >>> del inv['123']
1084
1107
        >>> i1 == i2
1085
1108
        True
1086
1109
        >>> i1.add(InventoryFile('123', 'foo', ROOT_ID))
1087
 
        InventoryFile('123', 'foo', parent_id='TREE_ROOT')
 
1110
        InventoryFile('123', 'foo', parent_id='TREE_ROOT', sha1=None, len=None)
1088
1111
        >>> i1 == i2
1089
1112
        False
1090
1113
        >>> i2.add(InventoryFile('123', 'foo', ROOT_ID))
1091
 
        InventoryFile('123', 'foo', parent_id='TREE_ROOT')
 
1114
        InventoryFile('123', 'foo', parent_id='TREE_ROOT', sha1=None, len=None)
1092
1115
        >>> i1 == i2
1093
1116
        True
1094
1117
        """
1095
1118
        if not isinstance(other, Inventory):
1096
1119
            return NotImplemented
1097
1120
 
1098
 
        if len(self._byid) != len(other._byid):
1099
 
            # shortcut: obviously not the same
1100
 
            return False
1101
 
 
1102
1121
        return self._byid == other._byid
1103
1122
 
1104
1123
    def __ne__(self, other):
1109
1128
 
1110
1129
    def _iter_file_id_parents(self, file_id):
1111
1130
        """Yield the parents of file_id up to the root."""
1112
 
        while file_id != None:
 
1131
        while file_id is not None:
1113
1132
            try:
1114
1133
                ie = self._byid[file_id]
1115
1134
            except KeyError: