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

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

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