~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
28
28
ROOT_ID = "TREE_ROOT"
29
29
 
30
30
 
31
 
import collections
32
31
import os.path
33
32
import re
34
33
import sys
38
37
import bzrlib
39
38
from bzrlib.osutils import (pumpfile, quotefn, splitpath, joinpath,
40
39
                            pathjoin, sha_strings)
 
40
from bzrlib.trace import mutter
41
41
from bzrlib.errors import (NotVersionedError, InvalidEntryName,
42
 
                           BzrError, BzrCheckError, BinaryFile)
43
 
from bzrlib.trace import mutter
 
42
                           BzrError, BzrCheckError)
44
43
 
45
44
 
46
45
class InventoryEntry(object):
114
113
    >>> i.id2path('2326')
115
114
    'src/wibble/wibble.c'
116
115
    """
117
 
 
118
 
    # Constants returned by describe_change()
119
 
    #
120
 
    # TODO: These should probably move to some kind of FileChangeDescription 
121
 
    # class; that's like what's inside a TreeDelta but we want to be able to 
122
 
    # generate them just for one file at a time.
123
 
    RENAMED = 'renamed'
124
 
    MODIFIED_AND_RENAMED = 'modified and renamed'
125
116
    
126
 
    __slots__ = []
 
117
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
 
118
                 'text_id', 'parent_id', 'children', 'executable', 
 
119
                 'revision']
 
120
 
 
121
    def _add_text_to_weave(self, new_lines, parents, weave_store, transaction):
 
122
        weave_store.add_text(self.file_id, self.revision, new_lines, parents,
 
123
                             transaction)
127
124
 
128
125
    def detect_changes(self, old_entry):
129
126
        """Return a (text_modified, meta_modified) from this to old_entry.
154
151
             output_to, reverse=False):
155
152
        """Perform a diff between two entries of the same kind."""
156
153
 
157
 
    def find_previous_heads(self, previous_inventories,
158
 
                            versioned_file_store,
159
 
                            transaction,
160
 
                            entry_vf=None):
161
 
        """Return the revisions and entries that directly precede this.
 
154
    def find_previous_heads(self, previous_inventories, entry_weave):
 
155
        """Return the revisions and entries that directly preceed this.
162
156
 
163
157
        Returned as a map from revision to inventory entry.
164
158
 
165
159
        This is a map containing the file revisions in all parents
166
160
        for which the file exists, and its revision is not a parent of
167
161
        any other. If the file is new, the set will be empty.
168
 
 
169
 
        :param versioned_file_store: A store where ancestry data on this
170
 
                                     file id can be queried.
171
 
        :param transaction: The transaction that queries to the versioned 
172
 
                            file store should be completed under.
173
 
        :param entry_vf: The entry versioned file, if its already available.
