~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

Merged Martin

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
52
52
from bzrlib.errors import BzrCheckError, DivergedBranches, NotVersionedError
53
53
from bzrlib.trace import mutter
 
54
import bzrlib.xml5
54
55
 
55
56
 
56
57
class TreeEntry(object):
134
135
            branch = Branch.open(basedir)
135
136
        assert isinstance(branch, Branch), \
136
137
            "branch %r is not a Branch" % branch
137
 
        self._inventory = branch.inventory
138
 
        self.path2id = self._inventory.path2id
139
138
        self.branch = branch
140
139
        self.basedir = basedir
 
140
        self._inventory = self.read_working_inventory()
 
141
        self.path2id = self._inventory.path2id
141
142
 
142
143
        # update the whole cache up front and write to disk if anything changed;
143
144
        # in the future we might want to do this more selectively
187
188
    def get_file_byname(self, filename):
188
189
        return file(self.abspath(filename), 'rb')
189
190
 
 
191
    def get_root_id(self):
 
192
        """Return the id of this trees root"""
 
193
        inv = self.read_working_inventory()
 
194
        return inv.root.file_id
 
195
        
190
196
    def _get_store_filename(self, file_id):
191
197
        ## XXX: badly named; this isn't in the store at all
192
198
        return self.abspath(self.id2path(file_id))
330
336
                yield stem
331
337
 
332
338
    @needs_write_lock
333
 
    def pull(self, source, remember=False, overwrite=False):
 
339
    def pull(self, source, overwrite=False):
334
340
        from bzrlib.merge import merge_inner
335
341
        source.lock_read()
336
342
        try:
337
343
            old_revision_history = self.branch.revision_history()
338
 
            try:
339
 
                self.branch.update_revisions(source)
340
 
            except DivergedBranches:
341
 
                if not overwrite:
342
 
                    raise
343
 
                self.branch.set_revision_history(source.revision_history())
 
344
            self.branch.pull(source, overwrite)
344
345
            new_revision_history = self.branch.revision_history()
345
346
            if new_revision_history != old_revision_history:
346
347
                if len(old_revision_history):
350
351
                merge_inner(self.branch,
351
352
                            self.branch.basis_tree(), 
352
353
                            self.branch.revision_tree(other_revision))
353
 
            if self.branch.get_parent() is None or remember:
354
 
                self.branch.set_parent(source.base)
355
354
        finally:
356
355
            source.unlock()
357
356
 
457
456
        """See Branch.lock_write, and WorkingTree.unlock."""
458
457
        return self.branch.lock_write()
459
458
 
 
459
    @needs_read_lock
 
460
    def read_working_inventory(self):
 
461
        """Read the working inventory."""
 
462
        # ElementTree does its own conversion from UTF-8, so open in
 
463
        # binary.
 
464
        f = self.branch.controlfile('inventory', 'rb')
 
465
        return bzrlib.xml5.serializer_v5.read_inventory(f)
 
466
 
460
467
    @needs_write_lock
461
468
    def remove(self, files, verbose=False):
462
469
        """Remove nominated files from the working inventory..
498
505
 
499
506
        self.branch._write_inventory(inv)
500
507
 
 
508
    @needs_write_lock
 
509
    def set_inventory(self, new_inventory_list):
 
510
        from bzrlib.inventory import (Inventory,
 
511
                                      InventoryDirectory,
 
512
                                      InventoryEntry,
 
513
                                      InventoryFile,
 
514
                                      InventoryLink)
 
515
        inv = Inventory(self.get_root_id())
 
516
        for path, file_id, parent, kind in new_inventory_list:
 
517
            name = os.path.basename(path)
 
518
            if name == "":
 
519
                continue
 
520
            # fixme, there should be a factory function inv,add_?? 
 
521
            if kind == 'directory':
 
522
                inv.add(InventoryDirectory(file_id, name, parent))
 
523
            elif kind == 'file':
 
524
                inv.add(InventoryFile(file_id, name, parent))
 
525
            elif kind == 'symlink':
 
526
                inv.add(InventoryLink(file_id, name, parent))
 
527
            else:
 
528
                raise BzrError("unknown kind %r" % kind)
 
529
        self.branch._write_inventory(inv)
 
530
 
501
531
    def unlock(self):
502
532
        """See Branch.unlock.
503
533