~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Aaron Bentley
  • Date: 2007-01-12 18:18:41 UTC
  • mto: (2230.3.47 branch6)
  • mto: This revision was merged to the branch mainline in revision 2290.
  • Revision ID: abentley@panoramicfeedback.com-20070112181841-94zoz6cv72clmsft
Get branch6 creation working

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 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
# TODO: remove unittest dependency; put that stuff inside the test suite
 
24
 
 
25
# TODO: The Format probe_transport seems a bit redundant with just trying to
 
26
# open the bzrdir. -- mbp
 
27
#
 
28
# TODO: Can we move specific formats into separate modules to make this file
 
29
# smaller?
 
30
 
 
31
from cStringIO import StringIO
 
32
import os
 
33
import textwrap
 
34
 
 
35
from bzrlib.lazy_import import lazy_import
 
36
lazy_import(globals(), """
 
37
from copy import deepcopy
 
38
from stat import S_ISDIR
 
39
import unittest
 
40
 
 
41
import bzrlib
 
42
from bzrlib import (
 
43
    errors,
 
44
    lockable_files,
 
45
    lockdir,
 
46
    registry,
 
47
    revision as _mod_revision,
 
48
    repository as _mod_repository,
 
49
    symbol_versioning,
 
50
    urlutils,
 
51
    xml4,
 
52
    xml5,
 
53
    )
 
54
from bzrlib.osutils import (
 
55
    safe_unicode,
 
56
    sha_strings,
 
57
    sha_string,
 
58
    )
 
59
from bzrlib.store.revision.text import TextRevisionStore
 
60
from bzrlib.store.text import TextStore
 
61
from bzrlib.store.versioned import WeaveStore
 
62
from bzrlib.transactions import WriteTransaction
 
63
from bzrlib.transport import get_transport
 
64
from bzrlib.weave import Weave
 
65
""")
 
66
 
 
67
from bzrlib.trace import mutter
 
68
from bzrlib.transport.local import LocalTransport
 
69
 
 
70
 
 
71
class BzrDir(object):
 
72
    """A .bzr control diretory.
 
73
    
 
74
    BzrDir instances let you create or open any of the things that can be
 
75
    found within .bzr - checkouts, branches and repositories.
 
76
    
 
77
    transport
 
78
        the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
 
79
    root_transport
 
80
        a transport connected to the directory this bzr was opened from.
 
81
    """
 
82
 
 
83
    def break_lock(self):
 
84
        """Invoke break_lock on the first object in the bzrdir.
 
85
 
 
86
        If there is a tree, the tree is opened and break_lock() called.
 
87
        Otherwise, branch is tried, and finally repository.
 
88
        """
 
89
        try:
 
90
            thing_to_unlock = self.open_workingtree()
 
91
        except (errors.NotLocalUrl, errors.NoWorkingTree):
 
92
            try:
 
93
                thing_to_unlock = self.open_branch()
 
94
            except errors.NotBranchError:
 
95
                try:
 
96
                    thing_to_unlock = self.open_repository()
 
97
                except errors.NoRepositoryPresent:
 
98
                    return
 
99
        thing_to_unlock.break_lock()
 
100
 
 
101
    def can_convert_format(self):
 
102
        """Return true if this bzrdir is one whose format we can convert from."""
 
103
        return True
 
104
 
 
105
    def check_conversion_target(self, target_format):
 
106
        target_repo_format = target_format.repository_format
 
107
        source_repo_format = self._format.repository_format
 
108
        source_repo_format.check_conversion_target(target_repo_format)
 
109
 
 
110
    @staticmethod
 
111
    def _check_supported(format, allow_unsupported):
 
112
        """Check whether format is a supported format.
 
113
 
 
114
        If allow_unsupported is True, this is a no-op.
 
115
        """
 
116
        if not allow_unsupported and not format.is_supported():
 
117
            # see open_downlevel to open legacy branches.
 
118
            raise errors.UnsupportedFormatError(format=format)
 
119
 
 
120
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
 
121
        """Clone this bzrdir and its contents to url verbatim.
 
122
 
 
123
        If urls last component does not exist, it will be created.
 
124
 
 
125
        if revision_id is not None, then the clone operation may tune
 
126
            itself to download less data.
 
127
        :param force_new_repo: Do not use a shared repository for the target 
 
128
                               even if one is available.
 
129
        """
 
130
        self._make_tail(url)
 
131
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
132
        result = self._format.initialize(url)
 
133
        try:
 
134
            local_repo = self.find_repository()
 
135
        except errors.NoRepositoryPresent:
 
136
            local_repo = None
 
137
        if local_repo:
 
138
            # may need to copy content in
 
139
            if force_new_repo:
 
140
                result_repo = local_repo.clone(
 
141
                    result,
 
142
                    revision_id=revision_id,
 
143
                    basis=basis_repo)
 
144
                result_repo.set_make_working_trees(local_repo.make_working_trees())
 
145
            else:
 
146
                try:
 
147
                    result_repo = result.find_repository()
 
148
                    # fetch content this dir needs.
 
149
                    if basis_repo:
 
150
                        # XXX FIXME RBC 20060214 need tests for this when the basis
 
151
                        # is incomplete
 
152
                        result_repo.fetch(basis_repo, revision_id=revision_id)
 
153
                    result_repo.fetch(local_repo, revision_id=revision_id)
 
154
                except errors.NoRepositoryPresent:
 
155
                    # needed to make one anyway.
 
156
                    result_repo = local_repo.clone(
 
157
                        result,
 
158
                        revision_id=revision_id,
 
159
                        basis=basis_repo)
 
160
                    result_repo.set_make_working_trees(local_repo.make_working_trees())
 
161
        # 1 if there is a branch present
 
162
        #   make sure its content is available in the target repository
 
163
        #   clone it.
 
164
        try:
 
165
            self.open_branch().clone(result, revision_id=revision_id)
 
166
        except errors.NotBranchError:
 
167
            pass
 
168
        try:
 
169
            self.open_workingtree().clone(result, basis=basis_tree)
 
170
        except (errors.NoWorkingTree, errors.NotLocalUrl):
 
171
            pass
 
172
        return result
 
173
 
 
174
    def _get_basis_components(self, basis):
 
175
        """Retrieve the basis components that are available at basis."""
 
176
        if basis is None:
 
177
            return None, None, None
 
178
        try:
 
179
            basis_tree = basis.open_workingtree()
 
180
            basis_branch = basis_tree.branch
 
181
            basis_repo = basis_branch.repository
 
182
        except (errors.NoWorkingTree, errors.NotLocalUrl):
 
183
            basis_tree = None
 
184
            try:
 
185
                basis_branch = basis.open_branch()
 
186
                basis_repo = basis_branch.repository
 
187
            except errors.NotBranchError:
 
188
                basis_branch = None
 
189
                try:
 
190
                    basis_repo = basis.open_repository()
 
191
                except errors.NoRepositoryPresent:
 
192
                    basis_repo = None
 
193
        return basis_repo, basis_branch, basis_tree
 
194
 
 
195
    # TODO: This should be given a Transport, and should chdir up; otherwise
 
196
    # this will open a new connection.
 
197
    def _make_tail(self, url):
 
198
        head, tail = urlutils.split(url)
 
199
        if tail and tail != '.':
 
200
            t = get_transport(head)
 
201
            try:
 
202
                t.mkdir(tail)
 
203
            except errors.FileExists:
 
204
                pass
 
205
 
 
206
    # TODO: Should take a Transport
 
207
    @classmethod
 
208
    def create(cls, base, format=None):
 
209
        """Create a new BzrDir at the url 'base'.
 
210
        
 
211
        This will call the current default formats initialize with base
 
212
        as the only parameter.
 
213
 
 
214
        :param format: If supplied, the format of branch to create.  If not
 
215
            supplied, the default is used.
 
216
        """
 
217
        if cls is not BzrDir:
 
218
            raise AssertionError("BzrDir.create always creates the default"
 
219
                " format, not one of %r" % cls)
 
220
        head, tail = urlutils.split(base)
 
221
        if tail and tail != '.':
 
222
            t = get_transport(head)
 
223
            try:
 
224
                t.mkdir(tail)
 
225
            except errors.FileExists:
 
226
                pass
 
227
        if format is None:
 
228
            format = BzrDirFormat.get_default_format()
 
229
        return format.initialize(safe_unicode(base))
 
230
 
 
231
    def create_branch(self):
 
232
        """Create a branch in this BzrDir.
 
233
 
 
234
        The bzrdirs format will control what branch format is created.
 
235
        For more control see BranchFormatXX.create(a_bzrdir).
 
236
        """
 
237
        raise NotImplementedError(self.create_branch)
 
238
 
 
239
    @staticmethod
 
240
    def create_branch_and_repo(base, force_new_repo=False, format=None):
 
241
        """Create a new BzrDir, Branch and Repository at the url 'base'.
 
242
 
 
243
        This will use the current default BzrDirFormat, and use whatever 
 
244
        repository format that that uses via bzrdir.create_branch and
 
245
        create_repository. If a shared repository is available that is used
 
246
        preferentially.
 
247
 
 
248
        The created Branch object is returned.
 
249
 
 
250
        :param base: The URL to create the branch at.
 
251
        :param force_new_repo: If True a new repository is always created.
 
252
        """
 
253
        bzrdir = BzrDir.create(base, format)
 
254
        bzrdir._find_or_create_repository(force_new_repo)
 
255
        return bzrdir.create_branch()
 
256
 
 
257
    def _find_or_create_repository(self, force_new_repo):
 
258
        """Create a new repository if needed, returning the repository."""
 
259
        if force_new_repo:
 
260
            return self.create_repository()
 
261
        try:
 
262
            return self.find_repository()
 
263
        except errors.NoRepositoryPresent:
 
264
            return self.create_repository()
 
265
        
 
266
    @staticmethod
 
267
    def create_branch_convenience(base, force_new_repo=False,
 
268
                                  force_new_tree=None, format=None):
 
269
        """Create a new BzrDir, Branch and Repository at the url 'base'.
 
270
 
 
271
        This is a convenience function - it will use an existing repository
 
272
        if possible, can be told explicitly whether to create a working tree or
 
273
        not.
 
274
 
 
275
        This will use the current default BzrDirFormat, and use whatever 
 
276
        repository format that that uses via bzrdir.create_branch and
 
277
        create_repository. If a shared repository is available that is used
 
278
        preferentially. Whatever repository is used, its tree creation policy
 
279
        is followed.
 
280
 
 
281
        The created Branch object is returned.
 
282
        If a working tree cannot be made due to base not being a file:// url,
 
283
        no error is raised unless force_new_tree is True, in which case no 
 
284
        data is created on disk and NotLocalUrl is raised.
 
285
 
 
286
        :param base: The URL to create the branch at.
 
287
        :param force_new_repo: If True a new repository is always created.
 
288
        :param force_new_tree: If True or False force creation of a tree or 
 
289
                               prevent such creation respectively.
 
290
        :param format: Override for the for the bzrdir format to create
 
291
        """
 
292
        if force_new_tree:
 
293
            # check for non local urls
 
294
            t = get_transport(safe_unicode(base))
 
295
            if not isinstance(t, LocalTransport):
 
296
                raise errors.NotLocalUrl(base)
 
297
        bzrdir = BzrDir.create(base, format)
 
298
        repo = bzrdir._find_or_create_repository(force_new_repo)
 
299
        result = bzrdir.create_branch()
 
300
        if force_new_tree or (repo.make_working_trees() and 
 
301
                              force_new_tree is None):
 
302
            try:
 
303
                bzrdir.create_workingtree()
 
304
            except errors.NotLocalUrl:
 
305
                pass
 
306
        return result
 
307
        
 
308
    @staticmethod
 
309
    def create_repository(base, shared=False, format=None):
 
310
        """Create a new BzrDir and Repository at the url 'base'.
 
311
 
 
312
        If no format is supplied, this will default to the current default
 
313
        BzrDirFormat by default, and use whatever repository format that that
 
314
        uses for bzrdirformat.create_repository.
 
315
 
 
316
        :param shared: Create a shared repository rather than a standalone
 
317
                       repository.
 
318
        The Repository object is returned.
 
319
 
 
320
        This must be overridden as an instance method in child classes, where
 
321
        it should take no parameters and construct whatever repository format
 
322
        that child class desires.
 
323
        """
 
324
        bzrdir = BzrDir.create(base, format)
 
325
        return bzrdir.create_repository(shared)
 
326
 
 
327
    @staticmethod
 
328
    def create_standalone_workingtree(base, format=None):
 
329
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
 
330
 
 
331
        'base' must be a local path or a file:// url.
 
332
 
 
333
        This will use the current default BzrDirFormat, and use whatever 
 
334
        repository format that that uses for bzrdirformat.create_workingtree,
 
335
        create_branch and create_repository.
 
336
 
 
337
        :return: The WorkingTree object.
 
338
        """
 
339
        t = get_transport(safe_unicode(base))
 
340
        if not isinstance(t, LocalTransport):
 
341
            raise errors.NotLocalUrl(base)
 
342
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
 
343
                                               force_new_repo=True,
 
344
                                               format=format).bzrdir
 
345
        return bzrdir.create_workingtree()
 
346
 
 
347
    def create_workingtree(self, revision_id=None):
 
348
        """Create a working tree at this BzrDir.
 
349
        
 
350
        revision_id: create it as of this revision id.
 
351
        """
 
352
        raise NotImplementedError(self.create_workingtree)
 
353
 
 
354
    def destroy_workingtree(self):
 
355
        """Destroy the working tree at this BzrDir.
 
356
 
 
357
        Formats that do not support this may raise UnsupportedOperation.
 
358
        """
 
359
        raise NotImplementedError(self.destroy_workingtree)
 
360
 
 
361
    def destroy_workingtree_metadata(self):
 
362
        """Destroy the control files for the working tree at this BzrDir.
 
363
 
 
364
        The contents of working tree files are not affected.
 
365
        Formats that do not support this may raise UnsupportedOperation.
 
366
        """
 
367
        raise NotImplementedError(self.destroy_workingtree_metadata)
 
368
 
 
369
    def find_repository(self):
 
370
        """Find the repository that should be used for a_bzrdir.
 
371
 
 
372
        This does not require a branch as we use it to find the repo for
 
373
        new branches as well as to hook existing branches up to their
 
374
        repository.
 
375
        """
 
376
        try:
 
377
            return self.open_repository()
 
378
        except errors.NoRepositoryPresent:
 
379
            pass
 
380
        next_transport = self.root_transport.clone('..')
 
381
        while True:
 
382
            # find the next containing bzrdir
 
383
            try:
 
384
                found_bzrdir = BzrDir.open_containing_from_transport(
 
385
                    next_transport)[0]
 
386
            except errors.NotBranchError:
 
387
                # none found
 
388
                raise errors.NoRepositoryPresent(self)
 
389
            # does it have a repository ?
 
390
            try:
 
391
                repository = found_bzrdir.open_repository()
 
392
            except errors.NoRepositoryPresent:
 
393
                next_transport = found_bzrdir.root_transport.clone('..')
 
394
                if (found_bzrdir.root_transport.base == next_transport.base):
 
395
                    # top of the file system
 
396
                    break
 
397
                else:
 
398
                    continue
 
399
            if ((found_bzrdir.root_transport.base == 
 
400
                 self.root_transport.base) or repository.is_shared()):
 
401
                return repository
 
402
            else:
 
403
                raise errors.NoRepositoryPresent(self)
 
404
        raise errors.NoRepositoryPresent(self)
 
405
 
 
406
    def get_branch_transport(self, branch_format):
 
407
        """Get the transport for use by branch format in this BzrDir.
 
408
 
 
409
        Note that bzr dirs that do not support format strings will raise
 
410
        IncompatibleFormat if the branch format they are given has
 
411
        a format string, and vice versa.
 
412
 
 
413
        If branch_format is None, the transport is returned with no 
 
414
        checking. if it is not None, then the returned transport is
 
415
        guaranteed to point to an existing directory ready for use.
 
416
        """
 
417
        raise NotImplementedError(self.get_branch_transport)
 
418
        
 
419
    def get_repository_transport(self, repository_format):
 
420
        """Get the transport for use by repository format in this BzrDir.
 
421
 
 
422
        Note that bzr dirs that do not support format strings will raise
 
423
        IncompatibleFormat if the repository format they are given has
 
424
        a format string, and vice versa.
 
425
 
 
426
        If repository_format is None, the transport is returned with no 
 
427
        checking. if it is not None, then the returned transport is
 
428
        guaranteed to point to an existing directory ready for use.
 
429
        """
 
430
        raise NotImplementedError(self.get_repository_transport)
 
431
        
 
432
    def get_workingtree_transport(self, tree_format):
 
433
        """Get the transport for use by workingtree format in this BzrDir.
 
434
 
 
435
        Note that bzr dirs that do not support format strings will raise
 
436
        IncompatibleFormat if the workingtree format they are given has
 
437
        a format string, and vice versa.
 
438
 
 
439
        If workingtree_format is None, the transport is returned with no 
 
440
        checking. if it is not None, then the returned transport is
 
441
        guaranteed to point to an existing directory ready for use.
 
442
        """
 
443
        raise NotImplementedError(self.get_workingtree_transport)
 
444
        
 
445
    def __init__(self, _transport, _format):
 
446
        """Initialize a Bzr control dir object.
 
447
        
 
448
        Only really common logic should reside here, concrete classes should be
 
449
        made with varying behaviours.
 
450
 
 
451
        :param _format: the format that is creating this BzrDir instance.
 
452
        :param _transport: the transport this dir is based at.
 
453
        """
 
454
        self._format = _format
 
455
        self.transport = _transport.clone('.bzr')
 
456
        self.root_transport = _transport
 
457
 
 
458
    def is_control_filename(self, filename):
 
459
        """True if filename is the name of a path which is reserved for bzrdir's.
 
460
        
 
461
        :param filename: A filename within the root transport of this bzrdir.
 
462
 
 
463
        This is true IF and ONLY IF the filename is part of the namespace reserved
 
464
        for bzr control dirs. Currently this is the '.bzr' directory in the root
 
465
        of the root_transport. it is expected that plugins will need to extend
 
466
        this in the future - for instance to make bzr talk with svn working
 
467
        trees.
 
468
        """
 
469
        # this might be better on the BzrDirFormat class because it refers to 
 
470
        # all the possible bzrdir disk formats. 
 
471
        # This method is tested via the workingtree is_control_filename tests- 
 
472
        # it was extracted from WorkingTree.is_control_filename. If the methods
 
473
        # contract is extended beyond the current trivial  implementation please
 
474
        # add new tests for it to the appropriate place.
 
475
        return filename == '.bzr' or filename.startswith('.bzr/')
 
476
 
 
477
    def needs_format_conversion(self, format=None):
 
478
        """Return true if this bzrdir needs convert_format run on it.
 
479
        
 
480
        For instance, if the repository format is out of date but the 
 
481
        branch and working tree are not, this should return True.
 
482
 
 
483
        :param format: Optional parameter indicating a specific desired
 
484
                       format we plan to arrive at.
 
485
        """
 
486
        raise NotImplementedError(self.needs_format_conversion)
 
487
 
 
488
    @staticmethod
 
489
    def open_unsupported(base):
 
490
        """Open a branch which is not supported."""
 
491
        return BzrDir.open(base, _unsupported=True)
 
492
        
 
493
    @staticmethod
 
494
    def open(base, _unsupported=False):
 
495
        """Open an existing bzrdir, rooted at 'base' (url)
 
496
        
 
497
        _unsupported is a private parameter to the BzrDir class.
 
498
        """
 
499
        t = get_transport(base)
 
500
        return BzrDir.open_from_transport(t, _unsupported=_unsupported)
 
501
 
 
502
    @staticmethod
 
503
    def open_from_transport(transport, _unsupported=False):
 
504
        """Open a bzrdir within a particular directory.
 
505
 
 
506
        :param transport: Transport containing the bzrdir.
 
507
        :param _unsupported: private.
 
508
        """
 
509
        format = BzrDirFormat.find_format(transport)
 
510
        BzrDir._check_supported(format, _unsupported)
 
511
        return format.open(transport, _found=True)
 
512
 
 
513
    def open_branch(self, unsupported=False):
 
514
        """Open the branch object at this BzrDir if one is present.
 
515
 
 
516
        If unsupported is True, then no longer supported branch formats can
 
517
        still be opened.
 
518
        
 
519
        TODO: static convenience version of this?
 
520
        """
 
521
        raise NotImplementedError(self.open_branch)
 
522
 
 
523
    @staticmethod
 
524
    def open_containing(url):
 
525
        """Open an existing branch which contains url.
 
526
        
 
527
        :param url: url to search from.
 
528
        See open_containing_from_transport for more detail.
 
529
        """
 
530
        return BzrDir.open_containing_from_transport(get_transport(url))
 
531
    
 
532
    @staticmethod
 
533
    def open_containing_from_transport(a_transport):
 
534
        """Open an existing branch which contains a_transport.base
 
535
 
 
536
        This probes for a branch at a_transport, and searches upwards from there.
 
537
 
 
538
        Basically we keep looking up until we find the control directory or
 
539
        run into the root.  If there isn't one, raises NotBranchError.
 
540
        If there is one and it is either an unrecognised format or an unsupported 
 
541
        format, UnknownFormatError or UnsupportedFormatError are raised.
 
542
        If there is one, it is returned, along with the unused portion of url.
 
543
 
 
544
        :return: The BzrDir that contains the path, and a Unicode path 
 
545
                for the rest of the URL.
 
546
        """
 
547
        # this gets the normalised url back. I.e. '.' -> the full path.
 
548
        url = a_transport.base
 
549
        while True:
 
550
            try:
 
551
                result = BzrDir.open_from_transport(a_transport)
 
552
                return result, urlutils.unescape(a_transport.relpath(url))
 
553
            except errors.NotBranchError, e:
 
554
                pass
 
555
            new_t = a_transport.clone('..')
 
556
            if new_t.base == a_transport.base:
 
557
                # reached the root, whatever that may be
 
558
                raise errors.NotBranchError(path=url)
 
559
            a_transport = new_t
 
560
 
 
561
    def open_repository(self, _unsupported=False):
 
562
        """Open the repository object at this BzrDir if one is present.
 
