13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
from cStringIO import StringIO
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" =>'
56
self.delta_reporter = delta._ChangeReporter()
59
"""Report that no changes were selected to apply."""
60
trace.warning('No changes to shelve.')
62
def shelved_id(self, shelf_id):
63
"""Report the id changes were shelved to."""
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.')
70
def selected_changes(self, transform):
71
"""Report the changes that were selected."""
72
trace.note("Selected changes:")
73
changes = transform.iter_changes()
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):
38
110
class Shelver(object):
39
111
"""Interactively shelve the changes in a working tree."""
41
113
def __init__(self, work_tree, target_tree, diff_writer=None, auto=False,
42
auto_apply=False, file_list=None, message=None):
114
auto_apply=False, file_list=None, message=None,
115
destroy=False, manager=None, reporter=None):
45
118
:param work_tree: The working tree to shelve changes from.
49
122
:param auto_apply: If True, shelve changes with no final prompt.
50
123
:param file_list: If supplied, only files in this list may be shelved.
51
124
:param message: The message to associate with the shelved changes.
125
:param destroy: Change the working tree without storing the shelved
127
:param manager: The shelf manager to use.
128
:param reporter: Object for reporting changes to user.
53
130
self.work_tree = work_tree
54
131
self.target_tree = target_tree
55
132
self.diff_writer = diff_writer
56
133
if self.diff_writer is None:
57
134
self.diff_writer = sys.stdout
58
self.manager = work_tree.get_shelf_manager()
136
manager = work_tree.get_shelf_manager()
137
self.manager = manager
60
139
self.auto_apply = auto_apply
61
140
self.file_list = file_list
62
141
self.message = message
142
self.destroy = destroy
144
reporter = ShelfReporter()
145
self.reporter = reporter
65
148
def from_args(klass, diff_writer, revision=None, all=False, file_list=None,
66
message=None, directory='.'):
149
message=None, directory='.', destroy=False):
67
150
"""Create a shelver from commandline arguments.
69
152
:param revision: RevisionSpec of the revision to compare to.
71
154
:param file_list: If supplied, only files in this list may be shelved.
72
155
:param message: The message to associate with the shelved changes.
73
156
:param directory: The directory containing the working tree.
157
:param destroy: Change the working tree without storing the shelved
75
160
tree, path = workingtree.WorkingTree.open_containing(directory)
76
161
target_tree = builtins._get_one_revision_tree('shelf2', revision,
77
162
tree.branch, tree)
78
163
files = builtins.safe_relpath_files(tree, file_list)
79
return klass(tree, target_tree, diff_writer, all, all, files, message)
164
return klass(tree, target_tree, diff_writer, all, all, files, message,
82
168
"""Interactively shelve the changes."""
91
177
changes_shelved += self.handle_modify_text(creator,
93
179
except errors.BinaryFile:
94
if self.prompt_bool('Shelve binary changes?'):
180
if self.prompt_bool(self.reporter.vocab['binary']):
95
181
changes_shelved += 1
96
182
creator.shelve_content_change(change[1])
97
if change[0] == 'add file':
98
if self.prompt_bool('Shelve adding file "%s"?'
100
creator.shelve_creation(change[1])
102
if change[0] == 'delete file':
103
if self.prompt_bool('Shelve removing file "%s"?'
105
creator.shelve_deletion(change[1])
107
if change[0] == 'change kind':
108
if self.prompt_bool('Shelve changing "%s" from %s to %s? '
109
% (change[4], change[2], change[3])):
110
creator.shelve_content_change(change[1])
112
if change[0] == 'rename':
113
if self.prompt_bool('Shelve renaming "%s" => "%s"?' %
115
creator.shelve_rename(change[1])
184
if self.prompt_bool(self.reporter.prompt_change(change)):
185
creator.shelve_change(change)
116
186
changes_shelved += 1
117
187
if changes_shelved > 0:
118
trace.note("Selected changes:")
119
changes = creator.work_transform.iter_changes()
120
reporter = delta._ChangeReporter()
121
delta.report_changes(changes, reporter)
188
self.reporter.selected_changes(creator.work_transform)
122
189
if (self.auto_apply or self.prompt_bool(
123
'Shelve %d change(s)?' % changes_shelved)):
124
shelf_id = self.manager.shelve_changes(creator,
126
trace.note('Changes shelved with id "%d".' % shelf_id)
190
self.reporter.vocab['final'] % changes_shelved)):
193
self.reporter.changes_destroyed()
195
shelf_id = self.manager.shelve_changes(creator,
197
self.reporter.shelved_id(shelf_id)
128
trace.warning('No changes to shelve.')
199
self.reporter.no_changes()
130
201
shutil.rmtree(self.tempdir)
131
202
creator.finalize()
133
def get_parsed_patch(self, file_id):
204
def get_parsed_patch(self, file_id, invert=False):
134
205
"""Return a parsed version of a file's patch.
136
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).
137
210
:return: A patches.Patch.
139
old_path = self.target_tree.id2path(file_id)
140
new_path = self.work_tree.id2path(file_id)
141
212
diff_file = StringIO()
142
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)
144
222
patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
145
223
diff_file.seek(0)
146
224
return patches.parse_patch(diff_file)
187
265
def handle_modify_text(self, creator, file_id):
188
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.
190
271
:param creator: a ShelfCreator
191
272
:param file_id: The id of the file to shelve.
192
273
:return: number of shelved hunks.
194
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)
195
279
textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
196
280
textfile.check_text_lines(target_lines)
197
parsed = self.get_parsed_patch(file_id)
281
parsed = self.get_parsed_patch(file_id, self.reporter.invert_diff)
199
283
if not self.auto:
201
285
self.diff_writer.write(parsed.get_header())
202
286
for hunk in parsed.hunks:
203
287
self.diff_writer.write(str(hunk))
204
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)
205
292
hunk.mod_pos += offset
206
293
final_hunks.append(hunk)
208
295
offset -= (hunk.mod_range - hunk.orig_range)
209
296
sys.stdout.flush()
210
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:
212
302
patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
213
303
creator.shelve_lines(file_id, list(patched))
304
if self.reporter.invert_diff:
305
return len(final_hunks)
214
306
return len(parsed.hunks) - len(final_hunks)
281
373
if unshelver.message is not None:
282
374
trace.note('Message: %s' % unshelver.message)
283
375
change_reporter = delta._ChangeReporter()
284
merger = unshelver.make_merger()
285
merger.change_reporter = change_reporter
286
if self.apply_changes:
287
pb = ui.ui_factory.nested_progress_bar()
376
task = ui.ui_factory.nested_progress_bar()
378
merger = unshelver.make_merger(task)
379
merger.change_reporter = change_reporter
380
if self.apply_changes:
289
381
merger.do_merge()
293
self.show_changes(merger)
383
self.show_changes(merger)
294
386
if self.delete_shelf:
295
387
self.manager.delete_shelf(self.shelf_id)