~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2012-02-20 12:19:29 UTC
  • mfrom: (6437.23.11 2.5)
  • mto: (6581.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6582.
  • Revision ID: jelmer@samba.org-20120220121929-7ni2psvjoatm1yp4
Merge bzr/2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
17
18
 
18
19
from cStringIO import StringIO
19
20
import shutil
34
35
    ui,
35
36
    workingtree,
36
37
)
37
 
 
 
38
from bzrlib.i18n import gettext
38
39
 
39
40
class UseEditor(Exception):
40
41
    """Use an editor instead of selecting hunks."""
42
43
 
43
44
class ShelfReporter(object):
44
45
 
45
 
    vocab = {'add file': 'Shelve adding file "%(path)s"?',
46
 
             'binary': 'Shelve binary changes?',
47
 
             'change kind': 'Shelve changing "%s" from %(other)s'
48
 
             ' to %(this)s?',
49
 
             'delete file': 'Shelve removing file "%(path)s"?',
50
 
             'final': 'Shelve %d change(s)?',
51
 
             'hunk': 'Shelve?',
52
 
             'modify target': 'Shelve changing target of'
53
 
             ' "%(path)s" from "%(other)s" to "%(this)s"?',
54
 
             'rename': 'Shelve renaming "%(other)s" =>'
55
 
                        ' "%(this)s"?'
 
46
    vocab = {'add file': gettext('Shelve adding file "%(path)s"?'),
 
47
             'binary': gettext('Shelve binary changes?'),
 
48
             'change kind': gettext('Shelve changing "%s" from %(other)s'
 
49
             ' to %(this)s?'),
 
50
             'delete file': gettext('Shelve removing file "%(path)s"?'),
 
51
             'final': gettext('Shelve %d change(s)?'),
 
52
             'hunk': gettext('Shelve?'),
 
53
             'modify target': gettext('Shelve changing target of'
 
54
             ' "%(path)s" from "%(other)s" to "%(this)s"?'),
 
55
             'rename': gettext('Shelve renaming "%(other)s" =>'
 
56
                        ' "%(this)s"?')
56
57
             }
57
58
 
58
59
    invert_diff = False
66
67
 
67
68
    def shelved_id(self, shelf_id):
68
69
        """Report the id changes were shelved to."""
69
 
        trace.note('Changes shelved with id "%d".' % shelf_id)
 
70
        trace.note(gettext('Changes shelved with id "%d".') % shelf_id)
70
71
 
71
72
    def changes_destroyed(self):
72
73
        """Report that changes were made without shelving."""
73
 
        trace.note('Selected changes destroyed.')
 
74
        trace.note(gettext('Selected changes destroyed.'))
74
75
 
75
76
    def selected_changes(self, transform):
76
77
        """Report the changes that were selected."""
77
 
        trace.note("Selected changes:")
 
78
        trace.note(gettext("Selected changes:"))
78
79
        changes = transform.iter_changes()
79
80
        delta.report_changes(changes, self.delta_reporter)
80
81
 
94
95
 
95
96
class ApplyReporter(ShelfReporter):
96
97
 
97
 
    vocab = {'add file': 'Delete file "%(path)s"?',
98
 
             'binary': 'Apply binary changes?',
99
 
             'change kind': 'Change "%(path)s" from %(this)s'
100
 
             ' to %(other)s?',
101
 
             'delete file': 'Add file "%(path)s"?',
102
 
             'final': 'Apply %d change(s)?',
103
 
             'hunk': 'Apply change?',
104
 
             'modify target': 'Change target of'
105
 
             ' "%(path)s" from "%(this)s" to "%(other)s"?',
106
 
             'rename': 'Rename "%(this)s" => "%(other)s"?',
 
98
    vocab = {'add file': gettext('Delete file "%(path)s"?'),
 
99
             'binary': gettext('Apply binary changes?'),
 
100
             'change kind': gettext('Change "%(path)s" from %(this)s'
 
101
             ' to %(other)s?'),
 
102
             'delete file': gettext('Add file "%(path)s"?'),
 
103
             'final': gettext('Apply %d change(s)?'),
 
104
             'hunk': gettext('Apply change?'),
 
105
             'modify target': gettext('Change target of'
 
106
             ' "%(path)s" from "%(this)s" to "%(other)s"?'),
 
107
             'rename': gettext('Rename "%(this)s" => "%(other)s"?'),
107
108
             }
108
109
 
109
110
    invert_diff = True
251
252
        diff_file.seek(0)
252
253
        return patches.parse_patch(diff_file)
253
254
 
254
 
    def prompt(self, message):
255
 
        """Prompt the user for a character.
256
 
 
257
 
        :param message: The message to prompt a user with.
258
 
        :return: A character.
259
 
        """
260
 
        if not sys.stdin.isatty():
261
 
            # Since there is no controlling terminal we will hang when trying
262
 
            # to prompt the user, better abort now.  See
263
 
            # https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
264
 
            # for more context.
265
 
            raise errors.BzrError("You need a controlling terminal.")
266
 
        sys.stdout.write(message)
267
 
        char = osutils.getchar()
268
 
        sys.stdout.write("\r" + ' ' * len(message) + '\r')
269
 
        sys.stdout.flush()
270
 
        return char
271
 
 
272
 
    def prompt_bool(self, question, long=False, allow_editor=False):
 
255
    def prompt(self, message, choices, default):
 
256
        return ui.ui_factory.choose(message, choices, default=default)
 
257
 
 
258
    def prompt_bool(self, question, allow_editor=False):
273
259
        """Prompt the user with a yes/no question.
274
260
 
275
261
        This may be overridden by self.auto.  It may also *set* self.auto.  It
279
265
        """
280
266
        if self.auto:
281
267
            return True
282
 
        editor_string = ''
283
 
        if long:
284
 
            if allow_editor:
285
 
                editor_string = '(E)dit manually, '
286
 
            prompt = ' [(y)es, (N)o, %s(f)inish, or (q)uit]' % editor_string
 
268
        alternatives_chars = 'yn'
 
269
        alternatives = '&yes\n&No'
 
270
        if allow_editor:
 
271
            alternatives_chars += 'e'
 
272
            alternatives += '\n&edit manually'
 
273
        alternatives_chars += 'fq'
 
274
        alternatives += '\n&finish\n&quit'
 
275
        choice = self.prompt(question, alternatives, 1)
 
276
        if choice is None:
 
277
            # EOF.
 
278
            char = 'n'
287
279
        else:
288
 
            if allow_editor:
289
 
                editor_string = 'e'
290
 
            prompt = ' [yN%sfq?]' % editor_string
291
 
        char = self.prompt(question + prompt)
 
280
            char = alternatives_chars[choice]
292
281
        if char == 'y':
293
282
            return True
294
283
        elif char == 'e' and allow_editor:
296
285
        elif char == 'f':
297
286
            self.auto = True
298
287
            return True
299
 
        elif char == '?':
300
 
            return self.prompt_bool(question, long=True)
301
288
        if char == 'q':
302
289
            raise errors.UserAbort()
303
290
        else:
412
399
            else:
413
400
                shelf_id = manager.last_shelf()
414
401
                if shelf_id is None:
415
 
                    raise errors.BzrCommandError('No changes are shelved.')
 
402
                    raise errors.BzrCommandError(gettext('No changes are shelved.'))
416
403
            apply_changes = True
417
404
            delete_shelf = True
418
405
            read_shelf = True
470
457
        cleanups = [self.tree.unlock]
471
458
        try:
472
459
            if self.read_shelf:
473
 
                trace.note('Using changes with id "%d".' % self.shelf_id)
 
460
                trace.note(gettext('Using changes with id "%d".') % self.shelf_id)
474
461
                unshelver = self.manager.get_unshelver(self.shelf_id)
475
462
                cleanups.append(unshelver.finalize)
476
463
                if unshelver.message is not None:
477
 
                    trace.note('Message: %s' % unshelver.message)
 
464
                    trace.note(gettext('Message: %s') % unshelver.message)
478
465
                change_reporter = delta._ChangeReporter()
479
466
                merger = unshelver.make_merger(None)
480
467
                merger.change_reporter = change_reporter
486
473
                    self.show_changes(merger)
487
474
            if self.delete_shelf:
488
475
                self.manager.delete_shelf(self.shelf_id)
489
 
                trace.note('Deleted changes with id "%d".' % self.shelf_id)
 
476
                trace.note(gettext('Deleted changes with id "%d".') % self.shelf_id)
490
477
        finally:
491
478
            for cleanup in reversed(cleanups):
492
479
                cleanup()