~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/controldir.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2010, 2011, 2012 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
 
23
23
"""
24
24
 
 
25
from __future__ import absolute_import
 
26
 
25
27
from bzrlib.lazy_import import lazy_import
26
28
lazy_import(globals(), """
27
29
import textwrap
28
30
 
29
31
from bzrlib import (
30
32
    errors,
 
33
    hooks,
31
34
    revision as _mod_revision,
32
35
    transport as _mod_transport,
 
36
    trace,
33
37
    ui,
 
38
    urlutils,
34
39
    )
 
40
from bzrlib.transport import local
35
41
from bzrlib.push import (
36
42
    PushResult,
37
43
    )
38
44
 
 
45
from bzrlib.i18n import gettext
39
46
""")
40
47
 
41
48
from bzrlib import registry
74
81
        return self.user_transport.base
75
82
 
76
83
 
77
 
 
78
84
class ControlDir(ControlComponent):
79
85
    """A control directory.
80
86
 
102
108
        """Return a sequence of all branches local to this control directory.
103
109
 
104
110
        """
 
111
        return self.get_branches().values()
 
112
 
 
113
    def get_branches(self):
 
114
        """Get all branches in this control directory, as a dictionary.
 
115
        
 
116
        :return: Dictionary mapping branch names to instances.
 
117
        """
105
118
        try:
106
 
            return [self.open_branch()]
 
119
           return { "": self.open_branch() }
107
120
        except (errors.NotBranchError, errors.NoRepositoryPresent):
108
 
            return []
 
121
           return {}
109
122
 
110
123
    def is_control_filename(self, filename):
111
124
        """True if filename is the name of a path which is reserved for
145
158
        """Destroy the repository in this ControlDir."""
146
159
        raise NotImplementedError(self.destroy_repository)
147
160
 
148
 
    def create_branch(self, name=None, repository=None):
 
161
    def create_branch(self, name=None, repository=None,
 
162
                      append_revisions_only=None):
149
163
        """Create a branch in this ControlDir.
150
164
 
151
165
        :param name: Name of the colocated branch to create, None for
152
 
            the default branch.
 
166
            the user selected branch or "" for the active branch.
 
167
        :param append_revisions_only: Whether this branch should only allow
 
168
            appending new revisions to its history.
153
169
 
154
170
        The controldirs format will control what branch format is created.
155
171
        For more control see BranchFormatXX.create(a_controldir).
159
175
    def destroy_branch(self, name=None):
160
176
        """Destroy a branch in this ControlDir.
161
177
 
162
 
        :param name: Name of the branch to destroy, None for the default 
163
 
            branch.
 
178
        :param name: Name of the branch to destroy, None for the 
 
179
            user selected branch or "" for the active branch.
 
180
        :raise NotBranchError: When the branch does not exist
164
181
        """
165
182
        raise NotImplementedError(self.destroy_branch)
166
183
 
193
210
        """
194
211
        raise NotImplementedError(self.destroy_workingtree_metadata)
195
212
 
 
213
    def find_branch_format(self, name=None):
 
214
        """Find the branch 'format' for this controldir.
 
215
 
 
216
        This might be a synthetic object for e.g. RemoteBranch and SVN.
 
217
        """
 
218
        raise NotImplementedError(self.find_branch_format)
 
219
 
196
220
    def get_branch_reference(self, name=None):
197
221
        """Return the referenced URL for the branch in this controldir.
198
222
 
207
231
            raise errors.NoColocatedBranchSupport(self)
208
232
        return None
209
233
 
 
234
    def set_branch_reference(self, target_branch, name=None):
 
235
        """Set the referenced URL for the branch in this controldir.
 
236
 
 
237
        :param name: Optional colocated branch name
 
238
        :param target_branch: Branch to reference
 
239
        :raises NoColocatedBranchSupport: If a branch name was specified
 
240
            but colocated branches are not supported.
 
241
        :return: The referencing branch
 
242
        """
 
243
        raise NotImplementedError(self.set_branch_reference)
 
244
 
210
245
    def open_branch(self, name=None, unsupported=False,
211
 
                    ignore_fallbacks=False):
 
246
                    ignore_fallbacks=False, possible_transports=None):
212
247
        """Open the branch object at this ControlDir if one is present.
213
248
 
214
 
        If unsupported is True, then no longer supported branch formats can
215
 
        still be opened.
216
 
 
217
 
        TODO: static convenience version of this?
 
249
        :param unsupported: if True, then no longer supported branch formats can
 
250
            still be opened.
 
251
        :param ignore_fallbacks: Whether to open fallback repositories
 
252
        :param possible_transports: Transports to use for opening e.g.
 
253
            fallback repositories.
218
254
        """
219
255
        raise NotImplementedError(self.open_branch)
220
256
 
226
262
        get at a repository.
227
263
 
228
264
        :param _unsupported: a private parameter, not part of the api.
229
 
 
230
 
        TODO: static convenience version of this?
231
265
        """
232
266
        raise NotImplementedError(self.open_repository)
233
267
 
240
274
        """
241
275
        raise NotImplementedError(self.find_repository)
242
276
 
243
 
    def open_workingtree(self, _unsupported=False,
 
277
    def open_workingtree(self, unsupported=False,
244
278
                         recommend_upgrade=True, from_branch=None):
245
279
        """Open the workingtree object at this ControlDir if one is present.
246
280
 
261
295
        branch and discards it, and that's somewhat expensive.)
262
296
        """
263
297
        try:
264
 
            self.open_branch(name)
 
298
            self.open_branch(name, ignore_fallbacks=True)
265
299
            return True
266
300
        except errors.NotBranchError:
267
301
            return False
268
302
 
 
303
    def _get_selected_branch(self):
 
304
        """Return the name of the branch selected by the user.
 
305
 
 
306
        :return: Name of the branch selected by the user, or "".
 
307
        """
 
308
        branch = self.root_transport.get_segment_parameters().get("branch")
 
309
        if branch is None:
 
310
            branch = ""
 
311
        return urlutils.unescape(branch)
 
312
 
269
313
    def has_workingtree(self):
270
314
        """Tell if this controldir contains a working tree.
271
315
 
298
342
        raise NotImplementedError(self.cloning_metadir)
299
343
 
300
344
    def checkout_metadir(self):
301
 
        """Produce a metadir suitable for checkouts of this controldir."""
 
345
        """Produce a metadir suitable for checkouts of this controldir.
 
346
 
 
347
        :returns: A ControlDirFormat with all component formats
 
348
            either set appropriately or set to None if that component
 
349
            should not be created.
 
350
        """
302
351
        return self.cloning_metadir()
303
352
 
304
353
    def sprout(self, url, revision_id=None, force_new_repo=False,
357
406
            repository_to.fetch(source.repository, revision_id=revision_id)
358
407
            br_to = source.clone(self, revision_id=revision_id)
359
408
            if source.get_push_location() is None or remember:
 
409
                # FIXME: Should be done only if we succeed ? -- vila 2012-01-18
360
410
                source.set_push_location(br_to.base)
361
411
            push_result.stacked_on = None
362
412
            push_result.branch_push_result = None
368
418
        else:
369
419
            # We have successfully opened the branch, remember if necessary:
370
420
            if source.get_push_location() is None or remember:
 
421
                # FIXME: Should be done only if we succeed ? -- vila 2012-01-18
371
422
                source.set_push_location(br_to.base)
372
423
            try:
373
424
                tree_to = self.open_workingtree()
395
446
        return push_result
396
447
 
397
448
    def _get_tree_branch(self, name=None):
398
 
        """Return the branch and tree, if any, for this bzrdir.
 
449
        """Return the branch and tree, if any, for this controldir.
399
450
 
400
451
        :param name: Name of colocated branch to open.
401
452
 
420
471
        raise NotImplementedError(self.get_config)
421
472
 
422
473
    def check_conversion_target(self, target_format):
423
 
        """Check that a bzrdir as a whole can be converted to a new format."""
 
474
        """Check that a controldir as a whole can be converted to a new format."""
424
475
        raise NotImplementedError(self.check_conversion_target)
425
476
 
426
477
    def clone(self, url, revision_id=None, force_new_repo=False,
427
478
              preserve_stacking=False):
428
 
        """Clone this bzrdir and its contents to url verbatim.
 
479
        """Clone this controldir and its contents to url verbatim.
429
480
 
430
481
        :param url: The url create the clone at.  If url's last component does
431
482
            not exist, it will be created.
445
496
    def clone_on_transport(self, transport, revision_id=None,
446
497
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
447
498
        create_prefix=False, use_existing_dir=True, no_tree=False):
448
 
        """Clone this bzrdir and its contents to transport verbatim.
 
499
        """Clone this controldir and its contents to transport verbatim.
449
500
 
450
501
        :param transport: The transport for the location to produce the clone
451
502
            at.  If the target directory does not exist, it will be created.
463
514
        """
464
515
        raise NotImplementedError(self.clone_on_transport)
465
516
 
 
517
    @classmethod
 
518
    def find_bzrdirs(klass, transport, evaluate=None, list_current=None):
 
519
        """Find control dirs recursively from current location.
 
520
 
 
521
        This is intended primarily as a building block for more sophisticated
 
522
        functionality, like finding trees under a directory, or finding
 
523
        branches that use a given repository.
 
524
 
 
525
        :param evaluate: An optional callable that yields recurse, value,
 
526
            where recurse controls whether this controldir is recursed into
 
527
            and value is the value to yield.  By default, all bzrdirs
 
528
            are recursed into, and the return value is the controldir.
 
529
        :param list_current: if supplied, use this function to list the current
 
530
            directory, instead of Transport.list_dir
 
531
        :return: a generator of found bzrdirs, or whatever evaluate returns.
 
532
        """
 
533
        if list_current is None:
 
534
            def list_current(transport):
 
535
                return transport.list_dir('')
 
536
        if evaluate is None:
 
537
            def evaluate(controldir):
 
538
                return True, controldir
 
539
 
 
540
        pending = [transport]
 
541
        while len(pending) > 0:
 
542
            current_transport = pending.pop()
 
543
            recurse = True
 
544
            try:
 
545
                controldir = klass.open_from_transport(current_transport)
 
546
            except (errors.NotBranchError, errors.PermissionDenied):
 
547
                pass
 
548
            else:
 
549
                recurse, value = evaluate(controldir)
 
550
                yield value
 
551
            try:
 
552
                subdirs = list_current(current_transport)
 
553
            except (errors.NoSuchFile, errors.PermissionDenied):
 
554
                continue
 
555
            if recurse:
 
556
                for subdir in sorted(subdirs, reverse=True):
 
557
                    pending.append(current_transport.clone(subdir))
 
558
 
 
559
    @classmethod
 
560
    def find_branches(klass, transport):
 
561
        """Find all branches under a transport.
 
562
 
 
563
        This will find all branches below the transport, including branches
 
564
        inside other branches.  Where possible, it will use
 
565
        Repository.find_branches.
 
566
 
 
567
        To list all the branches that use a particular Repository, see
 
568
        Repository.find_branches
 
569
        """
 
570
        def evaluate(controldir):
 
571
            try:
 
572
                repository = controldir.open_repository()
 
573
            except errors.NoRepositoryPresent:
 
574
                pass
 
575
            else:
 
576
                return False, ([], repository)
 
577
            return True, (controldir.list_branches(), None)
 
578
        ret = []
 
579
        for branches, repo in klass.find_bzrdirs(
 
580
                transport, evaluate=evaluate):
 
581
            if repo is not None:
 
582
                ret.extend(repo.find_branches())
 
583
            if branches is not None:
 
584
                ret.extend(branches)
 
585
        return ret
 
586
 
 
587
    @classmethod
 
588
    def create_branch_and_repo(klass, base, force_new_repo=False, format=None):
 
589
        """Create a new ControlDir, Branch and Repository at the url 'base'.
 
590
 
 
591
        This will use the current default ControlDirFormat unless one is
 
592
        specified, and use whatever
 
593
        repository format that that uses via controldir.create_branch and
 
594
        create_repository. If a shared repository is available that is used
 
595
        preferentially.
 
596
 
 
597
        The created Branch object is returned.
 
598
 
 
599
        :param base: The URL to create the branch at.
 
600
        :param force_new_repo: If True a new repository is always created.
 
601
        :param format: If supplied, the format of branch to create.  If not
 
602
            supplied, the default is used.
 
603
        """
 
604
        controldir = klass.create(base, format)
 
605
        controldir._find_or_create_repository(force_new_repo)
 
606
        return controldir.create_branch()
 
607
 
 
608
    @classmethod
 
609
    def create_branch_convenience(klass, base, force_new_repo=False,
 
610
                                  force_new_tree=None, format=None,
 
611
                                  possible_transports=None):
 
612
        """Create a new ControlDir, Branch and Repository at the url 'base'.
 
613
 
 
614
        This is a convenience function - it will use an existing repository
 
615
        if possible, can be told explicitly whether to create a working tree or
 
616
        not.
 
617
 
 
618
        This will use the current default ControlDirFormat unless one is
 
619
        specified, and use whatever
 
620
        repository format that that uses via ControlDir.create_branch and
 
621
        create_repository. If a shared repository is available that is used
 
622
        preferentially. Whatever repository is used, its tree creation policy
 
623
        is followed.
 
624
 
 
625
        The created Branch object is returned.
 
626
        If a working tree cannot be made due to base not being a file:// url,
 
627
        no error is raised unless force_new_tree is True, in which case no
 
628
        data is created on disk and NotLocalUrl is raised.
 
629
 
 
630
        :param base: The URL to create the branch at.
 
631
        :param force_new_repo: If True a new repository is always created.
 
632
        :param force_new_tree: If True or False force creation of a tree or
 
633
                               prevent such creation respectively.
 
634
        :param format: Override for the controldir format to create.
 
635
        :param possible_transports: An optional reusable transports list.
 
636
        """
 
637
        if force_new_tree:
 
638
            # check for non local urls
 
639
            t = _mod_transport.get_transport(base, possible_transports)
 
640
            if not isinstance(t, local.LocalTransport):
 
641
                raise errors.NotLocalUrl(base)
 
642
        controldir = klass.create(base, format, possible_transports)
 
643
        repo = controldir._find_or_create_repository(force_new_repo)
 
644
        result = controldir.create_branch()
 
645
        if force_new_tree or (repo.make_working_trees() and
 
646
                              force_new_tree is None):
 
647
            try:
 
648
                controldir.create_workingtree()
 
649
            except errors.NotLocalUrl:
 
650
                pass
 
651
        return result
 
652
 
 
653
    @classmethod
 
654
    def create_standalone_workingtree(klass, base, format=None):
 
655
        """Create a new ControlDir, WorkingTree, Branch and Repository at 'base'.
 
656
 
 
657
        'base' must be a local path or a file:// url.
 
658
 
 
659
        This will use the current default ControlDirFormat unless one is
 
660
        specified, and use whatever
 
661
        repository format that that uses for bzrdirformat.create_workingtree,
 
662
        create_branch and create_repository.
 
663
 
 
664
        :param format: Override for the controldir format to create.
 
665
        :return: The WorkingTree object.
 
666
        """
 
667
        t = _mod_transport.get_transport(base)
 
668
        if not isinstance(t, local.LocalTransport):
 
669
            raise errors.NotLocalUrl(base)
 
670
        controldir = klass.create_branch_and_repo(base,
 
671
                                               force_new_repo=True,
 
672
                                               format=format).bzrdir
 
673
        return controldir.create_workingtree()
 
674
 
 
675
    @classmethod
 
676
    def open_unsupported(klass, base):
 
677
        """Open a branch which is not supported."""
 
678
        return klass.open(base, _unsupported=True)
 
679
 
 
680
    @classmethod
 
681
    def open(klass, base, possible_transports=None, probers=None,
 
682
             _unsupported=False):
 
683
        """Open an existing controldir, rooted at 'base' (url).
 
684
 
 
685
        :param _unsupported: a private parameter to the ControlDir class.
 
686
        """
 
687
        t = _mod_transport.get_transport(base, possible_transports)
 
688
        return klass.open_from_transport(t, probers=probers,
 
689
                _unsupported=_unsupported)
 
690
 
 
691
    @classmethod
 
692
    def open_from_transport(klass, transport, _unsupported=False,
 
693
                            probers=None):
 
694
        """Open a controldir within a particular directory.
 
695
 
 
696
        :param transport: Transport containing the controldir.
 
697
        :param _unsupported: private.
 
698
        """
 
699
        for hook in klass.hooks['pre_open']:
 
700
            hook(transport)
 
701
        # Keep initial base since 'transport' may be modified while following
 
702
        # the redirections.
 
703
        base = transport.base
 
704
        def find_format(transport):
 
705
            return transport, ControlDirFormat.find_format(transport,
 
706
                probers=probers)
 
707
 
 
708
        def redirected(transport, e, redirection_notice):
 
709
            redirected_transport = transport._redirected_to(e.source, e.target)
 
710
            if redirected_transport is None:
 
711
                raise errors.NotBranchError(base)
 
712
            trace.note(gettext('{0} is{1} redirected to {2}').format(
 
713
                 transport.base, e.permanently, redirected_transport.base))
 
714
            return redirected_transport
 
715
 
 
716
        try:
 
717
            transport, format = _mod_transport.do_catching_redirections(
 
718
                find_format, transport, redirected)
 
719
        except errors.TooManyRedirections:
 
720
            raise errors.NotBranchError(base)
 
721
 
 
722
        format.check_support_status(_unsupported)
 
723
        return format.open(transport, _found=True)
 
724
 
 
725
    @classmethod
 
726
    def open_containing(klass, url, possible_transports=None):
 
727
        """Open an existing branch which contains url.
 
728
 
 
729
        :param url: url to search from.
 
730
 
 
731
        See open_containing_from_transport for more detail.
 
732
        """
 
733
        transport = _mod_transport.get_transport(url, possible_transports)
 
734
        return klass.open_containing_from_transport(transport)
 
735
 
 
736
    @classmethod
 
737
    def open_containing_from_transport(klass, a_transport):
 
738
        """Open an existing branch which contains a_transport.base.
 
739
 
 
740
        This probes for a branch at a_transport, and searches upwards from there.
 
741
 
 
742
        Basically we keep looking up until we find the control directory or
 
743
        run into the root.  If there isn't one, raises NotBranchError.
 
744
        If there is one and it is either an unrecognised format or an unsupported
 
745
        format, UnknownFormatError or UnsupportedFormatError are raised.
 
746
        If there is one, it is returned, along with the unused portion of url.
 
747
 
 
748
        :return: The ControlDir that contains the path, and a Unicode path
 
749
                for the rest of the URL.
 
750
        """
 
751
        # this gets the normalised url back. I.e. '.' -> the full path.
 
752
        url = a_transport.base
 
753
        while True:
 
754
            try:
 
755
                result = klass.open_from_transport(a_transport)
 
756
                return result, urlutils.unescape(a_transport.relpath(url))
 
757
            except errors.NotBranchError, e:
 
758
                pass
 
759
            except errors.PermissionDenied:
 
760
                pass
 
761
            try:
 
762
                new_t = a_transport.clone('..')
 
763
            except errors.InvalidURLJoin:
 
764
                # reached the root, whatever that may be
 
765
                raise errors.NotBranchError(path=url)
 
766
            if new_t.base == a_transport.base:
 
767
                # reached the root, whatever that may be
 
768
                raise errors.NotBranchError(path=url)
 
769
            a_transport = new_t
 
770
 
 
771
    @classmethod
 
772
    def open_tree_or_branch(klass, location):
 
773
        """Return the branch and working tree at a location.
 
774
 
 
775
        If there is no tree at the location, tree will be None.
 
776
        If there is no branch at the location, an exception will be
 
777
        raised
 
778
        :return: (tree, branch)
 
779
        """
 
780
        controldir = klass.open(location)
 
781
        return controldir._get_tree_branch()
 
782
 
 
783
    @classmethod
 
784
    def open_containing_tree_or_branch(klass, location,
 
785
            possible_transports=None):
 
786
        """Return the branch and working tree contained by a location.
 
787
 
 
788
        Returns (tree, branch, relpath).
 
789
        If there is no tree at containing the location, tree will be None.
 
790
        If there is no branch containing the location, an exception will be
 
791
        raised
 
792
        relpath is the portion of the path that is contained by the branch.
 
793
        """
 
794
        controldir, relpath = klass.open_containing(location,
 
795
            possible_transports=possible_transports)
 
796
        tree, branch = controldir._get_tree_branch()
 
797
        return tree, branch, relpath
 
798
 
 
799
    @classmethod
 
800
    def open_containing_tree_branch_or_repository(klass, location):
 
801
        """Return the working tree, branch and repo contained by a location.
 
802
 
 
803
        Returns (tree, branch, repository, relpath).
 
804
        If there is no tree containing the location, tree will be None.
 
805
        If there is no branch containing the location, branch will be None.
 
806
        If there is no repository containing the location, repository will be
 
807
        None.
 
808
        relpath is the portion of the path that is contained by the innermost
 
809
        ControlDir.
 
810
 
 
811
        If no tree, branch or repository is found, a NotBranchError is raised.
 
812
        """
 
813
        controldir, relpath = klass.open_containing(location)
 
814
        try:
 
815
            tree, branch = controldir._get_tree_branch()
 
816
        except errors.NotBranchError:
 
817
            try:
 
818
                repo = controldir.find_repository()
 
819
                return None, None, repo, relpath
 
820
            except (errors.NoRepositoryPresent):
 
821
                raise errors.NotBranchError(location)
 
822
        return tree, branch, branch.repository, relpath
 
823
 
 
824
    @classmethod
 
825
    def create(klass, base, format=None, possible_transports=None):
 
826
        """Create a new ControlDir at the url 'base'.
 
827
 
 
828
        :param format: If supplied, the format of branch to create.  If not
 
829
            supplied, the default is used.
 
830
        :param possible_transports: If supplied, a list of transports that
 
831
            can be reused to share a remote connection.
 
832
        """
 
833
        if klass is not ControlDir:
 
834
            raise AssertionError("ControlDir.create always creates the"
 
835
                "default format, not one of %r" % klass)
 
836
        t = _mod_transport.get_transport(base, possible_transports)
 
837
        t.ensure_base()
 
838
        if format is None:
 
839
            format = ControlDirFormat.get_default_format()
 
840
        return format.initialize_on_transport(t)
 
841
 
 
842
 
 
843
class ControlDirHooks(hooks.Hooks):
 
844
    """Hooks for ControlDir operations."""
 
845
 
 
846
    def __init__(self):
 
847
        """Create the default hooks."""
 
848
        hooks.Hooks.__init__(self, "bzrlib.controldir", "ControlDir.hooks")
 
849
        self.add_hook('pre_open',
 
850
            "Invoked before attempting to open a ControlDir with the transport "
 
851
            "that the open will use.", (1, 14))
 
852
        self.add_hook('post_repo_init',
 
853
            "Invoked after a repository has been initialized. "
 
854
            "post_repo_init is called with a "
 
855
            "bzrlib.controldir.RepoInitHookParams.",
 
856
            (2, 2))
 
857
 
 
858
# install the default hooks
 
859
ControlDir.hooks = ControlDirHooks()
 
860
 
466
861
 
467
862
class ControlComponentFormat(object):
468
 
    """A component that can live inside of a .bzr meta directory."""
 
863
    """A component that can live inside of a control directory."""
469
864
 
470
865
    upgrade_recommended = False
471
866
 
472
 
    def get_format_string(self):
473
 
        """Return the format of this format, if usable in meta directories."""
474
 
        raise NotImplementedError(self.get_format_string)
475
 
 
476
867
    def get_format_description(self):
477
868
        """Return the short description for this format."""
478
869
        raise NotImplementedError(self.get_format_description)
505
896
            ui.ui_factory.recommend_upgrade(
506
897
                self.get_format_description(), basedir)
507
898
 
 
899
    @classmethod
 
900
    def get_format_string(cls):
 
901
        raise NotImplementedError(cls.get_format_string)
 
902
 
508
903
 
509
904
class ControlComponentFormatRegistry(registry.FormatRegistry):
510
905
    """A registry for control components (branch, workingtree, repository)."""
661
1056
    def is_supported(self):
662
1057
        """Is this format supported?
663
1058
 
664
 
        Supported formats must be initializable and openable.
 
1059
        Supported formats must be openable.
665
1060
        Unsupported formats may not support initialization or committing or
666
1061
        some other features depending on the reason for not being supported.
667
1062
        """
668
1063
        return True
669
1064
 
 
1065
    def is_initializable(self):
 
1066
        """Whether new control directories of this format can be initialized.
 
1067
        """
 
1068
        return self.is_supported()
 
1069
 
670
1070
    def check_support_status(self, allow_unsupported, recommend_upgrade=True,
671
1071
        basedir=None):
672
1072
        """Give an error or warning on old formats.
728
1128
        return self.get_format_description().rstrip()
729
1129
 
730
1130
    @classmethod
 
1131
    def all_probers(klass):
 
1132
        return klass._server_probers + klass._probers
 
1133
 
 
1134
    @classmethod
731
1135
    def known_formats(klass):
732
1136
        """Return all the known formats.
733
1137
        """
734
1138
        result = set()
735
 
        for prober_kls in klass._probers + klass._server_probers:
 
1139
        for prober_kls in klass.all_probers():
736
1140
            result.update(prober_kls.known_formats())
737
1141
        return result
738
1142
 
739
1143
    @classmethod
740
 
    def find_format(klass, transport, _server_formats=True):
 
1144
    def find_format(klass, transport, probers=None):
741
1145
        """Return the format present at transport."""
742
 
        if _server_formats:
743
 
            _probers = klass._server_probers + klass._probers
744
 
        else:
745
 
            _probers = klass._probers
746
 
        for prober_kls in _probers:
 
1146
        if probers is None:
 
1147
            probers = klass.all_probers()
 
1148
        for prober_kls in probers:
747
1149
            prober = prober_kls()
748
1150
            try:
749
1151
                return prober.probe_transport(transport)
828
1230
        """Return the current default format."""
829
1231
        return klass._default_format
830
1232
 
 
1233
    def supports_transport(self, transport):
 
1234
        """Check if this format can be opened over a particular transport.
 
1235
        """
 
1236
        raise NotImplementedError(self.supports_transport)
 
1237
 
831
1238
 
832
1239
class Prober(object):
833
1240
    """Abstract class that can be used to detect a particular kind of
856
1263
        raise NotImplementedError(self.probe_transport)
857
1264
 
858
1265
    @classmethod
859
 
    def known_formats(cls):
 
1266
    def known_formats(klass):
860
1267
        """Return the control dir formats known by this prober.
861
1268
 
862
1269
        Multiple probers can return the same formats, so this should
864
1271
 
865
1272
        :return: A set of known formats.
866
1273
        """
867
 
        raise NotImplementedError(cls.known_formats)
 
1274
        raise NotImplementedError(klass.known_formats)
868
1275
 
869
1276
 
870
1277
class ControlDirFormatInfo(object):
1003
1410
            return output
1004
1411
 
1005
1412
 
 
1413
class RepoInitHookParams(object):
 
1414
    """Object holding parameters passed to `*_repo_init` hooks.
 
1415
 
 
1416
    There are 4 fields that hooks may wish to access:
 
1417
 
 
1418
    :ivar repository: Repository created
 
1419
    :ivar format: Repository format
 
1420
    :ivar bzrdir: The controldir for the repository
 
1421
    :ivar shared: The repository is shared
 
1422
    """
 
1423
 
 
1424
    def __init__(self, repository, format, controldir, shared):
 
1425
        """Create a group of RepoInitHook parameters.
 
1426
 
 
1427
        :param repository: Repository created
 
1428
        :param format: Repository format
 
1429
        :param controldir: The controldir for the repository
 
1430
        :param shared: The repository is shared
 
1431
        """
 
1432
        self.repository = repository
 
1433
        self.format = format
 
1434
        self.bzrdir = controldir
 
1435
        self.shared = shared
 
1436
 
 
1437
    def __eq__(self, other):
 
1438
        return self.__dict__ == other.__dict__
 
1439
 
 
1440
    def __repr__(self):
 
1441
        if self.repository:
 
1442
            return "<%s for %s>" % (self.__class__.__name__,
 
1443
                self.repository)
 
1444
        else:
 
1445
            return "<%s for %s>" % (self.__class__.__name__,
 
1446
                self.bzrdir)
 
1447
 
 
1448
 
1006
1449
# Please register new formats after old formats so that formats
1007
1450
# appear in chronological order and format descriptions can build
1008
1451
# on previous ones.