100
100
# directories, etc etc.
102
102
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
103
'text_id', 'parent_id', 'children', 'symlink_target']
103
'text_id', 'parent_id', 'children',
104
'text_version', 'entry_version', 'symlink_target']
105
107
def __init__(self, file_id, name, kind, parent_id, text_id=None):
106
108
"""Create an InventoryEntry
117
119
Traceback (most recent call last):
118
120
BzrCheckError: InventoryEntry name 'src/hello.c' is invalid
122
assert isinstance(name, basestring), name
120
123
if '/' in name or '\\' in name:
121
124
raise BzrCheckError('InventoryEntry name %r is invalid' % name)
126
self.text_version = None
127
self.entry_version = None
123
128
self.text_sha1 = None
124
129
self.text_size = None
126
130
self.file_id = file_id
174
def to_element(self):
175
"""Convert to XML element"""
176
from bzrlib.xml import Element
180
e.set('name', self.name)
181
e.set('file_id', self.file_id)
182
e.set('kind', self.kind)
184
if self.text_size != None:
185
e.set('text_size', '%d' % self.text_size)
187
for f in ['text_id', 'text_sha1', 'symlink_target']:
192
# to be conservative, we don't externalize the root pointers
193
# for now, leaving them as null in the xml form. in a future
194
# version it will be implied by nested elements.
195
if self.parent_id != ROOT_ID:
196
assert isinstance(self.parent_id, basestring)
197
e.set('parent_id', self.parent_id)
204
def from_element(cls, elt):
205
assert elt.tag == 'entry'
207
## original format inventories don't have a parent_id for
208
## nodes in the root directory, but it's cleaner to use one
210
parent_id = elt.get('parent_id')
211
if parent_id == None:
214
self = cls(elt.get('file_id'), elt.get('name'), elt.get('kind'), parent_id)
215
self.text_id = elt.get('text_id')
216
self.text_sha1 = elt.get('text_sha1')
217
self.symlink_target = elt.get('symlink_target')
219
## mutter("read inventoryentry: %r" % (elt.attrib))
221
v = elt.get('text_size')
222
self.text_size = v and int(v)
227
from_element = classmethod(from_element)
229
177
def __eq__(self, other):
230
178
if not isinstance(other, InventoryEntry):
231
179
return NotImplemented
237
185
and (self.text_size == other.text_size) \
238
186
and (self.text_id == other.text_id) \
239
187
and (self.parent_id == other.parent_id) \
240
and (self.kind == other.kind)
188
and (self.kind == other.kind) \
189
and (self.text_version == other.text_version) \
190
and (self.entry_version == other.entry_version)
243
192
def __ne__(self, other):
244
193
return not (self == other)
427
376
"""Add entry to inventory.
429
378
To add a file to a branch ready to be committed, use Branch.add,
381
Returns the new entry object.
431
383
if entry.file_id in self._byid:
432
384
raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
451
403
def add_path(self, relpath, kind, file_id=None):
452
404
"""Add entry from a path.
454
The immediate parent must already be versioned"""
406
The immediate parent must already be versioned.
408
Returns the new entry object."""
455
409
from bzrlib.branch import gen_file_id
457
411
parts = bzrlib.osutils.splitpath(relpath)
498
452
del self[ie.parent_id].children[ie.name]
501
def to_element(self):
502
"""Convert to XML Element"""
503
from bzrlib.xml import Element
505
e = Element('inventory')
507
if self.root.file_id not in (None, ROOT_ID):
508
e.set('file_id', self.root.file_id)
509
for path, ie in self.iter_entries():
510
e.append(ie.to_element())
514
def from_element(cls, elt):
515
"""Construct from XML Element
517
>>> inv = Inventory()
518
>>> inv.add(InventoryEntry('foo.c-123981239', 'foo.c', 'file', ROOT_ID))
519
InventoryEntry('foo.c-123981239', 'foo.c', kind='file', parent_id='TREE_ROOT')
520
>>> elt = inv.to_element()
521
>>> inv2 = Inventory.from_element(elt)
525
# XXXX: doctest doesn't run this properly under python2.3
526
assert elt.tag == 'inventory'
527
root_id = elt.get('file_id') or ROOT_ID
530
ie = InventoryEntry.from_element(e)
531
if ie.parent_id == ROOT_ID:
532
ie.parent_id = root_id
536
from_element = classmethod(from_element)
539
455
def __eq__(self, other):
540
456
"""Compare two sets by comparing their contents.