/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: Martin
  • Date: 2017-06-18 10:15:11 UTC
  • mto: This revision was merged to the branch mainline in revision 6715.
  • Revision ID: gzlist@googlemail.com-20170618101511-fri1mouxt1hc09r8
Make _simple_set tests pass on py3 and with random hash

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.
38
40
class ReconfigureStackedOn(object):
39
41
    """Reconfigures a branch to be stacked on another branch."""
40
42
 
41
 
    def apply(self, bzrdir, stacked_on_url):
42
 
        branch = bzrdir.open_branch()
 
43
    def apply(self, controldir, stacked_on_url):
 
44
        branch = controldir.open_branch()
43
45
        # it may be a path relative to the cwd or a url; the branch wants
44
46
        # a path relative to itself...
45
47
        on_url = urlutils.relative_url(branch.base,
48
50
        try:
49
51
            branch.set_stacked_on_url(on_url)
50
52
            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()))
 
53
                ui.ui_factory.note(gettext(
 
54
                    "{0} is now stacked on {1}\n").format(
 
55
                      branch.base, branch.get_stacked_on_url()))
54
56
        finally:
55
57
            branch.unlock()
56
58
 
57
59
 
58
60
class ReconfigureUnstacked(object):
59
61
 
60
 
    def apply(self, bzrdir):
61
 
        branch = bzrdir.open_branch()
 
62
    def apply(self, controldir):
 
63
        branch = controldir.open_branch()
62
64
        branch.lock_write()
63
65
        try:
64
66
            branch.set_stacked_on_url(None)
65
67
            if not trace.is_quiet():
66
 
                ui.ui_factory.note(
67
 
                    "%s is now not stacked\n"
 
68
                ui.ui_factory.note(gettext(
 
69
                    "%s is now not stacked\n")
68
70
                    % (branch.base,))
69
71
        finally:
70
72
            branch.unlock()
72
74
 
73
75
class Reconfigure(object):
74
76
 
75
 
    def __init__(self, bzrdir, new_bound_location=None):
76
 
        self.bzrdir = bzrdir
 
77
    def __init__(self, controldir, new_bound_location=None):
 
78
        self.controldir = controldir
77
79
        self.new_bound_location = new_bound_location
78
80
        self.local_repository = None
79
81
        try:
80
 
            self.repository = self.bzrdir.find_repository()
 
82
            self.repository = self.controldir.find_repository()
81
83
        except errors.NoRepositoryPresent:
82
84
            self.repository = None
83
85
            self.local_repository = None
84
86
        else:
85
 
            if (self.repository.user_url == self.bzrdir.user_url):
 
87
            if (self.repository.user_url == self.controldir.user_url):
86
88
                self.local_repository = self.repository
87
89
            else:
88
90
                self.local_repository = None
89
91
        try:
90
 
            branch = self.bzrdir.open_branch()
91
 
            if branch.user_url == bzrdir.user_url:
 
92
            branch = self.controldir.open_branch()
 
93
            if branch.user_url == controldir.user_url:
92
94
                self.local_branch = branch
93
95
                self.referenced_branch = None
94
96
            else:
98
100
            self.local_branch = None
99
101
            self.referenced_branch = None
100
102
        try:
101
 
            self.tree = bzrdir.open_workingtree()
 
103
            self.tree = controldir.open_workingtree()
102
104
        except errors.NoWorkingTree:
103
105
            self.tree = None
104
106
        self._unbind = False
114
116
        self._repository_trees = None
115
117
 
116
118
    @staticmethod
117
 
    def to_branch(bzrdir):
118
 
        """Return a Reconfiguration to convert this bzrdir into a branch
 
119
    def to_branch(controldir):
 
120
        """Return a Reconfiguration to convert this controldir into a branch
119
121
 
120
 
        :param bzrdir: The bzrdir to reconfigure
121
 
        :raise errors.AlreadyBranch: if bzrdir is already a branch
 
122
        :param controldir: The controldir to reconfigure
 
123
        :raise errors.AlreadyBranch: if controldir is already a branch
122
124
        """
123
 
        reconfiguration = Reconfigure(bzrdir)
 
125
        reconfiguration = Reconfigure(controldir)
124
126
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
125
127
                                      want_bound=False, want_reference=False)
126
128
        if not reconfiguration.changes_planned():
127
 
            raise errors.AlreadyBranch(bzrdir)
 
129
            raise errors.AlreadyBranch(controldir)
128
130
        return reconfiguration
129
131
 
130
132
    @staticmethod
131
 
    def to_tree(bzrdir):
132
 
        """Return a Reconfiguration to convert this bzrdir into a tree
 
133
    def to_tree(controldir):
 
134
        """Return a Reconfiguration to convert this controldir into a tree
133
135
 
134
 
        :param bzrdir: The bzrdir to reconfigure
135
 
        :raise errors.AlreadyTree: if bzrdir is already a tree
 
136
        :param controldir: The controldir to reconfigure
 
137
        :raise errors.AlreadyTree: if controldir is already a tree
136
138
        """
137
 
        reconfiguration = Reconfigure(bzrdir)
 
139
        reconfiguration = Reconfigure(controldir)
138
140
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
139
141
                                      want_bound=False, want_reference=False)
