/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/reconfigure.py

  • Committer: Robert Collins
  • Date: 2008-01-06 20:04:22 UTC
  • mto: (3221.11.1 StackableBranch)
  • mto: This revision was merged to the branch mainline in revision 3226.
  • Revision ID: robertc@robertcollins.net-20080106200422-x8yz6cxotlzltvwp
The bzrdir format registry now accepts an ``alias`` keyword to
register_metadir, used to indicate that a format name is an alias for
some other format and thus should not be reported when describing the
format. (Robert Collins)
-------------- This line and the fmllowing will be ignored --------------

modified:
  NEWS
  bzrlib/bzrdir.py
  bzrlib/info.py
  bzrlib/tests/test_bzrdir.py
  bzrlib/tests/test_info.py

=== modified file 'NEWS'
--- a/NEWS      2008-01-02 22:30:46 +0000
+++ b/NEWS      2008-01-06 20:04:15 +0000
@@ -135,6 +135,11 @@
     * Patience Diff now supports arbitrary python objects, as long as they
       support ``hash()``. (John Arbash Meinel)
 
+    * The bzrdir format registry now accepts an ``alias`` keyword to
+      register_metadir, used to indicate that a format name is an alias for
+      some other format and thus should not be reported when describing the
+      format. (Robert Collins)
+
   API BREAKS:
 
   TESTING:

=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py  2008-01-02 22:30:46 +0000
+++ b/bzrlib/bzrdir.py  2008-01-06 19:41:29 +0000
@@ -2447,12 +2447,22 @@
     e.g. BzrDirMeta1 with weave repository.  Also, it's more user-oriented.
     """
 
+    def __init__(self):
+        """Create a BzrDirFormatRegistry."""
+        self._aliases = set()
+        super(BzrDirFormatRegistry, self).__init__()
+
+    def aliases(self):
+        """Return a set of the format names which are aliases."""
+        return frozenset(self._aliases)
+
     def register_metadir(self, key,
              repository_format, help, native=True, deprecated=False,
              branch_format=None,
              tree_format=None,
              hidden=False,
-             experimental=False):
+             experimental=False,
+             alias=False):
         """Register a metadir subformat.
 
         These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
@@ -2491,10 +2501,10 @@
                 bd.repository_format = _load(repository_format)
             return bd
         self.register(key, helper, help, native, deprecated, hidden,
-            experimental)
+            experimental, alias)
 
     def register(self, key, factory, help, native=True, deprecated=False,
-                 hidden=False, experimental=False):
+                 hidden=False, experimental=False, alias=False):
         """Register a BzrDirFormat factory.
         
         The factory must be a callable that takes one parameter: the key.
@@ -2505,11 +2515,15 @@
         """
         registry.Registry.register(self, key, factory, help,
             BzrDirFormatInfo(native, deprecated, hidden, experimental))
+        if alias:
+            self._aliases.add(key)
 
     def register_lazy(self, key, module_name, member_name, help, native=True,
-                      deprecated=False, hidden=False, experimental=False):
+        deprecated=False, hidden=False, experimental=False, alias=False):
         registry.Registry.register_lazy(self, key, module_name, member_name,
             help, BzrDirFormatInfo(native, deprecated, hidden, experimental))
+        if alias:
+            self._aliases.add(key)
 
     def set_default(self, key):
         """Set the 'default' key to be a clone of the supplied key.
@@ -2518,6 +2532,7 @@
         """
         registry.Registry.register(self, 'default', self.get(key),
             self.get_help(key), info=self.get_info(key))
