/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

Add simple tests and docstrings for GraphWalker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 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
 
Note: This module has a lot of ``open`` functions/methods that return
23
 
references to in-memory objects. As a rule, there are no matching ``close``
24
 
methods. To free any associated resources, simply stop referencing the
25
 
objects returned.
26
 
"""
27
 
 
28
 
# TODO: Move old formats into a plugin to make this file smaller.
29
 
 
30
 
from cStringIO import StringIO
31
 
import os
32
 
import sys
33
 
 
34
 
from bzrlib.lazy_import import lazy_import
35
 
lazy_import(globals(), """
36
 
from stat import S_ISDIR
37
 
import textwrap
38
 
from warnings import warn
39
 
 
40
 
import bzrlib
41
 
from bzrlib import (
42
 
    errors,
43
 
    graph,
44
 
    lockable_files,
45
 
    lockdir,
46
 
    registry,
47
 
    remote,
48
 
    revision as _mod_revision,
49
 
    symbol_versioning,
50
 
    ui,
51
 
    urlutils,
52
 
    win32utils,
53
 
    workingtree,
54
 
    workingtree_4,
55
 
    xml4,
56
 
    xml5,
57
 
    )
58
 
from bzrlib.osutils import (
59
 
    sha_strings,
60
 
    sha_string,
61
 
    )
62
 
from bzrlib.smart.client import _SmartClient
63
 
from bzrlib.smart import protocol
64
 
from bzrlib.store.revision.text import TextRevisionStore
65
 
from bzrlib.store.text import TextStore
66
 
from bzrlib.store.versioned import WeaveStore
67
 
from bzrlib.transactions import WriteTransaction
68
 
from bzrlib.transport import (
69
 
    do_catching_redirections,
70
 
    get_transport,
71
 
    )
72
 
from bzrlib.weave import Weave
73
 
""")
74
 
 
75
 
from bzrlib.trace import (
76
 
    mutter,
77
 
    note,
78
 
    )
79
 
from bzrlib.transport.local import LocalTransport
80
 
from bzrlib.symbol_versioning import (
81
 
    deprecated_function,
82
 
    deprecated_method,
83
 
    zero_ninetyone,
84
 
    )
85
 
 
86
 
 
87
 
class BzrDir(object):
88
 
    """A .bzr control diretory.
89
 
    
90
 
    BzrDir instances let you create or open any of the things that can be
91
 
    found within .bzr - checkouts, branches and repositories.
92
 
    
93
 
    transport
94
 
        the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
95
 
    root_transport
96
 
        a transport connected to the directory this bzr was opened from
97
 
        (i.e. the parent directory holding the .bzr directory).
98
 
    """
99
 
 
100
 
    def break_lock(self):
101
 
        """Invoke break_lock on the first object in the bzrdir.
102
 
 
103
 
        If there is a tree, the tree is opened and break_lock() called.
104
 
        Otherwise, branch is tried, and finally repository.
105
 
        """
106
 
        # XXX: This seems more like a UI function than something that really
107
 
        # belongs in this class.
108
 
        try:
109
 
            thing_to_unlock = self.open_workingtree()
110
 
        except (errors.NotLocalUrl, errors.NoWorkingTree):
111
 
            try:
112
 
                thing_to_unlock = self.open_branch()
113
 
            except errors.NotBranchError:
114
 
                try:
115
 
                    thing_to_unlock = self.open_repository()
116
 
                except errors.NoRepositoryPresent:
117
 
                    return
118
 
        thing_to_unlock.break_lock()
119
 
 
120
 
    def can_convert_format(self):
121
 
        """Return true if this bzrdir is one whose format we can convert from."""
122
 
        return True
123
 
 
124
 
    def check_conversion_target(self, target_format):
125
 
        target_repo_format = target_format.repository_format
126
 
        source_repo_format = self._format.repository_format
127
 
        source_repo_format.check_conversion_target(target_repo_format)
128
 
 
129
 
    @staticmethod
130
 
    def _check_supported(format, allow_unsupported,
131
 
        recommend_upgrade=True,
132
 
        basedir=None):
133
 
        """Give an error or warning on old formats.
134
 
 
135
 
        :param format: may be any kind of format - workingtree, branch, 
136
 
        or repository.
137
 
 
138
 
        :param allow_unsupported: If true, allow opening 
139
 
        formats that are strongly deprecated, and which may 
140
 
        have limited functionality.
141
 
 
142
 
        :param recommend_upgrade: If true (default), warn
143
 
        the user through the ui object that they may wish
144
 
        to upgrade the object.
145
 
        """
146
 
        # TODO: perhaps move this into a base Format class; it's not BzrDir
147
 
        # specific. mbp 20070323
148
 
        if not allow_unsupported and not format.is_supported():
149
 
            # see open_downlevel to open legacy branches.
150
 
            raise errors.UnsupportedFormatError(format=format)
151
 
        if recommend_upgrade \
152
 
            and getattr(format, 'upgrade_recommended', False):
153
 
            ui.ui_factory.recommend_upgrade(
154
 
                format.get_format_description(),
155
 
                basedir)
156
 
 
157
 
    def clone(self, url, revision_id=None, force_new_repo=False):
158
 
        """Clone this bzrdir and its contents to url verbatim.
159
 
 
160
 
        If url's last component does not exist, it will be created.
161
 
 
162
 
        if revision_id is not None, then the clone operation may tune
163
 
            itself to download less data.
164
 
        :param force_new_repo: Do not use a shared repository for the target 
165
 
                               even if one is available.
166
 
        """
167
 
        return self.clone_on_transport(get_transport(url),
168
 
                                       revision_id=revision_id,
169
 
                                       force_new_repo=force_new_repo)
170
 
 
171
 
    def clone_on_transport(self, transport, revision_id=None,
172
 
                           force_new_repo=False):
173
 
        """Clone this bzrdir and its contents to transport verbatim.
174
 
 
175
 
        If the target directory does not exist, it will be created.
176
 
 
177
 
        if revision_id is not None, then the clone operation may tune
178
 
            itself to download less data.
179
 
        :param force_new_repo: Do not use a shared repository for the target 
180
 
                               even if one is available.
181
 
        """
182
 
        transport.ensure_base()
183
 
        result = self._format.initialize_on_transport(transport)
184
 
        try:
185
 
            local_repo = self.find_repository()
186
 
        except errors.NoRepositoryPresent:
187
 
            local_repo = None
188
 
        if local_repo:
189
 
            # may need to copy content in
190
 
            if force_new_repo:
191
 
                result_repo = local_repo.clone(
192
 
                    result,
193
 
                    revision_id=revision_id)
194
 
                result_repo.set_make_working_trees(local_repo.make_working_trees())
195
 
            else:
196
 
                try:
197
 
                    result_repo = result.find_repository()
198
 
                    # fetch content this dir needs.
199
 
                    result_repo.fetch(local_repo, revision_id=revision_id)
200
 
                except errors.NoRepositoryPresent:
201
 
                    # needed to make one anyway.
202
 
                    result_repo = local_repo.clone(
203
 
                        result,
204
 
                        revision_id=revision_id)
205
 
                    result_repo.set_make_working_trees(local_repo.make_working_trees())
206
 
        # 1 if there is a branch present
207
 
        #   make sure its content is available in the target repository
208
 
        #   clone it.
209
 
        try:
210
 
            self.open_branch().clone(result, revision_id=revision_id)
211
 
        except errors.NotBranchError:
212
 
            pass
213
 
        try:
214
 
            result_repo = result.find_repository()
215
 
        except errors.NoRepositoryPresent:
216
 
            result_repo = None
217
 
        if result_repo is None or result_repo.make_working_trees():
218
 
            try:
219
 
                self.open_workingtree().clone(result)
220
 
            except (errors.NoWorkingTree, errors.NotLocalUrl):
221
 
                pass
222
 
        return result
223
 
 
224
 
    # TODO: This should be given a Transport, and should chdir up; otherwise
225
 
    # this will open a new connection.
226
 
    def _make_tail(self, url):
227
 
        t = get_transport(url)
228
 
        t.ensure_base()
229
 
 
230
 
    @classmethod
231
 
    def create(cls, base, format=None, possible_transports=None):
232
 
        """Create a new BzrDir at the url 'base'.
233
 
        
234
 
        :param format: If supplied, the format of branch to create.  If not
235
 
            supplied, the default is used.
236
 
        :param possible_transports: If supplied, a list of transports that 
237
 
            can be reused to share a remote connection.
238
 
        """
239
 
        if cls is not BzrDir:
240
 
            raise AssertionError("BzrDir.create always creates the default"
241
 
                " format, not one of %r" % cls)
242
 
        t = get_transport(base, possible_transports)
243
 
        t.ensure_base()
244
 
        if format is None:
245
 
            format = BzrDirFormat.get_default_format()
246
 
        return format.initialize_on_transport(t)
247
 
 
248
 
    def destroy_repository(self):
249
 
        """Destroy the repository in this BzrDir"""
250
 
        raise NotImplementedError(self.destroy_repository)
251
 
 
252
 
    def create_branch(self):
253
 
        """Create a branch in this BzrDir.
254
 
 
255
 
        The bzrdir's format will control what branch format is created.
256
 
        For more control see BranchFormatXX.create(a_bzrdir).
257
 
        """
258
 
        raise NotImplementedError(self.create_branch)
259
 
 
260
 
    def destroy_branch(self):
261
 
        """Destroy the branch in this BzrDir"""
262
 
        raise NotImplementedError(self.destroy_branch)
263
 
 
264
 
    @staticmethod
265
 
    def create_branch_and_repo(base, force_new_repo=False, format=None):
266
 
        """Create a new BzrDir, Branch and Repository at the url 'base'.
267
 
 
268
 
        This will use the current default BzrDirFormat unless one is
269
 
        specified, and use whatever 
270
 
        repository format that that uses via bzrdir.create_branch and
271
 
        create_repository. If a shared repository is available that is used
272
 
        preferentially.
273
 
 
274
 
        The created Branch object is returned.
275
 
 
276
 
        :param base: The URL to create the branch at.
277
 
        :param force_new_repo: If True a new repository is always created.
278
 
        :param format: If supplied, the format of branch to create.  If not
279
 
            supplied, the default is used.
280
 
        """
281
 
        bzrdir = BzrDir.create(base, format)
282
 
        bzrdir._find_or_create_repository(force_new_repo)
283
 
        return bzrdir.create_branch()
284
 
 
285
 
    def _find_or_create_repository(self, force_new_repo):
286
 
        """Create a new repository if needed, returning the repository."""
287
 
        if force_new_repo:
288
 
            return self.create_repository()
289
 
        try:
290
 
            return self.find_repository()
291
 
        except errors.NoRepositoryPresent:
292
 
            return self.create_repository()
293
 
        
294
 
    @staticmethod
295
 
    def create_branch_convenience(base, force_new_repo=False,
296
 
                                  force_new_tree=None, format=None,
297
 
                                  possible_transports=None):
298
 
        """Create a new BzrDir, Branch and Repository at the url 'base'.
299
 
 
300
 
        This is a convenience function - it will use an existing repository
301
 
        if possible, can be told explicitly whether to create a working tree or
302
 
        not.
303
 
 
304
 
        This will use the current default BzrDirFormat unless one is
305
 
        specified, and use whatever 
306
 
        repository format that that uses via bzrdir.create_branch and
307
 
        create_repository. If a shared repository is available that is used
308
 
        preferentially. Whatever repository is used, its tree creation policy
309
 
        is followed.
310
 
 
311
 
        The created Branch object is returned.
312
 
        If a working tree cannot be made due to base not being a file:// url,
313
 
        no error is raised unless force_new_tree is True, in which case no 
314
 
        data is created on disk and NotLocalUrl is raised.
315
 
 
316
 
        :param base: The URL to create the branch at.
317
 
        :param force_new_repo: If True a new repository is always created.
318
 
        :param force_new_tree: If True or False force creation of a tree or 
319
 
                               prevent such creation respectively.
320
 
        :param format: Override for the bzrdir format to create.
321
 
        :param possible_transports: An optional reusable transports list.
322
 
        """
323
 
        if force_new_tree:
324
 
            # check for non local urls
325
 
            t = get_transport(base, possible_transports)
326
 
            if not isinstance(t, LocalTransport):
327
 
                raise errors.NotLocalUrl(base)
328
 
        bzrdir = BzrDir.create(base, format, possible_transports)
329
 
        repo = bzrdir._find_or_create_repository(force_new_repo)
330
 
        result = bzrdir.create_branch()
331
 
        if force_new_tree or (repo.make_working_trees() and
332
 
                              force_new_tree is None):
333
 
            try:
334
 
                bzrdir.create_workingtree()
335
 
            except errors.NotLocalUrl:
336
 
                pass
337
 
        return result
338
 
 
339
 
    @staticmethod
340
 
    @deprecated_function(zero_ninetyone)
341
 
    def create_repository(base, shared=False, format=None):
342
 
        """Create a new BzrDir and Repository at the url 'base'.
343
 
 
344
 
        If no format is supplied, this will default to the current default
345
 
        BzrDirFormat by default, and use whatever repository format that that
346
 
        uses for bzrdirformat.create_repository.
347
 
 
348
 
        :param shared: Create a shared repository rather than a standalone
349
 
                       repository.
350
 
        The Repository object is returned.
351
 
 
352
 
        This must be overridden as an instance method in child classes, where
353
 
        it should take no parameters and construct whatever repository format
354
 
        that child class desires.
355
 
 
356
 
        This method is deprecated, please call create_repository on a bzrdir
357
 
        instance instead.
358
 
        """
359
 
        bzrdir = BzrDir.create(base, format)
360
 
        return bzrdir.create_repository(shared)
361
 
 
362
 
    @staticmethod
363
 
    def create_standalone_workingtree(base, format=None):
364
 
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
365
 
 
366
 
        'base' must be a local path or a file:// url.
367
 
 
368
 
        This will use the current default BzrDirFormat unless one is
369
 
        specified, and use whatever 
370
 
        repository format that that uses for bzrdirformat.create_workingtree,
371
 
        create_branch and create_repository.
372
 
 
373
 
        :param format: Override for the bzrdir format to create.
374
 
        :return: The WorkingTree object.
375
 
        """
376
 
        t = get_transport(base)
377
 
        if not isinstance(t, LocalTransport):
378
 
            raise errors.NotLocalUrl(base)
379
 
        bzrdir = BzrDir.create_branch_and_repo(base,
380
 
                                               force_new_repo=True,
381
 
                                               format=format).bzrdir
382
 
        return bzrdir.create_workingtree()
383
 
 
384
 
    def create_workingtree(self, revision_id=None, from_branch=None,
385
 
        accelerator_tree=None, hardlink=False):
386
 
        """Create a working tree at this BzrDir.
387
 
        
388
 
        :param revision_id: create it as of this revision id.
389
 
        :param from_branch: override bzrdir branch (for lightweight checkouts)
390
 
        :param accelerator_tree: A tree which can be used for retrieving file
391
 
            contents more quickly than the revision tree, i.e. a workingtree.
392
 
            The revision tree will be used for cases where accelerator_tree's
393
 
            content is different.
394
 
        """
395
 
        raise NotImplementedError(self.create_workingtree)
396
 
 
397
 
    def retire_bzrdir(self, limit=10000):
398
 
        """Permanently disable the bzrdir.
399
 
 
400
 
        This is done by renaming it to give the user some ability to recover
401
 
        if there was a problem.
402
 
 
403
 
        This will have horrible consequences if anyone has anything locked or
404
 
        in use.
405
 
        :param limit: number of times to retry
406
 
        """
407
 
        i  = 0
408
 
        while True:
409
 
            try:
410
 
                to_path = '.bzr.retired.%d' % i
411
 
                self.root_transport.rename('.bzr', to_path)
412
 
                note("renamed %s to %s"
413
 
                    % (self.root_transport.abspath('.bzr'), to_path))
414
 
                return
415
 
            except (errors.TransportError, IOError, errors.PathError):
416
 
                i += 1
417
 
                if i > limit:
418
 
                    raise
419
 
                else:
420
 
                    pass
421
 
 
422
 
    def destroy_workingtree(self):
423
 
        """Destroy the working tree at this BzrDir.
424
 
 
425
 
        Formats that do not support this may raise UnsupportedOperation.
426
 
        """
427
 
        raise NotImplementedError(self.destroy_workingtree)
428
 
 
429
 
    def destroy_workingtree_metadata(self):
430
 
        """Destroy the control files for the working tree at this BzrDir.
431
 
 
432
 
        The contents of working tree files are not affected.
433
 
        Formats that do not support this may raise UnsupportedOperation.
434
 
        """
435
 
        raise NotImplementedError(self.destroy_workingtree_metadata)
436
 
 
437
 
    def find_repository(self):
438
 
        """Find the repository that should be used.
439
 
 
440
 
        This does not require a branch as we use it to find the repo for
441
 
        new branches as well as to hook existing branches up to their
442
 
        repository.
443
 
        """
444
 
        try:
445
 
            return self.open_repository()
446
 
        except errors.NoRepositoryPresent:
447
 
            pass
448
 
        next_transport = self.root_transport.clone('..')
449
 
        while True:
450
 
            # find the next containing bzrdir
451
 
            try:
452
 
                found_bzrdir = BzrDir.open_containing_from_transport(
453
 
                    next_transport)[0]
454
 
            except errors.NotBranchError:
455
 
                # none found
456
 
                raise errors.NoRepositoryPresent(self)
457
 
            # does it have a repository ?
458
 
            try:
459
 
                repository = found_bzrdir.open_repository()
460
 
            except errors.NoRepositoryPresent:
461
 
                next_transport = found_bzrdir.root_transport.clone('..')
462
 
                if (found_bzrdir.root_transport.base == next_transport.base):
463
 
                    # top of the file system
464
 
                    break
465
 
                else:
466
 
                    continue
467
 
            if ((found_bzrdir.root_transport.base ==
468
 
                 self.root_transport.base) or repository.is_shared()):
469
 
                return repository
470
 
            else:
471
 
                raise errors.NoRepositoryPresent(self)
472
 
        raise errors.NoRepositoryPresent(self)
473
 
 
474
 
    def get_branch_reference(self):
475
 
        """Return the referenced URL for the branch in this bzrdir.
476
 
 
477
 
        :raises NotBranchError: If there is no Branch.
478
 
        :return: The URL the branch in this bzrdir references if it is a
479
 
            reference branch, or None for regular branches.
480
 
        """
481
 
        return None
482
 
 
483
 
    def get_branch_transport(self, branch_format):
484
 
        """Get the transport for use by branch format in this BzrDir.
485
 
 
486
 
        Note that bzr dirs that do not support format strings will raise
487
 
        IncompatibleFormat if the branch format they are given has
488
 
        a format string, and vice versa.
489
 
 
490
 
        If branch_format is None, the transport is returned with no 
491
 
        checking. If it is not None, then the returned transport is
492
 
        guaranteed to point to an existing directory ready for use.
493
 
        """
494
 
        raise NotImplementedError(self.get_branch_transport)
495
 
        
496
 
    def get_repository_transport(self, repository_format):
497
 
        """Get the transport for use by repository format in this BzrDir.
498
 
 
499
 
        Note that bzr dirs that do not support format strings will raise
500
 
        IncompatibleFormat if the repository format they are given has
501
 
        a format string, and vice versa.
502
 
 
503
 
        If repository_format is None, the transport is returned with no 
504
 
        checking. If it is not None, then the returned transport is
505
 
        guaranteed to point to an existing directory ready for use.
506
 
        """
507
 
        raise NotImplementedError(self.get_repository_transport)
508
 
        
509
 
    def get_workingtree_transport(self, tree_format):
510
 
        """Get the transport for use by workingtree format in this BzrDir.
511
 
 
512
 
        Note that bzr dirs that do not support format strings will raise
513
 
        IncompatibleFormat if the workingtree format they are given has a
514
 
        format string, and vice versa.
515
 
 
516
 
        If workingtree_format is None, the transport is returned with no 
517
 
        checking. If it is not None, then the returned transport is
518
 
        guaranteed to point to an existing directory ready for use.
519
 
        """
520
 
        raise NotImplementedError(self.get_workingtree_transport)
521
 
        
522
 
    def __init__(self, _transport, _format):
523
 
        """Initialize a Bzr control dir object.
524
 
        
525
 
        Only really common logic should reside here, concrete classes should be
526
 
        made with varying behaviours.
527
 
 
528
 
        :param _format: the format that is creating this BzrDir instance.
529
 
        :param _transport: the transport this dir is based at.
530
 
        """
531
 
        self._format = _format
532
 
        self.transport = _transport.clone('.bzr')
533
 
        self.root_transport = _transport
534
 
 
535
 
    def is_control_filename(self, filename):
536
 
        """True if filename is the name of a path which is reserved for bzrdir's.
537
 
        
538
 
        :param filename: A filename within the root transport of this bzrdir.
539
 
 
540
 
        This is true IF and ONLY IF the filename is part of the namespace reserved
541
 
        for bzr control dirs. Currently this is the '.bzr' directory in the root
542
 
        of the root_transport. it is expected that plugins will need to extend
543
 
        this in the future - for instance to make bzr talk with svn working
544
 
        trees.
545
 
        """
546
 
        # this might be better on the BzrDirFormat class because it refers to 
547
 
        # all the possible bzrdir disk formats. 
548
 
        # This method is tested via the workingtree is_control_filename tests- 
549
 
        # it was extracted from WorkingTree.is_control_filename. If the method's
550
 
        # contract is extended beyond the current trivial implementation, please
551
 
        # add new tests for it to the appropriate place.
552
 
        return filename == '.bzr' or filename.startswith('.bzr/')
553
 
 
554
 
    def needs_format_conversion(self, format=None):
555
 
        """Return true if this bzrdir needs convert_format run on it.
556
 
        
557
 
        For instance, if the repository format is out of date but the 
558
 
        branch and working tree are not, this should return True.
559
 
 
560
 
        :param format: Optional parameter indicating a specific desired
561
 
                       format we plan to arrive at.
562
 
        """
563
 
        raise NotImplementedError(self.needs_format_conversion)
564
 
 
565
 
    @staticmethod
566
 
    def open_unsupported(base):
567
 
        """Open a branch which is not supported."""
568
 
        return BzrDir.open(base, _unsupported=True)
569
 
        
570
 
    @staticmethod
571
 
    def open(base, _unsupported=False, possible_transports=None):
572
 
        """Open an existing bzrdir, rooted at 'base' (url).
573
 
        
574
 
        :param _unsupported: a private parameter to the BzrDir class.
575
 
        """
576
 
        t = get_transport(base, possible_transports=possible_transports)
577
 
        return BzrDir.open_from_transport(t, _unsupported=_unsupported)
578
 
 
579
 
    @staticmethod
580
 
    def open_from_transport(transport, _unsupported=False,
581
 
                            _server_formats=True):
582
 
        """Open a bzrdir within a particular directory.
583
 
 
584
 
        :param transport: Transport containing the bzrdir.
585
 
        :param _unsupported: private.
586
 
        """
587
 
        base = transport.base
588
 
 
589
 
        def find_format(transport):
590
 
            return transport, BzrDirFormat.find_format(
591
 
                transport, _server_formats=_server_formats)
592
 
 
593
 
        def redirected(transport, e, redirection_notice):
594
 
            qualified_source = e.get_source_url()
595
 
            relpath = transport.relpath(qualified_source)
596
 
            if not e.target.endswith(relpath):
597
 
                # Not redirected to a branch-format, not a branch
598
 
                raise errors.NotBranchError(path=e.target)
599
 
            target = e.target[:-len(relpath)]
600
 
            note('%s is%s redirected to %s',
601
 
                 transport.base, e.permanently, target)
602
 
            # Let's try with a new transport
603
 
            # FIXME: If 'transport' has a qualifier, this should
604
 
            # be applied again to the new transport *iff* the
605
 
            # schemes used are the same. Uncomment this code
606
 
            # once the function (and tests) exist.
607
 
            # -- vila20070212
608
 
            #target = urlutils.copy_url_qualifiers(original, target)
609
 
            return get_transport(target)
610
 
 
611
 
        try:
612
 
            transport, format = do_catching_redirections(find_format,
613
 
                                                         transport,
614
 
                                                         redirected)
615
 
        except errors.TooManyRedirections:
616
 
            raise errors.NotBranchError(base)
617
 
 
618
 
        BzrDir._check_supported(format, _unsupported)
619
 
        return format.open(transport, _found=True)
620
 
 
621
 
    def open_branch(self, unsupported=False):
622
 
        """Open the branch object at this BzrDir if one is present.
623
 
 
624
 
        If unsupported is True, then no longer supported branch formats can
625
 
        still be opened.
626
 
        
627
 
        TODO: static convenience version of this?
628
 
        """
629
 
        raise NotImplementedError(self.open_branch)
630
 
 
631
 
    @staticmethod
632
 
    def open_containing(url, possible_transports=None):
633
 
        """Open an existing branch which contains url.
634
 
        
635
 
        :param url: url to search from.
636
 
        See open_containing_from_transport for more detail.
637
 
        """
638
 
        transport = get_transport(url, possible_transports)
639
 
        return BzrDir.open_containing_from_transport(transport)
640
 
    
641
 
    @staticmethod
642
 
    def open_containing_from_transport(a_transport):
643
 
        """Open an existing branch which contains a_transport.base.
644
 
 
645
 
        This probes for a branch at a_transport, and searches upwards from there.
646
 
 
647
 
        Basically we keep looking up until we find the control directory or
648
 
        run into the root.  If there isn't one, raises NotBranchError.
649
 
        If there is one and it is either an unrecognised format or an unsupported 
650
 
        format, UnknownFormatError or UnsupportedFormatError are raised.
651
 
        If there is one, it is returned, along with the unused portion of url.
652
 
 
653
 
        :return: The BzrDir that contains the path, and a Unicode path 
654
 
                for the rest of the URL.
655
 
        """
656
 
        # this gets the normalised url back. I.e. '.' -> the full path.
657
 
        url = a_transport.base
658
 
        while True:
659
 
            try:
660
 
                result = BzrDir.open_from_transport(a_transport)
661
 
                return result, urlutils.unescape(a_transport.relpath(url))
662
 
            except errors.NotBranchError, e:
663
 
                pass
664
 
            try:
665
 
                new_t = a_transport.clone('..')
666
 
            except errors.InvalidURLJoin:
667
 
                # reached the root, whatever that may be
668
 
                raise errors.NotBranchError(path=url)
669
 
            if new_t.base == a_transport.base:
670
 
                # reached the root, whatever that may be
671
 
                raise errors.NotBranchError(path=url)
672
 
            a_transport = new_t
673
 
 
674
 
    def _get_tree_branch(self):
675
 
        """Return the branch and tree, if any, for this bzrdir.
676
 
 
677
 
        Return None for tree if not present.
678
 
        Raise NotBranchError if no branch is present.
679
 
        :return: (tree, branch)
680
 
        """
681
 
        try:
682
 
            tree = self.open_workingtree()
683
 
        except (errors.NoWorkingTree, errors.NotLocalUrl):
684
 
            tree = None
685
 
            branch = self.open_branch()
686
 
        else:
687
 
            branch = tree.branch
688
 
        return tree, branch
689
 
 
690
 
    @classmethod
691
 
    def open_tree_or_branch(klass, location):
692
 
        """Return the branch and working tree at a location.
693
 
 
694
 
        If there is no tree at the location, tree will be None.
695
 
        If there is no branch at the location, an exception will be
696
 
        raised
697
 
        :return: (tree, branch)
698
 
        """