174
162
        """
175
163
        def get_ancestors(weave, entry):
176
 
            return set(weave.get_ancestry(entry.revision))
177
 
        # revision:ie mapping for each ie found in previous_inventories.
178
 
        candidates = {}
179
 
        # revision:ie mapping with one revision for each head.
 
164
            return set(map(weave.idx_to_name,
 
165
                           weave.inclusions([weave.lookup(entry.revision)])))
180
166
        heads = {}
181
 
        # revision: ancestor list for each head
182
167
        head_ancestors = {}
183
 
        # identify candidate head revision ids.
184
168
        for inv in previous_inventories:
185
169
            if self.file_id in inv:
186
170
                ie = inv[self.file_id]
187
171
                assert ie.file_id == self.file_id
188
 
                if ie.revision in candidates:
189
 
                    # same revision value in two different inventories:
190
 
                    # correct possible inconsistencies:
191
 
                    #     * there was a bug in revision updates with 'x' bit 
192
 
                    #       support.
 
172
                if ie.revision in heads:
 
173
                    # fixup logic, there was a bug in revision updates.
 
174
                    # with x bit support.
193
175
                    try:
194
 
                        if candidates[ie.revision].executable != ie.executable:
195
 
                            candidates[ie.revision].executable = False
 
176
                        if heads[ie.revision].executable != ie.executable:
 
177
                            heads[ie.revision].executable = False
196
178
                            ie.executable = False
197
179
                    except AttributeError:
198
180
                        pass
199
 
                    # must now be the same.
200
 
                    assert candidates[ie.revision] == ie
 
181
                    assert heads[ie.revision] == ie
201
182
                else:
202
 
                    # add this revision as a candidate.
203
 
                    candidates[ie.revision] = ie
204
 
 
205
 
        # common case optimisation
206
 
        if len(candidates) == 1:
207
 
            # if there is only one candidate revision found
208
 
            # then we can opening the versioned file to access ancestry:
209
 
            # there cannot be any ancestors to eliminate when there is 
210
 
            # only one revision available.
211
 
            heads[ie.revision] = ie
212
 
            return heads
213
 
 
214
 
        # eliminate ancestors amongst the available candidates:
215
 
        # heads are those that are not an ancestor of any other candidate
216
 
        # - this provides convergence at a per-file level.
217
 
        for ie in candidates.values():
218
 
            # may be an ancestor of a known head:
219
 
            already_present = 0 != len(
220
 
                [head for head in heads 
221
 
                 if ie.revision in head_ancestors[head]])
222
 
            if already_present:
223
 
                # an ancestor of an analyzed candidate.
224
 
                continue
225
 
            # not an ancestor of a known head:
226
 
            # load the versioned file for this file id if needed
227
 
            if entry_vf is None:
228
 
                entry_vf = versioned_file_store.get_weave_or_empty(
229
 
                    self.file_id, transaction)
230
 
            ancestors = get_ancestors(entry_vf, ie)
231
 
            # may knock something else out:
232
 
            check_heads = list(heads.keys())
233
 
            for head in check_heads:
234
 
                if head in ancestors:
235
 
                    # this previously discovered 'head' is not
236
 
                    # really a head - its an ancestor of the newly 
237
 
                    # found head,
238
 
                    heads.pop(head)
239
 
            head_ancestors[ie.revision] = ancestors
240
 
            heads[ie.revision] = ie
 
183
                    # may want to add it.
 
184
                    # may already be covered:
 
185
                    already_present = 0 != len(
 
186
                        [head for head in heads 
 
187
                         if ie.revision in head_ancestors[head]])
 
188
                    if already_present:
 
189
                        # an ancestor of a known head.
 
190
                        continue
 
191
                    # definately a head:
 
192
                    ancestors = get_ancestors(entry_weave, ie)
 
193
                    # may knock something else out:
 
194
                    check_heads = list(heads.keys())
 
195
                    for head in check_heads:
 
196
                        if head in ancestors:
 
197
                            # this head is not really a head
 
198
                            heads.pop(head)
 
199
                    head_ancestors[ie.revision] = ancestors
 
200
                    heads[ie.revision] = ie
241
201
        return heads
242
202
 
243
203
    def get_tar_item(self, root, dp, now, tree):
317
277
        raise BzrError("don't know how to export {%s} of kind %r" % (self.file_id, self.kind))
318
278
 
319
279
    def sorted_children(self):
320
 
        return sorted(self.children.items())
 
280
        l = self.children.items()
 
281
        l.sort()
 
282
        return l
321
283
 
322
284
    @staticmethod
323
285
    def versionable_kind(kind):
328
290
 
329
291
        This is a template method, override _check for kind specific
330
292
        tests.
331
 
 
332
 
        :param checker: Check object providing context for the checks; 
333
 
             can be used to find out what parts of the repository have already
334
 
             been checked.
335
 
        :param rev_id: Revision id from which this InventoryEntry was loaded.
336
 
             Not necessarily the last-changed revision for this file.
337
 
        :param inv: Inventory from which the entry was loaded.
338
 
        :param tree: RevisionTree for this entry.
339
293
        """
340
 
        if self.parent_id is not None:
 
294
        if self.parent_id != None:
341
295
            if not inv.has_id(self.parent_id):
342
296
                raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
343
297
                        % (self.parent_id, rev_id))
348
302
        raise BzrCheckError('unknown entry kind %r in revision {%s}' % 
349
303
                            (self.kind, rev_id))
350
304
 
 
305
 
351
306
    def copy(self):
352
307
        """Clone this inventory entry."""
353
308
        raise NotImplementedError
354
309
 
355
 
    @staticmethod
356
 
    def describe_change(old_entry, new_entry):
357
 
        """Describe the change between old_entry and this.
358
 
        
359
 
        This smells of being an InterInventoryEntry situation, but as its
360
 
        the first one, we're making it a static method for now.
361
 
 
362
 
        An entry with a different parent, or different name is considered 
363
 
        to be renamed. Reparenting is an internal detail.
364
 
        Note that renaming the parent does not trigger a rename for the
365
 
        child entry itself.
366
 
        """
367
 
        # TODO: Perhaps return an object rather than just a string
368
 
        if old_entry is new_entry:
369
 
            # also the case of both being None
370
 
            return 'unchanged'
371
 
        elif old_entry is None:
 
310
    def _get_snapshot_change(self, previous_entries):
 
311
        if len(previous_entries) > 1:
 
312
            return 'merged'
 
313
        elif len(previous_entries) == 0:
372
314
            return 'added'
373
 
        elif new_entry is None:
374
 
            return 'removed'
375
 
        text_modified, meta_modified = new_entry.detect_changes(old_entry)
