/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: Aaron Bentley
  • Date: 2009-06-29 14:51:13 UTC
  • mfrom: (4489 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4490.
  • Revision ID: aaron@aaronbentley.com-20090629145113-3w350dxgqppnzo4g
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
# FIXME: This refactoring of the workingtree code doesn't seem to keep
 
18
# the WorkingTree's copy of the inventory in sync with the branch.  The
 
19
# branch modifies its working inventory when it does a commit to make
 
20
# missing files permanently removed.
 
21
 
 
22
# TODO: Maybe also keep the full path of the entry, and the children?
 
23
# But those depend on its position within a particular inventory, and
 
24
# it would be nice not to need to hold the backpointer here.
 
25
 
 
26
# This should really be an id randomly assigned when the tree is
 
27
# created, but it's not for now.
 
28
ROOT_ID = "TREE_ROOT"
 
29
 
 
30
from copy import deepcopy
 
31
 
 
32
from bzrlib.lazy_import import lazy_import
 
33
lazy_import(globals(), """
 
34
import collections
 
35
import os
 
36
import re
 
37
import tarfile
 
38
 
 
39
import bzrlib
 
40
from bzrlib import (
 
41
    chk_map,
 
42
    errors,
 
43
    generate_ids,
 
44
    osutils,
 
45
    symbol_versioning,
 
46
    workingtree,
 
47
    )
 
48
""")
 
49
 
 
50
from bzrlib.errors import (
 
51
    BzrCheckError,
 
52
    BzrError,
 
53
    )
 
54
from bzrlib.symbol_versioning import deprecated_in, deprecated_method
 
55
from bzrlib.trace import mutter
 
56
 
 
57
 
 
58
class InventoryEntry(object):
 
59
    """Description of a versioned file.
 
60
 
 
61
    An InventoryEntry has the following fields, which are also
 
62
    present in the XML inventory-entry element:
 
63
 
 
64
    file_id
 
65
 
 
66
    name
 
67
        (within the parent directory)
 
68
 
 
69
    parent_id
 
70
        file_id of the parent directory, or ROOT_ID
 
71
 
 
72
    revision
 
73
        the revision_id in which this variation of this file was
 
74
        introduced.
 
75
 
 
76
    executable
 
77
        Indicates that this file should be executable on systems
 
78
        that support it.
 
79
 
 
80
    text_sha1
 
81
        sha-1 of the text of the file
 
82
 
 
83
    text_size
 
84
        size in bytes of the text of the file
 
85
 
 
86
    (reading a version 4 tree created a text_id field.)
 
87
 
 
88
    >>> i = Inventory()
 
89
    >>> i.path2id('')
 
90
    'TREE_ROOT'
 
91
    >>> i.add(InventoryDirectory('123', 'src', ROOT_ID))
 
92
    InventoryDirectory('123', 'src', parent_id='TREE_ROOT', revision=None)
 
93
    >>> i.add(InventoryFile('2323', 'hello.c', parent_id='123'))
 
94
    InventoryFile('2323', 'hello.c', parent_id='123', sha1=None, len=None, revision=None)
 
95
    >>> shouldbe = {0: '', 1: 'src', 2: 'src/hello.c'}
 
96
    >>> for ix, j in enumerate(i.iter_entries()):
 
97
    ...   print (j[0] == shouldbe[ix], j[1])
 
98
    ...
 
99
    (True, InventoryDirectory('TREE_ROOT', u'', parent_id=None, revision=None))
 
100
    (True, InventoryDirectory('123', 'src', parent_id='TREE_ROOT', revision=None))
 
101
    (True, InventoryFile('2323', 'hello.c', parent_id='123', sha1=None, len=None, revision=None))
 
102
    >>> i.add(InventoryFile('2324', 'bye.c', '123'))
 
103
    InventoryFile('2324', 'bye.c', parent_id='123', sha1=None, len=None, revision=None)
 
104
    >>> i.add(InventoryDirectory('2325', 'wibble', '123'))
 
105
    InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
 
106
    >>> i.path2id('src/wibble')
 
107
    '2325'
 
108
    >>> '2325' in i
 
109
    True
 
110
    >>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
 
111
    InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
 
112
    >>> i['2326']
 
113
    InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
 
114
    >>> for path, entry in i.iter_entries():
 
115
    ...     print path
 
116
    ...
 
117
    <BLANKLINE>
 
118
    src
 
119
    src/bye.c
 
120
    src/hello.c
 
121
    src/wibble
 
122
    src/wibble/wibble.c
 
123
    >>> i.id2path('2326')
 
124
    'src/wibble/wibble.c'
 
125
    """
 
126
 
 
127
    # Constants returned by describe_change()
 
128
    #
 
129
    # TODO: These should probably move to some kind of FileChangeDescription
 
130
    # class; that's like what's inside a TreeDelta but we want to be able to
 
131
    # generate them just for one file at a time.
 
132
    RENAMED = 'renamed'
 
133
    MODIFIED_AND_RENAMED = 'modified and renamed'
 
134
 
 
135
    __slots__ = []
 
136
 
 
137
    def detect_changes(self, old_entry):
 
138
        """Return a (text_modified, meta_modified) from this to old_entry.
 
139
 
 
140
        _read_tree_state must have been called on self and old_entry prior to
 
141
        calling detect_changes.
 
142
        """
 
143
        return False, False
 
144
 
 
145
    def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
 
146
             output_to, reverse=False):
 
147
        """Perform a diff between two entries of the same kind."""
 
148
 
 
149
    def parent_candidates(self, previous_inventories):
 
150
        """Find possible per-file graph parents.
 
151
 
 
152
        This is currently defined by:
 
153
         - Select the last changed revision in the parent inventory.
 
154
         - Do deal with a short lived bug in bzr 0.8's development two entries
 
155
           that have the same last changed but different 'x' bit settings are
 
156
           changed in-place.
 
157
        """
 
158
        # revision:ie mapping for each ie found in previous_inventories.
 
159
        candidates = {}
 
160
        # identify candidate head revision ids.
 
161
        for inv in previous_inventories:
 
162
            if self.file_id in inv:
 
163
                ie = inv[self.file_id]
 
164
                if ie.revision in candidates:
 
165
                    # same revision value in two different inventories:
 
166
                    # correct possible inconsistencies:
 
167
                    #     * there was a bug in revision updates with 'x' bit
 
168
                    #       support.
 
169
                    try:
 
170
                        if candidates[ie.revision].executable != ie.executable:
 
171
                            candidates[ie.revision].executable = False
 
172
                            ie.executable = False
 
173
                    except AttributeError:
 
174
                        pass
 
175
                else:
 
176
                    # add this revision as a candidate.
 
177
                    candidates[ie.revision] = ie
 
178
        return candidates
 
179
 
 
180
    @deprecated_method(deprecated_in((1, 6, 0)))
 
181
    def get_tar_item(self, root, dp, now, tree):
 
182
        """Get a tarfile item and a file stream for its content."""
 
183
        item = tarfile.TarInfo(osutils.pathjoin(root, dp).encode('utf8'))
 
184
        # TODO: would be cool to actually set it to the timestamp of the
 
185
        # revision it was last changed
 
186
        item.mtime = now
 
187
        fileobj = self._put_in_tar(item, tree)
 
188
        return item, fileobj
 
189
 
 
190
    def has_text(self):
 
191
        """Return true if the object this entry represents has textual data.
 
192
 
 
193
        Note that textual data includes binary content.
 
194
 
 
195
        Also note that all entries get weave files created for them.
 
196
        This attribute is primarily used when upgrading from old trees that
 
197
        did not have the weave index for all inventory entries.
 
198
        """
 
199
        return False
 
200
 
 
201
    def __init__(self, file_id, name, parent_id, text_id=None):
 
202
        """Create an InventoryEntry
 
203
 
 
204
        The filename must be a single component, relative to the
 
205
        parent directory; it cannot be a whole path or relative name.
 
206
 
 
207
        >>> e = InventoryFile('123', 'hello.c', ROOT_ID)
 
208
        >>> e.name
 
209
        'hello.c'
 
210
        >>> e.file_id
 
211
        '123'
 
212
        >>> e = InventoryFile('123', 'src/hello.c', ROOT_ID)
 
213
        Traceback (most recent call last):
 
214
        InvalidEntryName: Invalid entry name: src/hello.c
 
215
        """
 
216
        if '/' in name or '\\' in name:
 
217
            raise errors.InvalidEntryName(name=name)
 
218
        self.executable = False
 
219
        self.revision = None
 
220
        self.text_sha1 = None
 
221
        self.text_size = None
 
222
        self.file_id = file_id
 
223
        self.name = name
 
224
        self.text_id = text_id
 
225
        self.parent_id = parent_id
 
226
        self.symlink_target = None
 
227
        self.reference_revision = None
 
228
 
 
229
    def kind_character(self):
 
230
        """Return a short kind indicator useful for appending to names."""
 
231
        raise BzrError('unknown kind %r' % self.kind)
 
232
 
 
233
    known_kinds = ('file', 'directory', 'symlink')
 
234
 
 
235
    def _put_in_tar(self, item, tree):
 
236
        """populate item for stashing in a tar, and return the content stream.
 
237
 
 
238
        If no content is available, return None.
 
239
        """
 
240
        raise BzrError("don't know how to export {%s} of kind %r" %
 
241
                       (self.file_id, self.kind))
 
242
 
 
243
    @deprecated_method(deprecated_in((1, 6, 0)))
 
244
    def put_on_disk(self, dest, dp, tree):
 
245
        """Create a representation of self on disk in the prefix dest.
 
246
 
 
247
        This is a template method - implement _put_on_disk in subclasses.
 
248
        """
 
249
        fullpath = osutils.pathjoin(dest, dp)
 
250
        self._put_on_disk(fullpath, tree)
 
251
        # mutter("  export {%s} kind %s to %s", self.file_id,
 
252
        #         self.kind, fullpath)
 
253
 
 
254
    def _put_on_disk(self, fullpath, tree):
 
255
        """Put this entry onto disk at fullpath, from tree tree."""
 
256
        raise BzrError("don't know how to export {%s} of kind %r" % (self.file_id, self.kind))
 
257
 
 
258
    def sorted_children(self):
 
259
        return sorted(self.children.items())
 
260
 
 
261
    @staticmethod
 
262
    def versionable_kind(kind):
 
263
        return (kind in ('file', 'directory', 'symlink', 'tree-reference'))
 
264
 
 
265
    def check(self, checker, rev_id, inv, tree):
 
266
        """Check this inventory entry is intact.
 
267
 
 
268
        This is a template method, override _check for kind specific
 
269
        tests.
 
270
 
 
271
        :param checker: Check object providing context for the checks;
 
272
             can be used to find out what parts of the repository have already
 
273
             been checked.
 
274
        :param rev_id: Revision id from which this InventoryEntry was loaded.
 
275
             Not necessarily the last-changed revision for this file.
 
276
        :param inv: Inventory from which the entry was loaded.
 
277
        :param tree: RevisionTree for this entry.
 
278
        """
 
279
        if self.parent_id is not None:
 
280
            if not inv.has_id(self.parent_id):
 
281
                raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
 
282
                        % (self.parent_id, rev_id))
 
283
        self._check(checker, rev_id, tree)
 
284
 
 
285
    def _check(self, checker, rev_id, tree):
 
