/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: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,
123
48
        try:
124
49
            branch.set_stacked_on_url(on_url)
125
50
            if not trace.is_quiet():
126
 
                ui.ui_factory.note(gettext(
127
 
                    "{0} is now stacked on {1}\n").format(
128
 
                      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()))
129
54
        finally:
130
55
            branch.unlock()
131
56
 
132
57
 
133
58
class ReconfigureUnstacked(object):
134
59
 
135
 
    def apply(self, controldir):
136
 
        branch = controldir.open_branch()
 
60
    def apply(self, bzrdir):
 
61
        branch = bzrdir.open_branch()
137
62
        branch.lock_write()
138
63
        try:
139
64
            branch.set_stacked_on_url(None)
140
65
            if not trace.is_quiet():
141
 
                ui.ui_factory.note(gettext(
142
 
                    "%s is now not stacked\n")
 
66
                ui.ui_factory.note(
 
67
                    "%s is now not stacked\n"
143
68
                    % (branch.base,))
144
69
        finally:
145
70
            branch.unlock()
147
72
 
148
73
class Reconfigure(object):
149
74
 
150
 
    def __init__(self, controldir, new_bound_location=None):
151
 
        self.controldir = controldir
 
75
    def __init__(self, bzrdir, new_bound_location=None):
 
76
        self.bzrdir = bzrdir
152
77
        self.new_bound_location = new_bound_location
153
78
        self.local_repository = None
154
79
        try:
155
 
            self.repository = self.controldir.find_repository()
 
80
            self.repository = self.bzrdir.find_repository()
156
81
        except errors.NoRepositoryPresent:
157
82
            self.repository = None
158
83
            self.local_repository = None
159
84
        else:
160
 
            if (self.repository.user_url == self.controldir.user_url):
 
85
            if (self.repository.user_url == self.bzrdir.user_url):
161
86
                self.local_repository = self.repository
162
87
            else:
163
88
                self.local_repository = None
164
89
        try:
165
 
            branch = self.controldir.open_branch()
166
 
            if branch.user_url == controldir.user_url:
 
90
            branch = self.bzrdir.open_branch()
 
91
            if branch.user_url == bzrdir.user_url:
167
92
                self.local_branch = branch
168
93
                self.referenced_branch = None
169
94
            else:
173
98
            self.local_branch = None
174
99
            self.referenced_branch = None
175
100
        try:
176
 
            self.tree = controldir.open_workingtree()
 
101
            self.tree = bzrdir.open_workingtree()
177
102
        except errors.NoWorkingTree:
178
103
            self.tree = None
179
104
        self._unbind = False
189
114
        self._repository_trees = None
190
115
 
191
116
    @staticmethod
192
 
    def to_branch(controldir):
193
 
        """Return a Reconfiguration to convert this controldir into a branch
 
117
    def to_branch(bzrdir):
 
118
        """Return a Reconfiguration to convert this bzrdir into a branch
194
119
 
195
 
        :param controldir: The controldir to reconfigure
196
 
        :raise AlreadyBranch: if controldir is already a branch
 
120
        :param bzrdir: The bzrdir to reconfigure
 
121
        :raise errors.AlreadyBranch: if bzrdir is already a branch
197
122
        """
198
 
        reconfiguration = Reconfigure(controldir)
 
123
        reconfiguration = Reconfigure(bzrdir)
199
124
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
200
125
                                      want_bound=False, want_reference=False)
201
126
        if not reconfiguration.changes_planned():
202
 
            raise AlreadyBranch(controldir)
 
127
            raise errors.AlreadyBranch(bzrdir)
203
128
        return reconfiguration
204
129
 
205
130
    @staticmethod
206
 
    def to_tree(controldir):
207
 
        """Return a Reconfiguration to convert this controldir into a tree
 
131
    def to_tree(bzrdir):
 
132
        """Return a Reconfiguration to convert this bzrdir into a tree
208
133
 
209
 
        :param controldir: The controldir to reconfigure
210
 
        :raise AlreadyTree: if controldir is already a tree
 
134
        :param bzrdir: The bzrdir to reconfigure
 
135
        :raise errors.AlreadyTree: if bzrdir is already a tree
211
136
        """
212
 
        reconfiguration = Reconfigure(controldir)
 
137
        reconfiguration = Reconfigure(bzrdir)
213
138
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
214
139
                                      want_bound=False, want_reference=False)
215
140
        if not reconfiguration.changes_planned():
216
 
            raise AlreadyTree(controldir)
 
141
            raise errors.AlreadyTree(bzrdir)
