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
26
class ShelfCreator(object):
27
"""Create a transform to shelve objects and its inverse."""
29
def __init__(self, work_tree, target_tree, file_list=None):
32
:param work_tree: The working tree to apply changes to
33
:param target_tree: The tree to make the working tree more similar to.
34
:param file_list: The files to make more similar to the target.
36
self.work_tree = work_tree
37
self.work_transform = transform.TreeTransform(work_tree)
38
self.target_tree = target_tree
39
self.shelf_transform = transform.TransformPreview(self.target_tree)
43
self.iter_changes = work_tree.iter_changes(self.target_tree,
44
specific_files=file_list)
46
def iter_shelvable(self):
47
"""Iterable of tuples describing shelvable changes.
49
As well as generating the tuples, this updates several members.
51
('add file', file_id, work_kind, work_path)
52
('delete file', file_id, target_kind, target_path)
53
('rename', file_id, target_path, work_path)
54
('change kind', file_id, target_kind, work_kind, target_path)
55
('modify text', file_id)
57
for (file_id, paths, changed, versioned, parents, names, kind,
58
executable) in self.iter_changes:
59
if kind[0] is None or versioned[0] == False:
60
self.creation[file_id] = (kind[1], names[1], parents[1],
62
yield ('add file', file_id, kind[1], paths[1])
63
elif kind[1] is None or versioned[0] == False:
64
self.deletion[file_id] = (kind[0], names[0], parents[0],
66
yield ('delete file', file_id, kind[0], paths[0])
68
if names[0] != names[1] or parents[0] != parents[1]:
69
self.renames[file_id] = (names, parents)
70
yield ('rename', file_id) + paths
72
if kind[0] != kind [1]:
73
yield ('change kind', file_id, kind[0], kind[1], paths[0])
75
yield ('modify text', file_id)
77
def shelve_rename(self, file_id):
78
"""Shelve a file rename.
80
:param file_id: The file id of the file to shelve the renaming of.
82
names, parents = self.renames[file_id]
83
w_trans_id = self.work_transform.trans_id_file_id(file_id)
84
work_parent = self.work_transform.trans_id_file_id(parents[0])
85
self.work_transform.adjust_path(names[0], work_parent, w_trans_id)
87
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
88
shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
89
self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
91
def shelve_lines(self, file_id, new_lines):
92
"""Shelve text changes to a file, using provided lines.
94
:param file_id: The file id of the file to shelve the text of.
95
:param new_lines: The lines that the file should have due to shelving.
97
w_trans_id = self.work_transform.trans_id_file_id(file_id)
98
self.work_transform.delete_contents(w_trans_id)
99
self.work_transform.create_file(new_lines, w_trans_id)
101
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
102
self.shelf_transform.delete_contents(s_trans_id)
103
inverse_lines = self._inverse_lines(new_lines, file_id)
104
self.shelf_transform.create_file(inverse_lines, s_trans_id)
107
def _content_from_tree(tt, tree, file_id):
108
trans_id = tt.trans_id_file_id(file_id)
109
tt.delete_contents(trans_id)
110
transform.create_from_tree(tt, trans_id, tree, file_id)
112
def shelve_content_change(self, file_id):
113
"""Shelve a kind change or binary file content change.
115
:param file_id: The file id of the file to shelve the content change
118
self._content_from_tree(self.work_transform, self.target_tree, file_id)
119
self._content_from_tree(self.shelf_transform, self.work_tree, file_id)
121
def shelve_creation(self, file_id):
122
"""Shelve creation of a file.
124
This handles content and inventory id.
125
:param file_id: The file_id of the file to shelve creation of.
127
kind, name, parent, versioned = self.creation[file_id]
128
version = not versioned[0]
129
self._shelve_creation(self.work_tree, file_id, self.work_transform,
130
self.shelf_transform, kind, name, parent,
133
def shelve_deletion(self, file_id):
134
"""Shelve deletion of a file.
136
This handles content and inventory id.
137
:param file_id: The file_id of the file to shelve deletion of.
139
kind, name, parent, versioned = self.deletion[file_id]
140
existing_path = self.target_tree.id2path(file_id)
141
if not self.work_tree.has_filename(existing_path):
143
version = not versioned[1]
144
self._shelve_creation(self.target_tree, file_id, self.shelf_transform,
145
self.work_transform, kind, name, parent,
146
version, existing_path=existing_path)
148
def _shelve_creation(self, tree, file_id, from_transform, to_transform,
149
kind, name, parent, version, existing_path=None):
150
w_trans_id = from_transform.trans_id_file_id(file_id)
151
if parent is not None and kind is not None:
152
from_transform.delete_contents(w_trans_id)
153
from_transform.unversion_file(w_trans_id)
155
if existing_path is not None:
156
s_trans_id = to_transform.trans_id_tree_path(existing_path)
158
s_trans_id = to_transform.trans_id_file_id(file_id)
159
if parent is not None:
160
s_parent_id = to_transform.trans_id_file_id(parent)
161
to_transform.adjust_path(name, s_parent_id, s_trans_id)
162
if existing_path is None:
164
to_transform.create_file('', s_trans_id)
166
transform.create_from_tree(to_transform, s_trans_id,
169
to_transform.version_file(file_id, s_trans_id)
171
def read_tree_lines(self, tree, file_id):
172
"""Read text lines from a tree.
174
(Tree.get_file_lines is not an official API)
176
return osutils.split_lines(tree.get_file_text(file_id))
178
def _inverse_lines(self, new_lines, file_id):
179
"""Produce a version with only those changes removed from new_lines."""
180
target_lines = self.target_tree.get_file_lines(file_id)
181
work_lines = self.read_tree_lines(self.work_tree, file_id)
182
return merge3.Merge3(new_lines, target_lines, work_lines).merge_lines()
185
"""Release all resources used by this ShelfCreator."""
186
self.work_transform.finalize()
187
self.shelf_transform.finalize()
190
"""Shelve changes from working tree."""
191
self.work_transform.apply()
193
def make_shelf_filename(self):
194
"""Generate a filename for a shelf."""
195
transport = self.work_tree.bzrdir.root_transport.clone('.shelf2')
196
transport.ensure_base()
197
return transport.local_abspath('01')
199
def write_shelf(self):
200
"""Serialize the shelved changes to a file.
202
:return: the filename of the written file.
204
filename = self.make_shelf_filename()
205
shelf_file = open(filename, 'wb')
207
serializer = pack.ContainerSerialiser()
208
shelf_file.write(serializer.begin())
209
for bytes in self.shelf_transform.serialize(serializer):
210
shelf_file.write(bytes)
211
shelf_file.write(serializer.end())