~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Andrew Bennetts
  • Date: 2010-02-12 04:33:05 UTC
  • mfrom: (5031 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5032.
  • Revision ID: andrew.bennetts@canonical.com-20100212043305-ujdbsdoviql2t7i3
MergeĀ lp:bzr

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,
253
252
        :param message: The message to prompt a user with.
254
253
        :return: A character.
255
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.")
256
261
        sys.stdout.write(message)
257
262
        char = osutils.getchar()
258
263
        sys.stdout.write("\r" + ' ' * len(message) + '\r')
377
382
    """Unshelve changes into a working tree."""
378
383
 
379
384
    @classmethod
380
 
    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):
381
387
        """Create an unshelver from commandline arguments.
382
388
 
383
 
        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
384
390
        be unlocked.
385
391
 
386
392
        :param shelf_id: Integer id of the shelf, as a string.
387
393
        :param action: action to perform.  May be 'apply', 'dry-run',
388
 
            'delete'.
 
394
            'delete', 'preview'.
389
395
        :param directory: The directory to unshelve changes into.
 
396
        :param write_diff_to: See Unshelver.__init__().
390
397
        """
391
398
        tree, path = workingtree.WorkingTree.open_containing(directory)
392
399
        tree.lock_tree_write()
401
408
                shelf_id = manager.last_shelf()
402
409
                if shelf_id is None:
403
410
                    raise errors.BzrCommandError('No changes are shelved.')
404
 
                trace.note('Unshelving changes with id "%d".' % shelf_id)
405
411
            apply_changes = True
406
412
            delete_shelf = True
407
413
            read_shelf = True
 
414
            show_diff = False
408
415
            if action == 'dry-run':
409
416
                apply_changes = False
410
417
                delete_shelf = False
411
 
            if action == 'delete-only':
 
418
            elif action == 'preview':
 
419
                apply_changes = False
 
420
                delete_shelf = False
 
421
                show_diff = True
 
422
            elif action == 'delete-only':
412
423
                apply_changes = False
413
424
                read_shelf = False
 
425
            elif action == 'keep':
 
426
                apply_changes = True
 
427
                delete_shelf = False
414
428
        except:
415
429
            tree.unlock()
416
430
            raise
417
431
        return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
418
 
                     read_shelf)
 
432
                     read_shelf, show_diff, write_diff_to)
419
433
 
420
434
    def __init__(self, tree, manager, shelf_id, apply_changes=True,
421
 
                 delete_shelf=True, read_shelf=True):
 
435
                 delete_shelf=True, read_shelf=True, show_diff=False,
 
436
                 write_diff_to=None):
422
437
        """Constructor.
423
438
 
424
439
        :param tree: The working tree to unshelve into.
428
443
            working tree.
429
444
        :param delete_shelf: If True, delete the changes from the shelf.
430
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.
431
451
        """
432
452
        self.tree = tree
433
453
        manager = tree.get_shelf_manager()
436
456
        self.apply_changes = apply_changes
437
457
        self.delete_shelf = delete_shelf
438
458
        self.read_shelf = read_shelf
 
459
        self.show_diff = show_diff
 
460
        self.write_diff_to = write_diff_to
439
461
 
440
462
    def run(self):
441
463
        """Perform the unshelving operation."""
443
465
        cleanups = [self.tree.unlock]
444
466
        try:
445
467
            if self.read_shelf:
 
468
                trace.note('Using changes with id "%d".' % self.shelf_id)
446
469
                unshelver = self.manager.get_unshelver(self.shelf_id)
447
470
                cleanups.append(unshelver.finalize)
448
471
                if unshelver.message is not None:
449
472
                    trace.note('Message: %s' % unshelver.message)
450
473
                change_reporter = delta._ChangeReporter()
451
 
                task = ui.ui_factory.nested_progress_bar()
452
 
                try:
453
 
                    merger = unshelver.make_merger(task)
454
 
                    merger.change_reporter = change_reporter
455
 
                    if self.apply_changes:
456
 
                        merger.do_merge()
457
 
                    else:
458
 
                        self.show_changes(merger)
459
 
                finally:
460
 
                    task.finished()
 
474
                merger = unshelver.make_merger(None)
 
475
                merger.change_reporter = change_reporter
 
476
                if self.apply_changes:
 
477
                    merger.do_merge()
 
478
                elif self.show_diff:
 
479
                    self.write_diff(merger)
 
480
                else:
 
481
                    self.show_changes(merger)
461
482
            if self.delete_shelf:
462
483
                self.manager.delete_shelf(self.shelf_id)
 
484
                trace.note('Deleted changes with id "%d".' % self.shelf_id)
463
485
        finally:
464
486
            for cleanup in reversed(cleanups):
465
487
                cleanup()
466
488
 
 
489
    def write_diff(self, merger):
 
490
        """Write this operation's diff to self.write_diff_to."""
 
491
        tree_merger = merger.make_merger()
 
492
        tt = tree_merger.make_preview_transform()
 
493
        new_tree = tt.get_preview_tree()
 
494
        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)
 
497
        tt.finalize()
 
498
 
467
499
    def show_changes(self, merger):
468
500
        """Show the changes that this operation specifies."""
469
501
        tree_merger = merger.make_merger()