/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: John Arbash Meinel
  • Date: 2009-10-02 02:21:12 UTC
  • mto: (4679.6.1 2.1-export-c-api)
  • mto: This revision was merged to the branch mainline in revision 4735.
  • Revision ID: john@arbash-meinel.com-20091002022112-w4ispnaes2t78pty
A quick note about how I'd *like* to do things.

Basically, if I could procedurally declare my API, then all of the boilerplate
could be easily generated from that. However, it needs to be 'static' in a C
header file, and to get that you'd have to generate it from some other source
or have a *lot* more voodoo in your C pre-processor code. Shame that C sucks so
much. Note that Pyrex gets this right, where doing all the boilerplate amounts
to adding 'api' to your definition. Aka
    cdef api object myfunc(int foo)
Is enough to generate all the boilerplate C headers, etc you would need.
It is unfortunate I can't really use that here, because I want a pure C class
to avoid the python garbage collector.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2009 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
 
23
 
 
24
from bzrlib import (
 
25
    branch,
 
26
    bzrdir,
 
27
    errors,
 
28
    trace,
 
29
    ui,
 
30
    urlutils,
 
31
    )
 
32
 
 
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
class Reconfigure(object):
 
74
 
 
75
    def __init__(self, bzrdir, new_bound_location=None):
 
76
        self.bzrdir = bzrdir
 
77
        self.new_bound_location = new_bound_location
 
78
        self.local_repository = None
 
79
        try:
 
80
            self.repository = self.bzrdir.find_repository()
 
81
        except errors.NoRepositoryPresent:
 
82
            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
        try:
 
91
            branch = self.bzrdir.open_branch()
 
92
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
 
93
                self.local_branch = branch
 
94
                self.referenced_branch = None
 
95
            else:
 
96
                self.local_branch = None
 
97
                self.referenced_branch = branch
 
98
        except errors.NotBranchError:
 
99
            self.local_branch = None
 
100
            self.referenced_branch = None
 
101
        try:
 
102
            self.tree = bzrdir.open_workingtree()
 
103
        except errors.NoWorkingTree:
 
104
            self.tree = None
 
105
        self._unbind = False
 
106
        self._bind = False
 
107
        self._destroy_reference = False
 
108
        self._create_reference = False
 
109
        self._destroy_branch = False
 
110
        self._create_branch = False
 
111
        self._destroy_tree = False
 
112
        self._create_tree = False
 
113
        self._create_repository = False
 
114
        self._destroy_repository = False
 
115
        self._repository_trees = None
 
116
 
 
117
    @staticmethod
 
118
    def to_branch(bzrdir):
 
119
        """Return a Reconfiguration to convert this bzrdir into a branch
 
120
 
 
121
        :param bzrdir: The bzrdir to reconfigure
 
122
        :raise errors.AlreadyBranch: if bzrdir is already a branch
 
123
        """
 
124
        reconfiguration = Reconfigure(bzrdir)
 
125
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
 
126
                                      want_bound=False, want_reference=False)
 
127
        if not reconfiguration.changes_planned():
 
128
            raise errors.AlreadyBranch(bzrdir)
 
129
        return reconfiguration
 
130
 
 
131
    @staticmethod
 
132
    def to_tree(bzrdir):
 
133
        """Return a Reconfiguration to convert this bzrdir into a tree
 
134
 
 
135
        :param bzrdir: The bzrdir to reconfigure
 
136
        :raise errors.AlreadyTree: if bzrdir is already a tree
 
137
        """
 
138
        reconfiguration = Reconfigure(bzrdir)
 
139
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
 
140
                                      want_bound=False, want_reference=False)
 
141
        if not reconfiguration.changes_planned():
 
142
            raise errors.AlreadyTree(bzrdir)
 
143
        return reconfiguration
 
144
 
 
145
    @staticmethod
 
146
    def to_checkout(bzrdir, bound_location=None):
 
147
        """Return a Reconfiguration to convert this bzrdir into a checkout
 
148
 
 
149
        :param bzrdir: The bzrdir to reconfigure
 
150
        :param bound_location: The location the checkout should be bound to.
 
151
        :raise errors.AlreadyCheckout: if bzrdir is already a checkout
 
152
        """
 
153
        reconfiguration = Reconfigure(bzrdir, bound_location)
 
154
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
 
155
                                      want_bound=True, want_reference=False)
 
156
        if not reconfiguration.changes_planned():
 
157
            raise errors.AlreadyCheckout(bzrdir)
 
158
        return reconfiguration
 
159
 
 
160
    @classmethod
 
161
    def to_lightweight_checkout(klass, bzrdir, reference_location=None):
 
162
        """Make a Reconfiguration to convert bzrdir into a lightweight checkout
 
163
 
 
164
        :param bzrdir: The bzrdir to reconfigure
 
165
        :param bound_location: The location the checkout should be bound to.
 
166
        :raise errors.AlreadyLightweightCheckout: if bzrdir is already a
 
167
            lightweight checkout
 
168
        """
 
169
        reconfiguration = klass(bzrdir, reference_location)
 
170
        reconfiguration._plan_changes(want_tree=True, want_branch=False,
 
171
                                      want_bound=False, want_reference=True)
 