+        self._aliases.add('default')
 
     def set_default_repository(self, key):
         """Set the FormatRegistry default and Repository default.
@@ -2670,6 +2685,7 @@
     tree_format='bzrlib.workingtree.WorkingTreeFormat4',
     hidden=False,
     )
+# The following two formats should always just be aliases.
 format_registry.register_metadir('development',
     'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0',
     help='Current development format. Can convert data to and from pack-0.92 '
@@ -2681,6 +2697,7 @@
     branch_format='bzrlib.branch.BzrBranchFormat6',
     tree_format='bzrlib.workingtree.WorkingTreeFormat4',
     experimental=True,
+    alias=True,
     )
 format_registry.register_metadir('development-subtree',
     'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0Subtree',
@@ -2693,7 +2710,9 @@
     branch_format='bzrlib.branch.BzrBranchFormat6',
     tree_format='bzrlib.workingtree.WorkingTreeFormat4',
     experimental=True,
+    alias=True,
     )
+# And the development formats which the will have aliased one of follow:
 format_registry.register_metadir('development0',
     'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0',
     help='Trivial rename of pack-0.92 to provide a development format. '

=== modified file 'bzrlib/info.py'
--- a/bzrlib/info.py    2007-11-06 09:00:25 +0000
+++ b/bzrlib/info.py    2008-01-06 20:01:30 +0000
@@ -440,7 +440,9 @@
         tree.bzrdir.root_transport.base):
         branch = None
         repository = None
-    for key in bzrdir.format_registry.keys():
+    non_aliases = set(bzrdir.format_registry.keys())
+    non_aliases.difference_update(bzrdir.format_registry.aliases())
+    for key in non_aliases:
         format = bzrdir.format_registry.make_bzrdir(key)
         if isinstance(format, bzrdir.BzrDirMetaFormat1):
             if (tree and format.workingtree_format !=
@@ -457,11 +459,12 @@
         candidates.append(key)
     if len(candidates) == 0:
         return 'unnamed'
-    new_candidates = [c for c in candidates if c != 'default']
-    if len(new_candidates) > 0:
-        candidates = new_candidates
+    candidates.sort()
     new_candidates = [c for c in candidates if not
         bzrdir.format_registry.get_info(c).hidden]
     if len(new_candidates) > 0:
+        # If there are any non-hidden formats that match, only return those to
+        # avoid listing hidden formats except when only a hidden format will
+        # do.
         candidates = new_candidates
     return ' or '.join(candidates)

=== modified file 'bzrlib/tests/test_bzrdir.py'
--- a/bzrlib/tests/test_bzrdir.py       2007-12-21 20:32:22 +0000
+++ b/bzrlib/tests/test_bzrdir.py       2008-01-06 19:45:00 +0000
@@ -170,6 +170,16 @@
         finally:
             bzrdir.format_registry.set_default_repository(old_default)
 
+    def test_aliases(self):
+        a_registry = bzrdir.BzrDirFormatRegistry()
+        a_registry.register('weave', bzrdir.BzrDirFormat6,
+            'Pre-0.8 format.  Slower and does not support checkouts or shared'
+            ' repositories', deprecated=True)
+        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
+            'Pre-0.8 format.  Slower and does not support checkouts or shared'
+            ' repositories', deprecated=True, alias=True)
+        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
+    
 
 class SampleBranch(bzrlib.branch.Branch):
     """A dummy branch for guess what, dummy use."""

=== modified file 'bzrlib/tests/test_info.py'
--- a/bzrlib/tests/test_info.py 2007-11-26 13:55:51 +0000
+++ b/bzrlib/tests/test_info.py 2008-01-06 20:02:10 +0000
@@ -126,16 +126,22 @@
 
     def test_describe_tree_format(self):
         for key in bzrdir.format_registry.keys():
-            if key == 'default':
+            if key in bzrdir.format_registry.aliases():
                 continue
             self.assertTreeDescription(key)
 
     def test_describe_checkout_format(self):
         for key in bzrdir.format_registry.keys():
-            if key in ('default', 'weave', 'experimental'):
-                continue
-            if key.startswith('experimental-'):
-                # these are typically hidden or aliases for other formats
+            if key in bzrdir.format_registry.aliases():
+                # Aliases will not describe correctly in the UI because the
+                # real format is found.
+                continue
+            # legacy: weave does not support checkouts
+            if key == 'weave':
+                continue
+            if bzrdir.format_registry.get_info(key).experimental:
+                # We don't require that experimental formats support checkouts
+                # or describe correctly in the UI.
                 continue
             expected = None
             if key in ('dirstate', 'dirstate-tags', 'dirstate-with-subtree',
@@ -149,7 +155,7 @@
 
     def test_describe_branch_format(self):
         for key in bzrdir.format_registry.keys():
-            if key == 'default':
+            if key in bzrdir.format_registry.aliases():
                 continue
             expected = None
             if key in ('dirstate', 'knit'):
@@ -158,7 +164,7 @@
 
     def test_describe_repo_format(self):
         for key in bzrdir.format_registry.keys():
-            if key == 'default':
+            if key in bzrdir.format_registry.aliases():
                 continue
             expected = None
             if key in ('dirstate', 'knit', 'dirstate-tags'):

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
"""Reconfigure a bzrdir into a new tree/branch/repository layout.
18
 
 
19
 
