38
class UseEditor(Exception):
39
"""Use an editor instead of selecting hunks."""
38
42
class ShelfReporter(object):
40
44
vocab = {'add file': 'Shelve adding file "%(path)s"?',
235
239
sys.stdout.flush()
238
def prompt_bool(self, question, long=False):
242
def prompt_bool(self, question, long=False, allow_editor=False):
239
243
"""Prompt the user with a yes/no question.
241
245
This may be overridden by self.auto. It may also *set* self.auto. It
249
prompt = ' [(y)es, (N)o, (f)inish, or (q)uit]'
255
editor_string = '(E)dit manually, '
256
prompt = ' [(y)es, (N)o, %s(f)inish, or (q)uit]' % editor_string
260
prompt = ' [yN%sfq?]' % editor_string
252
261
char = self.prompt(question + prompt)
264
elif char == 'e' and allow_editor:
255
266
elif char == 'f':
265
276
def handle_modify_text(self, creator, file_id):
278
lines, change_count = self._select_hunks(creator, file_id)
280
lines, change_count = self._edit_file(creator, file_id)
281
if change_count != 0:
282
creator.shelve_lines(file_id, lines)
285
def _select_hunks(self, creator, file_id):
266
286
"""Provide diff hunk selection for modified text.
268
288
If self.reporter.invert_diff is True, the diff is inverted so that
276
296
target_lines = self.work_tree.get_file_lines(file_id)
278
298
target_lines = self.target_tree.get_file_lines(file_id)
279
textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
299
work_tree_lines = self.work_tree.get_file_lines(file_id)
300
textfile.check_text_lines(work_tree_lines)
280
301
textfile.check_text_lines(target_lines)
281
302
parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
285
306
self.diff_writer.write(parsed.get_header())
286
307
for hunk in parsed.hunks:
287
308
self.diff_writer.write(str(hunk))
288
selected = self.prompt_bool(self.reporter.vocab['hunk'])
309
selected = self.prompt_bool(self.reporter.vocab['hunk'],
289
311
if not self.reporter.invert_diff:
290
312
selected = (not selected)
296
318
sys.stdout.flush()
297
319
if not self.reporter.invert_diff and (
298
320
len(parsed.hunks) == len(final_hunks)):
321
return work_tree_lines, 0
300
322
if self.reporter.invert_diff and len(final_hunks) == 0:
323
return work_tree_lines, 0
302
324
patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
303
creator.shelve_lines(file_id, list(patched))
325
lines = list(patched)
304
326
if self.reporter.invert_diff:
305
return len(final_hunks)
306
return len(parsed.hunks) - len(final_hunks)
327
change_count = len(final_hunks)
329
change_count = len(parsed.hunks) - len(final_hunks)
330
return lines, change_count
332
def _edit_file(self, creator, file_id):
333
old_tree = self.target_tree
334
new_tree = self.work_tree
335
differ = diff.DiffFromTool.from_string('gvimdiff -f -o',
339
differ.force_temp = True
340
differ.allow_write = True
341
old_path = old_tree.id2path(file_id)
342
new_path = new_tree.id2path(file_id)
343
differ.diff(file_id, old_path, new_path, 'file', 'file')
344
new_abs_path = differ.get_new_path(new_path, abspath=True)
345
new_lines_file = open(new_abs_path, 'r')
347
lines = osutils.split_lines(new_lines_file.read())
348
return lines, len(lines)
350
new_lines_file.close()
309
356
class Unshelver(object):