/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.5 by John Arbash Meinel
Merge bzr.dev 5021 to resolve NEWS
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
1267 by Martin Pool
- notes on conversion of existing history to weaves
2
#
1080 by Martin Pool
- test tool for converting history to weave files
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1267 by Martin Pool
- notes on conversion of existing history to weaves
7
#
1080 by Martin Pool
- test tool for converting history to weave files
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1267 by Martin Pool
- notes on conversion of existing history to weaves
12
#
1080 by Martin Pool
- test tool for converting history to weave files
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1080 by Martin Pool
- test tool for converting history to weave files
16
1534.4.34 by Robert Collins
Fix remaining uses of deprecated apis within bzrlib.
17
"""bzr upgrade logic."""
18
3943.2.1 by Martin Pool
doc/import cleanups
19
4360.10.40 by Vincent Ladeuil
Fix imports ;)
20
from bzrlib import (
21
    errors,
22
    osutils,
23
    repository,
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
24
    trace,
4360.10.40 by Vincent Ladeuil
Fix imports ;)
25
    ui,
26
    )
4988.11.1 by Jelmer Vernooij
Remove unused imports.
27
from bzrlib.bzrdir import BzrDir, format_registry
3577.4.1 by Andrew Bennetts
Make 'bzr upgrade' work over HPSS.
28
from bzrlib.remote import RemoteBzrDir
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
29
30
31
class Convert(object):
32
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
33
    def __init__(self, url=None, format=None, control_dir=None):
34
        """Convert a Bazaar control directory to a given format.
35
36
        Either the url or control_dir parameter must be given.
37
38
        :param url: the URL of the control directory or None if the
39
          control_dir is explicitly given instead
40
        :param format: the format to convert to or None for the default
4360.10.10 by Ian Clatworthy
minor clean-ups
41
        :param control_dir: the control directory or None if it is
42
          specified via the URL parameter instead
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
43
        """
1534.5.13 by Robert Collins
Correct buggy test.
44
        self.format = format
4634.144.9 by Martin Pool
Suppress user warnings about cross-format fetch during upgrade
45
        # XXX: Change to cleanup
46
        warning_id = 'cross_format_fetch'
4634.144.11 by Martin Pool
Rename squelched to suppressed
47
        saved_warning = warning_id in ui.ui_factory.suppressed_warnings
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
48
        if url is None and control_dir is None:
49
            raise AssertionError(
50
                "either the url or control_dir parameter must be set.")
51
        if control_dir is not None:
52
            self.bzrdir = control_dir
53
        else:
54
            self.bzrdir = BzrDir.open_unsupported(url)
3577.4.1 by Andrew Bennetts
Make 'bzr upgrade' work over HPSS.
55
        if isinstance(self.bzrdir, RemoteBzrDir):
56
            self.bzrdir._ensure_real()
57
            self.bzrdir = self.bzrdir._real_bzrdir
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
58
        if self.bzrdir.root_transport.is_readonly():
59
            raise errors.UpgradeReadonly
60
        self.transport = self.bzrdir.root_transport
4634.144.11 by Martin Pool
Rename squelched to suppressed
61
        ui.ui_factory.suppressed_warnings.add(warning_id)
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
62
        try:
63
            self.convert()
64
        finally:
4634.144.9 by Martin Pool
Suppress user warnings about cross-format fetch during upgrade
65
            if not saved_warning:
4634.144.11 by Martin Pool
Rename squelched to suppressed
66
                ui.ui_factory.suppressed_warnings.remove(warning_id)
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
67
68
    def convert(self):
1558.7.2 by Aaron Bentley
Upgrade works in repositories
69
        try:
70
            branch = self.bzrdir.open_branch()
5158.6.9 by Martin Pool
Simplify various code to use user_url
71
            if branch.user_url != self.bzrdir.user_url:
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
72
                ui.ui_factory.note(
73
                    'This is a checkout. The branch (%s) needs to be upgraded'
74
                    ' separately.' % (branch.user_url,))