Various types of reconfiguration operation are available either by
20
 
constructing a class or using a factory method on Reconfigure.
21
 
"""
22
 
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
23
18
 
24
19
from bzrlib import (
25
20
    branch,
26
 
    bzrdir,
27
21
    errors,
28
 
    trace,
29
 
    ui,
30
 
    urlutils,
31
22
    )
32
23
 
33
 
 
34
 
# TODO: common base class for all reconfigure operations, making no
35
 
# assumptions about what kind of change will be done.
36
 
 
37
 
 
38
 
class ReconfigureStackedOn(object):
39
 
    """Reconfigures a branch to be stacked on another branch."""
40
 
 
41
 
    def apply(self, bzrdir, stacked_on_url):
42
 
        branch = bzrdir.open_branch()
43
 
        # it may be a path relative to the cwd or a url; the branch wants
44
 
        # a path relative to itself...
45
 
        on_url = urlutils.relative_url(branch.base,
46
 
            urlutils.normalize_url(stacked_on_url))
47
 
        branch.lock_write()
48
 
        try:
49
 
            branch.set_stacked_on_url(on_url)
50
 
            if not trace.is_quiet():
51
 
                ui.ui_factory.note(
52
 
                    "%s is now stacked on %s\n"
53
 
                    % (branch.base, branch.get_stacked_on_url()))
54
 
        finally:
55
 
            branch.unlock()
56
 
 
57
 
 
58
 
class ReconfigureUnstacked(object):
59
 
 
60
 
    def apply(self, bzrdir):
61
 
        branch = bzrdir.open_branch()
62
 
        branch.lock_write()
63
 
        try:
64
 
            branch.set_stacked_on_url(None)
65
 
            if not trace.is_quiet():
66
 
                ui.ui_factory.note(
67
 
                    "%s is now not stacked\n"
68
 
                    % (branch.base,))
69
 
        finally:
70
 
            branch.unlock()
71
 
 
72
 
 
73
24
class Reconfigure(object):
74
25
 
75
26
    def __init__(self, bzrdir, new_bound_location=None):
76
27
        self.bzrdir = bzrdir
77
28
        self.new_bound_location = new_bound_location
78
 
        self.local_repository = None
79
29
        try:
80
30
            self.repository = self.bzrdir.find_repository()
81
31
        except errors.NoRepositoryPresent:
82
32
            self.repository = None
83
 
            self.local_repository = None
84
 
        else:
85
 
            if (self.repository.user_url == self.bzrdir.user_url):
86
 
                self.local_repository = self.repository
87
 
            else:
88
 
                self.local_repository = None
89
33
        try:
90
34
            branch = self.bzrdir.open_branch()
91
 
            if branch.user_url == bzrdir.user_url:
 
35
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
92
36
                self.local_branch = branch
93
37
                self.referenced_branch = None
94
38
            else:
111
55
        self._create_tree = False
112
56
        self._create_repository = False
113
57
        self._destroy_repository = False
114
 
        self._repository_trees = None
115
58
 
116
59
    @staticmethod
117
60
    def to_branch(bzrdir):
172
115
            raise errors.AlreadyLightweightCheckout(bzrdir)
173
116
        return reconfiguration
174
117
 
175
 
    @classmethod
176
 
    def to_use_shared(klass, bzrdir):
177
 
        """Convert a standalone branch into a repository branch"""
178
 
        reconfiguration = klass(bzrdir)
179
 
        reconfiguration._set_use_shared(use_shared=True)
180
 
        if not reconfiguration.changes_planned():
181
 
            raise errors.AlreadyUsingShared(bzrdir)
182
 
        return reconfiguration
183
 
 
184
 
    @classmethod
185
 
    def to_standalone(klass, bzrdir):
186
 
        """Convert a repository branch into a standalone branch"""
187
 
        reconfiguration = klass(bzrdir)
188
 
        reconfiguration._set_use_shared(use_shared=False)
189
 
        if not reconfiguration.changes_planned():
190
 
            raise errors.AlreadyStandalone(bzrdir)
191
 
        return reconfiguration
192
 
 
193
 
    @classmethod
194
 
    def set_repository_trees(klass, bzrdir, with_trees):
195
 
        """Adjust a repository's working tree presence default"""
