~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

Merged Martin

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)
285
287
    def __str__(self):
286
288
        return '%s(%r)' % (self.__class__.__name__, self._transport.base)
287
289
 
288
 
 
289
290
    __repr__ = __str__
290
291
 
291
 
 
292
292
    def __del__(self):
293
293
        if self._lock_mode or self._lock:
294
294
            # XXX: This should show something every time, and be suitable for
304
304
        # should never expect their __del__ function to run.
305
305
        if hasattr(self, 'cache_root') and self.cache_root is not None:
306
306
            try:
307
 
                import shutil
308
307
                shutil.rmtree(self.cache_root)
309
308
            except:
310
309
                pass
528
527
 
529
528
    def get_root_id(self):
530
529
        """Return the id of this branches root"""
531
 
        inv = self.read_working_inventory()
 
530
        inv = self.get_inventory(self.last_revision())
532
531
        return inv.root.file_id
533
532
 
 
533
    @needs_write_lock
534
534
    def set_root_id(self, file_id):
535
 
        inv = self.read_working_inventory()
 
535
        inv = self.working_tree().read_working_inventory()
536
536
        orig_root_id = inv.root.file_id
537
537
        del inv._byid[inv.root.file_id]
538
538
        inv.root.file_id = file_id
543
543
                entry.parent_id = inv.root.file_id
544
544
        self._write_inventory(inv)
545
545
 
546
 
    @needs_read_lock
547
 
    def read_working_inventory(self):
548
 
        """Read the working inventory."""
549
 
        # ElementTree does its own conversion from UTF-8, so open in
550
 
        # binary.
551
 
        f = self.controlfile('inventory', 'rb')
552
 
        return bzrlib.xml5.serializer_v5.read_inventory(f)
553
 
 
554
546
    @needs_write_lock
555
547
    def _write_inventory(self, inv):
556
548
        """Update the working inventory.
567
559
        
568
560
        mutter('wrote working inventory')
569
561
            
570
 
    inventory = property(read_working_inventory, _write_inventory, None,
571
 
                         """Inventory for the working copy.""")
572
 
 
573
562
    @needs_write_lock
574
563
    def add(self, files, ids=None):
575
564
        """Make files versioned.
606
595
        else:
607
596
            assert(len(ids) == len(files))
608
597
 
609
 
        inv = self.read_working_inventory()
 
598
        inv = self.working_tree().read_working_inventory()
610
599
        for f,file_id in zip(files, ids):
611
600
            if is_control_file(f):
612
601
                raise BzrError("cannot add control file %s" % quotefn(f))
646
635
            raise BzrError("%r is not present in revision %s" % (file, revno))
647
636
        tree.print_file(file_id)
648
637
 
649
 
    # FIXME: this doesn't need to be a branch method
650
 
    def set_inventory(self, new_inventory_list):
651
 
        from bzrlib.inventory import Inventory, InventoryEntry
652
 
        inv = Inventory(self.get_root_id())
653
 
        for path, file_id, parent, kind in new_inventory_list:
654
 
            name = os.path.basename(path)
655
 
            if name == "":
656
 
                continue
657
 
            # fixme, there should be a factory function inv,add_?? 
658
 
            if kind == 'directory':
659
 
                inv.add(inventory.InventoryDirectory(file_id, name, parent))
660
 
            elif kind == 'file':
661
 
                inv.add(inventory.InventoryFile(file_id, name, parent))
662
 
            elif kind == 'symlink':
663
 
                inv.add(inventory.InventoryLink(file_id, name, parent))
664
 
            else:
665
 
                raise BzrError("unknown kind %r" % kind)
666
 
        self._write_inventory(inv)
667
 
 
668
638
    def unknowns(self):
669
639
        """Return all unknown files.
670
640
 
807
777
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
808
778
        # must be the same as its revision, so this is trivial.
809
779
        if revision_id == None:
810
 
            return Inventory(self.get_root_id())
 
780
            # This does not make sense: if there is no revision,
 
781
            # then it is the current tree inventory surely ?!
 
782
            # and thus get_root_id() is something that looks at the last
 
783
            # commit on the branch, and the get_root_id is an inventory check.
 
784
            raise NotImplementedError
 
785
            # return Inventory(self.get_root_id())
811
786
        else:
812
787
            return self.get_inventory(revision_id)
813
788
 
965
940
        # much more complex to keep consistent than our careful .bzr subset.
966
941
        # instead, we should say that working trees are local only, and optimise
967
942
        # for that.
 
943
        if self._transport.base.find('://') != -1:
 
944
            raise NoWorkingTree(self.base)
968
945
        return WorkingTree(self.base, branch=self)
969
946
 
 
947
    @needs_write_lock
 
948
    def pull(self, source, overwrite=False):
 
949
        source.lock_read()
 
950
        try:
 
951
            try:
 
952
                self.update_revisions(source)
 
953
            except DivergedBranches:
 
954
                if not overwrite:
 
955
                    raise
 
956
                self.set_revision_history(source.revision_history())
 
957
        finally:
 
958
            source.unlock()
970
959
 
971
960
    def basis_tree(self):
972
961
        """Return `Tree` object for last revision.
1096
1085
        from bzrlib.atomicfile import AtomicFile
1097
1086
        from bzrlib.osutils import backup_file
1098
1087
        
1099
 
        inv = self.read_working_inventory()
 
1088
        inv = self.working_tree().read_working_inventory()
1100
1089
        if old_tree is None:
1101
1090
            old_tree = self.basis_tree()
1102
1091
        old_inv = old_tree.inventory
1178
1167
                    raise
1179
1168
        return None
1180
1169
 
 
1170
    def get_push_location(self):
 
1171
        """Return the None or the location to push this branch to."""
 
1172
        config = bzrlib.config.BranchConfig(self)
 
1173
        push_loc = config.get_user_option('push_location')
 
1174
        return push_loc
 
1175
 
 
1176
    def set_push_location(self, location):
 
1177
        """Set a new push location for this branch."""
 
1178
        config = bzrlib.config.LocationConfig(self.base)
 
1179
        config.set_user_option('push_location', location)
 
1180
 
1181
1181
    @needs_write_lock
1182
1182
    def set_parent(self, url):
1183
1183
        # TODO: Maybe delete old location files?