17
17
"""bzr upgrade logic."""
19
from __future__ import absolute_import
20
from bzrlib.bzrdir import BzrDir, format_registry
21
import bzrlib.errors as errors
27
from bzrlib.controldir import (
31
from bzrlib.i18n import gettext
22
32
from bzrlib.remote import RemoteBzrDir
23
import bzrlib.ui as ui
26
35
class Convert(object):
28
def __init__(self, url, format=None):
37
def __init__(self, url=None, format=None, control_dir=None):
38
"""Convert a Bazaar control directory to a given format.
40
Either the url or control_dir parameter must be given.
42
:param url: the URL of the control directory or None if the
43
control_dir is explicitly given instead
44
:param format: the format to convert to or None for the default
45
:param control_dir: the control directory or None if it is
46
specified via the URL parameter instead
29
48
self.format = format
30
self.bzrdir = BzrDir.open_unsupported(url)
49
# XXX: Change to cleanup
50
warning_id = 'cross_format_fetch'
51
saved_warning = warning_id in ui.ui_factory.suppressed_warnings
52
if url is None and control_dir is None:
54
"either the url or control_dir parameter must be set.")
55
if control_dir is not None:
56
self.bzrdir = control_dir
58
self.bzrdir = ControlDir.open_unsupported(url)
31
59
if isinstance(self.bzrdir, RemoteBzrDir):
32
60
self.bzrdir._ensure_real()
33
61
self.bzrdir = self.bzrdir._real_bzrdir
34
62
if self.bzrdir.root_transport.is_readonly():
35
63
raise errors.UpgradeReadonly
36
64
self.transport = self.bzrdir.root_transport
65
ui.ui_factory.suppressed_warnings.add(warning_id)
70
ui.ui_factory.suppressed_warnings.remove(warning_id)
41
74
branch = self.bzrdir.open_branch()
42
if branch.bzrdir.root_transport.base != \
43
self.bzrdir.root_transport.base:
44
ui.ui_factory.note("This is a checkout. The branch (%s) needs to be "
45
"upgraded separately." %
46
branch.bzrdir.root_transport.base)
75
if branch.user_url != self.bzrdir.user_url:
76
ui.ui_factory.note(gettext(
77
'This is a checkout. The branch (%s) needs to be upgraded'
78
' separately.') % (urlutils.unescape_for_display(
79
branch.user_url, 'utf-8')))
48
81
except (errors.NotBranchError, errors.IncompatibleRepositories):
49
82
# might not be a format we can open without upgrading; see e.g.
64
97
if not self.bzrdir.needs_format_conversion(format):
65
98
raise errors.UpToDateFormat(self.bzrdir._format)
66
99
if not self.bzrdir.can_convert_format():
67
raise errors.BzrError("cannot upgrade from bzrdir format %s" %
100
raise errors.BzrError(gettext("cannot upgrade from bzrdir format %s") %
68
101
self.bzrdir._format)
69
102
self.bzrdir.check_conversion_target(format)
70
ui.ui_factory.note('starting upgrade of %s' % self.transport.base)
103
ui.ui_factory.note(gettext('starting upgrade of %s') %
104
urlutils.unescape_for_display(self.transport.base, 'utf-8'))
72
self.bzrdir.backup_bzrdir()
106
self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
73
107
while self.bzrdir.needs_format_conversion(format):
74
108
converter = self.bzrdir._format.get_converter(format)
75
109
self.bzrdir = converter.convert(self.bzrdir, None)
76
ui.ui_factory.note("finished")
79
def upgrade(url, format=None):
80
"""Upgrade to format, or the default bzrdir format if not supplied."""
110
ui.ui_factory.note(gettext('finished'))
113
"""Clean-up after a conversion.
115
This removes the backup.bzr directory.
117
transport = self.transport
118
backup_relpath = transport.relpath(self.backup_newpath)
119
child_pb = ui.ui_factory.nested_progress_bar()
120
child_pb.update(gettext('Deleting backup.bzr'))
122
transport.delete_tree(backup_relpath)
127
def upgrade(url, format=None, clean_up=False, dry_run=False):
128
"""Upgrade locations to format.
130
This routine wraps the smart_upgrade() routine with a nicer UI.
131
In particular, it ensures all URLs can be opened before starting
132
and reports a summary at the end if more than one upgrade was attempted.
133
This routine is useful for command line tools. Other bzrlib clients
134
probably ought to use smart_upgrade() instead.
136
:param url: a URL of the locations to upgrade.
137
:param format: the format to convert to or None for the best default
138
:param clean-up: if True, the backup.bzr directory is removed if the
139
upgrade succeeded for a given repo/branch/tree
140
:param dry_run: show what would happen but don't actually do any upgrades
141
:return: the list of exceptions encountered
143
control_dirs = [ControlDir.open_unsupported(url)]
144
attempted, succeeded, exceptions = smart_upgrade(control_dirs,
145
format, clean_up=clean_up, dry_run=dry_run)
146
if len(attempted) > 1:
147
attempted_count = len(attempted)
148
succeeded_count = len(succeeded)
149
failed_count = attempted_count - succeeded_count
151
gettext('\nSUMMARY: {0} upgrades attempted, {1} succeeded,'\
152
' {2} failed').format(
153
attempted_count, succeeded_count, failed_count))
157
def smart_upgrade(control_dirs, format, clean_up=False,
159
"""Convert control directories to a new format intelligently.
161
If the control directory is a shared repository, dependent branches
162
are also converted provided the repository converted successfully.
163
If the conversion of a branch fails, remaining branches are still tried.
165
:param control_dirs: the BzrDirs to upgrade
166
:param format: the format to convert to or None for the best default
167
:param clean_up: if True, the backup.bzr directory is removed if the
168
upgrade succeeded for a given repo/branch/tree
169
:param dry_run: show what would happen but don't actually do any upgrades
170
:return: attempted-control-dirs, succeeded-control-dirs, exceptions
175
for control_dir in control_dirs:
176
attempted, succeeded, exceptions = _smart_upgrade_one(control_dir,
177
format, clean_up=clean_up, dry_run=dry_run)
178
all_attempted.extend(attempted)
179
all_succeeded.extend(succeeded)
180
all_exceptions.extend(exceptions)
181
return all_attempted, all_succeeded, all_exceptions
184
def _smart_upgrade_one(control_dir, format, clean_up=False,
186
"""Convert a control directory to a new format intelligently.
188
See smart_upgrade for parameter details.
190
# If the URL is a shared repository, find the dependent branches
193
repo = control_dir.open_repository()
194
except errors.NoRepositoryPresent:
195
# A branch or checkout using a shared repository higher up
198
# The URL is a repository. If it successfully upgrades,
199
# then upgrade the dependent branches as well.
201
dependents = repo.find_branches(using=True)
204
attempted = [control_dir]
205
succeeded, exceptions = _convert_items([control_dir], format, clean_up,
207
if succeeded and dependents:
208
ui.ui_factory.note(gettext('Found %d dependent branches - upgrading ...')
209
% (len(dependents),))
210
# Convert dependent branches
211
branch_cdirs = [b.bzrdir for b in dependents]
212
successes, problems = _convert_items(branch_cdirs, format, clean_up,
213
dry_run, label="branch")
214
attempted.extend(branch_cdirs)
215
succeeded.extend(successes)
216
exceptions.extend(problems)
219
return attempted, succeeded, exceptions
221
# FIXME: There are several problems below:
222
# - RemoteRepository doesn't support _unsupported (really ?)
223
# - raising AssertionError is rude and may not be necessary
225
# - the only caller uses only the label
226
def _get_object_and_label(control_dir):
227
"""Return the primary object and type label for a control directory.
229
:return: object, label where:
230
* object is a Branch, Repository or WorkingTree and
233
* repository - a repository
234
* tree - a lightweight checkout
238
br = control_dir.open_branch(unsupported=True,
239
ignore_fallbacks=True)
240
except NotImplementedError:
241
# RemoteRepository doesn't support the unsupported parameter
242
br = control_dir.open_branch(ignore_fallbacks=True)
243
except errors.NotBranchError:
248
repo = control_dir.open_repository()
249
except errors.NoRepositoryPresent:
252
return repo, "repository"
254
wt = control_dir.open_workingtree()
255
except (errors.NoWorkingTree, errors.NotLocalUrl):
259
raise AssertionError("unknown type of control directory %s", control_dir)
262
def _convert_items(items, format, clean_up, dry_run, label=None):
263
"""Convert a sequence of control directories to the given format.
265
:param items: the control directories to upgrade
266
:param format: the format to convert to or None for the best default
267
:param clean-up: if True, the backup.bzr directory is removed if the
268
upgrade succeeded for a given repo/branch/tree
269
:param dry_run: show what would happen but don't actually do any upgrades
270
:param label: the label for these items or None to calculate one
271
:return: items successfully upgraded, exceptions
275
child_pb = ui.ui_factory.nested_progress_bar()
276
child_pb.update(gettext('Upgrading bzrdirs'), 0, len(items))
277
for i, control_dir in enumerate(items):
279
location = control_dir.root_transport.base
280
bzr_object, bzr_label = _get_object_and_label(control_dir)
281
type_label = label or bzr_label
282
child_pb.update(gettext("Upgrading %s") % (type_label), i+1, len(items))
283
ui.ui_factory.note(gettext('Upgrading {0} {1} ...').format(type_label,
284
urlutils.unescape_for_display(location, 'utf-8'),))
287
cv = Convert(control_dir=control_dir, format=format)
288
except errors.UpToDateFormat, ex:
289
ui.ui_factory.note(str(ex))
290
succeeded.append(control_dir)
292
except Exception, ex:
293
trace.warning('conversion error: %s' % ex)
294
exceptions.append(ex)
297
# Do any required post processing
298
succeeded.append(control_dir)
301
ui.ui_factory.note(gettext('Removing backup ...'))
304
except Exception, ex:
305
trace.warning(gettext('failed to clean-up {0}: {1}') % (location, ex))
306
exceptions.append(ex)
311
return succeeded, exceptions