376
 
        if text_modified or meta_modified:
377
 
            modified = True
378
 
        else:
379
 
            modified = False
380
 
        # TODO 20060511 (mbp, rbc) factor out 'detect_rename' here.
381
 
        if old_entry.parent_id != new_entry.parent_id:
382
 
            renamed = True
383
 
        elif old_entry.name != new_entry.name:
384
 
            renamed = True
385
 
        else:
386
 
            renamed = False
387
 
        if renamed and not modified:
388
 
            return InventoryEntry.RENAMED
389
 
        if modified and not renamed:
390
 
            return 'modified'
391
 
        if modified and renamed:
392
 
            return InventoryEntry.MODIFIED_AND_RENAMED
393
 
        return 'unchanged'
 
315
        else:
 
316
            return 'modified/renamed/reparented'
394
317
 
395
318
    def __repr__(self):
396
319
        return ("%s(%r, %r, parent_id=%r)"
400
323
                   self.parent_id))
401
324
 
402
325
    def snapshot(self, revision, path, previous_entries,
403
 
                 work_tree, commit_builder):
 
326
                 work_tree, weave_store, transaction):
404
327
        """Make a snapshot of this entry which may or may not have changed.
405
328
        
406
329
        This means that all its fields are populated, that it has its
408
331
        """
409
332
        mutter('new parents of %s are %r', path, previous_entries)
410
333
        self._read_tree_state(path, work_tree)
411
 
        # TODO: Where should we determine whether to reuse a
412
 
        # previous revision id or create a new revision? 20060606
413
334
        if len(previous_entries) == 1:
414
335
            # cannot be unchanged unless there is only one parent file rev.
415
336
            parent_ie = previous_entries.values()[0]
417
338
                mutter("found unchanged entry")
418
339
                self.revision = parent_ie.revision
419
340
                return "unchanged"
420
 
        return self._snapshot_into_revision(revision, previous_entries, 
421
 
                                            work_tree, commit_builder)
422
 
 
423
 
    def _snapshot_into_revision(self, revision, previous_entries, work_tree,
424
 
                                commit_builder):
425
 
        """Record this revision unconditionally into a store.
426
 
 
427
 
        The entry's last-changed revision property (`revision`) is updated to 
428
 
        that of the new revision.
429
 
        
430
 
        :param revision: id of the new revision that is being recorded.
431
 
 
432
 
        :returns: String description of the commit (e.g. "merged", "modified"), etc.
433
 
        """
434
 
        mutter('new revision {%s} for {%s}', revision, self.file_id)
 
341
        return self.snapshot_revision(revision, previous_entries, 
 
342
                                      work_tree, weave_store, transaction)
 
343
 
 
344
    def snapshot_revision(self, revision, previous_entries, work_tree,
 
345
                          weave_store, transaction):
 
346
        """Record this revision unconditionally."""
 
347
        mutter('new revision for {%s}', self.file_id)
435
348
        self.revision = revision
436
 
        self._snapshot_text(previous_entries, work_tree, commit_builder)
 
349
        change = self._get_snapshot_change(previous_entries)
 
350
        self._snapshot_text(previous_entries, work_tree, weave_store,
 
351
                            transaction)
 
352
        return change
437
353
 
438
 
    def _snapshot_text(self, file_parents, work_tree, commit_builder): 
 
354
    def _snapshot_text(self, file_parents, work_tree, weave_store, transaction): 
439
355
        """Record the 'text' of this entry, whatever form that takes.
440
356
        
441
357
        This default implementation simply adds an empty text.
442
358
        """
443
 
        raise NotImplementedError(self._snapshot_text)
 
359
        mutter('storing file {%s} in revision {%s}',
 
360
               self.file_id, self.revision)
 
361
        self._add_text_to_weave([], file_parents, weave_store, transaction)
444
362
 
445
363
    def __eq__(self, other):
446
364
        if not isinstance(other, InventoryEntry):
467
385
    def _unchanged(self, previous_ie):
468
386
        """Has this entry changed relative to previous_ie.
469
387
 
470
 
        This method should be overridden in child classes.
 
388
        This method should be overriden in child classes.
471
389
        """
472
390
        compatible = True
473
391
        # different inv parent
489
407
        # first requested, or preload them if they're already known
490
408
        pass            # nothing to do by default
491
409
 
492
 
    def _forget_tree_state(self):
493
 
        pass
494
 
 
495
410
 
496
411
class RootEntry(InventoryEntry):
497
412
 
498
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
499
 
                 'text_id', 'parent_id', 'children', 'executable', 
500
 
                 'revision', 'symlink_target']
501
 
 
502
413
    def _check(self, checker, rev_id, tree):
