~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

(jelmer) Reduce the number of connections made during "bzr branch
 --stacked". (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
    def user_transport(self):
87
87
        return self.bzrdir.user_transport
88
88
 
89
 
    def __init__(self, *ignored, **ignored_too):
 
89
    def __init__(self, possible_transports=None):
90
90
        self.tags = self._format.make_tags(self)
91
91
        self._revision_history_cache = None
92
92
        self._revision_id_to_revno_cache = None
96
96
        self._last_revision_info_cache = None
97
97
        self._master_branch_cache = None
98
98
        self._merge_sorted_revisions_cache = None
99
 
        self._open_hook()
 
99
        self._open_hook(possible_transports)
100
100
        hooks = Branch.hooks['open']
101
101
        for hook in hooks:
102
102
            hook(self)
103
103
 
104
 
    def _open_hook(self):
 
104
    def _open_hook(self, possible_transports):
105
105
        """Called by init to allow simpler extension of the base class."""
106
106
 
107
 
    def _activate_fallback_location(self, url):
 
107
    def _activate_fallback_location(self, url, possible_transports):
108
108
        """Activate the branch/repository from url as a fallback repository."""
109
109
        for existing_fallback_repo in self.repository._fallback_repositories:
110
110
            if existing_fallback_repo.user_url == url:
113
113
                # confusing _unstack we don't add this a second time.
114
114
                mutter('duplicate activation of fallback %r on %r', url, self)
115
115
                return
116
 
        repo = self._get_fallback_repository(url)
 
116
        repo = self._get_fallback_repository(url, possible_transports)
117
117
        if repo.has_same_location(self.repository):
118
118
            raise errors.UnstackableLocationError(self.user_url, url)
119
119
        self.repository.add_fallback_repository(repo)
175
175
        """
176
176
        control = controldir.ControlDir.open(base, _unsupported,
177
177
                                     possible_transports=possible_transports)
178
 
        return control.open_branch(unsupported=_unsupported)
 
178
        return control.open_branch(unsupported=_unsupported,
 
179
            possible_transports=possible_transports)
179
180
 
180
181
    @staticmethod
181
 
    def open_from_transport(transport, name=None, _unsupported=False):
 
182
    def open_from_transport(transport, name=None, _unsupported=False,
 
183
            possible_transports=None):
182
184
        """Open the branch rooted at transport"""
183
185
        control = controldir.ControlDir.open_from_transport(transport, _unsupported)
184
 
        return control.open_branch(name=name, unsupported=_unsupported)
 
186
        return control.open_branch(name=name, unsupported=_unsupported,
 
187
            possible_transports=possible_transports)
185
188
 
186
189
    @staticmethod
187
190
    def open_containing(url, possible_transports=None):
197
200
        """
198
201
        control, relpath = controldir.ControlDir.open_containing(url,
199
202
                                                         possible_transports)
200
 
        return control.open_branch(), relpath
 
203
        branch = control.open_branch(possible_transports=possible_transports)
 
204
        return (branch, relpath)
201
205
 
202
206
    def _push_should_merge_tags(self):
203
207
        """Should _basic_push merge this branch's tags into the target?
239
243
        """
240
244
        raise NotImplementedError(self._get_config)
241
245
 
242
 
    def _get_fallback_repository(self, url):
 
246
    def _get_fallback_repository(self, url, possible_transports):
243
247
        """Get the repository we fallback to at url."""
244
248
        url = urlutils.join(self.base, url)
245
 
        a_branch = Branch.open(url,
246
 
            possible_transports=[self.bzrdir.root_transport])
 
249
        a_branch = Branch.open(url, possible_transports=possible_transports)
247
250
        return a_branch.repository
248
251
 
249
252
    @needs_read_lock
851
854
                return
852
855
            self._unstack()
853
856
        else:
854
 
            self._activate_fallback_location(url)
 
857
            self._activate_fallback_location(url,
 
858
                possible_transports=[self.bzrdir.root_transport])