563
 
 
564
        This will not follow the Branch object pointer - its strictly a direct
 
565
        open facility. Most client code should use open_branch().repository to
 
566
        get at a repository.
 
567
 
 
568
        _unsupported is a private parameter, not part of the api.
 
569
        TODO: static convenience version of this?
 
570
        """
 
571
        raise NotImplementedError(self.open_repository)
 
572
 
 
573
    def open_workingtree(self, _unsupported=False):
 
574
        """Open the workingtree object at this BzrDir if one is present.
 
575
        
 
576
        TODO: static convenience version of this?
 
577
        """
 
578
        raise NotImplementedError(self.open_workingtree)
 
579
 
 
580
    def has_branch(self):
 
581
        """Tell if this bzrdir contains a branch.
 
582
        
 
583
        Note: if you're going to open the branch, you should just go ahead
 
584
        and try, and not ask permission first.  (This method just opens the 
 
585
        branch and discards it, and that's somewhat expensive.) 
 
586
        """
 
587
        try:
 
588
            self.open_branch()
 
589
            return True
 
590
        except errors.NotBranchError:
 
591
            return False
 
592
 
 
593
    def has_workingtree(self):
 
594
        """Tell if this bzrdir contains a working tree.
 
595
 
 
596
        This will still raise an exception if the bzrdir has a workingtree that
 
597
        is remote & inaccessible.
 
598
        
 
599
        Note: if you're going to open the working tree, you should just go ahead
 
600
        and try, and not ask permission first.  (This method just opens the 
 
601
        workingtree and discards it, and that's somewhat expensive.) 
 
602
        """
 
603
        try:
 
604
            self.open_workingtree()
 
605
            return True
 
606
        except errors.NoWorkingTree:
 
607
            return False
 
608
 
 
609
    def cloning_metadir(self, basis=None):
 
610
        """Produce a metadir suitable for cloning with"""
 
611
        def related_repository(bzrdir):
 
612
            try:
 
613
                branch = bzrdir.open_branch()
 
614
                return branch.repository
 
615
            except errors.NotBranchError:
 
616
                source_branch = None
 
617
                return bzrdir.open_repository()
 
618
        result_format = self._format.__class__()
 
619
        try:
 
620
            try:
 
621
                source_repository = related_repository(self)
 
622
            except errors.NoRepositoryPresent:
 
623
                if basis is None:
 
624
                    raise
 
625
                source_repository = related_repository(self)
 
626
            result_format.repository_format = source_repository._format
 
627
        except errors.NoRepositoryPresent:
 
628
            pass
 
629
        return result_format
 
630
 
 
631
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
 
632
        """Create a copy of this bzrdir prepared for use as a new line of
 
633
        development.
 
634
 
 
635
        If urls last component does not exist, it will be created.
 
636
 
 
637
        Attributes related to the identity of the source branch like
 
638
        branch nickname will be cleaned, a working tree is created
 
639
        whether one existed before or not; and a local branch is always
 
640
        created.
 
641
 
 
642
        if revision_id is not None, then the clone operation may tune
 
643
            itself to download less data.
 
644
        """
 
645
        self._make_tail(url)
 
646
        cloning_format = self.cloning_metadir(basis)
 
647
        result = cloning_format.initialize(url)
 
648
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
649
        try:
 
650
            source_branch = self.open_branch()
 
651
            source_repository = source_branch.repository
 
652
        except errors.NotBranchError:
 
653
            source_branch = None
 
654
            try:
 
655
                source_repository = self.open_repository()
 
656
            except errors.NoRepositoryPresent:
 
657
                # copy the entire basis one if there is one
 
658
                # but there is no repository.
 
659
                source_repository = basis_repo
 
660
        if force_new_repo:
 
661
            result_repo = None
 
662
        else:
 
663
            try:
 
664
                result_repo = result.find_repository()
 
665
            except errors.NoRepositoryPresent:
 
666
                result_repo = None
 
667
        if source_repository is None and result_repo is not None:
 
668
            pass
 
669
        elif source_repository is None and result_repo is None:
 
670
            # no repo available, make a new one
 
671
            result.create_repository()
 
672
        elif source_repository is not None and result_repo is None:
 
673
            # have source, and want to make a new target repo
 
674
            # we don't clone the repo because that preserves attributes
 
675
            # like is_shared(), and we have not yet implemented a 
 
676
            # repository sprout().
 
677
            result_repo = result.create_repository()
 
678
        if result_repo is not None:
 
679
            # fetch needed content into target.
 
680
            if basis_repo:
 
681
                # XXX FIXME RBC 20060214 need tests for this when the basis
 
682
                # is incomplete
 
683
                result_repo.fetch(basis_repo, revision_id=revision_id)
 
684
            if source_repository is not None:
 
685
                result_repo.fetch(source_repository, revision_id=revision_id)
 
686
        if source_branch is not None:
 
687
            source_branch.sprout(result, revision_id=revision_id)
 
688
        else:
 
689
            result.create_branch()
 
690
        # TODO: jam 20060426 we probably need a test in here in the
 
691
        #       case that the newly sprouted branch is a remote one
 
692
        if result_repo is None or result_repo.make_working_trees():
 
693
            wt = result.create_workingtree()
 
694
            if wt.inventory.root is None:
 
695
                try:
 
696
                    wt.set_root_id(self.open_workingtree.get_root_id())
 
697
                except errors.NoWorkingTree:
 
698
                    pass
 
699
        return result
 
700
 
 
701
 
 
702
class BzrDirPreSplitOut(BzrDir):
 
703
    """A common class for the all-in-one formats."""
 
704
 
 
705
    def __init__(self, _transport, _format):
 
706
        """See BzrDir.__init__."""
 
707
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
 
708
        assert self._format._lock_class == lockable_files.TransportLock
 
709
        assert self._format._lock_file_name == 'branch-lock'
 
710
        self._control_files = lockable_files.LockableFiles(
 
711
                                            self.get_branch_transport(None),
 
712
                                            self._format._lock_file_name,
 
713
                                            self._format._lock_class)
 
714
 
 
715
    def break_lock(self):
 
716
        """Pre-splitout bzrdirs do not suffer from stale locks."""
 
717
        raise NotImplementedError(self.break_lock)
 
718
 
 
719
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
 
720
        """See BzrDir.clone()."""
 
721
        from bzrlib.workingtree import WorkingTreeFormat2
 
722
        self._make_tail(url)
 
723
        result = self._format._initialize_for_clone(url)
 
724
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
725
        self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
 
726
        from_branch = self.open_branch()
 
727
        from_branch.clone(result, revision_id=revision_id)
 
728
        try:
 
729
            self.open_workingtree().clone(result, basis=basis_tree)
 
730
        except errors.NotLocalUrl:
 
731
            # make a new one, this format always has to have one.
 
732
            try:
 
733
                WorkingTreeFormat2().initialize(result)
 
734
            except errors.NotLocalUrl:
 
735
                # but we cannot do it for remote trees.
 
736
                to_branch = result.open_branch()
 
737
                WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
 
738
        return result
 
739
 
 
740
    def create_branch(self):
 
741
        """See BzrDir.create_branch."""
 
742
        return self.open_branch()
 
743
 
 
744
    def create_repository(self, shared=False):
 
745
        """See BzrDir.create_repository."""
 
746
        if shared:
 
747
            raise errors.IncompatibleFormat('shared repository', self._format)
 
748
        return self.open_repository()
 
749
 
 
750
    def create_workingtree(self, revision_id=None):
 
751
        """See BzrDir.create_workingtree."""
 
752
        # this looks buggy but is not -really-
 
753
        # clone and sprout will have set the revision_id
 
754
        # and that will have set it for us, its only
 
755
        # specific uses of create_workingtree in isolation
 
756
        # that can do wonky stuff here, and that only
 
757
        # happens for creating checkouts, which cannot be 
 
758
        # done on this format anyway. So - acceptable wart.
 
759
        result = self.open_workingtree()
 
760
        if revision_id is not None:
 
761
            if revision_id == _mod_revision.NULL_REVISION:
 
762
                result.set_parent_ids([])
 
763
            else:
 
764
                result.set_parent_ids([revision_id])
 
765
        return result
 
766
 
 
767
    def destroy_workingtree(self):
 
768
        """See BzrDir.destroy_workingtree."""
 
769
        raise errors.UnsupportedOperation(self.destroy_workingtree, self)
 
770
 
 
771
    def destroy_workingtree_metadata(self):
 
772
        """See BzrDir.destroy_workingtree_metadata."""
 
773
        raise errors.UnsupportedOperation(self.destroy_workingtree_metadata, 
 
774
                                          self)
 
775
 
 
776
    def get_branch_transport(self, branch_format):
 
777
        """See BzrDir.get_branch_transport()."""
 
778
        if branch_format is None:
 
779
            return self.transport
 
780
        try:
 
781
            branch_format.get_format_string()
 
782
        except NotImplementedError:
 
783
            return self.transport
 
784
        raise errors.IncompatibleFormat(branch_format, self._format)
 
785
 
 
786
    def get_repository_transport(self, repository_format):
 
787
        """See BzrDir.get_repository_transport()."""
 
788
        if repository_format is None:
 
789
            return self.transport
 
790
        try:
 
791
            repository_format.get_format_string()
 
792
        except NotImplementedError:
 
793
            return self.transport
 
794
        raise errors.IncompatibleFormat(repository_format, self._format)
 
795
 
 
796
    def get_workingtree_transport(self, workingtree_format):
 
797
        """See BzrDir.get_workingtree_transport()."""
 
798
        if workingtree_format is None:
 
799
            return self.transport
 
800
        try:
 
801
            workingtree_format.get_format_string()
 
802
        except NotImplementedError:
 
803
            return self.transport
 
804
        raise errors.IncompatibleFormat(workingtree_format, self._format)
 
805
 
 
806
    def needs_format_conversion(self, format=None):
 
807
        """See BzrDir.needs_format_conversion()."""
 
808
        # if the format is not the same as the system default,
 
809
        # an upgrade is needed.
 
810
        if format is None:
 
811
            format = BzrDirFormat.get_default_format()
 
812
        return not isinstance(self._format, format.__class__)
 
813
 
 
814
    def open_branch(self, unsupported=False):
 
815
        """See BzrDir.open_branch."""
 
816
        from bzrlib.branch import BzrBranchFormat4
 
817
        format = BzrBranchFormat4()
 
818
        self._check_supported(format, unsupported)
 
819
        return format.open(self, _found=True)
 
820
 
 
821
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
 
822
        """See BzrDir.sprout()."""
 
823
        from bzrlib.workingtree import WorkingTreeFormat2
 
824
        self._make_tail(url)
 
825
        result = self._format._initialize_for_clone(url)
 
826
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
827
        try:
 
828
            self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
 
829
        except errors.NoRepositoryPresent:
 
830
            pass
 
831
        try:
 
832
            self.open_branch().sprout(result, revision_id=revision_id)
 
833
        except errors.NotBranchError:
 
834
            pass
 
835
        # we always want a working tree
 
836
        WorkingTreeFormat2().initialize(result)
 
837
        return result
 
838
 
 
839
 
 
840
class BzrDir4(BzrDirPreSplitOut):
 
841
    """A .bzr version 4 control object.
 
842
    
 
843
    This is a deprecated format and may be removed after sept 2006.
 
844
    """
 
845
 
 
846
    def create_repository(self, shared=False):
 
847
        """See BzrDir.create_repository."""
 
848
        return self._format.repository_format.initialize(self, shared)
 
849
 
 
850
    def needs_format_conversion(self, format=None):
 
851
        """Format 4 dirs are always in need of conversion."""
 
852
        return True
 
853
 
 
854
    def open_repository(self):
 
855
        """See BzrDir.open_repository."""
 
856
        from bzrlib.repository import RepositoryFormat4
 
857
        return RepositoryFormat4().open(self, _found=True)
 
858
 
 
859
 
 
860
class BzrDir5(BzrDirPreSplitOut):
 
861
    """A .bzr version 5 control object.
 
862
 
 
863
    This is a deprecated format and may be removed after sept 2006.
 
864
    """
 
865
 
 
866
    def open_repository(self):
 
867
        """See BzrDir.open_repository."""
 
868
        from bzrlib.repository import RepositoryFormat5
 
869
        return RepositoryFormat5().open(self, _found=True)
 
870
 
 
871
    def open_workingtree(self, _unsupported=False):
 
872
        """See BzrDir.create_workingtree."""
 
873
        from bzrlib.workingtree import WorkingTreeFormat2
 
874
        return WorkingTreeFormat2().open(self, _found=True)
 
875
 
 
876
 
 
877
class BzrDir6(BzrDirPreSplitOut):
 
878
    """A .bzr version 6 control object.
 
879
 
 
880
    This is a deprecated format and may be removed after sept 2006.
 
881
    """
 
882
 
 
883
    def open_repository(self):
 
884
        """See BzrDir.open_repository."""
 
885
        from bzrlib.repository import RepositoryFormat6
 
886
        return RepositoryFormat6().open(self, _found=True)
 
887
 
 
888
    def open_workingtree(self, _unsupported=False):
 
889
        """See BzrDir.create_workingtree."""
 
890
        from bzrlib.workingtree import WorkingTreeFormat2
 
891
        return WorkingTreeFormat2().open(self, _found=True)
 
892
 
 
893
 
 
894
class BzrDirMeta1(BzrDir):
 
895
    """A .bzr meta version 1 control object.
 
896
    
 
897
    This is the first control object where the 
 
898
    individual aspects are really split out: there are separate repository,
 
899
    workingtree and branch subdirectories and any subset of the three can be
 
900
    present within a BzrDir.
 
901
    """
 
902
 
 
903
    def can_convert_format(self):
 
904
        """See BzrDir.can_convert_format()."""
 
905
        return True
 
906
 
 
907
    def create_branch(self):
 
908
        """See BzrDir.create_branch."""
 
909
        return self._format.branch_format.initialize(self)
 
910
 
 
911
    def create_repository(self, shared=False):
 
912
        """See BzrDir.create_repository."""
 
913
        return self._format.repository_format.initialize(self, shared)
 
914
 
 
915
    def create_workingtree(self, revision_id=None):
 
916
        """See BzrDir.create_workingtree."""
 
917
        from bzrlib.workingtree import WorkingTreeFormat
 
918
        return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
 
919
 
 
920
    def destroy_workingtree(self):
 
921
        """See BzrDir.destroy_workingtree."""
 
922
        wt = self.open_workingtree()
 
923
        repository = wt.branch.repository
 
924
        empty = repository.revision_tree(_mod_revision.NULL_REVISION)
 
925
        wt.revert([], old_tree=empty)
 
926
        self.destroy_workingtree_metadata()
 
927
 
 
928
    def destroy_workingtree_metadata(self):
 
929
        self.transport.delete_tree('checkout')
 
930
 
 
931
    def _get_mkdir_mode(self):
 
932
        """Figure out the mode to use when creating a bzrdir subdir."""
 
933
        temp_control = lockable_files.LockableFiles(self.transport, '',
 
934
                                     lockable_files.TransportLock)
 
935
        return temp_control._dir_mode
 
936
 
 
937
    def get_branch_transport(self, branch_format):
 
938
        """See BzrDir.get_branch_transport()."""
 
939
        if branch_format is None:
 
940
            return self.transport.clone('branch')
 
941
        try:
 
942
            branch_format.get_format_string()
 
943
        except NotImplementedError:
 
944
            raise errors.IncompatibleFormat(branch_format, self._format)
 
945
        try:
 
946
            self.transport.mkdir('branch', mode=self._get_mkdir_mode())
 
947
        except errors.FileExists:
 
948
            pass
 
949
        return self.transport.clone('branch')
 
950
 
 
951
    def get_repository_transport(self, repository_format):
 
952
        """See BzrDir.get_repository_transport()."""
 
953
        if repository_format is None:
 
954
            return self.transport.clone('repository')
 
955
        try:
 
956
            repository_format.get_format_string()
 
957
        except NotImplementedError:
 
958
            raise errors.IncompatibleFormat(repository_format, self._format)
 
959
        try:
 
960
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
 
961
        except errors.FileExists:
 
962
            pass
 
963
        return self.transport.clone('repository')
 
964
 
 
965
    def get_workingtree_transport(self, workingtree_format):
 
966
        """See BzrDir.get_workingtree_transport()."""
 
967
        if workingtree_format is None:
 
968
            return self.transport.clone('checkout')
 
969
        try:
 
970
            workingtree_format.get_format_string()
 
971
        except NotImplementedError:
 
972
            raise errors.IncompatibleFormat(workingtree_format, self._format)
 
973
        try:
 
974
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
 
975
        except errors.FileExists:
 
976
            pass
 
977
        return self.transport.clone('checkout')
 
978
 
 
979
    def needs_format_conversion(self, format=None):
 
980
        """See BzrDir.needs_format_conversion()."""
 
981
        if format is None:
 
982
            format = BzrDirFormat.get_default_format()
 
983
        if not isinstance(self._format, format.__class__):
 
984
            # it is not a meta dir format, conversion is needed.
 
985
            return True
 
986
        # we might want to push this down to the repository?
 
987
        try:
 
988
            if not isinstance(self.open_repository()._format,
 
989
                              format.repository_format.__class__):
 
990
                # the repository needs an upgrade.
 
991
                return True
 
992
        except errors.NoRepositoryPresent:
 
993
            pass
 
994
        # currently there are no other possible conversions for meta1 formats.
 
995
        return False
 
996
 
 
997
    def open_branch(self, unsupported=False):
 
998
        """See BzrDir.open_branch."""
 
999
        from bzrlib.branch import BranchFormat
 
1000
        format = BranchFormat.find_format(self)
 
1001
        self._check_supported(format, unsupported)
 
1002
        return format.open(self, _found=True)
 
1003
 
 
1004
    def open_repository(self, unsupported=False):
 
1005
        """See BzrDir.open_repository."""
 
1006
        from bzrlib.repository import RepositoryFormat
 
1007
        format = RepositoryFormat.find_format(self)
 
1008
        self._check_supported(format, unsupported)
 
1009
        return format.open(self, _found=True)
 
1010
 
 
1011
    def open_workingtree(self, unsupported=False):
 
1012
        """See BzrDir.open_workingtree."""
 
1013
        from bzrlib.workingtree import WorkingTreeFormat
 
1014
        format = WorkingTreeFormat.find_format(self)
 
1015
        self._check_supported(format, unsupported)
 
1016
        return format.open(self, _found=True)
 
1017
 
 
1018
 
 
1019
class BzrDirFormat(object):
 
1020
    """An encapsulation of the initialization and open routines for a format.
 
1021
 
 
1022
    Formats provide three things:
 
1023
     * An initialization routine,
 
1024
     * a format string,
 
1025
     * an open routine.
 
1026
 
 
1027
    Formats are placed in an dict by their format string for reference 
 
1028
    during bzrdir opening. These should be subclasses of BzrDirFormat
 
1029
    for consistency.
 
1030
 
 
1031
    Once a format is deprecated, just deprecate the initialize and open
 
1032
    methods on the format class. Do not deprecate the object, as the 
 
1033
    object will be created every system load.
 
1034
    """
 
1035
 
 
1036
    _default_format = None
 
1037
    """The default format used for new .bzr dirs."""
 
1038
 
 
1039
    _formats = {}
 
1040
    """The known formats."""
 
1041
 
 
1042
    _control_formats = []
 
1043
    """The registered control formats - .bzr, ....
 
1044
    
 
1045
    This is a list of BzrDirFormat objects.
 
1046
    """
 
1047
 
 
1048
    _lock_file_name = 'branch-lock'
 
1049
 
 
1050
    # _lock_class must be set in subclasses to the lock type, typ.
 
1051
    # TransportLock or LockDir
 
1052
 
 
1053
    @classmethod
 
1054
    def find_format(klass, transport):
 
1055
        """Return the format present at transport."""
 
1056
        for format in klass._control_formats:
 
1057
            try:
 
1058
                return format.probe_transport(transport)
 
1059
            except errors.NotBranchError:
 
1060
                # this format does not find a control dir here.
 
1061
                pass
 
1062
        raise errors.NotBranchError(path=transport.base)
 
1063
 
 
1064
    @classmethod
 
1065
    def probe_transport(klass, transport):
 
1066
        """Return the .bzrdir style transport present at URL."""
 
1067
        try:
 
1068
            format_string = transport.get(".bzr/branch-format").read()
 
1069
        except errors.NoSuchFile:
 
1070
            raise errors.NotBranchError(path=transport.base)
 
1071
 
 
1072
        try:
 
1073
            return klass._formats[format_string]
 
1074
        except KeyError:
 
1075
            raise errors.UnknownFormatError(format=format_string)
 
1076
 
 
1077
    @classmethod
 
1078
    def get_default_format(klass):
 
1079
        """Return the current default format."""
 
1080
        return klass._default_format
 
1081
 
 
1082
    def get_format_string(self):
 
1083
        """Return the ASCII format string that identifies this format."""
 
1084
        raise NotImplementedError(self.get_format_string)
 
1085
 
 
1086
    def get_format_description(self):
 
1087
        """Return the short description for this format."""
 
1088
        raise NotImplementedError(self.get_format_description)
 
1089
 
 
1090
    def get_converter(self, format=None):
 
1091
        """Return the converter to use to convert bzrdirs needing converts.
 
1092
 
 
1093
        This returns a bzrlib.bzrdir.Converter object.
 
1094
 
 
1095
        This should return the best upgrader to step this format towards the
 
1096
        current default format. In the case of plugins we can/should provide
 
1097
        some means for them to extend the range of returnable converters.
 
1098
 
 
1099
        :param format: Optional format to override the default format of the 
 
1100
                       library.
 
1101
        """
 
1102
        raise NotImplementedError(self.get_converter)
 
1103
 
 
1104
    def initialize(self, url):
 
1105
        """Create a bzr control dir at this url and return an opened copy.
 
1106
        
 
1107
        Subclasses should typically override initialize_on_transport
 
1108
        instead of this method.
 
1109
        """
 
1110
        return self.initialize_on_transport(get_transport(url))
 
1111
 
 
1112
    def initialize_on_transport(self, transport):
 
1113
        """Initialize a new bzrdir in the base directory of a Transport."""
 
1114
        # Since we don't have a .bzr directory, inherit the
 
1115
        # mode from the root directory
 
1116
        temp_control = lockable_files.LockableFiles(transport,
 
1117
                            '', lockable_files.TransportLock)
 
1118
        temp_control._transport.mkdir('.bzr',
 
1119
                                      # FIXME: RBC 20060121 don't peek under
 
1120
                                      # the covers
 
1121
                                      mode=temp_control._dir_mode)
 
1122
        file_mode = temp_control._file_mode
 
1123
        del temp_control
 
1124
        mutter('created control directory in ' + transport.base)
 
1125
        control = transport.clone('.bzr')
 
1126
        utf8_files = [('README', 
 
1127
                       "This is a Bazaar-NG control directory.\n"
 
1128
                       "Do not change any files in this directory.\n"),
 
1129
                      ('branch-format', self.get_format_string()),
 
1130
                      ]
 
1131
        # NB: no need to escape relative paths that are url safe.
 
1132
        control_files = lockable_files.LockableFiles(control,
 
1133
                            self._lock_file_name, self._lock_class)
 
1134
        control_files.create_lock()
 
1135
        control_files.lock_write()
 
1136
        try:
 
1137
            for file, content in utf8_files:
 
1138
                control_files.put_utf8(file, content)
 
1139
        finally:
 
1140
            control_files.unlock()
 
1141
        return self.open(transport, _found=True)
 
1142
 
 
1143
    def is_supported(self):
 
1144
        """Is this format supported?
 
1145
 
 
1146
        Supported formats must be initializable and openable.
 
1147
        Unsupported formats may not support initialization or committing or 
 
1148
        some other features depending on the reason for not being supported.
 
1149
        """
 
1150
        return True
 
1151
 
 
1152
    def same_model(self, target_format):
 
1153
        return (self.repository_format.rich_root_data == 
 
1154
            target_format.rich_root_data)
 
1155
 
 
1156
    @classmethod
 
1157
    def known_formats(klass):
 
1158
        """Return all the known formats.
 
1159
        
 
1160
        Concrete formats should override _known_formats.
 
1161
        """
 
1162
        # There is double indirection here to make sure that control 
 
1163
        # formats used by more than one dir format will only be probed 
 
1164
        # once. This can otherwise be quite expensive for remote connections.
 
1165
        result = set()
 
1166
        for format in klass._control_formats:
 
1167
            result.update(format._known_formats())
 
1168
        return result
 
1169
    
 
1170
    @classmethod
 
1171
    def _known_formats(klass):
 
1172
        """Return the known format instances for this control format."""
 
1173
        return set(klass._formats.values())
 
1174
 
 
1175
    def open(self, transport, _found=False):
 
1176
        """Return an instance of this format for the dir transport points at.
 
1177
        
 
1178
        _found is a private parameter, do not use it.
 
1179
        """
 
1180
        if not _found:
 
1181
            found_format = BzrDirFormat.find_format(transport)
 
1182
            if not isinstance(found_format, self.__class__):
 
1183
                raise AssertionError("%s was asked to open %s, but it seems to need "
 
1184
                        "format %s" 
 
1185
                        % (self, transport, found_format))
 
1186
        return self._open(transport)
 
1187
 
 
1188
    def _open(self, transport):
 
1189
        """Template method helper for opening BzrDirectories.
 
1190
 
 
1191
        This performs the actual open and any additional logic or parameter
 
1192
        passing.
 
1193
        """
 
1194
        raise NotImplementedError(self._open)
 
1195
 
 
1196
    @classmethod
 
1197
    def register_format(klass, format):
 
1198
        klass._formats[format.get_format_string()] = format
 
1199
 
 
1200
    @classmethod
 
1201
    def register_control_format(klass, format):
 
1202
        """Register a format that does not use '.bzrdir' for its control dir.
 
1203
 
 
1204
        TODO: This should be pulled up into a 'ControlDirFormat' base class
 
1205
        which BzrDirFormat can inherit from, and renamed to register_format 
 
1206
        there. It has been done without that for now for simplicity of
 
1207
        implementation.
 
1208
        """
 
1209
        klass._control_formats.append(format)
 
1210
 
 
1211
    @classmethod
 
1212
    @symbol_versioning.deprecated_method(symbol_versioning.zero_fourteen)
 
1213
    def set_default_format(klass, format):
 
1214
        klass._set_default_format(format)
 
1215
 
 
1216
    @classmethod
 
1217
    def _set_default_format(klass, format):
 
1218
        """Set default format (for testing behavior of defaults only)"""
 
1219
        klass._default_format = format
 
1220
 
 
1221
    def __str__(self):
 
1222
        return self.get_format_string()[:-1]
 
1223
 
 
1224
    @classmethod
 
1225
    def unregister_format(klass, format):
 
1226
        assert klass._formats[format.get_format_string()] is format
 
1227
        del klass._formats[format.get_format_string()]
 
1228
 
 
1229
    @classmethod
 
1230
    def unregister_control_format(klass, format):
 
1231
        klass._control_formats.remove(format)
 
1232
 
 
1233
 
 
1234
# register BzrDirFormat as a control format
 
1235
BzrDirFormat.register_control_format(BzrDirFormat)
 
1236
 
 
1237
 
 
1238
class BzrDirFormat4(BzrDirFormat):
 
1239
    """Bzr dir format 4.
 
1240
 
 
1241
    This format is a combined format for working tree, branch and repository.
 
1242
    It has:
 
1243
     - Format 1 working trees [always]
 
1244
     - Format 4 branches [always]
 
1245
     - Format 4 repositories [always]
 
1246
 
 
1247
    This format is deprecated: it indexes texts using a text it which is
 
1248
    removed in format 5; write support for this format has been removed.
 
1249
    """
 
1250
 
 
1251
    _lock_class = lockable_files.TransportLock
 
1252
 
 
1253
    def get_format_string(self):
 
1254
        """See BzrDirFormat.get_format_string()."""
 
1255
        return "Bazaar-NG branch, format 0.0.4\n"
 
1256
 
 
1257
    def get_format_description(self):
 
1258
        """See BzrDirFormat.get_format_description()."""
 
1259
        return "All-in-one format 4"
 
1260
 
 
1261
    def get_converter(self, format=None):
 
1262
        """See BzrDirFormat.get_converter()."""
 
1263
        # there is one and only one upgrade path here.
 
1264
        return ConvertBzrDir4To5()
 
1265
        
 
1266
    def initialize_on_transport(self, transport):
 
1267
        """Format 4 branches cannot be created."""
 
1268
        raise errors.UninitializableFormat(self)
 
1269
 
 
1270
    def is_supported(self):
 
1271
        """Format 4 is not supported.
 
1272
 
 
1273
        It is not supported because the model changed from 4 to 5 and the
 
1274
        conversion logic is expensive - so doing it on the fly was not 
 
1275
        feasible.
 
1276
        """
 
1277
        return False
 
1278
 
 
1279
    def _open(self, transport):
 
1280
        """See BzrDirFormat._open."""
 
1281
        return BzrDir4(transport, self)
 
1282
 
 
1283
    def __return_repository_format(self):
 
1284
        """Circular import protection."""
 
1285
        from bzrlib.repository import RepositoryFormat4
 
1286
        return RepositoryFormat4()
 
1287
    repository_format = property(__return_repository_format)
 
1288
 
 
1289
 
 
1290
class BzrDirFormat5(BzrDirFormat):
 
1291
    """Bzr control format 5.
 
1292
 
 
1293
    This format is a combined format for working tree, branch and repository.
 
1294
    It has:
 
1295
     - Format 2 working trees [always] 
 
1296
     - Format 4 branches [always] 
 
1297
     - Format 5 repositories [always]
 
1298
       Unhashed stores in the repository.
 
1299
    """
 
1300
 
 
1301
    _lock_class = lockable_files.TransportLock
 
1302
 
 
1303
    def get_format_string(self):
 
1304
        """See BzrDirFormat.get_format_string()."""
 
1305
        return "Bazaar-NG branch, format 5\n"
 
1306
 
 
1307
    def get_format_description(self):
 
1308
        """See BzrDirFormat.get_format_description()."""
 
1309
        return "All-in-one format 5"
 
1310
 
 
1311
    def get_converter(self, format=None):
 
1312
        """See BzrDirFormat.get_converter()."""
 
1313
        # there is one and only one upgrade path here.
 
1314
        return ConvertBzrDir5To6()
 
1315
 
 
1316
    def _initialize_for_clone(self, url):
 
1317
        return self.initialize_on_transport(get_transport(url), _cloning=True)
 
1318
        
 
1319
    def initialize_on_transport(self, transport, _cloning=False):
 
1320
        """Format 5 dirs always have working tree, branch and repository.
 
1321
        
 
1322
        Except when they are being cloned.
 
1323
        """
 
1324
        from bzrlib.branch import BzrBranchFormat4
 
1325
        from bzrlib.repository import RepositoryFormat5
 
1326
        from bzrlib.workingtree import WorkingTreeFormat2
 
1327
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
 
1328
        RepositoryFormat5().initialize(result, _internal=True)
 
1329
        if not _cloning:
 
1330
            branch = BzrBranchFormat4().initialize(result)
 
1331
            try:
 
1332
                WorkingTreeFormat2().initialize(result)
 
1333
            except errors.NotLocalUrl:
 
1334
                # Even though we can't access the working tree, we need to
 
1335
                # create its control files.
 
1336
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
 
1337
        return result
 
1338
 
 
1339
    def _open(self, transport):
 
1340
        """See BzrDirFormat._open."""
 
1341
        return BzrDir5(transport, self)
 
1342
 
 
1343
    def __return_repository_format(self):
 
1344
        """Circular import protection."""
 
1345
        from bzrlib.repository import RepositoryFormat5
 
1346
        return RepositoryFormat5()
 
1347
    repository_format = property(__return_repository_format)
 
1348
 
 
1349
 
 
1350
class BzrDirFormat6(BzrDirFormat):
 
1351
    """Bzr control format 6.
 
1352
 
 
1353
    This format is a combined format for working tree, branch and repository.
 
1354
    It has:
 
1355
     - Format 2 working trees [always] 
 
1356
     - Format 4 branches [always] 
 
1357
     - Format 6 repositories [always]
 
1358
    """
 
1359
 
 
1360
    _lock_class = lockable_files.TransportLock
 
1361
 
 
1362
    def get_format_string(self):
 
1363
        """See BzrDirFormat.get_format_string()."""
 
1364
        return "Bazaar-NG branch, format 6\n"
 
1365
 
 
1366
    def get_format_description(self):
 
1367
        """See BzrDirFormat.get_format_description()."""
 
1368
        return "All-in-one format 6"
 
1369
 
 
1370
    def get_converter(self, format=None):
 
1371
        """See BzrDirFormat.get_converter()."""
 
1372
        # there is one and only one upgrade path here.
 
1373
        return ConvertBzrDir6ToMeta()
 
1374
        
 
1375
    def _initialize_for_clone(self, url):
 
1376
        return self.initialize_on_transport(get_transport(url), _cloning=True)
 
1377
 
 
1378
    def initialize_on_transport(self, transport, _cloning=False):
 
1379
        """Format 6 dirs always have working tree, branch and repository.
 
1380
        
 
1381
        Except when they are being cloned.
 
1382
        """
 
1383
        from bzrlib.branch import BzrBranchFormat4
 
1384
        from bzrlib.repository import RepositoryFormat6
 
1385
        from bzrlib.workingtree import WorkingTreeFormat2
 
1386
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
 
1387
        RepositoryFormat6().initialize(result, _internal=True)
 
1388
        if not _cloning:
 
1389
            branch = BzrBranchFormat4().initialize(result)
 
1390
            try:
 
1391
                WorkingTreeFormat2().initialize(result)
 
1392
            except errors.NotLocalUrl:
 
1393
                # Even though we can't access the working tree, we need to
 
1394
                # create its control files.
 
1395
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
 
1396
        return result
 
1397
 
 
1398
    def _open(self, transport):
 
1399
        """See BzrDirFormat._open."""
 
1400
        return BzrDir6(transport, self)
 
1401
 
 
1402
    def __return_repository_format(self):
 
1403
        """Circular import protection."""
 
1404
        from bzrlib.repository import RepositoryFormat6
 
1405
        return RepositoryFormat6()
 
1406
    repository_format = property(__return_repository_format)
 
1407
 
 
1408
 
 
1409
class BzrDirMetaFormat1(BzrDirFormat):
 
1410
    """Bzr meta control format 1
 
1411
 
 
1412
    This is the first format with split out working tree, branch and repository
 
1413
    disk storage.
 
1414
    It has:
 
1415
     - Format 3 working trees [optional]
 
1416
     - Format 5 branches [optional]
 
1417
     - Format 7 repositories [optional]
 
1418
    """
 
1419
 
 
1420
    _lock_class = lockdir.LockDir
 
1421
 
 
1422
    def __init__(self):
 
1423
        self._branch_format = None
 
1424
 
 
1425
    def _get_branch_format(self):
 
1426
        if self._branch_format is None:
 
1427
            from bzrlib.branch import BranchFormat
 
1428
            self._branch_format = BranchFormat.get_default_format()
 
1429
        return self._branch_format
 
1430
 
 
1431
    def _set_branch_format(self, format):
 
1432
        self._branch_format = format
 
1433
 
 
1434
    branch_format = property(_get_branch_format, _set_branch_format)
 
1435
 
 
1436
    def get_converter(self, format=None):
 
1437
        """See BzrDirFormat.get_converter()."""
 
1438
        if format is None:
 
1439
            format = BzrDirFormat.get_default_format()
 
1440
        if not isinstance(self, format.__class__):
 
1441
            # converting away from metadir is not implemented
 
1442
            raise NotImplementedError(self.get_converter)
 
1443
        return ConvertMetaToMeta(format)
 
1444
 
 
1445
    def get_format_string(self):
 
1446
        """See BzrDirFormat.get_format_string()."""
 
1447
        return "Bazaar-NG meta directory, format 1\n"
 
1448
 
 
1449
    def get_format_description(self):
 
1450
        """See BzrDirFormat.get_format_description()."""
 
1451
        return "Meta directory format 1"
 
1452
 
 
1453
    def _open(self, transport):
 
1454
        """See BzrDirFormat._open."""
 
1455
        return BzrDirMeta1(transport, self)
 
1456
 
 
1457
    def __return_repository_format(self):
 
1458
        """Circular import protection."""
 
1459
        if getattr(self, '_repository_format', None):
 
1460
            return self._repository_format
 
1461
        from bzrlib.repository import RepositoryFormat
 
1462
        return RepositoryFormat.get_default_format()
 
1463
 
 
1464
    def __set_repository_format(self, value):
 
1465
        """Allow changint the repository format for metadir formats."""
 
1466
        self._repository_format = value
 
1467
 
 
1468
    repository_format = property(__return_repository_format, __set_repository_format)
 
1469
 
 
1470
 
 
1471
BzrDirFormat.register_format(BzrDirFormat4())
 
1472
BzrDirFormat.register_format(BzrDirFormat5())
 
1473
BzrDirFormat.register_format(BzrDirFormat6())
 
1474
__default_format = BzrDirMetaFormat1()
 
1475
BzrDirFormat.register_format(__default_format)
 
1476
BzrDirFormat._default_format = __default_format
 
1477
 
 
1478
 
 
1479
class BzrDirTestProviderAdapter(object):
 
1480
    """A tool to generate a suite testing multiple bzrdir formats at once.
 
1481
 
 
1482
    This is done by copying the test once for each transport and injecting
 
1483
    the transport_server, transport_readonly_server, and bzrdir_format
 
1484
    classes into each copy. Each copy is also given a new id() to make it
 
1485
    easy to identify.
 
1486
    """
 
1487
 
 
1488
    def __init__(self, transport_server, transport_readonly_server, formats):
 
1489
        self._transport_server = transport_server
 
1490
        self._transport_readonly_server = transport_readonly_server
 
1491
        self._formats = formats
 
1492
    
 
1493
    def adapt(self, test):
 
1494
        result = unittest.TestSuite()
 
1495
        for format in self._formats:
 
1496
            new_test = deepcopy(test)
 
1497
            new_test.transport_server = self._transport_server
 
1498
            new_test.transport_readonly_server = self._transport_readonly_server
 
1499
            new_test.bzrdir_format = format
 
1500
            def make_new_test_id():
 
1501
                new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
 
1502
                return lambda: new_id
 
1503
            new_test.id = make_new_test_id()
 
1504
            result.addTest(new_test)
 
1505
        return result
 
1506
 
 
1507
 
 
1508
class Converter(object):
 
1509
    """Converts a disk format object from one format to another."""
 
1510
 
 
1511
    def convert(self, to_convert, pb):
 
1512
        """Perform the conversion of to_convert, giving feedback via pb.
 
1513
 
 
1514
        :param to_convert: The disk object to convert.
 
1515
        :param pb: a progress bar to use for progress information.
 
1516
        """
 
1517
 
 
1518
    def step(self, message):
 
1519
        """Update the pb by a step."""
 
1520
        self.count +=1
 
1521
        self.pb.update(message, self.count, self.total)
 
1522
 
 
1523
 
 
1524
class ConvertBzrDir4To5(Converter):
 
1525
    """Converts format 4 bzr dirs to format 5."""
 
1526
 
 
1527
    def __init__(self):
 
1528
        super(ConvertBzrDir4To5, self).__init__()
 
1529
        self.converted_revs = set()
 
1530
        self.absent_revisions = set()
 
1531
        self.text_count = 0
 
1532
        self.revisions = {}
 
1533
        
 
1534
    def convert(self, to_convert, pb):
 
1535
        """See Converter.convert()."""
 
1536
        self.bzrdir = to_convert
 
1537
        self.pb = pb
 
1538
        self.pb.note('starting upgrade from format 4 to 5')
 
1539
        if isinstance(self.bzrdir.transport, LocalTransport):
 
1540
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
 
1541
        self._convert_to_weaves()
 
1542
        return BzrDir.open(self.bzrdir.root_transport.base)
 
1543
 
 
1544
    def _convert_to_weaves(self):
 
1545
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
 
1546
        try:
 
1547
            # TODO permissions
 
1548
            stat = self.bzrdir.transport.stat('weaves')
 
1549
            if not S_ISDIR(stat.st_mode):
 
1550
                self.bzrdir.transport.delete('weaves')
 
1551
                self.bzrdir.transport.mkdir('weaves')
 
1552
        except errors.NoSuchFile:
 
1553
            self.bzrdir.transport.mkdir('weaves')
 
1554
        # deliberately not a WeaveFile as we want to build it up slowly.
 
1555
        self.inv_weave = Weave('inventory')
 
1556
        # holds in-memory weaves for all files
 
1557
        self.text_weaves = {}
 
1558
        self.bzrdir.transport.delete('branch-format')
 
1559
        self.branch = self.bzrdir.open_branch()
 
1560
        self._convert_working_inv()
 
1561
        rev_history = self.branch.revision_history()
 
1562
        # to_read is a stack holding the revisions we still need to process;
 
1563
        # appending to it adds new highest-priority revisions
 
1564
        self.known_revisions = set(rev_history)
 
1565
        self.to_read = rev_history[-1:]
 
1566
        while self.to_read:
 
1567
            rev_id = self.to_read.pop()
 
1568
            if (rev_id not in self.revisions
 
1569
                and rev_id not in self.absent_revisions):
 
1570
                self._load_one_rev(rev_id)
 
1571
        self.pb.clear()
 
1572
        to_import = self._make_order()
 
1573
        for i, rev_id in enumerate(to_import):
 
1574
            self.pb.update('converting revision', i, len(to_import))
 
1575
            self._convert_one_rev(rev_id)
 
1576
        self.pb.clear()
 
1577
        self._write_all_weaves()
 
1578
        self._write_all_revs()
 
1579
        self.pb.note('upgraded to weaves:')
 
1580
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
 
1581
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
 
1582
        self.pb.note('  %6d texts', self.text_count)
 
1583
        self._cleanup_spare_files_after_format4()
 
1584
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
 
1585
 
 
1586
    def _cleanup_spare_files_after_format4(self):
 
1587
        # FIXME working tree upgrade foo.
 
1588
        for n in 'merged-patches', 'pending-merged-patches':
 
1589
            try:
 
1590
                ## assert os.path.getsize(p) == 0
 
1591
                self.bzrdir.transport.delete(n)
 
1592
            except errors.NoSuchFile:
 
1593
                pass
 
1594
        self.bzrdir.transport.delete_tree('inventory-store')
 
1595
        self.bzrdir.transport.delete_tree('text-store')
 
1596
 
 
1597
    def _convert_working_inv(self):
 
1598
        inv = xml4.serializer_v4.read_inventory(
 
1599
                    self.branch.control_files.get('inventory'))
 
1600
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
 
1601
        # FIXME inventory is a working tree change.
 
1602
        self.branch.control_files.put('inventory', StringIO(new_inv_xml))
 
1603
 
 
1604
    def _write_all_weaves(self):
 
1605
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
 
1606
        weave_transport = self.bzrdir.transport.clone('weaves')
 
1607
        weaves = WeaveStore(weave_transport, prefixed=False)
 
1608
        transaction = WriteTransaction()
 
1609
 
 
1610
        try:
 
1611
            i = 0
 
1612
            for file_id, file_weave in self.text_weaves.items():
 
1613
                self.pb.update('writing weave', i, len(self.text_weaves))
 
1614
                weaves._put_weave(file_id, file_weave, transaction)
 
1615
                i += 1
 
1616
            self.pb.update('inventory', 0, 1)
 
1617
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
 
1618
            self.pb.update('inventory', 1, 1)
 
1619
        finally:
 
1620
            self.pb.clear()
 
1621
 
 
1622
    def _write_all_revs(self):
 
1623
        """Write all revisions out in new form."""
 
1624
        self.bzrdir.transport.delete_tree('revision-store')
 
1625
        self.bzrdir.transport.mkdir('revision-store')
 
1626
        revision_transport = self.bzrdir.transport.clone('revision-store')
 
1627
        # TODO permissions
 
1628
        _revision_store = TextRevisionStore(TextStore(revision_transport,
 
1629
                                                      prefixed=False,
 
1630
                                                      compressed=True))
 
1631
        try:
 
1632
            transaction = WriteTransaction()
 
1633
            for i, rev_id in enumerate(self.converted_revs):
 
1634
                self.pb.update('write revision', i, len(self.converted_revs))
 
1635
                _revision_store.add_revision(self.revisions[rev_id], transaction)
 
1636
        finally:
 
1637
            self.pb.clear()
 
1638
            
 
1639
    def _load_one_rev(self, rev_id):
 
1640
        """Load a revision object into memory.
 
1641
 
 
1642
        Any parents not either loaded or abandoned get queued to be
 
1643
        loaded."""
 
1644
        self.pb.update('loading revision',
 
1645
                       len(self.revisions),
 
1646
                       len(self.known_revisions))
 
1647
        if not self.branch.repository.has_revision(rev_id):
 
1648
            self.pb.clear()
 
1649
            self.pb.note('revision {%s} not present in branch; '
 
1650
                         'will be converted as a ghost',
 
1651
                         rev_id)
 
1652
            self.absent_revisions.add(rev_id)
 
1653
        else:
 
1654
            rev = self.branch.repository._revision_store.get_revision(rev_id,
 
1655
                self.branch.repository.get_transaction())
 
1656
            for parent_id in rev.parent_ids:
 
1657
                self.known_revisions.add(parent_id)
 
1658
                self.to_read.append(parent_id)
 
1659
            self.revisions[rev_id] = rev
 
1660
 
 
1661
    def _load_old_inventory(self, rev_id):
 
1662
        assert rev_id not in self.converted_revs
 
1663
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
 
1664
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
 
1665
        inv.revision_id = rev_id
 
1666
        rev = self.revisions[rev_id]
 
1667
        if rev.inventory_sha1:
 
1668
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
 
1669
                'inventory sha mismatch for {%s}' % rev_id
 
1670
        return inv
 
1671
 
 
1672
    def _load_updated_inventory(self, rev_id):
 
1673
        assert rev_id in self.converted_revs
 
1674
        inv_xml = self.inv_weave.get_text(rev_id)
 
1675
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml)
 
1676
        return inv
 
1677
 
 
1678
    def _convert_one_rev(self, rev_id):
 
1679
        """Convert revision and all referenced objects to new format."""
 
1680
        rev = self.revisions[rev_id]
 
1681
        inv = self._load_old_inventory(rev_id)
 
1682
        present_parents = [p for p in rev.parent_ids
 
1683
                           if p not in self.absent_revisions]
 
1684
        self._convert_revision_contents(rev, inv, present_parents)
 
1685
        self._store_new_weave(rev, inv, present_parents)
 
1686
        self.converted_revs.add(rev_id)
 
1687
 
 
1688
    def _store_new_weave(self, rev, inv, present_parents):
 
1689
        # the XML is now updated with text versions
 
1690
        if __debug__:
 
1691
            entries = inv.iter_entries()
 
1692
            entries.next()
 
1693
            for path, ie in entries:
 
1694
                assert getattr(ie, 'revision', None) is not None, \
 
1695
                    'no revision on {%s} in {%s}' % \
 
1696
                    (file_id, rev.revision_id)
 
1697
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
 
1698
        new_inv_sha1 = sha_string(new_inv_xml)
 
1699
        self.inv_weave.add_lines(rev.revision_id, 
 
1700
                                 present_parents,
 
1701
                                 new_inv_xml.splitlines(True))
 
1702
        rev.inventory_sha1 = new_inv_sha1
 
1703
 
 
1704
    def _convert_revision_contents(self, rev, inv, present_parents):
 
1705
        """Convert all the files within a revision.
 
1706
 
 
1707
        Also upgrade the inventory to refer to the text revision ids."""
 
1708
        rev_id = rev.revision_id
 
1709
        mutter('converting texts of revision {%s}',
 
1710
               rev_id)
 
1711
        parent_invs = map(self._load_updated_inventory, present_parents)
 
1712
        entries = inv.iter_entries()
 
1713
        entries.next()
 
1714
        for path, ie in entries:
 
1715
            self._convert_file_version(rev, ie, parent_invs)
 
1716
 
 
1717
    def _convert_file_version(self, rev, ie, parent_invs):
 
1718
        """Convert one version of one file.
 
1719
 
 
1720
        The file needs to be added into the weave if it is a merge
 
1721
        of >=2 parents or if it's changed from its parent.
 
1722
        """
 
1723
        file_id = ie.file_id
 
1724
        rev_id = rev.revision_id
 
1725
        w = self.text_weaves.get(file_id)
 
1726
        if w is None:
 
1727
            w = Weave(file_id)
 
1728
            self.text_weaves[file_id] = w
 
1729
        text_changed = False
 
1730
        previous_entries = ie.find_previous_heads(parent_invs,
 
1731
                                                  None,
 
1732
                                                  None,
 
1733
                                                  entry_vf=w)
 
1734
        for old_revision in previous_entries:
 
1735
                # if this fails, its a ghost ?
 
1736
                assert old_revision in self.converted_revs, \
 
1737
                    "Revision {%s} not in converted_revs" % old_revision
 
1738
        self.snapshot_ie(previous_entries, ie, w, rev_id)
 
1739
        del ie.text_id
 
1740
        assert getattr(ie, 'revision', None) is not None
 
1741
 
 
1742
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
 
1743
        # TODO: convert this logic, which is ~= snapshot to
 
1744
        # a call to:. This needs the path figured out. rather than a work_tree
 
1745
        # a v4 revision_tree can be given, or something that looks enough like
 
1746
        # one to give the file content to the entry if it needs it.
 
1747
        # and we need something that looks like a weave store for snapshot to 
 
1748
        # save against.
 
1749
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
 
1750
        if len(previous_revisions) == 1:
 
1751
            previous_ie = previous_revisions.values()[0]
 
1752
            if ie._unchanged(previous_ie):
 
1753
                ie.revision = previous_ie.revision
 
1754
                return
 
1755
        if ie.has_text():
 
1756
            text = self.branch.repository.text_store.get(ie.text_id)
 
1757
            file_lines = text.readlines()
 
1758
            assert sha_strings(file_lines) == ie.text_sha1
 
1759
            assert sum(map(len, file_lines)) == ie.text_size
 
1760
            w.add_lines(rev_id, previous_revisions, file_lines)
 
1761
            self.text_count += 1
 
1762
        else:
 
1763
            w.add_lines(rev_id, previous_revisions, [])
 
1764
        ie.revision = rev_id
 
1765
 
 
1766
    def _make_order(self):
 
1767
        """Return a suitable order for importing revisions.
 
1768
 
 
1769
        The order must be such that an revision is imported after all
 
1770
        its (present) parents.
 
1771
        """
 
1772
        todo = set(self.revisions.keys())
 
1773
        done = self.absent_revisions.copy()
 
1774
        order = []
 
1775
        while todo:
 
1776
            # scan through looking for a revision whose parents
 
1777
            # are all done
 
1778
            for rev_id in sorted(list(todo)):
 
1779
                rev = self.revisions[rev_id]
 
1780
                parent_ids = set(rev.parent_ids)
 
1781
                if parent_ids.issubset(done):
 
1782
                    # can take this one now
 
1783
                    order.append(rev_id)
 
1784
                    todo.remove(rev_id)
 
1785
                    done.add(rev_id)
 
1786
        return order
 
1787
 
 
1788
 
 
1789
class ConvertBzrDir5To6(Converter):
 
1790
    """Converts format 5 bzr dirs to format 6."""
 
1791
 
 
1792
    def convert(self, to_convert, pb):
 
1793
        """See Converter.convert()."""
 
1794
        self.bzrdir = to_convert
 
1795
        self.pb = pb
 
1796
        self.pb.note('starting upgrade from format 5 to 6')
 
1797
        self._convert_to_prefixed()
 
1798
        return BzrDir.open(self.bzrdir.root_transport.base)
 
1799
 
 
1800
    def _convert_to_prefixed(self):
 
1801
        from bzrlib.store import TransportStore
 
1802
        self.bzrdir.transport.delete('branch-format')
 
1803
        for store_name in ["weaves", "revision-store"]:
 
1804
            self.pb.note("adding prefixes to %s" % store_name)
 
1805
            store_transport = self.bzrdir.transport.clone(store_name)
 
1806
            store = TransportStore(store_transport, prefixed=True)
 
1807
            for urlfilename in store_transport.list_dir('.'):
 
1808
                filename = urlutils.unescape(urlfilename)
 
1809
                if (filename.endswith(".weave") or
 
1810
                    filename.endswith(".gz") or
 
1811
                    filename.endswith(".sig")):
 
1812
                    file_id = os.path.splitext(filename)[0]
 
1813
                else:
 
1814
                    file_id = filename
 
1815
                prefix_dir = store.hash_prefix(file_id)
 
1816
                # FIXME keep track of the dirs made RBC 20060121
 
1817
                try:
 
1818
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
1819
                except errors.NoSuchFile: # catches missing dirs strangely enough
 
1820
                    store_transport.mkdir(prefix_dir)
 
1821
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
1822
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
 
1823
 
 
1824
 
 
1825
class ConvertBzrDir6ToMeta(Converter):
 
1826
    """Converts format 6 bzr dirs to metadirs."""
 
1827
 
 
1828
    def convert(self, to_convert, pb):
 
1829
        """See Converter.convert()."""
 
1830
        from bzrlib.branch import BzrBranchFormat5
 
1831
        self.bzrdir = to_convert
 
1832
        self.pb = pb
 
1833
        self.count = 0
 
1834
        self.total = 20 # the steps we know about
 
1835
        self.garbage_inventories = []
 
1836
 
 
1837
        self.pb.note('starting upgrade from format 6 to metadir')
 
1838
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
 
1839
        # its faster to move specific files around than to open and use the apis...
 
1840
        # first off, nuke ancestry.weave, it was never used.
 
1841
        try:
 
1842
            self.step('Removing ancestry.weave')
 
1843
            self.bzrdir.transport.delete('ancestry.weave')
 
1844
        except errors.NoSuchFile:
 
1845
            pass
 
1846
        # find out whats there
 
1847
        self.step('Finding branch files')
 
1848
        last_revision = self.bzrdir.open_branch().last_revision()
 
1849
        bzrcontents = self.bzrdir.transport.list_dir('.')
 
1850
        for name in bzrcontents:
 
1851
            if name.startswith('basis-inventory.'):
 
1852
                self.garbage_inventories.append(name)
 
1853
        # create new directories for repository, working tree and branch
 
1854
        self.dir_mode = self.bzrdir._control_files._dir_mode
 
1855
        self.file_mode = self.bzrdir._control_files._file_mode
 
1856
        repository_names = [('inventory.weave', True),
 
1857
                            ('revision-store', True),
 
1858
                            ('weaves', True)]
 
1859
        self.step('Upgrading repository  ')
 
1860
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
 
1861
        self.make_lock('repository')
 
1862
        # we hard code the formats here because we are converting into
 
1863
        # the meta format. The meta format upgrader can take this to a 
 
1864
        # future format within each component.
 
1865
        self.put_format('repository', _mod_repository.RepositoryFormat7())
 
1866
        for entry in repository_names:
 
1867
            self.move_entry('repository', entry)
 
1868
 
 
1869
        self.step('Upgrading branch      ')
 
1870
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
 
1871
        self.make_lock('branch')
 
1872
        self.put_format('branch', BzrBranchFormat5())
 
1873
        branch_files = [('revision-history', True),
 
1874
                        ('branch-name', True),
 
1875
                        ('parent', False)]
 
1876
        for entry in branch_files:
 
1877
            self.move_entry('branch', entry)
 
1878
 
 
1879
        checkout_files = [('pending-merges', True),
 
1880
                          ('inventory', True),
 
1881
                          ('stat-cache', False)]
 
1882
        # If a mandatory checkout file is not present, the branch does not have
 
1883
        # a functional checkout. Do not create a checkout in the converted
 
1884
        # branch.
 
1885
        for name, mandatory in checkout_files:
 
1886
            if mandatory and name not in bzrcontents:
 
1887
                has_checkout = False
 
1888
                break
 
1889
        else:
 
1890
            has_checkout = True
 
1891
        if not has_checkout:
 
1892
            self.pb.note('No working tree.')
 
1893
            # If some checkout files are there, we may as well get rid of them.
 
1894
            for name, mandatory in checkout_files:
 
1895
                if name in bzrcontents:
 
1896
                    self.bzrdir.transport.delete(name)
 
1897
        else:
 
1898
            from bzrlib.workingtree import WorkingTreeFormat3
 
1899
            self.step('Upgrading working tree')
 
1900
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
 
1901
            self.make_lock('checkout')
 
1902
            self.put_format(
 
1903
                'checkout', WorkingTreeFormat3())
 
1904
            self.bzrdir.transport.delete_multi(
 
1905
                self.garbage_inventories, self.pb)
 
1906
            for entry in checkout_files:
 
1907
                self.move_entry('checkout', entry)
 
1908
            if last_revision is not None:
 
1909
                self.bzrdir._control_files.put_utf8(
 
1910
                    'checkout/last-revision', last_revision)
 
1911
        self.bzrdir._control_files.put_utf8(
 
1912
            'branch-format', BzrDirMetaFormat1().get_format_string())
 
1913
        return BzrDir.open(self.bzrdir.root_transport.base)
 
1914
 
 
1915
    def make_lock(self, name):
 
1916
        """Make a lock for the new control dir name."""
 
1917
        self.step('Make %s lock' % name)
 
1918
        ld = lockdir.LockDir(self.bzrdir.transport,
 
1919
                             '%s/lock' % name,
 
1920
                             file_modebits=self.file_mode,
 
1921
                             dir_modebits=self.dir_mode)
 
1922
        ld.create()
 
1923
 
 
1924
    def move_entry(self, new_dir, entry):
 
1925
        """Move then entry name into new_dir."""
 
1926
        name = entry[0]
 
1927
        mandatory = entry[1]
 
1928
        self.step('Moving %s' % name)
 
1929
        try:
 
1930
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
 
1931
        except errors.NoSuchFile:
 
1932
            if mandatory:
 
1933
                raise
 
1934
 
 
1935
    def put_format(self, dirname, format):
 
1936
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
 
1937
 
 
1938
 
 
1939
class ConvertMetaToMeta(Converter):
 
1940
    """Converts the components of metadirs."""
 
1941
 
 
1942
    def __init__(self, target_format):
 
1943
        """Create a metadir to metadir converter.
 
1944
 
 
1945
        :param target_format: The final metadir format that is desired.
 
1946
        """
 
1947
        self.target_format = target_format
 
1948
 
 
1949
    def convert(self, to_convert, pb):
 
1950
        """See Converter.convert()."""
 
1951
        self.bzrdir = to_convert
 
1952
        self.pb = pb
 
1953
        self.count = 0
 
1954
        self.total = 1
 
1955
        self.step('checking repository format')
 
1956
        try:
 
1957
            repo = self.bzrdir.open_repository()
 
1958
        except errors.NoRepositoryPresent:
 
1959
            pass
 
1960
        else:
 
1961
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
 
1962
                from bzrlib.repository import CopyConverter
 
1963
                self.pb.note('starting repository conversion')
 
1964
                converter = CopyConverter(self.target_format.repository_format)
 
1965
                converter.convert(repo, pb)
 
1966
        return to_convert
 
1967
 
 
1968
 
 
1969
class BzrDirFormatInfo(object):
 
1970
 
 
1971
    def __init__(self, native, deprecated):
 
1972
        self.deprecated = deprecated
 
1973
        self.native = native
 
1974
 
 
1975
 
 
1976
class BzrDirFormatRegistry(registry.Registry):
 
1977
    """Registry of user-selectable BzrDir subformats.
 
1978
    
 
1979
    Differs from BzrDirFormat._control_formats in that it provides sub-formats,
 
1980
    e.g. BzrDirMeta1 with weave repository.  Also, it's more user-oriented.
 
1981
    """
 
1982
 
 
1983
    def register_metadir(self, key, repo, help, native=True, deprecated=False,
 
1984
                         branch_format=None):
 
1985
        """Register a metadir subformat.
 
1986
        
 
1987
        repo is the repository format name as a string.
 
1988
        """
 
1989
        # This should be expanded to support setting WorkingTree and Branch
 
1990
        # formats, once BzrDirMetaFormat1 supports that.
 
1991
        def helper():
 
1992
            import bzrlib.repository
 
1993
            import bzrlib.branch
 
1994
            repo_format = getattr(bzrlib.repository, repo)
 
1995
            bd = BzrDirMetaFormat1()
 
1996
            bd.repository_format = repo_format()
 
1997
            if branch_format is not None:
 
1998
                bd.branch_format =  getattr(bzrlib.branch, branch_format)()
 
1999
            return bd
 
2000
        self.register(key, helper, help, native, deprecated)
 
2001
 
 
2002
    def register(self, key, factory, help, native=True, deprecated=False):
 
2003
        """Register a BzrDirFormat factory.
 
2004
        
 
2005
        The factory must be a callable that takes one parameter: the key.
 
2006
        It must produce an instance of the BzrDirFormat when called.
 
2007
 
 
2008
        This function mainly exists to prevent the info object from being
 
2009
        supplied directly.
 
2010
        """
 
2011
        registry.Registry.register(self, key, factory, help, 
 
2012
            BzrDirFormatInfo(native, deprecated))
 
2013
 
 
2014
    def register_lazy(self, key, module_name, member_name, help, native=True,
 
2015
                      deprecated=False):
 
2016
        registry.Registry.register_lazy(self, key, module_name, member_name, 
 
2017
            help, BzrDirFormatInfo(native, deprecated))
 
2018
 
 
2019
    def set_default(self, key):
 
2020
        """Set the 'default' key to be a clone of the supplied key.
 
2021
        
 
2022
        This method must be called once and only once.
 
2023
        """
 
2024
        registry.Registry.register(self, 'default', self.get(key), 
 
2025
            self.get_help(key), info=self.get_info(key))
 
2026
 
 
2027
    def set_default_repository(self, key):
 
2028
        """Set the FormatRegistry default and Repository default.
 
2029
        
 
2030
        This is a transitional method while Repository.set_default_format
 
2031
        is deprecated.
 
2032
        """
 
2033
        if 'default' in self:
 
2034
            self.remove('default')
 
2035
        self.set_default(key)
 
2036
        format = self.get('default')()
 
2037
        assert isinstance(format, BzrDirMetaFormat1)
 
2038
        from bzrlib import repository
 
2039
        repository.RepositoryFormat._set_default_format(
 
2040
            format.repository_format)
 
2041
 
 
2042
    def make_bzrdir(self, key):
 
2043
        return self.get(key)()
 
2044
 
 
2045
    def help_topic(self, topic):
 
2046
        output = textwrap.dedent("""\
 
2047
            Bazaar directory formats
 
2048
            ------------------------
 
2049
 
 
2050
            These formats can be used for creating branches, working trees, and
 
2051
            repositories.
 
2052
 
 
2053
            """)
 
2054
        default_help = self.get_help('default')
 
2055
        help_pairs = []
 
2056
        for key in self.keys():
 
2057
            if key == 'default':
 
2058
                continue
 
2059
            help = self.get_help(key)
 
2060
            if help == default_help:
 
2061
                default_realkey = key
 
2062
            else:
 
2063
                help_pairs.append((key, help))
 
2064
 
 
2065
        def wrapped(key, help, info):
 
2066
            if info.native:
 
2067
                help = '(native) ' + help
 
2068
            return '  %s:\n%s\n\n' % (key, 
 
2069
                    textwrap.fill(help, initial_indent='    ', 
 
2070
                    subsequent_indent='    '))
 
2071
        output += wrapped('%s/default' % default_realkey, default_help,
 
2072
                          self.get_info('default'))
 
2073
        deprecated_pairs = []
 
2074
        for key, help in help_pairs:
 
2075
            info = self.get_info(key)
 
2076
            if info.deprecated:
 
2077
                deprecated_pairs.append((key, help))
 
2078
            else:
 
2079
                output += wrapped(key, help, info)
 
2080
        if len(deprecated_pairs) > 0:
 
2081
            output += "Deprecated formats\n------------------\n\n"
 
2082
            for key, help in deprecated_pairs:
 
2083
                info = self.get_info(key)
 
2084
                output += wrapped(key, help, info)
 
2085
 
 
2086
        return output
 
2087
 
 
2088
 
 
2089
format_registry = BzrDirFormatRegistry()
 
2090
format_registry.register('weave', BzrDirFormat6,
 
2091
    'Pre-0.8 format.  Slower than knit and does not'
 
2092
    ' support checkouts or shared repositories.', deprecated=True)
 
2093
format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
 
2094
    'Format using knits.  Recommended.')
 
2095
format_registry.set_default('knit')
 
2096
format_registry.register_metadir('metaweave', 'RepositoryFormat7',
 
2097
    'Transitional format in 0.8.  Slower than knit.',
 
2098
    deprecated=True)
 
2099
format_registry.register_metadir('experimental-knit2', 'RepositoryFormatKnit2',
 
2100
    'Experimental successor to knit.  Use at your own risk.')
 
2101
format_registry.register_metadir('experimental-branch6', 
 
2102
    'RepositoryFormatKnit2',
 
2103
    'Experimental successor to knit.  Use at your own risk.', 
 
2104
    branch_format='BzrBranchFormat6')