~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

[merge] robert

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
 
18
import shutil
18
19
import sys
19
20
import os
20
21
import errno
33
34
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
34
35
                           NoSuchRevision, HistoryMissing, NotBranchError,
35
36
                           DivergedBranches, LockError, UnlistableStore,
36
 
                           UnlistableBranch, NoSuchFile, NotVersionedError)
 
37
                           UnlistableBranch, NoSuchFile, NotVersionedError,
 
38
                           NoWorkingTree)
37
39
from bzrlib.textui import show_status
38
40
from bzrlib.revision import (Revision, is_ancestor, get_intervening_revisions,
39
41
                             NULL_REVISION)
272
274
    def __str__(self):
273
275
        return '%s(%r)' % (self.__class__.__name__, self._transport.base)
274
276
 
275
 
 
276
277
    __repr__ = __str__
277
278
 
278
 
 
279
279
    def __del__(self):
280
280
        if self._lock_mode or self._lock:
281
281
            # XXX: This should show something every time, and be suitable for
291
291
        # should never expect their __del__ function to run.
292
292
        if hasattr(self, 'cache_root') and self.cache_root is not None:
293
293
            try:
294
 
                import shutil
295
294
                shutil.rmtree(self.cache_root)
296
295
            except:
297
296
                pass
515
514
 
516
515
    def get_root_id(self):
517
516
        """Return the id of this branches root"""
518
 
        inv = self.read_working_inventory()
 
517
        inv = self.get_inventory(self.last_revision())
519
518
        return inv.root.file_id
520
519
 
 
520
    @needs_write_lock
521
521
    def set_root_id(self, file_id):
522
 
        inv = self.read_working_inventory()
 
522
        inv = self.working_tree().read_working_inventory()
523
523
        orig_root_id = inv.root.file_id
524
524
        del inv._byid[inv.root.file_id]
525
525
        inv.root.file_id = file_id
530
530
                entry.parent_id = inv.root.file_id
531
531
        self._write_inventory(inv)
532
532
 
533
 
    @needs_read_lock
534
 
    def read_working_inventory(self):
535
 
        """Read the working inventory."""
536
 
        # ElementTree does its own conversion from UTF-8, so open in
537
 
        # binary.
538
 
        f = self.controlfile('inventory', 'rb')
539
 
        return bzrlib.xml5.serializer_v5.read_inventory(f)
540
 
 
541
533
    @needs_write_lock
542
534
    def _write_inventory(self, inv):
543
535
        """Update the working inventory.
554
546
        
555
547
        mutter('wrote working inventory')
556
548
            
557
 
    inventory = property(read_working_inventory, _write_inventory, None,
558
 
                         """Inventory for the working copy.""")
559
 
 
560
549
    @needs_write_lock
561
550
    def add(self, files, ids=None):
562
551
        """Make files versioned.
593
582
        else:
594
583
            assert(len(ids) == len(files))
595
584
 
596
 
        inv = self.read_working_inventory()
 
585
        inv = self.working_tree().read_working_inventory()
597
586
        for f,file_id in zip(files, ids):
598
587
            if is_control_file(f):
599
588
                raise BzrError("cannot add control file %s" % quotefn(f))
633
622
            raise BzrError("%r is not present in revision %s" % (file, revno))
634
623
        tree.print_file(file_id)
635
624
 
636
 
    # FIXME: this doesn't need to be a branch method
637
 
    def set_inventory(self, new_inventory_list):
638
 
        from bzrlib.inventory import Inventory, InventoryEntry
639
 
        inv = Inventory(self.get_root_id())
640
 
        for path, file_id, parent, kind in new_inventory_list:
641
 
            name = os.path.basename(path)
642
 
            if name == "":
643
 
                continue
644
 
            # fixme, there should be a factory function inv,add_?? 
645
 
            if kind == 'directory':
646
 
                inv.add(inventory.InventoryDirectory(file_id, name, parent))
647
 
            elif kind == 'file':
648
 
                inv.add(inventory.InventoryFile(file_id, name, parent))
649
 
            elif kind == 'symlink':
650
 
                inv.add(inventory.InventoryLink(file_id, name, parent))
651
 
            else:
652
 
                raise BzrError("unknown kind %r" % kind)
653
 
        self._write_inventory(inv)
654
 
 
655
625
    def unknowns(self):
656
626
        """Return all unknown files.
657
627
 
794
764
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
795
765
        # must be the same as its revision, so this is trivial.
796
766
        if revision_id == None:
797
 
            return Inventory(self.get_root_id())
 
767
            # This does not make sense: if there is no revision,
 
768
            # then it is the current tree inventory surely ?!
 
769
            # and thus get_root_id() is something that looks at the last
 
770
            # commit on the branch, and the get_root_id is an inventory check.
 
771
            raise NotImplementedError
 
772
            # return Inventory(self.get_root_id())
798
773
        else:
799
774
            return self.get_inventory(revision_id)
800
775
 
952
927
        # much more complex to keep consistent than our careful .bzr subset.
953
928
        # instead, we should say that working trees are local only, and optimise
954
929
        # for that.
 
930
        if self._transport.base.find('://') != -1:
 
931
            raise NoWorkingTree(self.base)
955
932
        return WorkingTree(self.base, branch=self)
956
933
 
 
934
    @needs_write_lock
 
935
    def pull(self, source, overwrite=False):
 
936
        source.lock_read()
 
937
        try:
 
938
            try:
 
939
                self.update_revisions(source)
 
940
            except DivergedBranches:
 
941
                if not overwrite:
 
942
                    raise
 
943
                self.set_revision_history(source.revision_history())
 
944
        finally:
 
945
            source.unlock()
957
946
 
958
947
    def basis_tree(self):
959
948
        """Return `Tree` object for last revision.
1083
1072
        from bzrlib.atomicfile import AtomicFile
1084
1073
        from bzrlib.osutils import backup_file
1085
1074
        
1086
 
        inv = self.read_working_inventory()
 
1075
        inv = self.working_tree().read_working_inventory()
1087
1076
        if old_tree is None:
1088
1077
            old_tree = self.basis_tree()
1089
1078
        old_inv = old_tree.inventory
1165
1154
                    raise
1166
1155
        return None
1167
1156
 
 
1157
    def get_push_location(self):
 
1158
        """Return the None or the location to push this branch to."""
 
1159
        config = bzrlib.config.BranchConfig(self)
 
1160
        push_loc = config.get_user_option('push_location')
 
1161
        return push_loc
 
1162
 
 
1163
    def set_push_location(self, location):
 
1164
        """Set a new push location for this branch."""
 
1165
        config = bzrlib.config.LocationConfig(self.base)
 
1166
        config.set_user_option('push_location', location)
 
1167
 
1168
1168
    @needs_write_lock
1169
1169
    def set_parent(self, url):
1170
1170
        # TODO: Maybe delete old location files?