855
859
        # write this out after the repository is stacked to avoid setting a
856
860
        # stacked config that doesn't work.
857
861
        self._set_config_location('stacked_on_location', url)
1717
1721
        raise NotImplementedError(self.network_name)
1718
1722
 
1719
1723
    def open(self, controldir, name=None, _found=False, ignore_fallbacks=False,
1720
 
            found_repository=None):
 
1724
            found_repository=None, possible_transports=None):
1721
1725
        """Return the branch object for controldir.
1722
1726
 
1723
1727
        :param controldir: A ControlDir that contains a branch.
2065
2069
        return self.get_format_string()
2066
2070
 
2067
2071
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
2068
 
            found_repository=None):
 
2072
            found_repository=None, possible_transports=None):
2069
2073
        """See BranchFormat.open()."""
2070
2074
        if not _found:
2071
2075
            format = BranchFormat.find_format(a_bzrdir, name=name)
2083
2087
                              name=name,
2084
2088
                              a_bzrdir=a_bzrdir,
2085
2089
                              _repository=found_repository,
2086
 
                              ignore_fallbacks=ignore_fallbacks)
 
2090
                              ignore_fallbacks=ignore_fallbacks,
 
2091
                              possible_transports=possible_transports)
2087
2092
        except errors.NoSuchFile:
2088
2093
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2089
2094
 
2345
2350
        real_bzrdir = controldir.ControlDir.open(
2346
2351
            location, possible_transports=possible_transports)
2347
2352
        result = real_bzrdir.open_branch(name=name, 
2348
 
            ignore_fallbacks=ignore_fallbacks)
 
2353
            ignore_fallbacks=ignore_fallbacks,
 
2354
            possible_transports=possible_transports)
2349
2355
        # this changes the behaviour of result.clone to create a new reference
2350
2356
        # rather than a copy of the content of the branch.
2351
2357
        # I did not use a proxy object because that needs much more extensive
2432
2438
 
2433
2439
    def __init__(self, _format=None,
2434
2440
                 _control_files=None, a_bzrdir=None, name=None,
2435
 
                 _repository=None, ignore_fallbacks=False):
 
2441
                 _repository=None, ignore_fallbacks=False,
 
2442
                 possible_transports=None):
2436
2443
        """Create new branch object at a particular location."""
2437
2444
        if a_bzrdir is None:
2438
2445
            raise ValueError('a_bzrdir must be supplied')
2450
2457
        self.control_files = _control_files
2451
2458
        self._transport = _control_files._transport
2452
2459
        self.repository = _repository
2453
 
        Branch.__init__(self)
 
2460
        Branch.__init__(self, possible_transports)
2454
2461
 
2455
2462
    def __str__(self):
2456
2463
        return '%s(%s)' % (self.__class__.__name__, self.user_url)
2819
2826
class BzrBranch8(BzrBranch):
2820
2827
    """A branch that stores tree-reference locations."""
2821
2828
 
2822
 
    def _open_hook(self):
 
2829
    def _open_hook(self, possible_transports=None):
2823
2830
        if self._ignore_fallbacks:
2824
2831
            return
 
2832
        if possible_transports is None:
 
2833
            possible_transports = [self.bzrdir.root_transport]
2825
2834
        try:
2826
2835
            url = self.get_stacked_on_url()
2827
2836
        except (errors.UnstackableRepositoryFormat, errors.NotStacked,
2835
2844
                    raise AssertionError(
2836
2845
                        "'transform_fallback_location' hook %s returned "
2837
2846
                        "None, not a URL." % hook_name)
2838
 
            self._activate_fallback_location(url)
 
2847
            self._activate_fallback_location(url,
 
2848
                possible_transports=possible_transports)
2839
2849
 
2840
2850
    def __init__(self, *args, **kwargs):
2841
2851
        self._ignore_fallbacks = kwargs.get('ignore_fallbacks', False)