13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""bzr upgrade logic."""
20
from bzrlib.bzrdir import BzrDir, format_registry
21
import bzrlib.errors as errors
22
from bzrlib.remote import RemoteBzrDir
23
import bzrlib.ui as ui
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
26
98
class Convert(object):
28
def __init__(self, url, format=None):
30
self.bzrdir = BzrDir.open_unsupported(url)
31
# XXX: Change to cleanup
32
warning_id = 'cross_format_fetch'
33
saved_warning = warning_id in ui.ui_factory.suppressed_warnings
34
if isinstance(self.bzrdir, RemoteBzrDir):
35
self.bzrdir._ensure_real()
36
self.bzrdir = self.bzrdir._real_bzrdir
37
if self.bzrdir.root_transport.is_readonly():
38
raise errors.UpgradeReadonly
39
self.transport = self.bzrdir.root_transport
40
ui.ui_factory.suppressed_warnings.add(warning_id)
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')
118
# holds in-memory weaves for all files
119
self.text_weaves = {}
121
b = self.branch = Branch('.', relax_version_check=True)
124
rev_history = b.revision_history()
128
# to_read is a stack holding the revisions we still need to process;
129
# appending to it adds new highest-priority revisions
131
self.known_revisions = set(rev_history)
132
self.to_read = [rev_history[-1]]
134
rev_id = self.to_read.pop()
135
if (rev_id not in self.revisions
136
and rev_id not in self.absent_revisions):
137
self._load_one_rev(rev_id)
139
to_import = self._make_order()
140
for i, rev_id in enumerate(to_import):
141
self.pb.update('converting revision', i, len(to_import))
142
self._convert_one_rev(rev_id)
144
print '(not really) upgraded to weaves:'
145
print ' %6d revisions and inventories' % len(self.revisions)
146
print ' %6d absent revisions removed' % len(self.absent_revisions)
147
print ' %6d texts' % self.text_count
149
self._write_all_weaves()
152
def _write_all_weaves(self):
154
write_atomic_weave(self.inv_weave, 'weaves/inventory.weave')
156
for file_id, file_weave in self.text_weaves.items():
157
self.pb.update('writing weave', i, len(self.text_weaves))
158
write_atomic_weave(file_weave, 'weaves/%s.weave' % file_id)
45
ui.ui_factory.suppressed_warnings.remove(warning_id)
49
branch = self.bzrdir.open_branch()
50
if branch.user_url != self.bzrdir.user_url:
51
ui.ui_factory.note("This is a checkout. The branch (%s) needs to be "
52
"upgraded separately." %
55
except (errors.NotBranchError, errors.IncompatibleRepositories):
56
# might not be a format we can open without upgrading; see e.g.
57
# https://bugs.launchpad.net/bzr/+bug/253891
59
if self.format is None:
61
rich_root = self.bzrdir.find_repository()._format.rich_root_data
62
except errors.NoRepositoryPresent:
63
rich_root = False # assume no rich roots
65
format_name = "default-rich-root"
67
format_name = "default"
68
format = format_registry.make_bzrdir(format_name)
71
if not self.bzrdir.needs_format_conversion(format):
72
raise errors.UpToDateFormat(self.bzrdir._format)
73
if not self.bzrdir.can_convert_format():
74
raise errors.BzrError("cannot upgrade from bzrdir format %s" %
76
self.bzrdir.check_conversion_target(format)
77
ui.ui_factory.note('starting upgrade of %s' % self.transport.base)
79
self.bzrdir.backup_bzrdir()
80
while self.bzrdir.needs_format_conversion(format):
81
converter = self.bzrdir._format.get_converter(format)
82
self.bzrdir = converter.convert(self.bzrdir, None)
83
ui.ui_factory.note("finished")
86
def upgrade(url, format=None):
87
"""Upgrade to format, or the default bzrdir format if not supplied."""
162
## write_atomic_weave(self.anc_weave, 'weaves/ancestry.weave')
165
def _load_one_rev(self, rev_id):
166
"""Load a revision object into memory.
168
Any parents not either loaded or abandoned get queued to be
170
self.pb.update('loading revision',
172
len(self.known_revisions))
173
if rev_id not in self.branch.revision_store:
175
note('revision {%s} not present in branch; '
176
'will not be converted',
178
self.absent_revisions.add(rev_id)
180
rev_xml = self.branch.revision_store[rev_id].read()
181
rev = serializer_v4.read_revision_from_string(rev_xml)
182
for parent_id in rev.parent_ids:
183
self.known_revisions.add(parent_id)
184
self.to_read.append(parent_id)
185
self.revisions[rev_id] = rev
186
old_inv_xml = self.branch.inventory_store[rev_id].read()
187
inv = serializer_v4.read_inventory_from_string(old_inv_xml)
188
assert rev.inventory_sha1 == sha_string(old_inv_xml)
189
self.inventories[rev_id] = inv
192
def _convert_one_rev(self, rev_id):
193
"""Convert revision and all referenced objects to new format."""
194
rev = self.revisions[rev_id]
195
inv = self.inventories[rev_id]
196
self._convert_revision_contents(rev, inv)
197
# the XML is now updated with text versions
198
new_inv_xml = serializer_v5.write_inventory_to_string(inv)
199
inv_parents = [x for x in self.revisions[rev_id].parent_ids
200
if x not in self.absent_revisions]
201
new_inv_sha1 = sha_string(new_inv_xml)
202
self.inv_weave.add(rev_id, inv_parents,
203
new_inv_xml.splitlines(True),
205
# TODO: Upgrade revision XML and write that out
206
rev.inventory_sha1 = new_inv_sha1
207
self.converted_revs.add(rev_id)
210
def _convert_revision_contents(self, rev, inv):
211
"""Convert all the files within a revision.
213
Also upgrade the inventory to refer to the text revision ids."""
214
rev_id = rev.revision_id
215
mutter('converting texts of revision {%s}',
217
for path, ie in inv.iter_entries():
218
if ie.kind != 'file':
220
self._convert_file_version(rev, ie)
221
# TODO: Check and convert name versions
224
def _convert_file_version(self, rev, ie):
225
"""Convert one version of one file.
227
The file needs to be added into the weave if it is a merge
228
of >=2 parents or if it's changed from its parent.
231
rev_id = rev.revision_id
232
w = self.text_weaves.get(file_id)
235
self.text_weaves[file_id] = w
236
file_lines = self.branch.text_store[ie.text_id].readlines()
237
assert sha_strings(file_lines) == ie.text_sha1
238
assert sum(map(len, file_lines)) == ie.text_size
241
for parent_id in rev.parent_ids:
242
if parent_id in self.absent_revisions:
244
assert parent_id in self.converted_revs
245
parent_inv = self.inventories[parent_id]
246
if parent_inv.has_id(file_id):
247
parent_ie = parent_inv[file_id]
248
old_text_version = parent_ie.text_version
249
assert old_text_version in self.converted_revs
250
if old_text_version not in file_parents:
251
file_parents.append(old_text_version)
252
if parent_ie.text_sha1 != ie.text_sha1:
254
if len(file_parents) != 1 or text_changed:
255
w.add(rev_id, file_parents, file_lines, ie.text_sha1)
256
ie.name_version = ie.text_version = rev_id
257
mutter('import text {%s} of {%s}',
260
mutter('text of {%s} unchanged from parent', file_id)
261
ie.text_version = file_parents[0]
262
ie.name_version = file_parents[0]
267
def _make_order(self):
268
"""Return a suitable order for importing revisions.
270
The order must be such that an revision is imported after all
271
its (present) parents.
273
todo = set(self.revisions.keys())
274
done = self.absent_revisions.copy()
277
# scan through looking for a revision whose parents
279
for rev_id in sorted(list(todo)):
280
rev = self.revisions[rev_id]
281
parent_ids = set(rev.parent_ids)
282
if parent_ids.issubset(done):
283
# can take this one now
290
def write_atomic_weave(weave, filename):
291
inv_wf = AtomicFile(filename)
293
write_weave(weave, inv_wf)
301
def profile_convert():
302
prof_f = tempfile.NamedTemporaryFile()
304
prof = hotshot.Profile(prof_f.name)
306
prof.runcall(Convert)
309
stats = hotshot.stats.load(prof_f.name)
311
stats.sort_stats('time')
312
# XXX: Might like to write to stderr or the trace file instead but
313
# print_stats seems hardcoded to stdout
314
stats.print_stats(20)
317
enable_default_logging()
319
if '-p' in sys.argv[1:]: