~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Martin Packman
  • Date: 2011-10-06 16:41:45 UTC
  • mfrom: (6015.33.10 2.4)
  • mto: This revision was merged to the branch mainline in revision 6202.
  • Revision ID: martin.packman@canonical.com-20111006164145-o98oqn32440extgt
Merge 2.4 into dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
from cStringIO import StringIO
 
19
import os
19
20
import shutil
20
21
import sys
21
22
import tempfile
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
154
155
 
155
156
    @classmethod
156
157
    def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
157
 
                  message=None, directory='.', destroy=False):
 
158
                  message=None, directory=None, destroy=False):
158
159
        """Create a shelver from commandline arguments.
159
160
 
160
161
        The returned shelver wil have a work_tree that is locked and should
168
169
        :param destroy: Change the working tree without storing the shelved
169
170
            changes.
170
171
        """
 
172
        if directory is None:
 
173
            directory = u'.'
 
174
        elif file_list:
 
175
            file_list = [osutils.pathjoin(directory, f) for f in file_list]
171
176
        tree, path = workingtree.WorkingTree.open_containing(directory)
172
177
        # Ensure that tree is locked for the lifetime of target_tree, as
173
178
        # target tree may be reading from the same dirstate.
175
180
        try:
176
181
            target_tree = builtins._get_one_revision_tree('shelf2', revision,
177
182
                tree.branch, tree)
178
 
            files = builtins.safe_relpath_files(tree, file_list)
 
183
            files = tree.safe_relpath_files(file_list)
179
184
            return klass(tree, target_tree, diff_writer, all, all, files,
180
185
                         message, destroy)
181
186
        finally:
241
246
            new_tree = self.work_tree
242
247
        old_path = old_tree.id2path(file_id)
243
248
        new_path = new_tree.id2path(file_id)
244
 
        text_differ = diff.DiffText(old_tree, new_tree, diff_file)
 
249
        text_differ = diff.DiffText(old_tree, new_tree, diff_file,
 
250
            path_encoding=osutils.get_terminal_encoding())
245
251
        patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
246
252
        diff_file.seek(0)
247
253
        return patches.parse_patch(diff_file)
248
254
 
 
255
    def _char_based(self):
 
256
        # FIXME: A bit hackish to use INSIDE_EMACS here, but there is another
 
257
        # work in progress moving this method (and more importantly prompt()
 
258
        # below) into the ui area and address the issue in better ways.
 
259
        # -- vila 2011-09-28
 
260
        return os.environ.get('INSIDE_EMACS', None) is None
 
261
 
249
262
    def prompt(self, message):
250
263
        """Prompt the user for a character.
251
264
 
252
265
        :param message: The message to prompt a user with.
253
266
        :return: A character.
254
267
        """
255
 
        if not sys.stdin.isatty():
256
 
            # Since there is no controlling terminal we will hang when trying
257
 
            # to prompt the user, better abort now.  See
 
268
        char_based = self._char_based()
 
269
        if char_based and not sys.stdin.isatty():
 
270
            # Since there is no controlling terminal we will hang when
 
271
            # trying to prompt the user, better abort now.  See
258
272
            # https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
259
273
            # for more context.
260
 
            raise errors.BzrError("You need a controlling terminal.")
 
274
            raise errors.BzrError(gettext("You need a controlling terminal."))
261
275
        sys.stdout.write(message)
262
 
        char = osutils.getchar()
 
276
        if char_based:
 
277
            # We peek one char at a time which requires a real term here
 
278
            char = osutils.getchar()
 
279
        else:
 
280
            # While running tests (or under emacs) the input is line buffered
 
281
            # so we must not use osutils.getchar(). Instead we switch to a mode
 
282
            # where each line is terminated by a new line
 
283
            line = sys.stdin.readline()
 
284
            if line:
 
285
                # XXX: Warn if more than one char is typed ?
 
286
                char = line[0]
 
287
            else:
 
288
                # Empty input, callers handle it as enter
 
289
                char = ''
263
290
        sys.stdout.write("\r" + ' ' * len(message) + '\r')
264
291
        sys.stdout.flush()
265
292
        return char
407
434
            else:
408
435
                shelf_id = manager.last_shelf()
409
436
                if shelf_id is None:
410
 
                    raise errors.BzrCommandError('No changes are shelved.')
 
437
                    raise errors.BzrCommandError(gettext('No changes are shelved.'))
411
438
            apply_changes = True
412
439
            delete_shelf = True
413
440
            read_shelf = True
465
492
        cleanups = [self.tree.unlock]
466
493
        try:
467
494
            if self.read_shelf:
468
 
                trace.note('Using changes with id "%d".' % self.shelf_id)
 
495
                trace.note(gettext('Using changes with id "%d".') % self.shelf_id)
469
496
                unshelver = self.manager.get_unshelver(self.shelf_id)
470
497
                cleanups.append(unshelver.finalize)
471
498
                if unshelver.message is not None:
472
 
                    trace.note('Message: %s' % unshelver.message)
 
499
                    trace.note(gettext('Message: %s') % unshelver.message)
473
500
                change_reporter = delta._ChangeReporter()
474
501
                merger = unshelver.make_merger(None)
475
502
                merger.change_reporter = change_reporter
481
508
                    self.show_changes(merger)
482
509
            if self.delete_shelf:
483
510
                self.manager.delete_shelf(self.shelf_id)
484
 
                trace.note('Deleted changes with id "%d".' % self.shelf_id)
 
511
                trace.note(gettext('Deleted changes with id "%d".') % self.shelf_id)
485
512
        finally:
486
513
            for cleanup in reversed(cleanups):
487
514
                cleanup()
492
519
        tt = tree_merger.make_preview_transform()
493
520
        new_tree = tt.get_preview_tree()
494
521
        if self.write_diff_to is None:
495
 
            self.write_diff_to = ui.ui_factory.make_output_stream()
496
 
        diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to)
 
522
            self.write_diff_to = ui.ui_factory.make_output_stream(encoding_type='exact')
 
523
        path_encoding = osutils.get_diff_header_encoding()
 
524
        diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to,
 
525
            path_encoding=path_encoding)
497
526
        tt.finalize()
498
527
 
499
528
    def show_changes(self, merger):