~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/controldir.py

  • Committer: Jelmer Vernooij
  • Date: 2010-08-29 14:37:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5418.
  • Revision ID: jelmer@samba.org-20100829143751-9ry91e6u887gswiz
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""ControlDir is the basic control directory class.
 
18
 
 
19
The ControlDir class is the base for the control directory used
 
20
by all bzr and foreign formats. For the ".bzr" implementation,
 
21
see bzrlib.bzrdir.BzrDir.
 
22
 
 
23
"""
 
24
 
 
25
from bzrlib.lazy_import import lazy_import
 
26
lazy_import(globals(), """
 
27
import textwrap
 
28
 
 
29
from bzrlib import (
 
30
    errors,
 
31
    graph,
 
32
    registry,
 
33
    revision as _mod_revision,
 
34
    symbol_versioning,
 
35
    urlutils,
 
36
    )
 
37
from bzrlib.push import (
 
38
    PushResult,
 
39
    )
 
40
from bzrlib.trace import (
 
41
    mutter,
 
42
    )
 
43
from bzrlib.transport import (
 
44
    get_transport,
 
45
    local,
 
46
    )
 
47
 
 
48
""")
 
49
 
 
50
 
 
51
class ControlComponent(object):
 
52
    """Abstract base class for control directory components.
 
53
 
 
54
    This provides interfaces that are common across controldirs,
 
55
    repositories, branches, and workingtree control directories.
 
56
 
 
57
    They all expose two urls and transports: the *user* URL is the
 
58
    one that stops above the control directory (eg .bzr) and that
 
59
    should normally be used in messages, and the *control* URL is
 
60
    under that in eg .bzr/checkout and is used to read the control
 
61
    files.
 
62
 
 
63
    This can be used as a mixin and is intended to fit with
 
64
    foreign formats.
 
65
    """
 
66
 
 
67
    @property
 
68
    def control_transport(self):
 
69
        raise NotImplementedError
 
70
 
 
71
    @property
 
72
    def control_url(self):
 
73
        return self.control_transport.base
 
74
 
 
75
    @property
 
76
    def user_transport(self):
 
77
        raise NotImplementedError
 
78
 
 
79
    @property
 
80
    def user_url(self):
 
81
        return self.user_transport.base
 
82
 
 
83
 
 
84
class ControlDir(ControlComponent):
 
85
    """A control directory.
 
86
 
 
87
    While this represents a generic control directory, there are a few
 
88
    features that are present in this interface that are currently only
 
89
    supported by one of its implementations, BzrDir.
 
90
 
 
91
    These features (bound branches, stacked branches) are currently only
 
92
    supported by Bazaar, but could be supported by other version control
 
93
    systems as well. Implementations are required to raise the appropriate
 
94
    exceptions when an operation is requested that is not supported.
 
95
 
 
96
    This also makes life easier for API users who can rely on the
 
97
    implementation always allowing a particular feature to be requested but
 
98
    raising an exception when it is not supported, rather than requiring the
 
99
    API users to check for magic attributes to see what features are supported.
 
100
    """
 
101
 
 
102
    def can_convert_format(self):
 
103
        """Return true if this controldir is one whose format we can convert
 
104
        from."""
 
105
        return True
 
106
 
 
107
    def list_branches(self):
 
108
        """Return a sequence of all branches local to this control directory.
 
109
 
 
110
        """
 
111
        try:
 
112
            return [self.open_branch()]
 
113
        except (errors.NotBranchError, errors.NoRepositoryPresent):
 
114
            return []
 
115
 
 
116
    def is_control_filename(self, filename):
 
117
        """True if filename is the name of a path which is reserved for
 
118
        controldirs.
 
119
 
 
120
        :param filename: A filename within the root transport of this
 
121
            controldir.
 
122
 
 
123
        This is true IF and ONLY IF the filename is part of the namespace reserved
 
124
        for bzr control dirs. Currently this is the '.bzr' directory in the root
 
125
        of the root_transport. it is expected that plugins will need to extend
 
126
        this in the future - for instance to make bzr talk with svn working
 
127
        trees.
 
128
        """
 
129
        raise NotImplementedError(self.is_control_filename)
 
130
 
 
131
    def needs_format_conversion(self, format=None):
 
132
        """Return true if this controldir needs convert_format run on it.
 
133
 
 
134
        For instance, if the repository format is out of date but the
 
135
        branch and working tree are not, this should return True.
 
136
 
 
137
        :param format: Optional parameter indicating a specific desired
 
138
                       format we plan to arrive at.
 
139
        """
 
140
        raise NotImplementedError(self.needs_format_conversion)
 
141
 
 
142
    def destroy_repository(self):
 
143
        """Destroy the repository in this ControlDir."""
 
144
        raise NotImplementedError(self.destroy_repository)
 
145
 
 
146
    def create_branch(self, name=None):
 
147
        """Create a branch in this ControlDir.
 