699
 
        bzrdir = klass.open(location)
700
 
        return bzrdir._get_tree_branch()
701
 
 
702
 
    @classmethod
703
 
    def open_containing_tree_or_branch(klass, location):
704
 
        """Return the branch and working tree contained by a location.
705
 
 
706
 
        Returns (tree, branch, relpath).
707
 
        If there is no tree at containing the location, tree will be None.
708
 
        If there is no branch containing the location, an exception will be
709
 
        raised
710
 
        relpath is the portion of the path that is contained by the branch.
711
 
        """
712
 
        bzrdir, relpath = klass.open_containing(location)
713
 
        tree, branch = bzrdir._get_tree_branch()
714
 
        return tree, branch, relpath
715
 
 
716
 
    def open_repository(self, _unsupported=False):
717
 
        """Open the repository object at this BzrDir if one is present.
718
 
 
719
 
        This will not follow the Branch object pointer - it's strictly a direct
720
 
        open facility. Most client code should use open_branch().repository to
721
 
        get at a repository.
722
 
 
723
 
        :param _unsupported: a private parameter, not part of the api.
724
 
        TODO: static convenience version of this?
725
 
        """
726
 
        raise NotImplementedError(self.open_repository)
727
 
 
728
 
    def open_workingtree(self, _unsupported=False,
729
 
                         recommend_upgrade=True, from_branch=None):
730
 
        """Open the workingtree object at this BzrDir if one is present.
731
 
 
732
 
        :param recommend_upgrade: Optional keyword parameter, when True (the
733
 
            default), emit through the ui module a recommendation that the user
734
 
            upgrade the working tree when the workingtree being opened is old
735
 
            (but still fully supported).
736
 
        :param from_branch: override bzrdir branch (for lightweight checkouts)
737
 
        """
738
 
        raise NotImplementedError(self.open_workingtree)
739
 
 
740
 
    def has_branch(self):
741
 
        """Tell if this bzrdir contains a branch.
742
 
        
743
 
        Note: if you're going to open the branch, you should just go ahead
744
 
        and try, and not ask permission first.  (This method just opens the 
745
 
        branch and discards it, and that's somewhat expensive.) 
746
 
        """
747
 
        try:
748
 
            self.open_branch()
749
 
            return True
750
 
        except errors.NotBranchError:
751
 
            return False
752
 
 
753
 
    def has_workingtree(self):
754
 
        """Tell if this bzrdir contains a working tree.
755
 
 
756
 
        This will still raise an exception if the bzrdir has a workingtree that
757
 
        is remote & inaccessible.
758
 
        
759
 
        Note: if you're going to open the working tree, you should just go ahead
760
 
        and try, and not ask permission first.  (This method just opens the 
761
 
        workingtree and discards it, and that's somewhat expensive.) 
762
 
        """
763
 
        try:
764
 
            self.open_workingtree(recommend_upgrade=False)
765
 
            return True
766
 
        except errors.NoWorkingTree:
767
 
            return False
768
 
 
769
 
    def _cloning_metadir(self):
770
 
        """Produce a metadir suitable for cloning with."""
771
 
        result_format = self._format.__class__()
772
 
        try:
773
 
            try:
774
 
                branch = self.open_branch()
775
 
                source_repository = branch.repository
776
 
            except errors.NotBranchError:
777
 
                source_branch = None
778
 
                source_repository = self.open_repository()
779
 
        except errors.NoRepositoryPresent:
780
 
            source_repository = None
781
 
        else:
782
 
            # XXX TODO: This isinstance is here because we have not implemented
783
 
            # the fix recommended in bug # 103195 - to delegate this choice the
784
 
            # repository itself.
785
 
            repo_format = source_repository._format
786
 
            if not isinstance(repo_format, remote.RemoteRepositoryFormat):
787
 
                result_format.repository_format = repo_format
788
 
        try:
789
 
            # TODO: Couldn't we just probe for the format in these cases,
790
 
            # rather than opening the whole tree?  It would be a little
791
 
            # faster. mbp 20070401
792
 
            tree = self.open_workingtree(recommend_upgrade=False)
793
 
        except (errors.NoWorkingTree, errors.NotLocalUrl):
794
 
            result_format.workingtree_format = None
795
 
        else:
796
 
            result_format.workingtree_format = tree._format.__class__()
797
 
        return result_format, source_repository
798
 
 
799
 
    def cloning_metadir(self):
800
 
        """Produce a metadir suitable for cloning or sprouting with.
801
 
 
802
 
        These operations may produce workingtrees (yes, even though they're
803
 
        "cloning" something that doesn't have a tree), so a viable workingtree
804
 
        format must be selected.
805
 
        """
806
 
        format, repository = self._cloning_metadir()
807
 
        if format._workingtree_format is None:
808
 
            if repository is None:
809
 
                return format
810
 
            tree_format = repository._format._matchingbzrdir.workingtree_format
811
 
            format.workingtree_format = tree_format.__class__()
812
 
        return format
813
 
 
814
 
    def checkout_metadir(self):
815
 
        return self.cloning_metadir()
816
 
 
817
 
    def sprout(self, url, revision_id=None, force_new_repo=False,
818
 
               recurse='down', possible_transports=None,
819
 
               accelerator_tree=None, hardlink=False):
820
 
        """Create a copy of this bzrdir prepared for use as a new line of
821
 
        development.
822
 
 
823
 
        If url's last component does not exist, it will be created.
824
 
 
825
 
        Attributes related to the identity of the source branch like
826
 
        branch nickname will be cleaned, a working tree is created
827
 
        whether one existed before or not; and a local branch is always
828
 
        created.
829
 
 
830
 
        if revision_id is not None, then the clone operation may tune
831
 
            itself to download less data.
832
 
        :param accelerator_tree: A tree which can be used for retrieving file
833
 
            contents more quickly than the revision tree, i.e. a workingtree.
834
 
            The revision tree will be used for cases where accelerator_tree's
835
 
            content is different.
836
 
        :param hardlink: If true, hard-link files from accelerator_tree,
837
 
            where possible.
838
 
        """
839
 
        target_transport = get_transport(url, possible_transports)
840
 
        target_transport.ensure_base()
841
 
        cloning_format = self.cloning_metadir()
842
 
        result = cloning_format.initialize_on_transport(target_transport)
843
 
        try:
844
 
            source_branch = self.open_branch()
845
 
            source_repository = source_branch.repository
846
 
        except errors.NotBranchError:
847
 
            source_branch = None
848
 
            try:
849
 
                source_repository = self.open_repository()
850
 
            except errors.NoRepositoryPresent:
851
 
                source_repository = None
852
 
        if force_new_repo:
853
 
            result_repo = None
854
 
        else:
855
 
            try:
856
 
                result_repo = result.find_repository()
857
 
            except errors.NoRepositoryPresent:
858
 
                result_repo = None
859
 
        if source_repository is None and result_repo is not None:
860
 
            pass
861
 
        elif source_repository is None and result_repo is None:
862
 
            # no repo available, make a new one
863
 
            result.create_repository()
864
 
        elif source_repository is not None and result_repo is None:
865
 
            # have source, and want to make a new target repo
866
 
            result_repo = source_repository.sprout(result,
867
 
                                                   revision_id=revision_id)
868
 
        else:
869
 
            # fetch needed content into target.
870
 
            if source_repository is not None:
871
 
                # would rather do 
872
 
                # source_repository.copy_content_into(result_repo,
873
 
                #                                     revision_id=revision_id)
874
 
                # so we can override the copy method
875
 
                result_repo.fetch(source_repository, revision_id=revision_id)
876
 
        if source_branch is not None:
877
 
            source_branch.sprout(result, revision_id=revision_id)
878
 
        else:
879
 
            result.create_branch()
880
 
        if isinstance(target_transport, LocalTransport) and (
881
 
            result_repo is None or result_repo.make_working_trees()):
882
 
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
883
 
                hardlink=hardlink)
884
 
            wt.lock_write()
885
 
            try:
886
 
                if wt.path2id('') is None:
887
 
                    try:
888
 
                        wt.set_root_id(self.open_workingtree.get_root_id())
889
 
                    except errors.NoWorkingTree:
890
 
                        pass
891
 
            finally:
892
 
                wt.unlock()
893
 
        else:
894
 
            wt = None
895
 
        if recurse == 'down':
896
 
            if wt is not None:
897
 
                basis = wt.basis_tree()
898
 
                basis.lock_read()
899
 
                subtrees = basis.iter_references()
900
 
                recurse_branch = wt.branch
901
 
            elif source_branch is not None:
902
 
                basis = source_branch.basis_tree()
903
 
                basis.lock_read()
904
 
                subtrees = basis.iter_references()
905
 
                recurse_branch = source_branch
906
 
            else:
907
 
                subtrees = []
908
 
                basis = None
909
 
            try:
910
 
                for path, file_id in subtrees:
911
 
                    target = urlutils.join(url, urlutils.escape(path))
912
 
                    sublocation = source_branch.reference_parent(file_id, path)
913
 
                    sublocation.bzrdir.sprout(target,
914
 
                        basis.get_reference_revision(file_id, path),
915
 
                        force_new_repo=force_new_repo, recurse=recurse)
916
 
            finally:
917
 
                if basis is not None:
918
 
                    basis.unlock()
919
 
        return result
920
 
 
921
 
 
922
 
class BzrDirPreSplitOut(BzrDir):
923
 
    """A common class for the all-in-one formats."""
924
 
 
925
 
    def __init__(self, _transport, _format):
926
 
        """See BzrDir.__init__."""
927
 
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
928
 
        assert self._format._lock_class == lockable_files.TransportLock
929
 
        assert self._format._lock_file_name == 'branch-lock'
930
 
        self._control_files = lockable_files.LockableFiles(
931
 
                                            self.get_branch_transport(None),
932
 
                                            self._format._lock_file_name,
933
 
                                            self._format._lock_class)
934
 
 
935
 
    def break_lock(self):
936
 
        """Pre-splitout bzrdirs do not suffer from stale locks."""
937
 
        raise NotImplementedError(self.break_lock)
938
 
 
939
 
    def clone(self, url, revision_id=None, force_new_repo=False):
940
 
        """See BzrDir.clone()."""
941
 
        from bzrlib.workingtree import WorkingTreeFormat2
942
 
        self._make_tail(url)
943
 
        result = self._format._initialize_for_clone(url)
944
 
        self.open_repository().clone(result, revision_id=revision_id)
945
 
        from_branch = self.open_branch()
946
 
        from_branch.clone(result, revision_id=revision_id)
947
 
        try:
948
 
            self.open_workingtree().clone(result)
949
 
        except errors.NotLocalUrl:
950
 
            # make a new one, this format always has to have one.
951
 
            try:
952
 
                WorkingTreeFormat2().initialize(result)
953
 
            except errors.NotLocalUrl:
954
 
                # but we cannot do it for remote trees.
955
 
                to_branch = result.open_branch()
956
 
                WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
957
 
        return result
958
 
 
959
 
    def create_branch(self):
960
 
        """See BzrDir.create_branch."""
961
 
        return self.open_branch()
962
 
 
963
 
    def destroy_branch(self):
964
 
        """See BzrDir.destroy_branch."""
965
 
        raise errors.UnsupportedOperation(self.destroy_branch, self)
966
 
 
967
 
    def create_repository(self, shared=False):
968
 
        """See BzrDir.create_repository."""
969
 
        if shared:
970
 
            raise errors.IncompatibleFormat('shared repository', self._format)
971
 
        return self.open_repository()
972
 
 
973
 
    def destroy_repository(self):
974
 
        """See BzrDir.destroy_repository."""
975
 
        raise errors.UnsupportedOperation(self.destroy_repository, self)
976
 
 
977
 
    def create_workingtree(self, revision_id=None, from_branch=None,
978
 
                           accelerator_tree=None, hardlink=False):
979
 
        """See BzrDir.create_workingtree."""
980
 
        # this looks buggy but is not -really-
981
 
        # because this format creates the workingtree when the bzrdir is
982
 
        # created
983
 
        # clone and sprout will have set the revision_id
984
 
        # and that will have set it for us, its only
985
 
        # specific uses of create_workingtree in isolation
986
 
        # that can do wonky stuff here, and that only
987
 
        # happens for creating checkouts, which cannot be 
988
 
        # done on this format anyway. So - acceptable wart.
989
 
        result = self.open_workingtree(recommend_upgrade=False)
990
 
        if revision_id is not None:
991
 
            if revision_id == _mod_revision.NULL_REVISION:
992
 
                result.set_parent_ids([])
993
 
            else:
994
 
                result.set_parent_ids([revision_id])
995
 
        return result
996
 
 
997
 
    def destroy_workingtree(self):
998
 
        """See BzrDir.destroy_workingtree."""
999
 
        raise errors.UnsupportedOperation(self.destroy_workingtree, self)
1000
 
 
1001
 
    def destroy_workingtree_metadata(self):
1002
 
        """See BzrDir.destroy_workingtree_metadata."""
1003
 
        raise errors.UnsupportedOperation(self.destroy_workingtree_metadata, 
1004
 
                                          self)
1005
 
 
1006
 
    def get_branch_transport(self, branch_format):
1007
 
        """See BzrDir.get_branch_transport()."""
1008
 
        if branch_format is None:
1009
 
            return self.transport
1010
 
        try:
1011
 
            branch_format.get_format_string()
1012
 
        except NotImplementedError:
1013
 
            return self.transport
1014
 
        raise errors.IncompatibleFormat(branch_format, self._format)
1015
 
 
1016
 
    def get_repository_transport(self, repository_format):
1017
 
        """See BzrDir.get_repository_transport()."""
1018
 
        if repository_format is None:
1019
 
            return self.transport
1020
 
        try:
1021
 
            repository_format.get_format_string()
1022
 
        except NotImplementedError:
1023
 
            return self.transport
1024
 
        raise errors.IncompatibleFormat(repository_format, self._format)