196
 
        reconfiguration = klass(bzrdir)
197
 
        if not reconfiguration.repository.is_shared():
198
 
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
199
 
        if with_trees and reconfiguration.repository.make_working_trees():
200
 
            raise errors.AlreadyWithTrees(bzrdir)
201
 
        elif (not with_trees
202
 
              and not reconfiguration.repository.make_working_trees()):
203
 
            raise errors.AlreadyWithNoTrees(bzrdir)
204
 
        else:
205
 
            reconfiguration._repository_trees = with_trees
206
 
        return reconfiguration
207
 
 
208
118
    def _plan_changes(self, want_tree, want_branch, want_bound,
209
119
                      want_reference):
210
120
        """Determine which changes are needed to assume the configuration"""
216
126
            if not want_reference:
217
127
                self._create_repository = True
218
128
        else:
219
 
            if want_reference and (
220
 
                self.repository.user_url == self.bzrdir.user_url):
 
129
            if want_reference and (self.repository.bzrdir.root_transport.base
 
130
                                   == self.bzrdir.root_transport.base):
221
131
                if not self.repository.is_shared():
222
132
                    self._destroy_repository = True
223
133
        if self.referenced_branch is None:
224
134
            if want_reference:
225
135
                self._create_reference = True
226
 
                if self.local_branch is not None:
227
 
                    self._destroy_branch = True
228
136
        else:
229
137
            if not want_reference:
230
138
                self._destroy_reference = True
245
153
        if want_tree and self.tree is None:
246
154
            self._create_tree = True
247
155
 
248
 
    def _set_use_shared(self, use_shared=None):
249
 
        if use_shared is None:
250
 
            return
251
 
        if use_shared:
252
 
            if self.local_repository is not None:
253
 
                self._destroy_repository = True
254
 
        else:
255
 
            if self.local_repository is None:
256
 
                self._create_repository = True
257
 
 
258
156
    def changes_planned(self):
259
157
        """Return True if changes are planned, False otherwise"""
260
158
        return (self._unbind or self._bind or self._destroy_tree
261
159
                or self._create_tree or self._destroy_reference
262
160
                or self._create_branch or self._create_repository
263
 
                or self._create_reference or self._destroy_repository)
 
161
                or self._create_reference)
264
162
 
265
163
    def _check(self):
266
164
        """Raise if reconfiguration would destroy local changes"""
267
 
        if self._destroy_tree and self.tree.has_changes():
 
