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