1025
 
 
1026
 
    def get_workingtree_transport(self, workingtree_format):
1027
 
        """See BzrDir.get_workingtree_transport()."""
1028
 
        if workingtree_format is None:
1029
 
            return self.transport
1030
 
        try:
1031
 
            workingtree_format.get_format_string()
1032
 
        except NotImplementedError:
1033
 
            return self.transport
1034
 
        raise errors.IncompatibleFormat(workingtree_format, self._format)
1035
 
 
1036
 
    def needs_format_conversion(self, format=None):
1037
 
        """See BzrDir.needs_format_conversion()."""
1038
 
        # if the format is not the same as the system default,
1039
 
        # an upgrade is needed.
1040
 
        if format is None:
1041
 
            format = BzrDirFormat.get_default_format()
1042
 
        return not isinstance(self._format, format.__class__)
1043
 
 
1044
 
    def open_branch(self, unsupported=False):
1045
 
        """See BzrDir.open_branch."""
1046
 
        from bzrlib.branch import BzrBranchFormat4
1047
 
        format = BzrBranchFormat4()
1048
 
        self._check_supported(format, unsupported)
1049
 
        return format.open(self, _found=True)
1050
 
 
1051
 
    def sprout(self, url, revision_id=None, force_new_repo=False,
1052
 
               possible_transports=None, accelerator_tree=None,
1053
 
               hardlink=False):
1054
 
        """See BzrDir.sprout()."""
1055
 
        from bzrlib.workingtree import WorkingTreeFormat2
1056
 
        self._make_tail(url)
1057
 
        result = self._format._initialize_for_clone(url)
1058
 
        try:
1059
 
            self.open_repository().clone(result, revision_id=revision_id)
1060
 
        except errors.NoRepositoryPresent:
1061
 
            pass
1062
 
        try:
1063
 
            self.open_branch().sprout(result, revision_id=revision_id)
1064
 
        except errors.NotBranchError:
1065
 
            pass
1066
 
        # we always want a working tree
1067
 
        WorkingTreeFormat2().initialize(result,
1068
 
                                        accelerator_tree=accelerator_tree,
1069
 
                                        hardlink=hardlink)
1070
 
        return result
1071
 
 
1072
 
 
1073
 
class BzrDir4(BzrDirPreSplitOut):
1074
 
    """A .bzr version 4 control object.
1075
 
    
1076
 
    This is a deprecated format and may be removed after sept 2006.
1077
 
    """
1078
 
 
1079
 
    def create_repository(self, shared=False):
1080
 
        """See BzrDir.create_repository."""
1081
 
        return self._format.repository_format.initialize(self, shared)
1082
 
 
1083
 
    def needs_format_conversion(self, format=None):
1084
 
        """Format 4 dirs are always in need of conversion."""
1085
 
        return True
1086
 
 
1087
 
    def open_repository(self):
1088
 
        """See BzrDir.open_repository."""
1089
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
1090
 
        return RepositoryFormat4().open(self, _found=True)
1091
 
 
1092
 
 
1093
 
class BzrDir5(BzrDirPreSplitOut):
1094
 
    """A .bzr version 5 control object.
1095
 
 
1096
 
    This is a deprecated format and may be removed after sept 2006.
1097
 
    """
1098
 
 
1099
 
    def open_repository(self):
1100
 
        """See BzrDir.open_repository."""
1101
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1102
 
        return RepositoryFormat5().open(self, _found=True)
1103
 
 
1104
 
    def open_workingtree(self, _unsupported=False,
1105
 
            recommend_upgrade=True):
1106
 
        """See BzrDir.create_workingtree."""
1107
 
        from bzrlib.workingtree import WorkingTreeFormat2
1108
 
        wt_format = WorkingTreeFormat2()
1109
 
        # we don't warn here about upgrades; that ought to be handled for the
1110
 
        # bzrdir as a whole
1111
 
        return wt_format.open(self, _found=True)
1112
 
 
1113
 
 
1114
 
class BzrDir6(BzrDirPreSplitOut):
1115
 
    """A .bzr version 6 control object.
1116
 
 
1117
 
    This is a deprecated format and may be removed after sept 2006.
1118
 
    """
1119
 
 
1120
 
    def open_repository(self):
1121
 
        """See BzrDir.open_repository."""
1122
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1123
 
        return RepositoryFormat6().open(self, _found=True)
1124
 
 
1125
 
    def open_workingtree(self, _unsupported=False,
1126
 
        recommend_upgrade=True):
1127
 
        """See BzrDir.create_workingtree."""
1128
 
        # we don't warn here about upgrades; that ought to be handled for the
1129
 
        # bzrdir as a whole
1130
 
        from bzrlib.workingtree import WorkingTreeFormat2
1131
 
        return WorkingTreeFormat2().open(self, _found=True)
1132
 
 
1133
 
 
1134
 
class BzrDirMeta1(BzrDir):
1135
 
    """A .bzr meta version 1 control object.
1136
 
    
1137
 
    This is the first control object where the 
1138
 
    individual aspects are really split out: there are separate repository,
1139
 
    workingtree and branch subdirectories and any subset of the three can be
1140
 
    present within a BzrDir.
1141
 
    """
1142
 
 
1143
 
    def can_convert_format(self):
1144
 
        """See BzrDir.can_convert_format()."""
1145
 
        return True
1146
 
 
1147
 
    def create_branch(self):
1148
 
        """See BzrDir.create_branch."""
1149
 
        return self._format.get_branch_format().initialize(self)
1150
 
 
1151
 
    def destroy_branch(self):
1152
 
        """See BzrDir.create_branch."""
1153
 
        self.transport.delete_tree('branch')
1154
 
 
1155
 
    def create_repository(self, shared=False):
1156
 
        """See BzrDir.create_repository."""
1157
 
        return self._format.repository_format.initialize(self, shared)
1158
 
 
1159
 
    def destroy_repository(self):
1160
 
        """See BzrDir.destroy_repository."""
1161
 
        self.transport.delete_tree('repository')
1162
 
 
1163
 
    def create_workingtree(self, revision_id=None, from_branch=None,
1164
 
                           accelerator_tree=None, hardlink=False):
1165
 
        """See BzrDir.create_workingtree."""
1166
 
        return self._format.workingtree_format.initialize(
1167
 
            self, revision_id, from_branch=from_branch,
1168
 
            accelerator_tree=accelerator_tree, hardlink=hardlink)
1169
 
 
1170
 
    def destroy_workingtree(self):
1171
 
        """See BzrDir.destroy_workingtree."""
1172
 
        wt = self.open_workingtree(recommend_upgrade=False)
1173
 
        repository = wt.branch.repository
1174
 
        empty = repository.revision_tree(_mod_revision.NULL_REVISION)
1175
 
        wt.revert(old_tree=empty)
1176
 
        self.destroy_workingtree_metadata()
1177
 
 
1178
 
    def destroy_workingtree_metadata(self):
1179
 
        self.transport.delete_tree('checkout')
1180
 
 
1181
 
    def find_branch_format(self):
1182
 
        """Find the branch 'format' for this bzrdir.
1183
 
 
1184
 
        This might be a synthetic object for e.g. RemoteBranch and SVN.
1185
 
        """
1186
 
        from bzrlib.branch import BranchFormat
1187
 
        return BranchFormat.find_format(self)
1188
 
 
1189
 
    def _get_mkdir_mode(self):
1190
 
        """Figure out the mode to use when creating a bzrdir subdir."""
1191
 
        temp_control = lockable_files.LockableFiles(self.transport, '',
1192
 
                                     lockable_files.TransportLock)
1193
 
        return temp_control._dir_mode
1194
 
 
1195
 
    def get_branch_reference(self):
1196
 
        """See BzrDir.get_branch_reference()."""
1197
 
        from bzrlib.branch import BranchFormat
1198
 
        format = BranchFormat.find_format(self)
1199
 
        return format.get_reference(self)
1200
 
 
1201
 
    def get_branch_transport(self, branch_format):
1202
 
        """See BzrDir.get_branch_transport()."""
1203
 
        if branch_format is None:
1204
 
            return self.transport.clone('branch')
1205
 
        try:
1206
 
            branch_format.get_format_string()
1207
 
        except NotImplementedError:
1208
 
            raise errors.IncompatibleFormat(branch_format, self._format)
1209
 
        try:
1210
 
            self.transport.mkdir('branch', mode=self._get_mkdir_mode())
1211
 
        except errors.FileExists:
1212
 
            pass
1213
 
        return self.transport.clone('branch')
1214
 
 
1215
 
    def get_repository_transport(self, repository_format):
1216
 
        """See BzrDir.get_repository_transport()."""
1217
 
        if repository_format is None:
1218
 
            return self.transport.clone('repository')
1219
 
        try:
1220
 
            repository_format.get_format_string()
1221
 
        except NotImplementedError:
1222
 
            raise errors.IncompatibleFormat(repository_format, self._format)
1223
 
        try:
1224
 
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
1225
 
        except errors.FileExists:
1226
 
            pass
1227
 
        return self.transport.clone('repository')
1228
 
 
1229
 
    def get_workingtree_transport(self, workingtree_format):
1230
 
        """See BzrDir.get_workingtree_transport()."""
1231
 
        if workingtree_format is None:
1232
 
            return self.transport.clone('checkout')
1233
 
        try:
1234
 
            workingtree_format.get_format_string()
1235
 
        except NotImplementedError:
1236
 
            raise errors.IncompatibleFormat(workingtree_format, self._format)
1237
 
        try:
1238
 
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
1239
 
        except errors.FileExists:
1240
 
            pass
1241
 
        return self.transport.clone('checkout')
1242
 
 
1243
 
    def needs_format_conversion(self, format=None):
1244
 
        """See BzrDir.needs_format_conversion()."""
1245
 
        if format is None:
1246
 
            format = BzrDirFormat.get_default_format()
1247
 
        if not isinstance(self._format, format.__class__):
1248
 
            # it is not a meta dir format, conversion is needed.
1249
 
            return True
1250
 
        # we might want to push this down to the repository?
1251
 
        try:
1252
 
            if not isinstance(self.open_repository()._format,
1253
 
                              format.repository_format.__class__):
1254
 
                # the repository needs an upgrade.
1255
 
                return True
1256
 
        except errors.NoRepositoryPresent:
1257
 
            pass
1258
 
        try:
1259
 
            if not isinstance(self.open_branch()._format,
1260
 
                              format.get_branch_format().__class__):
1261
 
                # the branch needs an upgrade.
1262
 
                return True
1263
 
        except errors.NotBranchError:
1264
 
            pass
1265
 
        try:
1266
 
            my_wt = self.open_workingtree(recommend_upgrade=False)
1267
 
            if not isinstance(my_wt._format,
1268
 
                              format.workingtree_format.__class__):
1269
 
                # the workingtree needs an upgrade.
1270
 
                return True
1271
 
        except (errors.NoWorkingTree, errors.NotLocalUrl):
1272
 
            pass
1273
 
        return False
1274
 
 
1275
 
    def open_branch(self, unsupported=False):
1276
 
        """See BzrDir.open_branch."""
1277
 
        format = self.find_branch_format()
1278
 
        self._check_supported(format, unsupported)
1279
 
        return format.open(self, _found=True)
1280
 
 
1281
 
    def open_repository(self, unsupported=False):
1282
 
        """See BzrDir.open_repository."""
1283
 
        from bzrlib.repository import RepositoryFormat
1284
 
        format = RepositoryFormat.find_format(self)
1285
 
        self._check_supported(format, unsupported)
1286
 
        return format.open(self, _found=True)
1287
 
 
1288
 
    def open_workingtree(self, unsupported=False,
1289
 
            recommend_upgrade=True):
1290
 
        """See BzrDir.open_workingtree."""
1291
 
        from bzrlib.workingtree import WorkingTreeFormat
1292
 
        format = WorkingTreeFormat.find_format(self)
1293
 
        self._check_supported(format, unsupported,
1294
 
            recommend_upgrade,
1295
 
            basedir=self.root_transport.base)
1296
 
        return format.open(self, _found=True)
1297
 
 
1298
 
 
1299
 
class BzrDirFormat(object):
1300
 
    """An encapsulation of the initialization and open routines for a format.
1301
 
 
1302
 
    Formats provide three things:
1303
 
     * An initialization routine,
1304
 
     * a format string,
1305
 
     * an open routine.
1306
 
 
1307
 
    Formats are placed in a dict by their format string for reference 
1308
 
    during bzrdir opening. These should be subclasses of BzrDirFormat
1309
 
    for consistency.
1310
 
 
1311
 
    Once a format is deprecated, just deprecate the initialize and open
1312
 
    methods on the format class. Do not deprecate the object, as the 
1313
 
    object will be created every system load.
1314
 
    """
1315
 
 
1316
 
    _default_format = None
1317
 
    """The default format used for new .bzr dirs."""
1318
 
 
1319
 
    _formats = {}
1320
 
    """The known formats."""
1321
 
 
1322
 
    _control_formats = []
1323
 
    """The registered control formats - .bzr, ....
1324
 
    
1325
 
    This is a list of BzrDirFormat objects.
1326
 
    """
1327
 
 
1328
 
    _control_server_formats = []
1329
 
    """The registered control server formats, e.g. RemoteBzrDirs.
1330
 
 
1331
 
    This is a list of BzrDirFormat objects.
1332
 
    """
1333
 
 
1334
 
    _lock_file_name = 'branch-lock'
1335
 
 
1336
 
    # _lock_class must be set in subclasses to the lock type, typ.
1337
 
    # TransportLock or LockDir
1338
 
 
1339
 
    @classmethod
1340
 
    def find_format(klass, transport, _server_formats=True):
1341
 
        """Return the format present at transport."""
1342
 
        if _server_formats:
1343
 
            formats = klass._control_server_formats + klass._control_formats
1344
 
        else:
1345
 
            formats = klass._control_formats
1346
 
        for format in formats:
1347
 
            try:
