/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: Canonical.com Patch Queue Manager
  • Date: 2010-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007, 2009 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Reconfigure a controldir into a new tree/branch/repository layout.
 
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout.
18
18
 
19
19
Various types of reconfiguration operation are available either by
20
20
constructing a class or using a factory method on Reconfigure.
21
21
"""
22
22
 
23
 
from __future__ import absolute_import
24
 
 
25
 
 
26
 
from . import (
 
23
 
 
24
from bzrlib import (
27
25
    branch,
28
 
    controldir,
 
26
    bzrdir,
29
27
    errors,
30
28
    trace,
31
29
    ui,
32
30
    urlutils,
33
31
    )
34
 
from .i18n import gettext
 
32
 
35
33
 
36
34
# TODO: common base class for all reconfigure operations, making no
37
35
# assumptions about what kind of change will be done.
38
36
 
39
37
 
40
 
class BzrDirError(errors.BzrError):
41
 
 
42
 
    def __init__(self, controldir):
43
 
        display_url = urlutils.unescape_for_display(controldir.user_url,
44
 
                                                    'ascii')
45
 
        errors.BzrError.__init__(self, controldir=controldir,
46
 
                                 display_url=display_url)
47
 
 
48
 
 
49
 
class NoBindLocation(BzrDirError):
50
 
 
51
 
    _fmt = "No location could be found to bind to at %(display_url)s."
52
 
 
53
 
 
54
 
class UnsyncedBranches(BzrDirError):
55
 
 
56
 
    _fmt = ("'%(display_url)s' is not in sync with %(target_url)s.  See"
57
 
            " brz help sync-for-reconfigure.")
58
 
 
59
 
    def __init__(self, controldir, target_branch):
60
 
        errors.BzrError.__init__(self, controldir)
61
 
        from . import urlutils
62
 
        self.target_url = urlutils.unescape_for_display(target_branch.base,
63
 
                                                        'ascii')
64
 
 
65
 
 
66
 
class AlreadyBranch(BzrDirError):
67
 
 
68
 
    _fmt = "'%(display_url)s' is already a branch."
69
 
 
70
 
 
71
 
class AlreadyTree(BzrDirError):
72
 
 
73
 
    _fmt = "'%(display_url)s' is already a tree."
74
 
 
75
 
 
76
 
class AlreadyCheckout(BzrDirError):
77
 
 
78
 
    _fmt = "'%(display_url)s' is already a checkout."
79
 
 
80
 
 
81
 
class AlreadyLightweightCheckout(BzrDirError):
82
 
 
83
 
    _fmt = "'%(display_url)s' is already a lightweight checkout."
84
 
 
85
 
 
86
 
class AlreadyUsingShared(BzrDirError):
87
 
 
88
 
    _fmt = "'%(display_url)s' is already using a shared repository."
89
 
 
90
 
 
91
 
class AlreadyStandalone(BzrDirError):
92
 
 
93
 
    _fmt = "'%(display_url)s' is already standalone."
94
 
 
95
 
 
96
 
class AlreadyWithTrees(BzrDirError):
97
 
 
98
 
    _fmt = ("Shared repository '%(display_url)s' already creates "
99
 
            "working trees.")
100
 
 
101
 
 
102
 
class AlreadyWithNoTrees(BzrDirError):
103
 
 
104
 
    _fmt = ("Shared repository '%(display_url)s' already doesn't create "
105
 
            "working trees.")
106
 
 
107
 
 
108
 
class ReconfigurationNotSupported(BzrDirError):
109
 
 
110
 
    _fmt = "Requested reconfiguration of '%(display_url)s' is not supported."
111
 
 
112
 
 
113
38
class ReconfigureStackedOn(object):
114
39
    """Reconfigures a branch to be stacked on another branch."""
115
40
 
116
 
    def apply(self, controldir, stacked_on_url):
117
 
        branch = controldir.open_branch()
 
41
    def apply(self, bzrdir, stacked_on_url):
 
42
        branch = bzrdir.open_branch()
118
43
        # it may be a path relative to the cwd or a url; the branch wants
119
44
        # a path relative to itself...
120
45
        on_url = urlutils.relative_url(branch.base,
121
 
                                       urlutils.normalize_url(stacked_on_url))
122
 
        with branch.lock_write():
 
46
            urlutils.normalize_url(stacked_on_url))
 
47
        branch.lock_write()
 
48
        try:
123
49
            branch.set_stacked_on_url(on_url)
124
50
            if not trace.is_quiet():
125
 
                ui.ui_factory.note(gettext(
126
 
                    "{0} is now stacked on {1}\n").format(
127
 
                    branch.base, branch.get_stacked_on_url()))
 
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()
128
56
 
129
57
 
130
58
class ReconfigureUnstacked(object):
131
59
 
132
 
    def apply(self, controldir):
133
 
        branch = controldir.open_branch()
134
 
        with branch.lock_write():
 
60
    def apply(self, bzrdir):
 
61
        branch = bzrdir.open_branch()
 
62
        branch.lock_write()
 
63
        try:
135
64
            branch.set_stacked_on_url(None)
136
65
            if not trace.is_quiet():
137
 
                ui.ui_factory.note(gettext(
138
 
                    "%s is now not stacked\n")
 
66
                ui.ui_factory.note(
 
67
                    "%s is now not stacked\n"
139
68
                    % (branch.base,))
 
69
        finally:
 
70
            branch.unlock()
140
71
 
141
72
 
142
73
class Reconfigure(object):
143
74
 
144
 
    def __init__(self, controldir, new_bound_location=None):
145
 
        self.controldir = controldir
 
75
    def __init__(self, bzrdir, new_bound_location=None):
 
76
        self.bzrdir = bzrdir
146
77
        self.new_bound_location = new_bound_location
147
78
        self.local_repository = None
148
79
        try:
149
 
            self.repository = self.controldir.find_repository()
 
80
            self.repository = self.bzrdir.find_repository()
150
81
        except errors.NoRepositoryPresent:
151
82
            self.repository = None
152
83
            self.local_repository = None
153
84
        else:
154
 
            if (self.repository.user_url == self.controldir.user_url):
 
85
            if (self.repository.bzrdir.root_transport.base ==
 
86
                self.bzrdir.root_transport.base):
155
87
                self.local_repository = self.repository
156
88
            else:
157
89
                self.local_repository = None
158
90
        try:
159
 
            branch = self.controldir.open_branch()
160
 
            if branch.user_url == controldir.user_url:
 
91
            branch = self.bzrdir.open_branch()
 
92
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
161
93
                self.local_branch = branch
162
94
                self.referenced_branch = None
163
95
            else:
167
99
            self.local_branch = None
168
100
            self.referenced_branch = None
169
101
        try:
170
 
            self.tree = controldir.open_workingtree()
 
102
            self.tree = bzrdir.open_workingtree()
171
103
        except errors.NoWorkingTree:
172
104
            self.tree = None
173
105
        self._unbind = False
183
115
        self._repository_trees = None
184
116
 
185
117
    @staticmethod
186
 
    def to_branch(controldir):
187
 
        """Return a Reconfiguration to convert this controldir into a branch
 