503
414
        """See InventoryEntry._check"""
504
415
 
520
431
class InventoryDirectory(InventoryEntry):
521
432
    """A directory in an inventory."""
522
433
 
523
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
524
 
                 'text_id', 'parent_id', 'children', 'executable', 
525
 
                 'revision', 'symlink_target']
526
 
 
527
434
    def _check(self, checker, rev_id, tree):
528
435
        """See InventoryEntry._check"""
529
 
        if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
 
436
        if self.text_sha1 != None or self.text_size != None or self.text_id != None:
530
437
            raise BzrCheckError('directory {%s} has text in revision {%s}'
531
438
                                % (self.file_id, rev_id))
532
439
 
559
466
        """See InventoryEntry._put_on_disk."""
560
467
        os.mkdir(fullpath)
561
468
 
562
 
    def _snapshot_text(self, file_parents, work_tree, commit_builder):
563
 
        """See InventoryEntry._snapshot_text."""
564
 
        commit_builder.modified_directory(self.file_id, file_parents)
565
 
 
566
469
 
567
470
class InventoryFile(InventoryEntry):
568
471
    """A file in an inventory."""
569
472
 
570
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
571
 
                 'text_id', 'parent_id', 'children', 'executable', 
572
 
                 'revision', 'symlink_target']
573
 
 
574
 
    def _check(self, checker, tree_revision_id, tree):
 
473
    def _check(self, checker, rev_id, tree):
575
474
        """See InventoryEntry._check"""
576
 
        t = (self.file_id, self.revision)
 
475
        revision = self.revision
 
476
        t = (self.file_id, revision)
577
477
        if t in checker.checked_texts:
578
 
            prev_sha = checker.checked_texts[t]
 
478
            prev_sha = checker.checked_texts[t] 
579
479
            if prev_sha != self.text_sha1:
580
480
                raise BzrCheckError('mismatched sha1 on {%s} in {%s}' %
581
 
                                    (self.file_id, tree_revision_id))
 
481
                                    (self.file_id, rev_id))
582
482
            else:
583
483
                checker.repeated_text_cnt += 1
584
484
                return
592
492
            w.check()
593
493
            checker.checked_weaves[self.file_id] = True
594
494
        else:
595
 
            w = tree.get_weave(self.file_id)
 
495
            w = tree.get_weave_prelude(self.file_id)
596
496
 
597
 
        mutter('check version {%s} of {%s}', tree_revision_id, self.file_id)
598
 
        checker.checked_text_cnt += 1
 
497
        mutter('check version {%s} of {%s}', rev_id, self.file_id)
 
498
        checker.checked_text_cnt += 1 
599
499
        # We can't check the length, because Weave doesn't store that
600
500
        # information, and the whole point of looking at the weave's
601
501
        # sha1sum is that we don't have to extract the text.
615
515
 
616
516
    def detect_changes(self, old_entry):
617
517
        """See InventoryEntry.detect_changes."""
618
 
        assert self.text_sha1 is not None
619
 
        assert old_entry.text_sha1 is not None
 
518
        assert self.text_sha1 != None
 
519
        assert old_entry.text_sha1 != None
620
520
        text_modified = (self.text_sha1 != old_entry.text_sha1)
621
521
        meta_modified = (self.executable != old_entry.executable)
622
522
        return text_modified, meta_modified
624
524
    def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
625
525
             output_to, reverse=False):
626
526
        """See InventoryEntry._diff."""
627
 
        try:
628
 
            from_text = tree.get_file(self.file_id).readlines()
629
 
            if to_entry:
630
 
                to_text = to_tree.get_file(to_entry.file_id).readlines()
631
 
            else:
632
 
                to_text = []
633
 
            if not reverse:
634
 
                text_diff(from_label, from_text,
635
 
                          to_label, to_text, output_to)
636
 
            else:
637
 
                text_diff(to_label, to_text,
638
 
                          from_label, from_text, output_to)
639
 
        except BinaryFile:
640
 
            if reverse:
641
 
                label_pair = (to_label, from_label)
642
 
            else:
643
 
                label_pair = (from_label, to_label)
644
 
            print >> output_to, "Binary files %s and %s differ" % label_pair
 
527
        from_text = tree.get_file(self.file_id).readlines()
 
528
        if to_entry:
 
529
            to_text = to_tree.get_file(to_entry.file_id).readlines()
 
530
        else:
 
531
            to_text = []
 
532
        if not reverse:
 
533
            text_diff(from_label, from_text,
 
534
                      to_label, to_text, output_to)
 
535
        else:
 
536
            text_diff(to_label, to_text,
 
537
                      from_label, from_text, output_to)
645
538
 
646
539
    def has_text(self):
647
540
        """See InventoryEntry.has_text."""