1348
 
                return format.probe_transport(transport)
1349
 
            except errors.NotBranchError:
1350
 
                # this format does not find a control dir here.
1351
 
                pass
1352
 
        raise errors.NotBranchError(path=transport.base)
1353
 
 
1354
 
    @classmethod
1355
 
    def probe_transport(klass, transport):
1356
 
        """Return the .bzrdir style format present in a directory."""
1357
 
        try:
1358
 
            format_string = transport.get(".bzr/branch-format").read()
1359
 
        except errors.NoSuchFile:
1360
 
            raise errors.NotBranchError(path=transport.base)
1361
 
 
1362
 
        try:
1363
 
            return klass._formats[format_string]
1364
 
        except KeyError:
1365
 
            raise errors.UnknownFormatError(format=format_string)
1366
 
 
1367
 
    @classmethod
1368
 
    def get_default_format(klass):
1369
 
        """Return the current default format."""
1370
 
        return klass._default_format
1371
 
 
1372
 
    def get_format_string(self):
1373
 
        """Return the ASCII format string that identifies this format."""
1374
 
        raise NotImplementedError(self.get_format_string)
1375
 
 
1376
 
    def get_format_description(self):
1377
 
        """Return the short description for this format."""
1378
 
        raise NotImplementedError(self.get_format_description)
1379
 
 
1380
 
    def get_converter(self, format=None):
1381
 
        """Return the converter to use to convert bzrdirs needing converts.
1382
 
 
1383
 
        This returns a bzrlib.bzrdir.Converter object.
1384
 
 
1385
 
        This should return the best upgrader to step this format towards the
1386
 
        current default format. In the case of plugins we can/should provide
1387
 
        some means for them to extend the range of returnable converters.
1388
 
 
1389
 
        :param format: Optional format to override the default format of the 
1390
 
                       library.
1391
 
        """
1392
 
        raise NotImplementedError(self.get_converter)
1393
 
 
1394
 
    def initialize(self, url, possible_transports=None):
1395
 
        """Create a bzr control dir at this url and return an opened copy.
1396
 
        
1397
 
        Subclasses should typically override initialize_on_transport
1398
 
        instead of this method.
1399
 
        """
1400
 
        return self.initialize_on_transport(get_transport(url,
1401
 
                                                          possible_transports))
1402
 
 
1403
 
    def initialize_on_transport(self, transport):
1404
 
        """Initialize a new bzrdir in the base directory of a Transport."""
1405
 
        # Since we don't have a .bzr directory, inherit the
1406
 
        # mode from the root directory
1407
 
        temp_control = lockable_files.LockableFiles(transport,
1408
 
                            '', lockable_files.TransportLock)
1409
 
        temp_control._transport.mkdir('.bzr',
1410
 
                                      # FIXME: RBC 20060121 don't peek under
1411
 
                                      # the covers
1412
 
                                      mode=temp_control._dir_mode)
1413
 
        if sys.platform == 'win32' and isinstance(transport, LocalTransport):
1414
 
            win32utils.set_file_attr_hidden(transport._abspath('.bzr'))
1415
 
        file_mode = temp_control._file_mode
1416
 
        del temp_control
1417
 
        mutter('created control directory in ' + transport.base)
1418
 
        control = transport.clone('.bzr')
1419
 
        utf8_files = [('README', 
1420
 
                       "This is a Bazaar-NG control directory.\n"
1421
 
                       "Do not change any files in this directory.\n"),
1422
 
                      ('branch-format', self.get_format_string()),
1423
 
                      ]
1424
 
        # NB: no need to escape relative paths that are url safe.
1425
 
        control_files = lockable_files.LockableFiles(control,
1426
 
                            self._lock_file_name, self._lock_class)
1427
 
        control_files.create_lock()
1428
 
        control_files.lock_write()
1429
 
        try:
1430
 
            for file, content in utf8_files:
1431
 
                control_files.put_utf8(file, content)
1432
 
        finally:
1433
 
            control_files.unlock()
1434
 
        return self.open(transport, _found=True)
1435
 
 
1436
 
    def is_supported(self):
1437
 
        """Is this format supported?
1438
 
 
1439
 
        Supported formats must be initializable and openable.
1440
 
        Unsupported formats may not support initialization or committing or 
1441
 
        some other features depending on the reason for not being supported.
1442
 
        """
1443
 
        return True
1444
 
 
1445
 
    def same_model(self, target_format):
1446
 
        return (self.repository_format.rich_root_data == 
1447
 
            target_format.rich_root_data)
1448
 
 
1449
 
    @classmethod
1450
 
    def known_formats(klass):
1451
 
        """Return all the known formats.
1452
 
        
1453
 
        Concrete formats should override _known_formats.
1454
 
        """
1455
 
        # There is double indirection here to make sure that control 
1456
 
        # formats used by more than one dir format will only be probed 
1457
 
        # once. This can otherwise be quite expensive for remote connections.
1458
 
        result = set()
1459
 
        for format in klass._control_formats:
1460
 
            result.update(format._known_formats())
1461
 
        return result
1462
 
    
1463
 
    @classmethod
1464
 
    def _known_formats(klass):
1465
 
        """Return the known format instances for this control format."""
1466
 
        return set(klass._formats.values())
1467
 
 
1468
 
    def open(self, transport, _found=False):
1469
 
        """Return an instance of this format for the dir transport points at.
1470
 
        
1471
 
        _found is a private parameter, do not use it.
1472
 
        """
1473
 
        if not _found:
1474
 
            found_format = BzrDirFormat.find_format(transport)
1475
 
            if not isinstance(found_format, self.__class__):
1476
 
                raise AssertionError("%s was asked to open %s, but it seems to need "
1477
 
                        "format %s" 
1478
 
                        % (self, transport, found_format))
1479
 
        return self._open(transport)
1480
 
 
1481
 
    def _open(self, transport):
1482
 
        """Template method helper for opening BzrDirectories.
1483
 
 
1484
 
        This performs the actual open and any additional logic or parameter
1485
 
        passing.
1486
 
        """
1487
 
        raise NotImplementedError(self._open)
1488
 
 
1489
 
    @classmethod
1490
 
    def register_format(klass, format):
1491
 
        klass._formats[format.get_format_string()] = format
1492
 
 
1493
 
    @classmethod
1494
 
    def register_control_format(klass, format):
1495
 
        """Register a format that does not use '.bzr' for its control dir.
1496
 
 
1497
 
        TODO: This should be pulled up into a 'ControlDirFormat' base class
1498
 
        which BzrDirFormat can inherit from, and renamed to register_format 
1499
 
        there. It has been done without that for now for simplicity of
1500
 
        implementation.
1501
 
        """
1502
 
        klass._control_formats.append(format)
1503
 
 
1504
 
    @classmethod
1505
 
    def register_control_server_format(klass, format):
1506
 
        """Register a control format for client-server environments.
1507
 
 
1508
 
        These formats will be tried before ones registered with
1509
 
        register_control_format.  This gives implementations that decide to the
1510
 
        chance to grab it before anything looks at the contents of the format
1511
 
        file.
1512
 
        """
1513
 
        klass._control_server_formats.append(format)
1514
 
 
1515
 
    @classmethod
1516
 
    @symbol_versioning.deprecated_method(symbol_versioning.zero_fourteen)
1517
 
    def set_default_format(klass, format):
1518
 
        klass._set_default_format(format)
1519
 
 
1520
 
    @classmethod
1521
 
    def _set_default_format(klass, format):
1522
 
        """Set default format (for testing behavior of defaults only)"""
1523
 
        klass._default_format = format
1524
 
 
1525
 
    def __str__(self):
1526
 
        # Trim the newline
1527
 
        return self.get_format_string().rstrip()
1528
 
 
1529
 
    @classmethod
1530
 
    def unregister_format(klass, format):
1531
 
        assert klass._formats[format.get_format_string()] is format
1532
 
        del klass._formats[format.get_format_string()]
1533
 
 
1534
 
    @classmethod
1535
 
    def unregister_control_format(klass, format):
1536
 
        klass._control_formats.remove(format)
1537
 
 
1538
 
 
1539
 
class BzrDirFormat4(BzrDirFormat):
1540
 
    """Bzr dir format 4.
1541
 
 
1542
 
    This format is a combined format for working tree, branch and repository.
1543
 
    It has:
1544
 
     - Format 1 working trees [always]
1545
 
     - Format 4 branches [always]
1546
 
     - Format 4 repositories [always]
1547
 
 
1548
 
    This format is deprecated: it indexes texts using a text it which is
1549
 
    removed in format 5; write support for this format has been removed.
1550
 
    """
1551
 
 
1552
 
    _lock_class = lockable_files.TransportLock
1553
 
 
1554
 
    def get_format_string(self):
1555
 
        """See BzrDirFormat.get_format_string()."""
1556
 
        return "Bazaar-NG branch, format 0.0.4\n"
1557
 
 
1558
 
    def get_format_description(self):
1559
 
        """See BzrDirFormat.get_format_description()."""
1560
 
        return "All-in-one format 4"
1561
 
 
1562
 
    def get_converter(self, format=None):
1563
 
        """See BzrDirFormat.get_converter()."""
1564
 
        # there is one and only one upgrade path here.
1565
 
        return ConvertBzrDir4To5()
1566
 
        
1567
 
    def initialize_on_transport(self, transport):
1568
 
        """Format 4 branches cannot be created."""
1569
 
        raise errors.UninitializableFormat(self)
1570
 
 
1571
 
    def is_supported(self):
1572
 
        """Format 4 is not supported.
1573
 
 
1574
 
        It is not supported because the model changed from 4 to 5 and the
1575
 
        conversion logic is expensive - so doing it on the fly was not 
1576
 
        feasible.
1577
 
        """
1578
 
        return False
1579
 
 
1580
 
    def _open(self, transport):
1581
 
        """See BzrDirFormat._open."""
1582
 
        return BzrDir4(transport, self)
1583
 
 
1584
 
    def __return_repository_format(self):
1585
 
        """Circular import protection."""
1586
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
1587
 
        return RepositoryFormat4()
1588
 
    repository_format = property(__return_repository_format)
1589
 
 
1590
 
 
1591
 
class BzrDirFormat5(BzrDirFormat):
1592
 
    """Bzr control format 5.
1593
 
 
1594
 
    This format is a combined format for working tree, branch and repository.
1595
 
    It has:
1596
 
     - Format 2 working trees [always] 
1597
 
     - Format 4 branches [always] 
1598
 
     - Format 5 repositories [always]
1599
 
       Unhashed stores in the repository.
1600
 
    """
1601
 
 
1602
 
    _lock_class = lockable_files.TransportLock
1603
 
 
1604
 
    def get_format_string(self):
1605
 
        """See BzrDirFormat.get_format_string()."""
1606
 
        return "Bazaar-NG branch, format 5\n"
1607
 
 
1608
 
    def get_format_description(self):
1609
 
        """See BzrDirFormat.get_format_description()."""
1610
 
        return "All-in-one format 5"
1611
 
 
1612
 
    def get_converter(self, format=None):
1613
 
        """See BzrDirFormat.get_converter()."""
1614
 
        # there is one and only one upgrade path here.
1615
 
        return ConvertBzrDir5To6()
1616
 
 
1617
 
    def _initialize_for_clone(self, url):
1618
 
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1619
 
        
1620
 
    def initialize_on_transport(self, transport, _cloning=False):
1621
 
        """Format 5 dirs always have working tree, branch and repository.
1622
 
        
1623
 
        Except when they are being cloned.
1624
 
        """
1625
 
        from bzrlib.branch import BzrBranchFormat4
1626
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1627
 
        from bzrlib.workingtree import WorkingTreeFormat2
1628
 
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1629
 
        RepositoryFormat5().initialize(result, _internal=True)
1630
 
        if not _cloning:
1631
 
            branch = BzrBranchFormat4().initialize(result)
1632
 
            try:
1633
 
                WorkingTreeFormat2().initialize(result)
1634
 
            except errors.NotLocalUrl:
1635
 
                # Even though we can't access the working tree, we need to
1636
 
                # create its control files.
1637
 
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1638
 
        return result
1639
 
 
1640
 
    def _open(self, transport):
1641
 
        """See BzrDirFormat._open."""
1642
 
        return BzrDir5(transport, self)
1643
 
 
1644
 
    def __return_repository_format(self):
1645
 
        """Circular import protection."""
1646
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1647
 
        return RepositoryFormat5()
1648
 
    repository_format = property(__return_repository_format)
1649
 
 
1650
 
 
1651
 
class BzrDirFormat6(BzrDirFormat):
1652
 
    """Bzr control format 6.
1653
 
 
1654
 
    This format is a combined format for working tree, branch and repository.
1655
 
    It has:
1656
 
     - Format 2 working trees [always] 
1657
 
     - Format 4 branches [always] 
1658
 
     - Format 6 repositories [always]
1659
 
    """
1660
 
 
1661
 
    _lock_class = lockable_files.TransportLock
1662
 
 
1663
 
    def get_format_string(self):
1664
 
        """See BzrDirFormat.get_format_string()."""
1665
 
        return "Bazaar-NG branch, format 6\n"
1666
 
 
1667
 
    def get_format_description(self):
1668
 
        """See BzrDirFormat.get_format_description()."""
1669
 
        return "All-in-one format 6"
1670
 
 
1671
 
    def get_converter(self, format=None):
1672
 
        """See BzrDirFormat.get_converter()."""
1673
 
        # there is one and only one upgrade path here.
1674
 
        return ConvertBzrDir6ToMeta()
1675
 
        
1676
 
    def _initialize_for_clone(self, url):
1677
 
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1678
 
 
1679
 
    def initialize_on_transport(self, transport, _cloning=False):
1680
 
        """Format 6 dirs always have working tree, branch and repository.
1681
 
        
1682
 
        Except when they are being cloned.
1683
 
        """
1684
 
        from bzrlib.branch import BzrBranchFormat4
1685
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1686
 
        from bzrlib.workingtree import WorkingTreeFormat2
1687
 
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1688
 
        RepositoryFormat6().initialize(result, _internal=True)
1689
 
        if not _cloning:
1690
 
            branch = BzrBranchFormat4().initialize(result)
1691
 
            try:
1692
 
                WorkingTreeFormat2().initialize(result)
1693
 
            except errors.NotLocalUrl:
1694
 
                # Even though we can't access the working tree, we need to
1695
 
                # create its control files.
1696
 
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1697
 
        return result
1698
 
 
1699
 
    def _open(self, transport):
1700
 
        """See BzrDirFormat._open."""
1701
 
        return BzrDir6(transport, self)
1702
 
 
1703
 
    def __return_repository_format(self):
1704
 
        """Circular import protection."""
1705
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1706
 
        return RepositoryFormat6()
1707
 
    repository_format = property(__return_repository_format)
1708
 
 
1709
 
 
1710
 
class BzrDirMetaFormat1(BzrDirFormat):
1711
 
    """Bzr meta control format 1
1712
 
 
1713
 
    This is the first format with split out working tree, branch and repository
1714
 
    disk storage.
1715
 
    It has:
1716
 
     - Format 3 working trees [optional]
1717
 
     - Format 5 branches [optional]
1718
 
     - Format 7 repositories [optional]
1719
 
    """
1720
 
 
1721
 
    _lock_class = lockdir.LockDir
1722
 
 
1723
 
    def __init__(self):
1724
 
        self._workingtree_format = None
1725
 
        self._branch_format = None
1726
 
 
1727
 
    def __eq__(self, other):
1728
 
        if other.__class__ is not self.__class__:
1729
 
            return False
1730
 
        if other.repository_format != self.repository_format:
1731
 
            return False
1732
 
        if other.workingtree_format != self.workingtree_format:
1733
 
            return False
1734
 
        return True
1735
 
 
1736
 
    def __ne__(self, other):
1737
 
        return not self == other
1738
 
 
1739
 
    def get_branch_format(self):
1740
 
        if self._branch_format is None:
1741
 
            from bzrlib.branch import BranchFormat
1742
 
            self._branch_format = BranchFormat.get_default_format()
1743
 
        return self._branch_format
1744
 
 
1745
 
    def set_branch_format(self, format):
1746
 
        self._branch_format = format
1747
 
 
1748
 
    def get_converter(self, format=None):
1749
 
        """See BzrDirFormat.get_converter()."""
1750
 
        if format is None:
1751
 
            format = BzrDirFormat.get_default_format()
1752
 
        if not isinstance(self, format.__class__):
1753
 
            # converting away from metadir is not implemented
1754
 
            raise NotImplementedError(self.get_converter)
1755
 
        return ConvertMetaToMeta(format)
1756
 
 
1757
 
    def get_format_string(self):
1758
 
        """See BzrDirFormat.get_format_string()."""
1759
 
        return "Bazaar-NG meta directory, format 1\n"
1760
 
 
1761
 
    def get_format_description(self):
1762
 
        """See BzrDirFormat.get_format_description()."""
1763
 
        return "Meta directory format 1"
1764
 
 
1765
 
    def _open(self, transport):
1766
 
        """See BzrDirFormat._open."""
1767
 
        return BzrDirMeta1(transport, self)
1768
 
 
1769
 
    def __return_repository_format(self):
1770
 
        """Circular import protection."""
1771
 
        if getattr(self, '_repository_format', None):
1772
 
            return self._repository_format
1773
 
        from bzrlib.repository import RepositoryFormat
1774
 
        return RepositoryFormat.get_default_format()
1775
 
 
1776
 
    def __set_repository_format(self, value):
1777
 
        """Allow changing the repository format for metadir formats."""
1778
 
        self._repository_format = value
1779
 
 
1780
 
    repository_format = property(__return_repository_format, __set_repository_format)
1781
 
 
1782
 
    def __get_workingtree_format(self):
1783
 
        if self._workingtree_format is None:
1784
 
            from bzrlib.workingtree import WorkingTreeFormat
1785
 
            self._workingtree_format = WorkingTreeFormat.get_default_format()
1786
 
        return self._workingtree_format
1787
 
 
1788
 
    def __set_workingtree_format(self, wt_format):
1789
 
        self._workingtree_format = wt_format
1790
 
 
1791
 
    workingtree_format = property(__get_workingtree_format,
1792
 
                                  __set_workingtree_format)
1793
 
 
1794
 
 
1795
 
# Register bzr control format
1796
 
BzrDirFormat.register_control_format(BzrDirFormat)
1797
 
 
1798
 
# Register bzr formats
1799
 
BzrDirFormat.register_format(BzrDirFormat4())
1800
 
BzrDirFormat.register_format(BzrDirFormat5())
1801
 
BzrDirFormat.register_format(BzrDirFormat6())
1802
 
__default_format = BzrDirMetaFormat1()
1803
 
BzrDirFormat.register_format(__default_format)
1804
 
BzrDirFormat._default_format = __default_format
1805
 
 
1806
 
 
1807
 
class Converter(object):
1808
 
    """Converts a disk format object from one format to another."""
1809
 
 
1810
 
    def convert(self, to_convert, pb):
1811
 
        """Perform the conversion of to_convert, giving feedback via pb.
1812
 
 
1813
 
        :param to_convert: The disk object to convert.
1814
 
        :param pb: a progress bar to use for progress information.
1815
 
        """
1816
 
 
1817
 
    def step(self, message):
1818
 
        """Update the pb by a step."""
1819
 
        self.count +=1
1820
 
        self.pb.update(message, self.count, self.total)
1821
 
 
1822
 
 
1823
 
class ConvertBzrDir4To5(Converter):
1824
 
    """Converts format 4 bzr dirs to format 5."""
1825
 
 
1826
 
    def __init__(self):
1827
 
        super(ConvertBzrDir4To5, self).__init__()
1828
 
        self.converted_revs = set()
1829
 
        self.absent_revisions = set()
1830
 
        self.text_count = 0
1831
 
        self.revisions = {}
1832
 
        
1833
 
    def convert(self, to_convert, pb):
1834
 
        """See Converter.convert()."""
1835
 
        self.bzrdir = to_convert
1836
 
        self.pb = pb
1837
 
        self.pb.note('starting upgrade from format 4 to 5')
1838
 
        if isinstance(self.bzrdir.transport, LocalTransport):
1839
 
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1840
 
        self._convert_to_weaves()
1841
 
        return BzrDir.open(self.bzrdir.root_transport.base)
1842
 
 
1843
 
    def _convert_to_weaves(self):
1844
 
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1845
 
        try:
1846
 
            # TODO permissions
1847
 
            stat = self.bzrdir.transport.stat('weaves')
1848
 
            if not S_ISDIR(stat.st_mode):
1849
 
                self.bzrdir.transport.delete('weaves')
1850
 
                self.bzrdir.transport.mkdir('weaves')
1851
 
        except errors.NoSuchFile:
1852
 
            self.bzrdir.transport.mkdir('weaves')
1853
 
        # deliberately not a WeaveFile as we want to build it up slowly.
1854
 
        self.inv_weave = Weave('inventory')
1855
 
        # holds in-memory weaves for all files
1856
 
        self.text_weaves = {}
1857
 
        self.bzrdir.transport.delete('branch-format')
1858
 
        self.branch = self.bzrdir.open_branch()
1859
 
        self._convert_working_inv()
1860
 
        rev_history = self.branch.revision_history()
1861
 
        # to_read is a stack holding the revisions we still need to process;
1862
 
        # appending to it adds new highest-priority revisions
1863
 
        self.known_revisions = set(rev_history)
1864
 
        self.to_read = rev_history[-1:]
1865
 
        while self.to_read:
1866
 
            rev_id = self.to_read.pop()
1867
 
            if (rev_id not in self.revisions
1868
 
                and rev_id not in self.absent_revisions):
1869
 
                self._load_one_rev(rev_id)
1870
 
        self.pb.clear()
1871
 
        to_import = self._make_order()
1872
 
        for i, rev_id in enumerate(to_import):
1873
 
            self.pb.update('converting revision', i, len(to_import))
1874
 
            self._convert_one_rev(rev_id)
1875
 
        self.pb.clear()
1876
 
        self._write_all_weaves()
1877
 
        self._write_all_revs()
1878
 
        self.pb.note('upgraded to weaves:')
1879
 
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
1880
 
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
1881
 
        self.pb.note('  %6d texts', self.text_count)
1882
 
        self._cleanup_spare_files_after_format4()
1883
 
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1884
 
 
1885
 
    def _cleanup_spare_files_after_format4(self):
1886
 
        # FIXME working tree upgrade foo.
1887
 
        for n in 'merged-patches', 'pending-merged-patches':
1888
 
            try:
1889
 
                ## assert os.path.getsize(p) == 0
1890
 
                self.bzrdir.transport.delete(n)
1891
 
            except errors.NoSuchFile:
1892
 
                pass
1893
 
        self.bzrdir.transport.delete_tree('inventory-store')
1894
 
        self.bzrdir.transport.delete_tree('text-store')
1895
 
 
1896
 
    def _convert_working_inv(self):
1897
 
        inv = xml4.serializer_v4.read_inventory(
1898
 
                    self.branch.control_files.get('inventory'))
1899
 
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv, working=True)
1900
 
        # FIXME inventory is a working tree change.
1901
 
        self.branch.control_files.put('inventory', StringIO(new_inv_xml))
1902
 
 
1903
 
    def _write_all_weaves(self):
1904
 
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1905
 
        weave_transport = self.bzrdir.transport.clone('weaves')
1906
 
        weaves = WeaveStore(weave_transport, prefixed=False)
1907
 
        transaction = WriteTransaction()
1908
 
 
1909
 
        try:
1910
 
            i = 0
1911
 
            for file_id, file_weave in self.text_weaves.items():
1912
 
                self.pb.update('writing weave', i, len(self.text_weaves))
1913
 
                weaves._put_weave(file_id, file_weave, transaction)
1914
 
                i += 1
1915
 
            self.pb.update('inventory', 0, 1)
1916
 
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
1917
 
            self.pb.update('inventory', 1, 1)
1918
 
        finally:
1919
 
            self.pb.clear()
1920
 
 
1921
 
    def _write_all_revs(self):
1922
 
        """Write all revisions out in new form."""
1923
 
        self.bzrdir.transport.delete_tree('revision-store')
1924
 
        self.bzrdir.transport.mkdir('revision-store')
1925
 
        revision_transport = self.bzrdir.transport.clone('revision-store')
1926
 
        # TODO permissions
1927
 
        _revision_store = TextRevisionStore(TextStore(revision_transport,
1928
 
                                                      prefixed=False,
1929
 
                                                      compressed=True))
1930
 
        try:
1931
 
            transaction = WriteTransaction()
1932
 
            for i, rev_id in enumerate(self.converted_revs):
1933
 
                self.pb.update('write revision', i, len(self.converted_revs))
1934
 
                _revision_store.add_revision(self.revisions[rev_id], transaction)
1935
 
        finally:
1936
 
            self.pb.clear()
1937
 
            
1938
 
    def _load_one_rev(self, rev_id):
1939
 
        """Load a revision object into memory.
1940
 
 
1941
 
        Any parents not either loaded or abandoned get queued to be
1942
 
        loaded."""
1943
 
        self.pb.update('loading revision',
1944
 
                       len(self.revisions),
1945
 
                       len(self.known_revisions))
1946
 
        if not self.branch.repository.has_revision(rev_id):
1947
 
            self.pb.clear()
1948
 
            self.pb.note('revision {%s} not present in branch; '
1949
 
                         'will be converted as a ghost',
1950
 
                         rev_id)
1951
 
            self.absent_revisions.add(rev_id)
1952
 
        else:
1953
 
            rev = self.branch.repository._revision_store.get_revision(rev_id,
1954
 
                self.branch.repository.get_transaction())
1955
 
            for parent_id in rev.parent_ids:
1956
 
                self.known_revisions.add(parent_id)
1957
 
                self.to_read.append(parent_id)
1958
 
            self.revisions[rev_id] = rev
1959
 
 
1960
 
    def _load_old_inventory(self, rev_id):
1961
 
        assert rev_id not in self.converted_revs
1962
 
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1963
 
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
1964
 
        inv.revision_id = rev_id
1965
 
        rev = self.revisions[rev_id]
1966
 
        if rev.inventory_sha1:
1967
 
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1968
 
                'inventory sha mismatch for {%s}' % rev_id
1969
 
        return inv
1970
 
 
1971
 
    def _load_updated_inventory(self, rev_id):
1972
 
        assert rev_id in self.converted_revs
1973
 
        inv_xml = self.inv_weave.get_text(rev_id)
1974
 
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml)
1975
 
        return inv
1976
 
 
1977
 
    def _convert_one_rev(self, rev_id):
1978
 
        """Convert revision and all referenced objects to new format."""
1979
 
        rev = self.revisions[rev_id]
1980
 
        inv = self._load_old_inventory(rev_id)
1981
 
        present_parents = [p for p in rev.parent_ids
1982
 
                           if p not in self.absent_revisions]
1983
 
        self._convert_revision_contents(rev, inv, present_parents)
1984
 
        self._store_new_inv(rev, inv, present_parents)
1985
 
        self.converted_revs.add(rev_id)
1986
 
 
1987
 
    def _store_new_inv(self, rev, inv, present_parents):
1988
 
        # the XML is now updated with text versions
1989
 
        if __debug__:
1990
 
            entries = inv.iter_entries()
1991
 
            entries.next()
1992
 
            for path, ie in entries:
1993
 
                assert getattr(ie, 'revision', None) is not None, \
1994
 
                    'no revision on {%s} in {%s}' % \
1995
 
                    (file_id, rev.revision_id)
1996
 
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
1997
 
        new_inv_sha1 = sha_string(new_inv_xml)