286
        """Check this inventory entry for kind specific errors."""
 
287
        raise BzrCheckError('unknown entry kind %r in revision {%s}' %
 
288
                            (self.kind, rev_id))
 
289
 
 
290
    def copy(self):
 
291
        """Clone this inventory entry."""
 
292
        raise NotImplementedError
 
293
 
 
294
    @staticmethod
 
295
    def describe_change(old_entry, new_entry):
 
296
        """Describe the change between old_entry and this.
 
297
 
 
298
        This smells of being an InterInventoryEntry situation, but as its
 
299
        the first one, we're making it a static method for now.
 
300
 
 
301
        An entry with a different parent, or different name is considered
 
302
        to be renamed. Reparenting is an internal detail.
 
303
        Note that renaming the parent does not trigger a rename for the
 
304
        child entry itself.
 
305
        """
 
306
        # TODO: Perhaps return an object rather than just a string
 
307
        if old_entry is new_entry:
 
308
            # also the case of both being None
 
309
            return 'unchanged'
 
310
        elif old_entry is None:
 
311
            return 'added'
 
312
        elif new_entry is None:
 
313
            return 'removed'
 
314
        if old_entry.kind != new_entry.kind:
 
315
            return 'modified'
 
316
        text_modified, meta_modified = new_entry.detect_changes(old_entry)
 
317
        if text_modified or meta_modified:
 
318
            modified = True
 
319
        else:
 
320
            modified = False
 
321
        # TODO 20060511 (mbp, rbc) factor out 'detect_rename' here.
 
322
        if old_entry.parent_id != new_entry.parent_id:
 
323
            renamed = True
 
324
        elif old_entry.name != new_entry.name:
 
325
            renamed = True
 
326
        else:
 
327
            renamed = False
 
328
        if renamed and not modified:
 
329
            return InventoryEntry.RENAMED
 
330
        if modified and not renamed:
 
331
            return 'modified'
 
332
        if modified and renamed:
 
333
            return InventoryEntry.MODIFIED_AND_RENAMED
 
334
        return 'unchanged'
 
335
 
 
336
    def __repr__(self):
 
337
        return ("%s(%r, %r, parent_id=%r, revision=%r)"
 
338
                % (self.__class__.__name__,
 
339
                   self.file_id,
 
340
                   self.name,
 
341
                   self.parent_id,
 
342
                   self.revision))
 
343
 
 
344
    def __eq__(self, other):
 
345
        if other is self:
 
346
            # For the case when objects are cached
 
347
            return True
 
348
        if not isinstance(other, InventoryEntry):
 
349
            return NotImplemented
 
350
 
 
351
        return ((self.file_id == other.file_id)
 
352
                and (self.name == other.name)
 
353
                and (other.symlink_target == self.symlink_target)
 
354
                and (self.text_sha1 == other.text_sha1)
 
355
                and (self.text_size == other.text_size)
 
356
                and (self.text_id == other.text_id)
 
357
                and (self.parent_id == other.parent_id)
 
358
                and (self.kind == other.kind)
 
359
                and (self.revision == other.revision)
 
360
                and (self.executable == other.executable)
 
361
                and (self.reference_revision == other.reference_revision)
 
362
                )
 
363
 
 
364
    def __ne__(self, other):
 
365
        return not (self == other)
 
366
 
 
367
    def __hash__(self):
 
368
        raise ValueError('not hashable')
 
369
 
 
370
    def _unchanged(self, previous_ie):
 
371
        """Has this entry changed relative to previous_ie.
 
372
 
 
373
        This method should be overridden in child classes.
 
374
        """
 
375
        compatible = True
 
376
        # different inv parent
 
377
        if previous_ie.parent_id != self.parent_id:
 
378
            compatible = False
 
379
        # renamed
 
380
        elif previous_ie.name != self.name:
 
381
            compatible = False
 
382
        elif previous_ie.kind != self.kind:
 
383
            compatible = False
 
384
        return compatible
 
385
 
 
386
    def _read_tree_state(self, path, work_tree):
 
387
        """Populate fields in the inventory entry from the given tree.
 
388
 
 
389
        Note that this should be modified to be a noop on virtual trees
 
390
        as all entries created there are prepopulated.
 
391
        """
 
392
        # TODO: Rather than running this manually, we should check the
 
393
        # working sha1 and other expensive properties when they're
 
394
        # first requested, or preload them if they're already known
 
395
        pass            # nothing to do by default
 
396
 
 
397
    def _forget_tree_state(self):
 
398
        pass
 
399
 
 
400
 
 
401
class RootEntry(InventoryEntry):
 
402
 
 
403
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
404
                 'text_id', 'parent_id', 'children', 'executable',
 
405
                 'revision', 'symlink_target', 'reference_revision']
 
406
 
 
407
    def _check(self, checker, rev_id, tree):
 
408
        """See InventoryEntry._check"""
 
409
 
 
410
    def __init__(self, file_id):
 
411
        self.file_id = file_id
 
412
        self.children = {}
 
413
        self.kind = 'directory'
 
414
        self.parent_id = None
 
415
        self.name = u''
 
416
        self.revision = None
 
417
        symbol_versioning.warn('RootEntry is deprecated as of bzr 0.10.'
 
418
                               '  Please use InventoryDirectory instead.',
 
419
                               DeprecationWarning, stacklevel=2)
 
420
 
 
421
    def __eq__(self, other):
 
422
        if not isinstance(other, RootEntry):
 
423
            return NotImplemented
 
424
 
 
425
        return (self.file_id == other.file_id) \
 
426
               and (self.children == other.children)
 
427
 
 
428
 
 
429
class InventoryDirectory(InventoryEntry):
 
430
    """A directory in an inventory."""
 
431
 
 
432
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
433
                 'text_id', 'parent_id', 'children', 'executable',
 
434
                 'revision', 'symlink_target', 'reference_revision']
 
435
 
 
436
    def _check(self, checker, rev_id, tree):
 
437
        """See InventoryEntry._check"""
 
438
        if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
 
439
            raise BzrCheckError('directory {%s} has text in revision {%s}'
 
440
                                % (self.file_id, rev_id))
 
441
 
 
442
    def copy(self):
 
443
        other = InventoryDirectory(self.file_id, self.name, self.parent_id)
 
444
        other.revision = self.revision
 
445
        # note that children are *not* copied; they're pulled across when
 
446
        # others are added
 
447
        return other
 
448
 
 
449
    def __init__(self, file_id, name, parent_id):
 
450
        super(InventoryDirectory, self).__init__(file_id, name, parent_id)
 
451
        self.children = {}
 
452
        self.kind = 'directory'
 
453
 
 
454
    def kind_character(self):
 
455
        """See InventoryEntry.kind_character."""
 
456
        return '/'
 
457
 
 
458
    def _put_in_tar(self, item, tree):
 
459
        """See InventoryEntry._put_in_tar."""
 
460
        item.type = tarfile.DIRTYPE
 
461
        fileobj = None
 
462
        item.name += '/'
 
463
        item.size = 0
 
464
        item.mode = 0755
 
465
        return fileobj
 
466
 
 
467
    def _put_on_disk(self, fullpath, tree):
 
468
        """See InventoryEntry._put_on_disk."""
 
469
        os.mkdir(fullpath)
 
470
 
 
471
 
 
472
class InventoryFile(InventoryEntry):
 
473
    """A file in an inventory."""
 
474
 
 
475
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
476
                 'text_id', 'parent_id', 'children', 'executable',
 
477
                 'revision', 'symlink_target', 'reference_revision']
 
478
 
 
479
    def _check(self, checker, tree_revision_id, tree):
 
480
        """See InventoryEntry._check"""
 
481
        key = (self.file_id, self.revision)
 
482
        if key in checker.checked_texts:
 
483
            prev_sha = checker.checked_texts[key]
 
484
            if prev_sha != self.text_sha1:
 
485
                raise BzrCheckError(
 
486
                    'mismatched sha1 on {%s} in {%s} (%s != %s) %r' %
 
487
                    (self.file_id, tree_revision_id, prev_sha, self.text_sha1,
 
488
                     t))
 
489
            else:
 
490
                checker.repeated_text_cnt += 1
 
491
                return
 
492
 
 
493
        checker.checked_text_cnt += 1
 
494
        # We can't check the length, because Weave doesn't store that
 
495
        # information, and the whole point of looking at the weave's
 
496
        # sha1sum is that we don't have to extract the text.
 
497
        if (self.text_sha1 != tree._repository.texts.get_sha1s([key])[key]):
 
498
            raise BzrCheckError('text {%s} version {%s} wrong sha1' % key)
 
499
        checker.checked_texts[key] = self.text_sha1
 
500
 
 
501
    def copy(self):
 
502
        other = InventoryFile(self.file_id, self.name, self.parent_id)
 
503
        other.executable = self.executable
 
504
        other.text_id = self.text_id
 
505
        other.text_sha1 = self.text_sha1
 
506
        other.text_size = self.text_size
 
507
        other.revision = self.revision
 
508
        return other
 
509
 
 
510
    def detect_changes(self, old_entry):
 
511
        """See InventoryEntry.detect_changes."""
 
512
        text_modified = (self.text_sha1 != old_entry.text_sha1)
 
513
        meta_modified = (self.executable != old_entry.executable)
 
514
        return text_modified, meta_modified
 
515
 
 
516
    def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
 
517
             output_to, reverse=False):
 
518
        """See InventoryEntry._diff."""
 
519
        from bzrlib.diff import DiffText
 
520
        from_file_id = self.file_id
 
521
        if to_entry:
 
522
            to_file_id = to_entry.file_id
 
523
        else:
 
524
            to_file_id = None
 
525
        if reverse:
 
526
            to_file_id, from_file_id = from_file_id, to_file_id
 
527
            tree, to_tree = to_tree, tree
 
528
            from_label, to_label = to_label, from_label
 
529
        differ = DiffText(tree, to_tree, output_to, 'utf-8', '', '',
 
530
                          text_diff)
 
531
        return differ.diff_text(from_file_id, to_file_id, from_label, to_label)
 
532
 
 
533
    def has_text(self):
 
534
        """See InventoryEntry.has_text."""
 
535
        return True
 
536
 
 
537
    def __init__(self, file_id, name, parent_id):
 
538
        super(InventoryFile, self).__init__(file_id, name, parent_id)
 
539
        self.kind = 'file'
 
540
 
 
541
    def kind_character(self):
 
542
        """See InventoryEntry.kind_character."""
 
543
        return ''
 
544
 
 
545
    def _put_in_tar(self, item, tree):
 
546
        """See InventoryEntry._put_in_tar."""
 
547
        item.type = tarfile.REGTYPE
 
548
        fileobj = tree.get_file(self.file_id)
 
549
        item.size = self.text_size
 
550
        if tree.is_executable(self.file_id):
 
551
            item.mode = 0755
 
552
        else:
 
553
            item.mode = 0644
 
554
        return fileobj
 
555
 
 
556
    def _put_on_disk(self, fullpath, tree):
 
557
        """See InventoryEntry._put_on_disk."""
 
558
        osutils.pumpfile(tree.get_file(self.file_id), file(fullpath, 'wb'))
 
559
        if tree.is_executable(self.file_id):
 
560
            os.chmod(fullpath, 0755)
 
