~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-01 06:34:34 UTC
  • mfrom: (4226.1.4 Branch.set_stacked_on_url)
  • Revision ID: pqm@pqm.ubuntu.com-20090401063434-motksin95y4undi6
(robertc) Lift up Branch7.set_stacked_on_url to Branch,
        allowing reuse by RemoteBranch and decreasing round trips for pushing
        new stacked branches. (Robert Collins, Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
    def _open_hook(self):
100
100
        """Called by init to allow simpler extension of the base class."""
101
101
 
 
102
    def _activate_fallback_location(self, url):
 
103
        """Activate the branch/repository from url as a fallback repository."""
 
104
        self.repository.add_fallback_repository(
 
105
            self._get_fallback_repository(url))
 
106
 
102
107
    def break_lock(self):
103
108
        """Break a lock if one is present from another instance.
104
109
 
113
118
        if master is not None:
114
119
            master.break_lock()
115
120
 
 
121
    def _check_stackable_repo(self):
 
122
        if not self.repository._format.supports_external_lookups:
 
123
            raise errors.UnstackableRepositoryFormat(self.repository._format,
 
124
                self.repository.base)
 
125
 
116
126
    @staticmethod
117
127
    def open(base, _unsupported=False, possible_transports=None):
118
128
        """Open the branch rooted at base.
157
167
    def get_config(self):
158
168
        return BranchConfig(self)
159
169
 
 
170
    def _get_fallback_repository(self, url):
 
171
        """Get the repository we fallback to at url."""
 
172
        url = urlutils.join(self.base, url)
 
173
        a_bzrdir = bzrdir.BzrDir.open(url,
 
174
            possible_transports=[self.bzrdir.root_transport])
 
175
        return a_bzrdir.open_branch().repository
 
176
 
160
177
    def _get_tags_bytes(self):
161
178
        """Get the bytes of a serialised tags dict.
162
179
 
569
586
        :raises UnstackableRepositoryFormat: If the repository does not support
570
587
            stacking.
571
588
        """
572
 
        raise NotImplementedError(self.set_stacked_on_url)
 
589
        if not self._format.supports_stacking():
 
590
            raise errors.UnstackableBranchFormat(self._format, self.base)
 
591
        self._check_stackable_repo()
 
592
        if not url:
 
593
            try:
 
594
                old_url = self.get_stacked_on_url()
 
595
            except (errors.NotStacked, errors.UnstackableBranchFormat,
 
596
                errors.UnstackableRepositoryFormat):
 
597
                return
 
598
            url = ''
 
599
            # repositories don't offer an interface to remove fallback
 
600
            # repositories today; take the conceptually simpler option and just
 
601
            # reopen it.
 
602
            self.repository = self.bzrdir.find_repository()
 
603
            # for every revision reference the branch has, ensure it is pulled
 
604
            # in.
 
605
            source_repository = self._get_fallback_repository(old_url)
 
606
            for revision_id in chain([self.last_revision()],
 
607
                self.tags.get_reverse_tag_dict()):
 
608
                self.repository.fetch(source_repository, revision_id,
 
609
                    find_ghosts=True)
 
610
        else:
 
611
            self._activate_fallback_location(url)
 
612
        # write this out after the repository is stacked to avoid setting a
 
613
        # stacked config that doesn't work.
 
614
        self._set_config_location('stacked_on_location', url)
 
615
 
573
616
 
574
617
    def _set_tags_bytes(self, bytes):
575
618
        """Mirror method for _get_tags_bytes.
2161
2204
            self._transport.put_bytes('parent', url + '\n',
2162
2205
                mode=self.bzrdir._get_file_mode())
2163
2206
 
2164
 
    def set_stacked_on_url(self, url):
2165
 
        raise errors.UnstackableBranchFormat(self._format, self.base)
2166
 
 
2167
2207
 
2168
2208
class BzrBranch5(BzrBranch):
2169
2209
    """A format 5 branch. This supports new features over plain branches.
2295
2335
class BzrBranch7(BzrBranch5):
2296
2336
    """A branch with support for a fallback repository."""
2297
2337
 
2298
 
    def _get_fallback_repository(self, url):
2299
 
        """Get the repository we fallback to at url."""
2300
 
        url = urlutils.join(self.base, url)
2301
 
        a_bzrdir = bzrdir.BzrDir.open(url,
2302
 
                                      possible_transports=[self._transport])
2303
 
        return a_bzrdir.open_branch().repository
2304
 
 
2305
 
    def _activate_fallback_location(self, url):
2306
 
        """Activate the branch/repository from url as a fallback repository."""
2307
 
        self.repository.add_fallback_repository(
2308
 
            self._get_fallback_repository(url))
2309
 
 
2310
2338
    def _open_hook(self):
2311
2339
        if self._ignore_fallbacks:
2312
2340
            return
2325
2353
                        "None, not a URL." % hook_name)
2326
2354
            self._activate_fallback_location(url)
2327
2355
 
2328
 
    def _check_stackable_repo(self):
2329
 
        if not self.repository._format.supports_external_lookups:
2330
 
            raise errors.UnstackableRepositoryFormat(self.repository._format,
2331
 
                self.repository.base)
2332
 
 
2333
2356
    def __init__(self, *args, **kwargs):
2334
2357
        self._ignore_fallbacks = kwargs.get('ignore_fallbacks', False)
2335
2358
        super(BzrBranch7, self).__init__(*args, **kwargs)
2510
2533
        self.get_config().set_user_option('append_revisions_only', value,
2511
2534
            warn_masked=True)
2512
2535
 
2513
 
    def set_stacked_on_url(self, url):
2514
 
        self._check_stackable_repo()
2515
 
        if not url:
2516
 
            try:
2517
 
                old_url = self.get_stacked_on_url()
2518
 
            except (errors.NotStacked, errors.UnstackableBranchFormat,
2519
 
                errors.UnstackableRepositoryFormat):
2520
 
                return
2521
 
            url = ''
2522
 
            # repositories don't offer an interface to remove fallback
2523
 
            # repositories today; take the conceptually simpler option and just
2524
 
            # reopen it.
2525
 
            self.repository = self.bzrdir.find_repository()
2526
 
            # for every revision reference the branch has, ensure it is pulled
2527
 
            # in.
2528
 
            source_repository = self._get_fallback_repository(old_url)
2529
 
            for revision_id in chain([self.last_revision()],
2530
 
                self.tags.get_reverse_tag_dict()):
2531
 
                self.repository.fetch(source_repository, revision_id,
2532
 
                    find_ghosts=True)
2533
 
        else:
2534
 
            self._activate_fallback_location(url)
2535
 
        # write this out after the repository is stacked to avoid setting a
2536
 
        # stacked config that doesn't work.
2537
 
        self._set_config_location('stacked_on_location', url)
2538
 
 
2539
2536
    def _get_append_revisions_only(self):
2540
2537
        value = self.get_config().get_user_option('append_revisions_only')
2541
2538
        return value == 'True'
2594
2591
    def get_stacked_on_url(self):
2595
2592
        raise errors.UnstackableBranchFormat(self._format, self.base)
2596
2593
 
2597
 
    def set_stacked_on_url(self, url):
2598
 
        raise errors.UnstackableBranchFormat(self._format, self.base)
2599
 
 
2600
2594
 
2601
2595
######################################################################
2602
2596
# results of operations