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
31
from bzrlib.util import bencode
34
class ShelfCreator(object):
35
"""Create a transform to shelve objects and its inverse."""
37
def __init__(self, work_tree, target_tree, file_list=None):
40
:param work_tree: The working tree to apply changes to
41
:param target_tree: The tree to make the working tree more similar to.
42
:param file_list: The files to make more similar to the target.
44
self.work_tree = work_tree
45
self.work_transform = transform.TreeTransform(work_tree)
46
self.target_tree = target_tree
47
self.shelf_transform = transform.TransformPreview(self.target_tree)
52
self.iter_changes = work_tree.iter_changes(
53
self.target_tree, specific_files=file_list)
57
def iter_shelvable(self):
58
"""Iterable of tuples describing shelvable changes.
60
As well as generating the tuples, this updates several members.
62
('add file', file_id, work_kind, work_path)
63
('delete file', file_id, target_kind, target_path)
64
('rename', file_id, target_path, work_path)
65
('change kind', file_id, target_kind, work_kind, target_path)
66
('modify text', file_id)
68
for (file_id, paths, changed, versioned, parents, names, kind,
69
executable) in self.iter_changes:
70
if kind[0] is None or versioned[0] == False:
71
self.creation[file_id] = (kind[1], names[1], parents[1],
73
yield ('add file', file_id, kind[1], paths[1])
74
elif kind[1] is None or versioned[0] == False:
75
self.deletion[file_id] = (kind[0], names[0], parents[0],
77
yield ('delete file', file_id, kind[0], paths[0])
79
if names[0] != names[1] or parents[0] != parents[1]:
80
self.renames[file_id] = (names, parents)
81
yield ('rename', file_id) + paths
83
if kind[0] != kind [1]:
84
yield ('change kind', file_id, kind[0], kind[1], paths[0])
86
yield ('modify text', file_id)
88
def shelve_rename(self, file_id):
89
"""Shelve a file rename.
91
:param file_id: The file id of the file to shelve the renaming of.
93
names, parents = self.renames[file_id]
94
w_trans_id = self.work_transform.trans_id_file_id(file_id)
95
work_parent = self.work_transform.trans_id_file_id(parents[0])
96
self.work_transform.adjust_path(names[0], work_parent, w_trans_id)
98
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
99
shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
100
self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
102
def shelve_lines(self, file_id, new_lines):
103
"""Shelve text changes to a file, using provided lines.
105
:param file_id: The file id of the file to shelve the text of.
106
:param new_lines: The lines that the file should have due to shelving.
108
w_trans_id = self.work_transform.trans_id_file_id(file_id)
109
self.work_transform.delete_contents(w_trans_id)
110
self.work_transform.create_file(new_lines, w_trans_id)
112
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
113
self.shelf_transform.delete_contents(s_trans_id)
114
inverse_lines = self._inverse_lines(new_lines, file_id)
115
self.shelf_transform.create_file(inverse_lines, s_trans_id)
118
def _content_from_tree(tt, tree, file_id):
119
trans_id = tt.trans_id_file_id(file_id)
120
tt.delete_contents(trans_id)
121
transform.create_from_tree(tt, trans_id, tree, file_id)
123
def shelve_content_change(self, file_id):
124
"""Shelve a kind change or binary file content change.
126
:param file_id: The file id of the file to shelve the content change
129
self._content_from_tree(self.work_transform, self.target_tree, file_id)
130
self._content_from_tree(self.shelf_transform, self.work_tree, file_id)
132
def shelve_creation(self, file_id):
133
"""Shelve creation of a file.
135
This handles content and inventory id.
136
:param file_id: The file_id of the file to shelve creation of.
138
kind, name, parent, versioned = self.creation[file_id]
139
version = not versioned[0]
140
self._shelve_creation(self.work_tree, file_id, self.work_transform,
141
self.shelf_transform, kind, name, parent,
144
def shelve_deletion(self, file_id):
145
"""Shelve deletion of a file.
147
This handles content and inventory id.
148
:param file_id: The file_id of the file to shelve deletion of.
150
kind, name, parent, versioned = self.deletion[file_id]
151
existing_path = self.target_tree.id2path(file_id)
152
if not self.work_tree.has_filename(existing_path):
154
version = not versioned[1]
155
self._shelve_creation(self.target_tree, file_id, self.shelf_transform,
156
self.work_transform, kind, name, parent,
157
version, existing_path=existing_path)
159
def _shelve_creation(self, tree, file_id, from_transform, to_transform,
160
kind, name, parent, version, existing_path=None):
161
w_trans_id = from_transform.trans_id_file_id(file_id)
162
if parent is not None and kind is not None:
163
from_transform.delete_contents(w_trans_id)
164
from_transform.unversion_file(w_trans_id)
166
if existing_path is not None:
167
s_trans_id = to_transform.trans_id_tree_path(existing_path)
169
s_trans_id = to_transform.trans_id_file_id(file_id)
170
if parent is not None:
171
s_parent_id = to_transform.trans_id_file_id(parent)
172
to_transform.adjust_path(name, s_parent_id, s_trans_id)
173
if existing_path is None:
175
to_transform.create_file('', s_trans_id)
177
transform.create_from_tree(to_transform, s_trans_id,
180
to_transform.version_file(file_id, s_trans_id)
182
def _inverse_lines(self, new_lines, file_id):
183
"""Produce a version with only those changes removed from new_lines."""
184
target_lines = self.target_tree.get_file_lines(file_id)
185
work_lines = self.work_tree.get_file_lines(file_id)
186
return merge3.Merge3(new_lines, target_lines, work_lines).merge_lines()
189
"""Release all resources used by this ShelfCreator."""
190
self.work_transform.finalize()
191
self.shelf_transform.finalize()
194
"""Shelve changes from working tree."""
195
self.work_transform.apply()
197
def write_shelf(self, shelf_file, message=None):
198
"""Serialize the shelved changes to a file.
200
:param shelf_file: A file-like object to write the shelf to.
201
:param message: An optional message describing the shelved changes.
202
:return: the filename of the written file.
204
transform.resolve_conflicts(self.shelf_transform)
205
serializer = pack.ContainerSerialiser()
206
shelf_file.write(serializer.begin())
208
'revision_id': self.target_tree.get_revision_id(),
210
if message is not None:
211
metadata['message'] = message.encode('utf-8')
212
shelf_file.write(serializer.bytes_record(
213
bencode.bencode(metadata), (('metadata',),)))
214
for bytes in self.shelf_transform.serialize(serializer):
215
shelf_file.write(bytes)
216
shelf_file.write(serializer.end())
219
class Unshelver(object):
220
"""Unshelve shelved changes."""
222
def __init__(self, tree, base_tree, transform, message):
225
:param tree: The tree to apply the changes to.
226
:param base_tree: The basis to apply the tranform to.
227
:param message: A message from the shelved transform.
230
self.base_tree = base_tree
231
self.transform = transform
232
self.message = message
235
def iter_records(shelf_file):
236
parser = pack.ContainerPushParser()
237
parser.accept_bytes(shelf_file.read())
238
return iter(parser.read_pending_records())
241
def parse_metadata(records):
242
names, metadata_bytes = records.next()
243
if names[0] != ('metadata',):
244
raise errors.ShelfCorrupt
245
metadata = bencode.bdecode(metadata_bytes)
246
message = metadata.get('message')
247
if message is not None:
248
metadata['message'] = message.decode('utf-8')
252
def from_tree_and_shelf(klass, tree, shelf_file):
253
"""Create an Unshelver from a tree and a shelf file.
255
:param tree: The tree to apply shelved changes to.
256
:param shelf_file: A file-like object containing shelved changes.
257
:return: The Unshelver.
259
records = klass.iter_records(shelf_file)
260
metadata = klass.parse_metadata(records)
261
base_revision_id = metadata['revision_id']
263
base_tree = tree.revision_tree(base_revision_id)
264
except errors.NoSuchRevisionInTree:
265
base_tree = tree.branch.repository.revision_tree(base_revision_id)
266
tt = transform.TransformPreview(base_tree)
267
tt.deserialize(records)
268
return klass(tree, base_tree, tt, metadata.get('message'))
270
def make_merger(self, task=None):
271
"""Return a merger that can unshelve the changes."""
272
target_tree = self.transform.get_preview_tree()
273
merger = merge.Merger.from_uncommitted(self.tree, target_tree,
274
task, self.base_tree)
275
merger.merge_type = merge.Merge3Merger
279
"""Release all resources held by this Unshelver."""
280
self.transform.finalize()
283
class ShelfManager(object):
284
"""Maintain a list of shelved changes."""
286
def __init__(self, tree, transport):
288
self.transport = transport.clone('shelf')
289
self.transport.ensure_base()
291
def get_shelf_filename(self, shelf_id):
292
return 'shelf-%d' % shelf_id
294
def get_shelf_ids(self, filenames):
295
matcher = re.compile('shelf-([1-9][0-9]*)')
297
for filename in filenames:
298
match = matcher.match(filename)
299
if match is not None:
300
shelf_ids.append(int(match.group(1)))
304
"""Return a file object and id for a new set of shelved changes."""
305
last_shelf = self.last_shelf()
306
if last_shelf is None:
309
next_shelf = last_shelf + 1
310
filename = self.get_shelf_filename(next_shelf)
311
shelf_file = open(self.transport.local_abspath(filename), 'wb')
312
return next_shelf, shelf_file
314
def shelve_changes(self, creator, message=None):
315
"""Store the changes in a ShelfCreator on a shelf."""
316
next_shelf, shelf_file = self.new_shelf()
318
creator.write_shelf(shelf_file, message)
324
def read_shelf(self, shelf_id):
325
"""Return the file associated with a shelf_id for reading.
327
:param shelf_id: The id of the shelf to retrive the file for.
329
filename = self.get_shelf_filename(shelf_id)
331
return open(self.transport.local_abspath(filename), 'rb')
333
if e.errno != errno.ENOENT:
335
from bzrlib import errors
336
raise errors.NoSuchShelfId(shelf_id)
338
def get_unshelver(self, shelf_id):
339
"""Return an unshelver for a given shelf_id.
341
:param shelf_id: The shelf id to return the unshelver for.
343
shelf_file = self.read_shelf(shelf_id)
345
return Unshelver.from_tree_and_shelf(self.tree, shelf_file)
349
def get_metadata(self, shelf_id):
350
"""Return the metadata associated with a given shelf_id."""
351
shelf_file = self.read_shelf(shelf_id)
353
records = Unshelver.iter_records(shelf_file)
356
return Unshelver.parse_metadata(records)
358
def delete_shelf(self, shelf_id):
359
"""Delete the shelved changes for a given id.
361
:param shelf_id: id of the shelved changes to delete.
363
filename = self.get_shelf_filename(shelf_id)
364
self.transport.delete(filename)
366
def active_shelves(self):
367
"""Return a list of shelved changes."""
368
active = self.get_shelf_ids(self.transport.list_dir('.'))
372
def last_shelf(self):
373
"""Return the id of the last-created shelved change."""
374
active = self.active_shelves()