~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Martin Pool
  • Date: 2005-07-22 23:58:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050722235811-bebdd984c5aec42e
- add some passing tests for is_inside_any

Show diffs side-by-side

added added

removed removed

Lines of Context:
92
92
    # TODO: split InventoryEntry into subclasses for files,
93
93
    # directories, etc etc.
94
94
 
95
 
    text_sha1 = None
96
 
    text_size = None
97
 
    
 
95
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
96
                 'text_id', 'parent_id', 'children', ]
 
97
 
98
98
    def __init__(self, file_id, name, kind, parent_id, text_id=None):
99
99
        """Create an InventoryEntry
100
100
        
113
113
        if '/' in name or '\\' in name:
114
114
            raise BzrCheckError('InventoryEntry name %r is invalid' % name)
115
115
        
 
116
        self.text_sha1 = None
 
117
        self.text_size = None
 
118
    
116
119
        self.file_id = file_id
117
120
        self.name = name
118
121
        self.kind = kind
277
280
 
278
281
    >>> [x[0] for x in inv.iter_entries()]
279
282
    ['hello.c']
 
283
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
 
284
    >>> inv.add(InventoryEntry('123-123', 'hello.c', 'file', ROOT_ID))
280
285
    """
281
 
    def __init__(self):
 
286
    def __init__(self, root_id=ROOT_ID):
282
287
        """Create or read an inventory.
283
288
 
284
289
        If a working directory is specified, the inventory is read
288
293
        The inventory is created with a default root directory, with
289
294
        an id of None.
290
295
        """
291
 
        self.root = RootEntry(ROOT_ID)
 
296
        # We are letting Branch(init=True) create a unique inventory
 
297
        # root id. Rather than generating a random one here.
 
298
        #if root_id is None:
 
299
        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
 
300
        self.root = RootEntry(root_id)
292
301
        self._byid = {self.root.file_id: self.root}
293
302
 
294
303
 
400
409
        if entry.file_id in self._byid:
401
410
            raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
402
411
 
 
412
        if entry.parent_id == ROOT_ID or entry.parent_id is None:
 
413
            entry.parent_id = self.root.file_id
 
414
 
403
415
        try:
404
416
            parent = self._byid[entry.parent_id]
405
417
        except KeyError:
469
481
        
470
482
        e = Element('inventory')
471
483
        e.text = '\n'
 
484
        if self.root.file_id not in (None, ROOT_ID):
 
485
            e.set('file_id', self.root.file_id)
472
486
        for path, ie in self.iter_entries():
473
487
            e.append(ie.to_element())
474
488
        return e
486
500
        """
487
501
        # XXXX: doctest doesn't run this properly under python2.3
488
502
        assert elt.tag == 'inventory'
489
 
        o = cls()
 
503
        root_id = elt.get('file_id') or ROOT_ID
 
504
        o = cls(root_id)
490
505
        for e in elt:
491
 
            o.add(InventoryEntry.from_element(e))
 
506
            ie = InventoryEntry.from_element(e)
 
507
            if ie.parent_id == ROOT_ID:
 
508
                ie.parent_id = root_id
 
509
            o.add(ie)
492
510
        return o
493
511
        
494
512
    from_element = classmethod(from_element)
550
568
        """Return as a list the path to file_id."""
551
569
 
552
570
        # get all names, skipping root
553
 
        p = [self[fid].name for fid in self.get_idpath(file_id)[1:]]
 
571
        p = [self._byid[fid].name for fid in self.get_idpath(file_id)[1:]]
554
572
        return os.sep.join(p)
555
573
            
556
574