14
16
# along with this program; if not, write to the Free Software
15
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""bzr upgrade logic."""
19
# change upgrade from .bzr to create a '.bzr-new', then do a bait and switch.
22
from bzrlib.bzrdir import ConvertBzrDir4To5, ConvertBzrDir5To6, BzrDir, BzrDirFormat4, BzrDirFormat5, BzrDirFormat
23
import bzrlib.errors as errors
24
from bzrlib.transport import get_transport
25
import bzrlib.ui as ui
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
28
98
class Convert(object):
30
def __init__(self, url, format):
32
self.bzrdir = BzrDir.open_unsupported(url)
33
if self.bzrdir.root_transport.is_readonly():
34
raise errors.UpgradeReadonly
35
self.transport = self.bzrdir.root_transport
36
self.pb = ui.ui_factory.nested_progress_bar()
100
self.converted_revs = set()
101
self.absent_revisions = set()
104
self.inventories = {}
42
110
def convert(self):
44
branch = self.bzrdir.open_branch()
45
if branch.bzrdir.root_transport.base != \
46
self.bzrdir.root_transport.base:
47
self.pb.note("This is a checkout. The branch (%s) needs to be "
48
"upgraded separately.",
49
branch.bzrdir.root_transport.base)
50
except errors.NotBranchError:
52
if not self.bzrdir.needs_format_conversion(self.format):
53
raise errors.UpToDateFormat(self.bzrdir._format)
54
if not self.bzrdir.can_convert_format():
55
raise errors.BzrError("cannot upgrade from branch format %s" %
57
if self.format is None:
58
target_format = BzrDirFormat.get_default_format()
60
target_format = self.format
61
self.bzrdir.check_conversion_target(target_format)
62
self.pb.note('starting upgrade of %s', self.transport.base)
63
self._backup_control_dir()
64
while self.bzrdir.needs_format_conversion(self.format):
65
converter = self.bzrdir._format.get_converter(self.format)
66
self.bzrdir = converter.convert(self.bzrdir, self.pb)
67
self.pb.note("finished")
69
def _backup_control_dir(self):
70
self.pb.note('making backup of tree history')
71
self.transport.copy_tree('.bzr', '.bzr.backup')
72
self.pb.note('%s.bzr has been backed up to %s.bzr.backup',
75
self.pb.note('if conversion fails, you can move this directory back to .bzr')
76
self.pb.note('if it succeeds, you can remove this directory if you wish')
78
def upgrade(url, format=None):
79
"""Upgrade to format, or the default bzrdir format if not supplied."""
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
231
lines = list(self.anc_weave.mash_iter(rev.parent_ids))
234
lines.append(rev_id + '\n')
236
parent_ancestries = [self.ancestries[p] for p in rev.parent_ids]
237
new_lines = merge_ancestry_lines(rev_id, parent_ancestries)
238
assert set(lines) == set(new_lines)
239
self.ancestries[rev_id] = new_lines
240
self.anc_weave.add(rev_id, rev.parent_ids, lines)
243
def _convert_revision_contents(self, rev, inv):
244
"""Convert all the files within a revision.
246
Also upgrade the inventory to refer to the text revision ids."""
247
rev_id = rev.revision_id
248
mutter('converting texts of revision {%s}',
252
if ie.kind != 'file':
254
self._convert_file_version(rev, ie)
255
# TODO: Check and convert name versions
258
def _convert_file_version(self, rev, ie):
259
"""Convert one version of one file.
261
The file needs to be added into the weave if it is a merge
262
of >=2 parents or if it's changed from its parent.
265
rev_id = rev.revision_id
266
w = self.text_weaves.get(file_id)
269
self.text_weaves[file_id] = w
270
file_lines = self.branch.text_store[ie.text_id].readlines()
271
assert sha_strings(file_lines) == ie.text_sha1
272
assert sum(map(len, file_lines)) == ie.text_size
275
for parent_id in rev.parent_ids:
276
##if parent_id in self.absent_revisions:
278
assert parent_id in self.converted_revs, \
279
'parent {%s} not converted' % parent_id
280
parent_inv = self.inventories[parent_id]
281
if parent_inv.has_id(file_id):
282
parent_ie = parent_inv[file_id]
283
old_text_version = parent_ie.text_version
284
assert old_text_version in self.converted_revs
285
if old_text_version not in file_parents:
286
file_parents.append(old_text_version)
287
if parent_ie.text_sha1 != ie.text_sha1:
289
if len(file_parents) != 1 or text_changed:
290
w.add(rev_id, file_parents, file_lines, ie.text_sha1)
291
ie.name_version = ie.text_version = rev_id
293
##mutter('import text {%s} of {%s}',
294
## ie.text_id, file_id)
296
##mutter('text of {%s} unchanged from parent', file_id)
297
ie.text_version = file_parents[0]
298
ie.name_version = file_parents[0]
303
def _make_order(self):
304
"""Return a suitable order for importing revisions.
306
The order must be such that an revision is imported after all
307
its (present) parents.
309
todo = set(self.revisions.keys())
310
done = self.absent_revisions.copy()
313
# scan through looking for a revision whose parents
315
for rev_id in sorted(list(todo)):
316
rev = self.revisions[rev_id]
317
parent_ids = set(rev.parent_ids)
318
if parent_ids.issubset(done):
319
# can take this one now
326
def write_a_weave(weave, filename):
327
inv_wf = file(filename, 'wb')
329
write_weave(weave, inv_wf)
336
def profile_convert():
337
prof_f = tempfile.NamedTemporaryFile()
339
prof = hotshot.Profile(prof_f.name)
341
prof.runcall(Convert)
344
stats = hotshot.stats.load(prof_f.name)
346
stats.sort_stats('time')
347
# XXX: Might like to write to stderr or the trace file instead but
348
# print_stats seems hardcoded to stdout
349
stats.print_stats(100)
352
enable_default_logging()
354
if '-p' in sys.argv[1:]: