~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2010-01-12 01:44:13 UTC
  • mto: (4634.119.3 2.0)
  • mto: This revision was merged to the branch mainline in revision 4951.
  • Revision ID: mbp@sourcefrog.net-20100112014413-uw90vrssc3trlzmt
Refuse to build with pyrex 0.9.4*

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
        symbol_versioning,
36
36
        transport,
37
37
        tsort,
 
38
        ui,
38
39
        urlutils,
39
40
        )
40
41
from bzrlib.config import BranchConfig, TransportConfig
148
149
        if self._partial_revision_history_cache[-1] == _mod_revision.NULL_REVISION:
149
150
            self._partial_revision_history_cache.pop()
150
151
 
 
152
    def _get_check_refs(self):
 
153
        """Get the references needed for check().
 
154
 
 
155
        See bzrlib.check.
 
156
        """
 
157
        revid = self.last_revision()
 
158
        return [('revision-existence', revid), ('lefthand-distance', revid)]
 
159
 
151
160
    @staticmethod
152
161
    def open(base, _unsupported=False, possible_transports=None):
153
162
        """Open the branch rooted at base.
437
446
        # start_revision_id.
438
447
        if self._merge_sorted_revisions_cache is None:
439
448
            last_revision = self.last_revision()
440
 
            graph = self.repository.get_graph()
441
 
            parent_map = dict(((key, value) for key, value in
442
 
                     graph.iter_ancestry([last_revision]) if value is not None))
443
 
            revision_graph = repository._strip_NULL_ghosts(parent_map)
444
 
            revs = tsort.merge_sort(revision_graph, last_revision, None,
445
 
                generate_revno=True)
446
 
            # Drop the sequence # before caching
447
 
            self._merge_sorted_revisions_cache = [r[1:] for r in revs]
448
 
 
 
449
            last_key = (last_revision,)
 
450
            known_graph = self.repository.revisions.get_known_graph_ancestry(
 
451
                [last_key])
 
452
            self._merge_sorted_revisions_cache = known_graph.merge_sort(
 
453
                last_key)
449
454
        filtered = self._filter_merge_sorted_revisions(
450
455
            self._merge_sorted_revisions_cache, start_revision_id,
451
456
            stop_revision_id, stop_rule)
461
466
        """Iterate over an inclusive range of sorted revisions."""
462
467
        rev_iter = iter(merge_sorted_revisions)
463
468
        if start_revision_id is not None:
464
 
            for rev_id, depth, revno, end_of_merge in rev_iter:
 
469
            for node in rev_iter:
 
470
                rev_id = node.key[-1]
465
471
                if rev_id != start_revision_id:
466
472
                    continue
467
473
                else:
468
474
                    # The decision to include the start or not
469
475
                    # depends on the stop_rule if a stop is provided
470
 
                    rev_iter = chain(
471
 
                        iter([(rev_id, depth, revno, end_of_merge)]),
472
 
                        rev_iter)
 
476
                    # so pop this node back into the iterator
 
477
                    rev_iter = chain(iter([node]), rev_iter)
473
478
                    break
474
479
        if stop_revision_id is None:
475
 
            for rev_id, depth, revno, end_of_merge in rev_iter:
476
 
                yield rev_id, depth, revno, end_of_merge
 
480
            # Yield everything
 
481
            for node in rev_iter:
 
482
                rev_id = node.key[-1]
 
483
                yield (rev_id, node.merge_depth, node.revno,
 
484
                       node.end_of_merge)
477
485
        elif stop_rule == 'exclude':
478
 
            for rev_id, depth, revno, end_of_merge in rev_iter:
 
486
            for node in rev_iter:
 
487
                rev_id = node.key[-1]
479
488
                if rev_id == stop_revision_id:
480
489
                    return
481
 
                yield rev_id, depth, revno, end_of_merge
 
490
                yield (rev_id, node.merge_depth, node.revno,
 
491
                       node.end_of_merge)
482
492
        elif stop_rule == 'include':
483
 
            for rev_id, depth, revno, end_of_merge in rev_iter:
484
 
                yield rev_id, depth, revno, end_of_merge
 
493
            for node in rev_iter:
 
494
                rev_id = node.key[-1]
 
495
                yield (rev_id, node.merge_depth, node.revno,
 
496
                       node.end_of_merge)
485
497
                if rev_id == stop_revision_id:
486
498
                    return
487
499
        elif stop_rule == 'with-merges':
490
502
                left_parent = stop_rev.parent_ids[0]
491
503
            else:
492
504
                left_parent = _mod_revision.NULL_REVISION
493
 
            for rev_id, depth, revno, end_of_merge in rev_iter:
 
505
            for node in rev_iter:
 
506
                rev_id = node.key[-1]
494
507
                if rev_id == left_parent:
495
508
                    return
496
 
                yield rev_id, depth, revno, end_of_merge
 
509
                yield (rev_id, node.merge_depth, node.revno,
 
510
                       node.end_of_merge)
497
511
        else:
498
512
            raise ValueError('invalid stop_rule %r' % stop_rule)
499
513
 
662
676
        """
663
677
        if not self._format.supports_stacking():
664
678
            raise errors.UnstackableBranchFormat(self._format, self.base)
 
679
        # XXX: Changing from one fallback repository to another does not check
 
680
        # that all the data you need is present in the new fallback.
 
681
        # Possibly it should.
665
682
        self._check_stackable_repo()
666
683
        if not url:
667
684
            try:
669
686
            except (errors.NotStacked, errors.UnstackableBranchFormat,
670
687
                errors.UnstackableRepositoryFormat):
671
688
                return
672
 
            url = ''
673
 
            # XXX: Lock correctness - should unlock our old repo if we were
674
 
            # locked.
675
 
            # repositories don't offer an interface to remove fallback
676
 
            # repositories today; take the conceptually simpler option and just
677
 
            # reopen it.
678
 
            self.repository = self.bzrdir.find_repository()
679
 
            self.repository.lock_write()
680
 
            # for every revision reference the branch has, ensure it is pulled
681
 
            # in.
682
 
            source_repository = self._get_fallback_repository(old_url)
683
 
            for revision_id in chain([self.last_revision()],
684
 
                self.tags.get_reverse_tag_dict()):
685
 
                self.repository.fetch(source_repository, revision_id,
686
 
                    find_ghosts=True)
 
689
            self._unstack()
687
690
        else:
688
691
            self._activate_fallback_location(url)
689
692
        # write this out after the repository is stacked to avoid setting a
690
693
        # stacked config that doesn't work.
691
694
        self._set_config_location('stacked_on_location', url)
692
695
 
 
696
    def _unstack(self):
 
697
        """Change a branch to be unstacked, copying data as needed.
 
698
        
 
699
        Don't call this directly, use set_stacked_on_url(None).
 
700
        """
 
701
        pb = ui.ui_factory.nested_progress_bar()
 
702
        try:
 
703
            pb.update("Unstacking")
 
704
            # The basic approach here is to fetch the tip of the branch,
 
705
            # including all available ghosts, from the existing stacked
 
706
            # repository into a new repository object without the fallbacks. 
 
707
            #
 
708
            # XXX: See <https://launchpad.net/bugs/397286> - this may not be
 
709
            # correct for CHKMap repostiories
 
710
            old_repository = self.repository
 
711
            if len(old_repository._fallback_repositories) != 1:
 
712
                raise AssertionError("can't cope with fallback repositories "
 
713
                    "of %r" % (self.repository,))
 
714
            # unlock it, including unlocking the fallback
 
715
            old_repository.unlock()
 
716
            old_repository.lock_read()
 
717
            try:
 
718
                # Repositories don't offer an interface to remove fallback
 
719
                # repositories today; take the conceptually simpler option and just
 
720
                # reopen it.  We reopen it starting from the URL so that we
 
721
                # get a separate connection for RemoteRepositories and can
 
722
                # stream from one of them to the other.  This does mean doing
 
723
                # separate SSH connection setup, but unstacking is not a
 
724
                # common operation so it's tolerable.
 
725
                new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
 
726
                new_repository = new_bzrdir.find_repository()
 
727
                self.repository = new_repository
 
728
                if self.repository._fallback_repositories:
 
729
                    raise AssertionError("didn't expect %r to have "
 
730
                        "fallback_repositories"
 
731
                        % (self.repository,))
 
732
                # this is not paired with an unlock because it's just restoring
 
733
                # the previous state; the lock's released when set_stacked_on_url
 
734
                # returns
 
735
                self.repository.lock_write()
 
736
                # XXX: If you unstack a branch while it has a working tree
 
737
                # with a pending merge, the pending-merged revisions will no
 
738
                # longer be present.  You can (probably) revert and remerge.
 
739
                #
 
740
                # XXX: This only fetches up to the tip of the repository; it
 
741
                # doesn't bring across any tags.  That's fairly consistent
 
742
                # with how branch works, but perhaps not ideal.
 
743
                self.repository.fetch(old_repository,
 
744
                    revision_id=self.last_revision(),
 
745
                    find_ghosts=True)
 
746
            finally:
 
747
                old_repository.unlock()
 
748
        finally:
 
749
            pb.finished()
693
750
 
694
751
    def _set_tags_bytes(self, bytes):
695
752
        """Mirror method for _get_tags_bytes.
1095
1152
        revision_id: if not None, the revision history in the new branch will
1096
1153
                     be truncated to end with revision_id.
1097
1154
        """
 
1155
        if (repository_policy is not None and
 
1156
            repository_policy.requires_stacking()):
 
1157
            to_bzrdir._format.require_stacking(_skip_repo=True)
1098
1158
        result = to_bzrdir.create_branch()
1099
1159
        result.lock_write()
1100
1160
        try:
1168
1228
        target._set_all_reference_info(target_reference_dict)
1169
1229
 
1170
1230
    @needs_read_lock
1171
 
    def check(self):
 
1231
    def check(self, refs):
1172
1232
        """Check consistency of the branch.
1173
1233
 
1174
1234
        In particular this checks that revisions given in the revision-history
1177
1237
 
1178
1238
        Callers will typically also want to check the repository.
1179
1239
 
 
1240
        :param refs: Calculated refs for this branch as specified by
 
1241
            branch._get_check_refs()
1180
1242
        :return: A BranchCheckResult.
1181
1243
        """
