~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Reconfigure a controldir into a new tree/branch/repository layout.
18
 
 
19
 
Various types of reconfiguration operation are available either by
20
 
constructing a class or using a factory method on Reconfigure.
21
 
"""
22
 
 
23
 
from __future__ import absolute_import
24
 
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
25
18
 
26
19
from bzrlib import (
27
20
    branch,
28
 
    controldir,
29
21
    errors,
30
 
    trace,
31
 
    ui,
32
 
    urlutils,
33
22
    )
34
 
from bzrlib.i18n import gettext
35
 
 
36
 
# TODO: common base class for all reconfigure operations, making no
37
 
# assumptions about what kind of change will be done.
38
 
 
39
 
 
40
 
class ReconfigureStackedOn(object):
41
 
    """Reconfigures a branch to be stacked on another branch."""
42
 
 
43
 
    def apply(self, bzrdir, stacked_on_url):
44
 
        branch = bzrdir.open_branch()
45
 
        # it may be a path relative to the cwd or a url; the branch wants
46
 
        # a path relative to itself...
47
 
        on_url = urlutils.relative_url(branch.base,
48
 
            urlutils.normalize_url(stacked_on_url))
49
 
        branch.lock_write()
50
 
        try:
51
 
            branch.set_stacked_on_url(on_url)
52
 
            if not trace.is_quiet():
53
 
                ui.ui_factory.note(gettext(
54
 
                    "{0} is now stacked on {1}\n").format(
55
 
                      branch.base, branch.get_stacked_on_url()))
56
 
        finally:
57
 
            branch.unlock()
58
 
 
59
 
 
60
 
class ReconfigureUnstacked(object):
61
 
 
62
 
    def apply(self, bzrdir):
63
 
        branch = bzrdir.open_branch()
64
 
        branch.lock_write()
65
 
        try:
66
 
            branch.set_stacked_on_url(None)
67
 
            if not trace.is_quiet():
68
 
                ui.ui_factory.note(gettext(
69
 
                    "%s is now not stacked\n")
70
 
                    % (branch.base,))
71
 
        finally:
72
 
            branch.unlock()
73
 
 
74
23
 
75
24
class Reconfigure(object):
76
25
 
77
26
    def __init__(self, bzrdir, new_bound_location=None):
78
27
        self.bzrdir = bzrdir
79
28
        self.new_bound_location = new_bound_location
80
 
        self.local_repository = None
81
29
        try:
82
30
            self.repository = self.bzrdir.find_repository()
83
31
        except errors.NoRepositoryPresent:
84
32
            self.repository = None
85
 
            self.local_repository = None
86
 
        else:
87
 
            if (self.repository.user_url == self.bzrdir.user_url):
88
 
                self.local_repository = self.repository
89
 
            else:
90
 
                self.local_repository = None
91
33
        try:
92
34
            branch = self.bzrdir.open_branch()
93
 
            if branch.user_url == bzrdir.user_url:
 
35
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
94
36
                self.local_branch = branch
95
37
                self.referenced_branch = None
96
38
            else:
106
48
        self._unbind = False
107
49
        self._bind = False
108
50
        self._destroy_reference = False
109
 
        self._create_reference = False
110
 
        self._destroy_branch = False
111
51
        self._create_branch = False
112
52
        self._destroy_tree = False
113
53
        self._create_tree = False
114
54
        self._create_repository = False
115
 
        self._destroy_repository = False
116
 
        self._repository_trees = None
117
55
 
118
56
    @staticmethod
119
57
    def to_branch(bzrdir):
121
59
 
122
60
        :param bzrdir: The bzrdir to reconfigure
123
61
        :raise errors.AlreadyBranch: if bzrdir is already a branch
 
62
        :raise errors.ReconfigurationNotSupported: if bzrdir does not contain
 
63
            a branch or branch reference
124
64
        """
125
65
        reconfiguration = Reconfigure(bzrdir)
126
66
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
127
 
                                      want_bound=False, want_reference=False)
 
67
                                      want_bound=False)
128
68
        if not reconfiguration.changes_planned():
129
69
            raise errors.AlreadyBranch(bzrdir)
130
70
        return reconfiguration
135
75
 
136
76
        :param bzrdir: The bzrdir to reconfigure
137
77
        :raise errors.AlreadyTree: if bzrdir is already a tree
 
78
        :raise errors.ReconfigurationNotSupported: if bzrdir does not contain
 
79
            a branch or branch reference
138
80
        """
139
81
        reconfiguration = Reconfigure(bzrdir)
140
82
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
141
 
                                      want_bound=False, want_reference=False)
 
83
                                      want_bound=False)
142
84
        if not reconfiguration.changes_planned():
143
85
            raise errors.AlreadyTree(bzrdir)
144
86
        return reconfiguration
150
92
        :param bzrdir: The bzrdir to reconfigure
151
93
        :param bound_location: The location the checkout should be bound to.
152
94
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
 
95
        :raise errors.ReconfigurationNotSupported: if bzrdir does not contain
 
96
            a branch or branch reference
153
97
        """
