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 '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.
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
41
39
# an iterator returning the weave lines... We don't really need to
42
40
# deserialize it into memory.
44
FORMAT_1 = '# bzr weave file v5\n'
42
FORMAT_1 = '# bzr weave file v4\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_v4(weave, f)
51
49
raise ValueError("unknown weave format %r" % format)
54
def write_weave_v5(weave, f):
52
def write_weave_v4(weave, f):
55
53
"""Write weave to file f."""
56
54
print >>f, FORMAT_1,
96
# FIXME: detect the weave type and dispatch
97
from bzrlib.trace import mutter
98
from weave import Weave
99
w = Weave(getattr(f, 'name', None))
104
def _read_weave_v5(f, w):
105
"""Private helper routine to read a weave format 5 file into memory.
107
This is only to be used by read_weave and WeaveFile.__init__.
109
# 200 0 2075.5080 1084.0360 bzrlib.weavefile:104(_read_weave_v5)
110
# +60412 0 366.5900 366.5900 +<method 'readline' of 'file' objects>
111
# +59982 0 320.5280 320.5280 +<method 'startswith' of 'str' objects>
112
# +59363 0 297.8080 297.8080 +<method 'append' of 'list' objects>
113
# replace readline call with iter over all lines ->
114
# safe because we already suck on memory.
115
# 200 0 1492.7170 802.6220 bzrlib.weavefile:104(_read_weave_v5)
116
# +59982 0 329.9100 329.9100 +<method 'startswith' of 'str' objects>
117
# +59363 0 320.2980 320.2980 +<method 'append' of 'list' objects>
118
# replaced startswith with slice lookups:
119
# 200 0 851.7250 501.1120 bzrlib.weavefile:104(_read_weave_v5)
120
# +59363 0 311.8780 311.8780 +<method 'append' of 'list' objects>
121
# +200 0 30.2500 30.2500 +<method 'readlines' of 'file' objects>
123
from weave import WeaveFormatError
125
lines = iter(f.readlines())
93
return read_weave_v4(f)
97
from weave import Weave, WeaveFormatError
100
wfe = WeaveFormatError
128
102
if l != FORMAT_1:
129
103
raise WeaveFormatError('invalid weave file header: %r' % l)
137
w._parents.append(map(int, l[2:].split(' ')))
112
w._parents.append(frozenset(map(int, l[2:].split(' '))))
139
w._parents.append([])
114
w._parents.append(frozenset())
141
l = lines.next()[:-1]
142
assert '1 ' == l[0:2]
116
l = f.readline()[:-1]
117
assert l.startswith('1 ')
143
118
w._sha1s.append(l[2:])
146
assert 'n ' == l[0:2]
148
assert name not in w._name_map
149
w._names.append(name)
150
w._name_map[name] = ver
159
125
raise WeaveFormatError('unexpected line %r' % l)
131
elif l.startswith('. '):
167
132
w._weave.append(l[2:]) # include newline
133
elif l.startswith(', '):
169
134
w._weave.append(l[2:-1]) # exclude newline
171
136
w._weave.append(('}', None))