40
class UseEditor(Exception):
41
"""Use an editor instead of selecting hunks."""
38
44
class ShelfReporter(object):
40
46
vocab = {'add file': 'Shelve adding file "%(path)s"?',
143
149
if reporter is None:
144
150
reporter = ShelfReporter()
145
151
self.reporter = reporter
152
config = self.work_tree.branch.get_config()
153
self.change_editor = config.get_change_editor(target_tree, work_tree)
154
self.work_tree.lock_tree_write()
148
157
def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
168
177
target_tree = builtins._get_one_revision_tree('shelf2', revision,
169
178
tree.branch, tree)
170
179
files = builtins.safe_relpath_files(tree, file_list)
180
return klass(tree, target_tree, diff_writer, all, all, files,
174
return klass(tree, target_tree, diff_writer, all, all, files, message,
178
186
"""Interactively shelve the changes."""
211
219
shutil.rmtree(self.tempdir)
212
220
creator.finalize()
223
if self.change_editor is not None:
224
self.change_editor.finish()
225
self.work_tree.unlock()
214
228
def get_parsed_patch(self, file_id, invert=False):
215
229
"""Return a parsed version of a file's patch.
239
253
:param message: The message to prompt a user with.
240
254
:return: A character.
256
if not sys.stdin.isatty():
257
# Since there is no controlling terminal we will hang when trying
258
# to prompt the user, better abort now. See
259
# https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
261
raise errors.BzrError("You need a controlling terminal.")
242
262
sys.stdout.write(message)
243
263
char = osutils.getchar()
244
264
sys.stdout.write("\r" + ' ' * len(message) + '\r')
245
265
sys.stdout.flush()
248
def prompt_bool(self, question, long=False):
268
def prompt_bool(self, question, long=False, allow_editor=False):
249
269
"""Prompt the user with a yes/no question.
251
271
This may be overridden by self.auto. It may also *set* self.auto. It
259
prompt = ' [(y)es, (N)o, (f)inish, or (q)uit]'
281
editor_string = '(E)dit manually, '
282
prompt = ' [(y)es, (N)o, %s(f)inish, or (q)uit]' % editor_string
286
prompt = ' [yN%sfq?]' % editor_string
262
287
char = self.prompt(question + prompt)
290
elif char == 'e' and allow_editor:
265
292
elif char == 'f':
275
302
def handle_modify_text(self, creator, file_id):
303
"""Handle modified text, by using hunk selection or file editing.
305
:param creator: A ShelfCreator.
306
:param file_id: The id of the file that was modified.
307
:return: The number of changes.
309
work_tree_lines = self.work_tree.get_file_lines(file_id)
311
lines, change_count = self._select_hunks(creator, file_id,
314
lines, change_count = self._edit_file(file_id, work_tree_lines)
315
if change_count != 0:
316
creator.shelve_lines(file_id, lines)
319
def _select_hunks(self, creator, file_id, work_tree_lines):
276
320
"""Provide diff hunk selection for modified text.
278
322
If self.reporter.invert_diff is True, the diff is inverted so that
281
325
:param creator: a ShelfCreator
282
326
:param file_id: The id of the file to shelve.
327
:param work_tree_lines: Line contents of the file in the working tree.
283
328
:return: number of shelved hunks.
285
330
if self.reporter.invert_diff:
286
target_lines = self.work_tree.get_file_lines(file_id)
331
target_lines = work_tree_lines
288
333
target_lines = self.target_tree.get_file_lines(file_id)
289
textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
334
textfile.check_text_lines(work_tree_lines)
290
335
textfile.check_text_lines(target_lines)
291
336
parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
295
340
self.diff_writer.write(parsed.get_header())
296
341
for hunk in parsed.hunks:
297
342
self.diff_writer.write(str(hunk))
298
selected = self.prompt_bool(self.reporter.vocab['hunk'])
343
selected = self.prompt_bool(self.reporter.vocab['hunk'],
344
allow_editor=(self.change_editor
299
346
if not self.reporter.invert_diff:
300
347
selected = (not selected)
305
352
offset -= (hunk.mod_range - hunk.orig_range)
306
353
sys.stdout.flush()
307
if not self.reporter.invert_diff and (
308
len(parsed.hunks) == len(final_hunks)):
310
if self.reporter.invert_diff and len(final_hunks) == 0:
312
patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
313
creator.shelve_lines(file_id, list(patched))
314
354
if self.reporter.invert_diff:
315
return len(final_hunks)
316
return len(parsed.hunks) - len(final_hunks)
355
change_count = len(final_hunks)
357
change_count = len(parsed.hunks) - len(final_hunks)
358
patched = patches.iter_patched_from_hunks(target_lines,
360
lines = list(patched)
361
return lines, change_count
363
def _edit_file(self, file_id, work_tree_lines):
365
:param file_id: id of the file to edit.
366
:param work_tree_lines: Line contents of the file in the working tree.
367
:return: (lines, change_region_count), where lines is the new line
368
content of the file, and change_region_count is the number of
371
lines = osutils.split_lines(self.change_editor.edit_file(file_id))
372
return lines, self._count_changed_regions(work_tree_lines, lines)
375
def _count_changed_regions(old_lines, new_lines):
376
matcher = patiencediff.PatienceSequenceMatcher(None, old_lines,
378
blocks = matcher.get_matching_blocks()
379
return len(blocks) - 2
319
382
class Unshelver(object):
344
407
shelf_id = manager.last_shelf()
345
408
if shelf_id is None:
346
409
raise errors.BzrCommandError('No changes are shelved.')
347
trace.note('Unshelving changes with id "%d".' % shelf_id)
348
410
apply_changes = True
349
411
delete_shelf = True
350
412
read_shelf = True
351
413
if action == 'dry-run':
352
414
apply_changes = False
353
415
delete_shelf = False
354
if action == 'delete-only':
416
elif action == 'delete-only':
355
417
apply_changes = False
356
418
read_shelf = False
419
elif action == 'keep':
386
451
cleanups = [self.tree.unlock]
388
453
if self.read_shelf:
454
trace.note('Using changes with id "%d".' % self.shelf_id)
389
455
unshelver = self.manager.get_unshelver(self.shelf_id)
390
456
cleanups.append(unshelver.finalize)
391
457
if unshelver.message is not None: