~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

[merge] test renames and other fixes (John)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
 
3
1
# Copyright (C) 2005 Canonical Ltd
4
2
 
5
3
# This program is free software; you can redistribute it and/or modify
26
24
There is one format marker followed by a blank line, followed by a
27
25
series of version headers, followed by the weave itself.
28
26
 
29
 
Each version marker has 'i' and the included previous versions, then
30
 
'1' and the SHA-1 of the text, if known.  The inclusions do not need
31
 
to list versions included by a parent.
 
27
Each version marker has
 
28
 
 
29
 'i'   parent version indexes
 
30
 '1'   SHA-1 of text
 
31
 'n'   name
 
32
 
 
33
The inclusions do not need to list versions included by a parent.
32
34
 
33
35
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
34
36
processing instructions.  Lines of text are prefixed by '.' if the
39
41
# an iterator returning the weave lines...  We don't really need to
40
42
# deserialize it into memory.
41
43
 
42
 
FORMAT_1 = '# bzr weave file v4\n'
 
44
FORMAT_1 = '# bzr weave file v5\n'
43
45
 
44
46
 
45
47
def write_weave(weave, f, format=None):
46
48
    if format == None or format == 1:
47
 
        return write_weave_v4(weave, f)
 
49
        return write_weave_v5(weave, f)
48
50
    else:
49
51
        raise ValueError("unknown weave format %r" % format)
50
52
 
51
53
 
52
 
def write_weave_v4(weave, f):
 
54
def write_weave_v5(weave, f):
53
55
    """Write weave to file f."""
54
56
    print >>f, FORMAT_1,
55
57
 
64
66
        else:
65
67
            print >>f, 'i'
66
68
        print >>f, '1', weave._sha1s[version]
 
69
        print >>f, 'n', weave._names[version]
67
70
        print >>f
68
71
 
69
72
    print >>f, 'w'
90
93
 
91
94
 
92
95
def read_weave(f):
93
 
    return read_weave_v4(f)
94
 
 
95
 
 
96
 
def read_weave_v4(f):
 
96
    return read_weave_v5(f)
 
97
 
 
98
 
 
99
def read_weave_v5(f):
97
100
    from weave import Weave, WeaveFormatError
98
 
    w = Weave()
 
101
    w = Weave(getattr(f, 'name', None))
99
102
 
100
 
    wfe = WeaveFormatError
101
103
    l = f.readline()
102
104
    if l != FORMAT_1:
103
105
        raise WeaveFormatError('invalid weave file header: %r' % l)
106
108
    while True:
107
109
        l = f.readline()
108
110
        if l[0] == 'i':
109
 
            ver += 1
110
 
 
111
111
            if len(l) > 2:
112
 
                w._parents.append(frozenset(map(int, l[2:].split(' '))))
 
112
                w._parents.append(map(int, l[2:].split(' ')))
113
113
            else:
114
 
                w._parents.append(frozenset())
 
114
                w._parents.append([])
115
115
 
116
116
            l = f.readline()[:-1]
117
117
            assert l.startswith('1 ')
118
118
            w._sha1s.append(l[2:])
119
119
                
120
120
            l = f.readline()
 
121
            assert l.startswith('n ')
 
122
            name = l[2:-1]
 
123
            assert name not in w._name_map
 
124
            w._names.append(name)
 
125
            w._name_map[name] = ver
 
126
                
 
127
            l = f.readline()
121
128
            assert l == '\n'
 
129
 
 
130
            ver += 1
122
131
        elif l == 'w\n':
123
132
            break
124
133
        else: