~bzr-pqm/bzr/bzr.dev

3823.3.2 by John Arbash Meinel
Add a bit of help text when supplying --help.
1
#!/usr/bin/env python
2
"""Convert a pack-0.92 repository into a 1.9 (btree) repository.
3
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).
9
"""
3823.3.1 by John Arbash Meinel
Add convert_to_1.9.py as a contrib script
10
11
12
steps_to_revert = []
13
def add_revert_step(func, *args, **kwargs):
14
    steps_to_revert.append((func, args, kwargs))
15
16
def do_revert():
17
    print "** Reverting repository"
18
    for func, args, kwargs in reversed(steps_to_revert):
19
        func(*args, **kwargs)
20
21
22
def main(args):
23
    import optparse
3823.3.2 by John Arbash Meinel
Add a bit of help text when supplying --help.
24
    p = optparse.OptionParser(usage='%prog [options]\n' + __doc__)
3823.3.1 by John Arbash Meinel
Add convert_to_1.9.py as a contrib script
25
26
    opts, args = p.parse_args(args)
27
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()
33
34
    trace.note('processing "."')
35
36
    fmt = getattr(pack_repo, 'RepositoryFormatKnitPack6', None)
37
    if fmt is None:
38
        trace.note("** Your bzrlib does not have RepositoryFormatPack6 (--1.9)")
39
        trace.note("   upgrade your bzrlib installation.")
40
        return
41
42
    r = repository.Repository.open('.')
43
    if isinstance(r._format, (pack_repo.RepositoryFormatKnitPack1, # 0.92
44
                              pack_repo.RepositoryFormatKnitPack5, # 1.6
45
                              )):
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
49
                               )):
50
        fmt = pack_repo.RepositoryFormatKnitPack6RichRoot
51
    elif isinstance(r._format, (pack_repo.RepositoryFormatKnitPack6, # 1.9
52
                                pack_repo.RepositoryFormatKnitPack6RichRoot, # 1.9-rich-root
53
                               )):
54
        trace.note("Repository is already upgraded to: %s", r._format)
55
        return
56
    else:
57
        trace.note("** Do not know how to upgrade a repository of format:")
58
        trace.note("   %s", (r._format,))
59
        return
60
61
    t = r._transport
62
    t.rename('format', 'format-gi')
63
    add_revert_step(t.rename, 'format-gi', 'format')
64
    t.put_bytes('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()
68
    try:
69
        do_upgrade(r, fmt, pb)
70
    except:
71
        do_revert()
72
        pb.finished()
73
        raise
74
    pb.finished()
75
76
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
80
    t = r._transport
81
    index_t = t.clone('indices')
82
    btree_index_t = t.clone('indices-btree')
83
84
    try:
85
      btree_index_t.mkdir('.')
86
    except errors.FileExists:
87
      pass
88
89
    names_to_sizes = {}
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
102
103
104
    ext_to_offset = {'rix':0, 'iix':1, 'tix':2, 'six':3}
105
    base_to_sizes = {}
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
110
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]]:
119
            assert x is not None
120
        new_value = ' '.join(map(str, base_to_sizes[key[0]]))
121
        pack_name_bi.add_node(key, new_value)
122
123
    t.put_file('pack-names-btree', pack_name_bi.finish())
124
125
    del r
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
137
138
139
if __name__ == '__main__':
140
    import sys
141
    main(sys.argv[1:])