76
97
self.bzrdir.check_conversion_target(format)
77
98
ui.ui_factory.note('starting upgrade of %s' % self.transport.base)
79
self.bzrdir.backup_bzrdir()
100
self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
80
101
while self.bzrdir.needs_format_conversion(format):
81
102
converter = self.bzrdir._format.get_converter(format)
82
103
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."""
104
ui.ui_factory.note('finished')
107
"""Clean-up after a conversion.
109
This removes the backup.bzr directory.
111
transport = self.transport
112
backup_relpath = transport.relpath(self.backup_newpath)
113
child_pb = ui.ui_factory.nested_progress_bar()
114
child_pb.update('Deleting backup.bzr')
116
transport.delete_tree(backup_relpath)
121
def upgrade(url, format=None, clean_up=False, dry_run=False):
122
"""Upgrade locations to format.
124
This routine wraps the smart_upgrade() routine with a nicer UI.
125
In particular, it ensures all URLs can be opened before starting
126
and reports a summary at the end if more than one upgrade was attempted.
127
This routine is useful for command line tools. Other bzrlib clients
128
probably ought to use smart_upgrade() instead.
130
:param url: a URL of the locations to upgrade.
131
:param format: the format to convert to or None for the best default
132
:param clean-up: if True, the backup.bzr directory is removed if the
133
upgrade succeeded for a given repo/branch/tree
134
:param dry_run: show what would happen but don't actually do any upgrades
135
:return: the list of exceptions encountered
137
control_dirs = [BzrDir.open_unsupported(url)]
138
attempted, succeeded, exceptions = smart_upgrade(control_dirs,
139
format, clean_up=clean_up, dry_run=dry_run)
140
if len(attempted) > 1:
141
attempted_count = len(attempted)
142
succeeded_count = len(succeeded)
143
failed_count = attempted_count - succeeded_count
145
'\nSUMMARY: %d upgrades attempted, %d succeeded, %d failed'
146
% (attempted_count, succeeded_count, failed_count))
150
def smart_upgrade(control_dirs, format, clean_up=False,
152
"""Convert control directories to a new format intelligently.
154
If the control directory is a shared repository, dependent branches
155
are also converted provided the repository converted successfully.
156
If the conversion of a branch fails, remaining branches are still tried.
158
:param control_dirs: the BzrDirs to upgrade
159
:param format: the format to convert to or None for the best default
160
:param clean_up: if True, the backup.bzr directory is removed if the
161
upgrade succeeded for a given repo/branch/tree
162
:param dry_run: show what would happen but don't actually do any upgrades
163
:return: attempted-control-dirs, succeeded-control-dirs, exceptions
168
for control_dir in control_dirs:
169
attempted, succeeded, exceptions = _smart_upgrade_one(control_dir,
170
format, clean_up=clean_up, dry_run=dry_run)
171
all_attempted.extend(attempted)
172
all_succeeded.extend(succeeded)
173
all_exceptions.extend(exceptions)
174
return all_attempted, all_succeeded, all_exceptions
177
def _smart_upgrade_one(control_dir, format, clean_up=False,
179
"""Convert a control directory to a new format intelligently.
181
See smart_upgrade for parameter details.
183
# If the URL is a shared repository, find the dependent branches
186
repo = control_dir.open_repository()
187
except errors.NoRepositoryPresent:
188
# A branch or checkout using a shared repository higher up
191
# The URL is a repository. If it successfully upgrades,
192
# then upgrade the dependent branches as well.
194
dependents = repo.find_branches(using=True)
197
attempted = [control_dir]
198
succeeded, exceptions = _convert_items([control_dir], format, clean_up,
200
if succeeded and dependents:
201
ui.ui_factory.note('Found %d dependent branches - upgrading ...'
202
% (len(dependents),))
203
# Convert dependent branches
204
branch_cdirs = [b.bzrdir for b in dependents]
205
successes, problems = _convert_items(branch_cdirs, format, clean_up,
206
dry_run, label="branch")
207
attempted.extend(branch_cdirs)
208
succeeded.extend(successes)
209
exceptions.extend(problems)
212
return attempted, succeeded, exceptions
214
# FIXME: There are several problems below:
215
# - RemoteRepository doesn't support _unsupported (really ?)
216
# - raising AssertionError is rude and may not be necessary
218
# - the only caller uses only the label
219
def _get_object_and_label(control_dir):
220
"""Return the primary object and type label for a control directory.
222
:return: object, label where
223
object is a Branch, Repository or WorkingTree and
226
repository - a repository
227
tree - a lightweight checkout
231
br = control_dir.open_branch(unsupported=True,
232
ignore_fallbacks=True)
233
except NotImplementedError:
234
# RemoteRepository doesn't support the unsupported parameter
235
br = control_dir.open_branch(ignore_fallbacks=True)
236
except errors.NotBranchError:
241
repo = control_dir.open_repository()
242
except errors.NoRepositoryPresent:
245
return repo, "repository"
247
wt = control_dir.open_workingtree()
248
except (errors.NoWorkingTree, errors.NotLocalUrl):
252
raise AssertionError("unknown type of control directory %s", control_dir)
255
def _convert_items(items, format, clean_up, dry_run, label=None):
256
"""Convert a sequence of control directories to the given format.
258
:param items: the control directories to upgrade
259
:param format: the format to convert to or None for the best default
260
:param clean-up: if True, the backup.bzr directory is removed if the
261
upgrade succeeded for a given repo/branch/tree
262
:param dry_run: show what would happen but don't actually do any upgrades
263
:param label: the label for these items or None to calculate one
264
:return: items successfully upgraded, exceptions
268
child_pb = ui.ui_factory.nested_progress_bar()
269
child_pb.update('Upgrading bzrdirs', 0, len(items))
270
for i, control_dir in enumerate(items):
272
location = control_dir.root_transport.base
273
bzr_object, bzr_label = _get_object_and_label(control_dir)
274
type_label = label or bzr_label
275
child_pb.update("Upgrading %s" % (type_label), i+1, len(items))
276
ui.ui_factory.note('Upgrading %s %s ...' % (type_label, location,))
279
cv = Convert(control_dir=control_dir, format=format)
280
except Exception, ex:
281
trace.warning('conversion error: %s' % ex)
282
exceptions.append(ex)
285
# Do any required post processing
286
succeeded.append(control_dir)
289
ui.ui_factory.note('Removing backup ...')
292
except Exception, ex:
293
trace.warning('failed to clean-up %s: %s' % (location, ex))
294
exceptions.append(ex)
299
return succeeded, exceptions