674
567
 
675
568
    def _read_tree_state(self, path, work_tree):
676
569
        """See InventoryEntry._read_tree_state."""
677
 
        self.text_sha1 = work_tree.get_file_sha1(self.file_id, path=path)
678
 
        # FIXME: 20050930 probe for the text size when getting sha1
679
 
        # in _read_tree_state
680
 
        self.executable = work_tree.is_executable(self.file_id, path=path)
681
 
 
682
 
    def _forget_tree_state(self):
683
 
        self.text_sha1 = None
684
 
        self.executable = None
685
 
 
686
 
    def _snapshot_text(self, file_parents, work_tree, commit_builder):
 
570
        self.text_sha1 = work_tree.get_file_sha1(self.file_id)
 
571
        self.executable = work_tree.is_executable(self.file_id)
 
572
 
 
573
    def _snapshot_text(self, file_parents, work_tree, weave_store, transaction):
687
574
        """See InventoryEntry._snapshot_text."""
688
 
        def get_content_byte_lines():
689
 
            return work_tree.get_file(self.file_id).readlines()
690
 
        self.text_sha1, self.text_size = commit_builder.modified_file_text(
691
 
            self.file_id, file_parents, get_content_byte_lines, self.text_sha1, self.text_size)
 
575
        mutter('storing file {%s} in revision {%s}',
 
576
               self.file_id, self.revision)
 
577
        # special case to avoid diffing on renames or 
 
578
        # reparenting
 
579
        if (len(file_parents) == 1
 
580
            and self.text_sha1 == file_parents.values()[0].text_sha1
 
581
            and self.text_size == file_parents.values()[0].text_size):
 
582
            previous_ie = file_parents.values()[0]
 
583
            weave_store.add_identical_text(
 
584
                self.file_id, previous_ie.revision, 
 
585
                self.revision, file_parents, transaction)
 
586
        else:
 
587
            new_lines = work_tree.get_file(self.file_id).readlines()
 
588
            self._add_text_to_weave(new_lines, file_parents, weave_store,
 
589
                                    transaction)
 
590
            self.text_sha1 = sha_strings(new_lines)
 
591
            self.text_size = sum(map(len, new_lines))
 
592
 
692
593
 
693
594
    def _unchanged(self, previous_ie):
694
595
        """See InventoryEntry._unchanged."""
707
608
class InventoryLink(InventoryEntry):
708
609
    """A file in an inventory."""
709
610
 
710
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
711
 
                 'text_id', 'parent_id', 'children', 'executable', 
712
 
                 'revision', 'symlink_target']
 
611
    __slots__ = ['symlink_target']
713
612
 
714
613
    def _check(self, checker, rev_id, tree):
715
614
        """See InventoryEntry._check"""
716
 
        if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
 
615
        if self.text_sha1 != None or self.text_size != None or self.text_id != None:
717
616
            raise BzrCheckError('symlink {%s} has text in revision {%s}'
718
617
                    % (self.file_id, rev_id))
719
 
        if self.symlink_target is None:
 
618
        if self.symlink_target == None:
720
619
            raise BzrCheckError('symlink {%s} has no target in revision {%s}'
721
620
                    % (self.file_id, rev_id))
722
621
 
780
679
        """See InventoryEntry._read_tree_state."""
781
680
        self.symlink_target = work_tree.get_symlink_target(self.file_id)
782
681
 
783
 
    def _forget_tree_state(self):
784
 
        self.symlink_target = None
785
 
 
786
682
    def _unchanged(self, previous_ie):
787
683
        """See InventoryEntry._unchanged."""
788
684
        compatible = super(InventoryLink, self)._unchanged(previous_ie)
790
686
            compatible = False
791
687
        return compatible
792
688
 
793
 
    def _snapshot_text(self, file_parents, work_tree, commit_builder):
794
 
        """See InventoryEntry._snapshot_text."""
795
 
        commit_builder.modified_link(
796
 
            self.file_id, file_parents, self.symlink_target)
797
 
 
798
689
 
799
690
class Inventory(object):
800
691
    """Inventory of versioned files in a tree.
829
720
    May also look up by name:
830
721
 
831
722
    >>> [x[0] for x in inv.iter_entries()]
832
 
    [u'hello.c']
 
723
    ['hello.c']
833
724
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
834
725
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
835
726
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT-12345678-12345678')
836
727
    """
837
 
    def __init__(self, root_id=ROOT_ID, revision_id=None):
 
728
    def __init__(self, root_id=ROOT_ID):
838
729
        """Create or read an inventory.
839
730
 
840
731
        If a working directory is specified, the inventory is read
849
740
        #if root_id is None:
850
741
        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
851
742
        self.root = RootEntry(root_id)
852
 
        self.revision_id = revision_id
853
743
        self._byid = {self.root.file_id: self.root}
854
744
 
 
745
 
855
746
    def copy(self):
856
 
        # TODO: jam 20051218 Should copy also copy the revision_id?
857
747
        other = Inventory(self.root.file_id)
858
748
        # copy recursively so we know directories will be added before
859
749
        # their children.  There are more efficient ways than this...
863
753
            other.add(entry.copy())
864
754
        return other
865
755
 
 
756
 
866
757
    def __iter__(self):
867
758
        return iter(self._byid)
868
759
 
 
760
 
869
761
    def __len__(self):
870
762
        """Returns number of entries."""
871
763
        return len(self._byid)
872
764
 
 
765
 
873
766
    def iter_entries(self, from_dir=None):
874
767
        """Return (path, entry) pairs, in order by name."""
875
 
        if from_dir is None:
876
 
            assert self.root
877
 
            from_dir = self.root
878
 
        elif isinstance(from_dir, basestring):
879
 
            from_dir = self._byid[from_dir]
880
 
            
881
 
        # unrolling the recursive called changed the time from
882
 
        # 440ms/663ms (inline/total) to 116ms/116ms
883
 
        children = from_dir.children.items()
884
 
        children.sort()
885
 
        children = collections.deque(children)
886
 
        stack = [(u'', children)]
887
 
        while stack:
888
 
            from_dir_relpath, children = stack[-1]
889
 
 
890
 
            while children:
891
 
                name, ie = children.popleft()
892
 
 
893
 
                # we know that from_dir_relpath never ends in a slash
894
 
                # and 'f' doesn't begin with one, we can do a string op, rather
895
 
                # than the checks of pathjoin(), though this means that all paths
896
 
                # start with a slash
897
 
                path = from_dir_relpath + '/' + name
898
 
 
899
 
                yield path[1:], ie
900
 
 
901
 
                if ie.kind != 'directory':
902
 
                    continue
903
 
 
904
 
                # But do this child first
905
 
                new_children = ie.children.items()
906
 
                new_children.sort()
907
 
                new_children = collections.deque(new_children)
908
 
                stack.append((path, new_children))
909
 
                # Break out of inner loop, so that we start outer loop with child
910
 
                break
911
 
            else:
912
 
                # if we finished all children, pop it off the stack
913
 
                stack.pop()
914
 
 
915
 
    def iter_entries_by_dir(self, from_dir=None):
916
 
        """Iterate over the entries in a directory first order.
917
 
 
918
 
        This returns all entries for a directory before returning
919
 
        the entries for children of a directory. This is not
920
 
        lexicographically sorted order, and is a hybrid between
921
 
        depth-first and breadth-first.
922
 
 
923
 
        :return: This yields (path, entry) pairs
924
 
        """
925
 
        # TODO? Perhaps this should return the from_dir so that the root is
926
 
        # yielded? or maybe an option?
927
 
        if from_dir is None:
928
 
            assert self.root
929
 
            from_dir = self.root
930
 
        elif isinstance(from_dir, basestring):
931
 
            from_dir = self._byid[from_dir]
932
 
            
933
 
        stack = [(u'', from_dir)]
934
 
        while stack:
935
 
            cur_relpath, cur_dir = stack.pop()
936
 
 
937
 
            child_dirs = []
938
 
            for child_name, child_ie in sorted(cur_dir.children.iteritems()):
939
 
 
940
 
                child_relpath = cur_relpath + child_name
941
 
 
942
 
                yield child_relpath, child_ie
943
 
 
944
 
                if child_ie.kind == 'directory':
945
 
                    child_dirs.append((child_relpath+'/', child_ie))
946
 
            stack.extend(reversed(child_dirs))
 
768
        if from_dir == None:
 
769
            assert self.root
 
770
            from_dir = self.root
 
771
        elif isinstance(from_dir, basestring):
 
772
            from_dir = self._byid[from_dir]
 
773
            
 
774
        kids = from_dir.children.items()
 
775
        kids.sort()
 
776
        for name, ie in kids:
 
777
            yield name, ie
 
778
            if ie.kind == 'directory':
 
779
                for cn, cie in self.iter_entries(from_dir=ie.file_id):
 
780
                    yield pathjoin(name, cn), cie
 
781
 
947
782
 
948
783
    def entries(self):
949
784
        """Return list of (path, ie) for all entries except the root.
963
798
        descend(self.root, u'')
964
799
        return accum
965
800
 
 
801
 
966
802
    def directories(self):
967
803
        """Return (path, entry) pairs for all directories, including the root.
968
804
        """
979
815
        descend(self.root, u'')
980
816
        return accum
981
817
        
 
818
 
 
819
 
