1
# Convert a pack-0.92 repository into a 1.9 (btree) repository
5
def add_revert_step(func, *args, **kwargs):
6
steps_to_revert.append((func, args, kwargs))
9
print "** Reverting repository"
10
for func, args, kwargs in reversed(steps_to_revert):
16
p = optparse.OptionParser()
18
opts, args = p.parse_args(args)
20
from bzrlib import ui, trace, repository
21
from bzrlib.ui import text
22
from bzrlib.repofmt import pack_repo
23
ui.ui_factory = text.TextUIFactory()
24
trace.enable_default_logging()
26
trace.note('processing "."')
28
fmt = getattr(pack_repo, 'RepositoryFormatKnitPack6', None)
30
trace.note("** Your bzrlib does not have RepositoryFormatPack6 (--1.9)")
31
trace.note(" upgrade your bzrlib installation.")
34
r = repository.Repository.open('.')
35
if isinstance(r._format, (pack_repo.RepositoryFormatKnitPack1, # 0.92
36
pack_repo.RepositoryFormatKnitPack5, # 1.6
38
fmt = pack_repo.RepositoryFormatKnitPack6
39
elif isinstance(r._format, (pack_repo.RepositoryFormatKnitPack4, # rich-root-pack
40
pack_repo.RepositoryFormatKnitPack5RichRoot, # 1.6.1-rich-root
42
fmt = pack_repo.RepositoryFormatKnitPack6RichRoot
43
elif isinstance(r._format, (pack_repo.RepositoryFormatKnitPack6, # 1.9
44
pack_repo.RepositoryFormatKnitPack6RichRoot, # 1.9-rich-root
46
trace.note("Repository is already upgraded to: %s", r._format)
49
trace.note("** Do not know how to upgrade a repository of format:")
50
trace.note(" %s", (r._format,))
54
t.rename('format', 'format-gi')
55
add_revert_step(t.rename, 'format-gi', 'format')
57
'Bazaar Repository Upgrade to 1.9 (%s) in progress\n' % (fmt,))
58
add_revert_step(t.delete, 'format')
59
pb = ui.ui_factory.nested_progress_bar()
61
do_upgrade(r, fmt, pb)
69
def do_upgrade(r, fmt, pb):
70
from bzrlib import errors, repository, transport, ui, index, btree_index
71
from bzrlib.repofmt import pack_repo
73
index_t = t.clone('indices')
74
btree_index_t = t.clone('indices-btree')
77
btree_index_t.mkdir('.')
78
except errors.FileExists:
82
files = index_t.list_dir('.')
83
step_count = len(files)*2
84
for idx, n in enumerate(files):
85
gi = index.GraphIndex(index_t, n, index_t.stat(n).st_size)
86
key_count = gi.key_count()
87
msg = 'copying %s (%5d)' % (n, key_count)
88
pb.update(msg, 2*idx, step_count)
89
new_bi = btree_index.BTreeBuilder(gi.node_ref_lists, gi._key_length)
90
new_bi.add_nodes(x[1:] for x in gi.iter_all_entries())
91
pb.update(msg, 2*idx+1, step_count)
92
size = btree_index_t.put_file(n, new_bi.finish())
93
names_to_sizes[n] = size
96
ext_to_offset = {'rix':0, 'iix':1, 'tix':2, 'six':3}
98
for n, size in names_to_sizes.iteritems():
99
base, ext = n.split('.')
100
sizes = base_to_sizes.setdefault(base, [None, None, None, None])
101
sizes[ext_to_offset[ext]] = size
103
# We upgrade all index files, but all of them may not be referenced by
104
# pack-names, so make sure to only include the referenced ones.
105
pack_name_gi = index.GraphIndex(t, 'pack-names', None)
106
pack_name_gi.key_count() # Parse the header
107
pack_name_bi = btree_index.BTreeBuilder(pack_name_gi.node_ref_lists,
108
pack_name_gi._key_length)
109
for index, key, value in pack_name_gi.iter_all_entries():
110
for x in base_to_sizes[key[0]]:
112
new_value = ' '.join(map(str, base_to_sizes[key[0]]))
113
pack_name_bi.add_node(key, new_value)
115
t.put_file('pack-names-btree', pack_name_bi.finish())
118
# While we swap everything out, block other clients.
119
t.rename('pack-names', 'pack-names-gi')
120
add_revert_step(t.rename, 'pack-names-gi', 'pack-names')
121
t.rename('indices', 'indices-gi')
122
add_revert_step(t.rename, 'indices-gi', 'indices')
123
t.rename('pack-names-btree', 'pack-names')
124
add_revert_step(t.rename, 'pack-names', 'pack-names-btree')
125
t.rename('indices-btree', 'indices')
126
add_revert_step(t.rename, 'indices', 'indices-btree')
127
t.put_bytes('format', fmt().get_format_string())
128
# format will be deleted by the earlier steps
131
if __name__ == '__main__':