1182
 
        ret = BranchCheckResult(self)
1183
 
        mainline_parent_id = None
 
1244
        result = BranchCheckResult(self)
1184
1245
        last_revno, last_revision_id = self.last_revision_info()
1185
 
        real_rev_history = []
1186
 
        try:
1187
 
            for revid in self.repository.iter_reverse_revision_history(
1188
 
                last_revision_id):
1189
 
                real_rev_history.append(revid)
1190
 
        except errors.RevisionNotPresent:
1191
 
            ret.ghosts_in_mainline = True
1192
 
        else:
1193
 
            ret.ghosts_in_mainline = False
1194
 
        real_rev_history.reverse()
1195
 
        if len(real_rev_history) != last_revno:
1196
 
            raise errors.BzrCheckError('revno does not match len(mainline)'
1197
 
                ' %s != %s' % (last_revno, len(real_rev_history)))
1198
 
        # TODO: We should probably also check that real_rev_history actually
1199
 
        #       matches self.revision_history()
1200
 
        for revision_id in real_rev_history:
1201
 
            try:
1202
 
                revision = self.repository.get_revision(revision_id)
1203
 
            except errors.NoSuchRevision, e:
1204
 
                raise errors.BzrCheckError("mainline revision {%s} not in repository"
1205
 
                            % revision_id)
1206
 
            # In general the first entry on the revision history has no parents.
1207
 
            # But it's not illegal for it to have parents listed; this can happen
1208
 
            # in imports from Arch when the parents weren't reachable.
1209
 
            if mainline_parent_id is not None:
1210
 
                if mainline_parent_id not in revision.parent_ids:
1211
 
                    raise errors.BzrCheckError("previous revision {%s} not listed among "
1212
 
                                        "parents of {%s}"
1213
 
                                        % (mainline_parent_id, revision_id))
1214
 
            mainline_parent_id = revision_id
1215
 
        return ret
 
1246
        actual_revno = refs[('lefthand-distance', last_revision_id)]
 
1247
        if actual_revno != last_revno:
 
1248
            result.errors.append(errors.BzrCheckError(
 
1249
                'revno does not match len(mainline) %s != %s' % (
 
1250
                last_revno, actual_revno)))
 
1251
        # TODO: We should probably also check that self.revision_history
 
1252
        # matches the repository for older branch formats.
 
1253
        # If looking for the code that cross-checks repository parents against
 
1254
        # the iter_reverse_revision_history output, that is now a repository
 
1255
        # specific check.
 
1256
        return result
1216
1257
 
1217
1258
    def _get_checkout_format(self):
1218
1259
        """Return the most suitable metadir for a checkout of this branch.
1243
1284
        # clone call. Or something. 20090224 RBC/spiv.
1244
1285
        if revision_id is None:
1245
1286
            revision_id = self.last_revision()
1246
 
        try:
1247
 
            dir_to = self.bzrdir.clone_on_transport(to_transport,
1248
 
                revision_id=revision_id, stacked_on=stacked_on,
1249
 
                create_prefix=create_prefix, use_existing_dir=use_existing_dir)
1250
 
        except errors.FileExists:
1251
 
            if not use_existing_dir:
1252
 
                raise
1253
 
        except errors.NoSuchFile:
1254
 
            if not create_prefix:
1255
 
                raise
 
1287
        dir_to = self.bzrdir.clone_on_transport(to_transport,
 
1288
            revision_id=revision_id, stacked_on=stacked_on,
 
1289
            create_prefix=create_prefix, use_existing_dir=use_existing_dir)
1256
1290
        return dir_to.open_branch()
1257
1291
 
1258
1292
    def create_checkout(self, to_location, revision_id=None,
2031
2065
BranchFormat.register_format(__format6)
2032
2066
BranchFormat.register_format(__format7)
2033
2067
BranchFormat.register_format(__format8)
2034
 
BranchFormat.set_default_format(__format6)
 
2068
BranchFormat.set_default_format(__format7)
2035
2069
_legacy_formats = [BzrBranchFormat4(),
2036
2070
    ]
2037
2071
network_format_registry.register(
2795
2829
 
2796
2830
    def __init__(self, branch):
2797
2831
        self.branch = branch
2798
 
        self.ghosts_in_mainline = False
 
2832
        self.errors = []
2799
2833
 
2800
2834
    def report_results(self, verbose):
2801
2835
        """Report the check results via trace.note.
2803
2837
        :param verbose: Requests more detailed display of what was checked,
2804
2838
            if any.
2805
2839
        """
2806
 
        note('checked branch %s format %s',
2807
 
             self.branch.base,
2808
 
             self.branch._format)
2809
 
        if self.ghosts_in_mainline:
2810
 
            note('branch contains ghosts in mainline')
 
2840
        note('checked branch %s format %s', self.branch.base,
 
2841
            self.branch._format)
 
2842
        for error in self.errors:
 
2843
            note('found error:%s', error)
2811
2844
 
2812
2845
 
2813
2846
class Converter5to6(object):