512
  7   ˆl–Ým_JJi?u8 Ap"¸ÈTÅ£‘äÇò 8à$n†6ÊG¿ŸÂƒh¹ÁÀ¿      1
      ˆ¾‘äÇò 8à$n†6ÊG¿ŸÂƒh¹ÁÀ¿      0000004c
  B   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þY	_Y¹DGÃ Â�ÏÃ„�ußÂ_ŠI|¥�+Wé3ž¯ÂÁ 1é    # Copyright (C) 2005, 2006, 2008-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""brz upgrade logic."""

from . import (
    errors,
    trace,
    ui,
    urlutils,
    )
from .controldir import (
    ControlDir,
    format_registry,
    )
from .i18n import gettext
from .bzr.remote import RemoteBzrDir


class Convert:

    def __init__(self, url=None, format=None, control_dir=None):
        """Convert a Bazaar control directory to a given format.

        Either the url or control_dir parameter must be given.

        :param url: the URL of the control directory or None if the
          control_dir is explicitly given instead
        :param format: the format to convert to or None for the default
        :param control_dir: the control directory or None if it is
          specified via the URL parameter instead
        """
        self.format = format
        # XXX: Change to cleanup
        warning_id = 'cross_format_fetch'
        saved_warning = warning_id in ui.ui_factory.suppressed_warnings
        if url is None and control_dir is None:
            raise AssertionError(
                "either the url or control_dir parameter must be set.")
        if control_dir is not None:
            self.controldir = control_dir
        else:
            self.controldir = ControlDir.open_unsupported(url)
        if isinstance(self.controldir, RemoteBzrDir):
            self.controldir._ensure_real()
            self.controldir = self.controldir._real_bzrdir
        if self.controldir.root_transport.is_readonly():
            raise errors.UpgradeReadonly
        self.transport = self.controldir.root_transport
        ui.ui_factory.suppressed_warnings.add(warning_id)
        try:
            self.convert()
        finally:
            if not saved_warning:
                ui.ui_factory.suppressed_warnings.remove(warning_id)

    def convert(self):
        try:
            branch = self.controldir.open_branch()
            if branch.user_url != self.controldir.user_url:
                ui.ui_factory.note(gettext(
                    'This is a checkout. The branch (%s) needs to be upgraded'
                    ' separately.') % (urlutils.unescape_for_display(
                        branch.user_url, 'utf-8')))
            del branch
        except (errors.NotBranchError, errors.IncompatibleRepositories):
            # might not be a format we can open without upgrading; see e.g.
            # https://bugs.launchpad.net/bzr/+bug/253891
            pass
        if self.format is None:
            try:
                rich_root = self.controldir.find_repository()._format.rich_root_data
            except errors.NoRepositoryPresent:
                rich_root = False  # assume no rich roots
            if rich_root:
                format_name = "default-rich-root"
            else:
                format_name = "default"
            format = format_registry.make_controldir(format_name)
        else:
            format = self.format
        if not self.controldir.needs_format_conversion(format):
            raise errors.UpToDateFormat(self.controldir._format)
        if not self.controldir.can_convert_format():
            raise errors.BzrError(gette