217
142
        return reconfiguration
218
143
 
219
144
    @staticmethod
220
 
    def to_checkout(controldir, bound_location=None):
221
 
        """Return a Reconfiguration to convert this controldir into a checkout
 
145
    def to_checkout(bzrdir, bound_location=None):
 
146
        """Return a Reconfiguration to convert this bzrdir into a checkout
222
147
 
223
 
        :param controldir: The controldir to reconfigure
 
148
        :param bzrdir: The bzrdir to reconfigure
224
149
        :param bound_location: The location the checkout should be bound to.
225
 
        :raise AlreadyCheckout: if controldir is already a checkout
 
150
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
226
151
        """
227
 
        reconfiguration = Reconfigure(controldir, bound_location)
 
152
        reconfiguration = Reconfigure(bzrdir, bound_location)
228
153
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
229
154
                                      want_bound=True, want_reference=False)
230
155
        if not reconfiguration.changes_planned():
231
 
            raise AlreadyCheckout(controldir)
 
156
            raise errors.AlreadyCheckout(bzrdir)
232
157
        return reconfiguration
233
158
 
234
159
    @classmethod
235
 
    def to_lightweight_checkout(klass, controldir, reference_location=None):
236
 
        """Make a Reconfiguration to convert controldir into a lightweight checkout
 
160
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
 
161
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
237
162
 
238
 
        :param controldir: The controldir to reconfigure
 
163
        :param bzrdir: The bzrdir to reconfigure
239
164
        :param bound_location: The location the checkout should be bound to.
240
 
        :raise AlreadyLightweightCheckout: if controldir is already a
 
165
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
241
166
            lightweight checkout
242
167
        """
243
 
        reconfiguration = klass(controldir, reference_location)
 
168
        reconfiguration = klass(bzrdir, reference_location)
244
169
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
245
170
                                      want_bound=False, want_reference=True)
246
171
        if not reconfiguration.changes_planned():
247
 
            raise AlreadyLightweightCheckout(controldir)
 
172
            raise errors.AlreadyLightweightCheckout(bzrdir)
248
173
        return reconfiguration
249
174
 
250
175
    @classmethod
251
 
    def to_use_shared(klass, controldir):
 
176
    def to_use_shared(klass, bzrdir):
252
177
        """Convert a standalone branch into a repository branch"""
253
 
        reconfiguration = klass(controldir)
 
178
        reconfiguration = klass(bzrdir)
254
179
        reconfiguration._set_use_shared(use_shared=True)
255
180
        if not reconfiguration.changes_planned():
256
 
            raise AlreadyUsingShared(controldir)
 
181
            raise errors.AlreadyUsingShared(bzrdir)
257
182
        return reconfiguration
258
183
 
259
184
    @classmethod
260
 
    def to_standalone(klass, controldir):
 
185
    def to_standalone(klass, bzrdir):
261
186
        """Convert a repository branch into a standalone branch"""
262
 
        reconfiguration = klass(controldir)
 
187
        reconfiguration = klass(bzrdir)
263
188
        reconfiguration._set_use_shared(use_shared=False)
264
189
        if not reconfiguration.changes_planned():
265
 
            raise AlreadyStandalone(controldir)
 
190
            raise errors.AlreadyStandalone(bzrdir)
266
191
        return reconfiguration
267
192
 
268
193
    @classmethod
269
 
    def set_repository_trees(klass, controldir, with_trees):
 
194
    def set_repository_trees(klass, bzrdir, with_trees):
270
195
        """Adjust a repository's working tree presence default"""
271
 
        reconfiguration = klass(controldir)
 
196
        reconfiguration = klass(bzrdir)
272
197
        if not reconfiguration.repository.is_shared():
273
 
            raise ReconfigurationNotSupported(reconfiguration.controldir)
 
198
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
274
199
        if with_trees and reconfiguration.repository.make_working_trees():
275
 
            raise AlreadyWithTrees(controldir)
 
200
            raise errors.AlreadyWithTrees(bzrdir)
276
201
        elif (not with_trees
277
202
              and not reconfiguration.repository.make_working_trees()):
278
 
            raise AlreadyWithNoTrees(controldir)
 
203
            raise errors.AlreadyWithNoTrees(bzrdir)
279
204
        else:
280
205
            reconfiguration._repository_trees = with_trees
281
206
        return reconfiguration
284
209
                      want_reference):
285
210
        """Determine which changes are needed to assume the configuration"""
286
211
        if not want_branch and not want_reference:
287
 
            raise ReconfigurationNotSupported(self.controldir)
 
