/brz/remove-bazaar

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