1
# Copyright (C) 2005, 2006, 2008-2011 Canonical Ltd
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.
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.
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
17
"""bzr upgrade logic."""
26
from bzrlib.controldir import (
30
from bzrlib.i18n import gettext
31
from bzrlib.remote import RemoteBzrDir
34
class Convert(object):
36
def __init__(self, url=None, format=None, control_dir=None):
37
"""Convert a Bazaar control directory to a given format.
39
Either the url or control_dir parameter must be given.
41
:param url: the URL of the control directory or None if the
42
control_dir is explicitly given instead
43
:param format: the format to convert to or None for the default
44
:param control_dir: the control directory or None if it is
45
specified via the URL parameter instead
48
# XXX: Change to cleanup
49
warning_id = 'cross_format_fetch'
50
saved_warning = warning_id in ui.ui_factory.suppressed_warnings
51
if url is None and control_dir is None:
53
"either the url or control_dir parameter must be set.")
54
if control_dir is not None:
55
self.bzrdir = control_dir
57
self.bzrdir = ControlDir.open_unsupported(url)
58
if isinstance(self.bzrdir, RemoteBzrDir):
59
self.bzrdir._ensure_real()
60
self.bzrdir = self.bzrdir._real_bzrdir
61
if self.bzrdir.root_transport.is_readonly():
62
raise errors.UpgradeReadonly
63
self.transport = self.bzrdir.root_transport
64
ui.ui_factory.suppressed_warnings.add(warning_id)
69
ui.ui_factory.suppressed_warnings.remove(warning_id)
73
branch = self.bzrdir.open_branch()
74
if branch.user_url != self.bzrdir.user_url:
75
ui.ui_factory.note(gettext(
76
'This is a checkout. The branch (%s) needs to be upgraded'
77
' separately.') % (urlutils.unescape_for_display(
78
branch.user_url, 'utf-8')))
80
except (errors.NotBranchError, errors.IncompatibleRepositories):
81
# might not be a format we can open without upgrading; see e.g.
82
# https://bugs.launchpad.net/bzr/+bug/253891
84
if self.format is None:
86
rich_root = self.bzrdir.find_repository()._format.rich_root_data
87
except errors.NoRepositoryPresent:
88
rich_root = False # assume no rich roots
90
format_name = "default-rich-root"
92
format_name = "default"
93
format = format_registry.make_bzrdir(format_name)
96
if not self.bzrdir.needs_format_conversion(format):
97
raise errors.UpToDateFormat(self.bzrdir._format)
98
if not self.bzrdir.can_convert_format():
99
raise errors.BzrError(gettext("cannot upgrade from bzrdir format %s") %
101
self.bzrdir.check_conversion_target(format)
102
ui.ui_factory.note(gettext('starting upgrade of %s') %
103
urlutils.unescape_for_display(self.transport.base, 'utf-8'))
105
self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
106
while self.bzrdir.needs_format_conversion(format):
107
converter = self.bzrdir._format.get_converter(format)
108
self.bzrdir = converter.convert(self.bzrdir, None)
109
ui.ui_factory.note(gettext('finished'))
112
"""Clean-up after a conversion.
114
This removes the backup.bzr directory.
116
transport = self.transport
117
backup_relpath = transport.relpath(self.backup_newpath)
118
child_pb = ui.ui_factory.nested_progress_bar()
119
child_pb.update(gettext('Deleting backup.bzr'))
121
transport.delete_tree(backup_relpath)
126
def upgrade(url, format=None, clean_up=False, dry_run=False):
127
"""Upgrade locations to format.
129
This routine wraps the smart_upgrade() routine with a nicer UI.
130
In particular, it ensures all URLs can be opened before starting
131
and reports a summary at the end if more than one upgrade was attempted.
132
This routine is useful for command line tools. Other bzrlib clients
133
probably ought to use smart_upgrade() instead.
135
:param url: a URL of the locations to upgrade.
136
:param format: the format to convert to or None for the best default
137
:param clean-up: if True, the backup.bzr directory is removed if the
138
upgrade succeeded for a given repo/branch/tree
139
:param dry_run: show what would happen but don't actually do any upgrades
140
:return: the list of exceptions encountered
142
control_dirs = [ControlDir.open_unsupported(url)]
143
attempted, succeeded, exceptions = smart_upgrade(control_dirs,
144
format, clean_up=clean_up, dry_run=dry_run)
145
if len(attempted) > 1:
146
attempted_count = len(attempted)
147
succeeded_count = len(succeeded)
148
failed_count = attempted_count - succeeded_count
150
gettext('\nSUMMARY: {0} upgrades attempted, {1} succeeded,'\
151
' {2} failed').format(
152
attempted_count, succeeded_count, failed_count))
156
def smart_upgrade(control_dirs, format, clean_up=False,
158
"""Convert control directories to a new format intelligently.
160
If the control directory is a shared repository, dependent branches
161
are also converted provided the repository converted successfully.
162
If the conversion of a branch fails, remaining branches are still tried.
164
:param control_dirs: the BzrDirs to upgrade
165
:param format: the format to convert to or None for the best default
166
:param clean_up: if True, the backup.bzr directory is removed if the
167
upgrade succeeded for a given repo/branch/tree
168
:param dry_run: show what would happen but don't actually do any upgrades
169
:return: attempted-control-dirs, succeeded-control-dirs, exceptions
174
for control_dir in control_dirs:
175
attempted, succeeded, exceptions = _smart_upgrade_one(control_dir,
176
format, clean_up=clean_up, dry_run=dry_run)
177
all_attempted.extend(attempted)
178
all_succeeded.extend(succeeded)
179
all_exceptions.extend(exceptions)
180
return all_attempted, all_succeeded, all_exceptions
183
def _smart_upgrade_one(control_dir, format, clean_up=False,
185
"""Convert a control directory to a new format intelligently.
187
See smart_upgrade for parameter details.
189
# If the URL is a shared repository, find the dependent branches
192
repo = control_dir.open_repository()
193
except errors.NoRepositoryPresent:
194
# A branch or checkout using a shared repository higher up
197
# The URL is a repository. If it successfully upgrades,
198
# then upgrade the dependent branches as well.
200
dependents = repo.find_branches(using=True)
203
attempted = [control_dir]
204
succeeded, exceptions = _convert_items([control_dir], format, clean_up,
206
if succeeded and dependents:
207
ui.ui_factory.note(gettext('Found %d dependent branches - upgrading ...')
208
% (len(dependents),))
209
# Convert dependent branches
210
branch_cdirs = [b.bzrdir for b in dependents]
211
successes, problems = _convert_items(branch_cdirs, format, clean_up,
212
dry_run, label="branch")
213
attempted.extend(branch_cdirs)
214
succeeded.extend(successes)
215
exceptions.extend(problems)
218
return attempted, succeeded, exceptions
220
# FIXME: There are several problems below:
221
# - RemoteRepository doesn't support _unsupported (really ?)
222
# - raising AssertionError is rude and may not be necessary
224
# - the only caller uses only the label
225
def _get_object_and_label(control_dir):
226
"""Return the primary object and type label for a control directory.
228
:return: object, label where:
229
* object is a Branch, Repository or WorkingTree and
232
* repository - a repository
233
* tree - a lightweight checkout
237
br = control_dir.open_branch(unsupported=True,
238
ignore_fallbacks=True)
239
except NotImplementedError:
240
# RemoteRepository doesn't support the unsupported parameter
241
br = control_dir.open_branch(ignore_fallbacks=True)
242
except errors.NotBranchError:
247
repo = control_dir.open_repository()
248
except errors.NoRepositoryPresent:
251
return repo, "repository"
253
wt = control_dir.open_workingtree()
254
except (errors.NoWorkingTree, errors.NotLocalUrl):
258
raise AssertionError("unknown type of control directory %s", control_dir)
261
def _convert_items(items, format, clean_up, dry_run, label=None):
262
"""Convert a sequence of control directories to the given format.
264
:param items: the control directories to upgrade
265
:param format: the format to convert to or None for the best default
266
:param clean-up: if True, the backup.bzr directory is removed if the
267
upgrade succeeded for a given repo/branch/tree
268
:param dry_run: show what would happen but don't actually do any upgrades
269
:param label: the label for these items or None to calculate one
270
:return: items successfully upgraded, exceptions
274
child_pb = ui.ui_factory.nested_progress_bar()
275
child_pb.update(gettext('Upgrading bzrdirs'), 0, len(items))
276
for i, control_dir in enumerate(items):
278
location = control_dir.root_transport.base
279
bzr_object, bzr_label = _get_object_and_label(control_dir)
280
type_label = label or bzr_label
281
child_pb.update(gettext("Upgrading %s") % (type_label), i+1, len(items))
282
ui.ui_factory.note(gettext('Upgrading {0} {1} ...').format(type_label,
283
urlutils.unescape_for_display(location, 'utf-8'),))
286
cv = Convert(control_dir=control_dir, format=format)
287
except errors.UpToDateFormat, ex:
288
ui.ui_factory.note(str(ex))
289
succeeded.append(control_dir)
291
except Exception, ex:
292
trace.warning('conversion error: %s' % ex)
293
exceptions.append(ex)
296
# Do any required post processing
297
succeeded.append(control_dir)
300
ui.ui_factory.note(gettext('Removing backup ...'))
303
except Exception, ex:
304
trace.warning(gettext('failed to clean-up {0}: {1}') % (location, ex))
305
exceptions.append(ex)
310
return succeeded, exceptions