~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: 2009-11-17 03:20:35 UTC
  • mfrom: (4792.4.3 456036)
  • Revision ID: pqm@pqm.ubuntu.com-20091117032035-s3sgtlixj1lrminn
(Gordon Tyler) Fix IndexError during 'bzr ignore /' (#456036)

Show diffs side-by-side

added added

removed removed

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