14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
18
from cStringIO import StringIO
24
from io import BytesIO
40
from .i18n import gettext
39
43
class UseEditor(Exception):
43
47
class ShelfReporter(object):
45
vocab = {'add file': 'Shelve adding file "%(path)s"?',
46
'binary': 'Shelve binary changes?',
47
'change kind': 'Shelve changing "%s" from %(other)s'
49
'delete file': 'Shelve removing file "%(path)s"?',
50
'final': 'Shelve %d change(s)?',
52
'modify target': 'Shelve changing target of'
53
' "%(path)s" from "%(other)s" to "%(this)s"?',
54
'rename': 'Shelve renaming "%(other)s" =>'
49
vocab = {'add file': gettext('Shelve adding file "%(path)s"?'),
50
'binary': gettext('Shelve binary changes?'),
51
'change kind': gettext('Shelve changing "%s" from %(other)s'
53
'delete file': gettext('Shelve removing file "%(path)s"?'),
54
'final': gettext('Shelve %d change(s)?'),
55
'hunk': gettext('Shelve?'),
56
'modify target': gettext('Shelve changing target of'
57
' "%(path)s" from "%(other)s" to "%(this)s"?'),
58
'rename': gettext('Shelve renaming "%(other)s" =>'
58
62
invert_diff = False
67
71
def shelved_id(self, shelf_id):
68
72
"""Report the id changes were shelved to."""
69
trace.note('Changes shelved with id "%d".' % shelf_id)
73
trace.note(gettext('Changes shelved with id "%d".') % shelf_id)
71
75
def changes_destroyed(self):
72
76
"""Report that changes were made without shelving."""
73
trace.note('Selected changes destroyed.')
77
trace.note(gettext('Selected changes destroyed.'))
75
79
def selected_changes(self, transform):
76
80
"""Report the changes that were selected."""
77
trace.note("Selected changes:")
81
trace.note(gettext("Selected changes:"))
78
82
changes = transform.iter_changes()
79
83
delta.report_changes(changes, self.delta_reporter)
95
99
class ApplyReporter(ShelfReporter):
97
vocab = {'add file': 'Delete file "%(path)s"?',
98
'binary': 'Apply binary changes?',
99
'change kind': 'Change "%(path)s" from %(this)s'
101
'delete file': 'Add file "%(path)s"?',
102
'final': 'Apply %d change(s)?',
103
'hunk': 'Apply change?',
104
'modify target': 'Change target of'
105
' "%(path)s" from "%(this)s" to "%(other)s"?',
106
'rename': 'Rename "%(this)s" => "%(other)s"?',
101
vocab = {'add file': gettext('Delete file "%(path)s"?'),
102
'binary': gettext('Apply binary changes?'),
103
'change kind': gettext('Change "%(path)s" from %(this)s'
105
'delete file': gettext('Add file "%(path)s"?'),
106
'final': gettext('Apply %d change(s)?'),
107
'hunk': gettext('Apply change?'),
108
'modify target': gettext('Change target of'
109
' "%(path)s" from "%(this)s" to "%(other)s"?'),
110
'rename': gettext('Rename "%(this)s" => "%(other)s"?'),
109
113
invert_diff = True
156
160
def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
157
message=None, directory='.', destroy=False):
161
message=None, directory=None, destroy=False):
158
162
"""Create a shelver from commandline arguments.
160
164
The returned shelver wil have a work_tree that is locked and should
168
172
:param destroy: Change the working tree without storing the shelved
175
if directory is None:
178
file_list = [osutils.pathjoin(directory, f) for f in file_list]
171
179
tree, path = workingtree.WorkingTree.open_containing(directory)
172
180
# Ensure that tree is locked for the lifetime of target_tree, as
173
181
# target tree may be reading from the same dirstate.
174
tree.lock_tree_write()
182
with tree.lock_tree_write():
176
183
target_tree = builtins._get_one_revision_tree('shelf2', revision,
178
files = builtins.safe_relpath_files(tree, file_list)
185
files = tree.safe_relpath_files(file_list)
179
186
return klass(tree, target_tree, diff_writer, all, all, files,
180
187
message, destroy)
185
190
"""Interactively shelve the changes."""
204
209
if changes_shelved > 0:
205
210
self.reporter.selected_changes(creator.work_transform)
206
211
if (self.auto_apply or self.prompt_bool(
207
self.reporter.vocab['final'] % changes_shelved)):
212
self.reporter.vocab['final'] % changes_shelved)):
209
214
creator.transform()
210
215
self.reporter.changes_destroyed()
232
236
as removals, removals displayed as insertions).
233
237
:return: A patches.Patch.
235
diff_file = StringIO()
239
diff_file = BytesIO()
237
241
old_tree = self.work_tree
238
242
new_tree = self.target_tree
241
245
new_tree = self.work_tree
242
246
old_path = old_tree.id2path(file_id)
243
247
new_path = new_tree.id2path(file_id)
244
text_differ = diff.DiffText(old_tree, new_tree, diff_file)
245
patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
248
path_encoding = osutils.get_terminal_encoding()
249
text_differ = diff.DiffText(old_tree, new_tree, diff_file,
250
path_encoding=path_encoding)
251
patch = text_differ.diff(old_path, new_path, 'file', 'file')
246
252
diff_file.seek(0)
247
253
return patches.parse_patch(diff_file)
249
def prompt(self, message):
250
"""Prompt the user for a character.
252
:param message: The message to prompt a user with.
253
:return: A character.
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
260
raise errors.BzrError("You need a controlling terminal.")
261
sys.stdout.write(message)
262
char = osutils.getchar()
263
sys.stdout.write("\r" + ' ' * len(message) + '\r')
267
def prompt_bool(self, question, long=False, allow_editor=False):
255
def prompt(self, message, choices, default):
256
return ui.ui_factory.choose(message, choices, default=default)
258
def prompt_bool(self, question, allow_editor=False):
268
259
"""Prompt the user with a yes/no question.
270
261
This may be overridden by self.auto. It may also *set* self.auto. It
280
editor_string = '(E)dit manually, '
281
prompt = ' [(y)es, (N)o, %s(f)inish, or (q)uit]' % editor_string
268
alternatives_chars = 'yn'
269
alternatives = '&yes\n&No'
271
alternatives_chars += 'e'
272
alternatives += '\n&edit manually'
273
alternatives_chars += 'fq'
274
alternatives += '\n&finish\n&quit'
275
choice = self.prompt(question, alternatives, 1)
285
prompt = ' [yN%sfq?]' % editor_string
286
char = self.prompt(question + prompt)
280
char = alternatives_chars[choice]
289
283
elif char == 'e' and allow_editor:
305
297
:param file_id: The id of the file that was modified.
306
298
:return: The number of changes.
308
work_tree_lines = self.work_tree.get_file_lines(file_id)
300
path = self.work_tree.id2path(file_id)
301
work_tree_lines = self.work_tree.get_file_lines(path, file_id)
310
303
lines, change_count = self._select_hunks(creator, file_id,
329
322
if self.reporter.invert_diff:
330
323
target_lines = work_tree_lines
332
target_lines = self.target_tree.get_file_lines(file_id)
325
path = self.target_tree.id2path(file_id)
326
target_lines = self.target_tree.get_file_lines(path)
333
327
textfile.check_text_lines(work_tree_lines)
334
328
textfile.check_text_lines(target_lines)
335
329
parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
339
333
self.diff_writer.write(parsed.get_header())
340
334
for hunk in parsed.hunks:
341
self.diff_writer.write(str(hunk))
335
self.diff_writer.write(hunk.as_bytes())
342
336
selected = self.prompt_bool(self.reporter.vocab['hunk'],
343
337
allow_editor=(self.change_editor
367
361
content of the file, and change_region_count is the number of
370
lines = osutils.split_lines(self.change_editor.edit_file(file_id))
364
lines = osutils.split_lines(self.change_editor.edit_file(
365
self.change_editor.old_tree.id2path(file_id),
366
self.change_editor.new_tree.id2path(file_id)))
371
367
return lines, self._count_changed_regions(work_tree_lines, lines)
404
400
shelf_id = int(shelf_id)
405
401
except ValueError:
406
raise errors.InvalidShelfId(shelf_id)
402
raise shelf.InvalidShelfId(shelf_id)
408
404
shelf_id = manager.last_shelf()
409
405
if shelf_id is None:
410
raise errors.BzrCommandError('No changes are shelved.')
406
raise errors.BzrCommandError(
407
gettext('No changes are shelved.'))
411
408
apply_changes = True
412
409
delete_shelf = True
413
410
read_shelf = True
463
460
"""Perform the unshelving operation."""
464
self.tree.lock_tree_write()
465
cleanups = [self.tree.unlock]
461
with cleanup.ExitStack() as exit_stack:
462
exit_stack.enter_context(self.tree.lock_tree_write())
467
463
if self.read_shelf:
468
trace.note('Using changes with id "%d".' % self.shelf_id)
464
trace.note(gettext('Using changes with id "%d".') %
469
466
unshelver = self.manager.get_unshelver(self.shelf_id)
470
cleanups.append(unshelver.finalize)
467
exit_stack.callback(unshelver.finalize)
471
468
if unshelver.message is not None:
472
trace.note('Message: %s' % unshelver.message)
469
trace.note(gettext('Message: %s') % unshelver.message)
473
470
change_reporter = delta._ChangeReporter()
474
merger = unshelver.make_merger(None)
471
merger = unshelver.make_merger()
475
472
merger.change_reporter = change_reporter
476
473
if self.apply_changes:
477
474
merger.do_merge()
481
478
self.show_changes(merger)
482
479
if self.delete_shelf:
483
480
self.manager.delete_shelf(self.shelf_id)
484
trace.note('Deleted changes with id "%d".' % self.shelf_id)
486
for cleanup in reversed(cleanups):
481
trace.note(gettext('Deleted changes with id "%d".') %
489
484
def write_diff(self, merger):
490
485
"""Write this operation's diff to self.write_diff_to."""
492
487
tt = tree_merger.make_preview_transform()
493
488
new_tree = tt.get_preview_tree()
494
489
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)
490
self.write_diff_to = ui.ui_factory.make_output_stream(
491
encoding_type='exact')
492
path_encoding = osutils.get_diff_header_encoding()
493
diff.show_diff_trees(merger.this_tree, new_tree, self.write_diff_to,
494
path_encoding=path_encoding)
499
497
def show_changes(self, merger):