561
 
 
562
    def _read_tree_state(self, path, work_tree):
 
563
        """See InventoryEntry._read_tree_state."""
 
564
        self.text_sha1 = work_tree.get_file_sha1(self.file_id, path=path)
 
565
        # FIXME: 20050930 probe for the text size when getting sha1
 
566
        # in _read_tree_state
 
567
        self.executable = work_tree.is_executable(self.file_id, path=path)
 
568
 
 
569
    def __repr__(self):
 
570
        return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
 
571
                % (self.__class__.__name__,
 
572
                   self.file_id,
 
573
                   self.name,
 
574
                   self.parent_id,
 
575
                   self.text_sha1,
 
576
                   self.text_size,
 
577
                   self.revision))
 
578
 
 
579
    def _forget_tree_state(self):
 
580
        self.text_sha1 = None
 
581
 
 
582
    def _unchanged(self, previous_ie):
 
583
        """See InventoryEntry._unchanged."""
 
584
        compatible = super(InventoryFile, self)._unchanged(previous_ie)
 
585
        if self.text_sha1 != previous_ie.text_sha1:
 
586
            compatible = False
 
587
        else:
 
588
            # FIXME: 20050930 probe for the text size when getting sha1
 
589
            # in _read_tree_state
 
590
            self.text_size = previous_ie.text_size
 
591
        if self.executable != previous_ie.executable:
 
592
            compatible = False
 
593
        return compatible
 
594
 
 
595
 
 
596
class InventoryLink(InventoryEntry):
 
597
    """A file in an inventory."""
 
598
 
 
599
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
600
                 'text_id', 'parent_id', 'children', 'executable',
 
601
                 'revision', 'symlink_target', 'reference_revision']
 
602
 
 
603
    def _check(self, checker, rev_id, tree):
 
604
        """See InventoryEntry._check"""
 
605
        if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
 
606
            raise BzrCheckError('symlink {%s} has text in revision {%s}'
 
607
                    % (self.file_id, rev_id))
 
608
        if self.symlink_target is None:
 
609
            raise BzrCheckError('symlink {%s} has no target in revision {%s}'
 
610
                    % (self.file_id, rev_id))
 
611
 
 
612
    def copy(self):
 
613
        other = InventoryLink(self.file_id, self.name, self.parent_id)
 
614
        other.symlink_target = self.symlink_target
 
615
        other.revision = self.revision
 
616
        return other
 
617
 
 
618
    def detect_changes(self, old_entry):
 
619
        """See InventoryEntry.detect_changes."""
 
620
        # FIXME: which _modified field should we use ? RBC 20051003
 
621
        text_modified = (self.symlink_target != old_entry.symlink_target)
 
622
        if text_modified:
 
623
            mutter("    symlink target changed")
 
624
        meta_modified = False
 
625
        return text_modified, meta_modified
 
626
 
 
627
    def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
 
628
             output_to, reverse=False):
 
629
        """See InventoryEntry._diff."""
 
630
        from bzrlib.diff import DiffSymlink
 
631
        old_target = self.symlink_target
 
632
        if to_entry is not None:
 
633
            new_target = to_entry.symlink_target
 
634
        else:
 
635
            new_target = None
 
636
        if not reverse:
 
637
            old_tree = tree
 
638
            new_tree = to_tree
 
639
        else:
 
640
            old_tree = to_tree
 
641
            new_tree = tree
 
642
            new_target, old_target = old_target, new_target
 
643
        differ = DiffSymlink(old_tree, new_tree, output_to)
 
644
        return differ.diff_symlink(old_target, new_target)
 
645
 
 
646
    def __init__(self, file_id, name, parent_id):
 
647
        super(InventoryLink, self).__init__(file_id, name, parent_id)
 
648
        self.kind = 'symlink'
 
649
 
 
650
    def kind_character(self):
 
651
        """See InventoryEntry.kind_character."""
 
652
        return ''
 
653
 
 
654
    def _put_in_tar(self, item, tree):
 
655
        """See InventoryEntry._put_in_tar."""
 
656
        item.type = tarfile.SYMTYPE
 
657
        fileobj = None
 
658
        item.size = 0
 
659
        item.mode = 0755
 
660
        item.linkname = self.symlink_target
 
661
        return fileobj
 
662
 
 
663
    def _put_on_disk(self, fullpath, tree):
 
664
        """See InventoryEntry._put_on_disk."""
 
665
        try:
 
666
            os.symlink(self.symlink_target, fullpath)
 
667
        except OSError,e:
 
668
            raise BzrError("Failed to create symlink %r -> %r, error: %s" % (fullpath, self.symlink_target, e))
 
669
 
 
670
    def _read_tree_state(self, path, work_tree):
 
671
        """See InventoryEntry._read_tree_state."""
 
672
        self.symlink_target = work_tree.get_symlink_target(self.file_id)
 
673
 
 
674
    def _forget_tree_state(self):
 
675
        self.symlink_target = None
 
676
 
 
677
    def _unchanged(self, previous_ie):
 
678
        """See InventoryEntry._unchanged."""
 
679
        compatible = super(InventoryLink, self)._unchanged(previous_ie)
 
680
        if self.symlink_target != previous_ie.symlink_target:
 
681
            compatible = False
 
682
        return compatible
 
683
 
 
684
 
 
685
class TreeReference(InventoryEntry):
 
686
 
 
687
    kind = 'tree-reference'
 
688
 
 
689
    def __init__(self, file_id, name, parent_id, revision=None,
 
690
                 reference_revision=None):
 
691
        InventoryEntry.__init__(self, file_id, name, parent_id)
 
692
        self.revision = revision
 
693
        self.reference_revision = reference_revision
 
694
 
 
695
    def copy(self):
 
696
        return TreeReference(self.file_id, self.name, self.parent_id,
 
697
                             self.revision, self.reference_revision)
 
698
 
 
699
    def _read_tree_state(self, path, work_tree):
 
700
        """Populate fields in the inventory entry from the given tree.
 
701
        """
 
702
        self.reference_revision = work_tree.get_reference_revision(
 
703
            self.file_id, path)
 
704
 
 
705
    def _forget_tree_state(self):
 
706
        self.reference_revision = None
 
707
 
 
708
    def _unchanged(self, previous_ie):
 
709
        """See InventoryEntry._unchanged."""
 
710
        compatible = super(TreeReference, self)._unchanged(previous_ie)
 
711
        if self.reference_revision != previous_ie.reference_revision:
 
712
            compatible = False
 
713
        return compatible
 
714
 
 
715
 
 
716
class CommonInventory(object):
 
717
    """Basic inventory logic, defined in terms of primitives like has_id."""
 
718
 
 
719
    def __contains__(self, file_id):
 
720
        """True if this entry contains a file with given id.
 
721
 
 
722
        >>> inv = Inventory()
 
723
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
 
724
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
 
725
        >>> '123' in inv
 
726
        True
 
727
        >>> '456' in inv
 
728
        False
 
729
 
 
730
        Note that this method along with __iter__ are not encouraged for use as
 
731
        they are less clear than specific query methods - they may be rmeoved
 
732
        in the future.
 
733
        """
 
734
        return self.has_id(file_id)
 
735
 
 
736
    def id2path(self, file_id):
 
737
        """Return as a string the path to file_id.
 
738
 
 
739
        >>> i = Inventory()
 
740
        >>> e = i.add(InventoryDirectory('src-id', 'src', ROOT_ID))
 
741
        >>> e = i.add(InventoryFile('foo-id', 'foo.c', parent_id='src-id'))
 
742
        >>> print i.id2path('foo-id')
 
743
        src/foo.c
 
744
        """
 
745
        # get all names, skipping root
 
746
        return '/'.join(reversed(
 
747
            [parent.name for parent in
 
748
             self._iter_file_id_parents(file_id)][:-1]))
 
749
 
 
750
    def iter_entries(self, from_dir=None, recursive=True):
 
751
        """Return (path, entry) pairs, in order by name.
 
752
        
 
753
        :param from_dir: if None, start from the root,
 
754
          otherwise start from this directory (either file-id or entry)
 
755
        :param recursive: recurse into directories or not
 
756
        """
 
757
        if from_dir is None:
 
758
            if self.root is None:
 
759
                return
 
760
            from_dir = self.root
 
761
            yield '', self.root
 
762
        elif isinstance(from_dir, basestring):
 
763
            from_dir = self[from_dir]
 
764
 
 
765
        # unrolling the recursive called changed the time from
 
766
        # 440ms/663ms (inline/total) to 116ms/116ms
 
767
        children = from_dir.children.items()
 
768
        children.sort()
 
769
        if not recursive:
 
770
            for name, ie in children:
 
771
                yield name, ie
 
772
            return
 
773
        children = collections.deque(children)
 
774
        stack = [(u'', children)]
 
775
        while stack:
 
776
            from_dir_relpath, children = stack[-1]
 
777
 
 
778
            while children:
 
779
                name, ie = children.popleft()
 
780
 
 
781
                # we know that from_dir_relpath never ends in a slash
 
782
                # and 'f' doesn't begin with one, we can do a string op, rather
 
783
                # than the checks of pathjoin(), though this means that all paths
 
784
                # start with a slash
 
785
                path = from_dir_relpath + '/' + name
 
786
 
 
787
                yield path[1:], ie
 
788
 
 
789
                if ie.kind != 'directory':
 
790
                    continue
 
791
 
 
792
                # But do this child first
 
793
                new_children = ie.children.items()
 
794
                new_children.sort()
 
795
                new_children = collections.deque(new_children)
 
796
                stack.append((path, new_children))
 
797
                # Break out of inner loop, so that we start outer loop with child
 
798
                break
 
799
            else:
 
800
                # if we finished all children, pop it off the stack
 
801
                stack.pop()
 
802
 
 
803
    def iter_entries_by_dir(self, from_dir=None, specific_file_ids=None,
 
804
        yield_parents=False):
 
805
        """Iterate over the entries in a directory first order.
 
806
 
 
807
        This returns all entries for a directory before returning
 
808
        the entries for children of a directory. This is not
 
809
        lexicographically sorted order, and is a hybrid between
 
810
        depth-first and breadth-first.
 
811
 
 
812
        :param yield_parents: If True, yield the parents from the root leading
 
813
            down to specific_file_ids that have been requested. This has no
 
814
            impact if specific_file_ids is None.
 
815
        :return: This yields (path, entry) pairs
 
816
        """
 
817
        if specific_file_ids and not isinstance(specific_file_ids, set):
 
818
            specific_file_ids = set(specific_file_ids)
 
819
        # TODO? Perhaps this should return the from_dir so that the root is
 
820
        # yielded? or maybe an option?
 
821
        if from_dir is None:
 
822
            if self.root is None:
 
823
                return
 
824
            # Optimize a common case
 
825
            if (not yield_parents and specific_file_ids is not None and
 
826
                len(specific_file_ids) == 1):
 
827
                file_id = list(specific_file_ids)[0]
 
828
                if file_id in self:
 
829
                    yield self.id2path(file_id), self[file_id]
 
830
                return
 
831
            from_dir = self.root
 
832
            if (specific_file_ids is None or yield_parents or
 
833
                self.root.file_id in specific_file_ids):
 
834
                yield u'', self.root
 
835
        elif isinstance(from_dir, basestring):
 
836
            from_dir = self[from_dir]
 