148
 
 
149
        :param name: Name of the colocated branch to create, None for
 
150
            the default branch.
 
151
 
 
152
        The controldirs format will control what branch format is created.
 
153
        For more control see BranchFormatXX.create(a_controldir).
 
154
        """
 
155
        raise NotImplementedError(self.create_branch)
 
156
 
 
157
    def destroy_branch(self, name=None):
 
158
        """Destroy a branch in this ControlDir.
 
159
 
 
160
        :param name: Name of the branch to destroy, None for the default 
 
161
            branch.
 
162
        """
 
163
        raise NotImplementedError(self.destroy_branch)
 
164
 
 
165
    def create_workingtree(self, revision_id=None, from_branch=None,
 
166
        accelerator_tree=None, hardlink=False):
 
167
        """Create a working tree at this ControlDir.
 
168
 
 
169
        :param revision_id: create it as of this revision id.
 
170
        :param from_branch: override controldir branch 
 
171
            (for lightweight checkouts)
 
172
        :param accelerator_tree: A tree which can be used for retrieving file
 
173
            contents more quickly than the revision tree, i.e. a workingtree.
 
174
            The revision tree will be used for cases where accelerator_tree's
 
175
            content is different.
 
176
        """
 
177
        raise NotImplementedError(self.create_workingtree)
 
178
 
 
179
    def destroy_workingtree(self):
 
180
        """Destroy the working tree at this ControlDir.
 
181
 
 
182
        Formats that do not support this may raise UnsupportedOperation.
 
183
        """
 
184
        raise NotImplementedError(self.destroy_workingtree)
 
185
 
 
186
    def destroy_workingtree_metadata(self):
 
187
        """Destroy the control files for the working tree at this ControlDir.
 
188
 
 
189
        The contents of working tree files are not affected.
 
190
        Formats that do not support this may raise UnsupportedOperation.
 
191
        """
 
192
        raise NotImplementedError(self.destroy_workingtree_metadata)
 
193
 
 
194
    def get_branch_reference(self, name=None):
 
195
        """Return the referenced URL for the branch in this controldir.
 
196
 
 
197
        :param name: Optional colocated branch name
 
198
        :raises NotBranchError: If there is no Branch.
 
199
        :raises NoColocatedBranchSupport: If a branch name was specified
 
200
            but colocated branches are not supported.
 
201
        :return: The URL the branch in this controldir references if it is a
 
202
            reference branch, or None for regular branches.
 
203
        """
 
204
        if name is not None:
 
205
            raise errors.NoColocatedBranchSupport(self)
 
206
        return None
 
207
 
 
208
    def get_branch_transport(self, branch_format, name=None):
 
209
        """Get the transport for use by branch format in this ControlDir.
 
210
 
 
211
        Note that bzr dirs that do not support format strings will raise
 
212
        IncompatibleFormat if the branch format they are given has
 
213
        a format string, and vice versa.
 
214
 
 
215
        If branch_format is None, the transport is returned with no
 
216
        checking. If it is not None, then the returned transport is
 
217
        guaranteed to point to an existing directory ready for use.
 
218
        """
 
219
        raise NotImplementedError(self.get_branch_transport)
 
220
 
 
221
    def get_repository_transport(self, repository_format):
 
222
        """Get the transport for use by repository format in this ControlDir.
 
223
 
 
224
        Note that bzr dirs that do not support format strings will raise
 
225
        IncompatibleFormat if the repository format they are given has
 
226
        a format string, and vice versa.
 
227
 
 
228
        If repository_format is None, the transport is returned with no
 
229
        checking. If it is not None, then the returned transport is
 
230
        guaranteed to point to an existing directory ready for use.
 
231
        """
 
232
        raise NotImplementedError(self.get_repository_transport)
 
233
 
 
234
    def get_workingtree_transport(self, tree_format):
 
235
        """Get the transport for use by workingtree format in this ControlDir.
 
236
 
 
237
        Note that bzr dirs that do not support format strings will raise
 
238
        IncompatibleFormat if the workingtree format they are given has a
 
239
        format string, and vice versa.
 
240
 
 
241
        If workingtree_format is None, the transport is returned with no
 
242
        checking. If it is not None, then the returned transport is
 
243
        guaranteed to point to an existing directory ready for use.
 
244
        """
 
245
        raise NotImplementedError(self.get_workingtree_transport)
 
246
 
 
247
    def open_branch(self, name=None, unsupported=False,
 
248
                    ignore_fallbacks=False):
 
249
        """Open the branch object at this ControlDir if one is present.
 
250
 
 
251
        If unsupported is True, then no longer supported branch formats can
 
252
        still be opened.
 
253
 
 
254
        TODO: static convenience version of this?
 
255
        """
 
256
        raise NotImplementedError(self.open_branch)
 
257
 
 
258
    def open_repository(self, _unsupported=False):
 
259
        """Open the repository object at this ControlDir if one is present.
 
260
 
 
261
        This will not follow the Branch object pointer - it's strictly a direct
 
