~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-22 14:12:18 UTC
  • mfrom: (6155.3.1 jam)
  • Revision ID: pqm@pqm.ubuntu.com-20110922141218-86s4uu6nqvourw4f
(jameinel) Cleanup comments bzrlib/smart/__init__.py (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
 
23
23
from bzrlib import (
24
24
    builtins,
25
 
    commands,
26
25
    delta,
27
26
    diff,
28
27
    errors,
35
34
    ui,
36
35
    workingtree,
37
36
)
38
 
 
 
37
from bzrlib.i18n import gettext
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': 'Shelve adding file "%(path)s"?',
47
 
             'binary': 'Shelve binary changes?',
48
 
             'change kind': 'Shelve changing "%s" from %(other)s'
49
 
             ' to %(this)s?',
50
 
             'delete file': 'Shelve removing file "%(path)s"?',
51
 
             'final': 'Shelve %d change(s)?',
52
 
             'hunk': 'Shelve?',
53
 
             'modify target': 'Shelve changing target of'
54
 
             ' "%(path)s" from "%(other)s" to "%(this)s"?',
55
 
             'rename': 'Shelve renaming "%(other)s" =>'
56
 
                        ' "%(this)s"?'
 
45
    vocab = {'add file': gettext('Shelve adding file "%(path)s"?'),
 
46
             'binary': gettext('Shelve binary changes?'),
 
47
             'change kind': gettext('Shelve changing "%s" from %(other)s'
 
48
             ' to %(this)s?'),
 
49
             'delete file': gettext('Shelve removing file "%(path)s"?'),
 
50
             'final': gettext('Shelve %d change(s)?'),
 
51
             'hunk': gettext('Shelve?'),
 
52
             'modify target': gettext('Shelve changing target of'
 
53
             ' "%(path)s" from "%(other)s" to "%(this)s"?'),
 
54
             'rename': gettext('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('Changes shelved with id "%d".' % shelf_id)
 
69
        trace.note(gettext('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('Selected changes destroyed.')
 
73
        trace.note(gettext('Selected changes destroyed.'))
75
74
 
76
75
    def selected_changes(self, transform):
77
76
        """Report the changes that were selected."""
78
 
        trace.note("Selected changes:")
 
77
        trace.note(gettext("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': 'Delete file "%(path)s"?',
99
 
             'binary': 'Apply binary changes?',
100
 
             'change kind': 'Change "%(path)s" from %(this)s'
101
 
             ' to %(other)s?',
102
 
             'delete file': 'Add file "%(path)s"?',
103
 
             'final': 'Apply %d change(s)?',
104
 
             'hunk': 'Apply change?',
105
 
             'modify target': 'Change target of'
106
 
             ' "%(path)s" from "%(this)s" to "%(other)s"?',
107
 
             'rename': 'Rename "%(this)s" => "%(other)s"?',
 
97
    vocab = {'add file': gettext('Delete file "%(path)s"?'),
 
98
             'binary': gettext('Apply binary changes?'),
 
99
             'change kind': gettext('Change "%(path)s" from %(this)s'
 
100
             ' to %(other)s?'),
 
101
             'delete file': gettext('Add file "%(path)s"?'),
 
102
             'final': gettext('Apply %d change(s)?'),
 
103
             'hunk': gettext('Apply change?'),
 
104
             'modify target': gettext('Change target of'
 
105
             ' "%(path)s" from "%(this)s" to "%(other)s"?'),
 
106
             'rename': gettext('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='.', destroy=False):
 
157
                  message=None, directory=None, 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
        """
 
171
        if directory is None:
 
172
            directory = u'.'
 
173
        elif file_list:
 
174
            file_list = [osutils.pathjoin(directory, f) for f in file_list]
172
175
        tree, path = workingtree.WorkingTree.open_containing(directory)
173
176
        # Ensure that tree is locked for the lifetime of target_tree, as
174
177
        # target tree may be reading from the same dirstate.
176
179
        try:
177
180
            target_tree = builtins._get_one_revision_tree('shelf2', revision,
178
181
                tree.branch, tree)
179
 
            files = builtins.safe_relpath_files(tree, file_list)
 
182
            files = tree.safe_relpath_files(file_list)
180
183
            return klass(tree, target_tree, diff_writer, all, all, files,
181
184
                         message, destroy)
182
185
        finally:
242
245
            new_tree = self.work_tree
243
246
        old_path = old_tree.id2path(file_id)
244
247
        new_path = new_tree.id2path(file_id)
245
 
        text_differ = diff.DiffText(old_tree, new_tree, diff_file)
 
248
        text_differ = diff.DiffText(old_tree, new_tree, diff_file,
 
249
            path_encoding=osutils.get_terminal_encoding())
246
250
        patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
247
251
        diff_file.seek(0)
248
252
        return patches.parse_patch(diff_file)
258
262
            # to prompt the user, better abort now.  See
259
263
            # https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
260
264
            # for more context.
261
 
            raise errors.BzrError("You need a controlling terminal.")
 
265
            raise errors.BzrError(gettext("You need a controlling terminal."))
262
266
        sys.stdout.write(message)
263
267
        char = osutils.getchar()
264
268
        sys.stdout.write("\r" + ' ' * len(message) + '\r')
383
387
    """Unshelve changes into a working tree."""
384
388
 
385
389
    @classmethod
386
 
    def from_args(klass, shelf_id=None, action='apply', directory='.'):
 
390
    def from_args(klass, shelf_id=None, action='apply', directory='.',
 
391
                  write_diff_to=None):
387
392
        """Create an unshelver from commandline arguments.
388
393
 
389
 
        The returned shelver wil have a tree that is locked and should
 
394
        The returned shelver will have a tree that is locked and should
390
395
        be unlocked.
391
396
 
392
397
        :param shelf_id: Integer id of the shelf, as a string.
393
398
        :param action: action to perform.  May be 'apply', 'dry-run',
394
 
            'delete'.
 
399
            'delete', 'preview'.
395
400
        :param directory: The directory to unshelve changes into.
 
401
        :param write_diff_to: See Unshelver.__init__().
396
402
        """
397
403
        tree, path = workingtree.WorkingTree.open_containing(directory)
398
404
        tree.lock_tree_write()
406
412
            else:
407
413
                shelf_id = manager.last_shelf()
408
414
                if shelf_id is None:
409
 
                    raise errors.BzrCommandError('No changes are shelved.')
410
 
                trace.note('Unshelving changes with id "%d".' % shelf_id)
 
415
                    raise errors.BzrCommandError(gettext('No changes are shelved.'))
411
416
            apply_changes = True
412
417
            delete_shelf = True
413
418
            read_shelf = True
 
419
            show_diff = False
414
420
            if action == 'dry-run':
415
421
                apply_changes = False
416
422
                delete_shelf = False
 
423
            elif action == 'preview':
 
424
                apply_changes = False
 
425
                delete_shelf = False
 
426
                show_diff = True
417
427
            elif action == 'delete-only':
418
428
                apply_changes = False
419
429
                read_shelf = False
424
434
            tree.unlock()
425
435
            raise
426
436
        return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
427
 
                     read_shelf)
 
437
                     read_shelf, show_diff, write_diff_to)
428
438
 
429
439
    def __init__(self, tree, manager, shelf_id, apply_changes=True,
430
 
                 delete_shelf=True, read_shelf=True):
 
440
                 delete_shelf=True, read_shelf=True, show_diff=False,
 
441
                 write_diff_to=None):
431
442
        """Constructor.
432
443
 
433
444
        :param tree: The working tree to unshelve into.
437
448
            working tree.
438
449
        :param delete_shelf: If True, delete the changes from the shelf.
439
450
        :param read_shelf: If True, read the changes from the shelf.
 
451
        :param show_diff: If True, show the diff that would result from
 
452
            unshelving the changes.
 
453
        :param write_diff_to: A file-like object where the diff will be
 
454
            written to. If None, ui.ui_factory.make_output_stream() will
 
455
            be used.
440
456
        """
441
457
        self.tree = tree
442
458
        manager = tree.get_shelf_manager()
445
461
        self.apply_changes = apply_changes
446
462
        self.delete_shelf = delete_shelf
447
463
        self.read_shelf = read_shelf
 
464
        self.show_diff = show_diff
 
465
        self.write_diff_to = write_diff_to
448
466
 
449
467
    def run(self):
450
468
        """Perform the unshelving operation."""
452
470
        cleanups = [self.tree.unlock]
453
471
        try:
454
472
            if self.read_shelf:
 
473
                trace.note(gettext('Using changes with id "%d".') % self.shelf_id)
455
474
                unshelver = self.manager.get_unshelver(self.shelf_id)
456
475
                cleanups.append(unshelver.finalize)
457
476
                if unshelver.message is not None:
458
 
                    trace.note('Message: %s' % unshelver.message)
 
477
                    trace.note(gettext('Message: %s') % unshelver.message)
459
478
                change_reporter = delta._ChangeReporter()
460
 
                task = ui.ui_factory.nested_progress_bar()
461
 
                try:
462
 
                    merger = unshelver.make_merger(task)
463
 
                    merger.change_reporter = change_reporter
464
 
                    if self.apply_changes:
465
 
                        merger.do_merge()
466
 
                    else:
467
 
                        self.show_changes(merger)
468
 
                finally:
469
 
                    task.finished()
 
479
                merger = unshelver.make_merger(None)
 
480
                merger.change_reporter = change_reporter
 
481
                if self.apply_changes:
 
482
                    merger.do_merge()
 
483
                elif self.show_diff:
 
484
                    self.write_diff(merger)
 
485
                else:
 
486
                    self.show_changes(merger)
470
487
            if self.delete_shelf:
471
488
                self.manager.delete_shelf(self.shelf_id)
 
489
                trace.note(gettext('Deleted changes with id "%d".') % self.shelf_id)
472
490
        finally:
473
491
            for cleanup in reversed(cleanups):
474
492
                cleanup()
475
493
 
 
494
    def write_diff(self, merger):
 
495
        """Write this operation's diff to self.write_diff_to."""
 
496
        tree_merger = merger.make_merger()
 
497
        tt = tree_merger.make_preview_transform()
 
498
        new_tree = tt.get_preview_tree()
 
499
        if self.write_diff_to is None:
 
500
            self.write_diff_to = ui.ui_factory.make_output_stream(encoding_type='exact')
 
501
        path_encoding = osutils.get_diff_header_encoding()
 
502
        diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to,
 
503
            path_encoding=path_encoding)
 
504
        tt.finalize()
 
505
 
476
506
    def show_changes(self, merger):
477
507
        """Show the changes that this operation specifies."""
478
508
        tree_merger = merger.make_merger()