/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: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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