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
30
from bzrlib.util import bencode
33
class ShelfCreator(object):
34
"""Create a transform to shelve objects and its inverse."""
36
def __init__(self, work_tree, target_tree, file_list=None):
39
:param work_tree: The working tree to apply changes to
40
:param target_tree: The tree to make the working tree more similar to.
41
:param file_list: The files to make more similar to the target.
43
self.work_tree = work_tree
44
self.work_transform = transform.TreeTransform(work_tree)
45
self.target_tree = target_tree
46
self.shelf_transform = transform.TransformPreview(self.target_tree)
50
self.iter_changes = work_tree.iter_changes(self.target_tree,
51
specific_files=file_list)
53
def iter_shelvable(self):
54
"""Iterable of tuples describing shelvable changes.
56
As well as generating the tuples, this updates several members.
58
('add file', file_id, work_kind, work_path)
59
('delete file', file_id, target_kind, target_path)
60
('rename', file_id, target_path, work_path)
61
('change kind', file_id, target_kind, work_kind, target_path)
62
('modify text', file_id)
64
for (file_id, paths, changed, versioned, parents, names, kind,
65
executable) in self.iter_changes:
66
if kind[0] is None or versioned[0] == False:
67
self.creation[file_id] = (kind[1], names[1], parents[1],
69
yield ('add file', file_id, kind[1], paths[1])
70
elif kind[1] is None or versioned[0] == False:
71
self.deletion[file_id] = (kind[0], names[0], parents[0],
73
yield ('delete file', file_id, kind[0], paths[0])
75
if names[0] != names[1] or parents[0] != parents[1]:
76
self.renames[file_id] = (names, parents)
77
yield ('rename', file_id) + paths
79
if kind[0] != kind [1]:
80
yield ('change kind', file_id, kind[0], kind[1], paths[0])
82
yield ('modify text', file_id)
84
def shelve_rename(self, file_id):
85
"""Shelve a file rename.
87
:param file_id: The file id of the file to shelve the renaming of.
89
names, parents = self.renames[file_id]
90
w_trans_id = self.work_transform.trans_id_file_id(file_id)
91
work_parent = self.work_transform.trans_id_file_id(parents[0])
92
self.work_transform.adjust_path(names[0], work_parent, w_trans_id)
94
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
95
shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
96
self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
98
def shelve_lines(self, file_id, new_lines):
99
"""Shelve text changes to a file, using provided lines.
101
:param file_id: The file id of the file to shelve the text of.
102
:param new_lines: The lines that the file should have due to shelving.
104
w_trans_id = self.work_transform.trans_id_file_id(file_id)
105
self.work_transform.delete_contents(w_trans_id)
106
self.work_transform.create_file(new_lines, w_trans_id)
108
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
109
self.shelf_transform.delete_contents(s_trans_id)
110
inverse_lines = self._inverse_lines(new_lines, file_id)
111
self.shelf_transform.create_file(inverse_lines, s_trans_id)
114
def _content_from_tree(tt, tree, file_id):
115
trans_id = tt.trans_id_file_id(file_id)
116
tt.delete_contents(trans_id)
117
transform.create_from_tree(tt, trans_id, tree, file_id)
119
def shelve_content_change(self, file_id):
120
"""Shelve a kind change or binary file content change.
122
:param file_id: The file id of the file to shelve the content change
125
self._content_from_tree(self.work_transform, self.target_tree, file_id)
126
self._content_from_tree(self.shelf_transform, self.work_tree, file_id)
128
def shelve_creation(self, file_id):
129
"""Shelve creation of a file.
131
This handles content and inventory id.
132
:param file_id: The file_id of the file to shelve creation of.
134
kind, name, parent, versioned = self.creation[file_id]
135
version = not versioned[0]
136
self._shelve_creation(self.work_tree, file_id, self.work_transform,
137
self.shelf_transform, kind, name, parent,
140
def shelve_deletion(self, file_id):
141
"""Shelve deletion of a file.
143
This handles content and inventory id.
144
:param file_id: The file_id of the file to shelve deletion of.
146
kind, name, parent, versioned = self.deletion[file_id]
147
existing_path = self.target_tree.id2path(file_id)
148
if not self.work_tree.has_filename(existing_path):
150
version = not versioned[1]
151
self._shelve_creation(self.target_tree, file_id, self.shelf_transform,
152
self.work_transform, kind, name, parent,
153
version, existing_path=existing_path)
155
def _shelve_creation(self, tree, file_id, from_transform, to_transform,
156
kind, name, parent, version, existing_path=None):
157
w_trans_id = from_transform.trans_id_file_id(file_id)
158
if parent is not None and kind is not None:
159
from_transform.delete_contents(w_trans_id)
160
from_transform.unversion_file(w_trans_id)
162
if existing_path is not None:
163
s_trans_id = to_transform.trans_id_tree_path(existing_path)
165
s_trans_id = to_transform.trans_id_file_id(file_id)
166
if parent is not None:
167
s_parent_id = to_transform.trans_id_file_id(parent)
168
to_transform.adjust_path(name, s_parent_id, s_trans_id)
169
if existing_path is None:
171
to_transform.create_file('', s_trans_id)
173
transform.create_from_tree(to_transform, s_trans_id,
176
to_transform.version_file(file_id, s_trans_id)
178
def read_tree_lines(self, tree, file_id):
179
"""Read text lines from a tree.
181
(Tree.get_file_lines is not an official API)
183
return osutils.split_lines(tree.get_file_text(file_id))
185
def _inverse_lines(self, new_lines, file_id):
186
"""Produce a version with only those changes removed from new_lines."""
187
target_lines = self.target_tree.get_file_lines(file_id)
188
work_lines = self.read_tree_lines(self.work_tree, file_id)
189
return merge3.Merge3(new_lines, target_lines, work_lines).merge_lines()
192
"""Release all resources used by this ShelfCreator."""
193
self.work_transform.finalize()
194
self.shelf_transform.finalize()
197
"""Shelve changes from working tree."""
198
self.work_transform.apply()
200
def write_shelf(self, shelf_file, message=None):
201
"""Serialize the shelved changes to a file.
203
:param shelf_file: A file-like object to write the shelf to.
204
:param message: An optional message describing the shelved changes.
205
:return: the filename of the written file.
207
transform.resolve_conflicts(self.shelf_transform)
208
serializer = pack.ContainerSerialiser()
209
shelf_file.write(serializer.begin())
211
'revision_id': self.target_tree.get_revision_id(),
213
if message is not None:
214
metadata['message'] = message.encode('utf-8')
215
shelf_file.write(serializer.bytes_record(
216
bencode.bencode(metadata), (('metadata',),)))
217
for bytes in self.shelf_transform.serialize(serializer):
218
shelf_file.write(bytes)
219
shelf_file.write(serializer.end())
222
class Unshelver(object):
223
"""Unshelve shelved changes."""
225
def __init__(self, tree, base_tree, transform, message):
228
:param tree: The tree to apply the changes to.
229
:param base_tree: The basis to apply the tranform to.
230
:param message: A message from the shelved transform.
233
self.base_tree = base_tree
234
self.transform = transform
235
self.message = message
238
def from_tree_and_shelf(klass, tree, shelf_file):
239
"""Create an Unshelver from a tree and a shelf file.
241
:param tree: The tree to apply shelved changes to.
242
:param shelf_file: A file-like object containing shelved changes.
243
:return: The Unshelver.
245
parser = pack.ContainerPushParser()
246
parser.accept_bytes(shelf_file.read())
247
records = iter(parser.read_pending_records())
248
names, metadata_bytes = records.next()
249
if names[0] != ('metadata',):
250
raise errors.ShelfCorrupt
251
metadata = bencode.bdecode(metadata_bytes)
252
base_revision_id = metadata['revision_id']
253
message = metadata.get('message')
254
if message is not None:
255
message = message.decode('utf-8')
257
base_tree = tree.revision_tree(base_revision_id)
258
except errors.NoSuchRevisionInTree:
259
base_tree = tree.branch.repository.revision_tree(base_revision_id)
260
tt = transform.TransformPreview(base_tree)
261
tt.deserialize(records)
262
return klass(tree, base_tree, tt, message)
264
def make_merger(self):
265
"""Return a merger that can unshelve the changes."""
266
pb = ui.ui_factory.nested_progress_bar()
268
target_tree = self.transform.get_preview_tree()
269
merger = merge.Merger.from_uncommitted(self.tree, target_tree, pb,
271
merger.merge_type = merge.Merge3Merger
277
"""Release all resources held by this Unshelver."""
278
self.transform.finalize()
281
class ShelfManager(object):
282
"""Maintain a list of shelved changes."""
284
def __init__(self, tree, transport):
286
self.transport = transport.clone('shelf')
287
self.transport.ensure_base()
290
"""Return a file object and id for a new set of shelved changes."""
291
last_shelf = self.last_shelf()
292
if last_shelf is None:
295
next_shelf = last_shelf + 1
296
shelf_file = open(self.transport.local_abspath(str(next_shelf)), 'wb')
297
return next_shelf, shelf_file
299
def shelve_changes(self, creator, message=None):
300
"""Store the changes in a ShelfCreator on a shelf."""
301
next_shelf, shelf_file = self.new_shelf()
303
creator.write_shelf(shelf_file, message)
309
def read_shelf(self, shelf_id):
310
"""Return the file associated with a shelf_id for reading.
312
:param shelf_id: The id of the shelf to retrive the file for.
315
return open(self.transport.local_abspath(str(shelf_id)), 'rb')
317
if e.errno != errno.ENOENT:
319
from bzrlib import errors
320
raise errors.NoSuchShelfId(shelf_id)
322
def get_unshelver(self, shelf_id):
323
"""Return an unshelver for a given shelf_id.
325
:param shelf_id: The shelf id to return the unshelver for.
327
shelf_file = self.read_shelf(shelf_id)
329
return Unshelver.from_tree_and_shelf(self.tree, shelf_file)
333
def delete_shelf(self, shelf_id):
334
"""Delete the shelved changes for a given id.
336
:param shelf_id: id of the shelved changes to delete.
338
self.transport.delete(str(shelf_id))
340
def active_shelves(self):
341
"""Return a list of shelved changes."""
342
return [int(f) for f in self.transport.list_dir('.')]
344
def last_shelf(self):
345
"""Return the id of the last-created shelved change."""
346
active = self.active_shelves()