837
 
 
838
        if specific_file_ids is not None:
 
839
            # TODO: jam 20070302 This could really be done as a loop rather
 
840
            #       than a bunch of recursive calls.
 
841
            parents = set()
 
842
            byid = self
 
843
            def add_ancestors(file_id):
 
844
                if file_id not in byid:
 
845
                    return
 
846
                parent_id = byid[file_id].parent_id
 
847
                if parent_id is None:
 
848
                    return
 
849
                if parent_id not in parents:
 
850
                    parents.add(parent_id)
 
851
                    add_ancestors(parent_id)
 
852
            for file_id in specific_file_ids:
 
853
                add_ancestors(file_id)
 
854
        else:
 
855
            parents = None
 
856
 
 
857
        stack = [(u'', from_dir)]
 
858
        while stack:
 
859
            cur_relpath, cur_dir = stack.pop()
 
860
 
 
861
            child_dirs = []
 
862
            for child_name, child_ie in sorted(cur_dir.children.iteritems()):
 
863
 
 
864
                child_relpath = cur_relpath + child_name
 
865
 
 
866
                if (specific_file_ids is None or
 
867
                    child_ie.file_id in specific_file_ids or
 
868
                    (yield_parents and child_ie.file_id in parents)):
 
869
                    yield child_relpath, child_ie
 
870
 
 
871
                if child_ie.kind == 'directory':
 
872
                    if parents is None or child_ie.file_id in parents:
 
873
                        child_dirs.append((child_relpath+'/', child_ie))
 
874
            stack.extend(reversed(child_dirs))
 
875
 
 
876
    def _make_delta(self, old):
 
877
        """Make an inventory delta from two inventories."""
 
878
        old_ids = set(old)
 
879
        new_ids = set(self)
 
880
        adds = new_ids - old_ids
 
881
        deletes = old_ids - new_ids
 
882
        common = old_ids.intersection(new_ids)
 
883
        delta = []
 
884
        for file_id in deletes:
 
885
            delta.append((old.id2path(file_id), None, file_id, None))
 
886
        for file_id in adds:
 
887
            delta.append((None, self.id2path(file_id), file_id, self[file_id]))
 
888
        for file_id in common:
 
889
            if old[file_id] != self[file_id]:
 
890
                delta.append((old.id2path(file_id), self.id2path(file_id),
 
891
                    file_id, self[file_id]))
 
892
        return delta
 
893
 
 
894
    def _get_mutable_inventory(self):
 
895
        """Returns a mutable copy of the object.
 
896
 
 
897
        Some inventories are immutable, yet working trees, for example, needs
 
898
        to mutate exisiting inventories instead of creating a new one.
 
899
        """
 
900
        raise NotImplementedError(self._get_mutable_inventory)
 
901
 
 
902
    def make_entry(self, kind, name, parent_id, file_id=None):
 
903
        """Simple thunk to bzrlib.inventory.make_entry."""
 
904
        return make_entry(kind, name, parent_id, file_id)
 
905
 
 
906
    def entries(self):
 
907
        """Return list of (path, ie) for all entries except the root.
 
908
 
 
909
        This may be faster than iter_entries.
 
910
        """
 
911
        accum = []
 
912
        def descend(dir_ie, dir_path):
 
913
            kids = dir_ie.children.items()
 
914
            kids.sort()
 
915
            for name, ie in kids:
 
916
                child_path = osutils.pathjoin(dir_path, name)
 
917
                accum.append((child_path, ie))
 
918
                if ie.kind == 'directory':
 
919
                    descend(ie, child_path)
 
920
 
 
921
        descend(self.root, u'')
 
922
        return accum
 
923
 
 
924
    def directories(self):
 
925
        """Return (path, entry) pairs for all directories, including the root.
 
926
        """
 
927
        accum = []
 
928
        def descend(parent_ie, parent_path):
 
929
            accum.append((parent_path, parent_ie))
 
930
 
 
931
            kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
 
932
            kids.sort()
 
933
 
 
934
            for name, child_ie in kids:
 
935
                child_path = osutils.pathjoin(parent_path, name)
 
936
                descend(child_ie, child_path)
 
937
        descend(self.root, u'')
 
938
        return accum
 
939
 
 
940
    def path2id(self, name):
 
941
        """Walk down through directories to return entry of last component.
 
942
 
 
943
        names may be either a list of path components, or a single
 
944
        string, in which case it is automatically split.
 
945
 
 
946
        This returns the entry of the last component in the path,
 
947
        which may be either a file or a directory.
 
948
 
 
949
        Returns None IFF the path is not found.
 
950
        """
 
951
        if isinstance(name, basestring):
 
952
            name = osutils.splitpath(name)
 
953
 
 
954
        # mutter("lookup path %r" % name)
 
955
 
 
956
        try:
 
957
            parent = self.root
 
958
        except errors.NoSuchId:
 
959
            # root doesn't exist yet so nothing else can
 
960
            return None
 
961
        if parent is None:
 
962
            return None
 
963
        for f in name:
 
964
            try:
 
965
                children = getattr(parent, 'children', None)
 
966
                if children is None:
 
967
                    return None
 
968
                cie = children[f]
 
969
                parent = cie
 
970
            except KeyError:
 
971
                # or raise an error?
 
972
                return None
 
973
 
 
974
        return parent.file_id
 
975
 
 
976
    def filter(self, specific_fileids):
 
977
        """Get an inventory view filtered against a set of file-ids.
 
978
 
 
979
        Children of directories and parents are included.
 
980
 
 
981
        The result may or may not reference the underlying inventory
 
982
        so it should be treated as immutable.
 
983
        """
 
984
        interesting_parents = set()
 
985
        for fileid in specific_fileids:
 
986
            try:
 
987
                interesting_parents.update(self.get_idpath(fileid))
 
988
            except errors.NoSuchId:
 
989
                # This fileid is not in the inventory - that's ok
 
990
                pass
 
991
        entries = self.iter_entries()
 
992
        if self.root is None:
 
993
            return Inventory(root_id=None)
 
994
        other = Inventory(entries.next()[1].file_id)
 
995
        other.root.revision = self.root.revision
 
996
        other.revision_id = self.revision_id
 
997
        directories_to_expand = set()
 
998
        for path, entry in entries:
 
999
            file_id = entry.file_id
 
1000
            if (file_id in specific_fileids
 
1001
                or entry.parent_id in directories_to_expand):
 
1002
                if entry.kind == 'directory':
 
1003
                    directories_to_expand.add(file_id)
 
1004
            elif file_id not in interesting_parents:
 
1005
                continue
 
1006
            other.add(entry.copy())
 
1007
        return other
 
1008
 
 
1009
    def get_idpath(self, file_id):
 
1010
        """Return a list of file_ids for the path to an entry.
 
1011
 
 
1012
        The list contains one element for each directory followed by
 
1013
        the id of the file itself.  So the length of the returned list
 
1014
        is equal to the depth of the file in the tree, counting the
 
1015
        root directory as depth 1.
 
1016
        """
 
1017
        p = []
 
1018
        for parent in self._iter_file_id_parents(file_id):
 
1019
            p.insert(0, parent.file_id)
 
1020
        return p
 
1021
 
 
1022
 
 
1023
class Inventory(CommonInventory):
 
1024
    """Inventory of versioned files in a tree.
 
1025
 
 
1026
    This describes which file_id is present at each point in the tree,
 
1027
    and possibly the SHA-1 or other information about the file.
 
1028
    Entries can be looked up either by path or by file_id.
 
1029
 
 
1030
    The inventory represents a typical unix file tree, with
 
1031
    directories containing files and subdirectories.  We never store
 
1032
    the full path to a file, because renaming a directory implicitly
 
1033
    moves all of its contents.  This class internally maintains a
 
1034
    lookup tree that allows the children under a directory to be
 
1035
    returned quickly.
 
1036
 
 
1037
    InventoryEntry objects must not be modified after they are
 
1038
    inserted, other than through the Inventory API.
 
1039
 
 
1040
    >>> inv = Inventory()
 
1041
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
 
1042
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
 
1043
    >>> inv['123-123'].name
 
1044
    'hello.c'
 
1045
 
 
1046
    May be treated as an iterator or set to look up file ids:
 
1047
 
 
1048
    >>> bool(inv.path2id('hello.c'))
 
1049
    True
 
1050
    >>> '123-123' in inv
 
1051
    True
 
1052
 
 
1053
    May also look up by name:
 
1054
 
 
1055
    >>> [x[0] for x in inv.iter_entries()]
 
1056
    ['', u'hello.c']
 
1057
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
 
1058
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
 
1059
    Traceback (most recent call last):
 
1060
    BzrError: parent_id {TREE_ROOT} not in inventory
 
1061
    >>> inv.add(InventoryFile('123-123', 'hello.c', 'TREE_ROOT-12345678-12345678'))
 
1062
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT-12345678-12345678', sha1=None, len=None, revision=None)
 
1063
    """
 
1064
    def __init__(self, root_id=ROOT_ID, revision_id=None):
 
1065
        """Create or read an inventory.
 
1066
 
 
1067
        If a working directory is specified, the inventory is read
 
1068
        from there.  If the file is specified, read from that. If not,
 
1069
        the inventory is created empty.
 
1070
 
 
1071
        The inventory is created with a default root directory, with
 
1072
        an id of None.
 
1073
        """
 
1074
        if root_id is not None:
 
1075
            self._set_root(InventoryDirectory(root_id, u'', None))
 
1076
        else:
 
1077
            self.root = None
 
1078
            self._byid = {}
 
1079
        self.revision_id = revision_id
 
1080
 
 
1081
    def __repr__(self):
 
1082
        # More than one page of ouput is not useful anymore to debug
 
1083
        max_len = 2048
 
1084
        closing = '...}'
 
1085
        contents = repr(self._byid)
 
1086
        if len(contents) > max_len:
 
1087
            contents = contents[:(max_len-len(closing))] + closing
 
1088
        return "<Inventory object at %x, contents=%r>" % (id(self), contents)
 
1089
 
 
1090
    def apply_delta(self, delta):
 
1091
        """Apply a delta to this inventory.
 
1092
 
 
1093
        :param delta: A list of changes to apply. After all the changes are
 
1094
            applied the final inventory must be internally consistent, but it
 
1095
            is ok to supply changes which, if only half-applied would have an
 
1096
            invalid result - such as supplying two changes which rename two
 
1097
            files, 'A' and 'B' with each other : [('A', 'B', 'A-id', a_entry),
 
1098
            ('B', 'A', 'B-id', b_entry)].
 
1099
 
 
1100
            Each change is a tuple, of the form (old_path, new_path, file_id,
 
1101
            new_entry).
 
1102
 
 
1103
            When new_path is None, the change indicates the removal of an entry
 
1104
            from the inventory and new_entry will be ignored (using None is
 
1105
            appropriate). If new_path is not None, then new_entry must be an
 
1106
            InventoryEntry instance, which will be incorporated into the
 
1107
            inventory (and replace any existing entry with the same file id).
 
1108
 
 
1109
            When old_path is None, the change indicates the addition of
 
1110
            a new entry to the inventory.
 
1111
 
 
1112
            When neither new_path nor old_path are None, the change is a
 
1113
            modification to an entry, such as a rename, reparent, kind change
 
1114
            etc.
 
1115
 
 
1116
            The children attribute of new_entry is ignored. This is because
 
1117
            this method preserves children automatically across alterations to
 
1118
            the parent of the children, and cases where the parent id of a
 
1119
            child is changing require the child to be passed in as a separate
 
1120
            change regardless. E.g. in the recursive deletion of a directory -
 
1121
            the directory's children must be included in the delta, or the
 
1122
            final inventory will be invalid.
 
1123
 
 
1124
            Note that a file_id must only appear once within a given delta.
 
1125
            An AssertionError is raised otherwise.
 
1126
        """
 
1127
        # Check that the delta is legal. It would be nice if this could be
 
1128
        # done within the loops below but it's safer to validate the delta
 
1129
        # before starting to mutate the inventory.
 
1130
        unique_file_ids = set([f for _, _, f, _ in delta])
 
1131
        if len(unique_file_ids) != len(delta):
 
1132
            raise AssertionError("a file-id appears multiple times in %r"
 
1133
                    % (delta,))
 
1134
        del unique_file_ids
 
1135
 
 
1136
        children = {}
 
1137
        # Remove all affected items which were in the original inventory,
 
1138
        # starting with the longest paths, thus ensuring parents are examined
 
1139
        # after their children, which means that everything we examine has no
 
1140
        # modified children remaining by the time we examine it.
 
1141
        for old_path, file_id in sorted(((op, f) for op, np, f, e in delta
 
1142
                                        if op is not None), reverse=True):
 
1143
            if file_id not in self:
 
1144
                # adds come later
 
1145
                continue
 
1146
            # Preserve unaltered children of file_id for later reinsertion.
 
1147
            file_id_children = getattr(self[file_id], 'children', {})
 
1148
            if len(file_id_children):
 
1149
                children[file_id] = file_id_children
 
1150
            # Remove file_id and the unaltered children. If file_id is not
 
1151
            # being deleted it will be reinserted back later.
 
1152
            self.remove_recursive_id(file_id)
 
1153
        # Insert all affected which should be in the new inventory, reattaching
 
1154
        # their children if they had any. This is done from shortest path to
 
1155
        # longest, ensuring that items which were modified and whose parents in
 
1156
        # the resulting inventory were also modified, are inserted after their
 
1157
        # parents.
 
1158
        for new_path, new_entry in sorted((np, e) for op, np, f, e in
 
1159
                                          delta if np is not None):
 
1160
            if new_entry.kind == 'directory':
 
1161
                # Pop the child which to allow detection of children whose
 
1162
                # parents were deleted and which were not reattached to a new
 
1163
                # parent.
 
1164
                replacement = InventoryDirectory(new_entry.file_id,
 
1165
                    new_entry.name, new_entry.parent_id)
 
1166
                replacement.revision = new_entry.revision
 
1167
                replacement.children = children.pop(replacement.file_id, {})
 
1168
                new_entry = replacement
 
1169
            self.add(new_entry)
 
1170
        if len(children):
 
1171
            # Get the parent id that was deleted
 
1172
            parent_id, children = children.popitem()
 
1173
            raise errors.InconsistentDelta("<deleted>", parent_id,
 
1174
                "The file id was deleted but its children were not deleted.")
 
1175
 
 
1176
    def _set_root(self, ie):
 
1177
        self.root = ie
 
1178
        self._byid = {self.root.file_id: self.root}
 
1179
 
 
1180
    def copy(self):
 
1181
        # TODO: jam 20051218 Should copy also copy the revision_id?
 
1182
        entries = self.iter_entries()
 
1183
        if self.root is None:
 
1184
            return Inventory(root_id=None)
 
1185
        other = Inventory(entries.next()[1].file_id)
 
1186
        other.root.revision = self.root.revision
 
1187
        # copy recursively so we know directories will be added before
 
1188
        # their children.  There are more efficient ways than this...
 
1189
        for path, entry in entries:
 
1190
            other.add(entry.copy())
 
1191
        return other
 
1192
 
 
1193
    def _get_mutable_inventory(self):
 
1194
        """See CommonInventory._get_mutable_inventory."""
 
1195
        return deepcopy(self)
 
1196
 
 
1197
    def __iter__(self):
 
1198
        """Iterate over all file-ids."""
 
1199
        return iter(self._byid)
 
1200
 
 
1201
    def iter_just_entries(self):
 
1202
        """Iterate over all entries.
 
1203
        
 
1204
        Unlike iter_entries(), just the entries are returned (not (path, ie))
 
1205
        and the order of entries is undefined.
 
1206
 
 
1207
        XXX: We may not want to merge this into bzr.dev.
 
1208
        """
 
1209
        if self.root is None:
 
1210
            return
 
1211
        for _, ie in self._byid.iteritems():
 
1212
            yield ie
 
1213
 
 
1214
    def __len__(self):
 
1215
        """Returns number of entries."""
 
1216
        return len(self._byid)
 
1217
 
 
1218
    def __getitem__(self, file_id):
 
1219
        """Return the entry for given file_id.
 
1220
 
 
1221
        >>> inv = Inventory()
 
1222
        >>> inv.add(InventoryFile('123123', 'hello.c', ROOT_ID))
 
1223
        InventoryFile('123123', 'hello.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
 
1224
        >>> inv['123123'].name
 
1225
        'hello.c'
 
1226
        """
 
1227
        try:
 
1228
            return self._byid[file_id]
 
1229
        except KeyError:
 
1230
            # really we're passing an inventory, not a tree...
 
1231
            raise errors.NoSuchId(self, file_id)
 
1232
 
 
1233
    def get_file_kind(self, file_id):
 
1234
        return self._byid[file_id].kind
 
1235
 
 
1236
    def get_child(self, parent_id, filename):
 
1237
        return self[parent_id].children.get(filename)
 
1238
 
 
1239
    def _add_child(self, entry):
 
1240
        """Add an entry to the inventory, without adding it to its parent"""
 
1241
        if entry.file_id in self._byid:
 
1242
            raise BzrError("inventory already contains entry with id {%s}" %
 
1243
                           entry.file_id)
 
1244
        self._byid[entry.file_id] = entry
 
1245
        for child in getattr(entry, 'children', {}).itervalues():
 
1246
            self._add_child(child)
 
1247
        return entry
 
1248
 
 
1249
    def add(self, entry):
 
1250
        """Add entry to inventory.
 
1251
 
 
1252
        To add  a file to a branch ready to be committed, use Branch.add,
 
1253
        which calls this.
 
1254
 
 
1255
        Returns the new entry object.
 
1256
        """
 
1257
        if entry.file_id in self._byid:
 
1258
            raise errors.DuplicateFileId(entry.file_id,
 
1259
                                         self._byid[entry.file_id])
 
1260
 
 
1261
        if entry.parent_id is None:
 
1262
            self.root = entry
 
1263
        else:
 
1264
            try:
 
1265
                parent = self._byid[entry.parent_id]
 
1266
            except KeyError:
 
1267
                raise BzrError("parent_id {%s} not in inventory" %
 
1268
                               entry.parent_id)
 
1269
 
 
1270
            if entry.name in parent.children:
 
1271
                raise BzrError("%s is already versioned" %
 
1272
                        osutils.pathjoin(self.id2path(parent.file_id),
 
1273
                        entry.name).encode('utf-8'))
 
1274
            parent.children[entry.name] = entry
 
1275
        return self._add_child(entry)
 
1276
 
 
1277
    def add_path(self, relpath, kind, file_id=None, parent_id=None):
 
1278
        """Add entry from a path.
 
1279
 
 
1280
        The immediate parent must already be versioned.
 
1281
 
 
1282
        Returns the new entry object."""
 
1283
 
 
1284
        parts = osutils.splitpath(relpath)
 
1285
 
 
1286
        if len(parts) == 0:
 
1287
            if file_id is None:
 
1288
                file_id = generate_ids.gen_root_id()
 
1289
            self.root = InventoryDirectory(file_id, '', None)
 
1290
            self._byid = {self.root.file_id: self.root}
 
1291
            return self.root
 
1292
        else:
 
1293
            parent_path = parts[:-1]
 
1294
            parent_id = self.path2id(parent_path)
 
1295
            if parent_id is None:
 
1296
                raise errors.NotVersionedError(path=parent_path)
 
1297
        ie = make_entry(kind, parts[-1], parent_id, file_id)
 
1298
        return self.add(ie)
 
1299
 
 
1300
    def __delitem__(self, file_id):
 
1301
        """Remove entry by id.
 
1302
 
 
1303
        >>> inv = Inventory()
 
1304
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
 
1305
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
 
1306
        >>> '123' in inv
 
1307
        True
 
1308
        >>> del inv['123']
 
1309
        >>> '123' in inv
 
1310
        False
 
1311
        """
 
1312
        ie = self[file_id]
 
1313
        del self._byid[file_id]
 
1314
        if ie.parent_id is not None:
 
1315
            del self[ie.parent_id].children[ie.name]
 
1316
 
 
1317
    def __eq__(self, other):
 
1318
        """Compare two sets by comparing their contents.
 
1319
 
 
1320
        >>> i1 = Inventory()
 
1321
        >>> i2 = Inventory()
 
1322
        >>> i1 == i2
 
1323
        True
 
1324
        >>> i1.add(InventoryFile('123', 'foo', ROOT_ID))
 
1325
        InventoryFile('123', 'foo', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
 
1326
        >>> i1 == i2
 
1327
        False
 
1328
        >>> i2.add(InventoryFile('123', 'foo', ROOT_ID))
 
1329
        InventoryFile('123', 'foo', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
 
1330
        >>> i1 == i2
 
1331
        True
 
1332
        """
 
1333
        if not isinstance(other, Inventory):
 
1334
            return NotImplemented
 
1335
 
 
1336
        return self._byid == other._byid
 
1337
 
 
1338
    def __ne__(self, other):
 
1339
        return not self.__eq__(other)
 
1340
 
 
1341
    def __hash__(self):
 
1342
        raise ValueError('not hashable')
 
1343
 
 
1344
    def _iter_file_id_parents(self, file_id):
 
1345
        """Yield the parents of file_id up to the root."""
 
1346
        while file_id is not None:
 
1347
            try:
 
1348
                ie = self._byid[file_id]
 
1349
            except KeyError:
 
1350
                raise errors.NoSuchId(tree=None, file_id=file_id)
 
1351
            yield ie
 
1352
            file_id = ie.parent_id
 
1353
 
 
1354
    def has_filename(self, filename):
 
1355
        return bool(self.path2id(filename))
 
1356
 
 
1357
    def has_id(self, file_id):
 
1358
        return (file_id in self._byid)
 
1359
 
 
1360
    def _make_delta(self, old):
 
1361
        """Make an inventory delta from two inventories."""
 
1362
        old_getter = getattr(old, '_byid', old)
 
1363
        new_getter = self._byid
 
1364
        old_ids = set(old_getter)
 
1365
        new_ids = set(new_getter)
 
1366
        adds = new_ids - old_ids
 
1367
        deletes = old_ids - new_ids
 
1368
        if not adds and not deletes:
 
1369
            common = new_ids
 
1370
        else:
 
1371
            common = old_ids.intersection(new_ids)
 
1372
        delta = []
 
1373
        for file_id in deletes:
 
1374
            delta.append((old.id2path(file_id), None, file_id, None))
 
1375
        for file_id in adds:
 
1376
            delta.append((None, self.id2path(file_id), file_id, self[file_id]))
 
1377
        for file_id in common:
 
1378
            new_ie = new_getter[file_id]
 
1379
            old_ie = old_getter[file_id]
 
1380
            # If xml_serializer returns the cached InventoryEntries (rather
 
1381
            # than always doing .copy()), inlining the 'is' check saves 2.7M
 
1382
            # calls to __eq__.  Under lsprof this saves 20s => 6s.
 
1383
            # It is a minor improvement without lsprof.
 
1384
            if old_ie is new_ie or old_ie == new_ie:
 
1385
                continue
 
1386
            else:
 
1387
                delta.append((old.id2path(file_id), self.id2path(file_id),
 
1388
                              file_id, new_ie))
 
1389
        return delta
 
1390
 
 
1391
    def remove_recursive_id(self, file_id):
 
1392
        """Remove file_id, and children, from the inventory.
 
1393
 
 
1394
        :param file_id: A file_id to remove.
 
1395
        """
 
1396
        to_find_delete = [self._byid[file_id]]
 
1397
        to_delete = []
 
1398
        while to_find_delete:
 
1399
            ie = to_find_delete.pop()
 
1400
            to_delete.append(ie.file_id)
 
1401
            if ie.kind == 'directory':
 
1402
                to_find_delete.extend(ie.children.values())
 
1403
        for file_id in reversed(to_delete):
 
1404
            ie = self[file_id]
 
1405
            del self._byid[file_id]
 
1406
        if ie.parent_id is not None:
 
1407
            del self[ie.parent_id].children[ie.name]
 
1408
        else:
 
1409
            self.root = None
 
1410
 
 
1411
    def rename(self, file_id, new_parent_id, new_name):
 
1412
        """Move a file within the inventory.
 
1413
 
 
1414
        This can change either the name, or the parent, or both.
 
1415
 
 
1416
        This does not move the working file.
 
1417
        """
 
1418
        new_name = ensure_normalized_name(new_name)
 
1419
        if not is_valid_name(new_name):
 
1420
            raise BzrError("not an acceptable filename: %r" % new_name)
 
1421
 
 
1422
        new_parent = self._byid[new_parent_id]
 
1423
        if new_name in new_parent.children:
 
1424
            raise BzrError("%r already exists in %r" % (new_name, self.id2path(new_parent_id)))
 
1425
 
 
1426
        new_parent_idpath = self.get_idpath(new_parent_id)
 
1427
        if file_id in new_parent_idpath:
 
1428
            raise BzrError("cannot move directory %r into a subdirectory of itself, %r"
 
1429
                    % (self.id2path(file_id), self.id2path(new_parent_id)))
 
1430
 
 
1431
        file_ie = self._byid[file_id]
 
1432
        old_parent = self._byid[file_ie.parent_id]
 
1433
 
 
1434
        # TODO: Don't leave things messed up if this fails
 
1435
 
 
1436
        del old_parent.children[file_ie.name]
 
1437
        new_parent.children[new_name] = file_ie
 
1438
 
 
1439
        file_ie.name = new_name
 
1440
        file_ie.parent_id = new_parent_id
 
1441
 
 
1442
    def is_root(self, file_id):
 
1443
        return self.root is not None and file_id == self.root.file_id
 
1444
 
 
1445
 
 
1446
class CHKInventory(CommonInventory):
 
1447
    """An inventory persisted in a CHK store.
 
1448
 
 
1449
    By design, a CHKInventory is immutable so many of the methods
 
1450
    supported by Inventory - add, rename, apply_delta, etc - are *not*
 
1451
    supported. To create a new CHKInventory, use create_by_apply_delta()
 
1452
    or from_inventory(), say.
 
1453
 
 
1454
    Internally, a CHKInventory has one or two CHKMaps:
 
1455
 
 
1456
    * id_to_entry - a map from (file_id,) => InventoryEntry as bytes
 
1457
    * parent_id_basename_to_file_id - a map from (parent_id, basename_utf8)
 
1458
        => file_id as bytes
 
1459
 
 
1460
    The second map is optional and not present in early CHkRepository's.
 
1461
 
 
1462
    No caching is performed: every method call or item access will perform
 
1463
    requests to the storage layer. As such, keep references to objects you
 
1464
    want to reuse.
 
1465
    """
 
1466
 
 
1467
    def __init__(self, search_key_name):
 
1468
        CommonInventory.__init__(self)
 
1469
        self._fileid_to_entry_cache = {}
 
1470
        self._path_to_fileid_cache = {}
 
1471
        self._search_key_name = search_key_name
 
1472
 
 
1473
    def __eq__(self, other):
 
1474
        """Compare two sets by comparing their contents."""
 
1475
        if not isinstance(other, CHKInventory):
 
1476
            return NotImplemented
 
1477
 
 
1478
        this_key = self.id_to_entry.key()
 
1479
        other_key = other.id_to_entry.key()
 
1480
        this_pid_key = self.parent_id_basename_to_file_id.key()
 
1481
        other_pid_key = other.parent_id_basename_to_file_id.key()
 
1482
        if None in (this_key, this_pid_key, other_key, other_pid_key):
 
1483
            return False
 
1484
        return this_key == other_key and this_pid_key == other_pid_key
 
1485
 
 
1486
    def _entry_to_bytes(self, entry):
 
1487
        """Serialise entry as a single bytestring.
 
1488
 
 
1489
        :param Entry: An inventory entry.
 
1490
        :return: A bytestring for the entry.
 
1491
 
 
1492
        The BNF:
 
1493
        ENTRY ::= FILE | DIR | SYMLINK | TREE
 
1494
        FILE ::= "file: " COMMON SEP SHA SEP SIZE SEP EXECUTABLE
 
1495
        DIR ::= "dir: " COMMON
 
1496
        SYMLINK ::= "symlink: " COMMON SEP TARGET_UTF8
 
1497
        TREE ::= "tree: " COMMON REFERENCE_REVISION
 
1498
        COMMON ::= FILE_ID SEP PARENT_ID SEP NAME_UTF8 SEP REVISION
 
1499
        SEP ::= "\n"
 
1500
        """
 
1501
        if entry.parent_id is not None:
 
1502
            parent_str = entry.parent_id
 
1503
        else:
 
1504
            parent_str = ''
 
1505
        name_str = entry.name.encode("utf8")
 
1506
        if entry.kind == 'file':
 
1507
            if entry.executable:
 
1508
                exec_str = "Y"
 
1509
            else:
 
1510
                exec_str = "N"
 
1511
            return "file: %s\n%s\n%s\n%s\n%s\n%d\n%s" % (
 
1512
                entry.file_id, parent_str, name_str, entry.revision,
 
1513
                entry.text_sha1, entry.text_size, exec_str)
 
1514
        elif entry.kind == 'directory':
 
1515
            return "dir: %s\n%s\n%s\n%s" % (
 
1516
                entry.file_id, parent_str, name_str, entry.revision)
 
1517
        elif entry.kind == 'symlink':
 
1518
            return "symlink: %s\n%s\n%s\n%s\n%s" % (
 
1519
                entry.file_id, parent_str, name_str, entry.revision,
 
1520
                entry.symlink_target.encode("utf8"))
 
1521
        elif entry.kind == 'tree-reference':
 
1522
            return "tree: %s\n%s\n%s\n%s\n%s" % (
 
1523
                entry.file_id, parent_str, name_str, entry.revision,
 
1524
                entry.reference_revision)
 
1525
        else:
 
1526
            raise ValueError("unknown kind %r" % entry.kind)
 
1527
 
 
1528
    @staticmethod
 
1529
    def _bytes_to_utf8name_key(bytes):
 
1530
        """Get the file_id, revision_id key out of bytes."""
 
1531
        # We don't normally care about name, except for times when we want
 
1532
        # to filter out empty names because of non rich-root...
 
1533
        sections = bytes.split('\n')
 
1534
        kind, file_id = sections[0].split(': ')
 
1535
        return (sections[2], file_id, sections[3])
 
1536
 
 
1537
    def _bytes_to_entry(self, bytes):
 
1538
        """Deserialise a serialised entry."""
 
1539
        sections = bytes.split('\n')
 
1540
        if sections[0].startswith("file: "):
 
1541
            result = InventoryFile(sections[0][6:],
 
1542
                sections[2].decode('utf8'),
 
1543
                sections[1])
 
1544
            result.text_sha1 = sections[4]
 
1545
            result.text_size = int(sections[5])
 
1546
            result.executable = sections[6] == "Y"
 
1547
        elif sections[0].startswith("dir: "):
 
1548
            result = CHKInventoryDirectory(sections[0][5:],
 
1549
                sections[2].decode('utf8'),
 
1550
                sections[1], self)
 
1551
        elif sections[0].startswith("symlink: "):
 
1552
            result = InventoryLink(sections[0][9:],
 
1553
                sections[2].decode('utf8'),
 
1554
                sections[1])
 
1555
            result.symlink_target = sections[4].decode('utf8')
 
1556
        elif sections[0].startswith("tree: "):
 
1557
            result = TreeReference(sections[0][6:],
 
1558
                sections[2].decode('utf8'),
 
1559
                sections[1])
 
1560
            result.reference_revision = sections[4]
 
1561
        else:
 
1562
            raise ValueError("Not a serialised entry %r" % bytes)
 
1563
        result.revision = sections[3]
 
1564
        if result.parent_id == '':
 
1565
            result.parent_id = None
 
1566
        self._fileid_to_entry_cache[result.file_id] = result
 
1567
        return result
 
1568
 
 
1569
    def _get_mutable_inventory(self):
 
1570
        """See CommonInventory._get_mutable_inventory."""
 
1571
        entries = self.iter_entries()
 
1572
        inv = Inventory(None, self.revision_id)
 
1573
        for path, inv_entry in entries:
 
1574
            inv.add(inv_entry.copy())
 
1575
        return inv
 
1576
 
 
1577
    def create_by_apply_delta(self, inventory_delta, new_revision_id,
 
1578
        propagate_caches=False):
 
1579
        """Create a new CHKInventory by applying inventory_delta to this one.
 
1580
 
 
1581
        :param inventory_delta: The inventory delta to apply. See
 
1582
            Inventory.apply_delta for details.
 
1583
        :param new_revision_id: The revision id of the resulting CHKInventory.
 
1584
        :param propagate_caches: If True, the caches for this inventory are
 
1585
          copied to and updated for the result.
 
1586
        :return: The new CHKInventory.
 
1587
        """
 
1588
        result = CHKInventory(self._search_key_name)
 
1589
        if propagate_caches:
 
1590
            # Just propagate the path-to-fileid cache for now
 
1591
            result._path_to_fileid_cache = dict(self._path_to_fileid_cache.iteritems())
 
1592
        search_key_func = chk_map.search_key_registry.get(self._search_key_name)
 
1593
        self.id_to_entry._ensure_root()
 
1594
        maximum_size = self.id_to_entry._root_node.maximum_size
 
1595
        result.revision_id = new_revision_id
 
1596
        result.id_to_entry = chk_map.CHKMap(
 
1597
            self.id_to_entry._store,
 
1598
            self.id_to_entry.key(),
 
1599
            search_key_func=search_key_func)
 
1600
        result.id_to_entry._ensure_root()
 
1601
        result.id_to_entry._root_node.set_maximum_size(maximum_size)
 
1602
        parent_id_basename_delta = []
 
1603
        if self.parent_id_basename_to_file_id is not None:
 
1604
            result.parent_id_basename_to_file_id = chk_map.CHKMap(
 
1605
                self.parent_id_basename_to_file_id._store,
 
1606
                self.parent_id_basename_to_file_id.key(),
 
1607
                search_key_func=search_key_func)
 
1608
            result.parent_id_basename_to_file_id._ensure_root()
 
1609
            self.parent_id_basename_to_file_id._ensure_root()
 
1610
            result_p_id_root = result.parent_id_basename_to_file_id._root_node
 
1611
            p_id_root = self.parent_id_basename_to_file_id._root_node
 
1612
            result_p_id_root.set_maximum_size(p_id_root.maximum_size)
 
1613
            result_p_id_root._key_width = p_id_root._key_width
 
1614
        else:
 
1615
            result.parent_id_basename_to_file_id = None
 
1616
        result.root_id = self.root_id
 
1617
        id_to_entry_delta = []
 
1618
        for old_path, new_path, file_id, entry in inventory_delta:
 
1619
            # file id changes
 
1620
            if new_path == '':
 
1621
                result.root_id = file_id
 
1622
            if new_path is None:
 
1623
                # Make a delete:
 
1624
                new_key = None
 
1625
                new_value = None
 
1626
                # Update caches
 
1627
                if propagate_caches:
 
1628
                    try:
 
1629
                        del result._path_to_fileid_cache[old_path]
 
1630
                    except KeyError:
 
1631
                        pass
 
1632
            else:
 
1633
                new_key = (file_id,)
 
1634
                new_value = result._entry_to_bytes(entry)
 
1635
                # Update caches. It's worth doing this whether
 
1636
                # we're propagating the old caches or not.
 
1637
                result._path_to_fileid_cache[new_path] = file_id
 
1638
            if old_path is None:
 
1639
                old_key = None
 
1640
            else:
 
1641
                old_key = (file_id,)
 
1642
            id_to_entry_delta.append((old_key, new_key, new_value))
 
1643
            if result.parent_id_basename_to_file_id is not None:
 
1644
                # parent_id, basename changes
 
1645
                if old_path is None:
 
1646
                    old_key = None
 
1647
                else:
 
1648
                    old_entry = self[file_id]
 
1649
                    old_key = self._parent_id_basename_key(old_entry)
 
1650
                if new_path is None:
 
1651
                    new_key = None
 
1652
                    new_value = None
 
1653
                else:
 
1654
                    new_key = self._parent_id_basename_key(entry)
 
1655
                    new_value = file_id
 
1656
                if old_key != new_key:
 
1657
                    # If the two keys are the same, the value will be unchanged
 
1658
                    # as its always the file id.
 
1659
                    parent_id_basename_delta.append((old_key, new_key, new_value))
 
1660
        result.id_to_entry.apply_delta(id_to_entry_delta)
 
1661
        if parent_id_basename_delta:
 
1662
            result.parent_id_basename_to_file_id.apply_delta(parent_id_basename_delta)
 
1663
        return result
 
1664
 
 
1665
    @classmethod
 
1666
    def deserialise(klass, chk_store, bytes, expected_revision_id):
 
1667
        """Deserialise a CHKInventory.
 
1668
 
 
1669
        :param chk_store: A CHK capable VersionedFiles instance.
 
1670
        :param bytes: The serialised bytes.
 
1671
        :param expected_revision_id: The revision ID we think this inventory is
 
1672
            for.
 
1673
        :return: A CHKInventory
 
1674
        """
 
1675
        lines = bytes.split('\n')
 
1676
        if lines[-1] != '':
 
1677
            raise AssertionError('bytes to deserialize must end with an eol')
 
1678
        lines.pop()
 
1679
        if lines[0] != 'chkinventory:':
 
1680
            raise ValueError("not a serialised CHKInventory: %r" % bytes)
 
1681
        info = {}
 
1682
        allowed_keys = frozenset(['root_id', 'revision_id', 'search_key_name',
 
1683
                                  'parent_id_basename_to_file_id',
 
1684
                                  'id_to_entry'])
 
1685
        for line in lines[1:]:
 
1686
            key, value = line.split(': ', 1)
 
1687
            if key not in allowed_keys:
 
1688
                raise errors.BzrError('Unknown key in inventory: %r\n%r'
 
1689
                                      % (key, bytes))
 
1690
            if key in info:
 
1691
                raise errors.BzrError('Duplicate key in inventory: %r\n%r'
 
1692
                                      % (key, bytes))
 
1693
            info[key] = value
 
1694
        revision_id = info['revision_id']
 
1695
        root_id = info['root_id']
 
1696
        search_key_name = info.get('search_key_name', 'plain')
 
1697
        parent_id_basename_to_file_id = info.get(
 
1698
            'parent_id_basename_to_file_id', None)
 
1699
        id_to_entry = info['id_to_entry']
 
1700
 
 
1701
        result = CHKInventory(search_key_name)
 
1702
        result.revision_id = revision_id
 
1703
        result.root_id = root_id
 
1704
        search_key_func = chk_map.search_key_registry.get(
 
1705
                            result._search_key_name)
 
1706
        if parent_id_basename_to_file_id is not None:
 
1707
            result.parent_id_basename_to_file_id = chk_map.CHKMap(
 
1708
                chk_store, (parent_id_basename_to_file_id,),
 
1709
                search_key_func=search_key_func)
 
1710
        else:
 
1711
            result.parent_id_basename_to_file_id = None
 
1712
 
 
1713
        result.id_to_entry = chk_map.CHKMap(chk_store, (id_to_entry,),
 
1714
                                            search_key_func=search_key_func)
 
1715
        if (result.revision_id,) != expected_revision_id:
 
1716
            raise ValueError("Mismatched revision id and expected: %r, %r" %
 
1717
                (result.revision_id, expected_revision_id))
 
1718
        return result
 
1719
 
 
1720
    @classmethod
 
1721
    def from_inventory(klass, chk_store, inventory, maximum_size=0, search_key_name='plain'):
 
1722
        """Create a CHKInventory from an existing inventory.
 
1723
 
 
1724
        The content of inventory is copied into the chk_store, and a
 
1725
        CHKInventory referencing that is returned.
 
1726
 
 
1727
        :param chk_store: A CHK capable VersionedFiles instance.
 
1728
        :param inventory: The inventory to copy.
 
1729
        :param maximum_size: The CHKMap node size limit.
 
1730
        :param search_key_name: The identifier for the search key function
 
1731
        """
 
1732
        result = klass(search_key_name)
 
1733
        result.revision_id = inventory.revision_id
 
1734
        result.root_id = inventory.root.file_id
 
1735
 
 
1736
        entry_to_bytes = result._entry_to_bytes
 
1737
        parent_id_basename_key = result._parent_id_basename_key
 
1738
        id_to_entry_dict = {}
 
1739
        parent_id_basename_dict = {}
 
1740
        for path, entry in inventory.iter_entries():
 
1741
            id_to_entry_dict[(entry.file_id,)] = entry_to_bytes(entry)
 
1742
            p_id_key = parent_id_basename_key(entry)
 
1743
            parent_id_basename_dict[p_id_key] = entry.file_id
 
1744
 
 
1745
        result._populate_from_dicts(chk_store, id_to_entry_dict,
 
1746
            parent_id_basename_dict, maximum_size=maximum_size)
 
1747
        return result
 
1748
 
 
1749
    def _populate_from_dicts(self, chk_store, id_to_entry_dict,
 
1750
                             parent_id_basename_dict, maximum_size):
 
1751
        search_key_func = chk_map.search_key_registry.get(self._search_key_name)
 
1752
        root_key = chk_map.CHKMap.from_dict(chk_store, id_to_entry_dict,
 
1753
                   maximum_size=maximum_size, key_width=1,
 
1754
                   search_key_func=search_key_func)
 
1755
        self.id_to_entry = chk_map.CHKMap(chk_store, root_key,
 
1756
                                          search_key_func)
 
1757
        root_key = chk_map.CHKMap.from_dict(chk_store,
 
1758
                   parent_id_basename_dict,
 
1759
                   maximum_size=maximum_size, key_width=2,
 
1760
                   search_key_func=search_key_func)
 
1761
        self.parent_id_basename_to_file_id = chk_map.CHKMap(chk_store,
 
1762
                                                    root_key, search_key_func)
 
1763
 
 
1764
    def _parent_id_basename_key(self, entry):
 
1765
        """Create a key for a entry in a parent_id_basename_to_file_id index."""
 
1766
        if entry.parent_id is not None:
 
1767
            parent_id = entry.parent_id
 
1768
        else:
 
1769
            parent_id = ''
 
1770
        return parent_id, entry.name.encode('utf8')
 
1771
 
 
1772
    def __getitem__(self, file_id):
 
1773
        """map a single file_id -> InventoryEntry."""
 
1774
        if file_id is None:
 
1775
            raise errors.NoSuchId(self, file_id)
 
1776
        result = self._fileid_to_entry_cache.get(file_id, None)
 
1777
        if result is not None:
 
1778
            return result
 
1779
        try:
 
1780
            return self._bytes_to_entry(
 
1781
                self.id_to_entry.iteritems([(file_id,)]).next()[1])
 
1782
        except StopIteration:
 
1783
            # really we're passing an inventory, not a tree...
 
1784
            raise errors.NoSuchId(self, file_id)
 
1785
 
 
1786
    def has_id(self, file_id):
 
1787
        # Perhaps have an explicit 'contains' method on CHKMap ?
 
1788
        if self._fileid_to_entry_cache.get(file_id, None) is not None:
 
1789
            return True
 
1790
        return len(list(self.id_to_entry.iteritems([(file_id,)]))) == 1
 
1791
 
 
1792
    def is_root(self, file_id):
 
1793
        return file_id == self.root_id
 
1794
 
 
1795
    def _iter_file_id_parents(self, file_id):
 
1796
        """Yield the parents of file_id up to the root."""
 
1797
        while file_id is not None:
 
1798
            try:
 
1799
                ie = self[file_id]
 
1800
            except KeyError:
 
1801
                raise errors.NoSuchId(tree=self, file_id=file_id)
 
1802
            yield ie
 
1803
            file_id = ie.parent_id
 
1804
 
 
1805
    def __iter__(self):
 
1806
        """Iterate over all file-ids."""
 
1807
        for key, _ in self.id_to_entry.iteritems():
 
1808
            yield key[-1]
 
1809
 
 
1810
    def iter_just_entries(self):
 
1811
        """Iterate over all entries.
 
1812
        
 
1813
        Unlike iter_entries(), just the entries are returned (not (path, ie))
 
1814
        and the order of entries is undefined.
 
1815
 
 
1816
        XXX: We may not want to merge this into bzr.dev.
 
1817
        """
 
1818
        for key, entry in self.id_to_entry.iteritems():
 
1819
            file_id = key[0]
 
1820
            ie = self._fileid_to_entry_cache.get(file_id, None)
 
1821
            if ie is None:
 
1822
                ie = self._bytes_to_entry(entry)
 
1823
                self._fileid_to_entry_cache[file_id] = ie
 
1824
            yield ie
 
