3
# Copyright (C) 2005 Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Experiment in converting existing bzr branches to weaves."""
21
# To make this properly useful
23
# 1. assign text version ids, and put those text versions into
24
# the inventory as they're converted.
26
# 2. keep track of the previous version of each file, rather than
27
# just using the last one imported
29
# 3. assign entry versions when files are added, renamed or moved.
31
# 4. when merged-in versions are observed, walk down through them
32
# to discover everything, then commit bottom-up
34
# 5. track ancestry as things are merged in, and commit that in each
37
# Perhaps it's best to first walk the whole graph and make a plan for
38
# what should be imported in what order? Need a kind of topological
39
# sort of all revisions. (Or do we, can we just before doing a revision
40
# see that all its parents have either been converted or abandoned?)
43
# Cannot import a revision until all its parents have been
44
# imported. in other words, we can only import revisions whose
45
# parents have all been imported. the first step must be to
46
# import a revision with no parents, of which there must be at
47
# least one. (So perhaps it's useful to store forward pointers
48
# from a list of parents to their children?)
50
# Another (equivalent?) approach is to build up the ordered
51
# ancestry list for the last revision, and walk through that. We
52
# are going to need that.
54
# We don't want to have to recurse all the way back down the list.
56
# Suppose we keep a queue of the revisions able to be processed at
57
# any point. This starts out with all the revisions having no
60
# This seems like a generally useful algorithm...
62
# The current algorithm is dumb (O(n**2)?) but will do the job, and
63
# takes less than a second on the bzr.dev branch.
65
# This currently does a kind of lazy conversion of file texts, where a
66
# new text is written in every version. That's unnecessary but for
67
# the moment saves us having to worry about when files need new
80
import hotshot, hotshot.stats
85
from bzrlib.branch import Branch, find_branch
86
from bzrlib.revfile import Revfile
87
from bzrlib.weave import Weave
88
from bzrlib.weavefile import read_weave, write_weave
89
from bzrlib.progress import ProgressBar
90
from bzrlib.atomicfile import AtomicFile
91
from bzrlib.xml4 import serializer_v4
92
from bzrlib.xml5 import serializer_v5
93
from bzrlib.trace import mutter, note, warning, enable_default_logging
94
from bzrlib.osutils import sha_strings, sha_string
95
from bzrlib.commit import merge_ancestry_lines
98
class Convert(object):
100
self.converted_revs = set()
101
self.absent_revisions = set()
104
self.inventories = {}
111
enable_default_logging()
112
self.pb = ProgressBar()
113
self.inv_weave = Weave('__inventory')
114
self.anc_weave = Weave('__ancestry')
116
# holds in-memory weaves for all files
117
self.text_weaves = {}
119
self.branch = Branch('.', relax_version_check=True)
122
rev_history = self.branch.revision_history()
126
# to_read is a stack holding the revisions we still need to process;
127
# appending to it adds new highest-priority revisions
129
self.known_revisions = set(rev_history)
130
self.to_read = [rev_history[-1]]
132
rev_id = self.to_read.pop()
133
if (rev_id not in self.revisions
134
and rev_id not in self.absent_revisions):
135
self._load_one_rev(rev_id)
137
to_import = self._make_order()
138
for i, rev_id in enumerate(to_import):
139
self.pb.update('converting revision', i, len(to_import))
140
self._convert_one_rev(rev_id)
142
print 'upgraded to weaves:'
143
print ' %6d revisions and inventories' % len(self.revisions)
144
print ' %6d absent revisions removed' % len(self.absent_revisions)
145
print ' %6d texts' % self.text_count
146
self._write_all_weaves()
147
self._write_all_revs()
150
def _write_all_weaves(self):
151
write_a_weave(self.inv_weave, 'weaves/inventory.weave')
152
write_a_weave(self.anc_weave, 'weaves/ancestry.weave')
155
for file_id, file_weave in self.text_weaves.items():
156
self.pb.update('writing weave', i, len(self.text_weaves))
157
write_a_weave(file_weave, 'weaves/%s.weave' % file_id)
163
def _write_all_revs(self):
164
"""Write all revisions out in new form."""
166
for i, rev_id in enumerate(self.converted_revs):
167
self.pb.update('write revision', i, len(self.converted_revs))
168
f = file('new-revisions/%s' % rev_id, 'wb')
170
serializer_v5.write_revision(self.revisions[rev_id], f)
177
def _load_one_rev(self, rev_id):
178
"""Load a revision object into memory.
180
Any parents not either loaded or abandoned get queued to be
182
self.pb.update('loading revision',
184
len(self.known_revisions))
185
if rev_id not in self.branch.revision_store:
187
note('revision {%s} not present in branch; '
188
'will not be converted',
190
self.absent_revisions.add(rev_id)
192
rev_xml = self.branch.revision_store[rev_id].read()
193
rev = serializer_v4.read_revision_from_string(rev_xml)
194
for parent_id in rev.parent_ids:
195
self.known_revisions.add(parent_id)
196
self.to_read.append(parent_id)
197
self.revisions[rev_id] = rev
198
old_inv_xml = self.branch.inventory_store[rev_id].read()
199
inv = serializer_v4.read_inventory_from_string(old_inv_xml)
200
assert rev.inventory_sha1 == sha_string(old_inv_xml)
201
self.inventories[rev_id] = inv
204
def _convert_one_rev(self, rev_id):
205
"""Convert revision and all referenced objects to new format."""
206
rev = self.revisions[rev_id]
207
inv = self.inventories[rev_id]
208
for parent_id in rev.parent_ids[:]:
209
if parent_id in self.absent_revisions:
210
rev.parent_ids.remove(parent_id)
212
note('remove {%s} as parent of {%s}', parent_id, rev_id)
213
self._convert_revision_contents(rev, inv)
214
# the XML is now updated with text versions
215
new_inv_xml = serializer_v5.write_inventory_to_string(inv)
216
new_inv_sha1 = sha_string(new_inv_xml)
217
self.inv_weave.add(rev_id, rev.parent_ids,
218
new_inv_xml.splitlines(True),
220
# TODO: Upgrade revision XML and write that out
221
rev.inventory_sha1 = new_inv_sha1
222
self._make_rev_ancestry(rev)
223
self.converted_revs.add(rev_id)
226
def _make_rev_ancestry(self, rev):
227
rev_id = rev.revision_id
228
for parent_id in rev.parent_ids:
229
assert parent_id in self.converted_revs
230
parent_ancestries = [self.ancestries[p] for p in rev.parent_ids]
231
new_lines = merge_ancestry_lines(rev_id, parent_ancestries)
232
self.ancestries[rev_id] = new_lines
233
self.anc_weave.add(rev_id, rev.parent_ids, new_lines)
236
def _convert_revision_contents(self, rev, inv):
237
"""Convert all the files within a revision.
239
Also upgrade the inventory to refer to the text revision ids."""
240
rev_id = rev.revision_id
241
mutter('converting texts of revision {%s}',
245
if ie.kind != 'file':
247
self._convert_file_version(rev, ie)
248
# TODO: Check and convert name versions
251
def _convert_file_version(self, rev, ie):
252
"""Convert one version of one file.
254
The file needs to be added into the weave if it is a merge
255
of >=2 parents or if it's changed from its parent.
258
rev_id = rev.revision_id
259
w = self.text_weaves.get(file_id)
262
self.text_weaves[file_id] = w
263
file_lines = self.branch.text_store[ie.text_id].readlines()
264
assert sha_strings(file_lines) == ie.text_sha1
265
assert sum(map(len, file_lines)) == ie.text_size
268
for parent_id in rev.parent_ids:
269
##if parent_id in self.absent_revisions:
271
assert parent_id in self.converted_revs, \
272
'parent {%s} not converted' % parent_id
273
parent_inv = self.inventories[parent_id]
274
if parent_inv.has_id(file_id):
275
parent_ie = parent_inv[file_id]
276
old_text_version = parent_ie.text_version
277
assert old_text_version in self.converted_revs
278
if old_text_version not in file_parents:
279
file_parents.append(old_text_version)
280
if parent_ie.text_sha1 != ie.text_sha1:
282
if len(file_parents) != 1 or text_changed:
283
w.add(rev_id, file_parents, file_lines, ie.text_sha1)
284
ie.name_version = ie.text_version = rev_id
286
##mutter('import text {%s} of {%s}',
287
## ie.text_id, file_id)
289
##mutter('text of {%s} unchanged from parent', file_id)
290
ie.text_version = file_parents[0]
291
ie.name_version = file_parents[0]
296
def _make_order(self):
297
"""Return a suitable order for importing revisions.
299
The order must be such that an revision is imported after all
300
its (present) parents.
302
todo = set(self.revisions.keys())
303
done = self.absent_revisions.copy()
306
# scan through looking for a revision whose parents
308
for rev_id in sorted(list(todo)):
309
rev = self.revisions[rev_id]
310
parent_ids = set(rev.parent_ids)
311
if parent_ids.issubset(done):
312
# can take this one now
319
def write_a_weave(weave, filename):
320
inv_wf = file(filename, 'wb')
322
write_weave(weave, inv_wf)
329
def profile_convert():
330
prof_f = tempfile.NamedTemporaryFile()
332
prof = hotshot.Profile(prof_f.name)
334
prof.runcall(Convert)
337
stats = hotshot.stats.load(prof_f.name)
339
stats.sort_stats('time')
340
# XXX: Might like to write to stderr or the trace file instead but
341
# print_stats seems hardcoded to stdout
342
stats.print_stats(100)
345
enable_default_logging()
347
if '-p' in sys.argv[1:]: