/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2006-2011 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1534.4.39 by Robert Collins
Basic BzrDir support.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1534.4.39 by Robert Collins
Basic BzrDir support.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1534.4.39 by Robert Collins
Basic BzrDir support.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1534.4.39 by Robert Collins
Basic BzrDir support.
16
17
"""BzrDir logic. The BzrDir is the basic control directory used by bzr.
18
19
At format 7 this was split out into Branch, Repository and Checkout control
20
directories.
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
21
22
Note: This module has a lot of ``open`` functions/methods that return
23
references to in-memory objects. As a rule, there are no matching ``close``
24
methods. To free any associated resources, simply stop referencing the
25
objects returned.
1534.4.39 by Robert Collins
Basic BzrDir support.
26
"""
27
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
28
# TODO: Move old formats into a plugin to make this file smaller.
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
29
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
30
import os
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
31
import sys
4961.2.14 by Martin Pool
Further pb cleanups
32
import warnings
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
33
34
from bzrlib.lazy_import import lazy_import
35
lazy_import(globals(), """
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
36
from stat import S_ISDIR
1534.4.39 by Robert Collins
Basic BzrDir support.
37
38
import bzrlib
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
39
from bzrlib import (
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
40
    branch,
3242.1.1 by Aaron Bentley
Implement BzrDir configuration
41
    config,
5363.2.20 by Jelmer Vernooij
use controldir.X
42
    controldir,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
43
    errors,
2776.1.5 by Robert Collins
Add reasonably comprehensive tests for path last modified and per file graph behaviour.
44
    graph,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
45
    lockable_files,
46
    lockdir,
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
47
    osutils,
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
48
    pyutils,
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
49
    remote,
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
50
    repository,
1996.3.12 by John Arbash Meinel
Change how 'revision' is imported to avoid problems later
51
    revision as _mod_revision,
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
52
    transport as _mod_transport,
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
53
    ui,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
54
    urlutils,
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
55
    versionedfile,
3023.1.2 by Alexander Belchenko
Martin's review.
56
    win32utils,
57
    workingtree,
58
    workingtree_4,
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
59
    xml4,
60
    xml5,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
61
    )
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
62
from bzrlib.osutils import (
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
63
    sha_string,
64
    )
3978.3.14 by Jelmer Vernooij
Move BranchBzrDirInter.push() to BzrDir.push().
65
from bzrlib.push import (
66
    PushResult,
67
    )
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
68
from bzrlib.repofmt import pack_repo
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
69
from bzrlib.smart.client import _SmartClient
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
70
from bzrlib.store.versioned import VersionedFileStore
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
71
from bzrlib.transactions import WriteTransaction
2164.2.21 by Vincent Ladeuil
Take bundles into account.
72
from bzrlib.transport import (
73
    do_catching_redirections,
3224.5.24 by Andrew Bennetts
More minor import tidying suggested by pyflakes.
74
    local,
2164.2.21 by Vincent Ladeuil
Take bundles into account.
75
    )
5582.9.22 by Jelmer Vernooij
fix import.
76
from bzrlib.weave import (
77
    WeaveFile,
78
    Weave,
79
    )
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
80
""")
81
2164.2.1 by v.ladeuil+lp at free
First rough http branch redirection implementation.
82
from bzrlib.trace import (
83
    mutter,
84
    note,
4580.4.1 by Martin Pool
Give a warning if --hardlink can't be supported
85
    warning,
2164.2.1 by v.ladeuil+lp at free
First rough http branch redirection implementation.
86
    )
3224.5.24 by Andrew Bennetts
More minor import tidying suggested by pyflakes.
87
88
from bzrlib import (
4160.1.1 by Robert Collins
Add a BzrDir.pre_open hook for use by the smart server gaol.
89
    hooks,
3224.5.24 by Andrew Bennetts
More minor import tidying suggested by pyflakes.
90
    registry,
91
    symbol_versioning,
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
92
    )
5409.5.4 by Vincent Ladeuil
Deprecate BzrDir.generate_backup_name and use osutils.available_backup_name.
93
from bzrlib.symbol_versioning import (
94
    deprecated_in,
95
    deprecated_method,
96
    )
5363.2.2 by Jelmer Vernooij
Rename per_bzrdir => per_controldir.
97
98
5363.2.20 by Jelmer Vernooij
use controldir.X
99
class BzrDir(controldir.ControlDir):
1534.4.39 by Robert Collins
Basic BzrDir support.
100
    """A .bzr control diretory.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
101
1534.4.39 by Robert Collins
Basic BzrDir support.
102
    BzrDir instances let you create or open any of the things that can be
103
    found within .bzr - checkouts, branches and repositories.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
104
3407.2.13 by Martin Pool
Remove indirection through control_files to get transports
105
    :ivar transport:
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
106
        the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
3407.2.13 by Martin Pool
Remove indirection through control_files to get transports
107
    :ivar root_transport:
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
108
        a transport connected to the directory this bzr was opened from
109
        (i.e. the parent directory holding the .bzr directory).
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
110
111
    Everything in the bzrdir should have the same file permissions.
4160.1.1 by Robert Collins
Add a BzrDir.pre_open hook for use by the smart server gaol.
112
113
    :cvar hooks: An instance of BzrDirHooks.
1534.4.39 by Robert Collins
Basic BzrDir support.
114
    """
115
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
116
    def break_lock(self):
117
        """Invoke break_lock on the first object in the bzrdir.
118
119
        If there is a tree, the tree is opened and break_lock() called.
120
        Otherwise, branch is tried, and finally repository.
121
        """
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
122
        # XXX: This seems more like a UI function than something that really
123
        # belongs in this class.
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
124
        try:
125
            thing_to_unlock = self.open_workingtree()
126
        except (errors.NotLocalUrl, errors.NoWorkingTree):
127
            try:
128
                thing_to_unlock = self.open_branch()
129
            except errors.NotBranchError:
130
                try:
131
                    thing_to_unlock = self.open_repository()
132
                except errors.NoRepositoryPresent:
133
                    return
134
        thing_to_unlock.break_lock()
135
1910.2.12 by Aaron Bentley
Implement knit repo format 2
136
    def check_conversion_target(self, target_format):
4634.2.1 by Robert Collins
Fix regression in upgrade introduced with the change to upgrade in rev 4622.
137
        """Check that a bzrdir as a whole can be converted to a new format."""
138
        # The only current restriction is that the repository content can be 
139
        # fetched compatibly with the target.
1910.2.12 by Aaron Bentley
Implement knit repo format 2
140
        target_repo_format = target_format.repository_format
4634.2.1 by Robert Collins
Fix regression in upgrade introduced with the change to upgrade in rev 4622.
141
        try:
142
            self.open_repository()._format.check_conversion_target(
143
                target_repo_format)
144
        except errors.NoRepositoryPresent:
145
            # No repo, no problem.
146
            pass
1910.2.12 by Aaron Bentley
Implement knit repo format 2
147
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
148
    @staticmethod
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
149
    def _check_supported(format, allow_unsupported,
150
        recommend_upgrade=True,
151
        basedir=None):
152
        """Give an error or warning on old formats.
153
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
154
        :param format: may be any kind of format - workingtree, branch,
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
155
        or repository.
156
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
157
        :param allow_unsupported: If true, allow opening
158
        formats that are strongly deprecated, and which may
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
159
        have limited functionality.
160
161
        :param recommend_upgrade: If true (default), warn
162
        the user through the ui object that they may wish
163
        to upgrade the object.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
164
        """
2323.5.19 by Martin Pool
No upgrade recommendation on source when cloning
165
        # TODO: perhaps move this into a base Format class; it's not BzrDir
166
        # specific. mbp 20070323
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
167
        if not allow_unsupported and not format.is_supported():
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
168
            # see open_downlevel to open legacy branches.
1740.5.6 by Martin Pool
Clean up many exception classes.
169
            raise errors.UnsupportedFormatError(format=format)
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
170
        if recommend_upgrade \
171
            and getattr(format, 'upgrade_recommended', False):
172
            ui.ui_factory.recommend_upgrade(
173
                format.get_format_description(),
174
                basedir)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
175
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
176
    def clone_on_transport(self, transport, revision_id=None,
4294.2.2 by Robert Collins
Move use_existing and create_prefix all the way down to clone_on_transport, reducing duplicate work.
177
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
5448.6.1 by Matthew Gordon
Added --no-tree option to pull. Needs testing and help text.
178
        create_prefix=False, use_existing_dir=True, no_tree=False):
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
179
        """Clone this bzrdir and its contents to transport verbatim.
180
3242.3.36 by Aaron Bentley
Updates from review comments
181
        :param transport: The transport for the location to produce the clone
182
            at.  If the target directory does not exist, it will be created.
183
        :param revision_id: The tip revision-id to use for any branch or
184
            working tree.  If not None, then the clone operation may tune
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
185
            itself to download less data.
3242.3.35 by Aaron Bentley
Cleanups and documentation
186
        :param force_new_repo: Do not use a shared repository for the target,
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
187
                               even if one is available.
3242.3.22 by Aaron Bentley
Make clone stacking optional
188
        :param preserve_stacking: When cloning a stacked branch, stack the
189
            new branch on top of the other branch's stacked-on branch.
4294.2.2 by Robert Collins
Move use_existing and create_prefix all the way down to clone_on_transport, reducing duplicate work.
190
        :param create_prefix: Create any missing directories leading up to
191
            to_transport.
192
        :param use_existing_dir: Use an existing directory if one exists.
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
193
        """
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
194
        # Overview: put together a broad description of what we want to end up
195
        # with; then make as few api calls as possible to do it.
5363.2.2 by Jelmer Vernooij
Rename per_bzrdir => per_controldir.
196
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
197
        # We may want to create a repo/branch/tree, if we do so what format
198
        # would we want for each:
3650.5.1 by Aaron Bentley
Fix push to use clone all the time.
199
        require_stacking = (stacked_on is not None)
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
200
        format = self.cloning_metadir(require_stacking)
5363.2.2 by Jelmer Vernooij
Rename per_bzrdir => per_controldir.
201
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
202
        # Figure out what objects we want:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
203
        try:
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
204
            local_repo = self.find_repository()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
205
        except errors.NoRepositoryPresent:
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
206
            local_repo = None
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
207
        try:
208
            local_branch = self.open_branch()
209
        except errors.NotBranchError:
210
            local_branch = None
211
        else:
212
            # enable fallbacks when branch is not a branch reference
213
            if local_branch.repository.has_same_location(local_repo):
214
                local_repo = local_branch.repository
215
            if preserve_stacking:
216
                try:
3650.5.1 by Aaron Bentley
Fix push to use clone all the time.
217
                    stacked_on = local_branch.get_stacked_on_url()
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
218
                except (errors.UnstackableBranchFormat,
219
                        errors.UnstackableRepositoryFormat,
220
                        errors.NotStacked):
221
                    pass
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
222
        # Bug: We create a metadir without knowing if it can support stacking,
223
        # we should look up the policy needs first, or just use it as a hint,
224
        # or something.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
225
        if local_repo:
5448.6.1 by Matthew Gordon
Added --no-tree option to pull. Needs testing and help text.
226
            make_working_trees = local_repo.make_working_trees() and not no_tree
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
227
            want_shared = local_repo.is_shared()
228
            repo_format_name = format.repository_format.network_name()
229
        else:
230
            make_working_trees = False
231
            want_shared = False
232
            repo_format_name = None
233
234
        result_repo, result, require_stacking, repository_policy = \
235
            format.initialize_on_transport_ex(transport,
236
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
237
            force_new_repo=force_new_repo, stacked_on=stacked_on,
238
            stack_on_pwd=self.root_transport.base,
239
            repo_format_name=repo_format_name,
240
            make_working_trees=make_working_trees, shared_repo=want_shared)
241
        if repo_format_name:
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
242
            try:
243
                # If the result repository is in the same place as the
244
                # resulting bzr dir, it will have no content, further if the
245
                # result is not stacked then we know all content should be
246
                # copied, and finally if we are copying up to a specific
247
                # revision_id then we can use the pending-ancestry-result which
248
                # does not require traversing all of history to describe it.
5158.6.9 by Martin Pool
Simplify various code to use user_url
249
                if (result_repo.user_url == result.user_url
250
                    and not require_stacking and
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
251
                    revision_id is not None):
252
                    fetch_spec = graph.PendingAncestryResult(
253
                        [revision_id], local_repo)
254
                    result_repo.fetch(local_repo, fetch_spec=fetch_spec)
255
                else:
256
                    result_repo.fetch(local_repo, revision_id=revision_id)
257
            finally:
258
                result_repo.unlock()
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
259
        else:
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
260
            if result_repo is not None:
261
                raise AssertionError('result_repo not None(%r)' % result_repo)
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
262
        # 1 if there is a branch present
263
        #   make sure its content is available in the target repository
264
        #   clone it.
3242.3.37 by Aaron Bentley
Updates from reviews
265
        if local_branch is not None:
4050.1.1 by Robert Collins
Fix race condition with branch hooks during cloning when the new branch is stacked.
266
            result_branch = local_branch.clone(result, revision_id=revision_id,
267
                repository_policy=repository_policy)
4044.1.5 by Robert Collins
Stop trying to create working trees during clone when the target bzrdir cannot have a local abspath created for it.
268
        try:
269
            # Cheaper to check if the target is not local, than to try making
270
            # the tree and fail.
271
            result.root_transport.local_abspath('.')
272
            if result_repo is None or result_repo.make_working_trees():
2991.1.2 by Daniel Watkins
Working trees are no longer created by pushing into a local no-trees repo.
273
                self.open_workingtree().clone(result)
4044.1.5 by Robert Collins
Stop trying to create working trees during clone when the target bzrdir cannot have a local abspath created for it.
274
        except (errors.NoWorkingTree, errors.NotLocalUrl):
275
            pass
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
276
        return result
277
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
278
    # TODO: This should be given a Transport, and should chdir up; otherwise
279
    # this will open a new connection.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
280
    def _make_tail(self, url):
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
281
        t = _mod_transport.get_transport(url)
2475.3.3 by John Arbash Meinel
Change calls to try/mkdir('.')/except FileExists to ensure_base()
282
        t.ensure_base()
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
283
3140.1.1 by Aaron Bentley
Implement find_bzrdir functionality
284
    @staticmethod
285
    def find_bzrdirs(transport, evaluate=None, list_current=None):
286
        """Find bzrdirs recursively from current location.
287
288
        This is intended primarily as a building block for more sophisticated
289
        functionality, like finding trees under a directory, or finding
290
        branches that use a given repository.
291
        :param evaluate: An optional callable that yields recurse, value,
292
            where recurse controls whether this bzrdir is recursed into
293
            and value is the value to yield.  By default, all bzrdirs
294
            are recursed into, and the return value is the bzrdir.
295
        :param list_current: if supplied, use this function to list the current
296
            directory, instead of Transport.list_dir
297
        :return: a generator of found bzrdirs, or whatever evaluate returns.
298
        """
299
        if list_current is None:
300
            def list_current(transport):
301
                return transport.list_dir('')
302
        if evaluate is None:
303
            def evaluate(bzrdir):
304
                return True, bzrdir
305
306
        pending = [transport]
307
        while len(pending) > 0:
308
            current_transport = pending.pop()
309
            recurse = True
310
            try:
311
                bzrdir = BzrDir.open_from_transport(current_transport)
5215.3.3 by Marius Kruger
remove inappropriate catches
312
            except (errors.NotBranchError, errors.PermissionDenied):
3140.1.1 by Aaron Bentley
Implement find_bzrdir functionality
313
                pass
314
            else:
315
                recurse, value = evaluate(bzrdir)
316
                yield value
317
            try:
318
                subdirs = list_current(current_transport)
5215.3.1 by Marius Kruger
don't raise an exception when finding or brobing for a bzrdir and permission is denied
319
            except (errors.NoSuchFile, errors.PermissionDenied):
3140.1.1 by Aaron Bentley
Implement find_bzrdir functionality
320
                continue
321
            if recurse:
322
                for subdir in sorted(subdirs, reverse=True):
323
                    pending.append(current_transport.clone(subdir))
324
3140.1.3 by Aaron Bentley
Add support for finding branches to BzrDir
325
    @staticmethod
326
    def find_branches(transport):
3140.1.7 by Aaron Bentley
Update docs
327
        """Find all branches under a transport.
328
329
        This will find all branches below the transport, including branches
330
        inside other branches.  Where possible, it will use
331
        Repository.find_branches.
332
333
        To list all the branches that use a particular Repository, see
334
        Repository.find_branches
335
        """
3140.1.3 by Aaron Bentley
Add support for finding branches to BzrDir
336
        def evaluate(bzrdir):
337
            try:
338
                repository = bzrdir.open_repository()
339
            except errors.NoRepositoryPresent:
340
                pass
341
            else:
4997.1.2 by Jelmer Vernooij
Use list_branches rather than open_branch in find_branches.
342
                return False, ([], repository)
343
            return True, (bzrdir.list_branches(), None)
344
        ret = []
345
        for branches, repo in BzrDir.find_bzrdirs(transport,
346
                                                  evaluate=evaluate):
3140.1.3 by Aaron Bentley
Add support for finding branches to BzrDir
347
            if repo is not None:
4997.1.2 by Jelmer Vernooij
Use list_branches rather than open_branch in find_branches.
348
                ret.extend(repo.find_branches())
349
            if branches is not None:
350
                ret.extend(branches)
351
        return ret
3140.1.3 by Aaron Bentley
Add support for finding branches to BzrDir
352
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
353
    @staticmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
