71
96
if not self.bzrdir.needs_format_conversion(format):
72
97
raise errors.UpToDateFormat(self.bzrdir._format)
73
98
if not self.bzrdir.can_convert_format():
74
raise errors.BzrError("cannot upgrade from bzrdir format %s" %
99
raise errors.BzrError(gettext("cannot upgrade from bzrdir format %s") %
75
100
self.bzrdir._format)
76
101
self.bzrdir.check_conversion_target(format)
77
ui.ui_factory.note('starting upgrade of %s' % self.transport.base)
102
ui.ui_factory.note(gettext('starting upgrade of %s') %
103
urlutils.unescape_for_display(self.transport.base, 'utf-8'))
79
self.bzrdir.backup_bzrdir()
105
self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
80
106
while self.bzrdir.needs_format_conversion(format):
81
107
converter = self.bzrdir._format.get_converter(format)
82
108
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."""
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