17
17
"""bzr upgrade logic."""
26
from bzrlib.bzrdir import (
30
from bzrlib.i18n import gettext
20
from bzrlib.bzrdir import BzrDir, BzrDirFormat, format_registry
21
import bzrlib.errors as errors
31
22
from bzrlib.remote import RemoteBzrDir
23
from bzrlib.transport import get_transport
24
import bzrlib.ui as ui
34
27
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
29
def __init__(self, url, format=None):
47
30
self.format = format
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 = BzrDir.open_unsupported(url)
31
self.bzrdir = BzrDir.open_unsupported(url)
58
32
if isinstance(self.bzrdir, RemoteBzrDir):
59
33
self.bzrdir._ensure_real()
60
34
self.bzrdir = self.bzrdir._real_bzrdir
61
35
if self.bzrdir.root_transport.is_readonly():
62
36
raise errors.UpgradeReadonly
63
37
self.transport = self.bzrdir.root_transport
64
ui.ui_factory.suppressed_warnings.add(warning_id)
38
self.pb = ui.ui_factory.nested_progress_bar()
69
ui.ui_factory.suppressed_warnings.remove(warning_id)
73
46
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')))
47
if branch.bzrdir.root_transport.base != \
48
self.bzrdir.root_transport.base:
49
ui.ui_factory.note("This is a checkout. The branch (%s) needs to be "
50
"upgraded separately." %
51
branch.bzrdir.root_transport.base)
80
53
except (errors.NotBranchError, errors.IncompatibleRepositories):
81
54
# might not be a format we can open without upgrading; see e.g.
96
69
if not self.bzrdir.needs_format_conversion(format):
97
70
raise errors.UpToDateFormat(self.bzrdir._format)
98
71
if not self.bzrdir.can_convert_format():
99
raise errors.BzrError(gettext("cannot upgrade from bzrdir format %s") %
72
raise errors.BzrError("cannot upgrade from bzrdir format %s" %
100
73
self.bzrdir._format)
101
74
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'))
75
ui.ui_factory.note('starting upgrade of %s' % self.transport.base)
105
self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
77
self.bzrdir.backup_bzrdir()
106
78
while self.bzrdir.needs_format_conversion(format):
107
79
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 = [BzrDir.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 Exception, ex:
288
trace.warning('conversion error: %s' % ex)
289
exceptions.append(ex)
292
# Do any required post processing
293
succeeded.append(control_dir)
296
ui.ui_factory.note(gettext('Removing backup ...'))
299
except Exception, ex:
300
trace.warning(gettext('failed to clean-up {0}: {1}') % (location, ex))
301
exceptions.append(ex)
306
return succeeded, exceptions
80
self.bzrdir = converter.convert(self.bzrdir, self.pb)
81
ui.ui_factory.note("finished")
84
def upgrade(url, format=None):
85
"""Upgrade to format, or the default bzrdir format if not supplied."""