140
142
        if not reconfiguration.changes_planned():
141
 
            raise errors.AlreadyTree(bzrdir)
 
143
            raise errors.AlreadyTree(controldir)
142
144
        return reconfiguration
143
145
 
144
146
    @staticmethod
145
 
    def to_checkout(bzrdir, bound_location=None):
146
 
        """Return a Reconfiguration to convert this bzrdir into a checkout
 
147
    def to_checkout(controldir, bound_location=None):
 
148
        """Return a Reconfiguration to convert this controldir into a checkout
147
149
 
148
 
        :param bzrdir: The bzrdir to reconfigure
 
150
        :param controldir: The controldir to reconfigure
149
151
        :param bound_location: The location the checkout should be bound to.
150
 
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
 
152
        :raise errors.AlreadyCheckout: if controldir is already a checkout
151
153
        """
152
 
        reconfiguration = Reconfigure(bzrdir, bound_location)
 
154
        reconfiguration = Reconfigure(controldir, bound_location)
153
155
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
154
156
                                      want_bound=True, want_reference=False)
155
157
        if not reconfiguration.changes_planned():
156
 
            raise errors.AlreadyCheckout(bzrdir)
 
158
            raise errors.AlreadyCheckout(controldir)
157
159
        return reconfiguration
158
160
 
159
161
    @classmethod
160
 
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
161
 
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
 
162
    def to_lightweight_checkout(klass, controldir, reference_location=None):
 
163
        """Make a Reconfiguration to convert controldir into a lightweight checkout
162
164
 
163
 
        :param bzrdir: The bzrdir to reconfigure
 
165
        :param controldir: The controldir to reconfigure
164
166
        :param bound_location: The location the checkout should be bound to.
165
 
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
 
167
        :raise errors.AlreadyLightweightCheckout: if controldir is already a
166
168
            lightweight checkout
167
169
        """
168
 
        reconfiguration = klass(bzrdir, reference_location)
 
170
        reconfiguration = klass(controldir, reference_location)
169
171
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
170
172
                                      want_bound=False, want_reference=True)
171
173
        if not reconfiguration.changes_planned():
172
 
            raise errors.AlreadyLightweightCheckout(bzrdir)
 
174
            raise errors.AlreadyLightweightCheckout(controldir)
173
175
        return reconfiguration
174
176
 
175
177
    @classmethod
176
 
    def to_use_shared(klass, bzrdir):
 
178
    def to_use_shared(klass, controldir):
177
179
        """Convert a standalone branch into a repository branch"""
178
 
        reconfiguration = klass(bzrdir)
 
180
        reconfiguration = klass(controldir)
179
181
        reconfiguration._set_use_shared(use_shared=True)
180
182
        if not reconfiguration.changes_planned():
181
 
            raise errors.AlreadyUsingShared(bzrdir)
 
183
            raise errors.AlreadyUsingShared(controldir)
182
184
        return reconfiguration
183
185
 
184
186
    @classmethod
185
 
    def to_standalone(klass, bzrdir):
 
187
    def to_standalone(klass, controldir):
186
188
        """Convert a repository branch into a standalone branch"""
187
 
        reconfiguration = klass(bzrdir)
 
189
        reconfiguration = klass(controldir)
188
190
        reconfiguration._set_use_shared(use_shared=False)
189
191
        if not reconfiguration.changes_planned():
190
 
            raise errors.AlreadyStandalone(bzrdir)
 
192
            raise errors.AlreadyStandalone(controldir)
191
193
        return reconfiguration
192
194
 
193
195
    @classmethod
194
 
    def set_repository_trees(klass, bzrdir, with_trees):
 
196
    def set_repository_trees(klass, controldir, with_trees):
195
197
        """Adjust a repository's working tree presence default"""
196
 
        reconfiguration = klass(bzrdir)
 
198
        reconfiguration = klass(controldir)
197
199
        if not reconfiguration.repository.is_shared():
198
 
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
 
200
            raise errors.ReconfigurationNotSupported(reconfiguration.controldir)
199
201
        if with_trees and reconfiguration.repository.make_working_trees():
200
 
            raise errors.AlreadyWithTrees(bzrdir)
 
