~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

Avoid reopening (and relocking) the same branches/repositories in ControlDir.sprout.  Still a few rough edges, but the tests I've run are passing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
 
105
105
    def _activate_fallback_location(self, url):
106
106
        """Activate the branch/repository from url as a fallback repository."""
 
107
        for existing_fallback_repo in self.repository._fallback_repositories:
 
108
            if existing_fallback_repo.user_url == url:
 
109
                # This fallback is already configured.  This probably only
 
110
                # happens because BzrDir.sprout is a horrible mess.  To avoid
 
111
                # confusing _unstack we don't add this a second time.
 
112
                # XXX: log a warning, maybe?
 
113
                return
107
114
        repo = self._get_fallback_repository(url)
108
115
        if repo.has_same_location(self.repository):
109
116
            raise errors.UnstackableLocationError(self.user_url, url)
805
812
            old_repository = self.repository
806
813
            if len(old_repository._fallback_repositories) != 1:
807
814
                raise AssertionError("can't cope with fallback repositories "
808
 
                    "of %r" % (self.repository,))
 
815
                    "of %r (fallbacks: %r)" % (old_repository,
 
816
                        old_repository._fallback_repositories))
809
817
            # Open the new repository object.
810
818
            # Repositories don't offer an interface to remove fallback
811
819
            # repositories today; take the conceptually simpler option and just
1257
1265
        return result
1258
1266
 
1259
1267
    @needs_read_lock
1260
 
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None):
 
1268
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None,
 
1269
            repository=None):
1261
1270
        """Create a new line of development from the branch, into to_bzrdir.
1262
1271
 
1263
1272
        to_bzrdir controls the branch format.
1268
1277
        if (repository_policy is not None and
1269
1278
            repository_policy.requires_stacking()):
1270
1279
            to_bzrdir._format.require_stacking(_skip_repo=True)
1271
 
        result = to_bzrdir.create_branch()
 
1280
        result = to_bzrdir.create_branch(repository=repository)
1272
1281
        result.lock_write()
1273
1282
        try:
1274
1283
            if repository_policy is not None:
1625
1634
            hook(params)
1626
1635
 
1627
1636
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1628
 
                           lock_type='metadir', set_format=True):
 
1637
                           repository=None, lock_type='metadir',
 
1638
                           set_format=True):
1629
1639
        """Initialize a branch in a bzrdir, with specified files
1630
1640
 
1631
1641
        :param a_bzrdir: The bzrdir to initialize the branch in
1665
1675
        finally:
1666
1676
            if lock_taken:
1667
1677
                control_files.unlock()
1668
 
        branch = self.open(a_bzrdir, name, _found=True)
 
1678
        branch = self.open(a_bzrdir, name, _found=True,
 
1679
                found_repository=repository)
1669
1680
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
1670
1681
        return branch
1671
1682
 
1672
 
    def initialize(self, a_bzrdir, name=None):
 
1683
    def initialize(self, a_bzrdir, name=None, repository=None):
1673
1684
        """Create a branch of this format in a_bzrdir.
1674
1685
        
1675
1686
        :param name: Name of the colocated branch to create.
1709
1720
        """
1710
1721
        raise NotImplementedError(self.network_name)
1711
1722
 
1712
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
1723
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
1724
            found_repository=None):
1713
1725
        """Return the branch object for a_bzrdir
1714
1726
 
1715
1727
        :param a_bzrdir: A BzrDir that contains a branch.
2009
2021
        """See BranchFormat.get_format_description()."""
2010
2022
        return "Branch format 4"
2011
2023
 
2012
 
    def initialize(self, a_bzrdir, name=None):
 
2024
    def initialize(self, a_bzrdir, name=None, repository=None):
2013
2025
        """Create a branch of this format in a_bzrdir."""
 
2026
        if repository is not None:
 
2027
            raise NotImplementedError(
 
2028
                "initialize(repository=<not None>) on %r" % (self,))
2014
2029
        utf8_files = [('revision-history', ''),
2015
2030
                      ('branch-name', ''),
2016
2031
                      ]
2025
2040
        """The network name for this format is the control dirs disk label."""
2026
2041
        return self._matchingbzrdir.get_format_string()
2027
2042
 
2028
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
2043
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
2044
            found_repository=None):
2029
2045
        """See BranchFormat.open()."""
2030
2046
        if not _found:
2031
2047
            # we are being called directly and must probe.
2032
2048
            raise NotImplementedError
 
2049
        if found_repository is None:
 
2050
            found_repository = a_bzrdir.open_repository()
2033
2051
        return BzrBranch(_format=self,
2034
2052
                         _control_files=a_bzrdir._control_files,
2035
2053
                         a_bzrdir=a_bzrdir,
2036
2054
                         name=name,
2037
 
                         _repository=a_bzrdir.open_repository())
 
2055
                         _repository=found_repository)
2038
2056
 
2039
2057
    def __str__(self):
2040
2058
        return "Bazaar-NG branch format 4"
2054
2072
        """
