1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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
21
20
from bzrlib import (
30
from bzrlib.util import bencode
31
33
class ShelfCreator(object):
34
36
def __init__(self, work_tree, target_tree, file_list=None):
37
:param work_tree: The working tree to apply changes to. This is not
38
required to be locked - a tree_write lock will be taken out.
39
:param work_tree: The working tree to apply changes to
39
40
:param target_tree: The tree to make the working tree more similar to.
40
This is not required to be locked - a read_lock will be taken out.
41
41
:param file_list: The files to make more similar to the target.
43
43
self.work_tree = work_tree
44
44
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)
55
self.shelf_transform.finalize()
58
self.work_transform.finalize()
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)
61
def iter_shelvable(self):
62
54
"""Iterable of tuples describing shelvable changes.
64
56
As well as generating the tuples, this updates several members.
68
60
('rename', file_id, target_path, work_path)
69
61
('change kind', file_id, target_kind, work_kind, target_path)
70
62
('modify text', file_id)
71
('modify target', file_id, target_target, work_target)
73
64
for (file_id, paths, changed, versioned, parents, names, kind,
74
65
executable) in self.iter_changes:
75
# don't shelve add of tree root. Working tree should never
76
# lack roots, and bzr misbehaves when they do.
77
# FIXME ADHB (2009-08-09): should still shelve adds of tree roots
78
# when a tree root was deleted / renamed.
79
if kind[0] is None and names[1] == '':
81
66
if kind[0] is None or versioned[0] == False:
82
67
self.creation[file_id] = (kind[1], names[1], parents[1],
94
79
if kind[0] != kind [1]:
95
80
yield ('change kind', file_id, kind[0], kind[1], paths[0])
96
elif kind[0] == 'symlink':
97
t_target = self.target_tree.get_symlink_target(file_id)
98
w_target = self.work_tree.get_symlink_target(file_id)
99
yield ('modify target', file_id, paths[0], t_target,
102
82
yield ('modify text', file_id)
104
def shelve_change(self, change):
105
"""Shelve a change in the iter_shelvable format."""
106
if change[0] == 'rename':
107
self.shelve_rename(change[1])
108
elif change[0] == 'delete file':
109
self.shelve_deletion(change[1])
110
elif change[0] == 'add file':
111
self.shelve_creation(change[1])
112
elif change[0] in ('change kind', 'modify text'):
113
self.shelve_content_change(change[1])
114
elif change[0] == 'modify target':
115
self.shelve_modify_target(change[1])
117
raise ValueError('Unknown change kind: "%s"' % change[0])
119
def shelve_all(self):
120
"""Shelve all changes."""
121
for change in self.iter_shelvable():
122
self.shelve_change(change)
124
84
def shelve_rename(self, file_id):
125
85
"""Shelve a file rename.
135
95
shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
136
96
self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
138
def shelve_modify_target(self, file_id):
139
"""Shelve a change of symlink target.
141
:param file_id: The file id of the symlink which changed target.
142
:param new_target: The target that the symlink should have due
145
new_target = self.target_tree.get_symlink_target(file_id)
146
w_trans_id = self.work_transform.trans_id_file_id(file_id)
147
self.work_transform.delete_contents(w_trans_id)
148
self.work_transform.create_symlink(new_target, w_trans_id)
150
old_target = self.work_tree.get_symlink_target(file_id)
151
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
152
self.shelf_transform.delete_contents(s_trans_id)
153
self.shelf_transform.create_symlink(old_target, s_trans_id)
155
98
def shelve_lines(self, file_id, new_lines):
156
99
"""Shelve text changes to a file, using provided lines.
233
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))
235
185
def _inverse_lines(self, new_lines, file_id):
236
186
"""Produce a version with only those changes removed from new_lines."""
237
187
target_lines = self.target_tree.get_file_lines(file_id)
238
work_lines = self.work_tree.get_file_lines(file_id)
188
work_lines = self.read_tree_lines(self.work_tree, file_id)
239
189
return merge3.Merge3(new_lines, target_lines, work_lines).merge_lines()
241
191
def finalize(self):
284
234
self.transform = transform
285
235
self.message = message
288
def iter_records(shelf_file):
289
parser = pack.ContainerPushParser()
290
parser.accept_bytes(shelf_file.read())
291
return iter(parser.read_pending_records())
294
def parse_metadata(records):
295
names, metadata_bytes = records.next()
296
if names[0] != ('metadata',):
297
raise errors.ShelfCorrupt
298
metadata = bencode.bdecode(metadata_bytes)
299
message = metadata.get('message')
300
if message is not None:
301
metadata['message'] = message.decode('utf-8')
305
238
def from_tree_and_shelf(klass, tree, shelf_file):
306
239
"""Create an Unshelver from a tree and a shelf file.
309
242
:param shelf_file: A file-like object containing shelved changes.
310
243
:return: The Unshelver.
312
records = klass.iter_records(shelf_file)
313
metadata = klass.parse_metadata(records)
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
assert names[0] == ('metadata',)
250
metadata = bencode.bdecode(metadata_bytes)
314
251
base_revision_id = metadata['revision_id']
252
message = metadata.get('message')
253
if message is not None:
254
message = message.decode('utf-8')
316
256
base_tree = tree.revision_tree(base_revision_id)
317
257
except errors.NoSuchRevisionInTree:
318
258
base_tree = tree.branch.repository.revision_tree(base_revision_id)
319
259
tt = transform.TransformPreview(base_tree)
320
260
tt.deserialize(records)
321
return klass(tree, base_tree, tt, metadata.get('message'))
261
return klass(tree, base_tree, tt, message)
323
def make_merger(self, task=None):
263
def make_merger(self):
324
264
"""Return a merger that can unshelve the changes."""
325
target_tree = self.transform.get_preview_tree()
326
merger = merge.Merger.from_uncommitted(self.tree, target_tree,
327
task, self.base_tree)
328
merger.merge_type = merge.Merge3Merger
265
pb = ui.ui_factory.nested_progress_bar()
267
target_tree = self.transform.get_preview_tree()
268
merger = merge.Merger.from_uncommitted(self.tree, target_tree, pb,
270
merger.merge_type = merge.Merge3Merger
331
275
def finalize(self):
332
276
"""Release all resources held by this Unshelver."""
341
285
self.transport = transport.clone('shelf')
342
286
self.transport.ensure_base()
344
def get_shelf_filename(self, shelf_id):
345
return 'shelf-%d' % shelf_id
347
def get_shelf_ids(self, filenames):
348
matcher = re.compile('shelf-([1-9][0-9]*)')
350
for filename in filenames:
351
match = matcher.match(filename)
352
if match is not None:
353
shelf_ids.append(int(match.group(1)))
356
288
def new_shelf(self):
357
289
"""Return a file object and id for a new set of shelved changes."""
358
290
last_shelf = self.last_shelf()
362
294
next_shelf = last_shelf + 1
363
filename = self.get_shelf_filename(next_shelf)
364
shelf_file = open(self.transport.local_abspath(filename), 'wb')
295
shelf_file = open(self.transport.local_abspath(str(next_shelf)), 'wb')
365
296
return next_shelf, shelf_file
367
298
def shelve_changes(self, creator, message=None):
380
311
:param shelf_id: The id of the shelf to retrive the file for.
382
filename = self.get_shelf_filename(shelf_id)
384
return open(self.transport.local_abspath(filename), 'rb')
314
return open(self.transport.local_abspath(str(shelf_id)), 'rb')
385
315
except IOError, e:
386
316
if e.errno != errno.ENOENT:
400
330
shelf_file.close()
402
def get_metadata(self, shelf_id):
403
"""Return the metadata associated with a given shelf_id."""
404
shelf_file = self.read_shelf(shelf_id)
406
records = Unshelver.iter_records(shelf_file)
409
return Unshelver.parse_metadata(records)
411
332
def delete_shelf(self, shelf_id):
412
333
"""Delete the shelved changes for a given id.
414
335
:param shelf_id: id of the shelved changes to delete.
416
filename = self.get_shelf_filename(shelf_id)
417
self.transport.delete(filename)
337
self.transport.delete(str(shelf_id))
419
339
def active_shelves(self):
420
340
"""Return a list of shelved changes."""
421
active = self.get_shelf_ids(self.transport.list_dir('.'))
341
return [int(f) for f in self.transport.list_dir('.')]
425
343
def last_shelf(self):
426
344
"""Return the id of the last-created shelved change."""
427
345
active = self.active_shelves()
428
346
if len(active) > 0: