~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Martin Pool
  • Date: 2005-07-20 17:31:25 UTC
  • Revision ID: mbp@sourcefrog.net-20050720173125-e5974e73bd5fc31e
- add back update-hashes command for testing/profiling

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
from bzrlib.osutils import uuid, quotefn, splitpath, joinpath, appendpath
29
29
from bzrlib.trace import mutter
30
 
from bzrlib.errors import NotVersionedError
31
 
        
32
30
 
33
31
class InventoryEntry(object):
34
32
    """Description of a versioned file.
55
53
    >>> i.path2id('')
56
54
    'TREE_ROOT'
57
55
    >>> i.add(InventoryEntry('123', 'src', 'directory', ROOT_ID))
58
 
    InventoryEntry('123', 'src', kind='directory', parent_id='TREE_ROOT')
59
56
    >>> i.add(InventoryEntry('2323', 'hello.c', 'file', parent_id='123'))
60
 
    InventoryEntry('2323', 'hello.c', kind='file', parent_id='123')
61
57
    >>> for j in i.iter_entries():
62
58
    ...   print j
63
59
    ... 
68
64
    ...
69
65
    BzrError: inventory already contains entry with id {2323}
70
66
    >>> i.add(InventoryEntry('2324', 'bye.c', 'file', '123'))
71
 
    InventoryEntry('2324', 'bye.c', kind='file', parent_id='123')
72
67
    >>> i.add(InventoryEntry('2325', 'wibble', 'directory', '123'))
73
 
    InventoryEntry('2325', 'wibble', kind='directory', parent_id='123')
74
68
    >>> i.path2id('src/wibble')
75
69
    '2325'
76
70
    >>> '2325' in i
77
71
    True
78
72
    >>> i.add(InventoryEntry('2326', 'wibble.c', 'file', '2325'))
79
 
    InventoryEntry('2326', 'wibble.c', kind='file', parent_id='2325')
80
73
    >>> i['2326']
81
74
    InventoryEntry('2326', 'wibble.c', kind='file', parent_id='2325')
82
75
    >>> for j in i.iter_entries():
99
92
    # TODO: split InventoryEntry into subclasses for files,
100
93
    # directories, etc etc.
101
94
 
102
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
103
 
                 'text_id', 'parent_id', 'children', ]
104
 
 
 
95
    text_sha1 = None
 
96
    text_size = None
 
97
    
105
98
    def __init__(self, file_id, name, kind, parent_id, text_id=None):
106
99
        """Create an InventoryEntry
107
100
        
120
113
        if '/' in name or '\\' in name:
121
114
            raise BzrCheckError('InventoryEntry name %r is invalid' % name)
122
115
        
123
 
        self.text_sha1 = None
124
 
        self.text_size = None
125
 
    
126
116
        self.file_id = file_id
127
117
        self.name = name
128
118
        self.kind = kind
273
263
 
274
264
    >>> inv = Inventory()
275
265
    >>> inv.add(InventoryEntry('123-123', 'hello.c', 'file', ROOT_ID))
276
 
    InventoryEntry('123-123', 'hello.c', kind='file', parent_id='TREE_ROOT')
277
266
    >>> inv['123-123'].name
278
267
    'hello.c'
279
268
 
290
279
    ['hello.c']
291
280
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
292
281
    >>> inv.add(InventoryEntry('123-123', 'hello.c', 'file', ROOT_ID))
293
 
    InventoryEntry('123-123', 'hello.c', kind='file', parent_id='TREE_ROOT-12345678-12345678')
294
282
    """
295
283
    def __init__(self, root_id=ROOT_ID):
296
284
        """Create or read an inventory.
378
366
 
379
367
        >>> inv = Inventory()
380
368
        >>> inv.add(InventoryEntry('123', 'foo.c', 'file', ROOT_ID))
381
 
        InventoryEntry('123', 'foo.c', kind='file', parent_id='TREE_ROOT')
382
369
        >>> '123' in inv
383
370
        True
384
371
        >>> '456' in inv
392
379
 
393
380
        >>> inv = Inventory()
394
381
        >>> inv.add(InventoryEntry('123123', 'hello.c', 'file', ROOT_ID))
395
 
        InventoryEntry('123123', 'hello.c', kind='file', parent_id='TREE_ROOT')
396
382
        >>> inv['123123'].name
397
383
        'hello.c'
398
384
        """
434
420
 
435
421
        self._byid[entry.file_id] = entry
436
422
        parent.children[entry.name] = entry
437
 
        return entry
438
423
 
439
424
 
440
425
    def add_path(self, relpath, kind, file_id=None):
441
426
        """Add entry from a path.
442
427
 
443
428
        The immediate parent must already be versioned"""
444
 
        from bzrlib.branch import gen_file_id
 
429
        from bzrlib.errors import NotVersionedError
445
430
        
446
431
        parts = bzrlib.osutils.splitpath(relpath)
447
432
        if len(parts) == 0:
448
433
            raise BzrError("cannot re-add root of inventory")
449
434
 
450
435
        if file_id == None:
 
436
            from bzrlib.branch import gen_file_id
451
437
            file_id = gen_file_id(relpath)
452
438
 
453
439
        parent_path = parts[:-1]
465
451
 
466
452
        >>> inv = Inventory()
467
453
        >>> inv.add(InventoryEntry('123', 'foo.c', 'file', ROOT_ID))
468
 
        InventoryEntry('123', 'foo.c', kind='file', parent_id='TREE_ROOT')
469
454
        >>> '123' in inv
470
455
        True
471
456
        >>> del inv['123']
505
490
        
506
491
        >>> inv = Inventory()
507
492
        >>> 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
493
        >>> elt = inv.to_element()
510
494
        >>> inv2 = Inventory.from_element(elt)
511
495
        >>> inv2 == inv
533
517
        >>> i1 == i2
534
518
        True
535
519
        >>> i1.add(InventoryEntry('123', 'foo', 'file', ROOT_ID))
536
 
        InventoryEntry('123', 'foo', kind='file', parent_id='TREE_ROOT')
537
520
        >>> i1 == i2
538
521
        False
539
522
        >>> i2.add(InventoryEntry('123', 'foo', 'file', ROOT_ID))
540
 
        InventoryEntry('123', 'foo', kind='file', parent_id='TREE_ROOT')
541
523
        >>> i1 == i2
542
524
        True
543
525
        """
658
640
 
659
641
 
660
642
 
661
 
_NAME_RE = None
 
643
_NAME_RE = re.compile(r'^[^/\\]+$')
662
644
 
663
645
def is_valid_name(name):
664
 
    global _NAME_RE
665
 
    if _NAME_RE == None:
666
 
        _NAME_RE = re.compile(r'^[^/\\]+$')
667
 
        
668
646
    return bool(_NAME_RE.match(name))