154
98
        reconfiguration = Reconfigure(bzrdir, bound_location)
155
99
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
156
 
                                      want_bound=True, want_reference=False)
 
100
                                      want_bound=True)
157
101
        if not reconfiguration.changes_planned():
158
102
            raise errors.AlreadyCheckout(bzrdir)
159
103
        return reconfiguration
160
104
 
161
 
    @classmethod
162
 
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
163
 
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
164
 
 
165
 
        :param bzrdir: The bzrdir to reconfigure
166
 
        :param bound_location: The location the checkout should be bound to.
167
 
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
168
 
            lightweight checkout
169
 
        """
170
 
        reconfiguration = klass(bzrdir, reference_location)
171
 
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
172
 
                                      want_bound=False, want_reference=True)
173
 
        if not reconfiguration.changes_planned():
174
 
            raise errors.AlreadyLightweightCheckout(bzrdir)
175
 
        return reconfiguration
176
 
 
177
 
    @classmethod
178
 
    def to_use_shared(klass, bzrdir):
179
 
        """Convert a standalone branch into a repository branch"""
180
 
        reconfiguration = klass(bzrdir)
181
 
        reconfiguration._set_use_shared(use_shared=True)
182
 
        if not reconfiguration.changes_planned():
183
 
            raise errors.AlreadyUsingShared(bzrdir)
184
 
        return reconfiguration
185
 
 
186
 
    @classmethod
187
 
    def to_standalone(klass, bzrdir):
188
 
        """Convert a repository branch into a standalone branch"""
189
 
        reconfiguration = klass(bzrdir)
190
 
        reconfiguration._set_use_shared(use_shared=False)
191
 
        if not reconfiguration.changes_planned():
192
 
            raise errors.AlreadyStandalone(bzrdir)
193
 
        return reconfiguration
194
 
 
195
 
    @classmethod
196
 
    def set_repository_trees(klass, bzrdir, with_trees):
197
 
        """Adjust a repository's working tree presence default"""
198
 
        reconfiguration = klass(bzrdir)
199
 
        if not reconfiguration.repository.is_shared():
200
 
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
201
 
        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)
206
 
        else:
207
 
            reconfiguration._repository_trees = with_trees
208
 
        return reconfiguration
209
 
 
210
 
    def _plan_changes(self, want_tree, want_branch, want_bound,
211
 
                      want_reference):
 
105
    def _plan_changes(self, want_tree, want_branch, want_bound):
212
106
        """Determine which changes are needed to assume the configuration"""
213
 
        if not want_branch and not want_reference:
214
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
215
 
        if want_branch and want_reference:
216
 
            raise errors.ReconfigurationNotSupported(self.bzrdir)
217
107
        if self.repository is None:
218
 
            if not want_reference:
219
 
                self._create_repository = True
220
 
        else:
221
 
            if want_reference and (
222
 
                self.repository.user_url == self.bzrdir.user_url):
223
 
                if not self.repository.is_shared():
224
 
                    self._destroy_repository = True
225
 
        if self.referenced_branch is None:
226
 
            if want_reference:
227
 
                self._create_reference = True
228
 
                if self.local_branch is not None:
229
 
                    self._destroy_branch = True
230
 
        else:
231
 
            if not want_reference:
232
 
                self._destroy_reference = True
 
108
            self._create_repository = True
233
109
        if self.local_branch is None:
234
110
            if want_branch is True:
235
 
                self._create_branch = True
236
 
                if want_bound:
237
 
                    self._bind = True
 
111
                if self.referenced_branch is not None:
 
112
                    self._destroy_reference = True
 
113
                    self._create_branch = True
 
114
                    if want_bound:
 
115
                        self._bind = True
 
116
                else:
 
117
                    raise errors.ReconfigurationNotSupported(self.bzrdir)
238
118
        else:
239
119
            if want_bound:
240
120
                if self.local_branch.get_bound_location() is None:
247
127
        if want_tree and self.tree is None:
248
128
            self._create_tree = True
249
129
 
250
 
    def _set_use_shared(self, use_shared=None):
251
 
        if use_shared is None:
252
 
            return
253
 
        if use_shared:
254
 
            if self.local_repository is not None:
255
 
                self._destroy_repository = True
256
 
        else:
257
 
            if self.local_repository is None:
258
 
                self._create_repository = True
259
 
 
260
130
    def changes_planned(self):
261
131
        """Return True if changes are planned, False otherwise"""
262
132
        return (self._unbind or self._bind or self._destroy_tree
263
133
                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)
 
134
                or self._create_branch or self._create_repository)
266
135
 
267
136
    def _check(self):
268
137
        """Raise if reconfiguration would destroy local changes"""
