~bzr-pqm/bzr/bzr.dev

2796.2.1 by Aaron Bentley
Begin work on reconfigure command
1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Reconfigure a bzrdir into a new tree/branch/repository layout"""
18
19
from bzrlib import (
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
20
    branch,
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
21
    errors,
22
    )
23
24
class Reconfigure(object):
25
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
26
    def __init__(self, bzrdir, new_bound_location=None):
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
27
        self.bzrdir = bzrdir
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
28
        self.new_bound_location = new_bound_location
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
29
        try:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
30
            self.repository = self.bzrdir.find_repository()
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
31
        except errors.NoRepositoryPresent:
32
            self.repository = None
33
        try:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
34
            branch = self.bzrdir.open_branch()
35
            if branch.bzrdir.root_transport.base == bzrdir.root_transport.base:
36
                self.local_branch = branch
37
                self.referenced_branch = None
38
            else:
39
                self.local_branch = None
40
                self.referenced_branch = branch
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
41
        except errors.NotBranchError:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
42
            self.local_branch = None
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
43
            self.referenced_branch = None
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
44
        try:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
45
            self.tree = bzrdir.open_workingtree()
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
46
        except errors.NoWorkingTree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
47
            self.tree = None
2796.2.14 by Aaron Bentley
Updates from review
48
        self._unbind = False
49
        self._bind = False
50
        self._destroy_reference = False
51
        self._create_branch = False
52
        self._destroy_tree = False
53
        self._create_tree = False
54
        self._create_repository = False
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
55
56
    @staticmethod
57
    def to_branch(bzrdir):
2796.2.16 by Aaron Bentley
Documentation updates from review
58
        """Return a Reconfiguration to convert this bzrdir into a branch
59
60
        :param bzrdir: The bzrdir to reconfigure
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
64
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
65
        reconfiguration = Reconfigure(bzrdir)
2796.2.15 by Aaron Bentley
More updates from review
66
        reconfiguration._plan_changes(want_tree=False, want_branch=True,
67
                                      want_bound=False)
2796.2.14 by Aaron Bentley
Updates from review
68
        if not reconfiguration.changes_planned():
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
69
            raise errors.AlreadyBranch(bzrdir)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
70
        return reconfiguration
71
72
    @staticmethod
73
    def to_tree(bzrdir):
2796.2.16 by Aaron Bentley
Documentation updates from review
74
        """Return a Reconfiguration to convert this bzrdir into a tree
75
76
        :param bzrdir: The bzrdir to reconfigure
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
80
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
81
        reconfiguration = Reconfigure(bzrdir)
2796.2.15 by Aaron Bentley
More updates from review
82
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
83
                                      want_bound=False)
2796.2.14 by Aaron Bentley
Updates from review
84
        if not reconfiguration.changes_planned():
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
85
            raise errors.AlreadyTree(bzrdir)
86
        return reconfiguration
87
88
    @staticmethod
89
    def to_checkout(bzrdir, bound_location=None):
2796.2.16 by Aaron Bentley
Documentation updates from review
90
        """Return a Reconfiguration to convert this bzrdir into a checkout
91
92
        :param bzrdir: The bzrdir to reconfigure
93
        :param bound_location: The location the checkout should be bound to.
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
97
        """
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
98
        reconfiguration = Reconfigure(bzrdir, bound_location)
2796.2.15 by Aaron Bentley
More updates from review
99
        reconfiguration._plan_changes(want_tree=True, want_branch=True,
100
                                      want_bound=True)
2796.2.14 by Aaron Bentley
Updates from review
101
        if not reconfiguration.changes_planned():
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
102
            raise errors.AlreadyCheckout(bzrdir)
103
        return reconfiguration
104
2796.2.15 by Aaron Bentley
More updates from review
105
    def _plan_changes(self, want_tree, want_branch, want_bound):
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
106
        """Determine which changes are needed to assume the configuration"""
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
107
        if self.repository is None:
2796.2.14 by Aaron Bentley
Updates from review
108
            self._create_repository = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
109
        if self.local_branch is None:
2796.2.15 by Aaron Bentley
More updates from review
110
            if want_branch is True:
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
111
                if self.referenced_branch is not None:
2796.2.14 by Aaron Bentley
Updates from review
112
                    self._destroy_reference = True
113
                    self._create_branch = True
2796.2.15 by Aaron Bentley
More updates from review
114
                    if want_bound:
2796.2.14 by Aaron Bentley
Updates from review
115
                        self._bind = True
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
116
                else:
117
                    raise errors.ReconfigurationNotSupported(self.bzrdir)
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
118
        else:
2796.2.15 by Aaron Bentley
More updates from review
119
            if want_bound:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
120
                if self.local_branch.get_bound_location() is None:
2796.2.14 by Aaron Bentley
Updates from review
121
                    self._bind = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
122
            else:
123
                if self.local_branch.get_bound_location() is not None:
2796.2.14 by Aaron Bentley
Updates from review
124
                    self._unbind = True
2796.2.15 by Aaron Bentley
More updates from review
125
        if not want_tree and self.tree is not None:
2796.2.14 by Aaron Bentley
Updates from review
126
            self._destroy_tree = True
2796.2.15 by Aaron Bentley
More updates from review
127
        if want_tree and self.tree is None:
2796.2.14 by Aaron Bentley
Updates from review
128
            self._create_tree = True
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
129
2796.2.14 by Aaron Bentley
Updates from review
130
    def changes_planned(self):
2796.2.11 by Aaron Bentley
Cleanups
131
        """Return True if changes are planned, False otherwise"""
2796.2.14 by Aaron Bentley
Updates from review
132
        return (self._unbind or self._bind or self._destroy_tree
133
                or self._create_tree or self._destroy_reference
134
                or self._create_branch or self._create_repository)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
135
136
    def _check(self):
2796.2.11 by Aaron Bentley
Cleanups
137
        """Raise if reconfiguration would destroy local changes"""
2796.2.14 by Aaron Bentley
Updates from review
138
        if self._destroy_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
139
            changes = self.tree.changes_from(self.tree.basis_tree())
140
            if changes.has_changed():
141
                raise errors.UncommittedChanges(self.tree)
142
143
    def _select_bind_location(self):
2796.2.11 by Aaron Bentley
Cleanups
144
        """Select a location to bind to.