262
        open facility. Most client code should use open_branch().repository to
 
263
        get at a repository.
 
264
 
 
265
        :param _unsupported: a private parameter, not part of the api.
 
266
        TODO: static convenience version of this?
 
267
        """
 
268
        raise NotImplementedError(self.open_repository)
 
269
 
 
270
    def find_repository(self):
 
271
        """Find the repository that should be used.
 
272
 
 
273
        This does not require a branch as we use it to find the repo for
 
274
        new branches as well as to hook existing branches up to their
 
275
        repository.
 
276
        """
 
277
        raise NotImplementedError(self.find_repository)
 
278
 
 
279
    def open_workingtree(self, _unsupported=False,
 
280
                         recommend_upgrade=True, from_branch=None):
 
281
        """Open the workingtree object at this ControlDir if one is present.
 
282
 
 
283
        :param recommend_upgrade: Optional keyword parameter, when True (the
 
284
            default), emit through the ui module a recommendation that the user
 
285
            upgrade the working tree when the workingtree being opened is old
 
286
            (but still fully supported).
 
287
        :param from_branch: override controldir branch (for lightweight
 
288
            checkouts)
 
289
        """
 
290
        raise NotImplementedError(self.open_workingtree)
 
291
 
 
292
    def has_branch(self, name=None):
 
293
        """Tell if this controldir contains a branch.
 
294
 
 
295
        Note: if you're going to open the branch, you should just go ahead
 