269
 
        if self._destroy_tree and self.tree.has_changes():
 
138
        if self._destroy_tree:
 
139
            changes = self.tree.changes_from(self.tree.basis_tree())
 
140
            if changes.has_changed():
270
141
                raise errors.UncommittedChanges(self.tree)
271
 
        if self._create_reference and self.local_branch is not None:
272
 
            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)
276
142
 
277
143
    def _select_bind_location(self):
278
 
        """Select a location to bind or create a reference to.
 
144
        """Select a location to bind to.
279
145
 
280
146
        Preference is:
281
147
        1. user specified location
282
148
        2. branch reference location (it's a kind of bind location)
283
 
        3. current bind location
284
 
        4. previous bind location (it was a good choice once)
285
 
        5. push location (it's writeable, so committable)
286
 
        6. parent location (it's pullable, so update-from-able)
 
149
        3. previous bind location (it was a good choice once)
 
150
        4. push location (it's writeable, so committable)
 
151
        5. parent location (it's pullable, so update-from-able)
287
152
        """
288
153
        if self.new_bound_location is not None:
289
154
            return self.new_bound_location
290
155
        if self.local_branch is not None:
291
 
            bound = self.local_branch.get_bound_location()
292
 
            if bound is not None:
293
 
                return bound
294
156
            old_bound = self.local_branch.get_old_bound_location()
295
157
            if old_bound is not None:
296
158
                return old_bound
317
179
        if not force:
318
180
            self._check()
319
181
        if self._create_repository:
320
 
            if self.local_branch and not self._destroy_branch:
321
 
                old_repo = self.local_branch.repository
322
 
            elif self._create_branch and self.referenced_branch is not None:
323
 
                old_repo = self.referenced_branch.repository
324
 
            else:
325
 
                old_repo = None
326
 
            if old_repo is not None:
327
 
                repository_format = old_repo._format
328
 
            else:
329
 
                repository_format = None
330
 
            if repository_format is not None:
331
 
                repo = repository_format.initialize(self.bzrdir)
332
 
            else:
333
 
                repo = self.bzrdir.create_repository()
334
 
            if self.local_branch and not self._destroy_branch:
335
 
                repo.fetch(self.local_branch.repository,
336
 
                           self.local_branch.last_revision())
 
182
            repo = self.bzrdir.create_repository()
337
183
        else:
338
184
            repo = self.repository
339
 
        if self._create_branch and self.referenced_branch is not None:
 
185
        if self._create_branch:
340
186
            repo.fetch(self.referenced_branch.repository,
341
187
                       self.referenced_branch.last_revision())
342
 
        if self._create_reference:
343
 
            reference_branch = branch.Branch.open(self._select_bind_location())
344
 
        if self._destroy_repository:
345
 
            if self._create_reference:
346
 
                reference_branch.repository.fetch(self.repository)
347
 
            elif self.local_branch is not None and not self._destroy_branch:
348
 
                up = self.local_branch.user_transport.clone('..')
349
 
                up_bzrdir = controldir.ControlDir.open_containing_from_transport(
350
 
                    up)[0]
351
 
                new_repo = up_bzrdir.find_repository()
352
 
                new_repo.fetch(self.repository)
353
 
        last_revision_info = None
354
188
        if self._destroy_reference:
355
 
            last_revision_info = self.referenced_branch.last_revision_info()
356
 
            self.bzrdir.destroy_branch()
357
 
        if self._destroy_branch:
358
 
            last_revision_info = self.local_branch.last_revision_info()
359
 
            if self._create_reference:
360
 
                self.local_branch.tags.merge_to(reference_branch.tags)
 
189
            reference_info = self.referenced_branch.last_revision_info()
361
190
            self.bzrdir.destroy_branch()
362
191
        if self._create_branch:
363
192
            local_branch = self.bzrdir.create_branch()
364
 
            if last_revision_info is not None:
365
 
                local_branch.set_last_revision_info(*last_revision_info)
366
 
            if self._destroy_reference:
367
 
                self.referenced_branch.tags.merge_to(local_branch.tags)
368
 
                self.referenced_branch.update_references(local_branch)
 
193
            local_branch.set_last_revision_info(*reference_info)
369
194
        else:
370
195
            local_branch = self.local_branch
371
 
        if self._create_reference:
372
 
            self.bzrdir.set_branch_reference(reference_branch)
373
196
        if self._destroy_tree:
374
197
            self.bzrdir.destroy_workingtree()
375
198
        if self._create_tree:
379
202
        if self._bind:
380
203
            bind_location = self._select_bind_location()
381
204
            local_branch.bind(branch.Branch.open(bind_location))
382
 
        if self._destroy_repository:
383
 
            self.bzrdir.destroy_repository()
384
 
        if self._repository_trees is not None:
385
 
            repo.set_make_working_trees(self._repository_trees)