202
            raise errors.AlreadyWithTrees(controldir)
201
203
        elif (not with_trees
202
204
              and not reconfiguration.repository.make_working_trees()):
203
 
            raise errors.AlreadyWithNoTrees(bzrdir)
 
205
            raise errors.AlreadyWithNoTrees(controldir)
204
206
        else:
205
207
            reconfiguration._repository_trees = with_trees
206
208
        return reconfiguration
209
211
                      want_reference):
210
212
        """Determine which changes are needed to assume the configuration"""
211
213
        if not want_branch and not want_reference:
212
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
 
214
            raise errors.ReconfigurationNotSupported(self.controldir)
213
215
        if want_branch and want_reference:
214
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
 
216
            raise errors.ReconfigurationNotSupported(self.controldir)
215
217
        if self.repository is None:
216
218
            if not want_reference:
217
219
                self._create_repository = True
218
220
        else:
219
221
            if want_reference and (
220
 
                self.repository.user_url == self.bzrdir.user_url):
 
222
                self.repository.user_url == self.controldir.user_url):
221
223
                if not self.repository.is_shared():
222
224
                    self._destroy_repository = True
223
225
        if self.referenced_branch is None:
270
272
            reference_branch = branch.Branch.open(self._select_bind_location())
271
273
            if (reference_branch.last_revision() !=
272
274
                self.local_branch.last_revision()):
273
 
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
 
275
                raise errors.UnsyncedBranches(self.controldir, reference_branch)
274
276
 
275
277
    def _select_bind_location(self):
276
278
        """Select a location to bind or create a reference to.
300
302
                return parent
301
303
        elif self.referenced_branch is not None:
302
304
            return self.referenced_branch.base
303
 
        raise errors.NoBindLocation(self.bzrdir)
 
305
        raise errors.NoBindLocation(self.controldir)
304
306
 
305
307
    def apply(self, force=False):
306
308
        """Apply the reconfiguration
326
328
            else:
327
329
                repository_format = None
328
330
            if repository_format is not None:
329
 
                repo = repository_format.initialize(self.bzrdir)
 
331
                repo = repository_format.initialize(self.controldir)
330
332
            else:
331
 
                repo = self.bzrdir.create_repository()
 
333
                repo = self.controldir.create_repository()
332
334
            if self.local_branch and not self._destroy_branch:
333
335
                repo.fetch(self.local_branch.repository,
334
336
                           self.local_branch.last_revision())
344
346
                reference_branch.repository.fetch(self.repository)
345
347
            elif self.local_branch is not None and not self._destroy_branch:
346
348
                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()
 
349
                up_controldir = controldir.ControlDir.open_containing_from_transport(
 
350
                    up)[0]
 
351
                new_repo = up_controldir.find_repository()
349
352
                new_repo.fetch(self.repository)
350
353
        last_revision_info = None
351
354
        if self._destroy_reference:
352
355
            last_revision_info = self.referenced_branch.last_revision_info()
353
 
            self.bzrdir.destroy_branch()
 
356
            self.controldir.destroy_branch()
354
357
        if self._destroy_branch:
355
358
            last_revision_info = self.local_branch.last_revision_info()
356
359
            if self._create_reference:
357
360
                self.local_branch.tags.merge_to(reference_branch.tags)
358
 
            self.bzrdir.destroy_branch()
 
361
            self.controldir.destroy_branch()
359
362
        if self._create_branch:
360
 
            local_branch = self.bzrdir.create_branch()
 
363
            local_branch = self.controldir.create_branch()
361
364
            if last_revision_info is not None:
362
365
                local_branch.set_last_revision_info(*last_revision_info)
363
366
            if self._destroy_reference:
366
369
        else:
367
370
            local_branch = self.local_branch
368
371
        if self._create_reference:
369
 
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
370
 
                target_branch=reference_branch)
 
372
            self.controldir.set_branch_reference(reference_branch)
371
373
        if self._destroy_tree:
372
 
            self.bzrdir.destroy_workingtree()
 
374
            self.controldir.destroy_workingtree()
373
375
        if self._create_tree:
374
 
            self.bzrdir.create_workingtree()
 
376
            self.controldir.create_workingtree()
375
377
        if self._unbind:
376
378
            self.local_branch.unbind()
377
379
        if self._bind:
378
380
            bind_location = self._select_bind_location()
379
381
            local_branch.bind(branch.Branch.open(bind_location))
380
382
        if self._destroy_repository:
381
 
            self.bzrdir.destroy_repository()
 
383
            self.controldir.destroy_repository()
382
384
        if self._repository_trees is not None:
383
385
            repo.set_make_working_trees(self._repository_trees)