2055
2073
        return self.get_format_string()
2056
2074
 
2057
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
2075
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
2076
            found_repository=None):
2058
2077
        """See BranchFormat.open()."""
2059
2078
        if not _found:
2060
2079
            format = BranchFormat.find_format(a_bzrdir, name=name)
2065
2084
        try:
2066
2085
            control_files = lockable_files.LockableFiles(transport, 'lock',
2067
2086
                                                         lockdir.LockDir)
 
2087
            if found_repository is None:
 
2088
                found_repository = a_bzrdir.find_repository()
2068
2089
            return self._branch_class()(_format=self,
2069
2090
                              _control_files=control_files,
2070
2091
                              name=name,
2071
2092
                              a_bzrdir=a_bzrdir,
2072
 
                              _repository=a_bzrdir.find_repository(),
 
2093
                              _repository=found_repository,
2073
2094
                              ignore_fallbacks=ignore_fallbacks)
2074
2095
        except errors.NoSuchFile:
2075
2096
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2107
2128
        """See BranchFormat.get_format_description()."""
2108
2129
        return "Branch format 5"
2109
2130
 
2110
 
    def initialize(self, a_bzrdir, name=None):
 
2131
    def initialize(self, a_bzrdir, name=None, repository=None):
2111
2132
        """Create a branch of this format in a_bzrdir."""
2112
2133
        utf8_files = [('revision-history', ''),
2113
2134
                      ('branch-name', ''),
2114
2135
                      ]
2115
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2136
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2116
2137
 
2117
2138
    def supports_tags(self):
2118
2139
        return False
2140
2161
        """See BranchFormat.get_format_description()."""
2141
2162
        return "Branch format 6"
2142
2163
 
2143
 
    def initialize(self, a_bzrdir, name=None):
 
2164
    def initialize(self, a_bzrdir, name=None, repository=None):
2144
2165
        """Create a branch of this format in a_bzrdir."""
2145
2166
        utf8_files = [('last-revision', '0 null:\n'),
2146
2167
                      ('branch.conf', ''),
2147
2168
                      ('tags', ''),
2148
2169
                      ]
2149
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2170
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2150
2171
 
2151
2172
    def make_tags(self, branch):
2152
2173
        """See bzrlib.branch.BranchFormat.make_tags()."""
2170
2191
        """See BranchFormat.get_format_description()."""
2171
2192
        return "Branch format 8"
2172
2193
 
2173
 
    def initialize(self, a_bzrdir, name=None):
 
2194
    def initialize(self, a_bzrdir, name=None, repository=None):
2174
2195
        """Create a branch of this format in a_bzrdir."""
2175
2196
        utf8_files = [('last-revision', '0 null:\n'),
2176
2197
                      ('branch.conf', ''),
2177
2198
                      ('tags', ''),
2178
2199
                      ('references', '')
2179
2200
                      ]
2180
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2201
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2181
2202
 
2182
2203
    def __init__(self):
2183
2204
        super(BzrBranchFormat8, self).__init__()
2206
2227
    This format was introduced in bzr 1.6.
2207
2228
    """
2208
2229
 
2209
 
    def initialize(self, a_bzrdir, name=None):
 
2230
    def initialize(self, a_bzrdir, name=None, repository=None):
2210
2231
        """Create a branch of this format in a_bzrdir."""
2211
2232
        utf8_files = [('last-revision', '0 null:\n'),
2212
2233
                      ('branch.conf', ''),
2213
2234
                      ('tags', ''),
2214
2235
                      ]
2215
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2236
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2216
2237
 
2217
2238
    def _branch_class(self):
2218
2239
        return BzrBranch7
2260
2281
        transport = a_bzrdir.get_branch_transport(None, name=name)
2261
2282
        location = transport.put_bytes('location', to_branch.base)
2262
2283
 
2263
 
    def initialize(self, a_bzrdir, name=None, target_branch=None):
 
2284
    def initialize(self, a_bzrdir, name=None, target_branch=None,
 
2285
            repository=None):
2264
2286
        """Create a branch of this format in a_bzrdir."""
2265
2287
        if target_branch is None:
2266
2288
            # this format does not implement branch itself, thus the implicit
2294
2316
        return clone
2295
2317
 
2296
2318
    def open(self, a_bzrdir, name=None, _found=False, location=None,
2297
 
             possible_transports=None, ignore_fallbacks=False):
 
2319
             possible_transports=None, ignore_fallbacks=False,
 
2320
             found_repository=None):
2298
2321
        """Return the branch that the branch reference in a_bzrdir points at.
2299
2322
 
2300
2323
        :param a_bzrdir: A BzrDir that contains a branch.