1825
 
 
1826
    def iter_changes(self, basis):
 
1827
        """Generate a Tree.iter_changes change list between this and basis.
 
1828
 
 
1829
        :param basis: Another CHKInventory.
 
1830
        :return: An iterator over the changes between self and basis, as per
 
1831
            tree.iter_changes().
 
1832
        """
 
1833
        # We want: (file_id, (path_in_source, path_in_target),
 
1834
        # changed_content, versioned, parent, name, kind,
 
1835
        # executable)
 
1836
        for key, basis_value, self_value in \
 
1837
            self.id_to_entry.iter_changes(basis.id_to_entry):
 
1838
            file_id = key[0]
 
1839
            if basis_value is not None:
 
1840
                basis_entry = basis._bytes_to_entry(basis_value)
 
1841
                path_in_source = basis.id2path(file_id)
 
1842
                basis_parent = basis_entry.parent_id
 
1843
                basis_name = basis_entry.name
 
1844
                basis_executable = basis_entry.executable
 
1845
            else:
 
1846
                path_in_source = None
 
1847
                basis_parent = None
 
1848
                basis_name = None
 
1849
                basis_executable = None
 
1850
            if self_value is not None:
 
1851
                self_entry = self._bytes_to_entry(self_value)
 
1852
                path_in_target = self.id2path(file_id)
 
1853
                self_parent = self_entry.parent_id
 
1854
                self_name = self_entry.name
 
1855
                self_executable = self_entry.executable
 
1856
            else:
 
1857
                path_in_target = None
 
1858
                self_parent = None
 
1859
                self_name = None
 
1860
                self_executable = None
 
1861
            if basis_value is None:
 
1862
                # add
 
1863
                kind = (None, self_entry.kind)
 
1864
                versioned = (False, True)
 
1865
            elif self_value is None:
 
1866
                # delete
 
1867
                kind = (basis_entry.kind, None)
 
1868
                versioned = (True, False)
 
1869
            else:
 
1870
                kind = (basis_entry.kind, self_entry.kind)
 
1871
                versioned = (True, True)
 
1872
            changed_content = False
 
1873
            if kind[0] != kind[1]:
 
1874
                changed_content = True
 
1875
            elif kind[0] == 'file':
 
1876
                if (self_entry.text_size != basis_entry.text_size or
 
1877
                    self_entry.text_sha1 != basis_entry.text_sha1):
 
1878
                    changed_content = True
 
1879
            elif kind[0] == 'symlink':
 
1880
                if self_entry.symlink_target != basis_entry.symlink_target:
 
1881
                    changed_content = True
 
1882
            elif kind[0] == 'tree-reference':
 
1883
                if (self_entry.reference_revision !=
 
1884
                    basis_entry.reference_revision):
 
1885
                    changed_content = True
 
1886
            parent = (basis_parent, self_parent)
 
1887
            name = (basis_name, self_name)
 
1888
            executable = (basis_executable, self_executable)
 
1889
            if (not changed_content
 
1890
                and parent[0] == parent[1]
 
1891
                and name[0] == name[1]
 
1892
                and executable[0] == executable[1]):
 
1893
                # Could happen when only the revision changed for a directory
 
1894
                # for instance.
 
1895
                continue
 
1896
            yield (file_id, (path_in_source, path_in_target), changed_content,
 
1897
                versioned, parent, name, kind, executable)
 
1898
 
 
1899
    def __len__(self):
 
1900
        """Return the number of entries in the inventory."""
 
1901
        return len(self.id_to_entry)
 
1902
 
 
1903
    def _make_delta(self, old):
 
1904
        """Make an inventory delta from two inventories."""
 
1905
        if type(old) != CHKInventory:
 
1906
            return CommonInventory._make_delta(self, old)
 
1907
        delta = []
 
1908
        for key, old_value, self_value in \
 
1909
            self.id_to_entry.iter_changes(old.id_to_entry):
 
1910
            file_id = key[0]
 
1911
            if old_value is not None:
 
1912
                old_path = old.id2path(file_id)
 
1913
            else:
 
1914
                old_path = None
 
1915
            if self_value is not None:
 
1916
                entry = self._bytes_to_entry(self_value)
 
1917
                self._fileid_to_entry_cache[file_id] = entry
 
1918
                new_path = self.id2path(file_id)
 
1919
            else:
 
1920
                entry = None
 
1921
                new_path = None
 
1922
            delta.append((old_path, new_path, file_id, entry))
 
1923
        return delta
 
1924
 
 
1925
    def path2id(self, name):
 
1926
        """See CommonInventory.path2id()."""
 
1927
        result = self._path_to_fileid_cache.get(name, None)
 
1928
        if result is None:
 
1929
            result = CommonInventory.path2id(self, name)
 
1930
            self._path_to_fileid_cache[name] = result
 
1931
        return result
 
1932
 
 
1933
    def to_lines(self):
 
1934
        """Serialise the inventory to lines."""
 
1935
        lines = ["chkinventory:\n"]
 
1936
        if self._search_key_name != 'plain':
 
1937
            # custom ordering grouping things that don't change together
 
1938
            lines.append('search_key_name: %s\n' % (self._search_key_name,))
 
1939
            lines.append("root_id: %s\n" % self.root_id)
 
1940
            lines.append('parent_id_basename_to_file_id: %s\n' %
 
1941
                self.parent_id_basename_to_file_id.key())
 
1942
            lines.append("revision_id: %s\n" % self.revision_id)
 
1943
            lines.append("id_to_entry: %s\n" % self.id_to_entry.key())
 
1944
        else:
 
1945
            lines.append("revision_id: %s\n" % self.revision_id)
 
1946
            lines.append("root_id: %s\n" % self.root_id)
 
1947
            if self.parent_id_basename_to_file_id is not None:
 
1948
                lines.append('parent_id_basename_to_file_id: %s\n' %
 
1949
                    self.parent_id_basename_to_file_id.key())
 
1950
            lines.append("id_to_entry: %s\n" % self.id_to_entry.key())
 
1951
        return lines
 
1952
 
 
1953
    @property
 
1954
    def root(self):
 
1955
        """Get the root entry."""
 
1956
        return self[self.root_id]
 
1957
 
 
1958
 
 
1959
class CHKInventoryDirectory(InventoryDirectory):
 
1960
    """A directory in an inventory."""
 
1961
 
 
1962
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
1963
                 'text_id', 'parent_id', '_children', 'executable',
 
1964
                 'revision', 'symlink_target', 'reference_revision',
 
1965
                 '_chk_inventory']
 
1966
 
 
1967
    def __init__(self, file_id, name, parent_id, chk_inventory):
 
1968
        # Don't call InventoryDirectory.__init__ - it isn't right for this
 
1969
        # class.
 
1970
        InventoryEntry.__init__(self, file_id, name, parent_id)
 
1971
        self._children = None
 
1972
        self.kind = 'directory'
 
1973
        self._chk_inventory = chk_inventory
 
1974
 
 
1975
    @property
 
1976
    def children(self):
 
1977
        """Access the list of children of this directory.
 
1978
 
 
1979
        With a parent_id_basename_to_file_id index, loads all the children,
 
1980
        without loads the entire index. Without is bad. A more sophisticated
 
1981
        proxy object might be nice, to allow partial loading of children as
 
1982
        well when specific names are accessed. (So path traversal can be
 
1983
        written in the obvious way but not examine siblings.).
 
1984
        """
 
1985
        if self._children is not None:
 
1986
            return self._children
 
1987
        # No longer supported
 
1988
        if self._chk_inventory.parent_id_basename_to_file_id is None:
 
1989
            raise AssertionError("Inventories without"
 
1990
                " parent_id_basename_to_file_id are no longer supported")
 
1991
        result = {}
 
1992
        # XXX: Todo - use proxy objects for the children rather than loading
 
1993
        # all when the attribute is referenced.
 
1994
        parent_id_index = self._chk_inventory.parent_id_basename_to_file_id
 
1995
        child_keys = set()
 
1996
        for (parent_id, name_utf8), file_id in parent_id_index.iteritems(
 
1997
            key_filter=[(self.file_id,)]):
 
1998
            child_keys.add((file_id,))
 
1999
        cached = set()
 
2000
        for file_id_key in child_keys:
 
2001
            entry = self._chk_inventory._fileid_to_entry_cache.get(
 
2002
                file_id_key[0], None)
 
2003
            if entry is not None:
 
2004
                result[entry.name] = entry
 
2005
                cached.add(file_id_key)
 
2006
        child_keys.difference_update(cached)
 
2007
        # populate; todo: do by name
 
2008
        id_to_entry = self._chk_inventory.id_to_entry
 
2009
        for file_id_key, bytes in id_to_entry.iteritems(child_keys):
 
2010
            entry = self._chk_inventory._bytes_to_entry(bytes)
 
2011
            result[entry.name] = entry
 
2012
            self._chk_inventory._fileid_to_entry_cache[file_id_key[0]] = entry
 
2013
        self._children = result
 
2014
        return result
 
2015
 
 
2016
entry_factory = {
 
2017
    'directory': InventoryDirectory,
 
2018
    'file': InventoryFile,
 
2019
    'symlink': InventoryLink,
 
2020
    'tree-reference': TreeReference
 
2021
}
 
2022
 
 
2023
def make_entry(kind, name, parent_id, file_id=None):
 
2024
    """Create an inventory entry.
 
2025
 
 
2026
    :param kind: the type of inventory entry to create.
 
2027
    :param name: the basename of the entry.
 
2028
    :param parent_id: the parent_id of the entry.
 
2029
    :param file_id: the file_id to use. if None, one will be created.
 
2030
    """
 
2031
    if file_id is None:
 
2032
        file_id = generate_ids.gen_file_id(name)
 
2033
    name = ensure_normalized_name(name)
 
2034
    try:
 
2035
        factory = entry_factory[kind]
 
2036
    except KeyError:
 
2037
        raise BzrError("unknown kind %r" % kind)
 
2038
    return factory(file_id, name, parent_id)
 
2039
 
 
2040
 
 
2041
def ensure_normalized_name(name):
 
2042
    """Normalize name.
 
2043
 
 
2044
    :raises InvalidNormalization: When name is not normalized, and cannot be
 
2045
        accessed on this platform by the normalized path.
 
2046
    :return: The NFC normalised version of name.
 
2047
    """
 
2048
    #------- This has been copied to bzrlib.dirstate.DirState.add, please
 
2049
    # keep them synchronised.
 
2050
    # we dont import normalized_filename directly because we want to be
 
2051
    # able to change the implementation at runtime for tests.
 
2052
    norm_name, can_access = osutils.normalized_filename(name)
 
2053
    if norm_name != name:
 
2054
        if can_access:
 
2055
            return norm_name
 
2056
        else:
 
2057
            # TODO: jam 20060701 This would probably be more useful
 
2058
            #       if the error was raised with the full path
 
2059
            raise errors.InvalidNormalization(name)
 
2060
    return name
 
2061
 
 
2062
 
 
2063
_NAME_RE = None
 
2064
 
 
2065
def is_valid_name(name):
 
2066
    global _NAME_RE
 
2067
    if _NAME_RE is None:
 
2068
        _NAME_RE = re.compile(r'^[^/\\]+$')
 
2069
 
 
2070
    return bool(_NAME_RE.match(name))