/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: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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