/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: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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
 
23
25
 
24
26
from . import (
25
27
    branch,
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
 
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
62
    def apply(self, controldir):
131
63
        branch = controldir.open_branch()
132
 
        with branch.lock_write():
 
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):
185
120
        """Return a Reconfiguration to convert this controldir into a branch
186
121
 
187
122
        :param controldir: The controldir to reconfigure
188
 
        :raise AlreadyBranch: if controldir is already a branch
 
123
        :raise errors.AlreadyBranch: if controldir is already a branch
189
124
        """
190
125
        reconfiguration = Reconfigure(controldir)
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(controldir)
195
130
        return reconfiguration
196
131
 
197
132
    @staticmethod
199
134
        """Return a Reconfiguration to convert this controldir into a tree
200
135
 
201
136
        :param controldir: The controldir to reconfigure
202
 
        :raise AlreadyTree: if controldir is already a tree
 
137
        :raise errors.AlreadyTree: if controldir is already a tree
203
138
        """
204
139
        reconfiguration = Reconfigure(controldir)
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(controldir)
209
144
        return reconfiguration
210
145
 
211
146
    @staticmethod
214
149
 
215
150
        :param controldir: The controldir 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 controldir is already a checkout
218
153
        """
219
154
        reconfiguration = Reconfigure(controldir, 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(controldir)
224
159
        return reconfiguration
225
160
 
226
161
    @classmethod
229
164
 
230
165
        :param controldir: The controldir 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 controldir is already a
233
168
            lightweight checkout
234
169
        """
235
170
        reconfiguration = klass(controldir, 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(controldir)
240
175
        return reconfiguration
241
176
 
242
177
    @classmethod
245
180
        reconfiguration = klass(controldir)
246
181
        reconfiguration._set_use_shared(use_shared=True)
247
182
        if not reconfiguration.changes_planned():
248
 
            raise AlreadyUsingShared(controldir)
 
183
            raise errors.AlreadyUsingShared(controldir)
249
184
        return reconfiguration
250
185
 
251
186
    @classmethod
254
189
        reconfiguration = klass(controldir)
255
190
        reconfiguration._set_use_shared(use_shared=False)
256
191
        if not reconfiguration.changes_planned():
257
 
            raise AlreadyStandalone(controldir)
 
192
            raise errors.AlreadyStandalone(controldir)
258
193
        return reconfiguration
259
194
 
260
195
    @classmethod
262
197
        """Adjust a repository's working tree presence default"""
263
198
        reconfiguration = klass(controldir)
264
199
        if not reconfiguration.repository.is_shared():
265
 
            raise ReconfigurationNotSupported(reconfiguration.controldir)
 
200
            raise errors.ReconfigurationNotSupported(reconfiguration.controldir)
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(controldir)
 
203
        elif (not with_trees
 
204
              and not reconfiguration.repository.make_working_trees()):
 
205
            raise errors.AlreadyWithNoTrees(controldir)
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.controldir)
280
215
        if want_branch and want_reference:
281
 
            raise ReconfigurationNotSupported(self.controldir)
 
216
            raise errors.ReconfigurationNotSupported(self.controldir)
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.controldir.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.controldir, 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.controldir)
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: