1
# Copyright (C) 2008 Canonical Ltd
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
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
44
43
class ShelfReporter(object):
46
vocab = {'add file': 'Shelve adding file "%(path)s"?',
47
'binary': 'Shelve binary changes?',
48
'change kind': 'Shelve changing "%s" from %(other)s'
50
'delete file': 'Shelve removing file "%(path)s"?',
51
'final': 'Shelve %d change(s)?',
53
'modify target': 'Shelve changing target of'
54
' "%(path)s" from "%(other)s" to "%(this)s"?',
55
'rename': 'Shelve renaming "%(other)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'
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" =>'
59
58
invert_diff = False
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)
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.'))
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)
96
95
class ApplyReporter(ShelfReporter):
98
vocab = {'add file': 'Delete file "%(path)s"?',
99
'binary': 'Apply binary changes?',
100
'change kind': 'Change "%(path)s" from %(this)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'
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"?'),
110
109
invert_diff = True
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.
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
171
if directory is None:
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.
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)
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."""
386
def from_args(klass, shelf_id=None, action='apply', directory='.'):
390
def from_args(klass, shelf_id=None, action='apply', directory='.',
387
392
"""Create an unshelver from commandline arguments.
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
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',
395
400
:param directory: The directory to unshelve changes into.
401
:param write_diff_to: See Unshelver.__init__().
397
403
tree, path = workingtree.WorkingTree.open_containing(directory)
398
404
tree.lock_tree_write()
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
414
420
if action == 'dry-run':
415
421
apply_changes = False
416
422
delete_shelf = False
423
elif action == 'preview':
424
apply_changes = False
417
427
elif action == 'delete-only':
418
428
apply_changes = False
419
429
read_shelf = False
426
436
return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
437
read_shelf, show_diff, write_diff_to)
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,
433
444
:param tree: The working tree to unshelve into.
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
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
450
468
"""Perform the unshelving operation."""
452
470
cleanups = [self.tree.unlock]
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()
462
merger = unshelver.make_merger(task)
463
merger.change_reporter = change_reporter
464
if self.apply_changes:
467
self.show_changes(merger)
479
merger = unshelver.make_merger(None)
480
merger.change_reporter = change_reporter
481
if self.apply_changes:
484
self.write_diff(merger)
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)
473
491
for cleanup in reversed(cleanups):
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)
476
506
def show_changes(self, merger):
477
507
"""Show the changes that this operation specifies."""
478
508
tree_merger = merger.make_merger()