118
    def to_branch(bzrdir):
 
119
        """Return a Reconfiguration to convert this bzrdir into a branch
188
120
 
189
 
        :param controldir: The controldir to reconfigure
190
 
        :raise AlreadyBranch: if controldir is already a branch
 
121
        :param bzrdir: The bzrdir to reconfigure
 
122
        :raise errors.AlreadyBranch: if bzrdir is already a branch
191
123
        """
192
 
        reconfiguration = Reconfigure(controldir)
 
124
        reconfiguration = Reconfigure(bzrdir)
193
125
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
194
126
                                      want_bound=False, want_reference=False)
195
127
        if not reconfiguration.changes_planned():
196
 
            raise AlreadyBranch(controldir)
 
128
            raise errors.AlreadyBranch(bzrdir)
197
129
        return reconfiguration
198
130
 
199
131
    @staticmethod
200
 
    def to_tree(controldir):
201
 
        """Return a Reconfiguration to convert this controldir into a tree
 
132
    def to_tree(bzrdir):
 
133
        """Return a Reconfiguration to convert this bzrdir into a tree
202
134
 
203
 
        :param controldir: The controldir to reconfigure
204
 
        :raise AlreadyTree: if controldir is already a tree
 
135
        :param bzrdir: The bzrdir to reconfigure
 
136
        :raise errors.AlreadyTree: if bzrdir is already a tree
205
137
        """
206
 
        reconfiguration = Reconfigure(controldir)
 
138
        reconfiguration = Reconfigure(bzrdir)
207
139
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
208
140
                                      want_bound=False, want_reference=False)
209
141
        if not reconfiguration.changes_planned():
210
 
            raise AlreadyTree(controldir)
 
142
            raise errors.AlreadyTree(bzrdir)
211
143
        return reconfiguration
212
144
 
213
145
    @staticmethod
214
 
    def to_checkout(controldir, bound_location=None):
215
 
        """Return a Reconfiguration to convert this controldir into a checkout
 
146
    def to_checkout(bzrdir, bound_location=None):
 
147
        """Return a Reconfiguration to convert this bzrdir into a checkout
216
148
 
217
 
        :param controldir: The controldir to reconfigure
 
149
        :param bzrdir: The bzrdir to reconfigure
218
150
        :param bound_location: The location the checkout should be bound to.
219
 
        :raise AlreadyCheckout: if controldir is already a checkout
 
151
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
220
152
        """
221
 
        reconfiguration = Reconfigure(controldir, bound_location)
 
153
        reconfiguration = Reconfigure(bzrdir, bound_location)
222
154
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
223
155
                                      want_bound=True, want_reference=False)
224
156
        if not reconfiguration.changes_planned():
225
 
            raise AlreadyCheckout(controldir)
 
157
            raise errors.AlreadyCheckout(bzrdir)
226
158
        return reconfiguration
227
159
 
228
160
    @classmethod
229
 
    def to_lightweight_checkout(klass, controldir, reference_location=None):
230
 
        """Make a Reconfiguration to convert controldir into a lightweight checkout
 
161
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
 
162
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
231
163
 
232
 
        :param controldir: The controldir to reconfigure
 
164
        :param bzrdir: The bzrdir to reconfigure
233
165
        :param bound_location: The location the checkout should be bound to.
234
 
        :raise AlreadyLightweightCheckout: if controldir is already a
 
166
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
235
167
            lightweight checkout
236
168
        """
237
 
        reconfiguration = klass(controldir, reference_location)
 
169
        reconfiguration = klass(bzrdir, reference_location)
238
170
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
239
171
                                      want_bound=False, want_reference=True)
240
172
        if not reconfiguration.changes_planned():
241
 
            raise AlreadyLightweightCheckout(controldir)
 
173
            raise errors.AlreadyLightweightCheckout(bzrdir)
242
174
        return reconfiguration
243
175
 
244
176
    @classmethod
245
 
    def to_use_shared(klass, controldir):
 
177
    def to_use_shared(klass, bzrdir):
246
178
        """Convert a standalone branch into a repository branch"""
247
 
        reconfiguration = klass(controldir)
 
179
        reconfiguration = klass(bzrdir)
248
180
        reconfiguration._set_use_shared(use_shared=True)
249
181
        if not reconfiguration.changes_planned():
250
 
            raise AlreadyUsingShared(controldir)
 
182
            raise errors.AlreadyUsingShared(bzrdir)
