38
38
class ShelfReporter(object):
40
vocab = {'add file': 'Shelve adding file "%(path)s"?',
41
'binary': 'Shelve binary changes?',
42
'change kind': 'Shelve changing "%s" from %(other)s'
44
'delete file': 'Shelve removing file "%(path)s"?',
45
'final': 'Shelve %d change(s)?',
47
'modify target': 'Shelve changing target of'
48
' "%(path)s" from "%(other)s" to "%(this)s"?',
49
'rename': 'Shelve renaming "%(other)s" =>'
40
55
def __init__(self):
41
56
self.delta_reporter = delta._ChangeReporter()
43
58
def no_changes(self):
59
"""Report that no changes were selected to apply."""
44
60
trace.warning('No changes to shelve.')
46
62
def shelved_id(self, shelf_id):
63
"""Report the id changes were shelved to."""
47
64
trace.note('Changes shelved with id "%d".' % shelf_id)
66
def changes_destroyed(self):
67
"""Report that changes were made without shelving."""
68
trace.note('Selected changes destroyed.')
49
70
def selected_changes(self, transform):
71
"""Report the changes that were selected."""
50
72
trace.note("Selected changes:")
51
73
changes = transform.iter_changes()
52
74
delta.report_changes(changes, self.delta_reporter)
76
def prompt_change(self, change):
77
"""Determine the prompt for a change to apply."""
78
if change[0] == 'rename':
79
vals = {'this': change[3], 'other': change[2]}
80
elif change[0] == 'change kind':
81
vals = {'path': change[4], 'other': change[2], 'this': change[3]}
82
elif change[0] == 'modify target':
83
vals = {'path': change[2], 'other': change[3], 'this': change[4]}
85
vals = {'path': change[3]}
86
prompt = self.vocab[change[0]] % vals
90
class ApplyReporter(ShelfReporter):
92
vocab = {'add file': 'Delete file "%(path)s"?',
93
'binary': 'Apply binary changes?',
94
'change kind': 'Change "%(path)s" from %(this)s'
96
'delete file': 'Add file "%(path)s"?',
97
'final': 'Apply %d change(s)?',
98
'hunk': 'Apply change?',
99
'modify target': 'Change target of'
100
' "%(path)s" from "%(this)s" to "%(other)s"?',
101
'rename': 'Rename "%(this)s" => "%(other)s"?',
106
def changes_destroyed(self):
55
110
class Shelver(object):
56
111
"""Interactively shelve the changes in a working tree."""
121
177
changes_shelved += self.handle_modify_text(creator,
123
179
except errors.BinaryFile:
124
if self.prompt_bool('Shelve binary changes?'):
180
if self.prompt_bool(self.reporter.vocab['binary']):
125
181
changes_shelved += 1
126
182
creator.shelve_content_change(change[1])
127
if change[0] == 'add file':
128
if self.prompt_bool('Shelve adding file "%s"?'
130
creator.shelve_creation(change[1])
132
if change[0] == 'delete file':
133
if self.prompt_bool('Shelve removing file "%s"?'
135
creator.shelve_deletion(change[1])
137
if change[0] == 'change kind':
138
if self.prompt_bool('Shelve changing "%s" from %s to %s? '
139
% (change[4], change[2], change[3])):
140
creator.shelve_content_change(change[1])
142
if change[0] == 'rename':
143
if self.prompt_bool('Shelve renaming "%s" => "%s"?' %
145
creator.shelve_rename(change[1])
147
if change[0] == 'modify target':
148
if self.prompt_bool('Shelve changing target of "%s" '
149
'from "%s" to "%s"?' % change[2:]):
150
creator.shelve_modify_target(change[1])
184
if self.prompt_bool(self.reporter.prompt_change(change)):
185
creator.shelve_change(change)
151
186
changes_shelved += 1
152
187
if changes_shelved > 0:
153
188
self.reporter.selected_changes(creator.work_transform)
154
189
if (self.auto_apply or self.prompt_bool(
155
'Shelve %d change(s)?' % changes_shelved)):
190
self.reporter.vocab['final'] % changes_shelved)):
157
192
creator.transform()
158
trace.note('Selected changes destroyed.')
193
self.reporter.changes_destroyed()
160
195
shelf_id = self.manager.shelve_changes(creator,
166
201
shutil.rmtree(self.tempdir)
167
202
creator.finalize()
169
def get_parsed_patch(self, file_id):
204
def get_parsed_patch(self, file_id, invert=False):
170
205
"""Return a parsed version of a file's patch.
172
207
:param file_id: The id of the file to generate a patch for.
208
:param invert: If True, provide an inverted patch (insertions displayed
209
as removals, removals displayed as insertions).
173
210
:return: A patches.Patch.
175
old_path = self.target_tree.id2path(file_id)
176
new_path = self.work_tree.id2path(file_id)
177
212
diff_file = StringIO()
178
text_differ = diff.DiffText(self.target_tree, self.work_tree,
214
old_tree = self.work_tree
215
new_tree = self.target_tree
217
old_tree = self.target_tree
218
new_tree = self.work_tree
219
old_path = old_tree.id2path(file_id)
220
new_path = new_tree.id2path(file_id)
221
text_differ = diff.DiffText(old_tree, new_tree, diff_file)
180
222
patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
181
223
diff_file.seek(0)
182
224
return patches.parse_patch(diff_file)
223
265
def handle_modify_text(self, creator, file_id):
224
266
"""Provide diff hunk selection for modified text.
268
If self.reporter.invert_diff is True, the diff is inverted so that
269
insertions are displayed as removals and vice versa.
226
271
:param creator: a ShelfCreator
227
272
:param file_id: The id of the file to shelve.
228
273
:return: number of shelved hunks.
230
target_lines = self.target_tree.get_file_lines(file_id)
275
if self.reporter.invert_diff:
276
target_lines = self.work_tree.get_file_lines(file_id)
278
target_lines = self.target_tree.get_file_lines(file_id)
231
279
textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
232
280
textfile.check_text_lines(target_lines)
233
parsed = self.get_parsed_patch(file_id)
281
parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
235
283
if not self.auto:
237
285
self.diff_writer.write(parsed.get_header())
238
286
for hunk in parsed.hunks:
239
287
self.diff_writer.write(str(hunk))
240
if not self.prompt_bool('Shelve?'):
288
selected = self.prompt_bool(self.reporter.vocab['hunk'])
289
if not self.reporter.invert_diff:
290
selected = (not selected)
241
292
hunk.mod_pos += offset
242
293
final_hunks.append(hunk)
244
295
offset -= (hunk.mod_range - hunk.orig_range)
245
296
sys.stdout.flush()
246
if len(parsed.hunks) == len(final_hunks):
297
if not self.reporter.invert_diff and (
298
len(parsed.hunks) == len(final_hunks)):
300
if self.reporter.invert_diff and len(final_hunks) == 0:
248
302
patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
249
303
creator.shelve_lines(file_id, list(patched))
304
if self.reporter.invert_diff:
305
return len(final_hunks)
250
306
return len(parsed.hunks) - len(final_hunks)