212
            raise errors.ReconfigurationNotSupported(self.bzrdir)
288
213
        if want_branch and want_reference:
289
 
            raise ReconfigurationNotSupported(self.controldir)
 
214
            raise errors.ReconfigurationNotSupported(self.bzrdir)
290
215
        if self.repository is None:
291
216
            if not want_reference:
292
217
                self._create_repository = True
293
218
        else:
294
219
            if want_reference and (
295
 
                self.repository.user_url == self.controldir.user_url):
 
220
                self.repository.user_url == self.bzrdir.user_url):
296
221
                if not self.repository.is_shared():
297
222
                    self._destroy_repository = True
298
223
        if self.referenced_branch is None:
345
270
            reference_branch = branch.Branch.open(self._select_bind_location())
346
271
            if (reference_branch.last_revision() !=
347
272
                self.local_branch.last_revision()):
348
 
                raise UnsyncedBranches(self.controldir, reference_branch)
 
273
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
349
274
 
350
275
    def _select_bind_location(self):
351
276
        """Select a location to bind or create a reference to.
375
300
                return parent
376
301
        elif self.referenced_branch is not None:
377
302
            return self.referenced_branch.base
378
 
        raise NoBindLocation(self.controldir)
 
303
        raise errors.NoBindLocation(self.bzrdir)
379
304
 
380
305
    def apply(self, force=False):
381
306
        """Apply the reconfiguration
384
309
            destroy local changes.
385
310
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
386
311
            but contains uncommitted changes.
387
 
        :raise NoBindLocation: if no bind location was specified and
 
312
        :raise errors.NoBindLocation: if no bind location was specified and
388
313
            none could be autodetected.
389
314
        """
390
315
        if not force:
401
326
            else:
402
327
                repository_format = None
403
328
            if repository_format is not None:
404
 
                repo = repository_format.initialize(self.controldir)
 
329
                repo = repository_format.initialize(self.bzrdir)
405
330
            else:
406
 
                repo = self.controldir.create_repository()
 
331
                repo = self.bzrdir.create_repository()
407
332
            if self.local_branch and not self._destroy_branch:
408
333
                repo.fetch(self.local_branch.repository,
409
334
                           self.local_branch.last_revision())
419
344
                reference_branch.repository.fetch(self.repository)
420
345
            elif self.local_branch is not None and not self._destroy_branch:
421
346
                up = self.local_branch.user_transport.clone('..')
422
 
                up_controldir = controldir.ControlDir.open_containing_from_transport(
423
 
                    up)[0]
424
 
                new_repo = up_controldir.find_repository()
 
347
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
 
348
                new_repo = up_bzrdir.find_repository()
425
349
                new_repo.fetch(self.repository)
426
350
        last_revision_info = None
427
351
        if self._destroy_reference:
428
352
            last_revision_info = self.referenced_branch.last_revision_info()
429
 
            self.controldir.destroy_branch()
 
353
            self.bzrdir.destroy_branch()
430
354
        if self._destroy_branch:
431
355
            last_revision_info = self.local_branch.last_revision_info()
432
356
            if self._create_reference:
433
357
                self.local_branch.tags.merge_to(reference_branch.tags)
434
 
            self.controldir.destroy_branch()
 
358
            self.bzrdir.destroy_branch()
435
359
        if self._create_branch:
436
 
            local_branch = self.controldir.create_branch()
 
360
            local_branch = self.bzrdir.create_branch()
437
361
            if last_revision_info is not None:
438
362
                local_branch.set_last_revision_info(*last_revision_info)
439
363
            if self._destroy_reference:
442
366
        else:
443
367
            local_branch = self.local_branch
444
368
        if self._create_reference:
445
 
            self.controldir.set_branch_reference(reference_branch)
 
369
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
 
370
                target_branch=reference_branch)
446
371
        if self._destroy_tree:
447
 
            self.controldir.destroy_workingtree()
 
372
            self.bzrdir.destroy_workingtree()
448
373
        if self._create_tree:
449
 
            self.controldir.create_workingtree()
 
374
            self.bzrdir.create_workingtree()
450
375
        if self._unbind:
451
376
            self.local_branch.unbind()
452
377
        if self._bind:
453
378
            bind_location = self._select_bind_location()
454
379
            local_branch.bind(branch.Branch.open(bind_location))
455
380
        if self._destroy_repository:
456
 
            self.controldir.destroy_repository()
 
381
            self.bzrdir.destroy_repository()
457
382
        if self._repository_trees is not None:
458
383
            repo.set_make_working_trees(self._repository_trees)