296
        and try, and not ask permission first.  (This method just opens the
 
297
        branch and discards it, and that's somewhat expensive.)
 
298
        """
 
299
        try:
 
300
            self.open_branch(name)
 
301
            return True
 
302
        except errors.NotBranchError:
 
303
            return False
 
304
 
 
305
    def has_workingtree(self):
 
306
        """Tell if this controldir contains a working tree.
 
307
 
 
308
        This will still raise an exception if the controldir has a workingtree
 
309
        that is remote & inaccessible.
 
310
 
 
311
        Note: if you're going to open the working tree, you should just go ahead
 
312
        and try, and not ask permission first.  (This method just opens the
 
313
        workingtree and discards it, and that's somewhat expensive.)
 
314
        """
 
315
        try:
 
316
            self.open_workingtree(recommend_upgrade=False)
 
317
            return True
 
318
        except errors.NoWorkingTree:
 
319
            return False
 
320
 
 
321
    def cloning_metadir(self, require_stacking=False):
 
322
        """Produce a metadir suitable for cloning or sprouting with.
 
323
 
 
324
        These operations may produce workingtrees (yes, even though they're
 
325
        "cloning" something that doesn't have a tree), so a viable workingtree
 
326
        format must be selected.
 
327
 
 
328
        :require_stacking: If True, non-stackable formats will be upgraded
 
329
            to similar stackable formats.
 
330
        :returns: a ControlDirFormat with all component formats either set
 
331
            appropriately or set to None if that component should not be
 
332
            created.
 
333
        """
 
334
        raise NotImplementedError(self.cloning_metadir)
 
335
 
 
336
    def checkout_metadir(self):
 
337
        """Produce a metadir suitable for checkouts of this controldir."""
 
338
        return self.cloning_metadir()
 
339
 
 
340
    def sprout(self, url, revision_id=None, force_new_repo=False,
 
341
               recurse='down', possible_transports=None,
 
342
               accelerator_tree=None, hardlink=False, stacked=False,
 
343
               source_branch=None, create_tree_if_local=True):
 
344
        """Create a copy of this controldir prepared for use as a new line of
 
345
        development.
 
346
 
 
347
        If url's last component does not exist, it will be created.
 
348
 
 
349
        Attributes related to the identity of the source branch like
 
350
        branch nickname will be cleaned, a working tree is created
 
351
        whether one existed before or not; and a local branch is always
 
352
        created.
 
353
 
 
354
        if revision_id is not None, then the clone operation may tune
 
355
            itself to download less data.
 
356
        :param accelerator_tree: A tree which can be used for retrieving file
 
357
            contents more quickly than the revision tree, i.e. a workingtree.
 
358
            The revision tree will be used for cases where accelerator_tree's
 
359
            content is different.
 
360
        :param hardlink: If true, hard-link files from accelerator_tree,
 
361
            where possible.
 
362
        :param stacked: If true, create a stacked branch referring to the
 
363
            location of this control directory.
 
364
        :param create_tree_if_local: If true, a working-tree will be created
 
365
            when working locally.
 
366
        """
 
367
        target_transport = get_transport(url, possible_transports)
 
368
        target_transport.ensure_base()
 
369
        cloning_format = self.cloning_metadir(stacked)
 
370
        # Create/update the result branch
 
371
        result = cloning_format.initialize_on_transport(target_transport)
 
372
        # if a stacked branch wasn't requested, we don't create one
 
373
        # even if the origin was stacked
 
374
        stacked_branch_url = None
 
375
        if source_branch is not None:
 
376
            if stacked:
 
377
                stacked_branch_url = self.root_transport.base
 
378
            source_repository = source_branch.repository
 
379
        else:
 
380
            try:
 
381
                source_branch = self.open_branch()
 
382
                source_repository = source_branch.repository
 
383
                if stacked:
 
384
                    stacked_branch_url = self.root_transport.base
 
385
            except errors.NotBranchError:
 
386
                source_branch = None
 
387
                try:
 
388
                    source_repository = self.open_repository()
 
389
                except errors.NoRepositoryPresent:
 
390
                    source_repository = None
 
391
        repository_policy = result.determine_repository_policy(
 
392
            force_new_repo, stacked_branch_url, require_stacking=stacked)
 
393
        result_repo, is_new_repo = repository_policy.acquire_repository()
 
394
        is_stacked = stacked or (len(result_repo._fallback_repositories) != 0)
 
395
        if is_new_repo and revision_id is not None and not is_stacked:
 
396
            fetch_spec = graph.PendingAncestryResult(
 
397
                [revision_id], source_repository)
 
398
        else:
 
399
            fetch_spec = None
 
400
        if source_repository is not None:
 
401
            # Fetch while stacked to prevent unstacked fetch from
 
402
            # Branch.sprout.
 
403
            if fetch_spec is None:
 
404
                result_repo.fetch(source_repository, revision_id=revision_id)
 
405
            else:
 
406
                result_repo.fetch(source_repository, fetch_spec=fetch_spec)
 
407
 
 
408
        if source_branch is None:
 
409
            # this is for sprouting a controldir without a branch; is that
 
410
            # actually useful?
 
411
            # Not especially, but it's part of the contract.
 
412
            result_branch = result.create_branch()
 
413
        else:
 
414
            result_branch = source_branch.sprout(result,
 
415
                revision_id=revision_id, repository_policy=repository_policy)
 
416
        mutter("created new branch %r" % (result_branch,))
 
417
 
 
418
        # Create/update the result working tree
 
419
        if (create_tree_if_local and
 
420
            isinstance(target_transport, local.LocalTransport) and
 
421
            (result_repo is None or result_repo.make_working_trees())):
 
422
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
 
423
                hardlink=hardlink)
 
424
            wt.lock_write()
 
425
            try:
 
426
                if wt.path2id('') is None:
 
427
                    try:
 
428
                        wt.set_root_id(self.open_workingtree.get_root_id())
 
429
                    except errors.NoWorkingTree:
 
430
                        pass
 
431
            finally:
 
432
                wt.unlock()
 
433
        else:
 
434
            wt = None
 
435
        if recurse == 'down':
 
436
            if wt is not None:
 
437
                basis = wt.basis_tree()
 
438
                basis.lock_read()
 
439
                subtrees = basis.iter_references()
 
440
            elif result_branch is not None:
 
441
                basis = result_branch.basis_tree()
 
442
                basis.lock_read()
 
443
                subtrees = basis.iter_references()
 
444
            elif source_branch is not None:
 
445
                basis = source_branch.basis_tree()
 
446
                basis.lock_read()
 
447
                subtrees = basis.iter_references()
 
448
            else:
 
449
                subtrees = []
 
450
                basis = None
 
451
            try:
 
452
                for path, file_id in subtrees:
 
453
                    target = urlutils.join(url, urlutils.escape(path))
 
454
                    sublocation = source_branch.reference_parent(file_id, path)
 
455
                    sublocation.bzrdir.sprout(target,
 
456
                        basis.get_reference_revision(file_id, path),
 
457
                        force_new_repo=force_new_repo, recurse=recurse,
 
458
                        stacked=stacked)
 
459
            finally:
 
460
                if basis is not None:
 
461
                    basis.unlock()
 
462
        return result
 
463
 
 
464
    def push_branch(self, source, revision_id=None, overwrite=False, 
 
465
        remember=False, create_prefix=False):
 
466
        """Push the source branch into this ControlDir."""
 
467
        br_to = None
 
468
        # If we can open a branch, use its direct repository, otherwise see
 
469
        # if there is a repository without a branch.
 
470
        try:
 
471
            br_to = self.open_branch()
 
472
        except errors.NotBranchError:
 
473
            # Didn't find a branch, can we find a repository?
 
474
            repository_to = self.find_repository()
 
475
        else:
 
476
            # Found a branch, so we must have found a repository
 
477
            repository_to = br_to.repository
 
478
 
 
479
        push_result = PushResult()
 
480
        push_result.source_branch = source
 
481
        if br_to is None:
 
482
            # We have a repository but no branch, copy the revisions, and then
 
483
            # create a branch.
 
484
            repository_to.fetch(source.repository, revision_id=revision_id)
 
485
            br_to = source.clone(self, revision_id=revision_id)
 
486
            if source.get_push_location() is None or remember:
 
487
                source.set_push_location(br_to.base)
 
488
            push_result.stacked_on = None
 
489
            push_result.branch_push_result = None
 
490
            push_result.old_revno = None
 
491
            push_result.old_revid = _mod_revision.NULL_REVISION
 
492
            push_result.target_branch = br_to
 
493
            push_result.master_branch = None
 
494
            push_result.workingtree_updated = False
 
495
        else:
 
496
            # We have successfully opened the branch, remember if necessary:
 
497
            if source.get_push_location() is None or remember:
 
498
                source.set_push_location(br_to.base)
 
499
            try:
 
500
                tree_to = self.open_workingtree()
 
501
            except errors.NotLocalUrl:
 
502
                push_result.branch_push_result = source.push(br_to, 
 
503
                    overwrite, stop_revision=revision_id)
 
504
                push_result.workingtree_updated = False
 
505
            except errors.NoWorkingTree:
 
506
                push_result.branch_push_result = source.push(br_to,
 
507
                    overwrite, stop_revision=revision_id)
 
508
                push_result.workingtree_updated = None # Not applicable
 
509
            else:
 
510
                tree_to.lock_write()
 
511
                try:
 
512
                    push_result.branch_push_result = source.push(
 
513
                        tree_to.branch, overwrite, stop_revision=revision_id)
 
514
                    tree_to.update()
 
515
                finally:
 
516
                    tree_to.unlock()
 
517
                push_result.workingtree_updated = True
 
518
            push_result.old_revno = push_result.branch_push_result.old_revno
 
519
            push_result.old_revid = push_result.branch_push_result.old_revid
 
520
            push_result.target_branch = \
 
521
                push_result.branch_push_result.target_branch
 
522
        return push_result
 
523
 
 
524
    def _get_tree_branch(self, name=None):
 
525
        """Return the branch and tree, if any, for this bzrdir.
 
526
 
 
527
        :param name: Name of colocated branch to open.
 
528
 
 
529
        Return None for tree if not present or inaccessible.
 
530
        Raise NotBranchError if no branch is present.
 
531
        :return: (tree, branch)
 
532
        """
 
533
        try:
 
534
            tree = self.open_workingtree()
 
535
        except (errors.NoWorkingTree, errors.NotLocalUrl):
 
536
            tree = None
 
537
            branch = self.open_branch(name=name)
 
538
        else:
 
539
            if name is not None:
 
540
                branch = self.open_branch(name=name)
 
541
            else:
 
542
                branch = tree.branch
 
543
        return tree, branch
 
544
 
 
545
    def get_config(self):
 
546
        """Get configuration for this ControlDir."""
 
547
        raise NotImplementedError(self.get_config)
 
548
 
 
549
    def check_conversion_target(self, target_format):
 
550
        """Check that a bzrdir as a whole can be converted to a new format."""
 
551
        raise NotImplementedError(self.check_conversion_target)
 
552
 
 
553
    def clone(self, url, revision_id=None, force_new_repo=False,
 
554
              preserve_stacking=False):
 
555
        """Clone this bzrdir and its contents to url verbatim.
 
556
 
 
557
        :param url: The url create the clone at.  If url's last component does
 
558
            not exist, it will be created.
 
559
        :param revision_id: The tip revision-id to use for any branch or
 
560
            working tree.  If not None, then the clone operation may tune
 
561
            itself to download less data.
 
562
        :param force_new_repo: Do not use a shared repository for the target
 
563
                               even if one is available.
 
564
        :param preserve_stacking: When cloning a stacked branch, stack the
 
565
            new branch on top of the other branch's stacked-on branch.
 
566
        """
 
567
        return self.clone_on_transport(get_transport(url),
 
568
                                       revision_id=revision_id,
 
569
                                       force_new_repo=force_new_repo,
 
570
                                       preserve_stacking=preserve_stacking)
 
571
 
 
572
    def clone_on_transport(self, transport, revision_id=None,
 
573
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
 
574
        create_prefix=False, use_existing_dir=True):
 
575
        """Clone this bzrdir and its contents to transport verbatim.
 
576
 
 
577
        :param transport: The transport for the location to produce the clone
 
578
            at.  If the target directory does not exist, it will be created.
 
579
        :param revision_id: The tip revision-id to use for any branch or
 
580
            working tree.  If not None, then the clone operation may tune
 
581
            itself to download less data.
 
582
        :param force_new_repo: Do not use a shared repository for the target,
 
583
                               even if one is available.
 
584
        :param preserve_stacking: When cloning a stacked branch, stack the
 
585
            new branch on top of the other branch's stacked-on branch.
 
586
        :param create_prefix: Create any missing directories leading up to
 
587
            to_transport.
 
588
        :param use_existing_dir: Use an existing directory if one exists.
 
589
        """
 
590
        raise NotImplementedError(self.clone_on_transport)
 
591
 
 
592
 
 
593
class ControlDirFormat(object):
 
594
    """An encapsulation of the initialization and open routines for a format.
 
595
 
 
596
    Formats provide three things:
 
597
     * An initialization routine,
 
598
     * a format string,
 
599
     * an open routine.
 
600
 
 
601
    Formats are placed in a dict by their format string for reference
 
602
    during controldir opening. These should be subclasses of ControlDirFormat
 
603
    for consistency.
 
604
 
 
605
    Once a format is deprecated, just deprecate the initialize and open
 
606
    methods on the format class. Do not deprecate the object, as the
 
607
    object will be created every system load.
 
608
 
 
609
    :cvar colocated_branches: Whether this formats supports colocated branches.
 
610
    """
 
611
 
 
612
    _default_format = None
 
613
    """The default format used for new control directories."""
 
614
 
 
615
    _formats = []
 
616
    """The registered control formats - .bzr, ....
 
617
 
 
618
    This is a list of ControlDirFormat objects.
 
619
    """
 
620
 
 
621
    _server_probers = []
 
622
    """The registered server format probers, e.g. RemoteBzrProber.
 
623
 
 
624
    This is a list of Prober-derived classes.
 
625
    """
 
626
 
 
627
    _probers = []
 
628
    """The registered format probers, e.g. BzrProber.
 
629
 
 
630
    This is a list of Prober-derived classes.
 
631
    """
 
632
 
 
633
    colocated_branches = False
 
634
    """Whether co-located branches are supported for this control dir format.
 
635
    """
 
636
 
 
637
    def get_format_description(self):
 
638
        """Return the short description for this format."""
 
639
        raise NotImplementedError(self.get_format_description)
 
640
 
 
641
    def get_converter(self, format=None):
 
642
        """Return the converter to use to convert controldirs needing converts.
 
643
 
 
644
        This returns a bzrlib.controldir.Converter object.
 
645
 
 
646
        This should return the best upgrader to step this format towards the
 
647
        current default format. In the case of plugins we can/should provide
 
648
        some means for them to extend the range of returnable converters.
 
649
 
 
650
        :param format: Optional format to override the default format of the
 
651
                       library.
 
652
        """
 
653
        raise NotImplementedError(self.get_converter)
 
654
 
 
655
    def is_supported(self):
 
656
        """Is this format supported?
 
657
 
 
658
        Supported formats must be initializable and openable.
 
659
        Unsupported formats may not support initialization or committing or
 
660
        some other features depending on the reason for not being supported.
 
661
        """
 
662
        return True
 
663
 
 
664
    def same_model(self, target_format):
 
665
        return (self.repository_format.rich_root_data ==
 
666
            target_format.rich_root_data)
 
667
 
 
668
    @classmethod
 
669
    def register_format(klass, format):
 
670
        """Register a format that does not use '.bzr' for its control dir.
 
671
 
 
672
        """
 
673
        klass._formats.append(format)
 
674
 
 
675
    @classmethod
 
676
    def register_prober(klass, prober):
 
677
        """Register a prober that can look for a control dir.
 
678
 
 
679
        """
 
680
        klass._probers.append(prober)
 
681
 
 
682
    @classmethod
 
683
    def unregister_prober(klass, prober):
 
684
        """Unregister a prober.
 
685
 
 
686
        """
 
687
        klass._probers.remove(prober)
 
688
 
 
689
    @classmethod
 
690
    def register_server_prober(klass, prober):
 
691
        """Register a control format prober for client-server environments.
 
692
 
 
693
        These probers will be used before ones registered with
 
694
        register_prober.  This gives implementations that decide to the
 
695
        chance to grab it before anything looks at the contents of the format
 
696
        file.
 
697
        """
 
698
        klass._server_probers.append(prober)
 
699
 
 
700
    def __str__(self):
 
701
        # Trim the newline
 
702
        return self.get_format_description().rstrip()
 
703
 
 
704
    @classmethod
 
705
    def unregister_format(klass, format):
 
706
        klass._formats.remove(format)
 
707
 
 
708
    @classmethod
 
709
    def known_formats(klass):
 
710
        """Return all the known formats.
 
711
        """
 
712
        return set(klass._formats)
 
713
 
 
714
    @classmethod
 
715
    def find_format(klass, transport, _server_formats=True):
 
716
        """Return the format present at transport."""
 
717
        if _server_formats:
 
718
            _probers = klass._server_probers + klass._probers
 
719
        else:
 
720
            _probers = klass._probers
 
721
        for prober_kls in _probers:
 
722
            prober = prober_kls()
 
723
            try:
 
724
                return prober.probe_transport(transport)
 
725
            except errors.NotBranchError:
 
726
                # this format does not find a control dir here.
 
727
                pass
 
728
        raise errors.NotBranchError(path=transport.base)
 
729
 
 
730
    def initialize(self, url, possible_transports=None):
 
731
        """Create a control dir at this url and return an opened copy.
 
732
 
 
733
        While not deprecated, this method is very specific and its use will
 
734
        lead to many round trips to setup a working environment. See
 
735
        initialize_on_transport_ex for a [nearly] all-in-one method.
 
736
 
 
737
        Subclasses should typically override initialize_on_transport
 
738
        instead of this method.
 
739
        """
 
740
        return self.initialize_on_transport(get_transport(url,
 
741
                                                          possible_transports))
 
742
    def initialize_on_transport(self, transport):
 
743
        """Initialize a new controldir in the base directory of a Transport."""
 
744
        raise NotImplementedError(self.initialize_on_transport)
 
745
 
 
746
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
747
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
748
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
749
        shared_repo=False, vfs_only=False):
 
750
        """Create this format on transport.
 
751
 
 
752
        The directory to initialize will be created.
 
753
 
 
754
        :param force_new_repo: Do not use a shared repository for the target,
 
755
                               even if one is available.
 
756
        :param create_prefix: Create any missing directories leading up to
 
757
            to_transport.
 
758
        :param use_existing_dir: Use an existing directory if one exists.
 
759
        :param stacked_on: A url to stack any created branch on, None to follow
 
760
            any target stacking policy.
 
761
        :param stack_on_pwd: If stack_on is relative, the location it is
 
762
            relative to.
 
763
        :param repo_format_name: If non-None, a repository will be
 
764
            made-or-found. Should none be found, or if force_new_repo is True
 
765
            the repo_format_name is used to select the format of repository to
 
766
            create.
 
767
        :param make_working_trees: Control the setting of make_working_trees
 
768
            for a new shared repository when one is made. None to use whatever
 
769
            default the format has.
 
770
        :param shared_repo: Control whether made repositories are shared or
 
771
            not.
 
772
        :param vfs_only: If True do not attempt to use a smart server
 
773
        :return: repo, controldir, require_stacking, repository_policy. repo is
 
774
            None if none was created or found, controldir is always valid.
 
775
            require_stacking is the result of examining the stacked_on
 
776
            parameter and any stacking policy found for the target.
 
777
        """
 
778
        raise NotImplementedError(self.initialize_on_transport_ex)
 
779
 
 
780
    def network_name(self):
 
781
        """A simple byte string uniquely identifying this format for RPC calls.
 
782
 
 
783
        Bzr control formats use this disk format string to identify the format
 
784
        over the wire. Its possible that other control formats have more
 
785
        complex detection requirements, so we permit them to use any unique and
 
786
        immutable string they desire.
 
787
        """
 
788
        raise NotImplementedError(self.network_name)
 
789
 
 
790
    def open(self, transport, _found=False):
 
791
        """Return an instance of this format for the dir transport points at.
 
792
        """
 
793
        raise NotImplementedError(self.open)
 
794
 
 
795
    @classmethod
 
796
    def _set_default_format(klass, format):
 
797
        """Set default format (for testing behavior of defaults only)"""
 
798
        klass._default_format = format
 
799
 
 
800
    @classmethod
 
801
    def get_default_format(klass):
 
802
        """Return the current default format."""
 
803
        return klass._default_format
 
804
 
 
805
 
 
806
class Prober(object):
 
807
    """Abstract class that can be used to detect a particular kind of 
 
808
    control directory.
 
809
 
 
810
    At the moment this just contains a single method to probe a particular 
 
811
    transport, but it may be extended in the future to e.g. avoid 
 
812
    multiple levels of probing for Subversion repositories.
 
813
    """
 
814
 
 
815
    def probe_transport(self, transport):
 
816
        """Return the controldir style format present in a directory.
 
817
 
 
818
        :raise UnknownFormatError: If a control dir was found but is
 
819
            in an unknown format.
 
820
        :raise NotBranchError: If no control directory was found.
 
821
        :return: A ControlDirFormat instance.
 
822
        """
 
823
        raise NotImplementedError(self.probe_transport)
 
824
 
 
825
 
 
826
class ControlDirFormatInfo(object):
 
827
 
 
828
    def __init__(self, native, deprecated, hidden, experimental):
 
829
        self.deprecated = deprecated
 
830
        self.native = native
 
831
        self.hidden = hidden
 
832
        self.experimental = experimental
 
833
 
 
834
 
 
835
class ControlDirFormatRegistry(registry.Registry):
 
836
    """Registry of user-selectable ControlDir subformats.
 
837
 
 
838
    Differs from ControlDirFormat._formats in that it provides sub-formats,
 
839
    e.g. ControlDirMeta1 with weave repository.  Also, it's more user-oriented.
 
840
    """
 
841
 
 
842
    def __init__(self):
 
843
        """Create a ControlDirFormatRegistry."""
 
844
        self._aliases = set()
 
845
        self._registration_order = list()
 
846
        super(ControlDirFormatRegistry, self).__init__()
 
847
 
 
848
    def aliases(self):
 
849
        """Return a set of the format names which are aliases."""
 
850
        return frozenset(self._aliases)
 
851
 
 
852
    def register(self, key, factory, help, native=True, deprecated=False,
 
853
                 hidden=False, experimental=False, alias=False):
 
854
        """Register a ControlDirFormat factory.
 
855
 
 
856
        The factory must be a callable that takes one parameter: the key.
 
857
        It must produce an instance of the ControlDirFormat when called.
 
858
 
 
859
        This function mainly exists to prevent the info object from being
 
860
        supplied directly.
 
861
        """
 
862
        registry.Registry.register(self, key, factory, help,
 
863
            ControlDirFormatInfo(native, deprecated, hidden, experimental))
 
864
        if alias:
 
865
            self._aliases.add(key)
 
866
        self._registration_order.append(key)
 
867
 
 
868
    def register_lazy(self, key, module_name, member_name, help, native=True,
 
869
        deprecated=False, hidden=False, experimental=False, alias=False):
 
870
        registry.Registry.register_lazy(self, key, module_name, member_name,
 
871
            help, ControlDirFormatInfo(native, deprecated, hidden, experimental))
 
872
        if alias:
 
873
            self._aliases.add(key)
 
874
        self._registration_order.append(key)
 
875
 
 
876
    def set_default(self, key):
 
877
        """Set the 'default' key to be a clone of the supplied key.
 
878
 
 
879
        This method must be called once and only once.
 
880
        """
 
881
        registry.Registry.register(self, 'default', self.get(key),
 
882
            self.get_help(key), info=self.get_info(key))
 
883
        self._aliases.add('default')
 
884
 
 
885
    def set_default_repository(self, key):
 
886
        """Set the FormatRegistry default and Repository default.
 
887
 
 
888
        This is a transitional method while Repository.set_default_format
 
889
        is deprecated.
 
890
        """
 
891
        if 'default' in self:
 
892
            self.remove('default')
 
893
        self.set_default(key)
 
894
        format = self.get('default')()
 
895
 
 
896
    def make_bzrdir(self, key):
 
897
        return self.get(key)()
 
898
 
 
899
    def help_topic(self, topic):
 
900
        output = ""
 
901
        default_realkey = None
 
902
        default_help = self.get_help('default')
 
903
        help_pairs = []
 
904
        for key in self._registration_order:
 
905
            if key == 'default':
 
906
                continue
 
907
            help = self.get_help(key)
 
908
            if help == default_help:
 
909
                default_realkey = key
 
910
            else:
 
911
                help_pairs.append((key, help))
 
912
 
 
913
        def wrapped(key, help, info):
 
914
            if info.native:
 
915
                help = '(native) ' + help
 
916
            return ':%s:\n%s\n\n' % (key,
 
917
                textwrap.fill(help, initial_indent='    ',
 
918
                    subsequent_indent='    ',
 
919
                    break_long_words=False))
 
920
        if default_realkey is not None:
 
921
            output += wrapped(default_realkey, '(default) %s' % default_help,
 
922
                              self.get_info('default'))
 
923
        deprecated_pairs = []
 
924
        experimental_pairs = []
 
925
        for key, help in help_pairs:
 
926
            info = self.get_info(key)
 
927
            if info.hidden:
 
928
                continue
 
929
            elif info.deprecated:
 
930
                deprecated_pairs.append((key, help))
 
931
            elif info.experimental:
 
932
                experimental_pairs.append((key, help))
 
933
            else:
 
934
                output += wrapped(key, help, info)
 
935
        output += "\nSee :doc:`formats-help` for more about storage formats."
 
936
        other_output = ""
 
937
        if len(experimental_pairs) > 0:
 
938
            other_output += "Experimental formats are shown below.\n\n"
 
939
            for key, help in experimental_pairs:
 
940
                info = self.get_info(key)
 
941
                other_output += wrapped(key, help, info)
 
942
        else:
 
943
            other_output += \
 
944
                "No experimental formats are available.\n\n"
 
945
        if len(deprecated_pairs) > 0:
 
946
            other_output += "\nDeprecated formats are shown below.\n\n"
 
947
            for key, help in deprecated_pairs:
 
948
                info = self.get_info(key)
 
949
                other_output += wrapped(key, help, info)
 
950
        else:
 
951
            other_output += \
 
952
                "\nNo deprecated formats are available.\n\n"
 
953
        other_output += \
 
954
                "\nSee :doc:`formats-help` for more about storage formats."
 
955
 
 
956
        if topic == 'other-formats':
 
957
            return other_output
 
958
        else:
 
959
            return output
 
960
 
 
961
 
 
962
# Please register new formats after old formats so that formats
 
963
# appear in chronological order and format descriptions can build
 
964
# on previous ones.
 
965
format_registry = ControlDirFormatRegistry()
 
966
 
 
967
network_format_registry = registry.FormatRegistry()
 
968
"""Registry of formats indexed by their network name.
 
969
 
 
970
The network name for a ControlDirFormat is an identifier that can be used when
 
971
referring to formats with smart server operations. See
 
972
ControlDirFormat.network_name() for more detail.
 
973
"""