3602.2.1 by Martin Pool
Fix and test for problem upgrading stacked branches
75
            del branch
76
        except (errors.NotBranchError, errors.IncompatibleRepositories):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
77
            # might not be a format we can open without upgrading; see e.g.
3602.2.1 by Martin Pool
Fix and test for problem upgrading stacked branches
78
            # https://bugs.launchpad.net/bzr/+bug/253891
1558.7.2 by Aaron Bentley
Upgrade works in repositories
79
            pass
4119.7.1 by Jelmer Vernooij
Make upgrade default to a rich-root-capable format if the source format uses rich roots.
80
        if self.format is None:
4170.3.5 by Jelmer Vernooij
Fix upgrade if there is no local repository present.
81
            try:
82
                rich_root = self.bzrdir.find_repository()._format.rich_root_data
83
            except errors.NoRepositoryPresent:
84
                rich_root = False # assume no rich roots
85
            if rich_root:
4119.7.1 by Jelmer Vernooij
Make upgrade default to a rich-root-capable format if the source format uses rich roots.
86
                format_name = "default-rich-root"
87
            else:
88
                format_name = "default"
89
            format = format_registry.make_bzrdir(format_name)
90
        else:
91
            format = self.format
92
        if not self.bzrdir.needs_format_conversion(format):
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
93
            raise errors.UpToDateFormat(self.bzrdir._format)
1534.5.16 by Robert Collins
Review feedback.
94
        if not self.bzrdir.can_convert_format():
3602.2.1 by Martin Pool
Fix and test for problem upgrading stacked branches
95
            raise errors.BzrError("cannot upgrade from bzrdir format %s" %
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
96
                           self.bzrdir._format)
4119.7.1 by Jelmer Vernooij
Make upgrade default to a rich-root-capable format if the source format uses rich roots.
97
        self.bzrdir.check_conversion_target(format)
4471.2.2 by Martin Pool
Deprecate ProgressTask.note
98
        ui.ui_factory.note('starting upgrade of %s' % self.transport.base)
99
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
100
        self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
4119.7.1 by Jelmer Vernooij
Make upgrade default to a rich-root-capable format if the source format uses rich roots.
101
        while self.bzrdir.needs_format_conversion(format):
102
            converter = self.bzrdir._format.get_converter(format)
4961.2.15 by Martin Pool
Remove more pb-passing
103
            self.bzrdir = converter.convert(self.bzrdir, None)
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
104
        ui.ui_factory.note('finished')
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
105
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
106
    def clean_up(self):
107
        """Clean-up after a conversion.
108
109
        This removes the backup.bzr directory.
110
        """
4360.10.12 by Ian Clatworthy
use transport.delete_tree() instead of osutils.rmtree() for clean-up
111
        transport = self.transport
112
        backup_relpath = transport.relpath(self.backup_newpath)
4360.10.27 by Matthew Fuller
Wrap a progress bar around removing the backup dir; it may take a
113
        child_pb = ui.ui_factory.nested_progress_bar()
114
        child_pb.update('Deleting backup.bzr')
115
        try:
116
            transport.delete_tree(backup_relpath)
117
        finally:
118
            child_pb.finished()
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
119
120
4360.10.30 by Matthew Fuller
The various repository formats that do packing, pack themselves as
121
def upgrade(url, format=None, clean_up=False, dry_run=False):
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
122
    """Upgrade locations to format.
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
123
 
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.
129
4360.10.28 by Matthew Fuller
Revert upgrade() to taking only a single URL. Other code attempting
130
    :param url: a URL of the locations to upgrade.
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
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
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
134
    :param dry_run: show what would happen but don't actually do any upgrades
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
135
    :return: the list of exceptions encountered
136
    """
4360.10.28 by Matthew Fuller
Revert upgrade() to taking only a single URL. Other code attempting
137
    control_dirs = [BzrDir.open_unsupported(url)]
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
138
    attempted, succeeded, exceptions = smart_upgrade(control_dirs,
4360.10.30 by Matthew Fuller
The various repository formats that do packing, pack themselves as
139
        format, clean_up=clean_up, dry_run=dry_run)
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
140
    if len(attempted) > 1:
141
        attempted_count = len(attempted)
142
        succeeded_count = len(succeeded)
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
143
        failed_count = attempted_count - succeeded_count
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
144
        ui.ui_factory.note(
145
            '\nSUMMARY: %d upgrades attempted, %d succeeded, %d failed'
146
            % (attempted_count, succeeded_count, failed_count))
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
147
    return exceptions
148
149
4360.10.30 by Matthew Fuller
The various repository formats that do packing, pack themselves as
150
def smart_upgrade(control_dirs, format, clean_up=False,
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
151
    dry_run=False):
152
    """Convert control directories to a new format intelligently.
153
154
    If the control directory is a shared repository, dependent branches
155
    are also converted provided the repository converted successfully.
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
156
    If the conversion of a branch fails, remaining branches are still tried.
157
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
158
    :param control_dirs: the BzrDirs to upgrade
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
159
    :param format: the format to convert to or None for the best default
4360.10.29 by Matthew Fuller
The param is clean_up, not clean-up.
160
    :param clean_up: if True, the backup.bzr directory is removed if the
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
161
      upgrade succeeded for a given repo/branch/tree
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
162
    :param dry_run: show what would happen but don't actually do any upgrades
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
163
    :return: attempted-control-dirs, succeeded-control-dirs, exceptions
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
164
    """
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
165
    all_attempted = []
166
    all_succeeded = []
167
    all_exceptions = []
168
    for control_dir in control_dirs:
169
        attempted, succeeded, exceptions = _smart_upgrade_one(control_dir,
4360.10.30 by Matthew Fuller
The various repository formats that do packing, pack themselves as
170
            format, clean_up=clean_up, dry_run=dry_run)
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --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
175
176
4360.10.30 by Matthew Fuller
The various repository formats that do packing, pack themselves as
177
def _smart_upgrade_one(control_dir, format, clean_up=False,
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
178
    dry_run=False):
179
    """Convert a control directory to a new format intelligently.
180
4360.10.10 by Ian Clatworthy
minor clean-ups
181
    See smart_upgrade for parameter details.
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
182
    """
4360.10.11 by Ian Clatworthy
for a repo, just do dependent branches (not lw checkouts) for now
183
    # If the URL is a shared repository, find the dependent branches
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
184
    dependents = None
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
185
    try:
186
        repo = control_dir.open_repository()
187
    except errors.NoRepositoryPresent:
188
        # A branch or checkout using a shared repository higher up
189
        pass
190
    else:
191
        # The URL is a repository. If it successfully upgrades,
4360.10.11 by Ian Clatworthy
for a repo, just do dependent branches (not lw checkouts) for now
192
        # then upgrade the dependent branches as well.
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
193
        if repo.is_shared():
4360.10.17 by Ian Clatworthy
review feedback from abentley
194
            dependents = repo.find_branches(using=True)
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
195
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
196
    # Do the conversions
4360.10.1 by Ian Clatworthy
initial cut at smarter upgrades
197
    attempted = [control_dir]
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
198
    succeeded, exceptions = _convert_items([control_dir], format, clean_up,
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
199
                                           dry_run)
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
200
    if succeeded and dependents:
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
201
        ui.ui_factory.note('Found %d dependent branches - upgrading ...'
202
                           % (len(dependents),))
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
203
        # Convert dependent branches
4360.10.11 by Ian Clatworthy
for a repo, just do dependent branches (not lw checkouts) for now
204
        branch_cdirs = [b.bzrdir for b in dependents]
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
205
        successes, problems = _convert_items(branch_cdirs, format, clean_up,
4360.10.30 by Matthew Fuller
The various repository formats that do packing, pack themselves as
206
            dry_run, label="branch")
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
207
        attempted.extend(branch_cdirs)
