71
97
if not self.bzrdir.needs_format_conversion(format):
72
98
raise errors.UpToDateFormat(self.bzrdir._format)
73
99
if not self.bzrdir.can_convert_format():
74
raise errors.BzrError("cannot upgrade from bzrdir format %s" %
100
raise errors.BzrError(gettext("cannot upgrade from bzrdir format %s") %
75
101
self.bzrdir._format)
76
102
self.bzrdir.check_conversion_target(format)
77
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'))
79
self.bzrdir.backup_bzrdir()
106
self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
80
107
while self.bzrdir.needs_format_conversion(format):
81
108
converter = self.bzrdir._format.get_converter(format)
82
109
self.bzrdir = converter.convert(self.bzrdir, None)
83
ui.ui_factory.note("finished")
86
def upgrade(url, format=None):
87
"""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