       0        0
  B   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þZ”R,Ü¢#á�a	HçÃ„ãÿÂ_ŠI|¥�+Wé3ž¯ÂÁ ?÷     # Copyright (C) 2008, 2009, 2010 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

import contextlib
import patiencediff
import shutil
import sys
import tempfile

from io import BytesIO

from . import (
    builtins,
    delta,
    diff,
    errors,
    osutils,
    patches,
    shelf,
    textfile,
    trace,
    ui,
    workingtree,
)
from .i18n import gettext


class UseEditor(Exception):
    """Use an editor instead of selecting hunks."""


class ShelfReporter:

    vocab = {'add file': gettext('Shelve adding file "%(path)s"?'),
             'binary': gettext('Shelve binary changes?'),
             'change kind': gettext('Shelve changing "%s" from %(other)s'
                                    ' to %(this)s?'),
             'delete file': gettext('Shelve removing file "%(path)s"?'),
             'final': gettext('Shelve %d change(s)?'),
             'hunk': gettext('Shelve?'),
             'modify target': gettext('Shelve changing target of'
                                      ' "%(path)s" from "%(other)s" to "%(this)s"?'),
             'rename': gettext('Shelve renaming "%(other)s" =>'
                               ' "%(this)s"?')
             }

    invert_diff = False

    def __init__(self):
        self.delta_reporter = delta._ChangeReporter()

    def no_changes(self):
        """Report that no changes were selected to apply."""
        trace.warning('No changes to shelve.')

    def shelved_id(self, shelf_id):
        """Report the id changes were shelved to."""
        trace.note(gettext('Changes shelved with id "%d".') % shelf_id)

    def changes_destroyed(self):
        """Report that changes were made without shelving."""
        trace.note(gettext('Selected changes destroyed.'))

    def selected_changes(self, transform):
        """Report the changes that were selected."""
        trace.note(gettext("Selected changes:"))
        changes = transform.iter_changes()
        delta.report_changes(changes, self.delta_reporter)

    def prompt_change(self, change):
        """Determine the prompt for a change to apply."""
        if change[0] == 'rename':
            vals = {'this': change[3], 'other': change[2]}
        elif change[0] == 'change kind':
            vals = {'path': change[4], 'other': change[2], 'this': change[3]}
        elif change[0] == 'modify target':
            vals = {'path': change[2], 'other': change[3], 'this': change[4]}
        else:
            vals = {'path': change[3]}
        prompt = self.vocab[change[0]] % vals
        return prompt


class ApplyReporter(ShelfReporter):

    vocab = {'add file': gettext('Delete file "%(path)s"?'),
             'binary': gettext('Apply binary changes?'),
             'change kind': gettext('Change "%(path)s" from %(this)s'
                                    ' to %(other)s?'),
             'delete file': gettext('Add file "%(path)s"?'),
             'final': gettext('Apply %d change(s)?'),
             'hunk': gettext('Apply change?'),
             'modify target': gettext('Change target of'
                                      ' "%(path)s" from "%(this)s" to "%(other)s"?'),
             'rename': gettext('Rename "%(this)s" => "%(other)s"?'),
             }

    invert_diff = True

    def changes_destroyed(self):
        pass


class Shelver:
    """Interactively shelve the changes