172
        if not reconfiguration.changes_planned():
 
173
            raise errors.AlreadyLightweightCheckout(bzrdir)
 
174
        return reconfiguration
 
175
 
 
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
    def _plan_changes(self, want_tree, want_branch, want_bound,
 
210
                      want_reference):
 
211
        """Determine which changes are needed to assume the configuration"""
 
212
        if not want_branch and not want_reference:
 
213
            raise errors.ReconfigurationNotSupported(self.bzrdir)
 
214
        if want_branch and want_reference:
 
215
            raise errors.ReconfigurationNotSupported(self.bzrdir)
 
216
        if self.repository is None:
 
217
            if not want_reference:
 
218
                self._create_repository = True
 
219
        else:
 
220
            if want_reference and (self.repository.bzrdir.root_transport.base
 
221
                                   == self.bzrdir.root_transport.base):
 
222
                if not self.repository.is_shared():
 
223
                    self._destroy_repository = True
 
224
        if self.referenced_branch is None:
 
225
            if want_reference:
 
226
                self._create_reference = True
 
227
                if self.local_branch is not None:
 
228
                    self._destroy_branch = True
 
229
        else:
 
230
            if not want_reference:
 
231
                self._destroy_reference = True
 
232
        if self.local_branch is None:
 
233
            if want_branch is True:
 
234
                self._create_branch = True
 
235
                if want_bound:
 
236
                    self._bind = True
 
237
        else:
 
238
            if want_bound:
 
239
                if self.local_branch.get_bound_location() is None:
 
240
                    self._bind = True
 
241
            else:
 
242
                if self.local_branch.get_bound_location() is not None:
 
243
                    self._unbind = True
 
244
        if not want_tree and self.tree is not None:
 
245
            self._destroy_tree = True
 
246
        if want_tree and self.tree is None:
 
247
            self._create_tree = True
 
248
 
 
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
    def changes_planned(self):
 
260
        """Return True if changes are planned, False otherwise"""
 
261
        return (self._unbind or self._bind or self._destroy_tree
 
262
                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)
 
265
 
 
266
    def _check(self):
 
267
        """Raise if reconfiguration would destroy local changes"""
 
268
        if self._destroy_tree:
 
269
            # XXX: What about pending merges ? -- vila 20090629
 
270
            if self.tree.has_changes(self.tree.basis_tree()):
 
271
                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
 
 
278
    def _select_bind_location(self):
 
279
        """Select a location to bind or create a reference to.
 
280
 
 
281
        Preference is:
 
282
        1. user specified location
 
283
        2. branch reference location (it's a kind of bind location)
 
284
        3. current bind location
 
285
        4. previous bind location (it was a good choice once)
 
286
        5. push location (it's writeable, so committable)
 
287
        6. parent location (it's pullable, so update-from-able)
 
288
        """
 
289
        if self.new_bound_location is not None:
 
290
            return self.new_bound_location
 
291
        if self.local_branch is not None:
 
292
            bound = self.local_branch.get_bound_location()
 
293
            if bound is not None:
 
294
                return bound
 
295
            old_bound = self.local_branch.get_old_bound_location()
 
296
            if old_bound is not None:
 
297
                return old_bound
 
298
            push_location = self.local_branch.get_push_location()
 
299
            if push_location is not None:
 
300
                return push_location
 
301
            parent = self.local_branch.get_parent()
 
302
            if parent is not None:
 
303
                return parent
 
304
        elif self.referenced_branch is not None:
 
305
            return self.referenced_branch.base
 
306
        raise errors.NoBindLocation(self.bzrdir)
 
307
 
 
308
    def apply(self, force=False):
 
309
        """Apply the reconfiguration
 
310
 
 
311
        :param force: If true, the reconfiguration is applied even if it will
 
312
            destroy local changes.
 
313
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
 
314
            but contains uncommitted changes.
 
315
        :raise errors.NoBindLocation: if no bind location was specified and
 
316
            none could be autodetected.
 
317
        """
 
318
        if not force:
 
319
            self._check()
 
320
        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())
 
338
        else:
 
339
            repo = self.repository
 
340
        if self._create_branch and self.referenced_branch is not None:
 
341
            repo.fetch(self.referenced_branch.repository,
 
342
                       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
        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)
 
361
            self.bzrdir.destroy_branch()
 
362
        if self._create_branch:
 
363
            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)
 
369
        else:
 
370
            local_branch = self.local_branch
 
371
        if self._create_reference:
 
372
            format = branch.BranchReferenceFormat().initialize(self.bzrdir,
 
373
                reference_branch)
 
374
        if self._destroy_tree:
 
375
            self.bzrdir.destroy_workingtree()
 
376
        if self._create_tree:
 
377
            self.bzrdir.create_workingtree()
 
378
        if self._unbind:
 
379
            self.local_branch.unbind()
 
380
        if self._bind:
 
381
            bind_location = self._select_bind_location()
 
382
            local_branch.bind(branch.Branch.open(bind_location))
 
383
        if self._destroy_repository:
 
384
            self.bzrdir.destroy_repository()
 
385
        if self._repository_trees is not None:
 
386
            repo.set_make_working_trees(self._repository_trees)