~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

Merge description into dont-add-conflict-helpers

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,
383
382
    """Unshelve changes into a working tree."""
384
383
 
385
384
    @classmethod
386
 
    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):
387
387
        """Create an unshelver from commandline arguments.
388
388
 
389
 
        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
390
390
        be unlocked.
391
391
 
392
392
        :param shelf_id: Integer id of the shelf, as a string.
393
393
        :param action: action to perform.  May be 'apply', 'dry-run',
394
 
            'delete'.
 
394
            'delete', 'preview'.
395
395
        :param directory: The directory to unshelve changes into.
 
396
        :param write_diff_to: See Unshelver.__init__().
396
397
        """
397
398
        tree, path = workingtree.WorkingTree.open_containing(directory)
398
399
        tree.lock_tree_write()
410
411
            apply_changes = True
411
412
            delete_shelf = True
412
413
            read_shelf = True
 
414
            show_diff = False
413
415
            if action == 'dry-run':
414
416
                apply_changes = False
415
417
                delete_shelf = False
 
418
            elif action == 'preview':
 
419
                apply_changes = False
 
420
                delete_shelf = False
 
421
                show_diff = True
416
422
            elif action == 'delete-only':
417
423
                apply_changes = False
418
424
                read_shelf = False
423
429
            tree.unlock()
424
430
            raise
425
431
        return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
426
 
                     read_shelf)
 
432
                     read_shelf, show_diff, write_diff_to)
427
433
 
428
434
    def __init__(self, tree, manager, shelf_id, apply_changes=True,
429
 
                 delete_shelf=True, read_shelf=True):
 
435
                 delete_shelf=True, read_shelf=True, show_diff=False,
 
436
                 write_diff_to=None):
430
437
        """Constructor.
431
438
 
432
439
        :param tree: The working tree to unshelve into.
436
443
            working tree.
437
444
        :param delete_shelf: If True, delete the changes from the shelf.
438
445
        :param read_shelf: If True, read the changes from the shelf.
 
446
        :param show_diff: If True, show the diff that would result from
 
447
            unshelving the changes.
 
448
        :param write_diff_to: A file-like object where the diff will be
 
449
            written to. If None, ui.ui_factory.make_output_stream() will
 
450
            be used.
439
451
        """
440
452
        self.tree = tree
441
453
        manager = tree.get_shelf_manager()
444
456
        self.apply_changes = apply_changes
445
457
        self.delete_shelf = delete_shelf
446
458
        self.read_shelf = read_shelf
 
459
        self.show_diff = show_diff
 
460
        self.write_diff_to = write_diff_to
447
461
 
448
462
    def run(self):
449
463
        """Perform the unshelving operation."""
463
477
                    merger.change_reporter = change_reporter
464
478
                    if self.apply_changes:
465
479
                        merger.do_merge()
 
480
                    elif self.show_diff:
 
481
                        self.write_diff(merger)
466
482
                    else:
467
483
                        self.show_changes(merger)
468
484
                finally:
474
490
            for cleanup in reversed(cleanups):
475
491
                cleanup()
476
492
 
 
493
    def write_diff(self, merger):
 
494
        """Write this operation's diff to self.write_diff_to."""
 
495
        tree_merger = merger.make_merger()
 
496
        tt = tree_merger.make_preview_transform()
 
497
        new_tree = tt.get_preview_tree()
 
498
        if self.write_diff_to is None:
 
499
            self.write_diff_to = ui.ui_factory.make_output_stream()
 
500
        diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to)
 
501
        tt.finalize()
 
502
 
477
503
    def show_changes(self, merger):
478
504
        """Show the changes that this operation specifies."""
479
505
        tree_merger = merger.make_merger()