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.prompt_bool('Shelve %d change(s)?' %
126
changes_shelved, auto=self.auto_apply)):
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
old_path = self.target_tree.id2path(file_id)
138
new_path = self.work_tree.id2path(file_id)
139
diff_file = StringIO()
140
text_differ = diff.DiffText(self.target_tree, self.work_tree,
142
patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file')
144
return patches.parse_patch(diff_file)
146
def prompt(self, message):
147
sys.stdout.write(message)
148
char = osutils.getchar()
149
sys.stdout.write("\r" + ' ' * len(message) + '\r')
153
def prompt_bool(self, question, auto=None):
158
char = self.prompt(question + ' [yNfq]')
169
def handle_modify_text(self, creator, file_id):
170
target_lines = self.target_tree.get_file_lines(file_id)
171
work_file = self.work_tree.get_file(file_id)
173
textfile.text_file(work_file)
176
textfile.check_text_lines(target_lines)
177
parsed = self.get_parsed_patch(file_id)
181
self.diff_writer.write(parsed.get_header())
182
for hunk in parsed.hunks:
183
self.diff_writer.write(str(hunk))
184
if not self.prompt_bool('Shelve?'):
185
hunk.mod_pos += offset
186
final_hunks.append(hunk)
188
offset -= (hunk.mod_range - hunk.orig_range)
190
if len(parsed.hunks) == len(final_hunks):
192
patched = patches.iter_patched_from_hunks(target_lines, final_hunks)
193
creator.shelve_lines(file_id, list(patched))
194
return len(parsed.hunks) - len(final_hunks)
197
class Unshelver(object):
200
def from_args(klass, shelf_id=None, action='apply', directory='.'):
201
tree, path = workingtree.WorkingTree.open_containing(directory)
202
manager = tree.get_shelf_manager()
203
if shelf_id is not None:
204
shelf_id = int(shelf_id)
206
shelf_id = manager.last_shelf()
208
raise errors.BzrCommandError('No changes are shelved.')
209
trace.note('Unshelving changes with id "%d".' % shelf_id)
213
if action == 'dry-run':
214
apply_changes = False
216
if action == 'delete-only':
217
apply_changes = False
219
return klass(tree, manager, shelf_id, apply_changes, delete_shelf,
222
def __init__(self, tree, manager, shelf_id, apply_changes=True,
223
delete_shelf=True, read_shelf=True):
225
self.manager = manager
226
self.shelf_id = shelf_id
227
self.apply_changes = apply_changes
228
self.delete_shelf = delete_shelf
229
self.read_shelf = read_shelf
232
self.tree.lock_write()
233
cleanups = [self.tree.unlock]
236
unshelver = self.manager.get_unshelver(self.shelf_id)
237
cleanups.append(unshelver.finalize)
238
if unshelver.message is not None:
239
trace.note('Message: %s' % unshelver.message)
240
change_reporter = delta._ChangeReporter()
241
merger = unshelver.make_merger()
242
merger.change_reporter = change_reporter
243
if self.apply_changes:
244
pb = ui.ui_factory.nested_progress_bar()
250
self.show_changes(merger)
251
if self.delete_shelf:
252
self.manager.delete_shelf(self.shelf_id)
254
for cleanup in reversed(cleanups):
257
def show_changes(self, merger):
258
tree_merger = merger.make_merger()
259
# This implicitly shows the changes via the reporter, so we're done...
260
tt = tree_merger.make_preview_transform()