4360.10.3 by Ian Clatworthy
fix tests
208
        succeeded.extend(successes)
209
        exceptions.extend(problems)
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
210
211
    # Return the result
212
    return attempted, succeeded, exceptions
213
4360.10.49 by Vincent Ladeuil
Make _get_object_and_label() private to upgrade.py, its inmplementation is both incomplete and returning info that are not used.
214
# FIXME: There are several problems below:
215
# - RemoteRepository doesn't support _unsupported (really ?)
216
# - raising AssertionError is rude and may not be necessary
217
# - no tests
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.
221
222
    :return: object, label where
223
      object is a Branch, Repository or WorkingTree and
224
      label is one of:
225
        branch            - a branch
226
        repository        - a repository
227
        tree              - a lightweight checkout
228
    """
229
    try:
230
        try:
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:
237
        pass
238
    else:
239
        return br, "branch"
240
    try:
241
        repo = control_dir.open_repository()
242
    except errors.NoRepositoryPresent:
243
        pass
244
    else:
245
        return repo, "repository"
246
    try:
247
        wt = control_dir.open_workingtree()
248
    except (errors.NoWorkingTree, errors.NotLocalUrl):
249
        pass
250
    else:
251
        return wt, "tree"
252
    raise AssertionError("unknown type of control directory %s", control_dir)
253
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
254
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
255
def _convert_items(items, format, clean_up, dry_run, label=None):
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
256
    """Convert a sequence of control directories to the given format.
257
 
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
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
262
    :param dry_run: show what would happen but don't actually do any upgrades
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
263
    :param label: the label for these items or None to calculate one
264
    :return: items successfully upgraded, exceptions
265
    """
266
    succeeded = []
267
    exceptions = []
4360.10.31 by Matthew Fuller
Wrap a progress bar around the iteration of entries to upgrade.
268
    child_pb = ui.ui_factory.nested_progress_bar()
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
269
    child_pb.update('Upgrading bzrdirs', 0, len(items))
270
    for i, control_dir in enumerate(items):
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
271
        # Do the conversion
4360.10.12 by Ian Clatworthy
use transport.delete_tree() instead of osutils.rmtree() for clean-up
272
        location = control_dir.root_transport.base
4360.10.49 by Vincent Ladeuil
Make _get_object_and_label() private to upgrade.py, its inmplementation is both incomplete and returning info that are not used.
273
        bzr_object, bzr_label = _get_object_and_label(control_dir)
4360.10.31 by Matthew Fuller
Wrap a progress bar around the iteration of entries to upgrade.
274
        type_label = label or bzr_label
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
275
        child_pb.update("Upgrading %s" % (type_label), i+1, len(items))
276
        ui.ui_factory.note('Upgrading %s %s ...' % (type_label, location,))
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
277
        try:
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
278
            if not dry_run:
279
                cv = Convert(control_dir=control_dir, format=format)
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
280
        except Exception, ex:
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
281
            trace.warning('conversion error: %s' % ex)
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
282
            exceptions.append(ex)
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
283
            continue
284
285
        # Do any required post processing
286
        succeeded.append(control_dir)
287
        if clean_up:
288
            try:
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
289
                ui.ui_factory.note('Removing backup ...')
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
290
                if not dry_run:
291
                    cv.clean_up()
292
            except Exception, ex:
4360.10.46 by Vincent Ladeuil
Get rid of _verbose_warning and rework the messages overall display (revealing some incoherences) and fixing the tests accordingly.
293
                trace.warning('failed to clean-up %s: %s' % (location, ex))
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
294
                exceptions.append(ex)
295
4360.10.31 by Matthew Fuller
Wrap a progress bar around the iteration of entries to upgrade.
296
    child_pb.finished()
297
4360.10.4 by Ian Clatworthy
support multiple urls, --pack and --dry-run
298
    # Return the result
4360.10.2 by Ian Clatworthy
drop stacked vs unstacked logic; support multiple URLs
299
    return succeeded, exceptions