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