~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Guilherme Salgado
  • Date: 2010-01-06 13:50:55 UTC
  • mto: This revision was merged to the branch mainline in revision 4955.
  • Revision ID: salgado@canonical.com-20100106135055-qkteluei2dmtvesw
A few tweaks as per John's review

Show diffs side-by-side

added added

removed removed

Lines of Context:
382
382
    """Unshelve changes into a working tree."""
383
383
 
384
384
    @classmethod
385
 
    def from_args(klass, shelf_id=None, action='apply', directory='.'):
 
385
    def from_args(klass, shelf_id=None, action='apply', directory='.',
 
386
                  write_diff_to=None):
386
387
        """Create an unshelver from commandline arguments.
387
388
 
388
 
        The returned shelver wil have a tree that is locked and should
 
389
        The returned shelver will have a tree that is locked and should
389
390
        be unlocked.
390
391
 
391
392
        :param shelf_id: Integer id of the shelf, as a string.
392
393
        :param action: action to perform.  May be 'apply', 'dry-run',
393
394
            'delete', 'preview'.
394
395
        :param directory: The directory to unshelve changes into.
 
396
        :param write_diff_to: See Unshelver.__init__().
395
397
        """
396
398
        tree, path = workingtree.WorkingTree.open_containing(directory)
397
399
        tree.lock_tree_write()
428
430
            tree.unlock()
429
431
            raise
430
432
        return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
431
 
                     read_shelf, show_diff)
 
433
                     read_shelf, show_diff, write_diff_to)
432
434
 
433
435
    def __init__(self, tree, manager, shelf_id, apply_changes=True,
434
 
                 delete_shelf=True, read_shelf=True, show_diff=False):
 
436
                 delete_shelf=True, read_shelf=True, show_diff=False,
 
437
                 write_diff_to=None):
435
438
        """Constructor.
436
439
 
437
440
        :param tree: The working tree to unshelve into.
443
446
        :param read_shelf: If True, read the changes from the shelf.
444
447
        :param show_diff: If True, show the diff that would result from
445
448
            unshelving the changes.
 
449
        :param write_diff_to: A file-like object where the diff will be
 
450
            written to. If None, ui.ui_factory.make_output_stream() will
 
451
            be used.
446
452
        """
447
453
        self.tree = tree
448
454
        manager = tree.get_shelf_manager()
452
458
        self.delete_shelf = delete_shelf
453
459
        self.read_shelf = read_shelf
454
460
        self.show_diff = show_diff
 
461
        self.write_diff_to = write_diff_to
 
462
        if self.write_diff_to is None:
 
463
            self.write_diff_to = ui.ui_factory.make_output_stream()
455
464
 
456
 
    def run(self, diff_output=None):
 
465
    def run(self):
457
466
        """Perform the unshelving operation."""
458
467
        self.tree.lock_tree_write()
459
468
        cleanups = [self.tree.unlock]
471
480
                    if self.apply_changes:
472
481
                        merger.do_merge()
473
482
                    elif self.show_diff:
474
 
                        self.write_diff(merger, diff_output)
 
483
                        self.write_diff(merger)
475
484
                    else:
476
485
                        self.show_changes(merger)
477
486
                finally:
482
491
            for cleanup in reversed(cleanups):
483
492
                cleanup()
484
493
 
485
 
    def write_diff(self, merger, diff_output=None):
 
494
    def write_diff(self, merger):
 
495
        """Write this operation's diff to self.write_diff_to."""
486
496
        tree_merger = merger.make_merger()
487
 
        # This would implicitly show the changes via the reporter, but we
488
 
        # don't want that, so we silence it.
489
497
        merger.change_reporter.report = lambda *args: None
490
498
        tt = tree_merger.make_preview_transform()
491
 
        # And now we show the diff that would've been applied if this was
492
 
        # for real.
 
499
        # Show the diff that would've been applied if this was for real.
493
500
        new_tree = tt.get_preview_tree()
494
 
        if diff_output is None:
495
 
            diff_output = ui.ui_factory.make_output_stream()
496
 
        diff.show_diff_trees(merger.this_tree, new_tree, diff_output)
 
501
        diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to)
497
502
        tt.finalize()
498
503
 
499
504
    def show_changes(self, merger):
500
505
        """Show the changes that this operation specifies."""
501
506
        tree_merger = merger.make_merger()
502
 
        # This implicitly shows the changes via the reporter.
 
507
        # This implicitly shows the changes via the reporter, so we're done...
503
508
        tt = tree_merger.make_preview_transform()
504
509
        tt.finalize()