982
820
    def __contains__(self, file_id):
983
821
        """True if this entry contains a file with given id.
984
822
 
992
830
        """
993
831
        return file_id in self._byid
994
832
 
 
833
 
995
834
    def __getitem__(self, file_id):
996
835
        """Return the entry for given file_id.
997
836
 
1004
843
        try:
1005
844
            return self._byid[file_id]
1006
845
        except KeyError:
1007
 
            if file_id is None:
 
846
            if file_id == None:
1008
847
                raise BzrError("can't look up file_id None")
1009
848
            else:
1010
849
                raise BzrError("file_id {%s} not in inventory" % file_id)
1011
850
 
 
851
 
1012
852
    def get_file_kind(self, file_id):
1013
853
        return self._byid[file_id].kind
1014
854
 
1015
855
    def get_child(self, parent_id, filename):
1016
856
        return self[parent_id].children.get(filename)
1017
857
 
 
858
 
1018
859
    def add(self, entry):
1019
860
        """Add entry to inventory.
1020
861
 
1034
875
        except KeyError:
1035
876
            raise BzrError("parent_id {%s} not in inventory" % entry.parent_id)
1036
877
 
1037
 
        if entry.name in parent.children:
 
878
        if parent.children.has_key(entry.name):
1038
879
            raise BzrError("%s is already versioned" %
1039
880
                    pathjoin(self.id2path(parent.file_id), entry.name))
1040
881
 
1042
883
        parent.children[entry.name] = entry
1043
884
        return entry
1044
885
 
1045
 
    def add_path(self, relpath, kind, file_id=None, parent_id=None):
 
886
 
 
887
    def add_path(self, relpath, kind, file_id=None):
1046
888
        """Add entry from a path.
1047
889
 
1048
890
        The immediate parent must already be versioned.
1049
891
 
1050
892
        Returns the new entry object."""
 
893
        from bzrlib.workingtree import gen_file_id
1051
894
        
1052
895
        parts = bzrlib.osutils.splitpath(relpath)
1053
 
 
1054
896
        if len(parts) == 0:
1055
 
            if file_id is None:
1056
 
                file_id = bzrlib.workingtree.gen_root_id()
1057
 
            self.root = RootEntry(file_id)
1058
 
            self._byid = {self.root.file_id: self.root}
1059
 
            return
 
897
            raise BzrError("cannot re-add root of inventory")
 
898
 
 
899
        if file_id == None:
 
900
            file_id = gen_file_id(relpath)
 
901
 
 
902
        parent_path = parts[:-1]
 
903
        parent_id = self.path2id(parent_path)
 
904
        if parent_id == None:
 
905
            raise NotVersionedError(path=parent_path)
 
906
        if kind == 'directory':
 
907
            ie = InventoryDirectory(file_id, parts[-1], parent_id)
 
908
        elif kind == 'file':
 
909
            ie = InventoryFile(file_id, parts[-1], parent_id)
 
910
        elif kind == 'symlink':
 
911
            ie = InventoryLink(file_id, parts[-1], parent_id)
1060
912
        else:
1061
 
            parent_path = parts[:-1]
1062
 
            parent_id = self.path2id(parent_path)
1063
 
            if parent_id is None:
1064
 
                raise NotVersionedError(path=parent_path)
1065
 
        ie = make_entry(kind, parts[-1], parent_id, file_id)
 
913
            raise BzrError("unknown kind %r" % kind)
1066
914
        return self.add(ie)
1067
915
 
 
916
 
1068
917
    def __delitem__(self, file_id):
1069
918
        """Remove entry by id.
1070
919
 
1079
928
        """
1080
929
        ie = self[file_id]
1081
930
 
1082
 
        assert ie.parent_id is None or \
1083
 
            self[ie.parent_id].children[ie.name] == ie
 
931
        assert self[ie.parent_id].children[ie.name] == ie
1084
932
        
 
933
        # TODO: Test deleting all children; maybe hoist to a separate
 
934
        # deltree method?
 
935
        if ie.kind == 'directory':
 
936
            for cie in ie.children.values():
 
937
                del self[cie.file_id]
 
938
            del ie.children
 
939
 
1085
940
        del self._byid[file_id]
1086
 
        if ie.parent_id is not None:
1087
 
            del self[ie.parent_id].children[ie.name]
 
941
        del self[ie.parent_id].children[ie.name]
 
942
 
1088
943
 
1089
944
    def __eq__(self, other):