145
146
        Preference is:
147
        1. user specified location
148
        2. branch reference location (it's a kind of bind location)
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)
152
        """
153
        if self.new_bound_location is not None:
154
            return self.new_bound_location
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
155
        if self.local_branch is not None:
156
            old_bound = self.local_branch.get_old_bound_location()
157
            if old_bound is not None:
158
                return old_bound
159
            push_location = self.local_branch.get_push_location()
160
            if push_location is not None:
161
                return push_location
162
            parent = self.local_branch.get_parent()
163
            if parent is not None:
164
                return parent
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
165
        elif self.referenced_branch is not None:
166
            return self.referenced_branch.base
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
167
        raise errors.NoBindLocation(self.bzrdir)
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
168
169
    def apply(self, force=False):
2796.2.16 by Aaron Bentley
Documentation updates from review
170
        """Apply the reconfiguration
171
172
        :param force: If true, the reconfiguration is applied even if it will
173
            destroy local changes.
174
        :raise errors.UncommittedChanges: if the local tree is to be destroyed
175
            but contains uncommitted changes.
176
        :raise errors.NoBindLocation: if no bind location was specified and
177
            none could be autodetected.
178
        """
2796.2.1 by Aaron Bentley
Begin work on reconfigure command
179
        if not force:
180
            self._check()
2796.2.14 by Aaron Bentley
Updates from review
181
        if self._create_repository:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
182
            repo = self.bzrdir.create_repository()
183
        else:
184
            repo = self.repository
2796.2.14 by Aaron Bentley
Updates from review
185
        if self._create_branch:
2796.2.10 by Aaron Bentley
Ensure that shared repositories are used where possible
186
            repo.fetch(self.referenced_branch.repository,
187
                       self.referenced_branch.last_revision())
2796.2.14 by Aaron Bentley
Updates from review
188
        if self._destroy_reference:
2796.2.8 by Aaron Bentley
Ensure conversion from lightweight fetches revisions and sets revision info
189
            reference_info = self.referenced_branch.last_revision_info()
2796.2.7 by Aaron Bentley
Implement converting a lightweight checkout to a branch
190
            self.bzrdir.destroy_branch()
2796.2.14 by Aaron Bentley
Updates from review
191
        if self._create_branch:
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
192
            local_branch = self.bzrdir.create_branch()
193
            local_branch.set_last_revision_info(*reference_info)
194
        else:
195
            local_branch = self.local_branch
2796.2.14 by Aaron Bentley
Updates from review
196
        if self._destroy_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
197
            self.bzrdir.destroy_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
198
        if self._create_tree:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
199
            self.bzrdir.create_workingtree()
2796.2.14 by Aaron Bentley
Updates from review
200
        if self._unbind:
2796.2.3 by Aaron Bentley
Implement conversion to tree and checkout
201
            self.local_branch.unbind()
2796.2.14 by Aaron Bentley
Updates from review
202
        if self._bind:
2796.2.11 by Aaron Bentley
Cleanups
203
            bind_location = self._select_bind_location()
2796.2.9 by Aaron Bentley
Ensure conversion from lightweight checkout works
204
            local_branch.bind(branch.Branch.open(bind_location))