1
# Copyright (C) 2008 Canonical Ltd
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
38
# Plugins may want to override the following
39
diff_writer_factory = None
42
class Shelver(object):
43
"""Interactively shelve the changes in a working tree."""
45
def __init__(self, work_tree, target_tree, auto=False,
46
auto_apply=False, file_list=None, message=None):
49
:param work_tree: The working tree to shelve changes from.
50
:param target_tree: The "unchanged" / old tree to compare the
52
:param auto: If True, shelve each possible change.
53
:param auto_apply: If True, shelve changes with no final prompt.
54
:param file_list: If supplied, only files in this list may be shelved.
55
:param message: The message to associate with the shelved changes.
57
self.work_tree = work_tree
58
self.target_tree = target_tree
59
if diff_writer_factory is not None:
60
self.diff_writer = diff_writer_factory(target=sys.stdout,
63
self.diff_writer = sys.stdout
64
self.manager = work_tree.get_shelf_manager()
66
self.auto_apply = auto_apply
67
self.file_list = file_list
68
self.message = message
71
def from_args(klass, revision=None, all=False, file_list=None,
72
message=None, directory='.'):
73
"""Create a shelver from commandline arguments.
75
:param revision: RevisionSpec of the revision to compare to.
76
:param all: If True, shelve all changes without prompting.
77
:param file_list: If supplied, only files in this list may be shelved.
78
:param message: The message to associate with the shelved changes.
79
:param directory: The directory containing the working tree.
81
tree, path = workingtree.WorkingTree.open_containing(directory)
82
target_tree = builtins._get_one_revision_tree('shelf2', revision,
84
return klass(tree, target_tree, all, all, file_list, message)
87
"""Interactively shelve the changes."""
88
creator = shelf.ShelfCreator(self.work_tree, self.target_tree,
90
self.tempdir = tempfile.mkdtemp()
93
for change in creator.iter_shelvable():
94
if change[0] == 'modify text':
96
changes_shelved += self.handle_modify_text(creator,
98
except errors.BinaryFile:
99
if self.prompt_bool('Shelve binary changes?'):
101
creator.shelve_content_change(change[1])
102
if change[0] == 'add file':
103
if self.prompt_bool('Shelve adding file "%s"?'
105
creator.shelve_creation(change[1])
107
if change[0] == 'delete file':
108
if self.prompt_bool('Shelve removing file "%s"?'
110
creator.shelve_deletion(change[1])
112
if change[0] == 'change kind':
113
if self.prompt_bool('Shelve changing "%s" from %s to %s? '
114
% (change[4], change[2], change[3])):
115
creator.shelve_content_change(change[1])
117
if change[0] == 'rename':
118
if self.prompt_bool('Shelve renaming "%s" => "%s"?' %
120
creator.shelve_rename(change[1])
122
if changes_shelved > 0:
123
trace.note("Selected changes:")
124
changes = creator.work_transform.iter_changes()
125
reporter = delta._ChangeReporter()
126
delta.report_changes(changes, reporter)
127
if (self.auto_apply or self.prompt_bool(
128
'Shelve %d change(s)?' % changes_shelved)):
129
shelf_id = self.manager.shelve_changes(creator,
131
trace.note('Changes shelved with id "%d".' % shelf_id)
133
trace.warning('No changes to shelve.')
135
shutil.rmtree(self.tempdir)
138
def get_parsed_patch(self, file_id):
139
"""Return a parsed version of a file's patch.
141
:param file_id: The id of the file to generate a patch for.
142
:return: A patches.Patch.
144
old_path = self.target_tree.id2path(file_id)
145
new_path = self.work_tree.id2path(file_id)
146
diff_file = StringIO()
147
text_differ = diff.DiffText(self.target_tree, self.work_tree,
149
patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
151
return patches.parse_patch(diff_file)
153
def prompt(self, message):
154
"""Prompt the user for a character.
156
:param message: The message to prompt a user with.
157
:return: A character.
159
sys.stdout.write(message)
160
char = osutils.getchar()
161
sys.stdout.write("\r" + ' ' * len(message) + '\r')
165
def prompt_bool(self, question):
166
"""Prompt the user with a yes/no question.
168
This may be overridden by self.auto. It may also *set* self.auto. It
169
may also raise UserAbort.
170
:param question: The question to ask the user.
171
:return: True or False
175
char = self.prompt(question + ' [yNfq]')
182
raise errors.UserAbort()
186
def handle_modify_text(self, creator, file_id):
187
"""Provide diff hunk selection for modified text.
189
:param creator: a ShelfCreator
190
:param file_id: The id of the file to shelve.
191
:return: number of shelved hunks.
193
target_lines = self.target_tree.get_file_lines(file_id)
194
textfile.check_text_lines(self.work_tree.get_file_lines(file_id))
195
textfile.check_text_lines(target_lines)
196
parsed = self.get_parsed_patch(file_id)
200
self.diff_writer.write(parsed.get_header())
201
for hunk in parsed.hunks:
202
self.diff_writer.write(str(hunk))
203
if not self.prompt_bool('Shelve?'):
204
hunk.mod_pos += offset
205
final_hunks.append(hunk)
207
offset -= (hunk.mod_range - hunk.orig_range)
209
if len(parsed.hunks) == len(final_hunks):
211
patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
212
creator.shelve_lines(file_id, list(patched))
213
return len(parsed.hunks) - len(final_hunks)
216
class Unshelver(object):
217
"""Unshelve changes into a working tree."""
220
def from_args(klass, shelf_id=None, action='apply', directory='.'):
221
"""Create an unshelver from commandline arguments.
223
:param shelf_id: Integer id of the shelf, as a string.
224
:param action: action to perform. May be 'apply', 'dry-run',
226
:param directory: The directory to unshelve changes into.
228
tree, path = workingtree.WorkingTree.open_containing(directory)
229
manager = tree.get_shelf_manager()
230
if shelf_id is not None:
231
shelf_id = int(shelf_id)
233
shelf_id = manager.last_shelf()
235
raise errors.BzrCommandError('No changes are shelved.')
236
trace.note('Unshelving changes with id "%d".' % shelf_id)
240
if action == 'dry-run':
241
apply_changes = False
243
if action == 'delete-only':
244
apply_changes = False
246
return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
249
def __init__(self, tree, manager, shelf_id, apply_changes=True,
250
delete_shelf=True, read_shelf=True):
253
:param tree: The working tree to unshelve into.
254
:param manager: The ShelveManager containing the shelved changes.
256
:param apply_changes: If True, apply the shelved changes to the
258
:param delete_shelf: If True, delete the changes from the shelf.
259
:param read_shelf: If True, read the changes from the shelf.
262
manager = tree.get_shelf_manager()
263
self.manager = manager
264
self.shelf_id = shelf_id
265
self.apply_changes = apply_changes
266
self.delete_shelf = delete_shelf
267
self.read_shelf = read_shelf
270
"""Perform the unshelving operation."""
271
self.tree.lock_write()
272
cleanups = [self.tree.unlock]
275
unshelver = self.manager.get_unshelver(self.shelf_id)
276
cleanups.append(unshelver.finalize)
277
if unshelver.message is not None:
278
trace.note('Message: %s' % unshelver.message)
279
change_reporter = delta._ChangeReporter()
280
merger = unshelver.make_merger()
281
merger.change_reporter = change_reporter
282
if self.apply_changes:
283
pb = ui.ui_factory.nested_progress_bar()
289
self.show_changes(merger)
290
if self.delete_shelf:
291
self.manager.delete_shelf(self.shelf_id)
293
for cleanup in reversed(cleanups):
296
def show_changes(self, merger):
297
"""Show the changes that this operation specifies."""
298
tree_merger = merger.make_merger()
299
# This implicitly shows the changes via the reporter, so we're done...
300
tt = tree_merger.make_preview_transform()