118
119
def __init__(self, work_tree, target_tree, diff_writer=None, auto=False,
119
120
auto_apply=False, file_list=None, message=None,
120
destroy=False, manager=None, reporter=None):
121
destroy=False, manager=None, reporter=None,
123
125
:param work_tree: The working tree to shelve changes from.
132
134
:param manager: The shelf manager to use.
133
135
:param reporter: Object for reporting changes to user.
136
:param change_editor: Editor for changed files.
135
138
self.work_tree = work_tree
136
139
self.target_tree = target_tree
148
151
if reporter is None:
149
152
reporter = ShelfReporter()
150
153
self.reporter = reporter
154
self.change_editor = change_editor
153
157
def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
169
173
return klass(tree, target_tree, diff_writer, all, all, files, message,
176
def set_change_editor(self):
177
config = self.work_tree.branch.get_config()
178
commandline = config.get_user_option('change_editor')
179
if commandline is None:
181
command = commands.shlex_split_unicode(commandline)
182
if '%' not in commandline:
183
command.extend(['%(old_path)s', '%(new_path)s'])
184
self.change_editor = diff.DiffFromTool(command, self.target_tree,
185
self.work_tree, sys.stdout)
173
188
"""Interactively shelve the changes."""
174
189
creator = shelf.ShelfCreator(self.work_tree, self.target_tree,
176
191
self.tempdir = tempfile.mkdtemp()
177
192
changes_shelved = 0
179
old_tree = self.target_tree
180
new_tree = self.work_tree
181
command = ['gvimdiff', '-fo', '%(new_path)s', '%(old_path)s']
182
differ = diff.DiffFromTool(command, old_tree, new_tree,
185
for change in creator.iter_shelvable():
186
if change[0] == 'modify text':
188
changes_shelved += self.handle_modify_text(
189
creator, change[1], differ)
190
except errors.BinaryFile:
191
if self.prompt_bool(self.reporter.vocab['binary']):
193
creator.shelve_content_change(change[1])
195
if self.prompt_bool(self.reporter.prompt_change(change)):
196
creator.shelve_change(change)
194
for change in creator.iter_shelvable():
195
if change[0] == 'modify text':
197
changes_shelved += self.handle_modify_text(
199
except errors.BinaryFile:
200
if self.prompt_bool(self.reporter.vocab['binary']):
197
201
changes_shelved += 1
198
if changes_shelved > 0:
199
self.reporter.selected_changes(creator.work_transform)
200
if (self.auto_apply or self.prompt_bool(
201
self.reporter.vocab['final'] % changes_shelved)):
204
self.reporter.changes_destroyed()
206
shelf_id = self.manager.shelve_changes(creator,
208
self.reporter.shelved_id(shelf_id)
202
creator.shelve_content_change(change[1])
210
self.reporter.no_changes()
204
if self.prompt_bool(self.reporter.prompt_change(change)):
205
creator.shelve_change(change)
207
if changes_shelved > 0:
208
self.reporter.selected_changes(creator.work_transform)
209
if (self.auto_apply or self.prompt_bool(
210
self.reporter.vocab['final'] % changes_shelved)):
213
self.reporter.changes_destroyed()
215
shelf_id = self.manager.shelve_changes(creator,
217
self.reporter.shelved_id(shelf_id)
219
self.reporter.no_changes()
214
221
shutil.rmtree(self.tempdir)
215
222
creator.finalize()
285
def handle_modify_text(self, creator, file_id, differ):
292
def handle_modify_text(self, creator, file_id):
287
294
lines, change_count = self._select_hunks(creator, file_id)
288
295
except UseEditor:
289
lines, change_count = self._edit_file(creator, file_id, differ)
296
lines, change_count = self._edit_file(creator, file_id)
290
297
if change_count != 0:
291
298
creator.shelve_lines(file_id, lines)
292
299
return change_count
316
323
for hunk in parsed.hunks:
317
324
self.diff_writer.write(str(hunk))
318
325
selected = self.prompt_bool(self.reporter.vocab['hunk'],
326
allow_editor=(self.change_editor
320
328
if not self.reporter.invert_diff:
321
329
selected = (not selected)
334
342
lines = list(patched)
335
343
return lines, change_count
337
def _edit_file(self, creator, file_id, differ):
345
def _edit_file(self, creator, file_id):
338
346
work_tree_lines = creator.work_tree.get_file_lines(file_id)
339
lines = osutils.split_lines(differ.edit_file(file_id))
347
lines = osutils.split_lines(self.change_editor.edit_file(file_id))
340
348
return lines, self._count_changed_regions(work_tree_lines, lines)