/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.48 by Martin Pool
Fix some LockableFiles deprecation warnings
1
# Copyright (C) 2005, 2006 Canonical Ltd
1534.4.39 by Robert Collins
Basic BzrDir support.
2
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.
7
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.
12
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.
21
"""
22
23
from copy import deepcopy
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
24
import os
1534.4.39 by Robert Collins
Basic BzrDir support.
25
from cStringIO import StringIO
26
from unittest import TestSuite
27
28
import bzrlib
29
import bzrlib.errors as errors
1553.5.48 by Martin Pool
Fix some LockableFiles deprecation warnings
30
from bzrlib.lockable_files import LockableFiles, TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
31
from bzrlib.lockdir import LockDir
1534.4.39 by Robert Collins
Basic BzrDir support.
32
from bzrlib.osutils import safe_unicode
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
33
from bzrlib.osutils import (
34
                            abspath,
35
                            pathjoin,
36
                            safe_unicode,
37
                            sha_strings,
38
                            sha_string,
39
                            )
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
40
from bzrlib.store.revision.text import TextRevisionStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
41
from bzrlib.store.text import TextStore
1563.2.25 by Robert Collins
Merge in upstream.
42
from bzrlib.store.versioned import WeaveStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
43
from bzrlib.symbol_versioning import *
1534.4.39 by Robert Collins
Basic BzrDir support.
44
from bzrlib.trace import mutter
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
45
from bzrlib.transactions import WriteTransaction
1608.1.1 by Martin Pool
[patch] LocalTransport.list_dir should return url-quoted strings (ddaa)
46
from bzrlib.transport import get_transport, urlunescape
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
47
from bzrlib.transport.local import LocalTransport
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
48
from bzrlib.weave import Weave
49
from bzrlib.xml4 import serializer_v4
50
from bzrlib.xml5 import serializer_v5
1534.4.39 by Robert Collins
Basic BzrDir support.
51
52
53
class BzrDir(object):
54
    """A .bzr control diretory.
55
    
56
    BzrDir instances let you create or open any of the things that can be
57
    found within .bzr - checkouts, branches and repositories.
58
    
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
59
    transport
60
        the transport which this bzr dir is rooted at (i.e. file:///.../.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.
61
    root_transport
62
        a transport connected to the directory this bzr was opened from.
1534.4.39 by Robert Collins
Basic BzrDir support.
63
    """
64
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
65
    def break_lock(self):
66
        """Invoke break_lock on the first object in the bzrdir.
67
68
        If there is a tree, the tree is opened and break_lock() called.
69
        Otherwise, branch is tried, and finally repository.
70
        """
71
        try:
72
            thing_to_unlock = self.open_workingtree()
73
        except (errors.NotLocalUrl, errors.NoWorkingTree):
74
            try:
75
                thing_to_unlock = self.open_branch()
76
            except errors.NotBranchError:
77
                try:
78
                    thing_to_unlock = self.open_repository()
79
                except errors.NoRepositoryPresent:
80
                    return
81
        thing_to_unlock.break_lock()
82
1534.5.16 by Robert Collins
Review feedback.
83
    def can_convert_format(self):
84
        """Return true if this bzrdir is one whose format we can convert from."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
85
        return True
86
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
87
    @staticmethod
88
    def _check_supported(format, allow_unsupported):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
89
        """Check whether format is a supported format.
90
91
        If allow_unsupported is True, this is a no-op.
92
        """
93
        if not allow_unsupported and not format.is_supported():
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
94
            # see open_downlevel to open legacy branches.
95
            raise errors.UnsupportedFormatError(
96
                    'sorry, format %s not supported' % format,
97
                    ['use a different bzr version',
98
                     'or remove the .bzr directory'
99
                     ' and "bzr init" again'])
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
100
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.
101
    def clone(self, url, revision_id=None, basis=None, force_new_repo=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.
102
        """Clone this bzrdir and its contents to url verbatim.
103
104
        If urls last component does not exist, it will be created.
105
106
        if revision_id is not None, then the clone operation may tune
107
            itself to download less data.
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.
108
        :param force_new_repo: Do not use a shared repository for the target 
109
                               even if one is available.
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.
110
        """
111
        self._make_tail(url)
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.
112
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
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.
113
        result = self._format.initialize(url)
114
        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.
115
            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.
116
        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.
117
            local_repo = None
118
        if local_repo:
119
            # may need to copy content in
120
            if force_new_repo:
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
121
                result_repo = local_repo.clone(
122
                    result,
123
                    revision_id=revision_id,
124
                    basis=basis_repo)
125
                result_repo.set_make_working_trees(local_repo.make_working_trees())
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.
126
            else:
127
                try:
128
                    result_repo = result.find_repository()
129
                    # fetch content this dir needs.
130
                    if basis_repo:
131
                        # XXX FIXME RBC 20060214 need tests for this when the basis
132
                        # is incomplete
133
                        result_repo.fetch(basis_repo, revision_id=revision_id)
134
                    result_repo.fetch(local_repo, revision_id=revision_id)
135
                except errors.NoRepositoryPresent:
136
                    # needed to make one anyway.
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
137
                    result_repo = local_repo.clone(
138
                        result,
139
                        revision_id=revision_id,
140
                        basis=basis_repo)
141
                    result_repo.set_make_working_trees(local_repo.make_working_trees())
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.
142
        # 1 if there is a branch present
143
        #   make sure its content is available in the target repository
144
        #   clone it.
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.
145
        try:
146
            self.open_branch().clone(result, revision_id=revision_id)
147
        except errors.NotBranchError:
148
            pass
149
        try:
150
            self.open_workingtree().clone(result, basis=basis_tree)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
151
        except (errors.NoWorkingTree, errors.NotLocalUrl):
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.
152
            pass
153
        return result
154
155
    def _get_basis_components(self, basis):
156
        """Retrieve the basis components that are available at basis."""
157
        if basis is None:
158
            return None, None, None
159
        try:
160
            basis_tree = basis.open_workingtree()
161
            basis_branch = basis_tree.branch
162
            basis_repo = basis_branch.repository
163
        except (errors.NoWorkingTree, errors.NotLocalUrl):
164
            basis_tree = None
165
            try:
166
                basis_branch = basis.open_branch()
167
                basis_repo = basis_branch.repository
168
            except errors.NotBranchError:
169
                basis_branch = None
170
                try:
171
                    basis_repo = basis.open_repository()
172
                except errors.NoRepositoryPresent:
173
                    basis_repo = None
174
        return basis_repo, basis_branch, basis_tree
175
176
    def _make_tail(self, url):
177
        segments = url.split('/')
178
        if segments and segments[-1] not in ('', '.'):
179
            parent = '/'.join(segments[:-1])
180
            t = bzrlib.transport.get_transport(parent)
181
            try:
182
                t.mkdir(segments[-1])
183
            except errors.FileExists:
184
                pass
185
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
186
    @classmethod
187
    def create(cls, base):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
188
        """Create a new BzrDir at the url 'base'.
1534.4.39 by Robert Collins
Basic BzrDir support.
189
        
190
        This will call the current default formats initialize with base
191
        as the only parameter.
192
193
        If you need a specific format, consider creating an instance
194
        of that and calling initialize().
195
        """
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
196
        if cls is not BzrDir:
197
            raise AssertionError("BzrDir.create always creates the default format, "
198
                    "not one of %r" % cls)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
199
        segments = base.split('/')
200
        if segments and segments[-1] not in ('', '.'):
201
            parent = '/'.join(segments[:-1])
202
            t = bzrlib.transport.get_transport(parent)
203
            try:
204
                t.mkdir(segments[-1])
205
            except errors.FileExists:
206
                pass
1534.4.39 by Robert Collins
Basic BzrDir support.
207
        return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
208
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
209
    def create_branch(self):
210
        """Create a branch in this BzrDir.
211
212
        The bzrdirs format will control what branch format is created.
213
        For more control see BranchFormatXX.create(a_bzrdir).
214
        """
215
        raise NotImplementedError(self.create_branch)
216
217
    @staticmethod
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.
218
    def create_branch_and_repo(base, force_new_repo=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
219
        """Create a new BzrDir, Branch and Repository at the url 'base'.
220
221
        This will use the current default BzrDirFormat, and use whatever 
222
        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.
223
        create_repository. If a shared repository is available that is used
224
        preferentially.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
225
226
        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.
227
228
        :param base: The URL to create the branch at.
229
        :param force_new_repo: If True a new repository is always created.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
230
        """
231
        bzrdir = BzrDir.create(base)
1534.6.11 by Robert Collins
Review feedback.
232
        bzrdir._find_or_create_repository(force_new_repo)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
233
        return bzrdir.create_branch()
1534.6.11 by Robert Collins
Review feedback.
234
235
    def _find_or_create_repository(self, force_new_repo):
236
        """Create a new repository if needed, returning the repository."""
237
        if force_new_repo:
238
            return self.create_repository()
239
        try:
240
            return self.find_repository()
241
        except errors.NoRepositoryPresent:
242
            return self.create_repository()
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
243
        
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
244
    @staticmethod
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
245
    def create_branch_convenience(base, force_new_repo=False,
246
                                  force_new_tree=None, format=None):
1534.6.10 by Robert Collins
Finish use of repositories support.
247
        """Create a new BzrDir, Branch and Repository at the url 'base'.
248
249
        This is a convenience function - it will use an existing repository
250
        if possible, can be told explicitly whether to create a working tree or
1534.6.12 by Robert Collins
Typo found by John Meinel.
251
        not.
1534.6.10 by Robert Collins
Finish use of repositories support.
252
253
        This will use the current default BzrDirFormat, and use whatever 
254
        repository format that that uses via bzrdir.create_branch and
255
        create_repository. If a shared repository is available that is used
256
        preferentially. Whatever repository is used, its tree creation policy
257
        is followed.
258
259
        The created Branch object is returned.
260
        If a working tree cannot be made due to base not being a file:// url,
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.
261
        no error is raised unless force_new_tree is True, in which case no 
262
        data is created on disk and NotLocalUrl is raised.
1534.6.10 by Robert Collins
Finish use of repositories support.
263
264
        :param base: The URL to create the branch at.
265
        :param force_new_repo: If True a new repository is always created.
266
        :param force_new_tree: If True or False force creation of a tree or 
267
                               prevent such creation respectively.
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
268
        :param format: Override for the for the bzrdir format to create
1534.6.10 by Robert Collins
Finish use of repositories support.
269
        """
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.
270
        if force_new_tree:
271
            # check for non local urls
272
            t = get_transport(safe_unicode(base))
273
            if not isinstance(t, LocalTransport):
274
                raise errors.NotLocalUrl(base)
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
275
        if format is None:
276
            bzrdir = BzrDir.create(base)
277
        else:
278
            bzrdir = format.initialize(base)
1534.6.11 by Robert Collins
Review feedback.
279
        repo = bzrdir._find_or_create_repository(force_new_repo)
1534.6.10 by Robert Collins
Finish use of repositories support.
280
        result = bzrdir.create_branch()
281
        if force_new_tree or (repo.make_working_trees() and 
282
                              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.
283
            try:
284
                bzrdir.create_workingtree()
285
            except errors.NotLocalUrl:
286
                pass
1534.6.10 by Robert Collins
Finish use of repositories support.
287
        return result
288
        
289
    @staticmethod
1534.6.1 by Robert Collins
allow API creation of shared repositories
290
    def create_repository(base, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
291
        """Create a new BzrDir and Repository at the url 'base'.
292
293
        This will use the current default BzrDirFormat, and use whatever 
294
        repository format that that uses for bzrdirformat.create_repository.
295
1534.6.1 by Robert Collins
allow API creation of shared repositories
296
        ;param shared: Create a shared repository rather than a standalone
297
                       repository.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
298
        The Repository object is returned.
299
300
        This must be overridden as an instance method in child classes, where
301
        it should take no parameters and construct whatever repository format
302
        that child class desires.
303
        """
304
        bzrdir = BzrDir.create(base)
305
        return bzrdir.create_repository()
306
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
307
    @staticmethod
308
    def create_standalone_workingtree(base):
309
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
310
311
        'base' must be a local path or a file:// url.
312
313
        This will use the current default BzrDirFormat, and use whatever 
314
        repository format that that uses for bzrdirformat.create_workingtree,
315
        create_branch and create_repository.
316
317
        The WorkingTree object is returned.
318
        """
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
319
        t = get_transport(safe_unicode(base))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
320
        if not isinstance(t, LocalTransport):
321
            raise errors.NotLocalUrl(base)
1534.6.10 by Robert Collins
Finish use of repositories support.
322
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
323
                                               force_new_repo=True).bzrdir
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
324
        return bzrdir.create_workingtree()
325
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
326
    def create_workingtree(self, revision_id=None):
327
        """Create a working tree at this BzrDir.
328
        
329
        revision_id: create it as of this revision id.
330
        """
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
331
        raise NotImplementedError(self.create_workingtree)
332
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.
333
    def find_repository(self):
334
        """Find the repository that should be used for a_bzrdir.
335
336
        This does not require a branch as we use it to find the repo for
337
        new branches as well as to hook existing branches up to their
338
        repository.
339
        """
340
        try:
341
            return self.open_repository()
342
        except errors.NoRepositoryPresent:
343
            pass
344
        next_transport = self.root_transport.clone('..')
345
        while True:
346
            try:
1534.6.11 by Robert Collins
Review feedback.
347
                found_bzrdir = BzrDir.open_containing_from_transport(
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.
348
                    next_transport)[0]
349
            except errors.NotBranchError:
350
                raise errors.NoRepositoryPresent(self)
351
            try:
352
                repository = found_bzrdir.open_repository()
353
            except errors.NoRepositoryPresent:
354
                next_transport = found_bzrdir.root_transport.clone('..')
355
                continue
356
            if ((found_bzrdir.root_transport.base == 
357
                 self.root_transport.base) or repository.is_shared()):
358
                return repository
359
            else:
360
                raise errors.NoRepositoryPresent(self)
361
        raise errors.NoRepositoryPresent(self)
362
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
363
    def get_branch_transport(self, branch_format):
364
        """Get the transport for use by branch format in this BzrDir.
365
366
        Note that bzr dirs that do not support format strings will raise
367
        IncompatibleFormat if the branch format they are given has
368
        a format string, and vice verca.
369
370
        If branch_format is None, the transport is returned with no 
371
        checking. if it is not None, then the returned transport is
372
        guaranteed to point to an existing directory ready for use.
373
        """
374
        raise NotImplementedError(self.get_branch_transport)
375
        
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
376
    def get_repository_transport(self, repository_format):
377
        """Get the transport for use by repository format in this BzrDir.
378
379
        Note that bzr dirs that do not support format strings will raise
380
        IncompatibleFormat if the repository format they are given has
381
        a format string, and vice verca.
382
383
        If repository_format is None, the transport is returned with no 
384
        checking. if it is not None, then the returned transport is
385
        guaranteed to point to an existing directory ready for use.
386
        """
387
        raise NotImplementedError(self.get_repository_transport)
388
        
1534.4.53 by Robert Collins
Review feedback from John Meinel.
389
    def get_workingtree_transport(self, tree_format):
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
390
        """Get the transport for use by workingtree format in this BzrDir.
391
392
        Note that bzr dirs that do not support format strings will raise
393
        IncompatibleFormat if the workingtree format they are given has
394
        a format string, and vice verca.
395
396
        If workingtree_format is None, the transport is returned with no 
397
        checking. if it is not None, then the returned transport is
398
        guaranteed to point to an existing directory ready for use.
399
        """
400
        raise NotImplementedError(self.get_workingtree_transport)
401
        
1534.4.39 by Robert Collins
Basic BzrDir support.
402
    def __init__(self, _transport, _format):
403
        """Initialize a Bzr control dir object.
404
        
405
        Only really common logic should reside here, concrete classes should be
406
        made with varying behaviours.
407
1534.4.53 by Robert Collins
Review feedback from John Meinel.
408
        :param _format: the format that is creating this BzrDir instance.
409
        :param _transport: the transport this dir is based at.
1534.4.39 by Robert Collins
Basic BzrDir support.
410
        """
411
        self._format = _format
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
412
        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.
413
        self.root_transport = _transport
1534.4.39 by Robert Collins
Basic BzrDir support.
414
1534.5.16 by Robert Collins
Review feedback.
415
    def needs_format_conversion(self, format=None):
416
        """Return true if this bzrdir needs convert_format run on it.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
417
        
418
        For instance, if the repository format is out of date but the 
419
        branch and working tree are not, this should return True.
1534.5.13 by Robert Collins
Correct buggy test.
420
421
        :param format: Optional parameter indicating a specific desired
1534.5.16 by Robert Collins
Review feedback.
422
                       format we plan to arrive at.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
423
        """
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.
424
        raise NotImplementedError(self.needs_format_conversion)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
425
1534.4.39 by Robert Collins
Basic BzrDir support.
426
    @staticmethod
427
    def open_unsupported(base):
428
        """Open a branch which is not supported."""
429
        return BzrDir.open(base, _unsupported=True)
430
        
431
    @staticmethod
432
    def open(base, _unsupported=False):
1534.4.53 by Robert Collins
Review feedback from John Meinel.
433
        """Open an existing bzrdir, rooted at 'base' (url)
1534.4.39 by Robert Collins
Basic BzrDir support.
434
        
435
        _unsupported is a private parameter to the BzrDir class.
436
        """
437
        t = get_transport(base)
438
        mutter("trying to open %r with transport %r", base, t)
439
        format = BzrDirFormat.find_format(t)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
440
        BzrDir._check_supported(format, _unsupported)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
441
        return format.open(t, _found=True)
442
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
443
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
444
        """Open the branch object at this BzrDir if one is present.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
445
446
        If unsupported is True, then no longer supported branch formats can
447
        still be opened.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
448
        
449
        TODO: static convenience version of this?
450
        """
451
        raise NotImplementedError(self.open_branch)
1534.4.39 by Robert Collins
Basic BzrDir support.
452
453
    @staticmethod
454
    def open_containing(url):
455
        """Open an existing branch which contains url.
456
        
1534.6.3 by Robert Collins
find_repository sufficiently robust.
457
        :param url: url to search from.
1534.6.11 by Robert Collins
Review feedback.
458
        See open_containing_from_transport for more detail.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
459
        """
1534.6.11 by Robert Collins
Review feedback.
460
        return BzrDir.open_containing_from_transport(get_transport(url))
1534.6.3 by Robert Collins
find_repository sufficiently robust.
461
    
462
    @staticmethod
1534.6.11 by Robert Collins
Review feedback.
463
    def open_containing_from_transport(a_transport):
1534.6.3 by Robert Collins
find_repository sufficiently robust.
464
        """Open an existing branch which contains a_transport.base
465
466
        This probes for a branch at a_transport, and searches upwards from there.
1534.4.39 by Robert Collins
Basic BzrDir support.
467
468
        Basically we keep looking up until we find the control directory or
469
        run into the root.  If there isn't one, raises NotBranchError.
470
        If there is one and it is either an unrecognised format or an unsupported 
471
        format, UnknownFormatError or UnsupportedFormatError are raised.
472
        If there is one, it is returned, along with the unused portion of url.
473
        """
474
        # this gets the normalised url back. I.e. '.' -> the full path.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
475
        url = a_transport.base
1534.4.39 by Robert Collins
Basic BzrDir support.
476
        while True:
477
            try:
1534.6.3 by Robert Collins
find_repository sufficiently robust.
478
                format = BzrDirFormat.find_format(a_transport)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
479
                BzrDir._check_supported(format, False)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
480
                return format.open(a_transport), a_transport.relpath(url)
1534.4.39 by Robert Collins
Basic BzrDir support.
481
            except errors.NotBranchError, e:
1534.6.3 by Robert Collins
find_repository sufficiently robust.
482
                mutter('not a branch in: %r %s', a_transport.base, e)
483
            new_t = a_transport.clone('..')
484
            if new_t.base == a_transport.base:
1534.4.39 by Robert Collins
Basic BzrDir support.
485
                # reached the root, whatever that may be
486
                raise errors.NotBranchError(path=url)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
487
            a_transport = new_t
1534.4.39 by Robert Collins
Basic BzrDir support.
488
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
489
    def open_repository(self, _unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
490
        """Open the repository object at this BzrDir if one is present.
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
491
492
        This will not follow the Branch object pointer - its strictly a direct
493
        open facility. Most client code should use open_branch().repository to
494
        get at a repository.
495
496
        _unsupported is a private parameter, not part of the api.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
497
        TODO: static convenience version of this?
498
        """
499
        raise NotImplementedError(self.open_repository)
500
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
501
    def open_workingtree(self, _unsupported=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
502
        """Open the workingtree object at this BzrDir if one is present.
503
        
504
        TODO: static convenience version of this?
505
        """
506
        raise NotImplementedError(self.open_workingtree)
507
1662.1.19 by Martin Pool
Better error message when initting existing tree
508
    def has_branch(self):
509
        """Tell if this bzrdir contains a branch.
510
        
511
        Note: if you're going to open the branch, you should just go ahead
512
        and try, and not ask permission first.  (This method just opens the 
513
        branch and discards it, and that's somewhat expensive.) 
514
        """
515
        try:
516
            self.open_branch()
517
            return True
518
        except errors.NotBranchError:
519
            return False
520
521
    def has_workingtree(self):
522
        """Tell if this bzrdir contains a working tree.
523
524
        This will still raise an exception if the bzrdir has a workingtree that
525
        is remote & inaccessible.
526
        
527
        Note: if you're going to open the working tree, you should just go ahead
528
        and try, and not ask permission first.  (This method just opens the 
529
        workingtree and discards it, and that's somewhat expensive.) 
530
        """
531
        try:
532
            self.open_workingtree()
533
            return True
534
        except errors.NoWorkingTree:
535
            return False
536
1534.6.9 by Robert Collins
sprouting into shared repositories
537
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=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.
538
        """Create a copy of this bzrdir prepared for use as a new line of
539
        development.
540
541
        If urls last component does not exist, it will be created.
542
543
        Attributes related to the identity of the source branch like
544
        branch nickname will be cleaned, a working tree is created
545
        whether one existed before or not; and a local branch is always
546
        created.
547
548
        if revision_id is not None, then the clone operation may tune
549
            itself to download less data.
550
        """
551
        self._make_tail(url)
552
        result = self._format.initialize(url)
553
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
554
        try:
555
            source_branch = self.open_branch()
556
            source_repository = source_branch.repository
557
        except errors.NotBranchError:
558
            source_branch = None
559
            try:
560
                source_repository = self.open_repository()
561
            except errors.NoRepositoryPresent:
1534.6.9 by Robert Collins
sprouting into shared repositories
562
                # copy the entire basis one if there is one
563
                # but there is no 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.
564
                source_repository = basis_repo
1534.6.9 by Robert Collins
sprouting into shared repositories
565
        if force_new_repo:
566
            result_repo = None
567
        else:
568
            try:
569
                result_repo = result.find_repository()
570
            except errors.NoRepositoryPresent:
571
                result_repo = None
572
        if source_repository is None and result_repo is not None:
573
            pass
574
        elif source_repository is None and result_repo is None:
575
            # no repo available, make a new one
576
            result.create_repository()
577
        elif source_repository is not None and result_repo is None:
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
578
            # have source, and want to make a new target repo
579
            # we dont clone the repo because that preserves attributes
580
            # like is_shared(), and we have not yet implemented a 
581
            # repository sprout().
582
            result_repo = result.create_repository()
583
        if result_repo is not None:
1534.6.9 by Robert Collins
sprouting into shared repositories
584
            # fetch needed content into target.
585
            if basis_repo:
586
                # XXX FIXME RBC 20060214 need tests for this when the basis
587
                # is incomplete
588
                result_repo.fetch(basis_repo, revision_id=revision_id)
589
            result_repo.fetch(source_repository, 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.
590
        if source_branch is not None:
591
            source_branch.sprout(result, revision_id=revision_id)
592
        else:
593
            result.create_branch()
1558.5.8 by Aaron Bentley
Bugfix when result_repo is None
594
        if result_repo is None or result_repo.make_working_trees():
1558.5.7 by Aaron Bentley
Don't create unwanted working trees in branch
595
            result.create_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.
596
        return result
597
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
598
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
599
class BzrDirPreSplitOut(BzrDir):
600
    """A common class for the all-in-one formats."""
601
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
602
    def __init__(self, _transport, _format):
603
        """See BzrDir.__init__."""
604
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
605
        assert self._format._lock_class == TransportLock
606
        assert self._format._lock_file_name == 'branch-lock'
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
607
        self._control_files = LockableFiles(self.get_branch_transport(None),
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
608
                                            self._format._lock_file_name,
609
                                            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.
610
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
611
    def break_lock(self):
612
        """Pre-splitout bzrdirs do not suffer from stale locks."""
613
        raise NotImplementedError(self.break_lock)
614
1534.6.8 by Robert Collins
Test the use of clone on empty bzrdir with force_new_repo.
615
    def clone(self, url, revision_id=None, basis=None, force_new_repo=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.
616
        """See BzrDir.clone()."""
617
        from bzrlib.workingtree import WorkingTreeFormat2
618
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
619
        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.
620
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
621
        self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
622
        from_branch = self.open_branch()
623
        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.
624
        try:
625
            self.open_workingtree().clone(result, basis=basis_tree)
626
        except errors.NotLocalUrl:
627
            # make a new one, this format always has to have one.
1563.2.38 by Robert Collins
make push preserve tree formats.
628
            try:
629
                WorkingTreeFormat2().initialize(result)
630
            except errors.NotLocalUrl:
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
631
                # but we cannot do it for remote trees.
632
                to_branch = result.open_branch()
633
                WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
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.
634
        return result
635
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
636
    def create_branch(self):
637
        """See BzrDir.create_branch."""
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.
638
        return self.open_branch()
639
1534.6.1 by Robert Collins
allow API creation of shared repositories
640
    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.
641
        """See BzrDir.create_repository."""
1534.6.1 by Robert Collins
allow API creation of shared repositories
642
        if shared:
643
            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.
644
        return self.open_repository()
645
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
646
    def create_workingtree(self, revision_id=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.
647
        """See BzrDir.create_workingtree."""
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
648
        # this looks buggy but is not -really-
649
        # clone and sprout will have set the revision_id
650
        # and that will have set it for us, its only
651
        # specific uses of create_workingtree in isolation
652
        # that can do wonky stuff here, and that only
653
        # happens for creating checkouts, which cannot be 
654
        # done on this format anyway. So - acceptable wart.
655
        result = self.open_workingtree()
1508.1.24 by Robert Collins
Add update command for use with checkouts.
656
        if revision_id is not None:
657
            result.set_last_revision(revision_id)
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
658
        return result
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
659
660
    def get_branch_transport(self, branch_format):
661
        """See BzrDir.get_branch_transport()."""
662
        if branch_format is None:
663
            return self.transport
664
        try:
665
            branch_format.get_format_string()
666
        except NotImplementedError:
667
            return self.transport
668
        raise errors.IncompatibleFormat(branch_format, self._format)
669
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
670
    def get_repository_transport(self, repository_format):
671
        """See BzrDir.get_repository_transport()."""
672
        if repository_format is None:
673
            return self.transport
674
        try:
675
            repository_format.get_format_string()
676
        except NotImplementedError:
677
            return self.transport
678
        raise errors.IncompatibleFormat(repository_format, self._format)
679
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
680
    def get_workingtree_transport(self, workingtree_format):
681
        """See BzrDir.get_workingtree_transport()."""
682
        if workingtree_format is None:
683
            return self.transport
684
        try:
685
            workingtree_format.get_format_string()
686
        except NotImplementedError:
687
            return self.transport
688
        raise errors.IncompatibleFormat(workingtree_format, self._format)
689
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.
690
    def needs_format_conversion(self, format=None):
691
        """See BzrDir.needs_format_conversion()."""
692
        # if the format is not the same as the system default,
693
        # an upgrade is needed.
694
        if format is None:
695
            format = BzrDirFormat.get_default_format()
696
        return not isinstance(self._format, format.__class__)
697
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
698
    def open_branch(self, unsupported=False):
699
        """See BzrDir.open_branch."""
700
        from bzrlib.branch import BzrBranchFormat4
701
        format = BzrBranchFormat4()
702
        self._check_supported(format, unsupported)
703
        return format.open(self, _found=True)
704
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.
705
    def sprout(self, url, revision_id=None, basis=None):
706
        """See BzrDir.sprout()."""
707
        from bzrlib.workingtree import WorkingTreeFormat2
708
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
709
        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.
710
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
711
        try:
712
            self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
713
        except errors.NoRepositoryPresent:
714
            pass
715
        try:
716
            self.open_branch().sprout(result, revision_id=revision_id)
717
        except errors.NotBranchError:
718
            pass
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
719
        # we always want a working tree
720
        WorkingTreeFormat2().initialize(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.
721
        return result
722
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
723
724
class BzrDir4(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
725
    """A .bzr version 4 control object.
726
    
727
    This is a deprecated format and may be removed after sept 2006.
728
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
729
1534.6.1 by Robert Collins
allow API creation of shared repositories
730
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
731
        """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.
732
        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.
733
1534.5.16 by Robert Collins
Review feedback.
734
    def needs_format_conversion(self, format=None):
735
        """Format 4 dirs are always in need of conversion."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
736
        return True
737
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
738
    def open_repository(self):
739
        """See BzrDir.open_repository."""
740
        from bzrlib.repository import RepositoryFormat4
741
        return RepositoryFormat4().open(self, _found=True)
742
743
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
744
class BzrDir5(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
745
    """A .bzr version 5 control object.
746
747
    This is a deprecated format and may be removed after sept 2006.
748
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
749
750
    def open_repository(self):
751
        """See BzrDir.open_repository."""
752
        from bzrlib.repository import RepositoryFormat5
753
        return RepositoryFormat5().open(self, _found=True)
754
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
755
    def open_workingtree(self, _unsupported=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
756
        """See BzrDir.create_workingtree."""
757
        from bzrlib.workingtree import WorkingTreeFormat2
758
        return WorkingTreeFormat2().open(self, _found=True)
759
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
760
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
761
class BzrDir6(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
762
    """A .bzr version 6 control object.
763
764
    This is a deprecated format and may be removed after sept 2006.
765
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
766
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
767
    def open_repository(self):
768
        """See BzrDir.open_repository."""
769
        from bzrlib.repository import RepositoryFormat6
770
        return RepositoryFormat6().open(self, _found=True)
771
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
772
    def open_workingtree(self, _unsupported=False):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
773
        """See BzrDir.create_workingtree."""
774
        from bzrlib.workingtree import WorkingTreeFormat2
775
        return WorkingTreeFormat2().open(self, _found=True)
776
777
778
class BzrDirMeta1(BzrDir):
779
    """A .bzr meta version 1 control object.
780
    
781
    This is the first control object where the 
1553.5.67 by Martin Pool
doc
782
    individual aspects are really split out: there are separate repository,
783
    workingtree and branch subdirectories and any subset of the three can be
784
    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.
785
    """
786
1534.5.16 by Robert Collins
Review feedback.
787
    def can_convert_format(self):
788
        """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.
789
        return True
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
790
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
791
    def create_branch(self):
792
        """See BzrDir.create_branch."""
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
793
        from bzrlib.branch import BranchFormat
794
        return BranchFormat.get_default_format().initialize(self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
795
1534.6.1 by Robert Collins
allow API creation of shared repositories
796
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
797
        """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.
798
        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.
799
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
800
    def create_workingtree(self, revision_id=None):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
801
        """See BzrDir.create_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
802
        from bzrlib.workingtree import WorkingTreeFormat
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
803
        return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
804
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
805
    def _get_mkdir_mode(self):
806
        """Figure out the mode to use when creating a bzrdir subdir."""
807
        temp_control = LockableFiles(self.transport, '', TransportLock)
808
        return temp_control._dir_mode
809
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
810
    def get_branch_transport(self, branch_format):
811
        """See BzrDir.get_branch_transport()."""
812
        if branch_format is None:
813
            return self.transport.clone('branch')
814
        try:
815
            branch_format.get_format_string()
816
        except NotImplementedError:
817
            raise errors.IncompatibleFormat(branch_format, self._format)
818
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
819
            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.
820
        except errors.FileExists:
821
            pass
822
        return self.transport.clone('branch')
823
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
824
    def get_repository_transport(self, repository_format):
825
        """See BzrDir.get_repository_transport()."""
826
        if repository_format is None:
827
            return self.transport.clone('repository')
828
        try:
829
            repository_format.get_format_string()
830
        except NotImplementedError:
831
            raise errors.IncompatibleFormat(repository_format, self._format)
832
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
833
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
834
        except errors.FileExists:
835
            pass
836
        return self.transport.clone('repository')
837
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
838
    def get_workingtree_transport(self, workingtree_format):
839
        """See BzrDir.get_workingtree_transport()."""
840
        if workingtree_format is None:
841
            return self.transport.clone('checkout')
842
        try:
843
            workingtree_format.get_format_string()
844
        except NotImplementedError:
845
            raise errors.IncompatibleFormat(workingtree_format, self._format)
846
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
847
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
848
        except errors.FileExists:
849
            pass
850
        return self.transport.clone('checkout')
851
1534.5.16 by Robert Collins
Review feedback.
852
    def needs_format_conversion(self, format=None):
853
        """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.
854
        if format is None:
855
            format = BzrDirFormat.get_default_format()
856
        if not isinstance(self._format, format.__class__):
857
            # it is not a meta dir format, conversion is needed.
858
            return True
859
        # we might want to push this down to the repository?
860
        try:
861
            if not isinstance(self.open_repository()._format,
862
                              format.repository_format.__class__):
863
                # the repository needs an upgrade.
864
                return True
865
        except errors.NoRepositoryPresent:
866
            pass
867
        # currently there are no other possible conversions for meta1 formats.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
868
        return False
869
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
870
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
871
        """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.
872
        from bzrlib.branch import BranchFormat
873
        format = BranchFormat.find_format(self)
874
        self._check_supported(format, unsupported)
875
        return format.open(self, _found=True)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
876
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
877
    def open_repository(self, unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
878
        """See BzrDir.open_repository."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
879
        from bzrlib.repository import RepositoryFormat
880
        format = RepositoryFormat.find_format(self)
881
        self._check_supported(format, unsupported)
882
        return format.open(self, _found=True)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
883
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
884
    def open_workingtree(self, unsupported=False):
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
885
        """See BzrDir.open_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
886
        from bzrlib.workingtree import WorkingTreeFormat
887
        format = WorkingTreeFormat.find_format(self)
888
        self._check_supported(format, unsupported)
889
        return format.open(self, _found=True)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
890
1534.4.39 by Robert Collins
Basic BzrDir support.
891
892
class BzrDirFormat(object):
893
    """An encapsulation of the initialization and open routines for a format.
894
895
    Formats provide three things:
896
     * An initialization routine,
897
     * a format string,
898
     * an open routine.
899
900
    Formats are placed in an dict by their format string for reference 
901
    during bzrdir opening. These should be subclasses of BzrDirFormat
902
    for consistency.
903
904
    Once a format is deprecated, just deprecate the initialize and open
905
    methods on the format class. Do not deprecate the object, as the 
906
    object will be created every system load.
907
    """
908
909
    _default_format = None
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
910
    """The default format used for new .bzr dirs."""
1534.4.39 by Robert Collins
Basic BzrDir support.
911
912
    _formats = {}
913
    """The known formats."""
914
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
915
    _lock_file_name = 'branch-lock'
916
917
    # _lock_class must be set in subclasses to the lock type, typ.
918
    # TransportLock or LockDir
919
1534.4.39 by Robert Collins
Basic BzrDir support.
920
    @classmethod
921
    def find_format(klass, transport):
922
        """Return the format registered for URL."""
923
        try:
924
            format_string = transport.get(".bzr/branch-format").read()
925
            return klass._formats[format_string]
926
        except errors.NoSuchFile:
927
            raise errors.NotBranchError(path=transport.base)
928
        except KeyError:
929
            raise errors.UnknownFormatError(format_string)
930
931
    @classmethod
932
    def get_default_format(klass):
933
        """Return the current default format."""
934
        return klass._default_format
935
936
    def get_format_string(self):
937
        """Return the ASCII format string that identifies this format."""
938
        raise NotImplementedError(self.get_format_string)
939
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
940
    def get_format_description(self):
941
        """Return the short description for this format."""
942
        raise NotImplementedError(self.get_format_description)
943
1534.5.16 by Robert Collins
Review feedback.
944
    def get_converter(self, format=None):
945
        """Return the converter to use to convert bzrdirs needing converts.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
946
947
        This returns a bzrlib.bzrdir.Converter object.
948
949
        This should return the best upgrader to step this format towards the
950
        current default format. In the case of plugins we can/shouold provide
951
        some means for them to extend the range of returnable converters.
1534.5.13 by Robert Collins
Correct buggy test.
952
953
        :param format: Optional format to override the default foramt of the 
954
                       library.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
955
        """
1534.5.16 by Robert Collins
Review feedback.
956
        raise NotImplementedError(self.get_converter)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
957
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
958
    def initialize(self, url):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
959
        """Create a bzr control dir at this url and return an opened copy.
960
        
961
        Subclasses should typically override initialize_on_transport
962
        instead of this method.
963
        """
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
964
        return self.initialize_on_transport(get_transport(url))
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
965
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
966
    def initialize_on_transport(self, transport):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
967
        """Initialize a new bzrdir in the base directory of a Transport."""
968
        # Since we don'transport have a .bzr directory, inherit the
1534.4.39 by Robert Collins
Basic BzrDir support.
969
        # mode from the root directory
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
970
        temp_control = LockableFiles(transport, '', TransportLock)
1534.4.39 by Robert Collins
Basic BzrDir support.
971
        temp_control._transport.mkdir('.bzr',
972
                                      # FIXME: RBC 20060121 dont peek under
973
                                      # the covers
974
                                      mode=temp_control._dir_mode)
975
        file_mode = temp_control._file_mode
976
        del temp_control
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
977
        mutter('created control directory in ' + transport.base)
978
        control = transport.clone('.bzr')
1534.4.39 by Robert Collins
Basic BzrDir support.
979
        utf8_files = [('README', 
980
                       "This is a Bazaar-NG control directory.\n"
981
                       "Do not change any files in this directory.\n"),
982
                      ('branch-format', self.get_format_string()),
983
                      ]
984
        # NB: no need to escape relative paths that are url safe.
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
985
        control_files = LockableFiles(control, self._lock_file_name, 
986
                                      self._lock_class)
1553.5.60 by Martin Pool
New LockableFiles.create_lock() method
987
        control_files.create_lock()
1534.4.39 by Robert Collins
Basic BzrDir support.
988
        control_files.lock_write()
989
        try:
990
            for file, content in utf8_files:
991
                control_files.put_utf8(file, content)
992
        finally:
993
            control_files.unlock()
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
994
        return self.open(transport, _found=True)
1534.4.39 by Robert Collins
Basic BzrDir support.
995
996
    def is_supported(self):
997
        """Is this format supported?
998
999
        Supported formats must be initializable and openable.
1000
        Unsupported formats may not support initialization or committing or 
1001
        some other features depending on the reason for not being supported.
1002
        """
1003
        return True
1004
1005
    def open(self, transport, _found=False):
1006
        """Return an instance of this format for the dir transport points at.
1007
        
1008
        _found is a private parameter, do not use it.
1009
        """
1010
        if not _found:
1011
            assert isinstance(BzrDirFormat.find_format(transport),
1012
                              self.__class__)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1013
        return self._open(transport)
1014
1015
    def _open(self, transport):
1016
        """Template method helper for opening BzrDirectories.
1017
1018
        This performs the actual open and any additional logic or parameter
1019
        passing.
1020
        """
1021
        raise NotImplementedError(self._open)
1534.4.39 by Robert Collins
Basic BzrDir support.
1022
1023
    @classmethod
1024
    def register_format(klass, format):
1025
        klass._formats[format.get_format_string()] = format
1026
1027
    @classmethod
1028
    def set_default_format(klass, format):
1029
        klass._default_format = format
1030
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
1031
    def __str__(self):
1032
        return self.get_format_string()[:-1]
1033
1534.4.39 by Robert Collins
Basic BzrDir support.
1034
    @classmethod
1035
    def unregister_format(klass, format):
1036
        assert klass._formats[format.get_format_string()] is format
1037
        del klass._formats[format.get_format_string()]
1038
1039
1040
class BzrDirFormat4(BzrDirFormat):
1041
    """Bzr dir format 4.
1042
1043
    This format is a combined format for working tree, branch and repository.
1044
    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.
1045
     - Format 1 working trees [always]
1046
     - Format 4 branches [always]
1047
     - Format 4 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1048
1049
    This format is deprecated: it indexes texts using a text it which is
1050
    removed in format 5; write support for this format has been removed.
1051
    """
1052
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1053
    _lock_class = TransportLock
1054
1534.4.39 by Robert Collins
Basic BzrDir support.
1055
    def get_format_string(self):
1056
        """See BzrDirFormat.get_format_string()."""
1057
        return "Bazaar-NG branch, format 0.0.4\n"
1058
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1059
    def get_format_description(self):
1060
        """See BzrDirFormat.get_format_description()."""
1061
        return "All-in-one format 4"
1062
1534.5.16 by Robert Collins
Review feedback.
1063
    def get_converter(self, format=None):
1064
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1065
        # 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.
1066
        return ConvertBzrDir4To5()
1067
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1068
    def initialize_on_transport(self, transport):
1534.4.39 by Robert Collins
Basic BzrDir support.
1069
        """Format 4 branches cannot be created."""
1070
        raise errors.UninitializableFormat(self)
1071
1072
    def is_supported(self):
1073
        """Format 4 is not supported.
1074
1075
        It is not supported because the model changed from 4 to 5 and the
1076
        conversion logic is expensive - so doing it on the fly was not 
1077
        feasible.
1078
        """
1079
        return False
1080
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1081
    def _open(self, transport):
1082
        """See BzrDirFormat._open."""
1083
        return BzrDir4(transport, self)
1084
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.
1085
    def __return_repository_format(self):
1086
        """Circular import protection."""
1087
        from bzrlib.repository import RepositoryFormat4
1088
        return RepositoryFormat4(self)
1089
    repository_format = property(__return_repository_format)
1090
1534.4.39 by Robert Collins
Basic BzrDir support.
1091
1092
class BzrDirFormat5(BzrDirFormat):
1093
    """Bzr control format 5.
1094
1095
    This format is a combined format for working tree, branch and repository.
1096
    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.
1097
     - Format 2 working trees [always] 
1098
     - Format 4 branches [always] 
1534.4.53 by Robert Collins
Review feedback from John Meinel.
1099
     - 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.
1100
       Unhashed stores in the repository.
1534.4.39 by Robert Collins
Basic BzrDir support.
1101
    """
1102
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1103
    _lock_class = TransportLock
1104
1534.4.39 by Robert Collins
Basic BzrDir support.
1105
    def get_format_string(self):
1106
        """See BzrDirFormat.get_format_string()."""
1107
        return "Bazaar-NG branch, format 5\n"
1108
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1109
    def get_format_description(self):
1110
        """See BzrDirFormat.get_format_description()."""
1111
        return "All-in-one format 5"
1112
1534.5.16 by Robert Collins
Review feedback.
1113
    def get_converter(self, format=None):
1114
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1115
        # 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.
1116
        return ConvertBzrDir5To6()
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1117
1118
    def _initialize_for_clone(self, url):
1119
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1120
        
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1121
    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.
1122
        """Format 5 dirs always have working tree, branch and repository.
1123
        
1124
        Except when they are being cloned.
1125
        """
1126
        from bzrlib.branch import BzrBranchFormat4
1127
        from bzrlib.repository import RepositoryFormat5
1128
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1129
        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.
1130
        RepositoryFormat5().initialize(result, _internal=True)
1131
        if not _cloning:
1132
            BzrBranchFormat4().initialize(result)
1133
            WorkingTreeFormat2().initialize(result)
1134
        return result
1135
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1136
    def _open(self, transport):
1137
        """See BzrDirFormat._open."""
1138
        return BzrDir5(transport, self)
1139
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
    def __return_repository_format(self):
1141
        """Circular import protection."""
1142
        from bzrlib.repository import RepositoryFormat5
1143
        return RepositoryFormat5(self)
1144
    repository_format = property(__return_repository_format)
1145
1534.4.39 by Robert Collins
Basic BzrDir support.
1146
1147
class BzrDirFormat6(BzrDirFormat):
1148
    """Bzr control format 6.
1149
1150
    This format is a combined format for working tree, branch and repository.
1151
    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.
1152
     - Format 2 working trees [always] 
1153
     - Format 4 branches [always] 
1154
     - Format 6 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1155
    """
1156
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1157
    _lock_class = TransportLock
1158
1534.4.39 by Robert Collins
Basic BzrDir support.
1159
    def get_format_string(self):
1160
        """See BzrDirFormat.get_format_string()."""
1161
        return "Bazaar-NG branch, format 6\n"
1162
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1163
    def get_format_description(self):
1164
        """See BzrDirFormat.get_format_description()."""
1165
        return "All-in-one format 6"
1166
1534.5.16 by Robert Collins
Review feedback.
1167
    def get_converter(self, format=None):
1168
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1169
        # 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.
1170
        return ConvertBzrDir6ToMeta()
1171
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1172
    def _initialize_for_clone(self, url):
1173
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1174
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1175
    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.
1176
        """Format 6 dirs always have working tree, branch and repository.
1177
        
1178
        Except when they are being cloned.
1179
        """
1180
        from bzrlib.branch import BzrBranchFormat4
1181
        from bzrlib.repository import RepositoryFormat6
1182
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1183
        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.
1184
        RepositoryFormat6().initialize(result, _internal=True)
1185
        if not _cloning:
1186
            BzrBranchFormat4().initialize(result)
1187
            try:
1188
                WorkingTreeFormat2().initialize(result)
1189
            except errors.NotLocalUrl:
1190
                # emulate pre-check behaviour for working tree and silently 
1191
                # fail.
1192
                pass
1193
        return result
1194
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1195
    def _open(self, transport):
1196
        """See BzrDirFormat._open."""
1197
        return BzrDir6(transport, self)
1198
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.
1199
    def __return_repository_format(self):
1200
        """Circular import protection."""
1201
        from bzrlib.repository import RepositoryFormat6
1202
        return RepositoryFormat6(self)
1203
    repository_format = property(__return_repository_format)
1204
1534.4.39 by Robert Collins
Basic BzrDir support.
1205
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1206
class BzrDirMetaFormat1(BzrDirFormat):
1207
    """Bzr meta control format 1
1208
1209
    This is the first format with split out working tree, branch and repository
1210
    disk storage.
1211
    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.
1212
     - Format 3 working trees [optional]
1213
     - Format 5 branches [optional]
1214
     - 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.
1215
    """
1216
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1217
    _lock_class = LockDir
1218
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.
1219
    def get_converter(self, format=None):
1220
        """See BzrDirFormat.get_converter()."""
1221
        if format is None:
1222
            format = BzrDirFormat.get_default_format()
1223
        if not isinstance(self, format.__class__):
1224
            # converting away from metadir is not implemented
1225
            raise NotImplementedError(self.get_converter)
1226
        return ConvertMetaToMeta(format)
1227
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1228
    def get_format_string(self):
1229
        """See BzrDirFormat.get_format_string()."""
1230
        return "Bazaar-NG meta directory, format 1\n"
1231
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1232
    def get_format_description(self):
1233
        """See BzrDirFormat.get_format_description()."""
1234
        return "Meta directory format 1"
1235
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1236
    def _open(self, transport):
1237
        """See BzrDirFormat._open."""
1238
        return BzrDirMeta1(transport, self)
1239
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.
1240
    def __return_repository_format(self):
1241
        """Circular import protection."""
1242
        if getattr(self, '_repository_format', None):
1243
            return self._repository_format
1244
        from bzrlib.repository import RepositoryFormat
1245
        return RepositoryFormat.get_default_format()
1246
1247
    def __set_repository_format(self, value):
1248
        """Allow changint the repository format for metadir formats."""
1249
        self._repository_format = value
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
1250
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.
1251
    repository_format = property(__return_repository_format, __set_repository_format)
1252
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1253
1534.4.39 by Robert Collins
Basic BzrDir support.
1254
BzrDirFormat.register_format(BzrDirFormat4())
1255
BzrDirFormat.register_format(BzrDirFormat5())
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1256
BzrDirFormat.register_format(BzrDirFormat6())
1257
__default_format = BzrDirMetaFormat1()
1534.4.39 by Robert Collins
Basic BzrDir support.
1258
BzrDirFormat.register_format(__default_format)
1259
BzrDirFormat.set_default_format(__default_format)
1260
1261
1262
class BzrDirTestProviderAdapter(object):
1263
    """A tool to generate a suite testing multiple bzrdir formats at once.
1264
1265
    This is done by copying the test once for each transport and injecting
1266
    the transport_server, transport_readonly_server, and bzrdir_format
1267
    classes into each copy. Each copy is also given a new id() to make it
1268
    easy to identify.
1269
    """
1270
1271
    def __init__(self, transport_server, transport_readonly_server, formats):
1272
        self._transport_server = transport_server
1273
        self._transport_readonly_server = transport_readonly_server
1274
        self._formats = formats
1275
    
1276
    def adapt(self, test):
1277
        result = TestSuite()
1278
        for format in self._formats:
1279
            new_test = deepcopy(test)
1280
            new_test.transport_server = self._transport_server
1281
            new_test.transport_readonly_server = self._transport_readonly_server
1282
            new_test.bzrdir_format = format
1283
            def make_new_test_id():
1284
                new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1285
                return lambda: new_id
1286
            new_test.id = make_new_test_id()
1287
            result.addTest(new_test)
1288
        return result
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1289
1290
1291
class ScratchDir(BzrDir6):
1292
    """Special test class: a bzrdir that cleans up itself..
1293
1294
    >>> d = ScratchDir()
1295
    >>> base = d.transport.base
1296
    >>> isdir(base)
1297
    True
1298
    >>> b.transport.__del__()
1299
    >>> isdir(base)
1300
    False
1301
    """
1302
1303
    def __init__(self, files=[], dirs=[], transport=None):
1304
        """Make a test branch.
1305
1306
        This creates a temporary directory and runs init-tree in it.
1307
1308
        If any files are listed, they are created in the working copy.
1309
        """
1310
        if transport is None:
1311
            transport = bzrlib.transport.local.ScratchTransport()
1312
            # local import for scope restriction
1313
            BzrDirFormat6().initialize(transport.base)
1314
            super(ScratchDir, self).__init__(transport, BzrDirFormat6())
1315
            self.create_repository()
1316
            self.create_branch()
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.
1317
            self.create_workingtree()
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1318
        else:
1319
            super(ScratchDir, self).__init__(transport, BzrDirFormat6())
1320
1321
        # BzrBranch creates a clone to .bzr and then forgets about the
1322
        # original transport. A ScratchTransport() deletes itself and
1323
        # everything underneath it when it goes away, so we need to
1324
        # grab a local copy to prevent that from happening
1325
        self._transport = transport
1326
1327
        for d in dirs:
1328
            self._transport.mkdir(d)
1329
            
1330
        for f in files:
1331
            self._transport.put(f, 'content of %s' % f)
1332
1333
    def clone(self):
1334
        """
1335
        >>> orig = ScratchDir(files=["file1", "file2"])
1336
        >>> os.listdir(orig.base)
1337
        [u'.bzr', u'file1', u'file2']
1338
        >>> clone = orig.clone()
1339
        >>> if os.name != 'nt':
1340
        ...   os.path.samefile(orig.base, clone.base)
1341
        ... else:
1342
        ...   orig.base == clone.base
1343
        ...
1344
        False
1345
        >>> os.listdir(clone.base)
1346
        [u'.bzr', u'file1', u'file2']
1347
        """
1348
        from shutil import copytree
1349
        from bzrlib.osutils import mkdtemp
1350
        base = mkdtemp()
1351
        os.rmdir(base)
1352
        copytree(self.base, base, symlinks=True)
1353
        return ScratchDir(
1354
            transport=bzrlib.transport.local.ScratchTransport(base))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1355
1356
1357
class Converter(object):
1358
    """Converts a disk format object from one format to another."""
1359
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1360
    def convert(self, to_convert, pb):
1361
        """Perform the conversion of to_convert, giving feedback via pb.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1362
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1363
        :param to_convert: The disk object to convert.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1364
        :param pb: a progress bar to use for progress information.
1365
        """
1366
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.
1367
    def step(self, message):
1368
        """Update the pb by a step."""
1369
        self.count +=1
1370
        self.pb.update(message, self.count, self.total)
1371
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1372
1373
class ConvertBzrDir4To5(Converter):
1374
    """Converts format 4 bzr dirs to format 5."""
1375
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1376
    def __init__(self):
1377
        super(ConvertBzrDir4To5, self).__init__()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1378
        self.converted_revs = set()
1379
        self.absent_revisions = set()
1380
        self.text_count = 0
1381
        self.revisions = {}
1382
        
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1383
    def convert(self, to_convert, pb):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1384
        """See Converter.convert()."""
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1385
        self.bzrdir = to_convert
1386
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1387
        self.pb.note('starting upgrade from format 4 to 5')
1388
        if isinstance(self.bzrdir.transport, LocalTransport):
1389
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1390
        self._convert_to_weaves()
1391
        return BzrDir.open(self.bzrdir.root_transport.base)
1392
1393
    def _convert_to_weaves(self):
1394
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1395
        try:
1396
            # TODO permissions
1397
            stat = self.bzrdir.transport.stat('weaves')
1398
            if not S_ISDIR(stat.st_mode):
1399
                self.bzrdir.transport.delete('weaves')
1400
                self.bzrdir.transport.mkdir('weaves')
1401
        except errors.NoSuchFile:
1402
            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.
1403
        # 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.
1404
        self.inv_weave = Weave('inventory')
1405
        # holds in-memory weaves for all files
1406
        self.text_weaves = {}
1407
        self.bzrdir.transport.delete('branch-format')
1408
        self.branch = self.bzrdir.open_branch()
1409
        self._convert_working_inv()
1410
        rev_history = self.branch.revision_history()
1411
        # to_read is a stack holding the revisions we still need to process;
1412
        # appending to it adds new highest-priority revisions
1413
        self.known_revisions = set(rev_history)
1414
        self.to_read = rev_history[-1:]
1415
        while self.to_read:
1416
            rev_id = self.to_read.pop()
1417
            if (rev_id not in self.revisions
1418
                and rev_id not in self.absent_revisions):
1419
                self._load_one_rev(rev_id)
1420
        self.pb.clear()
1421
        to_import = self._make_order()
1422
        for i, rev_id in enumerate(to_import):
1423
            self.pb.update('converting revision', i, len(to_import))
1424
            self._convert_one_rev(rev_id)
1425
        self.pb.clear()
1426
        self._write_all_weaves()
1427
        self._write_all_revs()
1428
        self.pb.note('upgraded to weaves:')
1429
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
1430
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
1431
        self.pb.note('  %6d texts', self.text_count)
1432
        self._cleanup_spare_files_after_format4()
1433
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1434
1435
    def _cleanup_spare_files_after_format4(self):
1436
        # FIXME working tree upgrade foo.
1437
        for n in 'merged-patches', 'pending-merged-patches':
1438
            try:
1439
                ## assert os.path.getsize(p) == 0
1440
                self.bzrdir.transport.delete(n)
1441
            except errors.NoSuchFile:
1442
                pass
1443
        self.bzrdir.transport.delete_tree('inventory-store')
1444
        self.bzrdir.transport.delete_tree('text-store')
1445
1446
    def _convert_working_inv(self):
1447
        inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
1448
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
1449
        # FIXME inventory is a working tree change.
1450
        self.branch.control_files.put('inventory', new_inv_xml)
1451
1452
    def _write_all_weaves(self):
1453
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1454
        weave_transport = self.bzrdir.transport.clone('weaves')
1455
        weaves = WeaveStore(weave_transport, prefixed=False)
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1456
        transaction = WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1457
1458
        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.
1459
            i = 0
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1460
            for file_id, file_weave in self.text_weaves.items():
1461
                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.
1462
                weaves._put_weave(file_id, file_weave, transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1463
                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.
1464
            self.pb.update('inventory', 0, 1)
1465
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
1466
            self.pb.update('inventory', 1, 1)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1467
        finally:
1468
            self.pb.clear()
1469
1470
    def _write_all_revs(self):
1471
        """Write all revisions out in new form."""
1472
        self.bzrdir.transport.delete_tree('revision-store')
1473
        self.bzrdir.transport.mkdir('revision-store')
1474
        revision_transport = self.bzrdir.transport.clone('revision-store')
1475
        # TODO permissions
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1476
        _revision_store = TextRevisionStore(TextStore(revision_transport,
1477
                                                      prefixed=False,
1478
                                                      compressed=True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1479
        try:
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1480
            transaction = bzrlib.transactions.WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1481
            for i, rev_id in enumerate(self.converted_revs):
1482
                self.pb.update('write revision', i, len(self.converted_revs))
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1483
                _revision_store.add_revision(self.revisions[rev_id], transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1484
        finally:
1485
            self.pb.clear()
1486
            
1487
    def _load_one_rev(self, rev_id):
1488
        """Load a revision object into memory.
1489
1490
        Any parents not either loaded or abandoned get queued to be
1491
        loaded."""
1492
        self.pb.update('loading revision',
1493
                       len(self.revisions),
1494
                       len(self.known_revisions))
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1495
        if not self.branch.repository.has_revision(rev_id):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1496
            self.pb.clear()
1497
            self.pb.note('revision {%s} not present in branch; '
1498
                         'will be converted as a ghost',
1499
                         rev_id)
1500
            self.absent_revisions.add(rev_id)
1501
        else:
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1502
            rev = self.branch.repository._revision_store.get_revision(rev_id,
1503
                self.branch.repository.get_transaction())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1504
            for parent_id in rev.parent_ids:
1505
                self.known_revisions.add(parent_id)
1506
                self.to_read.append(parent_id)
1507
            self.revisions[rev_id] = rev
1508
1509
    def _load_old_inventory(self, rev_id):
1510
        assert rev_id not in self.converted_revs
1511
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1512
        inv = serializer_v4.read_inventory_from_string(old_inv_xml)
1513
        rev = self.revisions[rev_id]
1514
        if rev.inventory_sha1:
1515
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1516
                'inventory sha mismatch for {%s}' % rev_id
1517
        return inv
1518
1519
    def _load_updated_inventory(self, rev_id):
1520
        assert rev_id in self.converted_revs
1521
        inv_xml = self.inv_weave.get_text(rev_id)
1522
        inv = serializer_v5.read_inventory_from_string(inv_xml)
1523
        return inv
1524
1525
    def _convert_one_rev(self, rev_id):
1526
        """Convert revision and all referenced objects to new format."""
1527
        rev = self.revisions[rev_id]
1528
        inv = self._load_old_inventory(rev_id)
1529
        present_parents = [p for p in rev.parent_ids
1530
                           if p not in self.absent_revisions]
1531
        self._convert_revision_contents(rev, inv, present_parents)
1532
        self._store_new_weave(rev, inv, present_parents)
1533
        self.converted_revs.add(rev_id)
1534
1535
    def _store_new_weave(self, rev, inv, present_parents):
1536
        # the XML is now updated with text versions
1537
        if __debug__:
1538
            for file_id in inv:
1539
                ie = inv[file_id]
1540
                if ie.kind == 'root_directory':
1541
                    continue
1542
                assert hasattr(ie, 'revision'), \
1543
                    'no revision on {%s} in {%s}' % \
1544
                    (file_id, rev.revision_id)
1545
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
1546
        new_inv_sha1 = sha_string(new_inv_xml)
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1547
        self.inv_weave.add_lines(rev.revision_id, 
1548
                                 present_parents,
1549
                                 new_inv_xml.splitlines(True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1550
        rev.inventory_sha1 = new_inv_sha1
1551
1552
    def _convert_revision_contents(self, rev, inv, present_parents):
1553
        """Convert all the files within a revision.
1554
1555
        Also upgrade the inventory to refer to the text revision ids."""
1556
        rev_id = rev.revision_id
1557
        mutter('converting texts of revision {%s}',
1558
               rev_id)
1559
        parent_invs = map(self._load_updated_inventory, present_parents)
1560
        for file_id in inv:
1561
            ie = inv[file_id]
1562
            self._convert_file_version(rev, ie, parent_invs)
1563
1564
    def _convert_file_version(self, rev, ie, parent_invs):
1565
        """Convert one version of one file.
1566
1567
        The file needs to be added into the weave if it is a merge
1568
        of >=2 parents or if it's changed from its parent.
1569
        """
1570
        if ie.kind == 'root_directory':
1571
            return
1572
        file_id = ie.file_id
1573
        rev_id = rev.revision_id
1574
        w = self.text_weaves.get(file_id)
1575
        if w is None:
1576
            w = Weave(file_id)
1577
            self.text_weaves[file_id] = w
1578
        text_changed = False
1596.2.20 by Robert Collins
optimise commit to only access weaves for merged, or altered files during commit.
1579
        previous_entries = ie.find_previous_heads(parent_invs,
1580
                                                  None,
1581
                                                  None,
1582
                                                  entry_vf=w)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1583
        for old_revision in previous_entries:
1584
                # if this fails, its a ghost ?
1585
                assert old_revision in self.converted_revs 
1586
        self.snapshot_ie(previous_entries, ie, w, rev_id)
1587
        del ie.text_id
1588
        assert getattr(ie, 'revision', None) is not None
1589
1590
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
1591
        # TODO: convert this logic, which is ~= snapshot to
1592
        # a call to:. This needs the path figured out. rather than a work_tree
1593
        # a v4 revision_tree can be given, or something that looks enough like
1594
        # one to give the file content to the entry if it needs it.
1595
        # and we need something that looks like a weave store for snapshot to 
1596
        # save against.
1597
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
1598
        if len(previous_revisions) == 1:
1599
            previous_ie = previous_revisions.values()[0]
1600
            if ie._unchanged(previous_ie):
1601
                ie.revision = previous_ie.revision
1602
                return
1603
        if ie.has_text():
1604
            text = self.branch.repository.text_store.get(ie.text_id)
1605
            file_lines = text.readlines()
1606
            assert sha_strings(file_lines) == ie.text_sha1
1607
            assert sum(map(len, file_lines)) == ie.text_size
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
1608
            w.add_lines(rev_id, previous_revisions, file_lines)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1609
            self.text_count += 1
1610
        else:
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
1611
            w.add_lines(rev_id, previous_revisions, [])
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1612
        ie.revision = rev_id
1613
1614
    def _make_order(self):
1615
        """Return a suitable order for importing revisions.
1616
1617
        The order must be such that an revision is imported after all
1618
        its (present) parents.
1619
        """
1620
        todo = set(self.revisions.keys())
1621
        done = self.absent_revisions.copy()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1622
        order = []
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1623
        while todo:
1624
            # scan through looking for a revision whose parents
1625
            # are all done
1626
            for rev_id in sorted(list(todo)):
1627
                rev = self.revisions[rev_id]
1628
                parent_ids = set(rev.parent_ids)
1629
                if parent_ids.issubset(done):
1630
                    # can take this one now
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1631
                    order.append(rev_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1632
                    todo.remove(rev_id)
1633
                    done.add(rev_id)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1634
        return order
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1635
1636
1637
class ConvertBzrDir5To6(Converter):
1638
    """Converts format 5 bzr dirs to format 6."""
1639
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1640
    def convert(self, to_convert, pb):
1641
        """See Converter.convert()."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1642
        self.bzrdir = to_convert
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1643
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1644
        self.pb.note('starting upgrade from format 5 to 6')
1645
        self._convert_to_prefixed()
1646
        return BzrDir.open(self.bzrdir.root_transport.base)
1647
1648
    def _convert_to_prefixed(self):
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1649
        from bzrlib.store import TransportStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1650
        self.bzrdir.transport.delete('branch-format')
1651
        for store_name in ["weaves", "revision-store"]:
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1652
            self.pb.note("adding prefixes to %s" % store_name)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1653
            store_transport = self.bzrdir.transport.clone(store_name)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1654
            store = TransportStore(store_transport, prefixed=True)
1608.1.1 by Martin Pool
[patch] LocalTransport.list_dir should return url-quoted strings (ddaa)
1655
            for urlfilename in store_transport.list_dir('.'):
1656
                filename = urlunescape(urlfilename)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1657
                if (filename.endswith(".weave") or
1658
                    filename.endswith(".gz") or
1659
                    filename.endswith(".sig")):
1660
                    file_id = os.path.splitext(filename)[0]
1661
                else:
1662
                    file_id = filename
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1663
                prefix_dir = store.hash_prefix(file_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1664
                # FIXME keep track of the dirs made RBC 20060121
1665
                try:
1666
                    store_transport.move(filename, prefix_dir + '/' + filename)
1667
                except errors.NoSuchFile: # catches missing dirs strangely enough
1668
                    store_transport.mkdir(prefix_dir)
1669
                    store_transport.move(filename, prefix_dir + '/' + filename)
1670
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
1671
1672
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1673
class ConvertBzrDir6ToMeta(Converter):
1674
    """Converts format 6 bzr dirs to metadirs."""
1675
1676
    def convert(self, to_convert, pb):
1677
        """See Converter.convert()."""
1678
        self.bzrdir = to_convert
1679
        self.pb = pb
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1680
        self.count = 0
1681
        self.total = 20 # the steps we know about
1682
        self.garbage_inventories = []
1683
1534.5.13 by Robert Collins
Correct buggy test.
1684
        self.pb.note('starting upgrade from format 6 to metadir')
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1685
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
1686
        # its faster to move specific files around than to open and use the apis...
1687
        # first off, nuke ancestry.weave, it was never used.
1688
        try:
1689
            self.step('Removing ancestry.weave')
1690
            self.bzrdir.transport.delete('ancestry.weave')
1691
        except errors.NoSuchFile:
1692
            pass
1693
        # find out whats there
1694
        self.step('Finding branch files')
1666.1.3 by Robert Collins
Fix and test upgrades from bzrdir 6 over SFTP.
1695
        last_revision = self.bzrdir.open_branch().last_revision()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1696
        bzrcontents = self.bzrdir.transport.list_dir('.')
1697
        for name in bzrcontents:
1698
            if name.startswith('basis-inventory.'):
1699
                self.garbage_inventories.append(name)
1700
        # create new directories for repository, working tree and branch
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1701
        self.dir_mode = self.bzrdir._control_files._dir_mode
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1702
        self.file_mode = self.bzrdir._control_files._file_mode
1703
        repository_names = [('inventory.weave', True),
1704
                            ('revision-store', True),
1705
                            ('weaves', True)]
1706
        self.step('Upgrading repository  ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1707
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1708
        self.make_lock('repository')
1709
        # we hard code the formats here because we are converting into
1710
        # the meta format. The meta format upgrader can take this to a 
1711
        # future format within each component.
1712
        self.put_format('repository', bzrlib.repository.RepositoryFormat7())
1713
        for entry in repository_names:
1714
            self.move_entry('repository', entry)
1715
1716
        self.step('Upgrading branch      ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1717
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1718
        self.make_lock('branch')
1719
        self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
1720
        branch_files = [('revision-history', True),
1721
                        ('branch-name', True),
1722
                        ('parent', False)]
1723
        for entry in branch_files:
1724
            self.move_entry('branch', entry)
1725
1726
        self.step('Upgrading working tree')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1727
        self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1728
        self.make_lock('checkout')
1729
        self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
1730
        self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
1731
        checkout_files = [('pending-merges', True),
1732
                          ('inventory', True),
1733
                          ('stat-cache', False)]
1734
        for entry in checkout_files:
1735
            self.move_entry('checkout', entry)
1534.5.14 by Robert Collins
Bugfix upgrades to metadir to set the last-revision correctly.
1736
        if last_revision is not None:
1737
            self.bzrdir._control_files.put_utf8('checkout/last-revision',
1738
                                                last_revision)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1739
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1740
        return BzrDir.open(self.bzrdir.root_transport.base)
1741
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1742
    def make_lock(self, name):
1743
        """Make a lock for the new control dir name."""
1744
        self.step('Make %s lock' % name)
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1745
        ld = LockDir(self.bzrdir.transport, 
1746
                     '%s/lock' % name,
1747
                     file_modebits=self.file_mode,
1748
                     dir_modebits=self.dir_mode)
1749
        ld.create()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1750
1751
    def move_entry(self, new_dir, entry):
1752
        """Move then entry name into new_dir."""
1753
        name = entry[0]
1754
        mandatory = entry[1]
1755
        self.step('Moving %s' % name)
1756
        try:
1757
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
1758
        except errors.NoSuchFile:
1759
            if mandatory:
1760
                raise
1761
1762
    def put_format(self, dirname, format):
1763
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
1764
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.
1765
1766
class ConvertMetaToMeta(Converter):
1767
    """Converts the components of metadirs."""
1768
1769
    def __init__(self, target_format):
1770
        """Create a metadir to metadir converter.
1771
1772
        :param target_format: The final metadir format that is desired.
1773
        """
1774
        self.target_format = target_format
1775
1776
    def convert(self, to_convert, pb):
1777
        """See Converter.convert()."""
1778
        self.bzrdir = to_convert
1779
        self.pb = pb
1780
        self.count = 0
1781
        self.total = 1
1782
        self.step('checking repository format')
1783
        try:
1784
            repo = self.bzrdir.open_repository()
1785
        except errors.NoRepositoryPresent:
1786
            pass
1787
        else:
1788
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
1789
                from bzrlib.repository import CopyConverter
1790
                self.pb.note('starting repository conversion')
1791
                converter = CopyConverter(self.target_format.repository_format)
1792
                converter.convert(repo, pb)
1793
        return to_convert