24
26
There is one format marker followed by a blank line, followed by a
25
27
series of version headers, followed by the weave itself.
27
Each version marker has
29
'i' parent version indexes
33
The inclusions do not need to list versions included by a parent.
29
Each version marker has 'v' and the version, then 'i' and the included
30
previous versions. The inclusions do not need to list versions
35
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
36
34
processing instructions. Lines of text are prefixed by '.' if the
40
38
# TODO: When extracting a single version it'd be enough to just pass
41
# an iterator returning the weave lines... We don't really need to
42
# deserialize it into memory.
44
FORMAT_1 = '# bzr weave file v5\n'
39
# an iterator returning the weave lines...
41
FORMAT_1 = '# bzr weave file v1\n'
47
45
def write_weave(weave, f, format=None):
48
46
if format == None or format == 1:
49
return write_weave_v5(weave, f)
47
return write_weave_v1(weave, f)
51
49
raise ValueError("unknown weave format %r" % format)
54
def write_weave_v5(weave, f):
52
def write_weave_v1(weave, f):
55
53
"""Write weave to file f."""
56
54
print >>f, FORMAT_1,
58
for version, included in enumerate(weave._parents):
56
for version, included in enumerate(weave._v):
57
print >>f, 'v', version
60
# mininc = weave.minimal_parents(version)
59
# find a minimal expression of it; bias towards using
71
gotit.update(weave._v[pv])
74
assert mininc[-1] < version
68
print >>f, '1', weave._sha1s[version]
69
print >>f, 'n', weave._names[version]
74
for l in weave._weave:
75
86
if isinstance(l, tuple):
76
87
assert l[0] in '{}[]'
80
print >>f, '%s %d' % l
88
print >>f, '%s %d' % l
95
103
def read_weave(f):
96
return read_weave_v5(f)
104
return read_weave_v1(f)
107
def read_weave_v1(f):
100
108
from weave import Weave, WeaveFormatError
101
w = Weave(getattr(f, 'name', None))
111
wfe = WeaveFormatError
104
113
if l != FORMAT_1:
105
114
raise WeaveFormatError('invalid weave file header: %r' % l)
119
if l.startswith('v '):
122
raise WeaveFormatError('version %d!=%d out of order'
126
l = f.readline()[:-1]
128
raise WeaveFormatError('unexpected line %r' % l)
112
w._parents.append(map(int, l[2:].split(' ')))
130
included = map(int, l[2:].split(' '))
134
full.update(w._v[pv])
114
w._parents.append([])
116
l = f.readline()[:-1]
117
assert l.startswith('1 ')
118
w._sha1s.append(l[2:])
121
assert l.startswith('n ')
123
assert name not in w._name_map
124
w._names.append(name)
125
w._name_map[name] = ver
138
assert f.readline() == '\n'
140
148
elif l.startswith('. '):
141
w._weave.append(l[2:]) # include newline
149
w._l.append(l[2:]) # include newline
142
150
elif l.startswith(', '):
143
w._weave.append(l[2:-1]) # exclude newline
145
w._weave.append(('}', None))
151
w._l.append(l[2:-1]) # exclude newline
147
assert l[0] in '{[]', l
153
assert l[0] in '{}[]', l
148
154
assert l[1] == ' ', l
149
w._weave.append((intern(l[0]), int(l[2:])))
155
w._l.append((l[0], int(l[2:])))