online
  B   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þY+ Y¹DGÃ Â�ÏÂ„oˆÁ_ŠI|¥�+Wé3ž¯ÁÀ >0    # Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
#
# 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

"""Propose command implementations."""

from io import StringIO

from ... import (
    branch as _mod_branch,
    controldir,
    errors,
    log as _mod_log,
    missing as _mod_missing,
    msgeditor,
    urlutils,
    )
from ...i18n import gettext
from ...commands import Command
from ...option import (
    ListOption,
    Option,
    RegistryOption,
    )
from ...trace import note, warning
from ... import (
    forge as _mod_forge,
    )


def branch_name(branch):
    if branch.name:
        return branch.name
    return urlutils.basename(branch.user_url)


def _check_already_merged(branch, target):
    # TODO(jelmer): Check entire ancestry rather than just last revision?
    if branch.last_revision() == target.last_revision():
        raise errors.CommandError(gettext(
            'All local changes are already present in target.'))


class cmd_publish_derived(Command):
    __doc__ = """Publish a derived branch.

    Try to create a public copy of a local branch on a hosting site,
    derived from the specified base branch.

    Reasonable defaults are picked for owner name, branch name and project
    name, but they can also be overridden from the command-line.
    """

    takes_options = [
        'directory',
        Option('owner', help='Owner of the new remote branch.', type=str),
        Option('project', help='Project name for the new remote branch.',
               type=str),
        Option('name', help='Name of the new remote branch.', type=str),
        Option('no-allow-lossy',
               help='Allow fallback to lossy push, if necessary.'),
        Option('overwrite', help="Overwrite existing commits."),
        ]
    takes_args = ['submit_branch?']

    def run(self, submit_branch=None, owner=None, name=None, project=None,
            no_allow_lossy=False, overwrite=False, directory='.'):
        local_branch = _mod_branch.Branch.open_containing(directory)[0]
        self.add_cleanup(local_branch.lock_write().unlock)
        if submit_branch is None:
            submit_branch = local_branch.get_submit_branch()
            note(gettext('Using submit branch %s') % submit_branch)
        if submit_branch is None:
            submit_branch = local_branch.get_parent()
            note(gettext('Using parent branch %s') % submit_branch)
        submit_branch = _mod_branch.Branch.open(submit_branch)
        _check_already_merged(local_branch, submit_branch)
        if name is None:
            name = branch_name(local_branch)
        forge = _mod_forge.get_forge(submit_branch)
        remote_branch, public_url = forge.publish_derived(
            local_branch, submit_branch, name=name, project=project,
            owner=owner, allow_lossy=not no_allow_lossy,
            overwrite=overwrite)
        local_branch.set_push_location(remote_branch.user_url)
        local_branch.set_public_branch(public_url)
        local_branch.set_submit_branch(submit_branch.user_url)
        note(gettext("Pushed to %s") % public_url)


def summarize_unmerged(local_branch, remote_branch, target,
                       prerequisite_branch=None):
    """Generate a text description of the unmerged revisions in branch.

    :param branch: The proposed branch
    :param target: Target branch
    :p