bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.14.26
by Aaron Bentley
Update copyright |
1 |
# Copyright (C) 2008 Canonical Ltd
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
2 |
#
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
16 |
||
17 |
||
|
0.14.19
by Aaron Bentley
Convert bzrlib import to split-line |
18 |
from bzrlib import ( |
19 |
merge3, |
|
20 |
osutils, |
|
21 |
pack, |
|
22 |
transform, |
|
23 |
)
|
|
|
0.12.19
by Aaron Bentley
Add support for writing shelves |
24 |
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
25 |
|
26 |
class ShelfCreator(object): |
|
|
0.14.27
by Aaron Bentley
Update docs |
27 |
"""Create a transform to shelve objects and its inverse.""" |
|
0.12.12
by Aaron Bentley
Implement shelf creator |
28 |
|
|
0.14.21
by Aaron Bentley
Update to accept a list of files. |
29 |
def __init__(self, work_tree, target_tree, file_list=None): |
|
0.14.27
by Aaron Bentley
Update docs |
30 |
"""Constructor. |
31 |
||
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.
|
|
35 |
"""
|
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
36 |
self.work_tree = work_tree |
37 |
self.work_transform = transform.TreeTransform(work_tree) |
|
|
0.14.1
by Aaron Bentley
Use explicit target in ShelfCreator |
38 |
self.target_tree = target_tree |
39 |
self.shelf_transform = transform.TransformPreview(self.target_tree) |
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
40 |
self.renames = {} |
|
0.14.2
by Aaron Bentley
Somewhat clean up shelving |
41 |
self.creation = {} |
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
42 |
self.deletion = {} |
|
0.14.21
by Aaron Bentley
Update to accept a list of files. |
43 |
self.iter_changes = work_tree.iter_changes(self.target_tree, |
44 |
specific_files=file_list) |
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
45 |
|
|
0.14.32
by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable |
46 |
def iter_shelvable(self): |
|
0.14.27
by Aaron Bentley
Update docs |
47 |
"""Iterable of tuples describing shelvable changes. |
48 |
||
49 |
As well as generating the tuples, this updates several members.
|
|
50 |
Tuples may be:
|
|
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)
|
|
56 |
"""
|
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
57 |
for (file_id, paths, changed, versioned, parents, names, kind, |
58 |
executable) in self.iter_changes: |
|
|
0.12.14
by Aaron Bentley
Add shelving of created files |
59 |
if kind[0] is None or versioned[0] == False: |
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
60 |
self.creation[file_id] = (kind[1], names[1], parents[1], |
61 |
versioned) |
|
|
0.14.13
by Aaron Bentley
Provide path and kind when deletes/adds are detected. |
62 |
yield ('add file', file_id, kind[1], paths[1]) |
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
63 |
elif kind[1] is None or versioned[0] == False: |
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
64 |
self.deletion[file_id] = (kind[0], names[0], parents[0], |
65 |
versioned) |
|
|
0.14.13
by Aaron Bentley
Provide path and kind when deletes/adds are detected. |
66 |
yield ('delete file', file_id, kind[0], paths[0]) |
|
0.12.14
by Aaron Bentley
Add shelving of created files |
67 |
else: |
68 |
if names[0] != names[1] or parents[0] != parents[1]: |
|
69 |
self.renames[file_id] = (names, parents) |
|
70 |
yield ('rename', file_id) + paths |
|
|
0.14.23
by Aaron Bentley
Allow shelving kind change |
71 |
|
72 |
if kind[0] != kind [1]: |
|
73 |
yield ('change kind', file_id, kind[0], kind[1], paths[0]) |
|
74 |
elif changed: |
|
|
0.12.14
by Aaron Bentley
Add shelving of created files |
75 |
yield ('modify text', file_id) |
|
0.12.12
by Aaron Bentley
Implement shelf creator |
76 |
|
77 |
def shelve_rename(self, file_id): |
|
|
0.14.27
by Aaron Bentley
Update docs |
78 |
"""Shelve a file rename. |
79 |
||
80 |
:param file_id: The file id of the file to shelve the renaming of.
|
|
81 |
"""
|
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
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) |
|
86 |
||
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) |
|
90 |
||
|
0.14.14
by Aaron Bentley
Change shelf_text to shelve_lines |
91 |
def shelve_lines(self, file_id, new_lines): |
|
0.14.27
by Aaron Bentley
Update docs |
92 |
"""Shelve text changes to a file, using provided lines. |
93 |
||
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.
|
|
96 |
"""
|
|
|
0.12.13
by Aaron Bentley
Implement shelving content |
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) |
|
100 |
||
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) |
|
105 |
||
|
0.14.23
by Aaron Bentley
Allow shelving kind change |
106 |
@staticmethod
|
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) |
|
111 |
||
112 |
def shelve_content_change(self, file_id): |
|
|
0.14.27
by Aaron Bentley
Update docs |
113 |
"""Shelve a kind change or binary file content change. |
114 |
||
115 |
:param file_id: The file id of the file to shelve the content change
|
|
116 |
of.
|
|
117 |
"""
|
|
|
0.14.23
by Aaron Bentley
Allow shelving kind 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) |
|
120 |
||
|
0.14.2
by Aaron Bentley
Somewhat clean up shelving |
121 |
def shelve_creation(self, file_id): |
|
0.14.27
by Aaron Bentley
Update docs |
122 |
"""Shelve creation of a file. |
123 |
||
124 |
This handles content and inventory id.
|
|
125 |
:param file_id: The file_id of the file to shelve creation of.
|
|
126 |
"""
|
|
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
127 |
kind, name, parent, versioned = self.creation[file_id] |
128 |
version = not versioned[0] |
|
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
129 |
self._shelve_creation(self.work_tree, file_id, self.work_transform, |
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
130 |
self.shelf_transform, kind, name, parent, |
131 |
version) |
|
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
132 |
|
133 |
def shelve_deletion(self, file_id): |
|
|
0.14.27
by Aaron Bentley
Update docs |
134 |
"""Shelve deletion of a file. |
135 |
||
136 |
This handles content and inventory id.
|
|
137 |
:param file_id: The file_id of the file to shelve deletion of.
|
|
138 |
"""
|
|
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
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): |
|
142 |
existing_path = None |
|
143 |
version = not versioned[1] |
|
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
144 |
self._shelve_creation(self.target_tree, file_id, self.shelf_transform, |
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
145 |
self.work_transform, kind, name, parent, |
146 |
version, existing_path=existing_path) |
|
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
147 |
|
148 |
def _shelve_creation(self, tree, file_id, from_transform, to_transform, |
|
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
149 |
kind, name, parent, version, existing_path=None): |
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
150 |
w_trans_id = from_transform.trans_id_file_id(file_id) |
|
0.14.12
by Aaron Bentley
Handle new dangling ids |
151 |
if parent is not None and kind is not None: |
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
152 |
from_transform.delete_contents(w_trans_id) |
153 |
from_transform.unversion_file(w_trans_id) |
|
154 |
||
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
155 |
if existing_path is not None: |
156 |
s_trans_id = to_transform.trans_id_tree_path(existing_path) |
|
157 |
else: |
|
158 |
s_trans_id = to_transform.trans_id_file_id(file_id) |
|
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
159 |
if parent is not None: |
160 |
s_parent_id = to_transform.trans_id_file_id(parent) |
|
|
0.14.9
by Aaron Bentley
Shelve deleted files properly |
161 |
to_transform.adjust_path(name, s_parent_id, s_trans_id) |
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
162 |
if existing_path is None: |
|
0.14.18
by Aaron Bentley
Simplify creating files |
163 |
if kind is None: |
|
0.14.12
by Aaron Bentley
Handle new dangling ids |
164 |
to_transform.create_file('', s_trans_id) |
|
0.14.18
by Aaron Bentley
Simplify creating files |
165 |
else: |
166 |
transform.create_from_tree(to_transform, s_trans_id, |
|
167 |
tree, file_id) |
|
|
0.14.10
by Aaron Bentley
Fix behavior with deletions, unversioning, ... |
168 |
if version: |
169 |
to_transform.version_file(file_id, s_trans_id) |
|
|
0.12.14
by Aaron Bentley
Add shelving of created files |
170 |
|
|
0.14.4
by Aaron Bentley
Implement shelving deletion |
171 |
def read_tree_lines(self, tree, file_id): |
|
0.14.27
by Aaron Bentley
Update docs |
172 |
"""Read text lines from a tree. |
173 |
||
174 |
(Tree.get_file_lines is not an official API)
|
|
175 |
"""
|
|
|
0.14.17
by Aaron Bentley
Use safer line-splitting |
176 |
return osutils.split_lines(tree.get_file_text(file_id)) |
|
0.12.14
by Aaron Bentley
Add shelving of created files |
177 |
|
178 |
def _inverse_lines(self, new_lines, file_id): |
|
179 |
"""Produce a version with only those changes removed from new_lines.""" |
|
|
0.14.1
by Aaron Bentley
Use explicit target in ShelfCreator |
180 |
target_lines = self.target_tree.get_file_lines(file_id) |
|
0.14.5
by Aaron Bentley
Fix call to read_tree_lines |
181 |
work_lines = self.read_tree_lines(self.work_tree, file_id) |
|
0.14.1
by Aaron Bentley
Use explicit target in ShelfCreator |
182 |
return merge3.Merge3(new_lines, target_lines, work_lines).merge_lines() |
|
0.12.13
by Aaron Bentley
Implement shelving content |
183 |
|
|
0.12.12
by Aaron Bentley
Implement shelf creator |
184 |
def finalize(self): |
|
0.14.27
by Aaron Bentley
Update docs |
185 |
"""Release all resources used by this ShelfCreator.""" |
|
0.12.12
by Aaron Bentley
Implement shelf creator |
186 |
self.work_transform.finalize() |
187 |
self.shelf_transform.finalize() |
|
|
0.12.13
by Aaron Bentley
Implement shelving content |
188 |
|
189 |
def transform(self): |
|
|
0.14.27
by Aaron Bentley
Update docs |
190 |
"""Shelve changes from working tree.""" |
|
0.12.13
by Aaron Bentley
Implement shelving content |
191 |
self.work_transform.apply() |
|
0.12.19
by Aaron Bentley
Add support for writing shelves |
192 |
|
193 |
def make_shelf_filename(self): |
|
|
0.14.27
by Aaron Bentley
Update docs |
194 |
"""Generate a filename for a shelf.""" |
|
0.12.19
by Aaron Bentley
Add support for writing shelves |
195 |
transport = self.work_tree.bzrdir.root_transport.clone('.shelf2') |
196 |
transport.ensure_base() |
|
197 |
return transport.local_abspath('01') |
|
198 |
||
199 |
def write_shelf(self): |
|
|
0.14.27
by Aaron Bentley
Update docs |
200 |
"""Serialize the shelved changes to a file. |
201 |
||
202 |
:return: the filename of the written file.
|
|
203 |
"""
|
|
|
0.12.19
by Aaron Bentley
Add support for writing shelves |
204 |
filename = self.make_shelf_filename() |
205 |
shelf_file = open(filename, 'wb') |
|
206 |
try: |
|
207 |
serializer = pack.ContainerSerialiser() |
|
208 |
shelf_file.write(serializer.begin()) |
|
|
0.14.24
by Aaron Bentley
Merge with serialize-transform |
209 |
for bytes in self.shelf_transform.serialize(serializer): |
|
0.12.19
by Aaron Bentley
Add support for writing shelves |
210 |
shelf_file.write(bytes) |
211 |
shelf_file.write(serializer.end()) |
|
212 |
finally: |
|
213 |
shelf_file.close() |
|
214 |
return filename |