165
        if self._destroy_tree:
 
166
            changes = self.tree.changes_from(self.tree.basis_tree())
 
167
            if changes.has_changed():
268
168
                raise errors.UncommittedChanges(self.tree)
269
 
        if self._create_reference and self.local_branch is not None:
270
 
            reference_branch = branch.Branch.open(self._select_bind_location())
271
 
            if (reference_branch.last_revision() !=
272
 
                self.local_branch.last_revision()):
273
 
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
274
169
 
275
170
    def _select_bind_location(self):
276
171
        """Select a location to bind or create a reference to.
315
210
        if not force:
316
211
            self._check()
317
212
        if self._create_repository:
318
 
            if self.local_branch and not self._destroy_branch:
319
 
                old_repo = self.local_branch.repository
320
 
            elif self._create_branch and self.referenced_branch is not None:
321
 
                old_repo = self.referenced_branch.repository
322
 
            else:
323
 
                old_repo = None
324
 
            if old_repo is not None:
325
 
                repository_format = old_repo._format
326
 
            else:
327
 
                repository_format = None
328
 
            if repository_format is not None:
329
 
                repo = repository_format.initialize(self.bzrdir)
330
 
            else:
331
 
                repo = self.bzrdir.create_repository()
332
 
            if self.local_branch and not self._destroy_branch:
333
 
                repo.fetch(self.local_branch.repository,
334
 
                           self.local_branch.last_revision())
 
213
            repo = self.bzrdir.create_repository()
335
214
        else:
336
215
            repo = self.repository
337
216
        if self._create_branch and self.referenced_branch is not None:
338
217
            repo.fetch(self.referenced_branch.repository,
339
218
                       self.referenced_branch.last_revision())
340
 
        if self._create_reference:
341
 
            reference_branch = branch.Branch.open(self._select_bind_location())
342
 
        if self._destroy_repository:
343
 
            if self._create_reference:
344
 
                reference_branch.repository.fetch(self.repository)
345
 
            elif self.local_branch is not None and not self._destroy_branch:
346
 
                up = self.local_branch.user_transport.clone('..')
347
 
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
348
 
                new_repo = up_bzrdir.find_repository()
349
 
                new_repo.fetch(self.repository)
350
219
        last_revision_info = None
351
220
        if self._destroy_reference:
352
221
            last_revision_info = self.referenced_branch.last_revision_info()
353
222
            self.bzrdir.destroy_branch()
354
223
        if self._destroy_branch:
355
224
            last_revision_info = self.local_branch.last_revision_info()
356
 
            if self._create_reference:
357
 
                self.local_branch.tags.merge_to(reference_branch.tags)
358
225
            self.bzrdir.destroy_branch()
359
226
        if self._create_branch:
360
227
            local_branch = self.bzrdir.create_branch()
361
228
            if last_revision_info is not None:
362
229
                local_branch.set_last_revision_info(*last_revision_info)
363
 
            if self._destroy_reference:
364
 
                self.referenced_branch.tags.merge_to(local_branch.tags)
365
 
                self.referenced_branch.update_references(local_branch)
366
230
        else:
367
231
            local_branch = self.local_branch
368
232
        if self._create_reference:
 
233
            reference_branch = branch.Branch.open(self._select_bind_location())
369
234
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
370
 
                target_branch=reference_branch)
 
235
                reference_branch)
371
236
        if self._destroy_tree:
372
237
            self.bzrdir.destroy_workingtree()
373
238
        if self._create_tree:
378
243
            bind_location = self._select_bind_location()
379
244
            local_branch.bind(branch.Branch.open(bind_location))
380
245
        if self._destroy_repository:
 
246
            if self._create_reference:
 
247
                reference_branch.repository.fetch(self.repository)
381
248
            self.bzrdir.destroy_repository()
382
 
        if self._repository_trees is not None:
383
 
            repo.set_make_working_trees(self._repository_trees)