251
183
        return reconfiguration
252
184
 
253
185
    @classmethod
254
 
    def to_standalone(klass, controldir):
 
186
    def to_standalone(klass, bzrdir):
255
187
        """Convert a repository branch into a standalone branch"""
256
 
        reconfiguration = klass(controldir)
 
188
        reconfiguration = klass(bzrdir)
257
189
        reconfiguration._set_use_shared(use_shared=False)
258
190
        if not reconfiguration.changes_planned():
259
 
            raise AlreadyStandalone(controldir)
 
191
            raise errors.AlreadyStandalone(bzrdir)
260
192
        return reconfiguration
261
193
 
262
194
    @classmethod
263
 
    def set_repository_trees(klass, controldir, with_trees):
 
195
    def set_repository_trees(klass, bzrdir, with_trees):
264
196
        """Adjust a repository's working tree presence default"""
265
 
        reconfiguration = klass(controldir)
 
197
        reconfiguration = klass(bzrdir)
266
198
        if not reconfiguration.repository.is_shared():
267
 
            raise ReconfigurationNotSupported(reconfiguration.controldir)
 
199
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
268
200
        if with_trees and reconfiguration.repository.make_working_trees():
269
 
            raise AlreadyWithTrees(controldir)
270
 
        elif (not with_trees and
271
 
              not reconfiguration.repository.make_working_trees()):
272
 
            raise AlreadyWithNoTrees(controldir)
 
201
            raise errors.AlreadyWithTrees(bzrdir)
 
202
        elif (not with_trees
 
203
              and not reconfiguration.repository.make_working_trees()):
 
204
            raise errors.AlreadyWithNoTrees(bzrdir)
273
205
        else:
274
206
            reconfiguration._repository_trees = with_trees
275
207
        return reconfiguration
278
210
                      want_reference):
279
211
        """Determine which changes are needed to assume the configuration"""
280
212
        if not want_branch and not want_reference:
281
 
            raise ReconfigurationNotSupported(self.controldir)
 
213
            raise errors.ReconfigurationNotSupported(self.bzrdir)
282
214
        if want_branch and want_reference:
283
 
            raise ReconfigurationNotSupported(self.controldir)
 
215
            raise errors.ReconfigurationNotSupported(self.bzrdir)
284
216
        if self.repository is None:
285
217
            if not want_reference:
286
218
                self._create_repository = True
287
219
        else:
288
 
            if want_reference and (
289
 
                    self.repository.user_url == self.controldir.user_url):
 
220
            if want_reference and (self.repository.bzrdir.root_transport.base
 
221
                                   == self.bzrdir.root_transport.base):
290
222
                if not self.repository.is_shared():
291
223
                    self._destroy_repository = True
292
224
        if self.referenced_branch is None:
326
258
 
327
259
    def changes_planned(self):
328
260
        """Return True if changes are planned, False otherwise"""
329
 
        return (self._unbind or self._bind or self._destroy_tree or
330
 
                self._create_tree or self._destroy_reference or
331
 
                self._create_branch or self._create_repository or
332
 
                self._create_reference or self._destroy_repository)
 
261
        return (self._unbind or self._bind or self._destroy_tree
 
262
                or self._create_tree or self._destroy_reference
 
263
                or self._create_branch or self._create_repository
 
264
                or self._create_reference or self._destroy_repository)
333
265
 
334
266
    def _check(self):
335
267
        """Raise if reconfiguration would destroy local changes"""
336
268
        if self._destroy_tree and self.tree.has_changes():
337
 
            raise errors.UncommittedChanges(self.tree)
 
269
                raise errors.UncommittedChanges(self.tree)
338
270
        if self._create_reference and self.local_branch is not None:
339
271
            reference_branch = branch.Branch.open(self._select_bind_location())
340
 
            if (reference_branch.last_revision()
341
 
                    != self.local_branch.last_revision()):
