~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Robert Collins
  • Date: 2005-08-25 12:46:42 UTC
  • mfrom: (1116)
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050825124642-45ed1cd74db10370
merge from mpool

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
    # directories, etc etc.
101
101
 
102
102
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
103
 
                 'text_id', 'parent_id', 'children',
104
 
                 'text_version', 'entry_version', ]
105
 
 
 
103
                 'text_id', 'parent_id', 'children', ]
106
104
 
107
105
    def __init__(self, file_id, name, kind, parent_id, text_id=None):
108
106
        """Create an InventoryEntry
119
117
        Traceback (most recent call last):
120
118
        BzrCheckError: InventoryEntry name 'src/hello.c' is invalid
121
119
        """
122
 
        assert isinstance(name, basestring), name
123
120
        if '/' in name or '\\' in name:
124
121
            raise BzrCheckError('InventoryEntry name %r is invalid' % name)
125
122
        
126
 
        self.text_version = None
127
 
        self.entry_version = None
128
123
        self.text_sha1 = None
129
124
        self.text_size = None
 
125
    
130
126
        self.file_id = file_id
131
127
        self.name = name
132
128
        self.kind = kind
166
162
                   self.parent_id))
167
163
 
168
164
    
 
165
    def to_element(self):
 
166
        """Convert to XML element"""
 
167
        from bzrlib.xml import Element
 
168
        
 
169
        e = Element('entry')
 
170
 
 
171
        e.set('name', self.name)
 
172
        e.set('file_id', self.file_id)
 
173
        e.set('kind', self.kind)
 
174
 
 
175
        if self.text_size != None:
 
176
            e.set('text_size', '%d' % self.text_size)
 
177
            
 
178
        for f in ['text_id', 'text_sha1']:
 
179
            v = getattr(self, f)
 
180
            if v != None:
 
181
                e.set(f, v)
 
182
 
 
183
        # to be conservative, we don't externalize the root pointers
 
184
        # for now, leaving them as null in the xml form.  in a future
 
185
        # version it will be implied by nested elements.
 
186
        if self.parent_id != ROOT_ID:
 
187
            assert isinstance(self.parent_id, basestring)
 
188
            e.set('parent_id', self.parent_id)
 
189
 
 
190
        e.tail = '\n'
 
191
            
 
192
        return e
 
193
 
 
194
 
 
195
    def from_element(cls, elt):
 
196
        assert elt.tag == 'entry'
 
197
 
 
198
        ## original format inventories don't have a parent_id for
 
199
        ## nodes in the root directory, but it's cleaner to use one
 
200
        ## internally.
 
201
        parent_id = elt.get('parent_id')
 
202
        if parent_id == None:
 
203
            parent_id = ROOT_ID
 
204
 
 
205
        self = cls(elt.get('file_id'), elt.get('name'), elt.get('kind'), parent_id)
 
206
        self.text_id = elt.get('text_id')
 
207
        self.text_sha1 = elt.get('text_sha1')
 
208
        
 
209
        ## mutter("read inventoryentry: %r" % (elt.attrib))
 
210
 
 
211
        v = elt.get('text_size')
 
212
        self.text_size = v and int(v)
 
213
 
 
214
        return self
 
215
            
 
216
 
 
217
    from_element = classmethod(from_element)
 
218
 
169
219
    def __eq__(self, other):
170
220
        if not isinstance(other, InventoryEntry):
171
221
            return NotImplemented
176
226
               and (self.text_size == other.text_size) \
177
227
               and (self.text_id == other.text_id) \
178
228
               and (self.parent_id == other.parent_id) \
179
 
               and (self.kind == other.kind) \
180
 
               and (self.text_version == other.text_version) \
181
 
               and (self.entry_version == other.entry_version)
 
229
               and (self.kind == other.kind)
182
230
 
183
231
 
184
232
    def __ne__(self, other):
254
302
        The inventory is created with a default root directory, with
255
303
        an id of None.
256
304
        """
257
 
        # We are letting Branch.initialize() create a unique inventory
 
305
        # We are letting Branch(init=True) create a unique inventory
258
306
        # root id. Rather than generating a random one here.
259
307
        #if root_id is None:
260
308
        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
368
416
        """Add entry to inventory.
369
417
 
370
418
        To add  a file to a branch ready to be committed, use Branch.add,
371
 
        which calls this.
372
 
 
373
 
        Returns the new entry object.
374
 
        """
 
419
        which calls this."""
375
420
        if entry.file_id in self._byid:
376
421
            raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
377
422
 
395
440
    def add_path(self, relpath, kind, file_id=None):
396
441
        """Add entry from a path.
397
442
 
398
 
        The immediate parent must already be versioned.
399
 
 
400
 
        Returns the new entry object."""
 
443
        The immediate parent must already be versioned"""
401
444
        from bzrlib.branch import gen_file_id
402
445
        
403
446
        parts = bzrlib.osutils.splitpath(relpath)
444
487
        del self[ie.parent_id].children[ie.name]
445
488
 
446
489
 
 
490
    def to_element(self):
 
491
        """Convert to XML Element"""
 
492
        from bzrlib.xml import Element
 
493
        
 
494
        e = Element('inventory')
 
495
        e.text = '\n'
 
496
        if self.root.file_id not in (None, ROOT_ID):
 
497
            e.set('file_id', self.root.file_id)
 
498
        for path, ie in self.iter_entries():
 
499
            e.append(ie.to_element())
 
500
        return e
 
501
    
 
502
 
 
503
    def from_element(cls, elt):
 
504
        """Construct from XML Element
 
505
        
 
506
        >>> inv = Inventory()
 
507
        >>> inv.add(InventoryEntry('foo.c-123981239', 'foo.c', 'file', ROOT_ID))
 
508
        InventoryEntry('foo.c-123981239', 'foo.c', kind='file', parent_id='TREE_ROOT')
 
509
        >>> elt = inv.to_element()
 
510
        >>> inv2 = Inventory.from_element(elt)
 
511
        >>> inv2 == inv
 
512
        True
 
513
        """
 
514
        # XXXX: doctest doesn't run this properly under python2.3
 
515
        assert elt.tag == 'inventory'
 
516
        root_id = elt.get('file_id') or ROOT_ID
 
517
        o = cls(root_id)
 
518
        for e in elt:
 
519
            ie = InventoryEntry.from_element(e)
 
520
            if ie.parent_id == ROOT_ID:
 
521
                ie.parent_id = root_id
 
522
            o.add(ie)
 
523
        return o
 
524
        
 
525
    from_element = classmethod(from_element)
 
526
 
 
527
 
447
528
    def __eq__(self, other):
448
529
        """Compare two sets by comparing their contents.
449
530
 
478
559
        raise ValueError('not hashable')
479
560
 
480
561
 
 
562
 
481
563
    def get_idpath(self, file_id):
482
564
        """Return a list of file_ids for the path to an entry.
483
565