2
"""Convert a pack-0.92 repository into a 1.9 (btree) repository.
4
This works directly on the indices, rather than using the generic conversion
5
logic. After conversion, it will have backed up your old indices to
6
.bzr/repository/indices-gi. This is significantly faster than the generic 'bzr
7
upgrade' but it does not work for all repository formats (only pack format
8
repositories are supported).
13
def add_revert_step(func, *args, **kwargs):
14
steps_to_revert.append((func, args, kwargs))
17
print "** Reverting repository"
18
for func, args, kwargs in reversed(steps_to_revert):
24
p = optparse.OptionParser(usage='%prog [options]\n' + __doc__)
26
opts, args = p.parse_args(args)
28
from bzrlib import ui, trace, repository
29
from bzrlib.ui import text
30
from bzrlib.repofmt import pack_repo
31
ui.ui_factory = text.TextUIFactory()
32
trace.enable_default_logging()
34
trace.note('processing "."')
36
fmt = getattr(pack_repo, 'RepositoryFormatKnitPack6', None)
38
trace.note("** Your bzrlib does not have RepositoryFormatPack6 (--1.9)")
39
trace.note(" upgrade your bzrlib installation.")
42
r = repository.Repository.open('.')
43
if isinstance(r._format, (pack_repo.RepositoryFormatKnitPack1, # 0.92
44
pack_repo.RepositoryFormatKnitPack5, # 1.6
46
fmt = pack_repo.RepositoryFormatKnitPack6
47
elif isinstance(r._format, (pack_repo.RepositoryFormatKnitPack4, # rich-root-pack
48
pack_repo.RepositoryFormatKnitPack5RichRoot, # 1.6.1-rich-root
50
fmt = pack_repo.RepositoryFormatKnitPack6RichRoot
51
elif isinstance(r._format, (pack_repo.RepositoryFormatKnitPack6, # 1.9
52
pack_repo.RepositoryFormatKnitPack6RichRoot, # 1.9-rich-root
54
trace.note("Repository is already upgraded to: %s", r._format)
57
trace.note("** Do not know how to upgrade a repository of format:")
58
trace.note(" %s", (r._format,))
62
t.rename('format', 'format-gi')
63
add_revert_step(t.rename, 'format-gi', 'format')
65
'Bazaar Repository Upgrade to 1.9 (%s) in progress\n' % (fmt,))
66
add_revert_step(t.delete, 'format')
67
pb = ui.ui_factory.nested_progress_bar()
69
do_upgrade(r, fmt, pb)
77
def do_upgrade(r, fmt, pb):
78
from bzrlib import errors, repository, transport, ui, index, btree_index
79
from bzrlib.repofmt import pack_repo
81
index_t = t.clone('indices')
82
btree_index_t = t.clone('indices-btree')
85
btree_index_t.mkdir('.')
86
except errors.FileExists:
90
files = index_t.list_dir('.')
91
step_count = len(files)*2
92
for idx, n in enumerate(files):
93
gi = index.GraphIndex(index_t, n, index_t.stat(n).st_size)
94
key_count = gi.key_count()
95
msg = 'copying %s (%5d)' % (n, key_count)
96
pb.update(msg, 2*idx, step_count)
97
new_bi = btree_index.BTreeBuilder(gi.node_ref_lists, gi._key_length)
98
new_bi.add_nodes(x[1:] for x in gi.iter_all_entries())
99
pb.update(msg, 2*idx+1, step_count)
100
size = btree_index_t.put_file(n, new_bi.finish())
101
names_to_sizes[n] = size
104
ext_to_offset = {'rix':0, 'iix':1, 'tix':2, 'six':3}
106
for n, size in names_to_sizes.iteritems():
107
base, ext = n.split('.')
108
sizes = base_to_sizes.setdefault(base, [None, None, None, None])
109
sizes[ext_to_offset[ext]] = size
111
# We upgrade all index files, but all of them may not be referenced by
112
# pack-names, so make sure to only include the referenced ones.
113
pack_name_gi = index.GraphIndex(t, 'pack-names', None)
114
pack_name_gi.key_count() # Parse the header
115
pack_name_bi = btree_index.BTreeBuilder(pack_name_gi.node_ref_lists,
116
pack_name_gi._key_length)
117
for index, key, value in pack_name_gi.iter_all_entries():
118
for x in base_to_sizes[key[0]]:
120
new_value = ' '.join(map(str, base_to_sizes[key[0]]))
121
pack_name_bi.add_node(key, new_value)
123
t.put_file('pack-names-btree', pack_name_bi.finish())
126
# While we swap everything out, block other clients.
127
t.rename('pack-names', 'pack-names-gi')
128
add_revert_step(t.rename, 'pack-names-gi', 'pack-names')
129
t.rename('indices', 'indices-gi')
130
add_revert_step(t.rename, 'indices-gi', 'indices')
131
t.rename('pack-names-btree', 'pack-names')
132
add_revert_step(t.rename, 'pack-names', 'pack-names-btree')
133
t.rename('indices-btree', 'indices')
134
add_revert_step(t.rename, 'indices', 'indices-btree')
135
t.put_bytes('format', fmt().get_format_string())
136
# format will be deleted by the earlier steps
139
if __name__ == '__main__':