342
 
                raise UnsyncedBranches(self.controldir, reference_branch)
 
272
            if (reference_branch.last_revision() !=
 
273
                self.local_branch.last_revision()):
 
274
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
343
275
 
344
276
    def _select_bind_location(self):
345
277
        """Select a location to bind or create a reference to.
369
301
                return parent
370
302
        elif self.referenced_branch is not None:
371
303
            return self.referenced_branch.base
372
 
        raise NoBindLocation(self.controldir)
 
304
        raise errors.NoBindLocation(self.bzrdir)
373
305
 
374
306
    def apply(self, force=False):
375
307
        """Apply the reconfiguration
378
310
            destroy local changes.
379
311
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
380
312
            but contains uncommitted changes.
381
 
        :raise NoBindLocation: if no bind location was specified and
 
313
        :raise errors.NoBindLocation: if no bind location was specified and
382
314
            none could be autodetected.
383
315
        """
384
316
        if not force:
395
327
            else:
396
328
                repository_format = None
397
329
            if repository_format is not None:
398
 
                repo = repository_format.initialize(self.controldir)
 
330
                repo = repository_format.initialize(self.bzrdir)
399
331
            else:
400
 
                repo = self.controldir.create_repository()
 
332
                repo = self.bzrdir.create_repository()
401
333
            if self.local_branch and not self._destroy_branch:
402
334
                repo.fetch(self.local_branch.repository,
403
335
                           self.local_branch.last_revision())
412
344
            if self._create_reference:
413
345
                reference_branch.repository.fetch(self.repository)
414
346
            elif self.local_branch is not None and not self._destroy_branch:
415
 
                up = self.local_branch.user_transport.clone('..')
416
 
                up_controldir = controldir.ControlDir.open_containing_from_transport(
417
 
                    up)[0]
418
 
                new_repo = up_controldir.find_repository()
 
347
                up = self.local_branch.bzrdir.root_transport.clone('..')
 
348
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
 
349
                new_repo = up_bzrdir.find_repository()
419
350
                new_repo.fetch(self.repository)
420
351
        last_revision_info = None
421
352
        if self._destroy_reference:
422
353
            last_revision_info = self.referenced_branch.last_revision_info()
423
 
            self.controldir.destroy_branch()
 
354
            self.bzrdir.destroy_branch()
424
355
        if self._destroy_branch:
425
356
            last_revision_info = self.local_branch.last_revision_info()
426
357
            if self._create_reference:
427
358
                self.local_branch.tags.merge_to(reference_branch.tags)
428
 
            self.controldir.destroy_branch()
 
359
            self.bzrdir.destroy_branch()
429
360
        if self._create_branch:
430
 
            local_branch = self.controldir.create_branch()
 
361
            local_branch = self.bzrdir.create_branch()
431
362
            if last_revision_info is not None:
432
363
                local_branch.set_last_revision_info(*last_revision_info)
433
364
            if self._destroy_reference:
436
367
        else:
437
368
            local_branch = self.local_branch
438
369
        if self._create_reference:
439
 
            self.controldir.set_branch_reference(reference_branch)
 
370
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
 
371
                reference_branch)
440
372
        if self._destroy_tree:
441
 
            self.controldir.destroy_workingtree()
 
373
            self.bzrdir.destroy_workingtree()
442
374
        if self._create_tree:
443
 
            self.controldir.create_workingtree()
 
375
            self.bzrdir.create_workingtree()
444
376
        if self._unbind:
445
377
            self.local_branch.unbind()
446
378
        if self._bind:
447
379
            bind_location = self._select_bind_location()
448
380
            local_branch.bind(branch.Branch.open(bind_location))
449
381
        if self._destroy_repository:
450
 
            self.controldir.destroy_repository()
 
382
            self.bzrdir.destroy_repository()
451
383
        if self._repository_trees is not None:
452
384
            repo.set_make_working_trees(self._repository_trees)