~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-03-25 00:02:51 UTC
  • mfrom: (5106.1.1 version-bump)
  • Revision ID: pqm@pqm.ubuntu.com-20100325000251-bwsv5c5d3l9x3lnn
(Jelmer) Bump API version for 2.2.0.

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
18
17
 
19
18
from cStringIO import StringIO
20
19
import shutil
35
34
    ui,
36
35
    workingtree,
37
36
)
38
 
from bzrlib.i18n import gettext
 
37
 
39
38
 
40
39
class UseEditor(Exception):
41
40
    """Use an editor instead of selecting hunks."""
43
42
 
44
43
class ShelfReporter(object):
45
44
 
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"?')
 
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"?'
57
56
             }
58
57
 
59
58
    invert_diff = False
67
66
 
68
67
    def shelved_id(self, shelf_id):
69
68
        """Report the id changes were shelved to."""
70
 
        trace.note(gettext('Changes shelved with id "%d".') % shelf_id)
 
69
        trace.note('Changes shelved with id "%d".' % shelf_id)
71
70
 
72
71
    def changes_destroyed(self):
73
72
        """Report that changes were made without shelving."""
74
 
        trace.note(gettext('Selected changes destroyed.'))
 
73
        trace.note('Selected changes destroyed.')
75
74
 
76
75
    def selected_changes(self, transform):
77
76
        """Report the changes that were selected."""
78
 
        trace.note(gettext("Selected changes:"))
 
77
        trace.note("Selected changes:")
79
78
        changes = transform.iter_changes()
80
79
        delta.report_changes(changes, self.delta_reporter)
81
80
 
95
94
 
96
95
class ApplyReporter(ShelfReporter):
97
96
 
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"?'),
 
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"?',
108
107
             }
109
108
 
110
109
    invert_diff = True
155
154
 
156
155
    @classmethod
157
156
    def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
158
 
                  message=None, directory=None, destroy=False):
 
157
                  message=None, directory='.', destroy=False):
159
158
        """Create a shelver from commandline arguments.
160
159
 
161
160
        The returned shelver wil have a work_tree that is locked and should
169
168
        :param destroy: Change the working tree without storing the shelved
170
169
            changes.
171
170
        """
172
 
        if directory is None:
173
 
            directory = u'.'
174
 
        elif file_list:
175
 
            file_list = [osutils.pathjoin(directory, f) for f in file_list]
176
171
        tree, path = workingtree.WorkingTree.open_containing(directory)
177
172
        # Ensure that tree is locked for the lifetime of target_tree, as
178
173
        # target tree may be reading from the same dirstate.
180
175
        try:
181
176
            target_tree = builtins._get_one_revision_tree('shelf2', revision,
182
177
                tree.branch, tree)
183
 
            files = tree.safe_relpath_files(file_list)
 
178
            files = builtins.safe_relpath_files(tree, file_list)
184
179
            return klass(tree, target_tree, diff_writer, all, all, files,
185
180
                         message, destroy)
186
181
        finally:
246
241
            new_tree = self.work_tree
247
242
        old_path = old_tree.id2path(file_id)
248
243
        new_path = new_tree.id2path(file_id)
249
 
        text_differ = diff.DiffText(old_tree, new_tree, diff_file,
250
 
            path_encoding=osutils.get_terminal_encoding())
 
244
        text_differ = diff.DiffText(old_tree, new_tree, diff_file)
251
245
        patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
252
246
        diff_file.seek(0)
253
247
        return patches.parse_patch(diff_file)
254
248
 
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):
 
249
    def prompt(self, message):
 
250
        """Prompt the user for a character.
 
251
 
 
252
        :param message: The message to prompt a user with.
 
253
        :return: A character.
 
254
        """
 
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
 
258
            # https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
 
259
            # for more context.
 
260
            raise errors.BzrError("You need a controlling terminal.")
 
261
        sys.stdout.write(message)
 
262
        char = osutils.getchar()
 
263
        sys.stdout.write("\r" + ' ' * len(message) + '\r')
 
264
        sys.stdout.flush()
 
265
        return char
 
266
 
 
267
    def prompt_bool(self, question, long=False, allow_editor=False):
259
268
        """Prompt the user with a yes/no question.
260
269
 
261
270
        This may be overridden by self.auto.  It may also *set* self.auto.  It
265
274
        """
266
275
        if self.auto:
267
276
            return True
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'
 
277
        editor_string = ''
 
278
        if long:
 
279
            if allow_editor:
 
280
                editor_string = '(E)dit manually, '
 
281
            prompt = ' [(y)es, (N)o, %s(f)inish, or (q)uit]' % editor_string
279
282
        else:
280
 
            char = alternatives_chars[choice]
 
283
            if allow_editor:
 
284
                editor_string = 'e'
 
285
            prompt = ' [yN%sfq?]' % editor_string
 
286
        char = self.prompt(question + prompt)
281
287
        if char == 'y':
282
288
            return True
283
289
        elif char == 'e' and allow_editor:
285
291
        elif char == 'f':
286
292
            self.auto = True
287
293
            return True
 
294
        elif char == '?':
 
295
            return self.prompt_bool(question, long=True)
288
296
        if char == 'q':
289
297
            raise errors.UserAbort()
290
298
        else:
399
407
            else:
400
408
                shelf_id = manager.last_shelf()
401
409
                if shelf_id is None:
402
 
                    raise errors.BzrCommandError(gettext('No changes are shelved.'))
 
410
                    raise errors.BzrCommandError('No changes are shelved.')
403
411
            apply_changes = True
404
412
            delete_shelf = True
405
413
            read_shelf = True
457
465
        cleanups = [self.tree.unlock]
458
466
        try:
459
467
            if self.read_shelf:
460
 
                trace.note(gettext('Using changes with id "%d".') % self.shelf_id)
 
468
                trace.note('Using changes with id "%d".' % self.shelf_id)
461
469
                unshelver = self.manager.get_unshelver(self.shelf_id)
462
470
                cleanups.append(unshelver.finalize)
463
471
                if unshelver.message is not None:
464
 
                    trace.note(gettext('Message: %s') % unshelver.message)
 
472
                    trace.note('Message: %s' % unshelver.message)
465
473
                change_reporter = delta._ChangeReporter()
466
474
                merger = unshelver.make_merger(None)
467
475
                merger.change_reporter = change_reporter
473
481
                    self.show_changes(merger)
474
482
            if self.delete_shelf:
475
483
                self.manager.delete_shelf(self.shelf_id)
476
 
                trace.note(gettext('Deleted changes with id "%d".') % self.shelf_id)
 
484
                trace.note('Deleted changes with id "%d".' % self.shelf_id)
477
485
        finally:
478
486
            for cleanup in reversed(cleanups):
479
487
                cleanup()
484
492
        tt = tree_merger.make_preview_transform()
485
493
        new_tree = tt.get_preview_tree()
486
494
        if self.write_diff_to is None:
487
 
            self.write_diff_to = ui.ui_factory.make_output_stream(encoding_type='exact')
488
 
        path_encoding = osutils.get_diff_header_encoding()
489
 
        diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to,
490
 
            path_encoding=path_encoding)
 
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)
491
497
        tt.finalize()
492
498
 
493
499
    def show_changes(self, merger):