1090
945
        """Compare two sets by comparing their contents.
1111
966
 
1112
967
        return self._byid == other._byid
1113
968
 
 
969
 
1114
970
    def __ne__(self, other):
1115
971
        return not self.__eq__(other)
1116
972
 
 
973
 
1117
974
    def __hash__(self):
1118
975
        raise ValueError('not hashable')
1119
976
 
1120
 
    def _iter_file_id_parents(self, file_id):
1121
 
        """Yield the parents of file_id up to the root."""
1122
 
        while file_id is not None:
1123
 
            try:
1124
 
                ie = self._byid[file_id]
1125
 
            except KeyError:
1126
 
                raise BzrError("file_id {%s} not found in inventory" % file_id)
1127
 
            yield ie
1128
 
            file_id = ie.parent_id
1129
977
 
1130
978
    def get_idpath(self, file_id):
1131
979
        """Return a list of file_ids for the path to an entry.
1136
984
        root directory as depth 1.
1137
985
        """
1138
986
        p = []
1139
 
        for parent in self._iter_file_id_parents(file_id):
1140
 
            p.insert(0, parent.file_id)
 
987
        while file_id != None:
 
988
            try:
 
989
                ie = self._byid[file_id]
 
990
            except KeyError:
 
991
                raise BzrError("file_id {%s} not found in inventory" % file_id)
 
992
            p.insert(0, ie.file_id)
 
993
            file_id = ie.parent_id
1141
994
        return p
1142
995
 
 
996
 
1143
997
    def id2path(self, file_id):
1144
 
        """Return as a string the path to file_id.
 
998
        """Return as a list the path to file_id.
1145
999
        
1146
1000
        >>> i = Inventory()
1147
1001
        >>> e = i.add(InventoryDirectory('src-id', 'src', ROOT_ID))
1150
1004
        src/foo.c
1151
1005
        """
1152
1006
        # get all names, skipping root
1153
 
        return '/'.join(reversed(
1154
 
            [parent.name for parent in 
1155
 
             self._iter_file_id_parents(file_id)][:-1]))
 
1007
        p = [self._byid[fid].name for fid in self.get_idpath(file_id)[1:]]
 
1008
        if p:
 
1009
            return pathjoin(*p)
 
1010
        else:
 
1011
            return ''
1156
1012
            
 
1013
 
 
1014
 
1157
1015
    def path2id(self, name):
1158
1016
        """Walk down through directories to return entry of last component.
1159
1017
 
1163
1021
        This returns the entry of the last component in the path,
1164
1022
        which may be either a file or a directory.
1165
1023
 
1166
 
        Returns None IFF the path is not found.
 
1024
        Returns None iff the path is not found.
1167
1025
        """
1168
1026
        if isinstance(name, types.StringTypes):
1169
1027
            name = splitpath(name)
1170
1028
 
1171
 
        # mutter("lookup path %r" % name)
 
1029
        mutter("lookup path %r" % name)
1172
1030
 
1173
1031
        parent = self.root
1174
1032
        for f in name:
1183
1041
 
1184
1042
        return parent.file_id
1185
1043
 
 
1044
 
1186
1045
    def has_filename(self, names):
1187
1046
        return bool(self.path2id(names))
1188
1047
 
 
1048
 
1189
1049
    def has_id(self, file_id):
1190
1050
        return self._byid.has_key(file_id)
1191
1051
 
 
1052
 
1192
1053
    def rename(self, file_id, new_parent_id, new_name):
1193
1054
        """Move a file within the inventory.
1194
1055
 
1219
1080
        file_ie.parent_id = new_parent_id
1220
1081
 
1221
1082
 
1222
 
def make_entry(kind, name, parent_id, file_id=None):
1223
 
    """Create an inventory entry.
1224
 
 
1225
 
    :param kind: the type of inventory entry to create.
1226
 
    :param name: the basename of the entry.
1227
 
    :param parent_id: the parent_id of the entry.
1228
 
    :param file_id: the file_id to use. if None, one will be created.
1229
 
    """
1230
 
    if file_id is None:
1231
 
        file_id = bzrlib.workingtree.gen_file_id(name)
1232
 
    if kind == 'directory':
1233
 
        return InventoryDirectory(file_id, name, parent_id)
1234
 
    elif kind == 'file':
1235
 
        return InventoryFile(file_id, name, parent_id)
1236
 
    elif kind == 'symlink':
1237
 
        return InventoryLink(file_id, name, parent_id)
1238
 
    else:
1239
 
        raise BzrError("unknown kind %r" % kind)
1240
 
 
1241
1083
 
1242
1084
 
1243
1085
_NAME_RE = None
1244
1086
 
1245
1087
def is_valid_name(name):
1246
1088
    global _NAME_RE
1247
 
    if _NAME_RE is None:
 
1089
    if _NAME_RE == None:
1248
1090
        _NAME_RE = re.compile(r'^[^/\\]+$')
1249
1091
        
1250
1092
    return bool(_NAME_RE.match(name))