1998
 
        self.inv_weave.add_lines(rev.revision_id,
1999
 
                                 present_parents,
2000
 
                                 new_inv_xml.splitlines(True))
2001
 
        rev.inventory_sha1 = new_inv_sha1
2002
 
 
2003
 
    def _convert_revision_contents(self, rev, inv, present_parents):
2004
 
        """Convert all the files within a revision.
2005
 
 
2006
 
        Also upgrade the inventory to refer to the text revision ids."""
2007
 
        rev_id = rev.revision_id
2008
 
        mutter('converting texts of revision {%s}',
2009
 
               rev_id)
2010
 
        parent_invs = map(self._load_updated_inventory, present_parents)
2011
 
        entries = inv.iter_entries()
2012
 
        entries.next()
2013
 
        for path, ie in entries:
2014
 
            self._convert_file_version(rev, ie, parent_invs)
2015
 
 
2016
 
    def _convert_file_version(self, rev, ie, parent_invs):
2017
 
        """Convert one version of one file.
2018
 
 
2019
 
        The file needs to be added into the weave if it is a merge
2020
 
        of >=2 parents or if it's changed from its parent.
2021
 
        """
2022
 
        file_id = ie.file_id
2023
 
        rev_id = rev.revision_id
2024
 
        w = self.text_weaves.get(file_id)
2025
 
        if w is None:
2026
 
            w = Weave(file_id)
2027
 
            self.text_weaves[file_id] = w
2028
 
        text_changed = False
2029
 
        parent_candiate_entries = ie.parent_candidates(parent_invs)
2030
 
        for old_revision in parent_candiate_entries.keys():
2031
 
            # if this fails, its a ghost ?
2032
 
            assert old_revision in self.converted_revs, \
2033
 
                "Revision {%s} not in converted_revs" % old_revision
2034
 
        heads = graph.Graph(self).heads(parent_candiate_entries.keys())
2035
 
        # XXX: Note that this is unordered - and this is tolerable because 
2036
 
        # the previous code was also unordered.
2037
 
        previous_entries = dict((head, parent_candiate_entries[head]) for head
2038
 
            in heads)
2039
 
        self.snapshot_ie(previous_entries, ie, w, rev_id)
2040
 
        del ie.text_id
2041
 
        assert getattr(ie, 'revision', None) is not None
2042
 
 
2043
 
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
2044
 
    def get_parents(self, revision_ids):
2045
 
        for revision_id in revision_ids:
2046
 
            yield self.revisions[revision_id].parent_ids
2047
 
 
2048
 
    def get_parent_map(self, revision_ids):
2049
 
        """See graph._StackedParentsProvider.get_parent_map"""
2050
 
        return dict((revision_id, self.revisions[revision_id])
2051
 
                    for revision_id in revision_ids
2052
 
                     if revision_id in self.revisions)
2053
 
 
2054
 
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
2055
 
        # TODO: convert this logic, which is ~= snapshot to
2056
 
        # a call to:. This needs the path figured out. rather than a work_tree
2057
 
        # a v4 revision_tree can be given, or something that looks enough like
2058
 
        # one to give the file content to the entry if it needs it.
2059
 
        # and we need something that looks like a weave store for snapshot to 
2060
 
        # save against.
2061
 
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
2062
 
        if len(previous_revisions) == 1:
2063
 
            previous_ie = previous_revisions.values()[0]
2064
 
            if ie._unchanged(previous_ie):
2065
 
                ie.revision = previous_ie.revision
2066
 
                return
2067
 
        if ie.has_text():
2068
 
            text = self.branch.repository.weave_store.get(ie.text_id)
2069
 
            file_lines = text.readlines()
2070
 
            assert sha_strings(file_lines) == ie.text_sha1
2071
 
            assert sum(map(len, file_lines)) == ie.text_size
2072
 
            w.add_lines(rev_id, previous_revisions, file_lines)
2073
 
            self.text_count += 1
2074
 
        else:
2075
 
            w.add_lines(rev_id, previous_revisions, [])
2076
 
        ie.revision = rev_id
2077
 
 
2078
 
    def _make_order(self):
2079
 
        """Return a suitable order for importing revisions.
2080
 
 
2081
 
        The order must be such that an revision is imported after all
2082
 
        its (present) parents.
2083
 
        """
2084
 
        todo = set(self.revisions.keys())
2085
 
        done = self.absent_revisions.copy()
2086
 
        order = []
2087
 
        while todo:
2088
 
            # scan through looking for a revision whose parents
2089
 
            # are all done
2090
 
            for rev_id in sorted(list(todo)):
2091
 
                rev = self.revisions[rev_id]
2092
 
                parent_ids = set(rev.parent_ids)
2093
 
                if parent_ids.issubset(done):
2094
 
                    # can take this one now
2095
 
                    order.append(rev_id)
2096
 
                    todo.remove(rev_id)
2097
 
                    done.add(rev_id)
2098
 
        return order
2099
 
 
2100
 
 
2101
 
class ConvertBzrDir5To6(Converter):
2102
 
    """Converts format 5 bzr dirs to format 6."""
2103
 
 
2104
 
    def convert(self, to_convert, pb):
2105
 
        """See Converter.convert()."""
2106
 
        self.bzrdir = to_convert
2107
 
        self.pb = pb
2108
 
        self.pb.note('starting upgrade from format 5 to 6')
2109
 
        self._convert_to_prefixed()
2110
 
        return BzrDir.open(self.bzrdir.root_transport.base)
2111
 
 
2112
 
    def _convert_to_prefixed(self):
2113
 
        from bzrlib.store import TransportStore
2114
 
        self.bzrdir.transport.delete('branch-format')
2115
 
        for store_name in ["weaves", "revision-store"]:
2116
 
            self.pb.note("adding prefixes to %s" % store_name)
2117
 
            store_transport = self.bzrdir.transport.clone(store_name)
2118
 
            store = TransportStore(store_transport, prefixed=True)
2119
 
            for urlfilename in store_transport.list_dir('.'):
2120
 
                filename = urlutils.unescape(urlfilename)
2121
 
                if (filename.endswith(".weave") or
2122
 
                    filename.endswith(".gz") or
2123
 
                    filename.endswith(".sig")):
2124
 
                    file_id = os.path.splitext(filename)[0]
2125
 
                else:
2126
 
                    file_id = filename
2127
 
                prefix_dir = store.hash_prefix(file_id)
2128
 
                # FIXME keep track of the dirs made RBC 20060121
2129
 
                try:
2130
 
                    store_transport.move(filename, prefix_dir + '/' + filename)
2131
 
                except errors.NoSuchFile: # catches missing dirs strangely enough
2132
 
                    store_transport.mkdir(prefix_dir)
2133
 
                    store_transport.move(filename, prefix_dir + '/' + filename)
2134
 
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
2135
 
 
2136
 
 
2137
 
class ConvertBzrDir6ToMeta(Converter):
2138
 
    """Converts format 6 bzr dirs to metadirs."""
2139
 
 
2140
 
    def convert(self, to_convert, pb):
2141
 
        """See Converter.convert()."""
2142
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat7
2143
 
        from bzrlib.branch import BzrBranchFormat5
2144
 
        self.bzrdir = to_convert
2145
 
        self.pb = pb
2146
 
        self.count = 0
2147
 
        self.total = 20 # the steps we know about
2148
 
        self.garbage_inventories = []
2149
 
 
2150
 
        self.pb.note('starting upgrade from format 6 to metadir')
2151
 
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
2152
 
        # its faster to move specific files around than to open and use the apis...
2153
 
        # first off, nuke ancestry.weave, it was never used.
2154
 
        try:
2155
 
            self.step('Removing ancestry.weave')
2156
 
            self.bzrdir.transport.delete('ancestry.weave')
2157
 
        except errors.NoSuchFile:
2158
 
            pass
2159
 
        # find out whats there
2160
 
        self.step('Finding branch files')
2161
 
        last_revision = self.bzrdir.open_branch().last_revision()
2162
 
        bzrcontents = self.bzrdir.transport.list_dir('.')
2163
 
        for name in bzrcontents:
2164
 
            if name.startswith('basis-inventory.'):
2165
 
                self.garbage_inventories.append(name)
2166
 
        # create new directories for repository, working tree and branch
2167
 
        self.dir_mode = self.bzrdir._control_files._dir_mode
2168
 
        self.file_mode = self.bzrdir._control_files._file_mode
2169
 
        repository_names = [('inventory.weave', True),
2170
 
                            ('revision-store', True),
2171
 
                            ('weaves', True)]
2172
 
        self.step('Upgrading repository  ')
2173
 
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
2174
 
        self.make_lock('repository')
2175
 
        # we hard code the formats here because we are converting into
2176
 
        # the meta format. The meta format upgrader can take this to a 
2177
 
        # future format within each component.
2178
 
        self.put_format('repository', RepositoryFormat7())
2179
 
        for entry in repository_names:
2180
 
            self.move_entry('repository', entry)
2181
 
 
2182
 
        self.step('Upgrading branch      ')
2183
 
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
2184
 
        self.make_lock('branch')
2185
 
        self.put_format('branch', BzrBranchFormat5())
2186
 
        branch_files = [('revision-history', True),
2187
 
                        ('branch-name', True),
2188
 
                        ('parent', False)]
2189
 
        for entry in branch_files:
2190
 
            self.move_entry('branch', entry)
2191
 
 
2192
 
        checkout_files = [('pending-merges', True),
2193
 
                          ('inventory', True),
2194
 
                          ('stat-cache', False)]
2195
 
        # If a mandatory checkout file is not present, the branch does not have
2196
 
        # a functional checkout. Do not create a checkout in the converted
2197
 
        # branch.
2198
 
        for name, mandatory in checkout_files:
2199
 
            if mandatory and name not in bzrcontents:
2200
 
                has_checkout = False
2201
 
                break
2202
 
        else:
2203
 
            has_checkout = True
2204
 
        if not has_checkout:
2205
 
            self.pb.note('No working tree.')
2206
 
            # If some checkout files are there, we may as well get rid of them.
2207
 
            for name, mandatory in checkout_files:
2208
 
                if name in bzrcontents:
2209
 
                    self.bzrdir.transport.delete(name)
2210
 
        else:
2211
 
            from bzrlib.workingtree import WorkingTreeFormat3
2212
 
            self.step('Upgrading working tree')
2213
 
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
2214
 
            self.make_lock('checkout')
2215
 
            self.put_format(
2216
 
                'checkout', WorkingTreeFormat3())
2217
 
            self.bzrdir.transport.delete_multi(
2218
 
                self.garbage_inventories, self.pb)
2219
 
            for entry in checkout_files:
2220
 
                self.move_entry('checkout', entry)
2221
 
            if last_revision is not None:
2222
 
                self.bzrdir._control_files.put_utf8(
2223
 
                    'checkout/last-revision', last_revision)
2224
 
        self.bzrdir._control_files.put_utf8(
2225
 
            'branch-format', BzrDirMetaFormat1().get_format_string())
2226
 
        return BzrDir.open(self.bzrdir.root_transport.base)
2227
 
 
2228
 
    def make_lock(self, name):
2229
 
        """Make a lock for the new control dir name."""
2230
 
        self.step('Make %s lock' % name)
2231
 
        ld = lockdir.LockDir(self.bzrdir.transport,
2232
 
                             '%s/lock' % name,
2233
 
                             file_modebits=self.file_mode,
2234
 
                             dir_modebits=self.dir_mode)
2235
 
        ld.create()
2236
 
 
2237
 
    def move_entry(self, new_dir, entry):
2238
 
        """Move then entry name into new_dir."""
2239
 
        name = entry[0]
2240
 
        mandatory = entry[1]
2241
 
        self.step('Moving %s' % name)
2242
 
        try:
2243
 
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
2244
 
        except errors.NoSuchFile:
2245
 
            if mandatory:
2246
 
                raise
2247
 
 
2248
 
    def put_format(self, dirname, format):
2249
 
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
2250
 
 
2251
 
 
2252
 
class ConvertMetaToMeta(Converter):
2253
 
    """Converts the components of metadirs."""
2254
 
 
2255
 
    def __init__(self, target_format):
2256
 
        """Create a metadir to metadir converter.
2257
 
 
2258
 
        :param target_format: The final metadir format that is desired.
2259
 
        """
2260
 
        self.target_format = target_format
2261
 
 
2262
 
    def convert(self, to_convert, pb):
2263
 
        """See Converter.convert()."""
2264
 
        self.bzrdir = to_convert
2265
 
        self.pb = pb
2266
 
        self.count = 0
2267
 
        self.total = 1
2268
 
        self.step('checking repository format')
2269
 
        try:
2270
 
            repo = self.bzrdir.open_repository()
2271
 
        except errors.NoRepositoryPresent:
2272
 
            pass
2273
 
        else:
2274
 
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
2275
 
                from bzrlib.repository import CopyConverter
2276
 
                self.pb.note('starting repository conversion')
2277
 
                converter = CopyConverter(self.target_format.repository_format)
2278
 
                converter.convert(repo, pb)
2279
 
        try:
2280
 
            branch = self.bzrdir.open_branch()
2281
 
        except errors.NotBranchError:
2282
 
            pass
2283
 
        else:
2284
 
            # TODO: conversions of Branch and Tree should be done by
2285
 
            # InterXFormat lookups
2286
 
            # Avoid circular imports
2287
 
            from bzrlib import branch as _mod_branch
2288
 
            if (branch._format.__class__ is _mod_branch.BzrBranchFormat5 and
2289
 
                self.target_format.get_branch_format().__class__ is
2290
 
                _mod_branch.BzrBranchFormat6):
2291
 
                branch_converter = _mod_branch.Converter5to6()
2292
 
                branch_converter.convert(branch)
2293
 
        try:
2294
 
            tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2295
 
        except (errors.NoWorkingTree, errors.NotLocalUrl):
2296
 
            pass
2297
 
        else:
2298
 
            # TODO: conversions of Branch and Tree should be done by
2299
 
            # InterXFormat lookups
2300
 
            if (isinstance(tree, workingtree.WorkingTree3) and
2301
 
                not isinstance(tree, workingtree_4.WorkingTree4) and
2302
 
                isinstance(self.target_format.workingtree_format,
2303
 
                    workingtree_4.WorkingTreeFormat4)):
2304
 
                workingtree_4.Converter3to4().convert(tree)
2305
 
        return to_convert
2306
 
 
2307
 
 
2308
 
# This is not in remote.py because it's small, and needs to be registered.
2309
 
# Putting it in remote.py creates a circular import problem.
2310
 
# we can make it a lazy object if the control formats is turned into something
2311
 
# like a registry.
2312
 
class RemoteBzrDirFormat(BzrDirMetaFormat1):
2313
 
    """Format representing bzrdirs accessed via a smart server"""
2314
 
 
2315
 
    def get_format_description(self):
2316
 
        return 'bzr remote bzrdir'
2317
 
    
2318
 
    @classmethod
2319
 
    def probe_transport(klass, transport):
2320
 
        """Return a RemoteBzrDirFormat object if it looks possible."""
2321
 
        try:
2322
 
            client = transport.get_smart_client()
2323
 
        except (NotImplementedError, AttributeError,
2324
 
                errors.TransportNotPossible):
2325
 
            # no smart server, so not a branch for this format type.
2326
 
            raise errors.NotBranchError(path=transport.base)
2327
 
        else:
2328
 
            # Send a 'hello' request in protocol version one, and decline to
2329
 
            # open it if the server doesn't support our required version (2) so
2330
 
            # that the VFS-based transport will do it.
2331
 
            request = client.get_request()
2332
 
            smart_protocol = protocol.SmartClientRequestProtocolOne(request)
2333
 
            server_version = smart_protocol.query_version()
2334
 
            if server_version != 2:
2335
 
                raise errors.NotBranchError(path=transport.base)
2336
 
            return klass()
2337
 
 
2338
 
    def initialize_on_transport(self, transport):
2339
 
        try:
2340
 
            # hand off the request to the smart server
2341
 
            shared_medium = transport.get_shared_medium()
2342
 
        except errors.NoSmartMedium:
2343
 
            # TODO: lookup the local format from a server hint.
2344
 
            local_dir_format = BzrDirMetaFormat1()
2345
 
            return local_dir_format.initialize_on_transport(transport)
2346
 
        client = _SmartClient(shared_medium)
2347
 
        path = client.remote_path_from_transport(transport)
2348
 
        response = _SmartClient(shared_medium).call('BzrDirFormat.initialize',
2349
 
                                                    path)
2350
 
        assert response[0] in ('ok', ), 'unexpected response code %s' % (response,)
2351
 
        return remote.RemoteBzrDir(transport)
2352
 
 
2353
 
    def _open(self, transport):
2354
 
        return remote.RemoteBzrDir(transport)
2355
 
 
2356
 
    def __eq__(self, other):
2357
 
        if not isinstance(other, RemoteBzrDirFormat):
2358
 
            return False
2359
 
        return self.get_format_description() == other.get_format_description()
2360
 
 
2361
 
 
2362
 
BzrDirFormat.register_control_server_format(RemoteBzrDirFormat)
2363
 
 
2364
 
 
2365
 
class BzrDirFormatInfo(object):
2366
 
 
2367
 
    def __init__(self, native, deprecated, hidden, experimental):
2368
 
        self.deprecated = deprecated
2369
 
        self.native = native
2370
 
        self.hidden = hidden
2371
 
        self.experimental = experimental
2372
 
 
2373
 
 
2374
 
class BzrDirFormatRegistry(registry.Registry):
2375
 
    """Registry of user-selectable BzrDir subformats.
2376
 
    
2377
 
    Differs from BzrDirFormat._control_formats in that it provides sub-formats,
2378
 
    e.g. BzrDirMeta1 with weave repository.  Also, it's more user-oriented.
2379
 
    """
2380
 
 
2381
 
    def register_metadir(self, key,
2382
 
             repository_format, help, native=True, deprecated=False,
2383
 
             branch_format=None,
2384
 
             tree_format=None,
2385
 
             hidden=False,
2386
 
             experimental=False):
2387
 
        """Register a metadir subformat.
2388
 
 
2389
 
        These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
2390
 
        by the Repository format.
2391
 
 
2392
 
        :param repository_format: The fully-qualified repository format class
2393
 
            name as a string.
2394
 
        :param branch_format: Fully-qualified branch format class name as
2395
 
            a string.
2396
 
        :param tree_format: Fully-qualified tree format class name as
2397
 
            a string.
2398
 
        """
2399
 
        # This should be expanded to support setting WorkingTree and Branch
2400
 
        # formats, once BzrDirMetaFormat1 supports that.
2401
 
        def _load(full_name):
2402
 
            mod_name, factory_name = full_name.rsplit('.', 1)
2403
 
            try:
2404
 
                mod = __import__(mod_name, globals(), locals(),
2405
 
                        [factory_name])
2406
 
            except ImportError, e:
2407
 
                raise ImportError('failed to load %s: %s' % (full_name, e))
2408
 
            try:
2409
 
                factory = getattr(mod, factory_name)
2410
 
            except AttributeError:
2411
 
                raise AttributeError('no factory %s in module %r'
2412
 
                    % (full_name, mod))
2413
 
            return factory()
2414
 
 
2415
 
        def helper():
2416
 
            bd = BzrDirMetaFormat1()
2417
 
            if branch_format is not None:
2418
 
                bd.set_branch_format(_load(branch_format))
2419
 
            if tree_format is not None:
2420
 
                bd.workingtree_format = _load(tree_format)
2421
 
            if repository_format is not None:
2422
 
                bd.repository_format = _load(repository_format)
2423
 
            return bd
2424
 
        self.register(key, helper, help, native, deprecated, hidden,
2425
 
            experimental)
2426
 
 
2427
 
    def register(self, key, factory, help, native=True, deprecated=False,
2428
 
                 hidden=False, experimental=False):
2429
 
        """Register a BzrDirFormat factory.
2430
 
        
2431
 
        The factory must be a callable that takes one parameter: the key.
2432
 
        It must produce an instance of the BzrDirFormat when called.
2433
 
 
2434
 
        This function mainly exists to prevent the info object from being
2435
 
        supplied directly.
2436
 
        """
2437
 
        registry.Registry.register(self, key, factory, help, 
2438
 
            BzrDirFormatInfo(native, deprecated, hidden, experimental))
2439
 
 
2440
 
    def register_lazy(self, key, module_name, member_name, help, native=True,
2441
 
                      deprecated=False, hidden=False, experimental=False):
2442
 
        registry.Registry.register_lazy(self, key, module_name, member_name, 
2443
 
            help, BzrDirFormatInfo(native, deprecated, hidden, experimental))
2444
 
 
2445
 
    def set_default(self, key):
2446
 
        """Set the 'default' key to be a clone of the supplied key.
2447
 
        
2448
 
        This method must be called once and only once.
2449
 
        """
2450
 
        registry.Registry.register(self, 'default', self.get(key), 
2451
 
            self.get_help(key), info=self.get_info(key))
2452
 
 
2453
 
    def set_default_repository(self, key):
2454
 
        """Set the FormatRegistry default and Repository default.
2455
 
        
2456
 
        This is a transitional method while Repository.set_default_format
2457
 
        is deprecated.
2458
 
        """
2459
 
        if 'default' in self:
2460
 
            self.remove('default')
2461
 
        self.set_default(key)
2462
 
        format = self.get('default')()
2463
 
        assert isinstance(format, BzrDirMetaFormat1)
2464
 
 
2465
 
    def make_bzrdir(self, key):
2466
 
        return self.get(key)()
2467
 
 
2468
 
    def help_topic(self, topic):
2469
 
        output = textwrap.dedent("""\
2470
 
            These formats can be used for creating branches, working trees, and
2471
 
            repositories.
2472
 
 
2473
 
            """)
2474
 
        default_realkey = None
2475
 
        default_help = self.get_help('default')
2476
 
        help_pairs = []
2477
 
        for key in self.keys():
2478
 
            if key == 'default':
2479
 
                continue
2480
 
            help = self.get_help(key)
2481
 
            if help == default_help:
2482
 
                default_realkey = key
2483
 
            else:
2484
 
                help_pairs.append((key, help))
2485
 
 
2486
 
        def wrapped(key, help, info):
2487
 
            if info.native:
2488
 
                help = '(native) ' + help
2489
 
            return ':%s:\n%s\n\n' % (key, 
2490
 
                    textwrap.fill(help, initial_indent='    ', 
2491
 
                    subsequent_indent='    '))
2492
 
        if default_realkey is not None:
2493
 
            output += wrapped(default_realkey, '(default) %s' % default_help,
2494
 
                              self.get_info('default'))
2495
 
        deprecated_pairs = []
2496
 
        experimental_pairs = []
2497
 
        for key, help in help_pairs:
2498
 
            info = self.get_info(key)
2499
 
            if info.hidden:
2500
 
                continue
2501
 
            elif info.deprecated:
2502
 
                deprecated_pairs.append((key, help))
2503
 
            elif info.experimental:
2504
 
                experimental_pairs.append((key, help))
2505
 
            else:
2506
 
                output += wrapped(key, help, info)
2507
 
        if len(experimental_pairs) > 0:
2508
 
            output += "Experimental formats are shown below.\n\n"
2509
 
            for key, help in experimental_pairs:
2510
 
                info = self.get_info(key)
2511
 
                output += wrapped(key, help, info)
2512
 
        if len(deprecated_pairs) > 0:
2513
 
            output += "Deprecated formats are shown below.\n\n"
2514
 
            for key, help in deprecated_pairs:
2515
 
                info = self.get_info(key)
2516
 
                output += wrapped(key, help, info)
2517
 
 
2518
 
        return output
2519
 
 
2520
 
 
2521
 
format_registry = BzrDirFormatRegistry()
2522
 
format_registry.register('weave', BzrDirFormat6,
2523
 
    'Pre-0.8 format.  Slower than knit and does not'
2524
 
    ' support checkouts or shared repositories.',
2525
 
    deprecated=True)
2526
 
format_registry.register_metadir('knit',
2527
 
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2528
 
    'Format using knits.  Recommended for interoperation with bzr <= 0.14.',
2529
 
    branch_format='bzrlib.branch.BzrBranchFormat5',
2530
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat3')
2531
 
format_registry.register_metadir('metaweave',
2532
 
    'bzrlib.repofmt.weaverepo.RepositoryFormat7',
2533
 
    'Transitional format in 0.8.  Slower than knit.',
2534
 
    branch_format='bzrlib.branch.BzrBranchFormat5',
2535
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
2536
 
    deprecated=True)
2537
 
format_registry.register_metadir('dirstate',
2538
 
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2539
 
    help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
2540
 
        'above when accessed over the network.',
2541
 
    branch_format='bzrlib.branch.BzrBranchFormat5',
2542
 
    # this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
2543
 
    # directly from workingtree_4 triggers a circular import.
2544
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2545
 
    )
2546
 
format_registry.register_metadir('dirstate-tags',
2547
 
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2548
 
    help='New in 0.15: Fast local operations and improved scaling for '
2549
 
        'network operations. Additionally adds support for tags.'
2550
 
        ' Incompatible with bzr < 0.15.',
2551
 
    branch_format='bzrlib.branch.BzrBranchFormat6',
2552
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2553
 
    )
2554
 
format_registry.register_metadir('rich-root',
2555
 
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit4',
2556
 
    help='New in 1.0.  Better handling of tree roots.  Incompatible with'
2557
 
        ' bzr < 1.0',
2558
 
    branch_format='bzrlib.branch.BzrBranchFormat6',
2559
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2560
 
    hidden=False,
2561
 
    )
2562
 
format_registry.register_metadir('dirstate-with-subtree',
2563
 
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
2564
 
    help='New in 0.15: Fast local operations and improved scaling for '
2565
 
        'network operations. Additionally adds support for versioning nested '
2566
 
        'bzr branches. Incompatible with bzr < 0.15.',
2567
 
    branch_format='bzrlib.branch.BzrBranchFormat6',
2568
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2569
 
    hidden=True,
2570
 
    )
2571
 
format_registry.register_metadir('pack-0.92',
2572
 
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack1',
2573
 
    help='New in 0.92: Pack-based format with data compatible with '
2574
 
        'dirstate-tags format repositories. Interoperates with '
2575
 
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2576
 
        'Previously called knitpack-experimental.  '
2577
 
        'For more information, see '
2578
 
        'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
2579
 
    branch_format='bzrlib.branch.BzrBranchFormat6',
2580
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2581
 
    experimental=True,
2582
 
    )
2583
 
format_registry.register_metadir('pack-0.92-subtree',
2584
 
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack3',
2585
 
    help='New in 0.92: Pack-based format with data compatible with '
2586
 
        'dirstate-with-subtree format repositories. Interoperates with '
2587
 
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2588
 
        'Previously called knitpack-experimental.  '
2589
 
        'For more information, see '
2590
 
        'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
2591
 
    branch_format='bzrlib.branch.BzrBranchFormat6',
2592
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2593
 
    hidden=True,
2594
 
    experimental=True,
2595
 
    )
2596
 
format_registry.register_metadir('rich-root-pack',
2597
 
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
2598
 
    help='New in 1.0: Pack-based format with data compatible with '
2599
 
        'rich-root format repositories. Interoperates with '
2600
 
        'bzr repositories before 0.92 but cannot be read by bzr < 1.0. '
2601
 
        'NOTE: This format is experimental. Before using it, please read '
2602
 
        'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
2603
 
    branch_format='bzrlib.branch.BzrBranchFormat6',
2604
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2605
 
    hidden=False,
2606
 
    experimental=True,
2607
 
    )
2608
 
format_registry.set_default('pack-0.92')