354
    def create_branch_and_repo(base, force_new_repo=False, format=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
355
        """Create a new BzrDir, Branch and Repository at the url 'base'.
356
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
357
        This will use the current default BzrDirFormat unless one is
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
358
        specified, and use whatever
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
359
        repository format that that uses via bzrdir.create_branch and
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
360
        create_repository. If a shared repository is available that is used
361
        preferentially.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
362
363
        The created Branch object is returned.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
364
365
        :param base: The URL to create the branch at.
366
        :param force_new_repo: If True a new repository is always created.
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
367
        :param format: If supplied, the format of branch to create.  If not
368
            supplied, the default is used.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
369
        """
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
370
        bzrdir = BzrDir.create(base, format)
1534.6.11 by Robert Collins
Review feedback.
371
        bzrdir._find_or_create_repository(force_new_repo)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
372
        return bzrdir.create_branch()
1534.6.11 by Robert Collins
Review feedback.
373
3242.3.32 by Aaron Bentley
Defer handling relative stacking URLs as late as possible.
374
    def determine_repository_policy(self, force_new_repo=False, stack_on=None,
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
375
                                    stack_on_pwd=None, require_stacking=False):
3242.2.13 by Aaron Bentley
Update docs
376
        """Return an object representing a policy to use.
377
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
378
        This controls whether a new repository is created, and the format of
379
        that repository, or some existing shared repository used instead.
3242.3.35 by Aaron Bentley
Cleanups and documentation
380
381
        If stack_on is supplied, will not seek a containing shared repo.
3575.2.2 by Martin Pool
branch --stacked should force a stacked format
382
3242.3.35 by Aaron Bentley
Cleanups and documentation
383
        :param force_new_repo: If True, require a new repository to be created.
384
        :param stack_on: If supplied, the location to stack on.  If not
385
            supplied, a default_stack_on location may be used.
386
        :param stack_on_pwd: If stack_on is relative, the location it is
387
            relative to.
3242.2.13 by Aaron Bentley
Update docs
388
        """
3242.3.3 by Aaron Bentley
Use _find_containing to determine repository policy
389
        def repository_policy(found_bzrdir):
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
390
            stack_on = None
3242.3.32 by Aaron Bentley
Defer handling relative stacking URLs as late as possible.
391
            stack_on_pwd = None
3641.1.1 by John Arbash Meinel
Merge in 1.6rc5 and revert disabling default stack on policy
392
            config = found_bzrdir.get_config()
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
393
            stop = False
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
394
            stack_on = config.get_default_stack_on()
395
            if stack_on is not None:
5158.6.9 by Martin Pool
Simplify various code to use user_url
396
                stack_on_pwd = found_bzrdir.user_url
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
397
                stop = True
3242.3.3 by Aaron Bentley
Use _find_containing to determine repository policy
398
            # does it have a repository ?
399
            try:
400
                repository = found_bzrdir.open_repository()
401
            except errors.NoRepositoryPresent:
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
402
                repository = None
403
            else:
5158.6.9 by Martin Pool
Simplify various code to use user_url
404
                if (found_bzrdir.user_url != self.user_url 
405
                    and not repository.is_shared()):
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
406
                    # Don't look higher, can't use a higher shared repo.
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
407
                    repository = None
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
408
                    stop = True
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
409
                else:
410
                    stop = True
411
            if not stop:
3242.3.3 by Aaron Bentley
Use _find_containing to determine repository policy
412
                return None, False
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
413
            if repository:
3242.3.32 by Aaron Bentley
Defer handling relative stacking URLs as late as possible.
414
                return UseExistingRepository(repository, stack_on,
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
415
                    stack_on_pwd, require_stacking=require_stacking), True
3242.3.3 by Aaron Bentley
Use _find_containing to determine repository policy
416
            else:
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
417
                return CreateRepository(self, stack_on, stack_on_pwd,
418
                    require_stacking=require_stacking), True
3242.3.3 by Aaron Bentley
Use _find_containing to determine repository policy
419
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
420
        if not force_new_repo:
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
421
            if stack_on is None:
422
                policy = self._find_containing(repository_policy)
423
                if policy is not None:
424
                    return policy
425
            else:
426
                try:
427
                    return UseExistingRepository(self.open_repository(),
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
428
                        stack_on, stack_on_pwd,
429
                        require_stacking=require_stacking)
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
430
                except errors.NoRepositoryPresent:
431
                    pass
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
432
        return CreateRepository(self, stack_on, stack_on_pwd,
433
                                require_stacking=require_stacking)
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
434
1534.6.11 by Robert Collins
Review feedback.
435
    def _find_or_create_repository(self, force_new_repo):
436
        """Create a new repository if needed, returning the repository."""
3242.2.10 by Aaron Bentley
Rename RepositoryPolicy.apply to acquire_repository
437
        policy = self.determine_repository_policy(force_new_repo)
4070.9.2 by Andrew Bennetts
Rough prototype of allowing a SearchResult to be passed to fetch, and using that to improve network conversations.
438
        return policy.acquire_repository()[0]
3242.2.10 by Aaron Bentley
Rename RepositoryPolicy.apply to acquire_repository
439
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
440
    @staticmethod
2476.3.6 by Vincent Ladeuil
Fix the 'init connects multiple times' in a different way.
441
    def create_branch_convenience(base, force_new_repo=False,
442
                                  force_new_tree=None, format=None,
2476.3.11 by Vincent Ladeuil
Cosmetic changes.
443
                                  possible_transports=None):
1534.6.10 by Robert Collins
Finish use of repositories support.
444
        """Create a new BzrDir, Branch and Repository at the url 'base'.
445
446
        This is a convenience function - it will use an existing repository
447
        if possible, can be told explicitly whether to create a working tree or
1534.6.12 by Robert Collins
Typo found by John Meinel.
448
        not.
1534.6.10 by Robert Collins
Finish use of repositories support.
449
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
450
        This will use the current default BzrDirFormat unless one is
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
451
        specified, and use whatever
1534.6.10 by Robert Collins
Finish use of repositories support.
452
        repository format that that uses via bzrdir.create_branch and
453
        create_repository. If a shared repository is available that is used
454
        preferentially. Whatever repository is used, its tree creation policy
455
        is followed.
456
457
        The created Branch object is returned.
458
        If a working tree cannot be made due to base not being a file:// url,
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
459
        no error is raised unless force_new_tree is True, in which case no
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
460
        data is created on disk and NotLocalUrl is raised.
1534.6.10 by Robert Collins
Finish use of repositories support.
461
462
        :param base: The URL to create the branch at.
463
        :param force_new_repo: If True a new repository is always created.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
464
        :param force_new_tree: If True or False force creation of a tree or
1534.6.10 by Robert Collins
Finish use of repositories support.
465
                               prevent such creation respectively.
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
466
        :param format: Override for the bzrdir format to create.
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
467
        :param possible_transports: An optional reusable transports list.
1534.6.10 by Robert Collins
Finish use of repositories support.
468
        """
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
469
        if force_new_tree:
470
            # check for non local urls
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
471
            t = _mod_transport.get_transport(base, possible_transports)
3224.5.24 by Andrew Bennetts
More minor import tidying suggested by pyflakes.
472
            if not isinstance(t, local.LocalTransport):
2476.3.6 by Vincent Ladeuil
Fix the 'init connects multiple times' in a different way.
473
                raise errors.NotLocalUrl(base)
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
474
        bzrdir = BzrDir.create(base, format, possible_transports)
1534.6.11 by Robert Collins
Review feedback.
475
        repo = bzrdir._find_or_create_repository(force_new_repo)
1534.6.10 by Robert Collins
Finish use of repositories support.
476
        result = bzrdir.create_branch()
2476.3.4 by Vincent Ladeuil
Add tests.
477
        if force_new_tree or (repo.make_working_trees() and
1534.6.10 by Robert Collins
Finish use of repositories support.
478
                              force_new_tree is None):
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
479
            try:
480
                bzrdir.create_workingtree()
481
            except errors.NotLocalUrl:
482
                pass
1534.6.10 by Robert Collins
Finish use of repositories support.
483
        return result
2476.3.4 by Vincent Ladeuil
Add tests.
484
1551.8.2 by Aaron Bentley
Add create_checkout_convenience
485
    @staticmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
486
    def create_standalone_workingtree(base, format=None):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
487
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
488
489
        'base' must be a local path or a file:// url.
490
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
491
        This will use the current default BzrDirFormat unless one is
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
492
        specified, and use whatever
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
493
        repository format that that uses for bzrdirformat.create_workingtree,
494
        create_branch and create_repository.
495
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
496
        :param format: Override for the bzrdir format to create.
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
497
        :return: The WorkingTree object.
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
498
        """
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
499
        t = _mod_transport.get_transport(base)
3224.5.24 by Andrew Bennetts
More minor import tidying suggested by pyflakes.
500
        if not isinstance(t, local.LocalTransport):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
501
            raise errors.NotLocalUrl(base)
2485.8.45 by Vincent Ladeuil
Take jam's remarks into account.
502
        bzrdir = BzrDir.create_branch_and_repo(base,
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
503
                                               force_new_repo=True,
504
                                               format=format).bzrdir
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
505
        return bzrdir.create_workingtree()
506
5409.5.4 by Vincent Ladeuil
Deprecate BzrDir.generate_backup_name and use osutils.available_backup_name.
507
    @deprecated_method(deprecated_in((2, 3, 0)))
5340.8.4 by Marius Kruger
* gen_backup_name => generate_backup_name
508
    def generate_backup_name(self, base):
5409.5.4 by Vincent Ladeuil
Deprecate BzrDir.generate_backup_name and use osutils.available_backup_name.
509
        return self._available_backup_name(base)
510
511
    def _available_backup_name(self, base):
5409.5.8 by Vincent Ladeuil
Be more explicit about race conditions and LBYL being discouraged
512
        """Find a non-existing backup file name based on base.
513
514
        See bzrlib.osutils.available_backup_name about race conditions.
515
        """
5409.5.4 by Vincent Ladeuil
Deprecate BzrDir.generate_backup_name and use osutils.available_backup_name.
516
        return osutils.available_backup_name(base, self.root_transport.has)
5340.8.1 by Marius Kruger
* make the backup file name generator in bzrdir available to others
517
3872.3.2 by Jelmer Vernooij
make backup_bzrdir determine the name for the backup files.
518
    def backup_bzrdir(self):
3872.3.1 by Jelmer Vernooij
Allow BzrDir implementation to implement backing up of control directory.
519
        """Backup this bzr control directory.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
520
3872.3.2 by Jelmer Vernooij
make backup_bzrdir determine the name for the backup files.
521
        :return: Tuple with old path name and new path name
3872.3.1 by Jelmer Vernooij
Allow BzrDir implementation to implement backing up of control directory.
522
        """
5035.4.1 by Parth Malwankar
fixes 335033.
523
3943.2.4 by Martin Pool
Move backup progress indicators from upgrade.py into backup_bzrdir, and tweak text
524
        pb = ui.ui_factory.nested_progress_bar()
525
        try:
526
            old_path = self.root_transport.abspath('.bzr')
5409.5.4 by Vincent Ladeuil
Deprecate BzrDir.generate_backup_name and use osutils.available_backup_name.
527
            backup_dir = self._available_backup_name('backup.bzr')
5035.4.2 by Parth Malwankar
name_gen now works with all transports.
528
            new_path = self.root_transport.abspath(backup_dir)
5409.5.4 by Vincent Ladeuil
Deprecate BzrDir.generate_backup_name and use osutils.available_backup_name.
529
            ui.ui_factory.note('making backup of %s\n  to %s'
530
                               % (old_path, new_path,))
5035.4.2 by Parth Malwankar
name_gen now works with all transports.
531
            self.root_transport.copy_tree('.bzr', backup_dir)
3943.2.4 by Martin Pool
Move backup progress indicators from upgrade.py into backup_bzrdir, and tweak text
532
            return (old_path, new_path)
533
        finally:
534
            pb.finished()
3872.3.1 by Jelmer Vernooij
Allow BzrDir implementation to implement backing up of control directory.
535
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
536
    def retire_bzrdir(self, limit=10000):
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
537
        """Permanently disable the bzrdir.
538
539
        This is done by renaming it to give the user some ability to recover
540
        if there was a problem.
541
542
        This will have horrible consequences if anyone has anything locked or
543
        in use.
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
544
        :param limit: number of times to retry
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
545
        """
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
546
        i  = 0
547
        while True:
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
548
            try:
549
                to_path = '.bzr.retired.%d' % i
550
                self.root_transport.rename('.bzr', to_path)
551
                note("renamed %s to %s"
552
                    % (self.root_transport.abspath('.bzr'), to_path))
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
553
                return
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
554
            except (errors.TransportError, IOError, errors.PathError):
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
555
                i += 1
556
                if i > limit:
557
                    raise
558
                else:
559
                    pass
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
560
3242.3.2 by Aaron Bentley
Split _find_containing out of find_repository
561
    def _find_containing(self, evaluate):
3242.2.13 by Aaron Bentley
Update docs
562
        """Find something in a containing control directory.
563
564
        This method will scan containing control dirs, until it finds what
565
        it is looking for, decides that it will never find it, or runs out
566
        of containing control directories to check.
567
568
        It is used to implement find_repository and
569
        determine_repository_policy.
570
571
        :param evaluate: A function returning (value, stop).  If stop is True,
572
            the value will be returned.
573
        """
3242.3.2 by Aaron Bentley
Split _find_containing out of find_repository
574
        found_bzrdir = self
575
        while True:
576
            result, stop = evaluate(found_bzrdir)
577
            if stop:
578
                return result
579
            next_transport = found_bzrdir.root_transport.clone('..')
5158.6.9 by Martin Pool
Simplify various code to use user_url
580
            if (found_bzrdir.user_url == next_transport.base):
3242.3.2 by Aaron Bentley
Split _find_containing out of find_repository
581
                # top of the file system
582
                return None
583
            # find the next containing bzrdir
584
            try:
585
                found_bzrdir = BzrDir.open_containing_from_transport(
586
                    next_transport)[0]
587
            except errors.NotBranchError:
588
                return None
589
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
590
    def find_repository(self):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
591
        """Find the repository that should be used.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
592
593
        This does not require a branch as we use it to find the repo for
594
        new branches as well as to hook existing branches up to their
595
        repository.
596
        """
3242.3.2 by Aaron Bentley
Split _find_containing out of find_repository
597
        def usable_repository(found_bzrdir):
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
598
            # does it have a repository ?
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
599
            try:
600
                repository = found_bzrdir.open_repository()
601
            except errors.NoRepositoryPresent:
3242.3.2 by Aaron Bentley
Split _find_containing out of find_repository
602
                return None, False
5158.6.9 by Martin Pool
Simplify various code to use user_url
603
            if found_bzrdir.user_url == self.user_url:
3242.2.5 by Aaron Bentley
Avoid unnecessary is_shared check
604
                return repository, True
605
            elif repository.is_shared():
3242.3.2 by Aaron Bentley
Split _find_containing out of find_repository
606
                return repository, True
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
607
            else:
3242.2.5 by Aaron Bentley
Avoid unnecessary is_shared check
608
                return None, True
3242.3.2 by Aaron Bentley
Split _find_containing out of find_repository
609
610
        found_repo = self._find_containing(usable_repository)
611
        if found_repo is None:
612
            raise errors.NoRepositoryPresent(self)
613
        return found_repo
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
614
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
615
    def _find_creation_modes(self):
616
        """Determine the appropriate modes for files and directories.
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
617
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
618
        They're always set to be consistent with the base directory,
619
        assuming that this transport allows setting modes.
620
        """
621
        # TODO: Do we need or want an option (maybe a config setting) to turn
622
        # this off or override it for particular locations? -- mbp 20080512
623
        if self._mode_check_done:
624
            return
625
        self._mode_check_done = True
626
        try:
627
            st = self.transport.stat('.')
628
        except errors.TransportNotPossible:
629
            self._dir_mode = None
630
            self._file_mode = None
631
        else:
632
            # Check the directory mode, but also make sure the created
633
            # directories and files are read-write for this user. This is
634
            # mostly a workaround for filesystems which lie about being able to
635
            # write to a directory (cygwin & win32)
3641.2.1 by John Arbash Meinel
Fix bug #259855, if a Transport returns 0 for permission bits, ignore it
636
            if (st.st_mode & 07777 == 00000):
637
                # FTP allows stat but does not return dir/file modes
638
                self._dir_mode = None
639
                self._file_mode = None
640
            else:
641
                self._dir_mode = (st.st_mode & 07777) | 00700
642
                # Remove the sticky and execute bits for files
643
                self._file_mode = self._dir_mode & ~07111
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
644
645
    def _get_file_mode(self):
646
        """Return Unix mode for newly created files, or None.
647
        """
648
        if not self._mode_check_done:
649
            self._find_creation_modes()
650
        return self._file_mode
651
652
    def _get_dir_mode(self):
653
        """Return Unix mode for newly created directories, or None.
654
        """
655
        if not self._mode_check_done:
656
            self._find_creation_modes()
657
        return self._dir_mode
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
658
3242.1.1 by Aaron Bentley
Implement BzrDir configuration
659
    def get_config(self):
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
660
        """Get configuration for this BzrDir."""
661
        return config.BzrDirConfig(self)
662
663
    def _get_config(self):
664
        """By default, no configuration is available."""
665
        return None
3242.1.1 by Aaron Bentley
Implement BzrDir configuration
666
1534.4.39 by Robert Collins
Basic BzrDir support.
667
    def __init__(self, _transport, _format):
668
        """Initialize a Bzr control dir object.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
669
1534.4.39 by Robert Collins
Basic BzrDir support.
670
        Only really common logic should reside here, concrete classes should be
671
        made with varying behaviours.
672
1534.4.53 by Robert Collins
Review feedback from John Meinel.
673
        :param _format: the format that is creating this BzrDir instance.
674
        :param _transport: the transport this dir is based at.
1534.4.39 by Robert Collins
Basic BzrDir support.
675
        """
676
        self._format = _format
5158.6.1 by Martin Pool
Add ControlComponent interface and make BzrDir implement it
677
        # these are also under the more standard names of 
678
        # control_transport and user_transport
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
679
        self.transport = _transport.clone('.bzr')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
680
        self.root_transport = _transport
3416.2.1 by Martin Pool
Add BzrDir._get_file_mode and _get_dir_mode
681
        self._mode_check_done = False
5363.2.2 by Jelmer Vernooij
Rename per_bzrdir => per_controldir.
682
5158.6.1 by Martin Pool
Add ControlComponent interface and make BzrDir implement it
683
    @property 
684
    def user_transport(self):
685
        return self.root_transport
5363.2.2 by Jelmer Vernooij
Rename per_bzrdir => per_controldir.
686
5158.6.1 by Martin Pool
Add ControlComponent interface and make BzrDir implement it
687
    @property
688
    def control_transport(self):
689
        return self.transport
1534.4.39 by Robert Collins
Basic BzrDir support.
690
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
691
    def is_control_filename(self, filename):
692
        """True if filename is the name of a path which is reserved for bzrdir's.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
693
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
694
        :param filename: A filename within the root transport of this bzrdir.
695
696
        This is true IF and ONLY IF the filename is part of the namespace reserved
697
        for bzr control dirs. Currently this is the '.bzr' directory in the root
5363.2.2 by Jelmer Vernooij
Rename per_bzrdir => per_controldir.
698
        of the root_transport. 
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
699
        """
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
700
        # this might be better on the BzrDirFormat class because it refers to
701
        # all the possible bzrdir disk formats.
702
        # This method is tested via the workingtree is_control_filename tests-
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
703
        # it was extracted from WorkingTree.is_control_filename. If the method's
704
        # contract is extended beyond the current trivial implementation, please
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
705
        # add new tests for it to the appropriate place.
706
        return filename == '.bzr' or filename.startswith('.bzr/')
707
1534.4.39 by Robert Collins
Basic BzrDir support.
708
    @staticmethod
709
    def open_unsupported(base):
710
        """Open a branch which is not supported."""
711
        return BzrDir.open(base, _unsupported=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
712
1534.4.39 by Robert Collins
Basic BzrDir support.
713
    @staticmethod
2806.2.2 by Vincent Ladeuil
Fix #128076 and #131396 by reusing bound branch transport.
714
    def open(base, _unsupported=False, possible_transports=None):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
715
        """Open an existing bzrdir, rooted at 'base' (url).
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
716
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
717
        :param _unsupported: a private parameter to the BzrDir class.
1534.4.39 by Robert Collins
Basic BzrDir support.
718
        """
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
719
        t = _mod_transport.get_transport(base, possible_transports)
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
720
        return BzrDir.open_from_transport(t, _unsupported=_unsupported)
721
722
    @staticmethod
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
723
    def open_from_transport(transport, _unsupported=False,
724
                            _server_formats=True):
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
725
        """Open a bzrdir within a particular directory.
726
727
        :param transport: Transport containing the bzrdir.
728
        :param _unsupported: private.
729
        """
4160.1.1 by Robert Collins
Add a BzrDir.pre_open hook for use by the smart server gaol.
730
        for hook in BzrDir.hooks['pre_open']:
731
            hook(transport)
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
732
        # Keep initial base since 'transport' may be modified while following
733
        # the redirections.
2164.2.21 by Vincent Ladeuil
Take bundles into account.
734
        base = transport.base
735
        def find_format(transport):
5363.2.20 by Jelmer Vernooij
use controldir.X
736
            return transport, controldir.ControlDirFormat.find_format(
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
737
                transport, _server_formats=_server_formats)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
738
739
        def redirected(transport, e, redirection_notice):
3878.4.5 by Vincent Ladeuil
Don't use the exception as a parameter for _redirected_to.
740
            redirected_transport = transport._redirected_to(e.source, e.target)
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
741
            if redirected_transport is None:
742
                raise errors.NotBranchError(base)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
743
            note('%s is%s redirected to %s',
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
744
                 transport.base, e.permanently, redirected_transport.base)
745
            return redirected_transport
2164.2.21 by Vincent Ladeuil
Take bundles into account.
746
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
747
        try:
2164.2.28 by Vincent Ladeuil
TestingHTTPServer.test_case_server renamed from test_case to avoid confusions.
748
            transport, format = do_catching_redirections(find_format,
749
                                                         transport,
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
750
                                                         redirected)
751
        except errors.TooManyRedirections:
752
            raise errors.NotBranchError(base)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
753
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
754
        BzrDir._check_supported(format, _unsupported)
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
755
        return format.open(transport, _found=True)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
756
1534.4.39 by Robert Collins
Basic BzrDir support.
757
    @staticmethod
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
758
    def open_containing(url, possible_transports=None):
1534.4.39 by Robert Collins
Basic BzrDir support.
759
        """Open an existing branch which contains url.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
760
1534.6.3 by Robert Collins
find_repository sufficiently robust.
761
        :param url: url to search from.
1534.6.11 by Robert Collins
Review feedback.
762
        See open_containing_from_transport for more detail.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
763
        """
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
764
        transport = _mod_transport.get_transport(url, possible_transports)
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
765
        return BzrDir.open_containing_from_transport(transport)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
766
1534.6.3 by Robert Collins
find_repository sufficiently robust.
767
    @staticmethod
1534.6.11 by Robert Collins
Review feedback.
768
    def open_containing_from_transport(a_transport):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
769
        """Open an existing branch which contains a_transport.base.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
770
771
        This probes for a branch at a_transport, and searches upwards from there.
1534.4.39 by Robert Collins
Basic BzrDir support.
772
773
        Basically we keep looking up until we find the control directory or
774
        run into the root.  If there isn't one, raises NotBranchError.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
775
        If there is one and it is either an unrecognised format or an unsupported
1534.4.39 by Robert Collins
Basic BzrDir support.
776
        format, UnknownFormatError or UnsupportedFormatError are raised.
777
        If there is one, it is returned, along with the unused portion of url.
1685.1.27 by John Arbash Meinel
BzrDir works in URLs, but WorkingTree works in unicode paths
778
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
779
        :return: The BzrDir that contains the path, and a Unicode path
1685.1.28 by John Arbash Meinel
Changing open_containing to always return a unicode path.
780
                for the rest of the URL.
1534.4.39 by Robert Collins
Basic BzrDir support.
781
        """
782
        # this gets the normalised url back. I.e. '.' -> the full path.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
783
        url = a_transport.base
1534.4.39 by Robert Collins
Basic BzrDir support.
784
        while True:
785
            try:
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
786
                result = BzrDir.open_from_transport(a_transport)
787
                return result, urlutils.unescape(a_transport.relpath(url))
1534.4.39 by Robert Collins
Basic BzrDir support.
788
            except errors.NotBranchError, e:
1685.1.60 by Martin Pool
[broken] NotBranchError should unescape the url if possible
789
                pass
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
790
            try:
791
                new_t = a_transport.clone('..')
792
            except errors.InvalidURLJoin:
793
                # reached the root, whatever that may be
794
                raise errors.NotBranchError(path=url)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
795
            if new_t.base == a_transport.base:
1534.4.39 by Robert Collins
Basic BzrDir support.
796
                # reached the root, whatever that may be
797
                raise errors.NotBranchError(path=url)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
798
            a_transport = new_t
1534.4.39 by Robert Collins
Basic BzrDir support.
799
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
800
    @classmethod
801
    def open_tree_or_branch(klass, location):
802
        """Return the branch and working tree at a location.
803
804
        If there is no tree at the location, tree will be None.
805
        If there is no branch at the location, an exception will be
806
        raised
807
        :return: (tree, branch)
808
        """
809
        bzrdir = klass.open(location)
810
        return bzrdir._get_tree_branch()
811
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
812
    @classmethod
813
    def open_containing_tree_or_branch(klass, location):
814
        """Return the branch and working tree contained by a location.
815
816
        Returns (tree, branch, relpath).
817
        If there is no tree at containing the location, tree will be None.
818
        If there is no branch containing the location, an exception will be
819
        raised
820
        relpath is the portion of the path that is contained by the branch.
821
        """
822
        bzrdir, relpath = klass.open_containing(location)
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
823
        tree, branch = bzrdir._get_tree_branch()
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
824
        return tree, branch, relpath
825
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
826
    @classmethod
827
    def open_containing_tree_branch_or_repository(klass, location):
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
828
        """Return the working tree, branch and repo contained by a location.
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
829
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
830
        Returns (tree, branch, repository, relpath).
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
831
        If there is no tree containing the location, tree will be None.
832
        If there is no branch containing the location, branch will be None.
833
        If there is no repository containing the location, repository will be
834
        None.
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
835
        relpath is the portion of the path that is contained by the innermost
836
        BzrDir.
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
837
3015.3.59 by Daniel Watkins
Further tweaks as requested on-list.
838
        If no tree, branch or repository is found, a NotBranchError is raised.
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
839
        """
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
840
        bzrdir, relpath = klass.open_containing(location)
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
841
        try:
3015.3.51 by Daniel Watkins
Modified open_containing_tree_branch_or_repository as per Aaron's suggestion.
842
            tree, branch = bzrdir._get_tree_branch()
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
843
        except errors.NotBranchError:
844
            try:
3015.3.59 by Daniel Watkins
Further tweaks as requested on-list.
845
                repo = bzrdir.find_repository()
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
846
                return None, None, repo, relpath
847
            except (errors.NoRepositoryPresent):
848
                raise errors.NotBranchError(location)
849
        return tree, branch, branch.repository, relpath
3015.3.39 by Daniel Watkins
Added classmethod bzrlib.bzrdir.BzrDir.open_containing_tree_branch_or_repository.
850
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
851
    def _cloning_metadir(self):
3575.2.2 by Martin Pool
branch --stacked should force a stacked format
852
        """Produce a metadir suitable for cloning with.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
853
3575.2.2 by Martin Pool
branch --stacked should force a stacked format
854
        :returns: (destination_bzrdir_format, source_repository)
855
        """
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
856
        result_format = self._format.__class__()
857
        try:
1910.2.41 by Aaron Bentley
Clean up clone format creation
858
            try:
4160.2.6 by Andrew Bennetts
Add ignore_fallbacks flag.
859
                branch = self.open_branch(ignore_fallbacks=True)
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
860
                source_repository = branch.repository
3650.2.5 by Aaron Bentley
Stop creating a new instance
861
                result_format._branch_format = branch._format
1910.2.41 by Aaron Bentley
Clean up clone format creation
862
            except errors.NotBranchError:
863
                source_branch = None
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
864
                source_repository = self.open_repository()
2305.3.1 by Andrew Bennetts
Tidy up BzrDir.cloning_metadir: bogus try/except, and basis argument isn't actually used.
865
        except errors.NoRepositoryPresent:
2100.3.24 by Aaron Bentley
Get all tests passing again
866
            source_repository = None
2305.3.1 by Andrew Bennetts
Tidy up BzrDir.cloning_metadir: bogus try/except, and basis argument isn't actually used.
867
        else:
2018.5.138 by Robert Collins
Merge bzr.dev.
868
            # XXX TODO: This isinstance is here because we have not implemented
869
            # the fix recommended in bug # 103195 - to delegate this choice the
870
            # repository itself.
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
871
            repo_format = source_repository._format
3705.2.1 by Andrew Bennetts
Possible fix for bug 269214
872
            if isinstance(repo_format, remote.RemoteRepositoryFormat):
873
                source_repository._ensure_real()
874
                repo_format = source_repository._real_repository._format
875
            result_format.repository_format = repo_format
2100.3.28 by Aaron Bentley
Make sprout recursive
876
        try:
2323.5.19 by Martin Pool
No upgrade recommendation on source when cloning
877
            # TODO: Couldn't we just probe for the format in these cases,
878
            # rather than opening the whole tree?  It would be a little
879
            # faster. mbp 20070401
880
            tree = self.open_workingtree(recommend_upgrade=False)
2100.3.28 by Aaron Bentley
Make sprout recursive
881
        except (errors.NoWorkingTree, errors.NotLocalUrl):
882
            result_format.workingtree_format = None
883
        else:
884
            result_format.workingtree_format = tree._format.__class__()
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
885
        return result_format, source_repository
886
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
887
    def cloning_metadir(self, require_stacking=False):
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
888
        """Produce a metadir suitable for cloning or sprouting with.
1910.2.41 by Aaron Bentley
Clean up clone format creation
889
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
890
        These operations may produce workingtrees (yes, even though they're
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
891
        "cloning" something that doesn't have a tree), so a viable workingtree
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
892
        format must be selected.
3575.2.2 by Martin Pool
branch --stacked should force a stacked format
893
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
894
        :require_stacking: If True, non-stackable formats will be upgraded
895
            to similar stackable formats.
3575.2.2 by Martin Pool
branch --stacked should force a stacked format
896
        :returns: a BzrDirFormat with all component formats either set
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
897
            appropriately or set to None if that component should not be
3575.2.2 by Martin Pool
branch --stacked should force a stacked format
898
            created.
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
899
        """
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
900
        format, repository = self._cloning_metadir()
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
901
        if format._workingtree_format is None:
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
902
            # No tree in self.
2100.3.34 by Aaron Bentley
Fix BzrDir.cloning_metadir with no format
903
            if repository is None:
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
904
                # No repository either
2100.3.34 by Aaron Bentley
Fix BzrDir.cloning_metadir with no format
905
                return format
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
906
            # We have a repository, so set a working tree? (Why? This seems to
907
            # contradict the stated return value in the docstring).
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
908
            tree_format = repository._format._matchingbzrdir.workingtree_format
2100.3.28 by Aaron Bentley
Make sprout recursive
909
            format.workingtree_format = tree_format.__class__()
3904.3.1 by Andrew Bennetts
Probable fix for GaryvdM's bug when pushing a stacked qbzr branch to Launchpad.
910
        if require_stacking:
911
            format.require_stacking()
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
912
        return format
913
5363.2.14 by Jelmer Vernooij
Move ControlDir.create back to BzrDir.create.
914
    @classmethod
915
    def create(cls, base, format=None, possible_transports=None):
916
        """Create a new BzrDir at the url 'base'.
917
918
        :param format: If supplied, the format of branch to create.  If not
919
            supplied, the default is used.
920
        :param possible_transports: If supplied, a list of transports that
921
            can be reused to share a remote connection.
922
        """
923
        if cls is not BzrDir:
924
            raise AssertionError("BzrDir.create always creates the"
925
                "default format, not one of %r" % cls)
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
926
        t = _mod_transport.get_transport(base, possible_transports)
5363.2.14 by Jelmer Vernooij
Move ControlDir.create back to BzrDir.create.
927
        t.ensure_base()
928
        if format is None:
5363.2.20 by Jelmer Vernooij
use controldir.X
929
            format = controldir.ControlDirFormat.get_default_format()
5363.2.14 by Jelmer Vernooij
Move ControlDir.create back to BzrDir.create.
930
        return format.initialize_on_transport(t)
931
932
4160.1.1 by Robert Collins
Add a BzrDir.pre_open hook for use by the smart server gaol.
933
class BzrDirHooks(hooks.Hooks):
934
    """Hooks for BzrDir operations."""
935
936
    def __init__(self):
937
        """Create the default hooks."""
938
        hooks.Hooks.__init__(self)
939
        self.create_hook(hooks.HookPoint('pre_open',
940
            "Invoked before attempting to open a BzrDir with the transport "
941
            "that the open will use.", (1, 14), None))
5107.3.1 by Marco Pantaleoni
Added the new hooks 'post_branch', 'post_switch' and 'post_repo_init',
942
        self.create_hook(hooks.HookPoint('post_repo_init',
943
            "Invoked after a repository has been initialized. "
944
            "post_repo_init is called with a "
945
            "bzrlib.bzrdir.RepoInitHookParams.",
946
            (2, 2), None))
4160.1.1 by Robert Collins
Add a BzrDir.pre_open hook for use by the smart server gaol.
947
948
# install the default hooks
949
BzrDir.hooks = BzrDirHooks()
950
951
5107.3.1 by Marco Pantaleoni
Added the new hooks 'post_branch', 'post_switch' and 'post_repo_init',
952
class RepoInitHookParams(object):
953
    """Object holding parameters passed to *_repo_init hooks.
954
955
    There are 4 fields that hooks may wish to access:
956
957
    :ivar repository: Repository created
958
    :ivar format: Repository format
959
    :ivar bzrdir: The bzrdir for the repository
960
    :ivar shared: The repository is shared
961
    """
962
963
    def __init__(self, repository, format, a_bzrdir, shared):
964
        """Create a group of RepoInitHook parameters.
965
966
        :param repository: Repository created
967
        :param format: Repository format
968
        :param bzrdir: The bzrdir for the repository
969
        :param shared: The repository is shared
970
        """
5107.3.4 by Marco Pantaleoni
Applied suggestions from merge reviewer (John A Meinel):
971
        self.repository = repository
972
        self.format = format
973
        self.bzrdir = a_bzrdir
974
        self.shared = shared
5107.3.1 by Marco Pantaleoni
Added the new hooks 'post_branch', 'post_switch' and 'post_repo_init',
975
976
    def __eq__(self, other):
977
        return self.__dict__ == other.__dict__
978
979
    def __repr__(self):
980
        if self.repository:
981
            return "<%s for %s>" % (self.__class__.__name__,
982
                self.repository)
983
        else:
984
            return "<%s for %s>" % (self.__class__.__name__,
985
                self.bzrdir)
986
987
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
988
class BzrDirPreSplitOut(BzrDir):
989
    """A common class for the all-in-one formats."""
990
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
991
    def __init__(self, _transport, _format):
992
        """See BzrDir.__init__."""
993
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
994
        self._control_files = lockable_files.LockableFiles(
995
                                            self.get_branch_transport(None),
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
996
                                            self._format._lock_file_name,
997
                                            self._format._lock_class)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
998
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
999
    def break_lock(self):
1000
        """Pre-splitout bzrdirs do not suffer from stale locks."""
1001
        raise NotImplementedError(self.break_lock)
1002
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
1003
    def cloning_metadir(self, require_stacking=False):
3242.2.12 by Aaron Bentley
Get cloning_metadir working properly for old formats
1004
        """Produce a metadir suitable for cloning with."""
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
1005
        if require_stacking:
5363.2.20 by Jelmer Vernooij
use controldir.X
1006
            return controldir.format_registry.make_bzrdir('1.6')
3242.2.12 by Aaron Bentley
Get cloning_metadir working properly for old formats
1007
        return self._format.__class__()
1008
3242.3.37 by Aaron Bentley
Updates from reviews
1009
    def clone(self, url, revision_id=None, force_new_repo=False,
1010
              preserve_stacking=False):
1011
        """See BzrDir.clone().
1012
1013
        force_new_repo has no effect, since this family of formats always
1014
        require a new repository.
1015
        preserve_stacking has no effect, since no source branch using this
1016
        family of formats can be stacked, so there is no stacking to preserve.
1017
        """
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1018
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1019
        result = self._format._initialize_for_clone(url)
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
1020
        self.open_repository().clone(result, revision_id=revision_id)
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
1021
        from_branch = self.open_branch()
1022
        from_branch.clone(result, revision_id=revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1023
        try:
3650.5.7 by Aaron Bentley
Fix working tree initialization
1024
            tree = self.open_workingtree()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1025
        except errors.NotLocalUrl:
1026
            # make a new one, this format always has to have one.
3650.5.7 by Aaron Bentley
Fix working tree initialization
1027
            result._init_workingtree()
1028
        else:
1029
            tree.clone(result)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1030
        return result
1031
5536.1.1 by Andrew Bennetts
Avoid reopening (and relocking) the same branches/repositories in ControlDir.sprout. Still a few rough edges, but the tests I've run are passing.
1032
    def create_branch(self, name=None, repository=None):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1033
        """See BzrDir.create_branch."""
5536.1.1 by Andrew Bennetts
Avoid reopening (and relocking) the same branches/repositories in ControlDir.sprout. Still a few rough edges, but the tests I've run are passing.
1034
        if repository is not None:
1035
            raise NotImplementedError(
1036
                "create_branch(repository=<not None>) on %r" % (self,))
5051.3.10 by Jelmer Vernooij
Pass colocated branch name around in more places.
1037
        return self._format.get_branch_format().initialize(self, name=name)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1038
5051.3.1 by Jelmer Vernooij
Add optional name argument to BzrDir.destroy_branch.
1039
    def destroy_branch(self, name=None):
2796.2.16 by Aaron Bentley
Documentation updates from review
1040
        """See BzrDir.destroy_branch."""
2796.2.6 by Aaron Bentley
Implement destroy_branch
1041
        raise errors.UnsupportedOperation(self.destroy_branch, self)
1042
1534.6.1 by Robert Collins
allow API creation of shared repositories
1043
    def create_repository(self, shared=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1044
        """See BzrDir.create_repository."""
1534.6.1 by Robert Collins
allow API creation of shared repositories
1045
        if shared:
1046
            raise errors.IncompatibleFormat('shared repository', self._format)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1047
        return self.open_repository()
1048
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
1049
    def destroy_repository(self):
1050
        """See BzrDir.destroy_repository."""
1051
        raise errors.UnsupportedOperation(self.destroy_repository, self)
1052
3123.5.2 by Aaron Bentley
Allow checkout --files_from
1053
    def create_workingtree(self, revision_id=None, from_branch=None,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1054
                           accelerator_tree=None, hardlink=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1055
        """See BzrDir.create_workingtree."""
3650.5.6 by Aaron Bentley
Fix cloning problems by creating missing working tree files
1056
        # The workingtree is sometimes created when the bzrdir is created,
1057
        # but not when cloning.
1058
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1059
        # this looks buggy but is not -really-
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1060
        # because this format creates the workingtree when the bzrdir is
1061
        # created
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1062
        # clone and sprout will have set the revision_id
1063
        # and that will have set it for us, its only
1064
        # specific uses of create_workingtree in isolation
1065
        # that can do wonky stuff here, and that only
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1066
        # happens for creating checkouts, which cannot be
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1067
        # done on this format anyway. So - acceptable wart.
4580.4.1 by Martin Pool
Give a warning if --hardlink can't be supported
1068
        if hardlink:
1069
            warning("can't support hardlinked working trees in %r"
1070
                % (self,))
3650.5.6 by Aaron Bentley
Fix cloning problems by creating missing working tree files
1071
        try:
1072
            result = self.open_workingtree(recommend_upgrade=False)
1073
        except errors.NoSuchFile:
1074
            result = self._init_workingtree()
1508.1.24 by Robert Collins
Add update command for use with checkouts.
1075
        if revision_id is not None:
1996.3.12 by John Arbash Meinel
Change how 'revision' is imported to avoid problems later
1076
            if revision_id == _mod_revision.NULL_REVISION:
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
1077
                result.set_parent_ids([])
1078
            else:
1079
                result.set_parent_ids([revision_id])
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1080
        return result
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1081
3650.5.6 by Aaron Bentley
Fix cloning problems by creating missing working tree files
1082
    def _init_workingtree(self):
1083
        from bzrlib.workingtree import WorkingTreeFormat2
1084
        try:
1085
            return WorkingTreeFormat2().initialize(self)
1086
        except errors.NotLocalUrl:
1087
            # Even though we can't access the working tree, we need to
1088
            # create its control files.
3650.5.7 by Aaron Bentley
Fix working tree initialization
1089
            return WorkingTreeFormat2()._stub_initialize_on_transport(
1090
                self.transport, self._control_files._file_mode)
3650.5.6 by Aaron Bentley
Fix cloning problems by creating missing working tree files
1091
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1092
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
1093
        """See BzrDir.destroy_workingtree."""
1094
        raise errors.UnsupportedOperation(self.destroy_workingtree, self)
1095
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1096
    def destroy_workingtree_metadata(self):
1097
        """See BzrDir.destroy_workingtree_metadata."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1098
        raise errors.UnsupportedOperation(self.destroy_workingtree_metadata,
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1099
                                          self)
1100
5051.3.10 by Jelmer Vernooij
Pass colocated branch name around in more places.
1101
    def get_branch_transport(self, branch_format, name=None):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1102
        """See BzrDir.get_branch_transport()."""
5051.3.10 by Jelmer Vernooij
Pass colocated branch name around in more places.
1103
        if name is not None:
1104
            raise errors.NoColocatedBranchSupport(self)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1105
        if branch_format is None:
1106
            return self.transport
1107
        try:
1108
            branch_format.get_format_string()
1109
        except NotImplementedError:
1110
            return self.transport
1111
        raise errors.IncompatibleFormat(branch_format, self._format)
1112
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1113
    def get_repository_transport(self, repository_format):
1114
        """See BzrDir.get_repository_transport()."""
1115
        if repository_format is None:
1116
            return self.transport
1117
        try:
1118
            repository_format.get_format_string()
1119
        except NotImplementedError:
1120
            return self.transport
1121
        raise errors.IncompatibleFormat(repository_format, self._format)
1122
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1123
    def get_workingtree_transport(self, workingtree_format):
1124
        """See BzrDir.get_workingtree_transport()."""
1125
        if workingtree_format is None:
1126
            return self.transport
1127
        try:
1128
            workingtree_format.get_format_string()
1129
        except NotImplementedError:
1130
            return self.transport
1131
        raise errors.IncompatibleFormat(workingtree_format, self._format)
1132
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1133
    def needs_format_conversion(self, format=None):
1134
        """See BzrDir.needs_format_conversion()."""
1135
        # if the format is not the same as the system default,
1136
        # an upgrade is needed.
1137
        if format is None:
3943.2.5 by Martin Pool
deprecate needs_format_conversion(format=None)
1138
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
1139
                % 'needs_format_conversion(format=None)')
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1140
            format = BzrDirFormat.get_default_format()
1141
        return not isinstance(self._format, format.__class__)
1142
5051.3.4 by Jelmer Vernooij
Support name to BzrDir.open_branch.
1143
    def open_branch(self, name=None, unsupported=False,
1144
                    ignore_fallbacks=False):
4734.4.7 by Andrew Bennetts
Defer checking for a repository in NotBranchError case until we format the error as a string. (test_smart currently fails)
1145
        """See BzrDir.open_branch."""
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1146
        from bzrlib.branch import BzrBranchFormat4
1147
        format = BzrBranchFormat4()
1148
        self._check_supported(format, unsupported)
5051.3.13 by Jelmer Vernooij
Pass colocated branch name around rather than raising an exception directly.
1149
        return format.open(self, name, _found=True)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1150
2485.8.56 by Vincent Ladeuil
Fix bug #112173 and bzr branch multiple connections.
1151
    def sprout(self, url, revision_id=None, force_new_repo=False,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1152
               possible_transports=None, accelerator_tree=None,
4054.3.1 by Martin Pool
BzrDirPreSplitOut.sprout should accept source_branch parameter
1153
               hardlink=False, stacked=False, create_tree_if_local=True,
1154
               source_branch=None):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1155
        """See BzrDir.sprout()."""
4054.3.1 by Martin Pool
BzrDirPreSplitOut.sprout should accept source_branch parameter
1156
        if source_branch is not None:
1157
            my_branch = self.open_branch()
1158
            if source_branch.base != my_branch.base:
1159
                raise AssertionError(
1160
                    "source branch %r is not within %r with branch %r" %
1161
                    (source_branch, self, my_branch))
3221.18.4 by Ian Clatworthy
shallow -> stacked
1162
        if stacked:
3221.13.2 by Robert Collins
Add a shallow parameter to bzrdir.sprout, which involved fixing a lateny bug in pack to pack fetching with ghost discovery.
1163
            raise errors.UnstackableBranchFormat(
1164
                self._format, self.root_transport.base)
3983.1.11 by Daniel Watkins
Old BzrDirs which must have working trees are now allowed for in the test.
1165
        if not create_tree_if_local:
1166
            raise errors.MustHaveWorkingTree(
1167
                self._format, self.root_transport.base)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1168
        from bzrlib.workingtree import WorkingTreeFormat2
1169
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1170
        result = self._format._initialize_for_clone(url)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1171
        try:
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
1172
            self.open_repository().clone(result, revision_id=revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1173
        except errors.NoRepositoryPresent:
1174
            pass
1175
        try:
1176
            self.open_branch().sprout(result, revision_id=revision_id)
1177
        except errors.NotBranchError:
1178
            pass
3983.1.4 by Daniel Watkins
Added 'no_tree' parameter to BzrDirPreSplitOut.
1179
3983.1.7 by Daniel Watkins
Review comments from jam.
1180
        # we always want a working tree
1181
        WorkingTreeFormat2().initialize(result,
1182
                                        accelerator_tree=accelerator_tree,
1183
                                        hardlink=hardlink)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1184
        return result
1185
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1186
1187
class BzrDir4(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
1188
    """A .bzr version 4 control object.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1189
1508.1.25 by Robert Collins
Update per review comments.
1190
    This is a deprecated format and may be removed after sept 2006.
1191
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1192
1534.6.1 by Robert Collins
allow API creation of shared repositories
1193
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1194
        """See BzrDir.create_repository."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1195
        return self._format.repository_format.initialize(self, shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1196
1534.5.16 by Robert Collins
Review feedback.
1197
    def needs_format_conversion(self, format=None):
1198
        """Format 4 dirs are always in need of conversion."""
3943.2.5 by Martin Pool
deprecate needs_format_conversion(format=None)
1199
        if format is None:
1200
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
1201
                % 'needs_format_conversion(format=None)')
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1202
        return True
1203
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1204
    def open_repository(self):
1205
        """See BzrDir.open_repository."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1206
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1207
        return RepositoryFormat4().open(self, _found=True)
1208
1209
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1210
class BzrDir5(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
1211
    """A .bzr version 5 control object.
1212
1213
    This is a deprecated format and may be removed after sept 2006.
1214
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1215
4634.47.9 by Andrew Bennetts
has_workingtree is always true for BzrDirFormat5 and BzrDirFormat6.
1216
    def has_workingtree(self):
1217
        """See BzrDir.has_workingtree."""
1218
        return True
1219
    
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1220
    def open_repository(self):
1221
        """See BzrDir.open_repository."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1222
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1223
        return RepositoryFormat5().open(self, _found=True)
1224
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1225
    def open_workingtree(self, _unsupported=False,
1226
            recommend_upgrade=True):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1227
        """See BzrDir.create_workingtree."""
1228
        from bzrlib.workingtree import WorkingTreeFormat2
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1229
        wt_format = WorkingTreeFormat2()
1230
        # we don't warn here about upgrades; that ought to be handled for the
1231
        # bzrdir as a whole
1232
        return wt_format.open(self, _found=True)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1233
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1234
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1235
class BzrDir6(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
1236
    """A .bzr version 6 control object.
1237
1238
    This is a deprecated format and may be removed after sept 2006.
1239
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1240
4634.47.9 by Andrew Bennetts
has_workingtree is always true for BzrDirFormat5 and BzrDirFormat6.
1241
    def has_workingtree(self):
1242
        """See BzrDir.has_workingtree."""
1243
        return True
1244
    
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1245
    def open_repository(self):
1246
        """See BzrDir.open_repository."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1247
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1248
        return RepositoryFormat6().open(self, _found=True)
1249
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1250
    def open_workingtree(self, _unsupported=False,
1251
        recommend_upgrade=True):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1252
        """See BzrDir.create_workingtree."""
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1253
        # we don't warn here about upgrades; that ought to be handled for the
1254
        # bzrdir as a whole
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1255
        from bzrlib.workingtree import WorkingTreeFormat2
1256
        return WorkingTreeFormat2().open(self, _found=True)
1257
1258
1259
class BzrDirMeta1(BzrDir):
1260
    """A .bzr meta version 1 control object.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1261
1262
    This is the first control object where the
1553.5.67 by Martin Pool
doc
1263
    individual aspects are really split out: there are separate repository,
1264
    workingtree and branch subdirectories and any subset of the three can be
1265
    present within a BzrDir.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1266
    """
1267
1534.5.16 by Robert Collins
Review feedback.
1268
    def can_convert_format(self):
1269
        """See BzrDir.can_convert_format()."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1270
        return True
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1271
5536.1.1 by Andrew Bennetts
Avoid reopening (and relocking) the same branches/repositories in ControlDir.sprout. Still a few rough edges, but the tests I've run are passing.
1272
    def create_branch(self, name=None, repository=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1273
        """See BzrDir.create_branch."""
5536.1.1 by Andrew Bennetts
Avoid reopening (and relocking) the same branches/repositories in ControlDir.sprout. Still a few rough edges, but the tests I've run are passing.
1274
        return self._format.get_branch_format().initialize(self, name=name,
1275
                repository=repository)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1276
5051.3.1 by Jelmer Vernooij
Add optional name argument to BzrDir.destroy_branch.
1277
    def destroy_branch(self, name=None):
2796.2.6 by Aaron Bentley
Implement destroy_branch
1278
        """See BzrDir.create_branch."""
5051.3.1 by Jelmer Vernooij
Add optional name argument to BzrDir.destroy_branch.
1279
        if name is not None:
1280
            raise errors.NoColocatedBranchSupport(self)
2796.2.6 by Aaron Bentley
Implement destroy_branch
1281
        self.transport.delete_tree('branch')
1282
1534.6.1 by Robert Collins
allow API creation of shared repositories
1283
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1284
        """See BzrDir.create_repository."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1285
        return self._format.repository_format.initialize(self, shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1286
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
1287
    def destroy_repository(self):
1288
        """See BzrDir.destroy_repository."""
1289
        self.transport.delete_tree('repository')
1290
3123.5.2 by Aaron Bentley
Allow checkout --files_from
1291
    def create_workingtree(self, revision_id=None, from_branch=None,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1292
                           accelerator_tree=None, hardlink=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1293
        """See BzrDir.create_workingtree."""
2955.5.3 by Vincent Ladeuil
Fix second unwanted connection by providing the right branch to create_checkout.
1294
        return self._format.workingtree_format.initialize(
3123.5.2 by Aaron Bentley
Allow checkout --files_from
1295
            self, revision_id, from_branch=from_branch,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1296
            accelerator_tree=accelerator_tree, hardlink=hardlink)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1297
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1298
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
1299
        """See BzrDir.destroy_workingtree."""
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1300
        wt = self.open_workingtree(recommend_upgrade=False)
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1301
        repository = wt.branch.repository
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
1302
        empty = repository.revision_tree(_mod_revision.NULL_REVISION)
5409.1.7 by Vincent Ladeuil
First orphaning implementation (some tests lacking).
1303
        # We ignore the conflicts returned by wt.revert since we're about to
1304
        # delete the wt metadata anyway, all that should be left here are
5409.7.3 by Vincent Ladeuil
Orphan unversioned files to avoid 'missing parent' conflicts
1305
        # detritus. But see bug #634470 about subtree .bzr dirs.
5409.1.7 by Vincent Ladeuil
First orphaning implementation (some tests lacking).
1306
        conflicts = wt.revert(old_tree=empty)
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1307
        self.destroy_workingtree_metadata()
1308
1309
    def destroy_workingtree_metadata(self):
1310
        self.transport.delete_tree('checkout')
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
1311
5147.4.1 by Jelmer Vernooij
Pass branch names in more places.
1312
    def find_branch_format(self, name=None):
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
1313
        """Find the branch 'format' for this bzrdir.
1314
1315
        This might be a synthetic object for e.g. RemoteBranch and SVN.
1316
        """
1317
        from bzrlib.branch import BranchFormat
5147.4.3 by Jelmer Vernooij
Support branch name argument to BzrDir.get_branch_reference.
1318
        return BranchFormat.find_format(self, name=name)
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
1319
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1320
    def _get_mkdir_mode(self):
1321
        """Figure out the mode to use when creating a bzrdir subdir."""
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1322
        temp_control = lockable_files.LockableFiles(self.transport, '',
1323
                                     lockable_files.TransportLock)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1324
        return temp_control._dir_mode
1325
5147.4.3 by Jelmer Vernooij
Support branch name argument to BzrDir.get_branch_reference.
1326
    def get_branch_reference(self, name=None):
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
1327
        """See BzrDir.get_branch_reference()."""
4734.4.7 by Andrew Bennetts
Defer checking for a repository in NotBranchError case until we format the error as a string. (test_smart currently fails)
1328
        from bzrlib.branch import BranchFormat
5147.4.3 by Jelmer Vernooij
Support branch name argument to BzrDir.get_branch_reference.
1329
        format = BranchFormat.find_format(self, name=name)
1330
        return format.get_reference(self, name=name)
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
1331
5051.3.10 by Jelmer Vernooij
Pass colocated branch name around in more places.
1332
    def get_branch_transport(self, branch_format, name=None):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1333
        """See BzrDir.get_branch_transport()."""
5051.3.10 by Jelmer Vernooij
Pass colocated branch name around in more places.
1334
        if name is not None:
1335
            raise errors.NoColocatedBranchSupport(self)
4570.3.6 by Martin Pool
doc
1336
        # XXX: this shouldn't implicitly create the directory if it's just
1337
        # promising to get a transport -- mbp 20090727
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1338
        if branch_format is None:
1339
            return self.transport.clone('branch')
1340
        try:
1341
            branch_format.get_format_string()
1342
        except NotImplementedError:
1343
            raise errors.IncompatibleFormat(branch_format, self._format)
1344
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1345
            self.transport.mkdir('branch', mode=self._get_mkdir_mode())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1346
        except errors.FileExists:
1347
            pass
1348
        return self.transport.clone('branch')
1349
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1350
    def get_repository_transport(self, repository_format):
1351
        """See BzrDir.get_repository_transport()."""
1352
        if repository_format is None:
1353
            return self.transport.clone('repository')
1354
        try:
1355
            repository_format.get_format_string()
1356
        except NotImplementedError:
1357
            raise errors.IncompatibleFormat(repository_format, self._format)
1358
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1359
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1360
        except errors.FileExists:
1361
            pass
1362
        return self.transport.clone('repository')
1363
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1364
    def get_workingtree_transport(self, workingtree_format):
1365
        """See BzrDir.get_workingtree_transport()."""
1366
        if workingtree_format is None:
1367
            return self.transport.clone('checkout')
1368
        try:
1369
            workingtree_format.get_format_string()
1370
        except NotImplementedError:
1371
            raise errors.IncompatibleFormat(workingtree_format, self._format)
1372
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1373
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1374
        except errors.FileExists:
1375
            pass
1376
        return self.transport.clone('checkout')
1377
4634.47.5 by Andrew Bennetts
Add tests, and fix BzrDirMeta1.has_workingtree which was failing if the local transport is decorated with a ChrootTransport or similar.
1378
    def has_workingtree(self):
1379
        """Tell if this bzrdir contains a working tree.
1380
1381
        This will still raise an exception if the bzrdir has a workingtree that
1382
        is remote & inaccessible.
1383
1384
        Note: if you're going to open the working tree, you should just go
1385
        ahead and try, and not ask permission first.
1386
        """
1387
        from bzrlib.workingtree import WorkingTreeFormat
1388
        try:
1389
            WorkingTreeFormat.find_format(self)
1390
        except errors.NoWorkingTree:
1391
            return False
1392
        return True
1393
1534.5.16 by Robert Collins
Review feedback.
1394
    def needs_format_conversion(self, format=None):
1395
        """See BzrDir.needs_format_conversion()."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1396
        if format is None:
3943.2.5 by Martin Pool
deprecate needs_format_conversion(format=None)
1397
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
1398
                % 'needs_format_conversion(format=None)')
1399
        if format is None:
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1400
            format = BzrDirFormat.get_default_format()
1401
        if not isinstance(self._format, format.__class__):
1402
            # it is not a meta dir format, conversion is needed.
1403
            return True
1404
        # we might want to push this down to the repository?
1405
        try:
1406
            if not isinstance(self.open_repository()._format,
1407
                              format.repository_format.__class__):
1408
                # the repository needs an upgrade.
1409
                return True
1410
        except errors.NoRepositoryPresent:
1411
            pass
5051.3.4 by Jelmer Vernooij
Support name to BzrDir.open_branch.
1412
        for branch in self.list_branches():
1413
            if not isinstance(branch._format,
2230.3.55 by Aaron Bentley
Updates from review
1414
                              format.get_branch_format().__class__):
2255.12.1 by Robert Collins
Implement upgrade for working trees.
1415
                # the branch needs an upgrade.
1416
                return True
1417
        try:
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1418
            my_wt = self.open_workingtree(recommend_upgrade=False)
1419
            if not isinstance(my_wt._format,
2255.12.1 by Robert Collins
Implement upgrade for working trees.
1420
                              format.workingtree_format.__class__):
1421
                # the workingtree needs an upgrade.
1422
                return True
2255.2.196 by Robert Collins
Fix test_upgrade defects related to non local or absent working trees.
1423
        except (errors.NoWorkingTree, errors.NotLocalUrl):
2255.12.1 by Robert Collins
Implement upgrade for working trees.
1424
            pass
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1425
        return False
1426
5051.3.4 by Jelmer Vernooij
Support name to BzrDir.open_branch.
1427
    def open_branch(self, name=None, unsupported=False,
1428
                    ignore_fallbacks=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1429
        """See BzrDir.open_branch."""
5147.4.3 by Jelmer Vernooij
Support branch name argument to BzrDir.get_branch_reference.
1430
        format = self.find_branch_format(name=name)
4734.4.7 by Andrew Bennetts
Defer checking for a repository in NotBranchError case until we format the error as a string. (test_smart currently fails)
1431
        self._check_supported(format, unsupported)
5051.3.13 by Jelmer Vernooij
Pass colocated branch name around rather than raising an exception directly.
1432
        return format.open(self, name=name,
1433
            _found=True, ignore_fallbacks=ignore_fallbacks)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1434
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1435
    def open_repository(self, unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1436
        """See BzrDir.open_repository."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1437
        from bzrlib.repository import RepositoryFormat
1438
        format = RepositoryFormat.find_format(self)
1439
        self._check_supported(format, unsupported)
1440
        return format.open(self, _found=True)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1441
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1442
    def open_workingtree(self, unsupported=False,
1443
            recommend_upgrade=True):
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1444
        """See BzrDir.open_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1445
        from bzrlib.workingtree import WorkingTreeFormat
1446
        format = WorkingTreeFormat.find_format(self)
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1447
        self._check_supported(format, unsupported,
1448
            recommend_upgrade,
2323.6.5 by Martin Pool
Recommended-upgrade message should give base dir not the control dir url
1449
            basedir=self.root_transport.base)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1450
        return format.open(self, _found=True)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1451
3242.1.1 by Aaron Bentley
Implement BzrDir configuration
1452
    def _get_config(self):
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
1453
        return config.TransportConfig(self.transport, 'control.conf')
3242.1.1 by Aaron Bentley
Implement BzrDir configuration
1454
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1455
5363.2.20 by Jelmer Vernooij
use controldir.X
1456
class BzrProber(controldir.Prober):
5363.2.8 by Jelmer Vernooij
Docstrings.
1457
    """Prober for formats that use a .bzr/ control directory."""
5363.2.4 by Jelmer Vernooij
Introduce probers, use controldir in a couple more places.
1458
1459
    _formats = {}
1460
    """The known .bzr formats."""
1461
1462
    @classmethod
1463
    def register_bzrdir_format(klass, format):
1464
        klass._formats[format.get_format_string()] = format
1465
1466
    @classmethod
5363.2.6 by Jelmer Vernooij
Add ControlDirFormat.{un,}register_{server_,}prober.
1467
    def unregister_bzrdir_format(klass, format):
5363.2.4 by Jelmer Vernooij
Introduce probers, use controldir in a couple more places.
1468
        del klass._formats[format.get_format_string()]
1469
5363.2.7 by Jelmer Vernooij
Fix tests.
1470
    @classmethod
1471
    def probe_transport(klass, transport):
5363.2.4 by Jelmer Vernooij
Introduce probers, use controldir in a couple more places.
1472
        """Return the .bzrdir style format present in a directory."""
1473
        try:
1474
            format_string = transport.get_bytes(".bzr/branch-format")
1475
        except errors.NoSuchFile:
1476
            raise errors.NotBranchError(path=transport.base)
1477
        try:
5363.2.7 by Jelmer Vernooij
Fix tests.
1478
            return klass._formats[format_string]
5363.2.4 by Jelmer Vernooij
Introduce probers, use controldir in a couple more places.
1479
        except KeyError:
1480
            raise errors.UnknownFormatError(format=format_string, kind='bzrdir')
1481
1482
5363.2.20 by Jelmer Vernooij
use controldir.X
1483
controldir.ControlDirFormat.register_prober(BzrProber)
1484
1485
1486
class RemoteBzrProber(controldir.Prober):
5363.2.8 by Jelmer Vernooij
Docstrings.
1487
    """Prober for remote servers that provide a Bazaar smart server."""
5363.2.4 by Jelmer Vernooij
Introduce probers, use controldir in a couple more places.
1488
1489
    @classmethod
1490
    def probe_transport(klass, transport):
1491
        """Return a RemoteBzrDirFormat object if it looks possible."""
1492
        try:
1493
            medium = transport.get_smart_medium()
1494
        except (NotImplementedError, AttributeError,
1495
                errors.TransportNotPossible, errors.NoSmartMedium,
1496
                errors.SmartProtocolError):
1497
            # no smart server, so not a branch for this format type.
1498
            raise errors.NotBranchError(path=transport.base)
1499
        else:
1500
            # Decline to open it if the server doesn't support our required
1501
            # version (3) so that the VFS-based transport will do it.
1502
            if medium.should_probe():
1503
                try:
1504
                    server_version = medium.protocol_version()
1505
                except errors.SmartProtocolError:
1506
                    # Apparently there's no usable smart server there, even though
1507
                    # the medium supports the smart protocol.
1508
                    raise errors.NotBranchError(path=transport.base)
1509
                if server_version != '2':
1510
                    raise errors.NotBranchError(path=transport.base)
5363.2.7 by Jelmer Vernooij
Fix tests.
1511
            return RemoteBzrDirFormat()
5363.2.4 by Jelmer Vernooij
Introduce probers, use controldir in a couple more places.
1512
1513
5363.2.20 by Jelmer Vernooij
use controldir.X
1514
class BzrDirFormat(controldir.ControlDirFormat):
5363.2.3 by Jelmer Vernooij
Add ControlDirFormat.
1515
    """ControlDirFormat base class for .bzr/ directories.
1534.4.39 by Robert Collins
Basic BzrDir support.
1516
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1517
    Formats are placed in a dict by their format string for reference
1534.4.39 by Robert Collins
Basic BzrDir support.
1518
    during bzrdir opening. These should be subclasses of BzrDirFormat
1519
    for consistency.
1520
1521
    Once a format is deprecated, just deprecate the initialize and open
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1522
    methods on the format class. Do not deprecate the object, as the
1534.4.39 by Robert Collins
Basic BzrDir support.
1523
    object will be created every system load.
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1524
    """
1525
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1526
    _lock_file_name = 'branch-lock'
1527
1528
    # _lock_class must be set in subclasses to the lock type, typ.
1529
    # TransportLock or LockDir
1530
1534.4.39 by Robert Collins
Basic BzrDir support.
1531
    def get_format_string(self):
1532
        """Return the ASCII format string that identifies this format."""
1533
        raise NotImplementedError(self.get_format_string)
1534
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1535
    def initialize_on_transport(self, transport):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1536
        """Initialize a new bzrdir in the base directory of a Transport."""
4017.2.2 by Robert Collins
Perform creation of BzrDirMetaFormat1 control directories using an RPC where possible. (Robert Collins)
1537
        try:
1538
            # can we hand off the request to the smart server rather than using
1539
            # vfs calls?
1540
            client_medium = transport.get_smart_medium()
1541
        except errors.NoSmartMedium:
1542
            return self._initialize_on_transport_vfs(transport)
1543
        else:
1544
            # Current RPC's only know how to create bzr metadir1 instances, so
1545
            # we still delegate to vfs methods if the requested format is not a
1546
            # metadir1
1547
            if type(self) != BzrDirMetaFormat1:
1548
                return self._initialize_on_transport_vfs(transport)
1549
            remote_format = RemoteBzrDirFormat()
1550
            self._supply_sub_formats_to(remote_format)
1551
            return remote_format.initialize_on_transport(transport)
1552
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1553
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
1554
        create_prefix=False, force_new_repo=False, stacked_on=None,
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1555
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
1556
        shared_repo=False, vfs_only=False):
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1557
        """Create this format on transport.
1558
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
1559
        The directory to initialize will be created.
1560
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1561
        :param force_new_repo: Do not use a shared repository for the target,
1562
                               even if one is available.
1563
        :param create_prefix: Create any missing directories leading up to
1564
            to_transport.
1565
        :param use_existing_dir: Use an existing directory if one exists.
1566
        :param stacked_on: A url to stack any created branch on, None to follow
1567
            any target stacking policy.
1568
        :param stack_on_pwd: If stack_on is relative, the location it is
1569
            relative to.
1570
        :param repo_format_name: If non-None, a repository will be
1571
            made-or-found. Should none be found, or if force_new_repo is True
1572
            the repo_format_name is used to select the format of repository to
1573
            create.
1574
        :param make_working_trees: Control the setting of make_working_trees
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1575
            for a new shared repository when one is made. None to use whatever
1576
            default the format has.
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1577
        :param shared_repo: Control whether made repositories are shared or
1578
            not.
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
1579
        :param vfs_only: If True do not attempt to use a smart server
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1580
        :return: repo, bzrdir, require_stacking, repository_policy. repo is
1581
            None if none was created or found, bzrdir is always valid.
1582
            require_stacking is the result of examining the stacked_on
1583
            parameter and any stacking policy found for the target.
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1584
        """
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
1585
        if not vfs_only:
1586
            # Try to hand off to a smart server 
1587
            try:
1588
                client_medium = transport.get_smart_medium()
1589
            except errors.NoSmartMedium:
1590
                pass
1591
            else:
1592
                # TODO: lookup the local format from a server hint.
1593
                remote_dir_format = RemoteBzrDirFormat()
1594
                remote_dir_format._network_name = self.network_name()
1595
                self._supply_sub_formats_to(remote_dir_format)
1596
                return remote_dir_format.initialize_on_transport_ex(transport,
1597
                    use_existing_dir=use_existing_dir, create_prefix=create_prefix,
1598
                    force_new_repo=force_new_repo, stacked_on=stacked_on,
1599
                    stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
1600
                    make_working_trees=make_working_trees, shared_repo=shared_repo)
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1601
        # XXX: Refactor the create_prefix/no_create_prefix code into a
1602
        #      common helper function
1603
        # The destination may not exist - if so make it according to policy.
1604
        def make_directory(transport):
1605
            transport.mkdir('.')
1606
            return transport
1607
        def redirected(transport, e, redirection_notice):
1608
            note(redirection_notice)
1609
            return transport._redirected_to(e.source, e.target)
1610
        try:
1611
            transport = do_catching_redirections(make_directory, transport,
1612
                redirected)
1613
        except errors.FileExists:
1614
            if not use_existing_dir:
1615
                raise
1616
        except errors.NoSuchFile:
1617
            if not create_prefix:
1618
                raise
1619
            transport.create_prefix()
1620
1621
        require_stacking = (stacked_on is not None)
1622
        # Now the target directory exists, but doesn't have a .bzr
1623
        # directory. So we need to create it, along with any work to create
1624
        # all of the dependent branches, etc.
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1625
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1626
        result = self.initialize_on_transport(transport)
1627
        if repo_format_name:
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1628
            try:
1629
                # use a custom format
1630
                result._format.repository_format = \
1631
                    repository.network_format_registry.get(repo_format_name)
1632
            except AttributeError:
1633
                # The format didn't permit it to be set.
1634
                pass
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1635
            # A repository is desired, either in-place or shared.
1636
            repository_policy = result.determine_repository_policy(
1637
                force_new_repo, stacked_on, stack_on_pwd,
1638
                require_stacking=require_stacking)
1639
            result_repo, is_new_repo = repository_policy.acquire_repository(
1640
                make_working_trees, shared_repo)
1641
            if not require_stacking and repository_policy._require_stacking:
1642
                require_stacking = True
1643
                result._format.require_stacking()
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
1644
            result_repo.lock_write()
4294.2.4 by Robert Collins
Move dir, bzrdir and repo acquisition into a single method on bzrdir format.
1645
        else:
1646
            result_repo = None
1647
            repository_policy = None
1648
        return result_repo, result, require_stacking, repository_policy
1649
4017.2.2 by Robert Collins
Perform creation of BzrDirMetaFormat1 control directories using an RPC where possible. (Robert Collins)
1650
    def _initialize_on_transport_vfs(self, transport):
1651
        """Initialize a new bzrdir using VFS calls.
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
1652
4017.2.2 by Robert Collins
Perform creation of BzrDirMetaFormat1 control directories using an RPC where possible. (Robert Collins)
1653
        :param transport: The transport to create the .bzr directory in.
1654
        :return: A
1655
        """
1656
        # Since we are creating a .bzr directory, inherit the
1534.4.39 by Robert Collins
Basic BzrDir support.
1657
        # mode from the root directory
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1658
        temp_control = lockable_files.LockableFiles(transport,
1659
                            '', lockable_files.TransportLock)
1534.4.39 by Robert Collins
Basic BzrDir support.
1660
        temp_control._transport.mkdir('.bzr',
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1661
                                      # FIXME: RBC 20060121 don't peek under
1534.4.39 by Robert Collins
Basic BzrDir support.
1662
                                      # the covers
1663
                                      mode=temp_control._dir_mode)
3224.5.24 by Andrew Bennetts
More minor import tidying suggested by pyflakes.
1664
        if sys.platform == 'win32' and isinstance(transport, local.LocalTransport):
3023.1.2 by Alexander Belchenko
Martin's review.
1665
            win32utils.set_file_attr_hidden(transport._abspath('.bzr'))
1534.4.39 by Robert Collins
Basic BzrDir support.
1666
        file_mode = temp_control._file_mode
1667
        del temp_control
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
1668
        bzrdir_transport = transport.clone('.bzr')
1669
        utf8_files = [('README',
3250.2.1 by Marius Kruger
update .bzr/README to not refer to Bazaar-NG, and add link to website.
1670
                       "This is a Bazaar control directory.\n"
1671
                       "Do not change any files in this directory.\n"
5560.2.1 by Vincent Ladeuil
Fix the remaining references to http://bazaar-vcs.org (except the explicitly historical ones).
1672
                       "See http://bazaar.canonical.com/ for more information about Bazaar.\n"),
1534.4.39 by Robert Collins
Basic BzrDir support.
1673
                      ('branch-format', self.get_format_string()),
1674
                      ]
1675
        # NB: no need to escape relative paths that are url safe.
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
1676
        control_files = lockable_files.LockableFiles(bzrdir_transport,
1677
            self._lock_file_name, self._lock_class)
1553.5.60 by Martin Pool
New LockableFiles.create_lock() method
1678
        control_files.create_lock()
1534.4.39 by Robert Collins
Basic BzrDir support.
1679
        control_files.lock_write()
1680
        try:
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
1681
            for (filename, content) in utf8_files:
3407.2.12 by Martin Pool
Fix creation mode of control files
1682
                bzrdir_transport.put_bytes(filename, content,
1683
                    mode=file_mode)
1534.4.39 by Robert Collins
Basic BzrDir support.
1684
        finally:
1685
            control_files.unlock()
4017.2.2 by Robert Collins
Perform creation of BzrDirMetaFormat1 control directories using an RPC where possible. (Robert Collins)
1686
        return self.open(transport, _found=True)
1534.4.39 by Robert Collins
Basic BzrDir support.
1687
1688
    def open(self, transport, _found=False):
1689
        """Return an instance of this format for the dir transport points at.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1690
1534.4.39 by Robert Collins
Basic BzrDir support.
1691
        _found is a private parameter, do not use it.
1692
        """
1693
        if not _found:
5363.2.20 by Jelmer Vernooij
use controldir.X
1694
            found_format = controldir.ControlDirFormat.find_format(transport)
2090.2.2 by Martin Pool
Fix an assertion with side effects
1695
            if not isinstance(found_format, self.__class__):
1696
                raise AssertionError("%s was asked to open %s, but it seems to need "
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1697
                        "format %s"
2090.2.2 by Martin Pool
Fix an assertion with side effects
1698
                        % (self, transport, found_format))
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1699
            # Allow subclasses - use the found format.
1700
            self._supply_sub_formats_to(found_format)
1701
            return found_format._open(transport)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1702
        return self._open(transport)
1703
1704
    def _open(self, transport):
1705
        """Template method helper for opening BzrDirectories.
1706
1707
        This performs the actual open and any additional logic or parameter
1708
        passing.
1709
        """
1710
        raise NotImplementedError(self._open)
1534.4.39 by Robert Collins
Basic BzrDir support.
1711
1712
    @classmethod
1713
    def register_format(klass, format):
5363.2.4 by Jelmer Vernooij
Introduce probers, use controldir in a couple more places.
1714
        BzrProber.register_bzrdir_format(format)
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
1715
        # bzr native formats have a network name of their format string.
5363.2.23 by Jelmer Vernooij
Move network_format_registry to bzrlib.controldir.
1716
        controldir.network_format_registry.register(format.get_format_string(), format.__class__)
5363.2.20 by Jelmer Vernooij
use controldir.X
1717
        controldir.ControlDirFormat.register_format(format)
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1718
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1719
    def _supply_sub_formats_to(self, other_format):
1720
        """Give other_format the same values for sub formats as this has.
1721
1722
        This method is expected to be used when parameterising a
1723
        RemoteBzrDirFormat instance with the parameters from a
1724
        BzrDirMetaFormat1 instance.
1725
1726
        :param other_format: other_format is a format which should be
1727
            compatible with whatever sub formats are supported by self.
1728
        :return: None.
1729
        """
1730
1534.4.39 by Robert Collins
Basic BzrDir support.
1731
    @classmethod
1732
    def unregister_format(klass, format):
5363.2.6 by Jelmer Vernooij
Add ControlDirFormat.{un,}register_{server_,}prober.
1733
        BzrProber.unregister_bzrdir_format(format)
5363.2.20 by Jelmer Vernooij
use controldir.X
1734
        controldir.ControlDirFormat.unregister_format(format)
5363.2.23 by Jelmer Vernooij
Move network_format_registry to bzrlib.controldir.
1735
        controldir.network_format_registry.remove(format.get_format_string())
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1736
1737
1534.4.39 by Robert Collins
Basic BzrDir support.
1738
class BzrDirFormat4(BzrDirFormat):
1739
    """Bzr dir format 4.
1740
1741
    This format is a combined format for working tree, branch and repository.
1742
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1743
     - Format 1 working trees [always]
1744
     - Format 4 branches [always]
1745
     - Format 4 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1746
1747
    This format is deprecated: it indexes texts using a text it which is
1748
    removed in format 5; write support for this format has been removed.
1749
    """
1750
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1751
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1752
1534.4.39 by Robert Collins
Basic BzrDir support.
1753
    def get_format_string(self):
1754
        """See BzrDirFormat.get_format_string()."""
1755
        return "Bazaar-NG branch, format 0.0.4\n"
1756
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1757
    def get_format_description(self):
1758
        """See BzrDirFormat.get_format_description()."""
1759
        return "All-in-one format 4"
1760
1534.5.16 by Robert Collins
Review feedback.
1761
    def get_converter(self, format=None):
1762
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1763
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1764
        return ConvertBzrDir4To5()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1765
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1766
    def initialize_on_transport(self, transport):
1534.4.39 by Robert Collins
Basic BzrDir support.
1767
        """Format 4 branches cannot be created."""
1768
        raise errors.UninitializableFormat(self)
1769
1770
    def is_supported(self):
1771
        """Format 4 is not supported.
1772
1773
        It is not supported because the model changed from 4 to 5 and the
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1774
        conversion logic is expensive - so doing it on the fly was not
1534.4.39 by Robert Collins
Basic BzrDir support.
1775
        feasible.
1776
        """
1777
        return False
1778
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
1779
    def network_name(self):
1780
        return self.get_format_string()
1781
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1782
    def _open(self, transport):
1783
        """See BzrDirFormat._open."""
1784
        return BzrDir4(transport, self)
1785
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1786
    def __return_repository_format(self):
1787
        """Circular import protection."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1788
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1789
        return RepositoryFormat4()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1790
    repository_format = property(__return_repository_format)
1791
1534.4.39 by Robert Collins
Basic BzrDir support.
1792
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1793
class BzrDirFormatAllInOne(BzrDirFormat):
1794
    """Common class for formats before meta-dirs."""
1795
1796
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
1797
        create_prefix=False, force_new_repo=False, stacked_on=None,
1798
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
1799
        shared_repo=False):
1800
        """See BzrDirFormat.initialize_on_transport_ex."""
1801
        require_stacking = (stacked_on is not None)
4294.2.10 by Robert Collins
Review feedback.
1802
        # Format 5 cannot stack, but we've been asked to - actually init
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1803
        # a Meta1Dir
1804
        if require_stacking:
1805
            format = BzrDirMetaFormat1()
1806
            return format.initialize_on_transport_ex(transport,
1807
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
1808
                force_new_repo=force_new_repo, stacked_on=stacked_on,
1809
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
1810
                make_working_trees=make_working_trees, shared_repo=shared_repo)
1811
        return BzrDirFormat.initialize_on_transport_ex(self, transport,
1812
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
1813
            force_new_repo=force_new_repo, stacked_on=stacked_on,
1814
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
1815
            make_working_trees=make_working_trees, shared_repo=shared_repo)
1816
1817
1818
class BzrDirFormat5(BzrDirFormatAllInOne):
1534.4.39 by Robert Collins
Basic BzrDir support.
1819
    """Bzr control format 5.
1820
1821
    This format is a combined format for working tree, branch and repository.
1822
    It has:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1823
     - Format 2 working trees [always]
1824
     - Format 4 branches [always]
1534.4.53 by Robert Collins
Review feedback from John Meinel.
1825
     - Format 5 repositories [always]
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1826
       Unhashed stores in the repository.
1534.4.39 by Robert Collins
Basic BzrDir support.
1827
    """
1828
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1829
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1830
1534.4.39 by Robert Collins
Basic BzrDir support.
1831
    def get_format_string(self):
1832
        """See BzrDirFormat.get_format_string()."""
1833
        return "Bazaar-NG branch, format 5\n"
1834
3650.2.2 by Aaron Bentley
Implement get_branch_format, to unify branch creation code
1835
    def get_branch_format(self):
1836
        from bzrlib import branch
1837
        return branch.BzrBranchFormat4()
1838
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1839
    def get_format_description(self):
1840
        """See BzrDirFormat.get_format_description()."""
1841
        return "All-in-one format 5"
1842
1534.5.16 by Robert Collins
Review feedback.
1843
    def get_converter(self, format=None):
1844
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1845
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1846
        return ConvertBzrDir5To6()
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1847
1848
    def _initialize_for_clone(self, url):
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
1849
        return self.initialize_on_transport(
1850
            _mod_transport.get_transport(url), _cloning=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1851
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1852
    def initialize_on_transport(self, transport, _cloning=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1853
        """Format 5 dirs always have working tree, branch and repository.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1854
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1855
        Except when they are being cloned.
1856
        """
1857
        from bzrlib.branch import BzrBranchFormat4
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1858
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1859
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1860
        RepositoryFormat5().initialize(result, _internal=True)
1861
        if not _cloning:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1862
            branch = BzrBranchFormat4().initialize(result)
3650.5.6 by Aaron Bentley
Fix cloning problems by creating missing working tree files
1863
            result._init_workingtree()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1864
        return result
1865
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
1866
    def network_name(self):
1867
        return self.get_format_string()
1868
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1869
    def _open(self, transport):
1870
        """See BzrDirFormat._open."""
1871
        return BzrDir5(transport, self)
1872
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1873
    def __return_repository_format(self):
1874
        """Circular import protection."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1875
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1876
        return RepositoryFormat5()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1877
    repository_format = property(__return_repository_format)
1878
1534.4.39 by Robert Collins
Basic BzrDir support.
1879
4294.2.5 by Robert Collins
Reasonable unit test coverage for initialize_on_transport_ex.
1880
class BzrDirFormat6(BzrDirFormatAllInOne):
1534.4.39 by Robert Collins
Basic BzrDir support.
1881
    """Bzr control format 6.
1882
1883
    This format is a combined format for working tree, branch and repository.
1884
    It has:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1885
     - Format 2 working trees [always]
1886
     - Format 4 branches [always]
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1887
     - Format 6 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1888
    """
1889
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1890
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1891
1534.4.39 by Robert Collins
Basic BzrDir support.
1892
    def get_format_string(self):
1893
        """See BzrDirFormat.get_format_string()."""
1894
        return "Bazaar-NG branch, format 6\n"
1895
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1896
    def get_format_description(self):
1897
        """See BzrDirFormat.get_format_description()."""
1898
        return "All-in-one format 6"
1899
3650.2.2 by Aaron Bentley
Implement get_branch_format, to unify branch creation code
1900
    def get_branch_format(self):
1901
        from bzrlib import branch
1902
        return branch.BzrBranchFormat4()
1903
1534.5.16 by Robert Collins
Review feedback.
1904
    def get_converter(self, format=None):
1905
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1906
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1907
        return ConvertBzrDir6ToMeta()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1908
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1909
    def _initialize_for_clone(self, url):
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
1910
        return self.initialize_on_transport(
1911
            _mod_transport.get_transport(url), _cloning=True)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1912
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1913
    def initialize_on_transport(self, transport, _cloning=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1914
        """Format 6 dirs always have working tree, branch and repository.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1915
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1916
        Except when they are being cloned.
1917
        """
1918
        from bzrlib.branch import BzrBranchFormat4
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1919
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1920
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1921
        RepositoryFormat6().initialize(result, _internal=True)
1922
        if not _cloning:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1923
            branch = BzrBranchFormat4().initialize(result)
3650.5.7 by Aaron Bentley
Fix working tree initialization
1924
            result._init_workingtree()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1925
        return result
1926
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
1927
    def network_name(self):
1928
        return self.get_format_string()
1929
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1930
    def _open(self, transport):
1931
        """See BzrDirFormat._open."""
1932
        return BzrDir6(transport, self)
1933
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1934
    def __return_repository_format(self):
1935
        """Circular import protection."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1936
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1937
        return RepositoryFormat6()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1938
    repository_format = property(__return_repository_format)
1939
1534.4.39 by Robert Collins
Basic BzrDir support.
1940
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1941
class BzrDirMetaFormat1(BzrDirFormat):
1942
    """Bzr meta control format 1
1943
1944
    This is the first format with split out working tree, branch and repository
1945
    disk storage.
1946
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1947
     - Format 3 working trees [optional]
1948
     - Format 5 branches [optional]
1949
     - Format 7 repositories [optional]
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1950
    """
1951
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1952
    _lock_class = lockdir.LockDir
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1953
2100.3.10 by Aaron Bentley
Ensure added references are serialized properly, beef up Workingtreee3
1954
    def __init__(self):
1955
        self._workingtree_format = None
2230.3.1 by Aaron Bentley
Get branch6 creation working
1956
        self._branch_format = None
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
1957
        self._repository_format = None
2100.3.10 by Aaron Bentley
Ensure added references are serialized properly, beef up Workingtreee3
1958
2100.3.15 by Aaron Bentley
get test suite passing
1959
    def __eq__(self, other):
1960
        if other.__class__ is not self.__class__:
1961
            return False
1962
        if other.repository_format != self.repository_format:
1963
            return False
1964
        if other.workingtree_format != self.workingtree_format:
1965
            return False
1966
        return True
1967
2100.3.35 by Aaron Bentley
equality operations on bzrdir
1968
    def __ne__(self, other):
1969
        return not self == other
1970
2230.3.55 by Aaron Bentley
Updates from review
1971
    def get_branch_format(self):
2230.3.1 by Aaron Bentley
Get branch6 creation working
1972
        if self._branch_format is None:
1973
            from bzrlib.branch import BranchFormat
1974
            self._branch_format = BranchFormat.get_default_format()
1975
        return self._branch_format
1976
2230.3.55 by Aaron Bentley
Updates from review
1977
    def set_branch_format(self, format):
2230.3.1 by Aaron Bentley
Get branch6 creation working
1978
        self._branch_format = format
1979
4456.2.1 by Andrew Bennetts
Fix automatic branch format upgrades triggered by a default stacking policy on a 1.16rc1 (or later) smart server.
1980
    def require_stacking(self, stack_on=None, possible_transports=None,
1981
            _skip_repo=False):
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
1982
        """We have a request to stack, try to ensure the formats support it.
1983
1984
        :param stack_on: If supplied, it is the URL to a branch that we want to
1985
            stack on. Check to see if that format supports stacking before
1986
            forcing an upgrade.
1987
        """
1988
        # Stacking is desired. requested by the target, but does the place it
1989
        # points at support stacking? If it doesn't then we should
1990
        # not implicitly upgrade. We check this here.
1991
        new_repo_format = None
1992
        new_branch_format = None
1993
1994
        # a bit of state for get_target_branch so that we don't try to open it
1995
        # 2 times, for both repo *and* branch
1996
        target = [None, False, None] # target_branch, checked, upgrade anyway
1997
        def get_target_branch():
1998
            if target[1]:
1999
                # We've checked, don't check again
2000
                return target
2001
            if stack_on is None:
2002
                # No target format, that means we want to force upgrading
2003
                target[:] = [None, True, True]
2004
                return target
2005
            try:
2006
                target_dir = BzrDir.open(stack_on,
2007
                    possible_transports=possible_transports)
2008
            except errors.NotBranchError:
2009
                # Nothing there, don't change formats
2010
                target[:] = [None, True, False]
2011
                return target
2012
            except errors.JailBreak:
2013
                # JailBreak, JFDI and upgrade anyway
2014
                target[:] = [None, True, True]
2015
                return target
2016
            try:
2017
                target_branch = target_dir.open_branch()
2018
            except errors.NotBranchError:
2019
                # No branch, don't upgrade formats
2020
                target[:] = [None, True, False]
2021
                return target
2022
            target[:] = [target_branch, True, False]
2023
            return target
2024
4456.2.1 by Andrew Bennetts
Fix automatic branch format upgrades triggered by a default stacking policy on a 1.16rc1 (or later) smart server.
2025
        if (not _skip_repo and
2026
                 not self.repository_format.supports_external_lookups):
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
2027
            # We need to upgrade the Repository.
2028
            target_branch, _, do_upgrade = get_target_branch()
2029
            if target_branch is None:
2030
                # We don't have a target branch, should we upgrade anyway?
2031
                if do_upgrade:
2032
                    # stack_on is inaccessible, JFDI.
2033
                    # TODO: bad monkey, hard-coded formats...
2034
                    if self.repository_format.rich_root_data:
4401.1.3 by John Arbash Meinel
Change back to defaulting to --1.6 format, and update the blackbox tests.
2035
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5RichRoot()
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
2036
                    else:
4401.1.3 by John Arbash Meinel
Change back to defaulting to --1.6 format, and update the blackbox tests.
2037
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5()
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
2038
            else:
2039
                # If the target already supports stacking, then we know the
2040
                # project is already able to use stacking, so auto-upgrade
2041
                # for them
2042
                new_repo_format = target_branch.repository._format
2043
                if not new_repo_format.supports_external_lookups:
2044
                    # target doesn't, source doesn't, so don't auto upgrade
2045
                    # repo
2046
                    new_repo_format = None
2047
            if new_repo_format is not None:
2048
                self.repository_format = new_repo_format
2049
                note('Source repository format does not support stacking,'
4401.1.3 by John Arbash Meinel
Change back to defaulting to --1.6 format, and update the blackbox tests.
2050
                     ' using format:\n  %s',
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
2051
                     new_repo_format.get_format_description())
2052
3904.3.1 by Andrew Bennetts
Probable fix for GaryvdM's bug when pushing a stacked qbzr branch to Launchpad.
2053
        if not self.get_branch_format().supports_stacking():
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
2054
            # We just checked the repo, now lets check if we need to
2055
            # upgrade the branch format
2056
            target_branch, _, do_upgrade = get_target_branch()
2057
            if target_branch is None:
2058
                if do_upgrade:
2059
                    # TODO: bad monkey, hard-coded formats...
2060
                    new_branch_format = branch.BzrBranchFormat7()
3904.3.1 by Andrew Bennetts
Probable fix for GaryvdM's bug when pushing a stacked qbzr branch to Launchpad.
2061
            else:
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
2062
                new_branch_format = target_branch._format
2063
                if not new_branch_format.supports_stacking():
2064
                    new_branch_format = None
2065
            if new_branch_format is not None:
2066
                # Does support stacking, use its format.
2067
                self.set_branch_format(new_branch_format)
2068
                note('Source branch format does not support stacking,'
4401.1.3 by John Arbash Meinel
Change back to defaulting to --1.6 format, and update the blackbox tests.
2069
                     ' using format:\n  %s',
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
2070
                     new_branch_format.get_format_description())
3904.3.1 by Andrew Bennetts
Probable fix for GaryvdM's bug when pushing a stacked qbzr branch to Launchpad.
2071
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2072
    def get_converter(self, format=None):
2073
        """See BzrDirFormat.get_converter()."""
2074
        if format is None:
2075
            format = BzrDirFormat.get_default_format()
2076
        if not isinstance(self, format.__class__):
2077
            # converting away from metadir is not implemented
2078
            raise NotImplementedError(self.get_converter)
2079
        return ConvertMetaToMeta(format)
2080
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
2081
    def get_format_string(self):
2082
        """See BzrDirFormat.get_format_string()."""
2083
        return "Bazaar-NG meta directory, format 1\n"
2084
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
2085
    def get_format_description(self):
2086
        """See BzrDirFormat.get_format_description()."""
2087
        return "Meta directory format 1"
2088
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
2089
    def network_name(self):
2090
        return self.get_format_string()
2091
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
2092
    def _open(self, transport):
2093
        """See BzrDirFormat._open."""
4294.2.12 by Robert Collins
Prevent aliasing issues with BzrDirMetaFormat1 by making a new format object in _open.
2094
        # Create a new format instance because otherwise initialisation of new
2095
        # metadirs share the global default format object leading to alias
2096
        # problems.
2097
        format = BzrDirMetaFormat1()
2098
        self._supply_sub_formats_to(format)
2099
        return BzrDirMeta1(transport, format)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
2100
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2101
    def __return_repository_format(self):
2102
        """Circular import protection."""
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
2103
        if self._repository_format:
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2104
            return self._repository_format
2105
        from bzrlib.repository import RepositoryFormat
2106
        return RepositoryFormat.get_default_format()
2107
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2108
    def _set_repository_format(self, value):
3015.2.8 by Robert Collins
Typo in __set_repository_format's docstring.
2109
        """Allow changing the repository format for metadir formats."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2110
        self._repository_format = value
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
2111
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2112
    repository_format = property(__return_repository_format,
2113
        _set_repository_format)
2114
2115
    def _supply_sub_formats_to(self, other_format):
2116
        """Give other_format the same values for sub formats as this has.
2117
2118
        This method is expected to be used when parameterising a
2119
        RemoteBzrDirFormat instance with the parameters from a
2120
        BzrDirMetaFormat1 instance.
2121
2122
        :param other_format: other_format is a format which should be
2123
            compatible with whatever sub formats are supported by self.
2124
        :return: None.
2125
        """
2126
        if getattr(self, '_repository_format', None) is not None:
2127
            other_format.repository_format = self.repository_format
2128
        if self._branch_format is not None:
2129
            other_format._branch_format = self._branch_format
2130
        if self._workingtree_format is not None:
2131
            other_format.workingtree_format = self.workingtree_format
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2132
2100.3.10 by Aaron Bentley
Ensure added references are serialized properly, beef up Workingtreee3
2133
    def __get_workingtree_format(self):
2134
        if self._workingtree_format is None:
2135
            from bzrlib.workingtree import WorkingTreeFormat
2136
            self._workingtree_format = WorkingTreeFormat.get_default_format()
2137
        return self._workingtree_format
2138
2139
    def __set_workingtree_format(self, wt_format):
2140
        self._workingtree_format = wt_format
2141
2142
    workingtree_format = property(__get_workingtree_format,
2143
                                  __set_workingtree_format)
2144
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
2145
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
2146
# Register bzr formats
1534.4.39 by Robert Collins
Basic BzrDir support.
2147
BzrDirFormat.register_format(BzrDirFormat4())
2148
BzrDirFormat.register_format(BzrDirFormat5())
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
2149
BzrDirFormat.register_format(BzrDirFormat6())
2150
__default_format = BzrDirMetaFormat1()
1534.4.39 by Robert Collins
Basic BzrDir support.
2151
BzrDirFormat.register_format(__default_format)
5363.2.20 by Jelmer Vernooij
use controldir.X
2152
controldir.ControlDirFormat._default_format = __default_format
1534.4.39 by Robert Collins
Basic BzrDir support.
2153
2154
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2155
class Converter(object):
2156
    """Converts a disk format object from one format to another."""
2157
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2158
    def convert(self, to_convert, pb):
2159
        """Perform the conversion of to_convert, giving feedback via pb.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2160
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2161
        :param to_convert: The disk object to convert.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2162
        :param pb: a progress bar to use for progress information.
2163
        """
2164
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2165
    def step(self, message):
2166
        """Update the pb by a step."""
2167
        self.count +=1
2168
        self.pb.update(message, self.count, self.total)
2169
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2170
2171
class ConvertBzrDir4To5(Converter):
2172
    """Converts format 4 bzr dirs to format 5."""
2173
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2174
    def __init__(self):
2175
        super(ConvertBzrDir4To5, self).__init__()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2176
        self.converted_revs = set()
2177
        self.absent_revisions = set()
2178
        self.text_count = 0
2179
        self.revisions = {}
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2180
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2181
    def convert(self, to_convert, pb):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2182
        """See Converter.convert()."""
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2183
        self.bzrdir = to_convert
4961.2.14 by Martin Pool
Further pb cleanups
2184
        if pb is not None:
2185
            warnings.warn("pb parameter to convert() is deprecated")
2186
        self.pb = ui.ui_factory.nested_progress_bar()
2187
        try:
2188
            ui.ui_factory.note('starting upgrade from format 4 to 5')
2189
            if isinstance(self.bzrdir.transport, local.LocalTransport):
2190
                self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
2191
            self._convert_to_weaves()
5158.6.9 by Martin Pool
Simplify various code to use user_url
2192
            return BzrDir.open(self.bzrdir.user_url)
4961.2.14 by Martin Pool
Further pb cleanups
2193
        finally:
2194
            self.pb.finished()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2195
2196
    def _convert_to_weaves(self):
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
2197
        ui.ui_factory.note('note: upgrade may be faster if all store files are ungzipped first')
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2198
        try:
2199
            # TODO permissions
2200
            stat = self.bzrdir.transport.stat('weaves')
2201
            if not S_ISDIR(stat.st_mode):
2202
                self.bzrdir.transport.delete('weaves')
2203
                self.bzrdir.transport.mkdir('weaves')
2204
        except errors.NoSuchFile:
2205
            self.bzrdir.transport.mkdir('weaves')
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
2206
        # deliberately not a WeaveFile as we want to build it up slowly.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2207
        self.inv_weave = Weave('inventory')
2208
        # holds in-memory weaves for all files
2209
        self.text_weaves = {}
2210
        self.bzrdir.transport.delete('branch-format')
2211
        self.branch = self.bzrdir.open_branch()
2212
        self._convert_working_inv()
2213
        rev_history = self.branch.revision_history()
2214
        # to_read is a stack holding the revisions we still need to process;
2215
        # appending to it adds new highest-priority revisions
2216
        self.known_revisions = set(rev_history)
2217
        self.to_read = rev_history[-1:]
2218
        while self.to_read:
2219
            rev_id = self.to_read.pop()
2220
            if (rev_id not in self.revisions
2221
                and rev_id not in self.absent_revisions):
2222
                self._load_one_rev(rev_id)
2223
        self.pb.clear()
2224
        to_import = self._make_order()
2225
        for i, rev_id in enumerate(to_import):
2226
            self.pb.update('converting revision', i, len(to_import))
2227
            self._convert_one_rev(rev_id)
2228
        self.pb.clear()
2229
        self._write_all_weaves()
2230
        self._write_all_revs()
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
2231
        ui.ui_factory.note('upgraded to weaves:')
2232
        ui.ui_factory.note('  %6d revisions and inventories' % len(self.revisions))
2233
        ui.ui_factory.note('  %6d revisions not present' % len(self.absent_revisions))
2234
        ui.ui_factory.note('  %6d texts' % self.text_count)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2235
        self._cleanup_spare_files_after_format4()
3407.2.13 by Martin Pool
Remove indirection through control_files to get transports
2236
        self.branch._transport.put_bytes(
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
2237
            'branch-format',
2238
            BzrDirFormat5().get_format_string(),
3446.1.1 by Martin Pool
merge further LockableFile deprecations
2239
            mode=self.bzrdir._get_file_mode())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2240
2241
    def _cleanup_spare_files_after_format4(self):
2242
        # FIXME working tree upgrade foo.
2243
        for n in 'merged-patches', 'pending-merged-patches':
2244
            try:
2245
                ## assert os.path.getsize(p) == 0
2246
                self.bzrdir.transport.delete(n)
2247
            except errors.NoSuchFile:
2248
                pass
2249
        self.bzrdir.transport.delete_tree('inventory-store')
2250
        self.bzrdir.transport.delete_tree('text-store')
2251
2252
    def _convert_working_inv(self):
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
2253
        inv = xml4.serializer_v4.read_inventory(
3407.2.1 by Martin Pool
Deprecate LockableFiles.get
2254
                self.branch._transport.get('inventory'))
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
2255
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv, working=True)
3407.2.1 by Martin Pool
Deprecate LockableFiles.get
2256
        self.branch._transport.put_bytes('inventory', new_inv_xml,
3446.1.1 by Martin Pool
merge further LockableFile deprecations
2257
            mode=self.bzrdir._get_file_mode())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2258
2259
    def _write_all_weaves(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
2260
        controlweaves = VersionedFileStore(self.bzrdir.transport, prefixed=False,
2261
                                           versionedfile_class=WeaveFile)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2262
        weave_transport = self.bzrdir.transport.clone('weaves')
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
2263
        weaves = VersionedFileStore(weave_transport, prefixed=False,
2264
                            versionedfile_class=WeaveFile)
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
2265
        transaction = WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2266
2267
        try:
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
2268
            i = 0
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2269
            for file_id, file_weave in self.text_weaves.items():
2270
                self.pb.update('writing weave', i, len(self.text_weaves))
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
2271
                weaves._put_weave(file_id, file_weave, transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2272
                i += 1
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
2273
            self.pb.update('inventory', 0, 1)
2274
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
2275
            self.pb.update('inventory', 1, 1)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2276
        finally:
2277
            self.pb.clear()
2278
2279
    def _write_all_revs(self):
2280
        """Write all revisions out in new form."""
2281
        self.bzrdir.transport.delete_tree('revision-store')
2282
        self.bzrdir.transport.mkdir('revision-store')
2283
        revision_transport = self.bzrdir.transport.clone('revision-store')
2284
        # TODO permissions
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2285
        from bzrlib.xml5 import serializer_v5
3350.6.10 by Martin Pool
VersionedFiles review cleanups
2286
        from bzrlib.repofmt.weaverepo import RevisionTextStore
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2287
        revision_store = RevisionTextStore(revision_transport,
2288
            serializer_v5, False, versionedfile.PrefixMapper(),
2289
            lambda:True, lambda:True)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2290
        try:
2291
            for i, rev_id in enumerate(self.converted_revs):
2292
                self.pb.update('write revision', i, len(self.converted_revs))
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2293
                text = serializer_v5.write_revision_to_string(
2294
                    self.revisions[rev_id])
2295
                key = (rev_id,)
2296
                revision_store.add_lines(key, None, osutils.split_lines(text))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2297
        finally:
2298
            self.pb.clear()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2299
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2300
    def _load_one_rev(self, rev_id):
2301
        """Load a revision object into memory.
2302
2303
        Any parents not either loaded or abandoned get queued to be
2304
        loaded."""
2305
        self.pb.update('loading revision',
2306
                       len(self.revisions),
2307
                       len(self.known_revisions))
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
2308
        if not self.branch.repository.has_revision(rev_id):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2309
            self.pb.clear()
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
2310
            ui.ui_factory.note('revision {%s} not present in branch; '
2311
                         'will be converted as a ghost' %
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2312
                         rev_id)
2313
            self.absent_revisions.add(rev_id)
2314
        else:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2315
            rev = self.branch.repository.get_revision(rev_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2316
            for parent_id in rev.parent_ids:
2317
                self.known_revisions.add(parent_id)
2318
                self.to_read.append(parent_id)
2319
            self.revisions[rev_id] = rev
2320
2321
    def _load_old_inventory(self, rev_id):
4708.2.2 by Martin
Workingtree changes sitting around since November, more explict closing of files in bzrlib
2322
        f = self.branch.repository.inventory_store.get(rev_id)
2323
        try:
2324
            old_inv_xml = f.read()
2325
        finally:
2326
            f.close()
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
2327
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
1910.2.36 by Aaron Bentley
Get upgrade from format4 under test and fixed for all formats
2328
        inv.revision_id = rev_id
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2329
        rev = self.revisions[rev_id]
2330
        return inv
2331
2332
    def _load_updated_inventory(self, rev_id):
2333
        inv_xml = self.inv_weave.get_text(rev_id)
3169.2.2 by Robert Collins
Add a test to Repository.deserialise_inventory that the resulting ivnentory is the one asked for, and update relevant tests. Also tweak the model 1 to 2 regenerate inventories logic to use the revision trees parent marker which is more accurate in some cases.
2334
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml, rev_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2335
        return inv
2336
2337
    def _convert_one_rev(self, rev_id):
2338
        """Convert revision and all referenced objects to new format."""
2339
        rev = self.revisions[rev_id]
2340
        inv = self._load_old_inventory(rev_id)
2341
        present_parents = [p for p in rev.parent_ids
2342
                           if p not in self.absent_revisions]
2343
        self._convert_revision_contents(rev, inv, present_parents)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
2344
        self._store_new_inv(rev, inv, present_parents)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2345
        self.converted_revs.add(rev_id)
2346
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
2347
    def _store_new_inv(self, rev, inv, present_parents):
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
2348
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2349
        new_inv_sha1 = sha_string(new_inv_xml)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
2350
        self.inv_weave.add_lines(rev.revision_id,
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
2351
                                 present_parents,
2352
                                 new_inv_xml.splitlines(True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2353
        rev.inventory_sha1 = new_inv_sha1
2354
2355
    def _convert_revision_contents(self, rev, inv, present_parents):
2356
        """Convert all the files within a revision.
2357
2358
        Also upgrade the inventory to refer to the text revision ids."""
2359
        rev_id = rev.revision_id
2360
        mutter('converting texts of revision {%s}',
2361
               rev_id)
2362
        parent_invs = map(self._load_updated_inventory, present_parents)
1731.1.62 by Aaron Bentley
Changes from review comments
2363
        entries = inv.iter_entries()
2364
        entries.next()
2365
        for path, ie in entries:
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2366
            self._convert_file_version(rev, ie, parent_invs)
2367
2368
    def _convert_file_version(self, rev, ie, parent_invs):
2369
        """Convert one version of one file.
2370
2371
        The file needs to be added into the weave if it is a merge
2372
        of >=2 parents or if it's changed from its parent.
2373
        """
2374
        file_id = ie.file_id
2375
        rev_id = rev.revision_id
2376
        w = self.text_weaves.get(file_id)
2377
        if w is None:
2378
            w = Weave(file_id)
2379
            self.text_weaves[file_id] = w
2380
        text_changed = False
2776.1.5 by Robert Collins
Add reasonably comprehensive tests for path last modified and per file graph behaviour.
2381
        parent_candiate_entries = ie.parent_candidates(parent_invs)
2382
        heads = graph.Graph(self).heads(parent_candiate_entries.keys())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2383
        # XXX: Note that this is unordered - and this is tolerable because
2776.1.5 by Robert Collins
Add reasonably comprehensive tests for path last modified and per file graph behaviour.
2384
        # the previous code was also unordered.
2385
        previous_entries = dict((head, parent_candiate_entries[head]) for head
2386
            in heads)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2387
        self.snapshot_ie(previous_entries, ie, w, rev_id)
2388
3099.3.7 by John Arbash Meinel
Another parent provider I didn't realize existed.
2389
    def get_parent_map(self, revision_ids):
4379.3.3 by Gary van der Merwe
Rename and add doc string for StackedParentsProvider.
2390
        """See graph.StackedParentsProvider.get_parent_map"""
3099.3.7 by John Arbash Meinel
Another parent provider I didn't realize existed.
2391
        return dict((revision_id, self.revisions[revision_id])
2392
                    for revision_id in revision_ids
2393
                     if revision_id in self.revisions)
2394
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2395
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
2396
        # TODO: convert this logic, which is ~= snapshot to
2397
        # a call to:. This needs the path figured out. rather than a work_tree
2398
        # a v4 revision_tree can be given, or something that looks enough like
2399
        # one to give the file content to the entry if it needs it.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2400
        # and we need something that looks like a weave store for snapshot to
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2401
        # save against.
2402
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
2403
        if len(previous_revisions) == 1:
2404
            previous_ie = previous_revisions.values()[0]
2405
            if ie._unchanged(previous_ie):
2406
                ie.revision = previous_ie.revision
2407
                return
2408
        if ie.has_text():
4708.2.2 by Martin
Workingtree changes sitting around since November, more explict closing of files in bzrlib
2409
            f = self.branch.repository._text_store.get(ie.text_id)
2410
            try:
2411
                file_lines = f.readlines()
2412
            finally:
2413
                f.close()
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
2414
            w.add_lines(rev_id, previous_revisions, file_lines)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2415
            self.text_count += 1
2416
        else:
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
2417
            w.add_lines(rev_id, previous_revisions, [])
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2418
        ie.revision = rev_id
2419
2420
    def _make_order(self):
2421
        """Return a suitable order for importing revisions.
2422
2423
        The order must be such that an revision is imported after all
2424
        its (present) parents.
2425
        """
2426
        todo = set(self.revisions.keys())
2427
        done = self.absent_revisions.copy()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2428
        order = []
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2429
        while todo:
2430
            # scan through looking for a revision whose parents
2431
            # are all done
2432
            for rev_id in sorted(list(todo)):
2433
                rev = self.revisions[rev_id]
2434
                parent_ids = set(rev.parent_ids)
2435
                if parent_ids.issubset(done):
2436
                    # can take this one now
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2437
                    order.append(rev_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2438
                    todo.remove(rev_id)
2439
                    done.add(rev_id)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2440
        return order
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2441
2442
2443
class ConvertBzrDir5To6(Converter):
2444
    """Converts format 5 bzr dirs to format 6."""
2445
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2446
    def convert(self, to_convert, pb):
2447
        """See Converter.convert()."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2448
        self.bzrdir = to_convert
4961.2.18 by Martin Pool
Remove more pb-passing
2449
        pb = ui.ui_factory.nested_progress_bar()
4961.2.14 by Martin Pool
Further pb cleanups
2450
        try:
2451
            ui.ui_factory.note('starting upgrade from format 5 to 6')
2452
            self._convert_to_prefixed()
5158.6.9 by Martin Pool
Simplify various code to use user_url
2453
            return BzrDir.open(self.bzrdir.user_url)
4961.2.14 by Martin Pool
Further pb cleanups
2454
        finally:
2455
            pb.finished()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2456
2457
    def _convert_to_prefixed(self):
1608.2.1 by Martin Pool
[merge] Storage filename escaping
2458
        from bzrlib.store import TransportStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2459
        self.bzrdir.transport.delete('branch-format')
2460
        for store_name in ["weaves", "revision-store"]:
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
2461
            ui.ui_factory.note("adding prefixes to %s" % store_name)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2462
            store_transport = self.bzrdir.transport.clone(store_name)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
2463
            store = TransportStore(store_transport, prefixed=True)
1608.1.1 by Martin Pool
[patch] LocalTransport.list_dir should return url-quoted strings (ddaa)
2464
            for urlfilename in store_transport.list_dir('.'):
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
2465
                filename = urlutils.unescape(urlfilename)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2466
                if (filename.endswith(".weave") or
2467
                    filename.endswith(".gz") or
2468
                    filename.endswith(".sig")):
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
2469
                    file_id, suffix = os.path.splitext(filename)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2470
                else:
2471
                    file_id = filename
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
2472
                    suffix = ''
2473
                new_name = store._mapper.map((file_id,)) + suffix
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2474
                # FIXME keep track of the dirs made RBC 20060121
2475
                try:
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
2476
                    store_transport.move(filename, new_name)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2477
                except errors.NoSuchFile: # catches missing dirs strangely enough
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
2478
                    store_transport.mkdir(osutils.dirname(new_name))
2479
                    store_transport.move(filename, new_name)
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
2480
        self.bzrdir.transport.put_bytes(
2481
            'branch-format',
2482
            BzrDirFormat6().get_format_string(),
3407.2.18 by Martin Pool
BzrDir takes responsibility for default file/dir modes
2483
            mode=self.bzrdir._get_file_mode())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2484
2485
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2486
class ConvertBzrDir6ToMeta(Converter):
2487
    """Converts format 6 bzr dirs to metadirs."""
2488
2489
    def convert(self, to_convert, pb):
2490
        """See Converter.convert()."""
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
2491
        from bzrlib.repofmt.weaverepo import RepositoryFormat7
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
2492
        from bzrlib.branch import BzrBranchFormat5
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2493
        self.bzrdir = to_convert
4961.2.14 by Martin Pool
Further pb cleanups
2494
        self.pb = ui.ui_factory.nested_progress_bar()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2495
        self.count = 0
2496
        self.total = 20 # the steps we know about
2497
        self.garbage_inventories = []
3407.2.18 by Martin Pool
BzrDir takes responsibility for default file/dir modes
2498
        self.dir_mode = self.bzrdir._get_dir_mode()
2499
        self.file_mode = self.bzrdir._get_file_mode()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2500
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
2501
        ui.ui_factory.note('starting upgrade from format 6 to metadir')
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
2502
        self.bzrdir.transport.put_bytes(
2503
                'branch-format',
2504
                "Converting to format 6",
2505
                mode=self.file_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2506
        # its faster to move specific files around than to open and use the apis...
2507
        # first off, nuke ancestry.weave, it was never used.
2508
        try:
2509
            self.step('Removing ancestry.weave')
2510
            self.bzrdir.transport.delete('ancestry.weave')
2511
        except errors.NoSuchFile:
2512
            pass
2513
        # find out whats there
2514
        self.step('Finding branch files')
1666.1.3 by Robert Collins
Fix and test upgrades from bzrdir 6 over SFTP.
2515
        last_revision = self.bzrdir.open_branch().last_revision()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2516
        bzrcontents = self.bzrdir.transport.list_dir('.')
2517
        for name in bzrcontents:
2518
            if name.startswith('basis-inventory.'):
2519
                self.garbage_inventories.append(name)
2520
        # create new directories for repository, working tree and branch
2521
        repository_names = [('inventory.weave', True),
2522
                            ('revision-store', True),
2523
                            ('weaves', True)]
2524
        self.step('Upgrading repository  ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
2525
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2526
        self.make_lock('repository')
2527
        # we hard code the formats here because we are converting into
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2528
        # the meta format. The meta format upgrader can take this to a
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2529
        # future format within each component.
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
2530
        self.put_format('repository', RepositoryFormat7())
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2531
        for entry in repository_names:
2532
            self.move_entry('repository', entry)
2533
2534
        self.step('Upgrading branch      ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
2535
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2536
        self.make_lock('branch')
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
2537
        self.put_format('branch', BzrBranchFormat5())
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2538
        branch_files = [('revision-history', True),
2539
                        ('branch-name', True),
2540
                        ('parent', False)]
2541
        for entry in branch_files:
2542
            self.move_entry('branch', entry)
2543
2544
        checkout_files = [('pending-merges', True),
2545
                          ('inventory', True),
2546
                          ('stat-cache', False)]
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2547
        # If a mandatory checkout file is not present, the branch does not have
2548
        # a functional checkout. Do not create a checkout in the converted
2549
        # branch.
2550
        for name, mandatory in checkout_files:
2551
            if mandatory and name not in bzrcontents:
2552
                has_checkout = False
2553
                break
2554
        else:
2555
            has_checkout = True
2556
        if not has_checkout:
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
2557
            ui.ui_factory.note('No working tree.')
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2558
            # If some checkout files are there, we may as well get rid of them.
2559
            for name, mandatory in checkout_files:
2560
                if name in bzrcontents:
2561
                    self.bzrdir.transport.delete(name)
2562
        else:
2123.2.1 by John Arbash Meinel
Fix bug #70716, make bzrlib.bzrdir directly import bzrlib.workingtree
2563
            from bzrlib.workingtree import WorkingTreeFormat3
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2564
            self.step('Upgrading working tree')
2565
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
2566
            self.make_lock('checkout')
2567
            self.put_format(
2123.2.1 by John Arbash Meinel
Fix bug #70716, make bzrlib.bzrdir directly import bzrlib.workingtree
2568
                'checkout', WorkingTreeFormat3())
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2569
            self.bzrdir.transport.delete_multi(
2570
                self.garbage_inventories, self.pb)
2571
            for entry in checkout_files:
2572
                self.move_entry('checkout', entry)
2573
            if last_revision is not None:
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
2574
                self.bzrdir.transport.put_bytes(
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2575
                    'checkout/last-revision', last_revision)
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
2576
        self.bzrdir.transport.put_bytes(
2577
            'branch-format',
2578
            BzrDirMetaFormat1().get_format_string(),
2579
            mode=self.file_mode)
4961.2.14 by Martin Pool
Further pb cleanups
2580
        self.pb.finished()
5158.6.9 by Martin Pool
Simplify various code to use user_url
2581
        return BzrDir.open(self.bzrdir.user_url)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2582
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2583
    def make_lock(self, name):
2584
        """Make a lock for the new control dir name."""
2585
        self.step('Make %s lock' % name)
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
2586
        ld = lockdir.LockDir(self.bzrdir.transport,
2587
                             '%s/lock' % name,
2588
                             file_modebits=self.file_mode,
2589
                             dir_modebits=self.dir_mode)
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
2590
        ld.create()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2591
2592
    def move_entry(self, new_dir, entry):
2593
        """Move then entry name into new_dir."""
2594
        name = entry[0]
2595
        mandatory = entry[1]
2596
        self.step('Moving %s' % name)
2597
        try:
2598
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
2599
        except errors.NoSuchFile:
2600
            if mandatory:
2601
                raise
2602
2603
    def put_format(self, dirname, format):
3407.2.5 by Martin Pool
Deprecate LockableFiles.put_utf8
2604
        self.bzrdir.transport.put_bytes('%s/format' % dirname,
2605
            format.get_format_string(),
2606
            self.file_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2607
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2608
2609
class ConvertMetaToMeta(Converter):
2610
    """Converts the components of metadirs."""
2611
2612
    def __init__(self, target_format):
2613
        """Create a metadir to metadir converter.
2614
2615
        :param target_format: The final metadir format that is desired.
2616
        """
2617
        self.target_format = target_format
2618
2619
    def convert(self, to_convert, pb):
2620
        """See Converter.convert()."""
2621
        self.bzrdir = to_convert
4961.2.14 by Martin Pool
Further pb cleanups
2622
        self.pb = ui.ui_factory.nested_progress_bar()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2623
        self.count = 0
2624
        self.total = 1
2625
        self.step('checking repository format')
2626
        try:
2627
            repo = self.bzrdir.open_repository()
2628
        except errors.NoRepositoryPresent:
2629
            pass
2630
        else:
2631
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
2632
                from bzrlib.repository import CopyConverter
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
2633
                ui.ui_factory.note('starting repository conversion')
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2634
                converter = CopyConverter(self.target_format.repository_format)
2635
                converter.convert(repo, pb)
4997.1.3 by Jelmer Vernooij
Use list_branches during upgrades.
2636
        for branch in self.bzrdir.list_branches():
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2637
            # TODO: conversions of Branch and Tree should be done by
3221.11.5 by Robert Collins
Correctly handle multi-step branch upgrades.
2638
            # InterXFormat lookups/some sort of registry.
2230.3.29 by Aaron Bentley
Implement conversion to branch 6
2639
            # Avoid circular imports
2640
            from bzrlib import branch as _mod_branch
3221.11.5 by Robert Collins
Correctly handle multi-step branch upgrades.
2641
            old = branch._format.__class__
2642
            new = self.target_format.get_branch_format().__class__
2643
            while old != new:
2644
                if (old == _mod_branch.BzrBranchFormat5 and
2645
                    new in (_mod_branch.BzrBranchFormat6,
4273.1.13 by Aaron Bentley
Implement upgrade from branch format 7 to 8.
2646
                        _mod_branch.BzrBranchFormat7,
2647
                        _mod_branch.BzrBranchFormat8)):
3221.11.5 by Robert Collins
Correctly handle multi-step branch upgrades.
2648
                    branch_converter = _mod_branch.Converter5to6()
2649
                elif (old == _mod_branch.BzrBranchFormat6 and
4273.1.13 by Aaron Bentley
Implement upgrade from branch format 7 to 8.
2650
                    new in (_mod_branch.BzrBranchFormat7,
2651
                            _mod_branch.BzrBranchFormat8)):
3221.11.5 by Robert Collins
Correctly handle multi-step branch upgrades.
2652
                    branch_converter = _mod_branch.Converter6to7()
4273.1.13 by Aaron Bentley
Implement upgrade from branch format 7 to 8.
2653
                elif (old == _mod_branch.BzrBranchFormat7 and
2654
                      new is _mod_branch.BzrBranchFormat8):
2655
                    branch_converter = _mod_branch.Converter7to8()
3221.11.5 by Robert Collins
Correctly handle multi-step branch upgrades.
2656
                else:
4608.1.3 by Martin Pool
BadConversionTarget error includes source format
2657
                    raise errors.BadConversionTarget("No converter", new,
2658
                        branch._format)
2230.3.29 by Aaron Bentley
Implement conversion to branch 6
2659
                branch_converter.convert(branch)
3221.11.5 by Robert Collins
Correctly handle multi-step branch upgrades.
2660
                branch = self.bzrdir.open_branch()
2661
                old = branch._format.__class__
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2662
        try:
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
2663
            tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2255.2.196 by Robert Collins
Fix test_upgrade defects related to non local or absent working trees.
2664
        except (errors.NoWorkingTree, errors.NotLocalUrl):
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2665
            pass
2666
        else:
2667
            # TODO: conversions of Branch and Tree should be done by
2668
            # InterXFormat lookups
2669
            if (isinstance(tree, workingtree.WorkingTree3) and
3907.2.3 by Ian Clatworthy
DirStateWorkingTree and DirStateWorkingTreeFormat base classes introduced
2670
                not isinstance(tree, workingtree_4.DirStateWorkingTree) and
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2671
                isinstance(self.target_format.workingtree_format,
3907.2.3 by Ian Clatworthy
DirStateWorkingTree and DirStateWorkingTreeFormat base classes introduced
2672
                    workingtree_4.DirStateWorkingTreeFormat)):
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2673
                workingtree_4.Converter3to4().convert(tree)
3907.2.3 by Ian Clatworthy
DirStateWorkingTree and DirStateWorkingTreeFormat base classes introduced
2674
            if (isinstance(tree, workingtree_4.DirStateWorkingTree) and
2675
                not isinstance(tree, workingtree_4.WorkingTree5) and
3586.1.8 by Ian Clatworthy
add workingtree_5 and initial upgrade code
2676
                isinstance(self.target_format.workingtree_format,
3907.2.3 by Ian Clatworthy
DirStateWorkingTree and DirStateWorkingTreeFormat base classes introduced
2677
                    workingtree_4.WorkingTreeFormat5)):
2678
                workingtree_4.Converter4to5().convert(tree)
4210.4.2 by Ian Clatworthy
split filtered views support out into WorkingTreeFormat6
2679
            if (isinstance(tree, workingtree_4.DirStateWorkingTree) and
2680
                not isinstance(tree, workingtree_4.WorkingTree6) and
2681
                isinstance(self.target_format.workingtree_format,
2682
                    workingtree_4.WorkingTreeFormat6)):
2683
                workingtree_4.Converter4or5to6().convert(tree)
4961.2.14 by Martin Pool
Further pb cleanups
2684
        self.pb.finished()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2685
        return to_convert
1731.2.18 by Aaron Bentley
Get extract in repository under test
2686
2687
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2688
# This is not in remote.py because it's relatively small, and needs to be
2689
# registered. Putting it in remote.py creates a circular import problem.
2018.5.28 by Robert Collins
Fix RemoteBzrDirFormat probe api usage.
2690
# we can make it a lazy object if the control formats is turned into something
2691
# like a registry.
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2692
class RemoteBzrDirFormat(BzrDirMetaFormat1):
2693
    """Format representing bzrdirs accessed via a smart server"""
2694
5393.4.3 by Jelmer Vernooij
Consistent spelling.
2695
    supports_workingtrees = False
5393.4.1 by Jelmer Vernooij
Add ControlDirFormat.supports_workingtrees.
2696
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
2697
    def __init__(self):
2698
        BzrDirMetaFormat1.__init__(self)
4934.4.3 by Martin Pool
Initializing a bzrdir shouldn't mutate the RemoteBzrDirFormat
2699
        # XXX: It's a bit ugly that the network name is here, because we'd
2700
        # like to believe that format objects are stateless or at least
2701
        # immutable,  However, we do at least avoid mutating the name after
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
2702
        # it's returned.  See <https://bugs.launchpad.net/bzr/+bug/504102>
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
2703
        self._network_name = None
2704
4934.3.2 by Martin Pool
Add RemoteBzrDirFormat repr
2705
    def __repr__(self):
2706
        return "%s(_network_name=%r)" % (self.__class__.__name__,
2707
            self._network_name)
2708
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2709
    def get_format_description(self):
4792.1.1 by Andrew Bennetts
Show real branch/repo format description in 'info -v' over HPSS.
2710
        if self._network_name:
5363.2.23 by Jelmer Vernooij
Move network_format_registry to bzrlib.controldir.
2711
            real_format = controldir.network_format_registry.get(self._network_name)
4792.3.1 by Matt Nordhoff
Fix missing space in the description for remote bzrdirs
2712
            return 'Remote: ' + real_format.get_format_description()
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2713
        return 'bzr remote bzrdir'
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2714
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
2715
    def get_format_string(self):
2716
        raise NotImplementedError(self.get_format_string)
4032.3.6 by Robert Collins
Fix test_source errors.
2717
4070.2.1 by Robert Collins
Add a BzrDirFormat.network_name.
2718
    def network_name(self):
2719
        if self._network_name:
2720
            return self._network_name
2721
        else:
2722
            raise AssertionError("No network name set.")
2723
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
2724
    def initialize_on_transport(self, transport):
2018.5.128 by Robert Collins
Have RemoteBzrDirFormat create a local format object when it is asked to initialize something on a non-smart transport - allowing sprout to work cleanly.
2725
        try:
2726
            # hand off the request to the smart server
3313.2.1 by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection.
2727
            client_medium = transport.get_smart_medium()
2018.5.128 by Robert Collins
Have RemoteBzrDirFormat create a local format object when it is asked to initialize something on a non-smart transport - allowing sprout to work cleanly.
2728
        except errors.NoSmartMedium:
2729
            # TODO: lookup the local format from a server hint.
2730
            local_dir_format = BzrDirMetaFormat1()
2731
            return local_dir_format.initialize_on_transport(transport)
3431.3.2 by Andrew Bennetts
Remove 'base' from _SmartClient entirely, now that the medium has it.
2732
        client = _SmartClient(client_medium)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
2733
        path = client.remote_path_from_transport(transport)
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
2734
        try:
2735
            response = client.call('BzrDirFormat.initialize', path)
2736
        except errors.ErrorFromSmartServer, err:
2737
            remote._translate_error(err, path=path)
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
2738
        if response[0] != 'ok':
2739
            raise errors.SmartProtocolError('unexpected response code %s' % (response,))
4005.2.3 by Robert Collins
Fix test failure due to shared format objects being returned from initialize_on_transport.
2740
        format = RemoteBzrDirFormat()
2741
        self._supply_sub_formats_to(format)
2742
        return remote.RemoteBzrDir(transport, format)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
2743
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2744
    def parse_NoneTrueFalse(self, arg):
2745
        if not arg:
2746
            return None
2747
        if arg == 'False':
2748
            return False
2749
        if arg == 'True':
2750
            return True
2751
        raise AssertionError("invalid arg %r" % arg)
2752
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2753
    def _serialize_NoneTrueFalse(self, arg):
2754
        if arg is False:
2755
            return 'False'
2756
        if arg:
2757
            return 'True'
2758
        return ''
2759
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2760
    def _serialize_NoneString(self, arg):
2761
        return arg or ''
2762
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2763
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
2764
        create_prefix=False, force_new_repo=False, stacked_on=None,
2765
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
2766
        shared_repo=False):
2767
        try:
2768
            # hand off the request to the smart server
2769
            client_medium = transport.get_smart_medium()
2770
        except errors.NoSmartMedium:
4294.2.9 by Robert Collins
Fixup tests broken by cleaning up the layering.
2771
            do_vfs = True
2772
        else:
2773
            # Decline to open it if the server doesn't support our required
2774
            # version (3) so that the VFS-based transport will do it.
2775
            if client_medium.should_probe():
2776
                try:
2777
                    server_version = client_medium.protocol_version()
2778
                    if server_version != '2':
2779
                        do_vfs = True
2780
                    else:
2781
                        do_vfs = False
2782
                except errors.SmartProtocolError:
2783
                    # Apparently there's no usable smart server there, even though
2784
                    # the medium supports the smart protocol.
2785
                    do_vfs = True
2786
            else:
2787
                do_vfs = False
2788
        if not do_vfs:
2789
            client = _SmartClient(client_medium)
2790
            path = client.remote_path_from_transport(transport)
4436.1.1 by Andrew Bennetts
Rename BzrDirFormat.initialize_ex verb to BzrDirFormat.initialize_ex_1.16.
2791
            if client_medium._is_remote_before((1, 16)):
4294.2.9 by Robert Collins
Fixup tests broken by cleaning up the layering.
2792
                do_vfs = True
2793
        if do_vfs:
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2794
            # TODO: lookup the local format from a server hint.
2795
            local_dir_format = BzrDirMetaFormat1()
2796
            self._supply_sub_formats_to(local_dir_format)
2797
            return local_dir_format.initialize_on_transport_ex(transport,
2798
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
2799
                force_new_repo=force_new_repo, stacked_on=stacked_on,
2800
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2801
                make_working_trees=make_working_trees, shared_repo=shared_repo,
2802
                vfs_only=True)
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
2803
        return self._initialize_on_transport_ex_rpc(client, path, transport,
2804
            use_existing_dir, create_prefix, force_new_repo, stacked_on,
2805
            stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
2806
2807
    def _initialize_on_transport_ex_rpc(self, client, path, transport,
2808
        use_existing_dir, create_prefix, force_new_repo, stacked_on,
2809
        stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2810
        args = []
2811
        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2812
        args.append(self._serialize_NoneTrueFalse(create_prefix))
2813
        args.append(self._serialize_NoneTrueFalse(force_new_repo))
2814
        args.append(self._serialize_NoneString(stacked_on))
2815
        # stack_on_pwd is often/usually our transport
2816
        if stack_on_pwd:
2817
            try:
2818
                stack_on_pwd = transport.relpath(stack_on_pwd)
2819
                if not stack_on_pwd:
2820
                    stack_on_pwd = '.'
2821
            except errors.PathNotChild:
2822
                pass
2823
        args.append(self._serialize_NoneString(stack_on_pwd))
2824
        args.append(self._serialize_NoneString(repo_format_name))
2825
        args.append(self._serialize_NoneTrueFalse(make_working_trees))
2826
        args.append(self._serialize_NoneTrueFalse(shared_repo))
4934.4.3 by Martin Pool
Initializing a bzrdir shouldn't mutate the RemoteBzrDirFormat
2827
        request_network_name = self._network_name or \
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2828
            BzrDirFormat.get_default_format().network_name()
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2829
        try:
4436.1.1 by Andrew Bennetts
Rename BzrDirFormat.initialize_ex verb to BzrDirFormat.initialize_ex_1.16.
2830
            response = client.call('BzrDirFormat.initialize_ex_1.16',
4934.4.3 by Martin Pool
Initializing a bzrdir shouldn't mutate the RemoteBzrDirFormat
2831
                request_network_name, path, *args)
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2832
        except errors.UnknownSmartMethod:
4436.1.1 by Andrew Bennetts
Rename BzrDirFormat.initialize_ex verb to BzrDirFormat.initialize_ex_1.16.
2833
            client._medium._remember_remote_is_before((1,16))
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2834
            local_dir_format = BzrDirMetaFormat1()
2835
            self._supply_sub_formats_to(local_dir_format)
2836
            return local_dir_format.initialize_on_transport_ex(transport,
2837
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
2838
                force_new_repo=force_new_repo, stacked_on=stacked_on,
2839
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2840
                make_working_trees=make_working_trees, shared_repo=shared_repo,
2841
                vfs_only=True)
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
2842
        except errors.ErrorFromSmartServer, err:
2843
            remote._translate_error(err, path=path)
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2844
        repo_path = response[0]
2845
        bzrdir_name = response[6]
2846
        require_stacking = response[7]
2847
        require_stacking = self.parse_NoneTrueFalse(require_stacking)
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2848
        format = RemoteBzrDirFormat()
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2849
        format._network_name = bzrdir_name
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2850
        self._supply_sub_formats_to(format)
4307.2.1 by Robert Collins
Don't probe for bzrdir objects we just created via the smart server.
2851
        bzrdir = remote.RemoteBzrDir(transport, format, _client=client)
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2852
        if repo_path:
2853
            repo_format = remote.response_tuple_to_repo_format(response[1:])
2854
            if repo_path == '.':
2855
                repo_path = ''
2856
            if repo_path:
2857
                repo_bzrdir_format = RemoteBzrDirFormat()
2858
                repo_bzrdir_format._network_name = response[5]
2859
                repo_bzr = remote.RemoteBzrDir(transport.clone(repo_path),
2860
                    repo_bzrdir_format)
2861
            else:
2862
                repo_bzr = bzrdir
2863
            final_stack = response[8] or None
2864
            final_stack_pwd = response[9] or None
4416.3.8 by Jonathan Lange
This makes the unit test & one of the acceptance tests pass.
2865
            if final_stack_pwd:
4416.3.15 by Jonathan Lange
Use a URL joiner that works.
2866
                final_stack_pwd = urlutils.join(
2867
                    transport.base, final_stack_pwd)
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2868
            remote_repo = remote.RemoteRepository(repo_bzr, repo_format)
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
2869
            if len(response) > 10:
2870
                # Updated server verb that locks remotely.
2871
                repo_lock_token = response[10] or None
2872
                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
4307.2.6 by Robert Collins
Handle repositories that mutex on writes (rather than transactions).
2873
                if repo_lock_token:
2874
                    remote_repo.dont_leave_lock_in_place()
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
2875
            else:
4307.2.6 by Robert Collins
Handle repositories that mutex on writes (rather than transactions).
2876
                remote_repo.lock_write()
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2877
            policy = UseExistingRepository(remote_repo, final_stack,
2878
                final_stack_pwd, require_stacking)
2879
            policy.acquire_repository()
2880
        else:
2881
            remote_repo = None
2882
            policy = None
4466.1.1 by Andrew Bennetts
Quick fix 388908: set branch format on the result of initialize_ex before calling require_stacking.
2883
        bzrdir._format.set_branch_format(self.get_branch_format())
4456.2.1 by Andrew Bennetts
Fix automatic branch format upgrades triggered by a default stacking policy on a 1.16rc1 (or later) smart server.
2884
        if require_stacking:
2885
            # The repo has already been created, but we need to make sure that
2886
            # we'll make a stackable branch.
2887
            bzrdir._format.require_stacking(_skip_repo=True)
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2888
        return remote_repo, bzrdir, require_stacking, policy
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
2889
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2890
    def _open(self, transport):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2891
        return remote.RemoteBzrDir(transport, self)
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2892
2893
    def __eq__(self, other):
2894
        if not isinstance(other, RemoteBzrDirFormat):
2895
            return False
2896
        return self.get_format_description() == other.get_format_description()
2897
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2898
    def __return_repository_format(self):
2899
        # Always return a RemoteRepositoryFormat object, but if a specific bzr
2900
        # repository format has been asked for, tell the RemoteRepositoryFormat
2901
        # that it should use that for init() etc.
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
2902
        result = remote.RemoteRepositoryFormat()
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2903
        custom_format = getattr(self, '_repository_format', None)
2904
        if custom_format:
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
2905
            if isinstance(custom_format, remote.RemoteRepositoryFormat):
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
2906
                return custom_format
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
2907
            else:
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
2908
                # We will use the custom format to create repositories over the
2909
                # wire; expose its details like rich_root_data for code to
2910
                # query
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
2911
                result._custom_format = custom_format
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2912
        return result
2913
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
2914
    def get_branch_format(self):
2915
        result = BzrDirMetaFormat1.get_branch_format(self)
2916
        if not isinstance(result, remote.RemoteBranchFormat):
2917
            new_result = remote.RemoteBranchFormat()
2918
            new_result._custom_format = result
2919
            # cache the result
2920
            self.set_branch_format(new_result)
2921
            result = new_result
2922
        return result
2923
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2924
    repository_format = property(__return_repository_format,
2925
        BzrDirMetaFormat1._set_repository_format) #.im_func)
3845.1.1 by John Arbash Meinel
Ensure that RepositoryFormat._matchingbzrdir.repository_format matches.
2926
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2927
5363.2.20 by Jelmer Vernooij
use controldir.X
2928
controldir.ControlDirFormat.register_server_prober(RemoteBzrProber)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2929
2930
3242.2.14 by Aaron Bentley
Update from review comments
2931
class RepositoryAcquisitionPolicy(object):
2932
    """Abstract base class for repository acquisition policies.
3242.3.7 by Aaron Bentley
Delegate stacking to configure_branch
2933
3242.2.14 by Aaron Bentley
Update from review comments
2934
    A repository acquisition policy decides how a BzrDir acquires a repository
2935
    for a branch that is being created.  The most basic policy decision is
2936
    whether to create a new repository or use an existing one.
2937
    """
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
2938
    def __init__(self, stack_on, stack_on_pwd, require_stacking):
3242.3.35 by Aaron Bentley
Cleanups and documentation
2939
        """Constructor.
2940
2941
        :param stack_on: A location to stack on
2942
        :param stack_on_pwd: If stack_on is relative, the location it is
2943
            relative to.
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
2944
        :param require_stacking: If True, it is a failure to not stack.
3242.3.35 by Aaron Bentley
Cleanups and documentation
2945
        """
3242.3.7 by Aaron Bentley
Delegate stacking to configure_branch
2946
        self._stack_on = stack_on
3242.3.32 by Aaron Bentley
Defer handling relative stacking URLs as late as possible.
2947
        self._stack_on_pwd = stack_on_pwd
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
2948
        self._require_stacking = require_stacking
3242.3.7 by Aaron Bentley
Delegate stacking to configure_branch
2949
2950
    def configure_branch(self, branch):
3242.2.13 by Aaron Bentley
Update docs
2951
        """Apply any configuration data from this policy to the branch.
2952
3242.3.18 by Aaron Bentley
Clean up repository-policy work
2953
        Default implementation sets repository stacking.
3242.2.13 by Aaron Bentley
Update docs
2954
        """
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
2955
        if self._stack_on is None:
2956
            return
2957
        if self._stack_on_pwd is None:
2958
            stack_on = self._stack_on
2959
        else:
2960
            try:
3242.3.32 by Aaron Bentley
Defer handling relative stacking URLs as late as possible.
2961
                stack_on = urlutils.rebase_url(self._stack_on,
2962
                    self._stack_on_pwd,
5158.6.9 by Martin Pool
Simplify various code to use user_url
2963
                    branch.user_url)
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
2964
            except errors.InvalidRebaseURLs:
2965
                stack_on = self._get_full_stack_on()
3242.3.37 by Aaron Bentley
Updates from reviews
2966
        try:
3537.3.5 by Martin Pool
merge trunk including stacking policy
2967
            branch.set_stacked_on_url(stack_on)
4126.1.1 by Andrew Bennetts
Fix bug when pushing stackable branch in unstackable repo to default-stacking target.
2968
        except (errors.UnstackableBranchFormat,
2969
                errors.UnstackableRepositoryFormat):
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
2970
            if self._require_stacking:
2971
                raise
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
2972
4617.3.1 by Robert Collins
Fix test_stacking tests for 2a as a default format. The change to 2a exposed some actual bugs, both in tests and bzrdir/branch code.
2973
    def requires_stacking(self):
2974
        """Return True if this policy requires stacking."""
2975
        return self._stack_on is not None and self._require_stacking
2976
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
2977
    def _get_full_stack_on(self):
3242.3.35 by Aaron Bentley
Cleanups and documentation
2978
        """Get a fully-qualified URL for the stack_on location."""
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
2979
        if self._stack_on is None:
2980
            return None
2981
        if self._stack_on_pwd is None:
2982
            return self._stack_on
2983
        else:
2984
            return urlutils.join(self._stack_on_pwd, self._stack_on)
3242.3.7 by Aaron Bentley
Delegate stacking to configure_branch
2985
3928.3.2 by John Arbash Meinel
Track down the other cause of us connecting multiple times.
2986
    def _add_fallback(self, repository, possible_transports=None):
3242.3.35 by Aaron Bentley
Cleanups and documentation
2987
        """Add a fallback to the supplied repository, if stacking is set."""
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
2988
        stack_on = self._get_full_stack_on()
2989
        if stack_on is None:
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
2990
            return
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
2991
        try:
2992
            stacked_dir = BzrDir.open(stack_on,
2993
                                      possible_transports=possible_transports)
2994
        except errors.JailBreak:
2995
            # We keep the stacking details, but we are in the server code so
2996
            # actually stacking is not needed.
2997
            return
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
2998
        try:
2999
            stacked_repo = stacked_dir.open_branch().repository
3000
        except errors.NotBranchError:
3001
            stacked_repo = stacked_dir.open_repository()
3242.3.37 by Aaron Bentley
Updates from reviews
3002
        try:
3003
            repository.add_fallback_repository(stacked_repo)
3004
        except errors.UnstackableRepositoryFormat:
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
3005
            if self._require_stacking:
3006
                raise
3904.3.1 by Andrew Bennetts
Probable fix for GaryvdM's bug when pushing a stacked qbzr branch to Launchpad.
3007
        else:
3008
            self._require_stacking = True
3242.3.30 by Aaron Bentley
Handle adding fallback repositories in acquire_repository
3009
3242.2.10 by Aaron Bentley
Rename RepositoryPolicy.apply to acquire_repository
3010
    def acquire_repository(self, make_working_trees=None, shared=False):
3242.2.14 by Aaron Bentley
Update from review comments
3011
        """Acquire a repository for this bzrdir.
3012
3013
        Implementations may create a new repository or use a pre-exising
3014
        repository.
3015
        :param make_working_trees: If creating a repository, set
3016
            make_working_trees to this value (if non-None)
3017
        :param shared: If creating a repository, make it shared if True
4070.9.8 by Andrew Bennetts
Use MiniSearchResult in clone_on_transport down (further tightening the test_push ratchets), and improve acquire_repository docstrings.
3018
        :return: A repository, is_new_flag (True if the repository was
3019
            created).
3242.2.14 by Aaron Bentley
Update from review comments
3020
        """
3021
        raise NotImplemented(RepositoryAcquisitionPolicy.acquire_repository)
3022
3023
3024
class CreateRepository(RepositoryAcquisitionPolicy):
3242.2.13 by Aaron Bentley
Update docs
3025
    """A policy of creating a new repository"""
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
3026
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
3027
    def __init__(self, bzrdir, stack_on=None, stack_on_pwd=None,
3028
                 require_stacking=False):
3242.3.35 by Aaron Bentley
Cleanups and documentation
3029
        """
3030
        Constructor.
3031
        :param bzrdir: The bzrdir to create the repository on.
3032
        :param stack_on: A location to stack on
3033
        :param stack_on_pwd: If stack_on is relative, the location it is
3034
            relative to.
3035
        """
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
3036
        RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
3037
                                             require_stacking)
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
3038
        self._bzrdir = bzrdir
3039
3242.2.10 by Aaron Bentley
Rename RepositoryPolicy.apply to acquire_repository
3040
    def acquire_repository(self, make_working_trees=None, shared=False):
3242.2.14 by Aaron Bentley
Update from review comments
3041
        """Implementation of RepositoryAcquisitionPolicy.acquire_repository
3242.2.13 by Aaron Bentley
Update docs
3042
3242.2.14 by Aaron Bentley
Update from review comments
3043
        Creates the desired repository in the bzrdir we already have.
3242.2.13 by Aaron Bentley
Update docs
3044
        """
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
3045
        stack_on = self._get_full_stack_on()
3046
        if stack_on:
4401.1.2 by John Arbash Meinel
Move the logic back up into BzrDirFormat1.require_stacking, passing in the extra params.
3047
            format = self._bzrdir._format
3048
            format.require_stacking(stack_on=stack_on,
3049
                                    possible_transports=[self._bzrdir.root_transport])
3050
            if not self._require_stacking:
3051
                # We have picked up automatic stacking somewhere.
3052
                note('Using default stacking branch %s at %s', self._stack_on,
3053
                    self._stack_on_pwd)
3650.3.9 by Aaron Bentley
Move responsibility for stackable repo format to _get_metadir
3054
        repository = self._bzrdir.create_repository(shared=shared)
3928.3.2 by John Arbash Meinel
Track down the other cause of us connecting multiple times.
3055
        self._add_fallback(repository,
3056
                           possible_transports=[self._bzrdir.transport])
3242.2.4 by Aaron Bentley
Only set working tree policty when specified
3057
        if make_working_trees is not None:
3242.3.6 by Aaron Bentley
Work around strange test failure
3058
            repository.set_make_working_trees(make_working_trees)
4070.9.2 by Andrew Bennetts
Rough prototype of allowing a SearchResult to be passed to fetch, and using that to improve network conversations.
3059
        return repository, True
3242.2.2 by Aaron Bentley
Merge policy updates from stacked-policy thread
3060
3061
3242.2.14 by Aaron Bentley
Update from review comments
3062
class UseExistingRepository(RepositoryAcquisitionPolicy):
3242.2.13 by Aaron Bentley
Update docs
3063
    """A policy of reusing an existing repository"""
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
3064
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
3065
    def __init__(self, repository, stack_on=None, stack_on_pwd=None,
3066
                 require_stacking=False):
3242.3.35 by Aaron Bentley
Cleanups and documentation
3067
        """Constructor.
3068
3069
        :param repository: The repository to use.
3070
        :param stack_on: A location to stack on
3071
        :param stack_on_pwd: If stack_on is relative, the location it is
3072
            relative to.
3073
        """
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
3074
        RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
3075
                                             require_stacking)
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
3076
        self._repository = repository
3077
3242.2.10 by Aaron Bentley
Rename RepositoryPolicy.apply to acquire_repository
3078
    def acquire_repository(self, make_working_trees=None, shared=False):
3242.2.14 by Aaron Bentley
Update from review comments
3079
        """Implementation of RepositoryAcquisitionPolicy.acquire_repository
3242.2.13 by Aaron Bentley
Update docs
3080
4070.9.8 by Andrew Bennetts
Use MiniSearchResult in clone_on_transport down (further tightening the test_push ratchets), and improve acquire_repository docstrings.
3081
        Returns an existing repository to use.
3242.2.13 by Aaron Bentley
Update docs
3082
        """
3928.3.2 by John Arbash Meinel
Track down the other cause of us connecting multiple times.
3083
        self._add_fallback(self._repository,
3084
                       possible_transports=[self._repository.bzrdir.transport])
4070.9.2 by Andrew Bennetts
Rough prototype of allowing a SearchResult to be passed to fetch, and using that to improve network conversations.
3085
        return self._repository, False
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
3086
3087
5363.2.9 by Jelmer Vernooij
Fix some tests.
3088
def register_metadir(registry, key,
5363.2.6 by Jelmer Vernooij
Add ControlDirFormat.{un,}register_{server_,}prober.
3089
         repository_format, help, native=True, deprecated=False,
3090
         branch_format=None,
3091
         tree_format=None,
3092
         hidden=False,
3093
         experimental=False,
3094
         alias=False):
3095
    """Register a metadir subformat.
3096
3097
    These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
3098
    by the Repository/Branch/WorkingTreeformats.
3099
3100
    :param repository_format: The fully-qualified repository format class
3101
        name as a string.
3102
    :param branch_format: Fully-qualified branch format class name as
3103
        a string.
3104
    :param tree_format: Fully-qualified tree format class name as
3105
        a string.
3106
    """
3107
    # This should be expanded to support setting WorkingTree and Branch
3108
    # formats, once BzrDirMetaFormat1 supports that.
3109
    def _load(full_name):
3110
        mod_name, factory_name = full_name.rsplit('.', 1)
3111
        try:
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
3112
            factory = pyutils.get_named_object(mod_name, factory_name)
5363.2.6 by Jelmer Vernooij
Add ControlDirFormat.{un,}register_{server_,}prober.
3113
        except ImportError, e:
3114
            raise ImportError('failed to load %s: %s' % (full_name, e))
3115
        except AttributeError:
3116
            raise AttributeError('no factory %s in module %r'
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
3117
                % (full_name, sys.modules[mod_name]))
5363.2.6 by Jelmer Vernooij
Add ControlDirFormat.{un,}register_{server_,}prober.
3118
        return factory()
3119
3120
    def helper():
3121
        bd = BzrDirMetaFormat1()
3122
        if branch_format is not None:
3123
            bd.set_branch_format(_load(branch_format))
3124
        if tree_format is not None:
3125
            bd.workingtree_format = _load(tree_format)
3126
        if repository_format is not None:
3127
            bd.repository_format = _load(repository_format)
3128
        return bd
5363.2.9 by Jelmer Vernooij
Fix some tests.
3129
    registry.register(key, helper, help, native, deprecated, hidden,
5363.2.6 by Jelmer Vernooij
Add ControlDirFormat.{un,}register_{server_,}prober.
3130
        experimental, alias)
3131
3990.5.3 by Robert Collins
Docs and polish on RepositoryFormat.network_name.
3132
# The pre-0.8 formats have their repository format network name registered in
3133
# repository.py. MetaDir formats have their repository format network name
3134
# inferred from their disk format string.
5363.2.20 by Jelmer Vernooij
use controldir.X
3135
controldir.format_registry.register('weave', BzrDirFormat6,
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
3136
    'Pre-0.8 format.  Slower than knit and does not'
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
3137
    ' support checkouts or shared repositories.',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3138
    hidden=True,
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
3139
    deprecated=True)
5363.2.20 by Jelmer Vernooij
use controldir.X
3140
register_metadir(controldir.format_registry, 'metaweave',
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
3141
    'bzrlib.repofmt.weaverepo.RepositoryFormat7',
2230.3.30 by Aaron Bentley
Fix whitespace issues
3142
    'Transitional format in 0.8.  Slower than knit.',
2255.12.1 by Robert Collins
Implement upgrade for working trees.
3143
    branch_format='bzrlib.branch.BzrBranchFormat5',
2255.2.209 by Robert Collins
Remove circular imports in bzrdir format definitions.
3144
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3145
    hidden=True,
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
3146
    deprecated=True)
5363.2.20 by Jelmer Vernooij
use controldir.X
3147
register_metadir(controldir.format_registry, 'knit',
3892.1.2 by Ian Clatworthy
split formats topic into multiple topics
3148
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
3149
    'Format using knits.  Recommended for interoperation with bzr <= 0.14.',
3150
    branch_format='bzrlib.branch.BzrBranchFormat5',
3151
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3152
    hidden=True,
3892.1.2 by Ian Clatworthy
split formats topic into multiple topics
3153
    deprecated=True)
5363.2.20 by Jelmer Vernooij
use controldir.X
3154
register_metadir(controldir.format_registry, 'dirstate',
2255.12.1 by Robert Collins
Implement upgrade for working trees.
3155
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
3156
    help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
3157
        'above when accessed over the network.',
3158
    branch_format='bzrlib.branch.BzrBranchFormat5',
2255.2.209 by Robert Collins
Remove circular imports in bzrdir format definitions.
3159
    # this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
3160
    # directly from workingtree_4 triggers a circular import.
3161
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3162
    hidden=True,
3892.1.1 by Ian Clatworthy
improve help on storage formats
3163
    deprecated=True)
5363.2.20 by Jelmer Vernooij
use controldir.X
3164
register_metadir(controldir.format_registry, 'dirstate-tags',
1551.13.1 by Aaron Bentley
Introduce dirstate-tags format
3165
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
3166
    help='New in 0.15: Fast local operations and improved scaling for '
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
3167
        'network operations. Additionally adds support for tags.'
3168
        ' Incompatible with bzr < 0.15.',
1551.13.1 by Aaron Bentley
Introduce dirstate-tags format
3169
    branch_format='bzrlib.branch.BzrBranchFormat6',
3170
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3171
    hidden=True,
3892.1.1 by Ian Clatworthy
improve help on storage formats
3172
    deprecated=True)
5363.2.20 by Jelmer Vernooij
use controldir.X
3173
register_metadir(controldir.format_registry, 'rich-root',
2996.2.1 by Aaron Bentley
Add KnitRepositoryFormat4
3174
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit4',
3175
    help='New in 1.0.  Better handling of tree roots.  Incompatible with'
3892.1.2 by Ian Clatworthy
split formats topic into multiple topics
3176
        ' bzr < 1.0.',
2996.2.1 by Aaron Bentley
Add KnitRepositoryFormat4
3177
    branch_format='bzrlib.branch.BzrBranchFormat6',
3178
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3179
    hidden=True,
3892.1.1 by Ian Clatworthy
improve help on storage formats
3180
    deprecated=True)
5363.2.20 by Jelmer Vernooij
use controldir.X
3181
register_metadir(controldir.format_registry, 'dirstate-with-subtree',
2255.12.1 by Robert Collins
Implement upgrade for working trees.
3182
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
3183
    help='New in 0.15: Fast local operations and improved scaling for '
3184
        'network operations. Additionally adds support for versioning nested '
3185
        'bzr branches. Incompatible with bzr < 0.15.',
3186
    branch_format='bzrlib.branch.BzrBranchFormat6',
2255.2.209 by Robert Collins
Remove circular imports in bzrdir format definitions.
3187
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3170.4.3 by Adeodato Simó
Mark the subtree formats as experimental instead of hidden, and remove hidden=True from the rich-root ones.
3188
    experimental=True,
3170.4.4 by Adeodato Simó
Keep the hidden flag for subtree formats after review from Aaron.
3189
    hidden=True,
2255.12.1 by Robert Collins
Implement upgrade for working trees.
3190
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3191
register_metadir(controldir.format_registry, 'pack-0.92',
2592.3.224 by Martin Pool
Rename GraphKnitRepository etc to KnitPackRepository
3192
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack1',
2939.2.1 by Ian Clatworthy
use 'knitpack' naming instead of 'experimental' for pack formats
3193
    help='New in 0.92: Pack-based format with data compatible with '
2939.2.6 by Ian Clatworthy
more review feedback from lifeless and poolie
3194
        'dirstate-tags format repositories. Interoperates with '
3195
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
4988.4.2 by Martin Pool
Change url to canonical.com or wiki, plus some doc improvements in passing
3196
        ,
2592.3.22 by Robert Collins
Add new experimental repository formats.
3197
    branch_format='bzrlib.branch.BzrBranchFormat6',
3198
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3199
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3200
register_metadir(controldir.format_registry, 'pack-0.92-subtree',
2592.3.224 by Martin Pool
Rename GraphKnitRepository etc to KnitPackRepository
3201
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack3',
2939.2.1 by Ian Clatworthy
use 'knitpack' naming instead of 'experimental' for pack formats
3202
    help='New in 0.92: Pack-based format with data compatible with '
2939.2.6 by Ian Clatworthy
more review feedback from lifeless and poolie
3203
        'dirstate-with-subtree format repositories. Interoperates with '
3204
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
4988.4.2 by Martin Pool
Change url to canonical.com or wiki, plus some doc improvements in passing
3205
        ,
2592.3.22 by Robert Collins
Add new experimental repository formats.
3206
    branch_format='bzrlib.branch.BzrBranchFormat6',
3207
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3190.1.2 by Aaron Bentley
Undo spurious change
3208
    hidden=True,
3170.4.3 by Adeodato Simó
Mark the subtree formats as experimental instead of hidden, and remove hidden=True from the rich-root ones.
3209
    experimental=True,
2592.3.22 by Robert Collins
Add new experimental repository formats.
3210
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3211
register_metadir(controldir.format_registry, 'rich-root-pack',
2996.2.11 by Aaron Bentley
Implement rich-root-pack format ( #164639)
3212
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
3892.1.2 by Ian Clatworthy
split formats topic into multiple topics
3213
    help='New in 1.0: A variant of pack-0.92 that supports rich-root data '
4119.6.2 by Jelmer Vernooij
Use existing alias mechanism for default-rich-root.
3214
         '(needed for bzr-svn and bzr-git).',
2996.2.11 by Aaron Bentley
Implement rich-root-pack format ( #164639)
3215
    branch_format='bzrlib.branch.BzrBranchFormat6',
3216
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3217
    hidden=True,
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
3218
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3219
register_metadir(controldir.format_registry, '1.6',
3549.1.5 by Martin Pool
Add stable format names for stacked branches
3220
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5',
3892.1.6 by Ian Clatworthy
include feedback from poolie
3221
    help='A format that allows a branch to indicate that there is another '
3222
         '(stacked) repository that should be used to access data that is '
3223
         'not present locally.',
3549.1.5 by Martin Pool
Add stable format names for stacked branches
3224
    branch_format='bzrlib.branch.BzrBranchFormat7',
3225
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3226
    hidden=True,
3549.1.5 by Martin Pool
Add stable format names for stacked branches
3227
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3228
register_metadir(controldir.format_registry, '1.6.1-rich-root',
3549.1.6 by Martin Pool
Change stacked-subtree to stacked-rich-root
3229
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5RichRoot',
3892.1.2 by Ian Clatworthy
split formats topic into multiple topics
3230
    help='A variant of 1.6 that supports rich-root data '
4119.6.2 by Jelmer Vernooij
Use existing alias mechanism for default-rich-root.
3231
         '(needed for bzr-svn and bzr-git).',
3549.1.5 by Martin Pool
Add stable format names for stacked branches
3232
    branch_format='bzrlib.branch.BzrBranchFormat7',
3233
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3234
    hidden=True,
3549.1.5 by Martin Pool
Add stable format names for stacked branches
3235
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3236
register_metadir(controldir.format_registry, '1.9',
3805.3.1 by John Arbash Meinel
Add repository 1.9 format, and update the documentation.
3237
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
3892.1.6 by Ian Clatworthy
include feedback from poolie
3238
    help='A repository format using B+tree indexes. These indexes '
3892.1.4 by Ian Clatworthy
rich-root explanation and improved help for 1.6 and 1.9 formats
3239
         'are smaller in size, have smarter caching and provide faster '
3240
         'performance for most operations.',
3805.3.1 by John Arbash Meinel
Add repository 1.9 format, and update the documentation.
3241
    branch_format='bzrlib.branch.BzrBranchFormat7',
3242
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3243
    hidden=True,
3805.3.1 by John Arbash Meinel
Add repository 1.9 format, and update the documentation.
3244
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3245
register_metadir(controldir.format_registry, '1.9-rich-root',
3805.3.1 by John Arbash Meinel
Add repository 1.9 format, and update the documentation.
3246
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
3892.1.2 by Ian Clatworthy
split formats topic into multiple topics
3247
    help='A variant of 1.9 that supports rich-root data '
4119.6.2 by Jelmer Vernooij
Use existing alias mechanism for default-rich-root.
3248
         '(needed for bzr-svn and bzr-git).',
3805.3.1 by John Arbash Meinel
Add repository 1.9 format, and update the documentation.
3249
    branch_format='bzrlib.branch.BzrBranchFormat7',
3250
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
4976.2.1 by Ian Clatworthy
Hide most storage formats
3251
    hidden=True,
3805.3.1 by John Arbash Meinel
Add repository 1.9 format, and update the documentation.
3252
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3253
register_metadir(controldir.format_registry, '1.14',
3586.2.10 by Ian Clatworthy
rename formats from 1.7-* to 1.12-*
3254
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
4210.4.2 by Ian Clatworthy
split filtered views support out into WorkingTreeFormat6
3255
    help='A working-tree format that supports content filtering.',
3586.2.6 by Ian Clatworthy
add 1.7 and 1.7-rich-root formats
3256
    branch_format='bzrlib.branch.BzrBranchFormat7',
3995.7.1 by John Arbash Meinel
Fix bug #328135.
3257
    tree_format='bzrlib.workingtree.WorkingTreeFormat5',
3586.2.6 by Ian Clatworthy
add 1.7 and 1.7-rich-root formats
3258
    )
5363.2.20 by Jelmer Vernooij
use controldir.X
3259
register_metadir(controldir.format_registry, '1.14-rich-root',
3586.2.10 by Ian Clatworthy
rename formats from 1.7-* to 1.12-*
3260
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
4210.4.1 by Ian Clatworthy
replace experimental development-wt5 formats with 1.14 formats
3261
    help='A variant of 1.14 that supports rich-root data '
4119.6.2 by Jelmer Vernooij
Use existing alias mechanism for default-rich-root.
3262
         '(needed for bzr-svn and bzr-git).',
3586.2.6 by Ian Clatworthy
add 1.7 and 1.7-rich-root formats
3263
    branch_format='bzrlib.branch.BzrBranchFormat7',
3995.7.1 by John Arbash Meinel
Fix bug #328135.
3264
    tree_format='bzrlib.workingtree.WorkingTreeFormat5',
3586.2.6 by Ian Clatworthy
add 1.7 and 1.7-rich-root formats
3265
    )
4241.6.8 by Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil
Add --development6-rich-root, disabling the legacy and unneeded development2 format, and activating the tests for CHK features disabled pending this format. (Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil)
3266
# The following un-numbered 'development' formats should always just be aliases.
5363.2.20 by Jelmer Vernooij
use controldir.X
3267
register_metadir(controldir.format_registry, 'development-subtree',
5389.1.1 by Jelmer Vernooij
Add development8-subtree.
3268
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormat2aSubtree',
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
3269
    help='Current development format, subtree variant. Can convert data to and '
3221.11.7 by Robert Collins
Merge in real stacked repository work.
3270
        'from pack-0.92-subtree (and anything compatible with '
3271
        'pack-0.92-subtree) format repositories. Repositories and branches in '
3272
        'this format can only be read by bzr.dev. Please read '
4988.4.2 by Martin Pool
Change url to canonical.com or wiki, plus some doc improvements in passing
3273
        'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
3274
        'before use.',
3221.11.2 by Robert Collins
Create basic stackable branch facility.
3275
    branch_format='bzrlib.branch.BzrBranchFormat7',
4241.6.8 by Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil
Add --development6-rich-root, disabling the legacy and unneeded development2 format, and activating the tests for CHK features disabled pending this format. (Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil)
3276
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
3277
    experimental=True,
4976.2.1 by Ian Clatworthy
Hide most storage formats
3278
    hidden=True,
4241.6.8 by Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil
Add --development6-rich-root, disabling the legacy and unneeded development2 format, and activating the tests for CHK features disabled pending this format. (Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil)
3279
    alias=False, # Restore to being an alias when an actual development subtree format is added
3280
                 # This current non-alias status is simply because we did not introduce a
3281
                 # chk based subtree format.
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
3282
    )
5546.1.1 by Andrew Bennetts
Remove RepositoryFormatCHK1 and RepositoryFormatCHK2.
3283
register_metadir(controldir.format_registry, 'development5-subtree',
3284
    'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2Subtree',
3285
    help='Development format, subtree variant. Can convert data to and '
3286
        'from pack-0.92-subtree (and anything compatible with '
3287
        'pack-0.92-subtree) format repositories. Repositories and branches in '
3288
        'this format can only be read by bzr.dev. Please read '
3289
        'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
3290
        'before use.',
3291
    branch_format='bzrlib.branch.BzrBranchFormat7',
3292
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
3293
    experimental=True,
3294
    hidden=True,
3295
    alias=False,
3296
    )
4241.6.8 by Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil
Add --development6-rich-root, disabling the legacy and unneeded development2 format, and activating the tests for CHK features disabled pending this format. (Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil)
3297
3735.1.1 by Robert Collins
Add development2 formats using BTree indices.
3298
# And the development formats above will have aliased one of the following:
5546.1.1 by Andrew Bennetts
Remove RepositoryFormatCHK1 and RepositoryFormatCHK2.
3299
3300
# Finally, the current format.
5363.2.20 by Jelmer Vernooij
use controldir.X
3301
register_metadir(controldir.format_registry, '2a',
4428.2.1 by Martin Pool
Add 2a format
3302
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormat2a',
3303
    help='First format for bzr 2.0 series.\n'
4428.2.5 by Martin Pool
Mark 2a experimental and tweak its help per lifeless's review
3304
        'Uses group-compress storage.\n'
4428.2.6 by Martin Pool
Stupid typo fix
3305
        'Provides rich roots which are a one-way transition.\n',
4428.2.5 by Martin Pool
Mark 2a experimental and tweak its help per lifeless's review
3306
        # 'storage in packs, 255-way hashed CHK inventory, bencode revision, group compress, '
3307
        # 'rich roots. Supported by bzr 1.16 and later.',
4428.2.1 by Martin Pool
Add 2a format
3308
    branch_format='bzrlib.branch.BzrBranchFormat7',
3309
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
5389.1.1 by Jelmer Vernooij
Add development8-subtree.
3310
    experimental=False,
4428.2.1 by Martin Pool
Add 2a format
3311
    )
4428.2.2 by Martin Pool
Format 2a should not be hidden
3312
4119.6.2 by Jelmer Vernooij
Use existing alias mechanism for default-rich-root.
3313
# The following format should be an alias for the rich root equivalent 
3314
# of the default format
5363.2.20 by Jelmer Vernooij
use controldir.X
3315
register_metadir(controldir.format_registry, 'default-rich-root',
4599.4.37 by Robert Collins
Fix registration of default-rich-root as 2a.
3316
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormat2a',
3317
    branch_format='bzrlib.branch.BzrBranchFormat7',
3318
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
4599.4.23 by Robert Collins
default-rich-root should be an alias still.
3319
    alias=True,
4976.2.1 by Ian Clatworthy
Hide most storage formats
3320
    hidden=True,
4599.4.22 by mbp at sourcefrog
Don't forget to set default-rich-root to 2a too.
3321
    help='Same as 2a.')
3322
3221.11.2 by Robert Collins
Create basic stackable branch facility.
3323
# The current format that is made on 'bzr init'.
5448.4.3 by Neil Martinsen-Burrell
use option along with controldir.set_default to control the default format
3324
format_name = config.GlobalConfig().get_user_option('default_format')
3325
if format_name is None:
3326
    controldir.format_registry.set_default('2a')
3327
else:
3328
    controldir.format_registry.set_default(format_name)
5363.2.22 by Jelmer Vernooij
Provide bzrlib.bzrdir.format_registry.
3329
3330
# XXX 2010-08-20 JRV: There is still a lot of code relying on
3331
# bzrlib.bzrdir.format_registry existing. When BzrDir.create/BzrDir.open/etc
3332
# get changed to ControlDir.create/ControlDir.open/etc this should be removed.
3333
format_registry = controldir.format_registry