~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconfigure.py

  • Committer: Ian Clatworthy
  • Date: 2007-12-07 04:21:59 UTC
  • mto: This revision was merged to the branch mainline in revision 3092.
  • Revision ID: ian.clatworthy@internode.on.net-20071207042159-n9rmhanqid1l7olh
Better PDF for Qiock Start Card (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009 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 bzrdir 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
 
 
 
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"""
23
18
 
24
19
from bzrlib import (
25
20
    branch,
26
 
    bzrdir,
27
21
    errors,
28
 
    trace,
29
 
    ui,
30
 
    urlutils,
31
22
    )
32
23
 
33
 
 
34
 
# TODO: common base class for all reconfigure operations, making no
35
 
# assumptions about what kind of change will be done.
36
 
 
37
 
 
38
 
class ReconfigureStackedOn(object):
39
 
    """Reconfigures a branch to be stacked on another branch."""
40
 
 
41
 
    def apply(self, bzrdir, stacked_on_url):
42
 
        branch = bzrdir.open_branch()
43
 
        # it may be a path relative to the cwd or a url; the branch wants
44
 
        # a path relative to itself...
45
 
        on_url = urlutils.relative_url(branch.base,
46
 
            urlutils.normalize_url(stacked_on_url))
47
 
        branch.lock_write()
48
 
        try:
49
 
            branch.set_stacked_on_url(on_url)
50
 
            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()
56
 
 
57
 
 
58
 
class ReconfigureUnstacked(object):
59
 
 
60
 
    def apply(self, bzrdir):
61
 
        branch = bzrdir.open_branch()
62
 
        branch.lock_write()
63
 
        try:
64
 
            branch.set_stacked_on_url(None)
65
 
            if not trace.is_quiet():
66
 
                ui.ui_factory.note(
67
 
                    "%s is now not stacked\n"
68
 
                    % (branch.base,))
69
 
        finally:
70
 
            branch.unlock()
71
 
 
72
 
 
73
24
class Reconfigure(object):
74
25
 
75
26
    def __init__(self, bzrdir, new_bound_location=None):
76
27
        self.bzrdir = bzrdir
77
28
        self.new_bound_location = new_bound_location
78
 
        self.local_repository = None
79
29
        try:
80
30
            self.repository = self.bzrdir.find_repository()
81
31
        except errors.NoRepositoryPresent:
82
32
            self.repository = None
83
 
            self.local_repository = None
84
 
        else:
85
 
            if (self.repository.bzrdir.root_transport.base ==
86
 
                self.bzrdir.root_transport.base):
87
 
                self.local_repository = self.repository
88
 
            else:
89
 
                self.local_repository = None
90
33
        try:
91
34
            branch = self.bzrdir.open_branch()
92
35
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
112
55
        self._create_tree = False
113
56
        self._create_repository = False
114
57
        self._destroy_repository = False
115
 
        self._repository_trees = None
116
58
 
117
59
    @staticmethod
118
60
    def to_branch(bzrdir):
120
62
 
121
63
        :param bzrdir: The bzrdir to reconfigure
122
64
        :raise errors.AlreadyBranch: if bzrdir is already a branch
 
65
        :raise errors.ReconfigurationNotSupported: if bzrdir does not contain
 
66
            a branch or branch reference
123
67
        """
124
68
        reconfiguration = Reconfigure(bzrdir)
125
69
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
134
78
 
135
79
        :param bzrdir: The bzrdir to reconfigure
136
80
        :raise errors.AlreadyTree: if bzrdir is already a tree
 
81
        :raise errors.ReconfigurationNotSupported: if bzrdir does not contain
 
82
            a branch or branch reference
137
83
        """
138
84
        reconfiguration = Reconfigure(bzrdir)
139
85
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
149
95
        :param bzrdir: The bzrdir to reconfigure
150
96
        :param bound_location: The location the checkout should be bound to.
151
97
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
 
98
        :raise errors.ReconfigurationNotSupported: if bzrdir does not contain
 
99
            a branch or branch reference
152
100
        """
153
101
        reconfiguration = Reconfigure(bzrdir, bound_location)
154
102
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
165
113
        :param bound_location: The location the checkout should be bound to.
166
114
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
167
115
            lightweight checkout
 
116
        :raise errors.ReconfigurationNotSupported: if bzrdir does not contain
 
117
            a branch or branch reference
168
118
        """
169
119
        reconfiguration = klass(bzrdir, reference_location)
170
120
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
173
123
            raise errors.AlreadyLightweightCheckout(bzrdir)
174
124
        return reconfiguration
175
125
 
176
 
    @classmethod
177
 
    def to_use_shared(klass, bzrdir):
178
 
        """Convert a standalone branch into a repository branch"""
179
 
        reconfiguration = klass(bzrdir)
180
 
        reconfiguration._set_use_shared(use_shared=True)
181
 
        if not reconfiguration.changes_planned():
182
 
            raise errors.AlreadyUsingShared(bzrdir)
183
 
        return reconfiguration
184
 
 
185
 
    @classmethod
186
 
    def to_standalone(klass, bzrdir):
187
 
        """Convert a repository branch into a standalone branch"""
188
 
        reconfiguration = klass(bzrdir)
189
 
        reconfiguration._set_use_shared(use_shared=False)
190
 
        if not reconfiguration.changes_planned():
191
 
            raise errors.AlreadyStandalone(bzrdir)
192
 
        return reconfiguration
193
 
 
194
 
    @classmethod
195
 
    def set_repository_trees(klass, bzrdir, with_trees):
196
 
        """Adjust a repository's working tree presence default"""
197
 
        reconfiguration = klass(bzrdir)
198
 
        if not reconfiguration.repository.is_shared():
199
 
            raise errors.ReconfigurationNotSupported(reconfiguration.bzrdir)
200
 
        if with_trees and reconfiguration.repository.make_working_trees():
201
 
            raise errors.AlreadyWithTrees(bzrdir)
202
 
        elif (not with_trees
203
 
              and not reconfiguration.repository.make_working_trees()):
204
 
            raise errors.AlreadyWithNoTrees(bzrdir)
205
 
        else:
206
 
            reconfiguration._repository_trees = with_trees
207
 
        return reconfiguration
208
 
 
209
126
    def _plan_changes(self, want_tree, want_branch, want_bound,
210
127
                      want_reference):
211
128
        """Determine which changes are needed to assume the configuration"""
213
130
            raise errors.ReconfigurationNotSupported(self.bzrdir)
214
131
        if want_branch and want_reference:
215
132
            raise errors.ReconfigurationNotSupported(self.bzrdir)
 
133
        if (want_branch or want_reference) and (self.local_branch is None and
 
134
                                                self.referenced_branch
 
135
                                                is None):
 
136
            raise errors.ReconfigurationNotSupported(self.bzrdir)
216
137
        if self.repository is None:
217
138
            if not want_reference:
218
139
                self._create_repository = True
219
140
        else:
220
141
            if want_reference and (self.repository.bzrdir.root_transport.base
221
142
                                   == self.bzrdir.root_transport.base):
222
 
                if not self.repository.is_shared():
223
 
                    self._destroy_repository = True
 
143
                self._destroy_repository = True
224
144
        if self.referenced_branch is None:
225
145
            if want_reference:
226
146
                self._create_reference = True
227
 
                if self.local_branch is not None:
228
 
                    self._destroy_branch = True
229
147
        else:
230
148
            if not want_reference:
231
149
                self._destroy_reference = True
246
164
        if want_tree and self.tree is None:
247
165
            self._create_tree = True
248
166
 
249
 
    def _set_use_shared(self, use_shared=None):
250
 
        if use_shared is None:
251
 
            return
252
 
        if use_shared:
253
 
            if self.local_repository is not None:
254
 
                self._destroy_repository = True
255
 
        else:
256
 
            if self.local_repository is None:
257
 
                self._create_repository = True
258
 
 
259
167
    def changes_planned(self):
260
168
        """Return True if changes are planned, False otherwise"""
261
169
        return (self._unbind or self._bind or self._destroy_tree
262
170
                or self._create_tree or self._destroy_reference
263
 
                or self._create_branch or self._create_repository
264
 
                or self._create_reference or self._destroy_repository)
 
171
                or self._create_branch or self._create_repository)
265
172
 
266
173
    def _check(self):
267
174
        """Raise if reconfiguration would destroy local changes"""
268
175
        if self._destroy_tree:
269
 
            # XXX: What about pending merges ? -- vila 20090629
270
 
            if self.tree.has_changes(self.tree.basis_tree()):
 
176
            changes = self.tree.changes_from(self.tree.basis_tree())
 
177
            if changes.has_changed():
271
178
                raise errors.UncommittedChanges(self.tree)
272
 
        if self._create_reference and self.local_branch is not None:
273
 
            reference_branch = branch.Branch.open(self._select_bind_location())
274
 
            if (reference_branch.last_revision() !=
275
 
                self.local_branch.last_revision()):
276
 
                raise errors.UnsyncedBranches(self.bzrdir, reference_branch)
277
179
 
278
180
    def _select_bind_location(self):
279
181
        """Select a location to bind or create a reference to.
318
220
        if not force:
319
221
            self._check()
320
222
        if self._create_repository:
321
 
            if self.local_branch and not self._destroy_branch:
322
 
                old_repo = self.local_branch.repository
323
 
            elif self._create_branch and self.referenced_branch is not None:
324
 
                old_repo = self.referenced_branch.repository
325
 
            else:
326
 
                old_repo = None
327
 
            if old_repo is not None:
328
 
                repository_format = old_repo._format
329
 
            else:
330
 
                repository_format = None
331
 
            if repository_format is not None:
332
 
                repo = repository_format.initialize(self.bzrdir)
333
 
            else:
334
 
                repo = self.bzrdir.create_repository()
335
 
            if self.local_branch and not self._destroy_branch:
336
 
                repo.fetch(self.local_branch.repository,
337
 
                           self.local_branch.last_revision())
 
223
            repo = self.bzrdir.create_repository()
338
224
        else:
339
225
            repo = self.repository
340
 
        if self._create_branch and self.referenced_branch is not None:
 
226
        if self._create_branch:
341
227
            repo.fetch(self.referenced_branch.repository,
342
228
                       self.referenced_branch.last_revision())
343
 
        if self._create_reference:
344
 
            reference_branch = branch.Branch.open(self._select_bind_location())
345
 
        if self._destroy_repository:
346
 
            if self._create_reference:
347
 
                reference_branch.repository.fetch(self.repository)
348
 
            elif self.local_branch is not None and not self._destroy_branch:
349
 
                up = self.local_branch.bzrdir.root_transport.clone('..')
350
 
                up_bzrdir = bzrdir.BzrDir.open_containing_from_transport(up)[0]
351
 
                new_repo = up_bzrdir.find_repository()
352
 
                new_repo.fetch(self.repository)
353
 
        last_revision_info = None
354
229
        if self._destroy_reference:
355
 
            last_revision_info = self.referenced_branch.last_revision_info()
 
230
            reference_info = self.referenced_branch.last_revision_info()
356
231
            self.bzrdir.destroy_branch()
357
232
        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)
 
233
            reference_info = self.local_branch.last_revision_info()
361
234
            self.bzrdir.destroy_branch()
362
235
        if self._create_branch:
363
236
            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)
 
237
            local_branch.set_last_revision_info(*reference_info)
369
238
        else:
370
239
            local_branch = self.local_branch
371
240
        if self._create_reference:
 
241
            reference_branch = branch.Branch.open(self._select_bind_location())
372
242
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
373
243
                reference_branch)
374
244
        if self._destroy_tree:
381
251
            bind_location = self._select_bind_location()
382
252
            local_branch.bind(branch.Branch.open(bind_location))
383
253
        if self._destroy_repository:
 
254
            if self._create_reference:
 
255
                reference_branch.repository.fetch(self.repository)
384
256
            self.bzrdir.destroy_repository()
385
 
        if self._repository_trees is not None:
386
 
            repo.set_make_working_trees(self._repository_trees)