~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

Hacking notes on TDD

Show diffs side-by-side

added added

removed removed

Lines of Context:
236
236
    def set_root_id(self, file_id):
237
237
        raise NotImplementedError('set_root_id is abstract')
238
238
 
239
 
    def print_file(self, file, revision_id):
 
239
    def print_file(self, file, revno):
240
240
        """Print `file` to stdout."""
241
241
        raise NotImplementedError('print_file is abstract')
242
242
 
355
355
        >>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
356
356
        >>> br1.missing_revisions(br2)
357
357
        Traceback (most recent call last):
358
 
        DivergedBranches: These branches have diverged.  Try merge.
 
358
        DivergedBranches: These branches have diverged.
359
359
        """
360
360
        self_history = self.revision_history()
361
361
        self_len = len(self_history)
568
568
            # or entirely uncompressed is tidy, but breaks upgrade from 
569
569
            # some existing branches where there's a mixture; we probably 
570
570
            # still want the option to look for both.
571
 
            relpath = self._rel_controlfilename(unicode(name))
 
571
            relpath = self._rel_controlfilename(name)
572
572
            store = TextStore(self._transport.clone(relpath),
573
573
                              prefixed=prefixed,
574
574
                              compressed=compressed)
577
577
            #    os.mkdir(cache_path)
578
578
            #    store = bzrlib.store.CachedStore(store, cache_path)
579
579
            return store
580
 
 
581
580
        def get_weave(name, prefixed=False):
582
 
            relpath = self._rel_controlfilename(unicode(name))
 
581
            relpath = self._rel_controlfilename(name)
583
582
            ws = WeaveStore(self._transport.clone(relpath), prefixed=prefixed)
584
583
            if self._transport.should_cache():
585
584
                ws.enable_cache = True
590
589
            self.text_store = get_store('text-store')
591
590
            self.revision_store = get_store('revision-store')
592
591
        elif self._branch_format == 5:
593
 
            self.control_weaves = get_weave(u'')
594
 
            self.weave_store = get_weave(u'weaves')
595
 
            self.revision_store = get_store(u'revision-store', compressed=False)
 
592
            self.control_weaves = get_weave('')
 
593
            self.weave_store = get_weave('weaves')
 
594
            self.revision_store = get_store('revision-store', compressed=False)
596
595
        elif self._branch_format == 6:
597
 
            self.control_weaves = get_weave(u'')
598
 
            self.weave_store = get_weave(u'weaves', prefixed=True)
599
 
            self.revision_store = get_store(u'revision-store', compressed=False,
 
596
            self.control_weaves = get_weave('')
 
597
            self.weave_store = get_weave('weaves', prefixed=True)
 
598
            self.revision_store = get_store('revision-store', compressed=False,
600
599
                                            prefixed=True)
601
600
        self.revision_store.register_suffix('sig')
602
601
        self._transaction = None
657
656
        self._transaction = new_transaction
658
657
 
659
658
    def lock_write(self):
660
 
        #mutter("lock write: %s (%s)", self, self._lock_count)
 
659
        mutter("lock write: %s (%s)", self, self._lock_count)
661
660
        # TODO: Upgrade locking to support using a Transport,
662
661
        # and potentially a remote locking protocol
663
662
        if self._lock_mode:
673
672
            self._set_transaction(transactions.PassThroughTransaction())
674
673
 
675
674
    def lock_read(self):
676
 
        #mutter("lock read: %s (%s)", self, self._lock_count)
 
675
        mutter("lock read: %s (%s)", self, self._lock_count)
677
676
        if self._lock_mode:
678
677
            assert self._lock_mode in ('r', 'w'), \
679
678
                   "invalid lock mode %r" % self._lock_mode
688
687
            self.get_transaction().set_cache_size(5000)
689
688
                        
690
689
    def unlock(self):
691
 
        #mutter("unlock: %s (%s)", self, self._lock_count)
 
690
        mutter("unlock: %s (%s)", self, self._lock_count)
692
691
        if not self._lock_mode:
693
692
            raise LockError('branch %r is not locked' % (self))
694
693
 
706
705
 
707
706
    def _rel_controlfilename(self, file_or_path):
708
707
        if not isinstance(file_or_path, basestring):
709
 
            file_or_path = u'/'.join(file_or_path)
 
708
            file_or_path = '/'.join(file_or_path)
710
709
        if file_or_path == '':
711
710
            return bzrlib.BZRDIR
712
 
        return bzrlib.transport.urlescape(bzrlib.BZRDIR + u'/' + file_or_path)
 
711
        return bzrlib.transport.urlescape(bzrlib.BZRDIR + '/' + file_or_path)
713
712
 
714
713
    def controlfilename(self, file_or_path):
715
714
        """See Branch.controlfilename."""
825
824
        return inv.root.file_id
826
825
 
827
826
    @needs_read_lock
828
 
    def print_file(self, file, revision_id):
 
827
    def print_file(self, file, revno):
829
828
        """See Branch.print_file."""
830
 
        tree = self.revision_tree(revision_id)
 
829
        tree = self.revision_tree(self.get_rev_id(revno))
831
830
        # use inventory as it was in that revision
832
831
        file_id = tree.inventory.path2id(file)
833
832
        if not file_id:
834
 
            try:
835
 
                revno = self.revision_id_to_revno(revision_id)
836
 
            except errors.NoSuchRevision:
837
 
                # TODO: This should not be BzrError,
838
 
                # but NoSuchFile doesn't fit either
839
 
                raise BzrError('%r is not present in revision %s' 
840
 
                                % (file, revision_id))
841
 
            else:
842
 
                raise BzrError('%r is not present in revision %s'
843
 
                                % (file, revno))
 
833
            raise BzrError("%r is not present in revision %s" % (file, revno))
844
834
        tree.print_file(file_id)
845
835
 
846
836
    @needs_write_lock
855
845
    @needs_write_lock
856
846
    def set_revision_history(self, rev_history):
857
847
        """See Branch.set_revision_history."""
858
 
        old_revision = self.last_revision()
859
 
        new_revision = rev_history[-1]
860
848
        self.put_controlfile('revision-history', '\n'.join(rev_history))
861
 
        try:
862
 
            self.working_tree().set_last_revision(new_revision, old_revision)
863
 
        except NoWorkingTree:
864
 
            mutter('Unable to set_last_revision without a working tree.')
865
849
 
866
850
    def has_revision(self, revision_id):
867
851
        """See Branch.has_revision."""
1008
992
            inv = self.get_revision_inventory(revision_id)
1009
993
            return RevisionTree(self.weave_store, inv, revision_id)
1010
994
 
1011
 
    def basis_tree(self):
1012
 
        """See Branch.basis_tree."""
1013
 
        try:
1014
 
            revision_id = self.revision_history()[-1]
1015
 
            xml = self.working_tree().read_basis_inventory(revision_id)
1016
 
            inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
1017
 
            return RevisionTree(self.weave_store, inv, revision_id)
1018
 
        except (IndexError, NoSuchFile, NoWorkingTree), e:
1019
 
            return self.revision_tree(self.last_revision())
1020
 
 
1021
995
    def working_tree(self):
1022
996
        """See Branch.working_tree."""
1023
997
        from bzrlib.workingtree import WorkingTree
1036
1010
            except DivergedBranches:
1037
1011
                if not overwrite:
1038
1012
                    raise
1039
 
            if overwrite:
1040
1013
                self.set_revision_history(source.revision_history())
1041
1014
            new_count = len(self.revision_history())
1042
1015
            return new_count - old_count
1050
1023
        for l in _locs:
1051
1024
            try:
1052
1025
                return self.controlfile(l, 'r').read().strip('\n')
1053
 
            except NoSuchFile:
1054
 
                pass
 
1026
            except IOError, e:
 
1027
                if e.errno != errno.ENOENT:
 
1028
                    raise
1055
1029
        return None
1056
1030
 
1057
1031
    def get_push_location(self):