1
# Copyright (C) 2008 Aaron Bentley <aaron@aaronbentley.com>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
18
from cStringIO import StringIO
36
from bzrlib.plugins.bzrtools import colordiff
41
class Shelver(object):
42
"""Interactively shelve the changes in a working tree."""
44
def __init__(self, work_tree, target_tree, auto=False,
45
auto_apply=False, file_list=None, message=None):
48
:param work_tree: The working tree to shelve changes from.
49
:param target_tree: The "unchanged" / old tree to compare the
51
:param auto: If True, shelve each possible change.
52
:param auto_apply: If True, shelve changes with no final prompt.
53
:param file_list: If supplied, only files in this list may be shelved.
54
:param message: The message to associate with the shelved changes.
56
self.work_tree = work_tree
57
self.target_tree = target_tree
58
if colordiff is not None:
59
self.diff_writer = colordiff.DiffWriter(sys.stdout, False)
61
self.diff_writer = sys.stdout
62
self.manager = work_tree.get_shelf_manager()
64
self.auto_apply = auto_apply
65
self.file_list = file_list
66
self.message = message
69
def from_args(klass, revision=None, all=False, file_list=None,
70
message=None, directory='.'):
71
"""Create a shelver from commandline arguments.
73
:param revision: RevisionSpec of the revision to compare to.
74
:param all: If True, shelve all changes without prompting.
75
:param file_list: If supplied, only files in this list may be shelved.
76
:param message: The message to associate with the shelved changes.
77
:param directory: The directory containing the working tree.
79
tree, path = workingtree.WorkingTree.open_containing(directory)
80
target_tree = builtins._get_one_revision_tree('shelf2', revision,
82
return klass(tree, target_tree, all, all, file_list, message)
85
"""Interactively shelve the changes."""
86
creator = shelf.ShelfCreator(self.work_tree, self.target_tree,
88
self.tempdir = tempfile.mkdtemp()
91
for change in creator.iter_shelvable():
92
if change[0] == 'modify text':
94
changes_shelved += self.handle_modify_text(creator,
96
except errors.BinaryFile:
97
if self.prompt_bool('Shelve binary changes?'):
99
creator.shelve_content_change(change[1])
100
if change[0] == 'add file':
101
if self.prompt_bool('Shelve adding file "%s"?'
103
creator.shelve_creation(change[1])
105
if change[0] == 'delete file':
106
if self.prompt_bool('Shelve removing file "%s"? '
108
creator.shelve_deletion(change[1])
110
if change[0] == 'change kind':
111
if self.prompt_bool('Shelve changing "%s" from %s to %s? '
112
% (change[4], change[2], change[3])):
113
creator.shelve_content_change(change[1])
115
if change[0] == 'rename':
116
if self.prompt_bool('Shelve renaming %s => %s?' %
118
creator.shelve_rename(change[1])
120
if changes_shelved > 0:
121
trace.note("Selected changes:")
122
changes = creator.work_transform.iter_changes()
123
reporter = delta._ChangeReporter()
124
delta.report_changes(changes, reporter)
125
if (self.auto_apply or self.prompt_bool(
126
'Shelve %d change(s)?' % changes_shelved)):
127
shelf_id = self.manager.shelve_changes(creator,
129
trace.note('Changes shelved with id "%d".' % shelf_id)
131
trace.warning('No changes to shelve.')
133
shutil.rmtree(self.tempdir)
136
def get_parsed_patch(self, file_id):
137
"""Return a parsed version of a file's patch.
139
:param file_id: The id of the file to generate a patch for.
140
:return: A patches.Patch.
142
old_path = self.target_tree.id2path(file_id)
143
new_path = self.work_tree.id2path(file_id)
144
diff_file = StringIO()
145
text_differ = diff.DiffText(self.target_tree, self.work_tree,
147
patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
149
return patches.parse_patch(diff_file)
151
def prompt(self, message):
152
"""Prompt the user for a character.
154
:param message: The message to prompt a user with.
155
:return: A character.
157
sys.stdout.write(message)
158
char = osutils.getchar()
159
sys.stdout.write("\r" + ' ' * len(message) + '\r')
163
def prompt_bool(self, question):
164
"""Prompt the user with a yes/no question.
166
This may be overridden by self.auto, or auto may be supplied.
167
It may also *set* self.auto. It may also raise SystemExit.
168
:param question: The question to ask the user.
169
:return: True or False
173
char = self.prompt(question + ' [yNfq]')
184
def handle_modify_text(self, creator, file_id):
185
"""Provide diff hunk selection for modified text.
187
:param creator: a ShelfCreator
188
:param file_id: The id of the file to shelve.
189
:return: number of shelved hunks.
191
target_lines = self.target_tree.get_file_lines(file_id)
192
work_file = self.work_tree.get_file(file_id)
194
textfile.text_file(work_file)
197
textfile.check_text_lines(target_lines)
198
parsed = self.get_parsed_patch(file_id)
202
self.diff_writer.write(parsed.get_header())
203
for hunk in parsed.hunks:
204
self.diff_writer.write(str(hunk))
205
if not self.prompt_bool('Shelve?'):
206
hunk.mod_pos += offset
207
final_hunks.append(hunk)
209
offset -= (hunk.mod_range - hunk.orig_range)
211
if len(parsed.hunks) == len(final_hunks):
213
patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
214
creator.shelve_lines(file_id, list(patched))
215
return len(parsed.hunks) - len(final_hunks)
218
class Unshelver(object):
219
"""Unshelve changes into a working tree."""
222
def from_args(klass, shelf_id=None, action='apply', directory='.'):
223
"""Create an unshelver from commandline arguments.
225
:param shelf_id: Integer id of the shelf, as a string.
226
:param action: action to perform. May be 'apply', 'dry-run',
228
:param directory: The directory to unshelve changes into.
230
tree, path = workingtree.WorkingTree.open_containing(directory)
231
manager = tree.get_shelf_manager()
232
if shelf_id is not None:
233
shelf_id = int(shelf_id)
235
shelf_id = manager.last_shelf()
237
raise errors.BzrCommandError('No changes are shelved.')
238
trace.note('Unshelving changes with id "%d".' % shelf_id)
242
if action == 'dry-run':
243
apply_changes = False
245
if action == 'delete-only':
246
apply_changes = False
248
return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
251
def __init__(self, tree, manager, shelf_id, apply_changes=True,
252
delete_shelf=True, read_shelf=True):
255
:param tree: The working tree to unshelve into.
256
:param manager: The ShelveManager containing the shelved changes.
258
:param apply_changes: If True, apply the shelved changes to the
260
:param delete_shelf: If True, delete the changes from the shelf.
261
:param read_shelf: If True, read the changes from the shelf.
264
manager = tree.get_shelf_manager()
265
self.manager = manager
266
self.shelf_id = shelf_id
267
self.apply_changes = apply_changes
268
self.delete_shelf = delete_shelf
269
self.read_shelf = read_shelf
272
"""Perform the unshelving operation."""
273
self.tree.lock_write()
274
cleanups = [self.tree.unlock]
277
unshelver = self.manager.get_unshelver(self.shelf_id)
278
cleanups.append(unshelver.finalize)
279
if unshelver.message is not None:
280
trace.note('Message: %s' % unshelver.message)
281
change_reporter = delta._ChangeReporter()
282
merger = unshelver.make_merger()
283
merger.change_reporter = change_reporter
284
if self.apply_changes:
285
pb = ui.ui_factory.nested_progress_bar()
291
self.show_changes(merger)
292
if self.delete_shelf:
293
self.manager.delete_shelf(self.shelf_id)
295
for cleanup in reversed(cleanups):
298
def show_changes(self, merger):
299
"""Show the changes that this operation specifies."""
300
tree_merger = merger.make_merger()
301
# This implicitly shows the changes via the reporter, so we're done...
302
tt = tree_merger.make_preview_transform()