3
# Copyright (C) 2005 Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# Author: Martin Pool <mbp@canonical.com>
24
"""Store and retrieve weaves in files.
26
There is one format marker followed by a blank line, followed by a
27
series of version headers, followed by the weave itself.
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.
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
34
processing instructions. Lines of text are prefixed by '.' if the
35
line contains a newline, or ',' if not.
38
# TODO: When extracting a single version it'd be enough to just pass
39
# an iterator returning the weave lines... We don't really need to
40
# deserialize it into memory.
42
FORMAT_1 = '# bzr weave file v4\n'
45
def write_weave(weave, f, format=None):
46
if format == None or format == 1:
47
return write_weave_v4(weave, f)
49
raise ValueError("unknown weave format %r" % format)
52
def write_weave_v4(weave, f):
53
"""Write weave to file f."""
56
for version, included in enumerate(weave._parents):
58
# mininc = weave.minimal_parents(version)
66
print >>f, '1', weave._sha1s[version]
71
for l in weave._weave:
72
if isinstance(l, tuple):
77
print >>f, '%s %d' % l
82
assert l.find('\n', 0, -1) == -1
85
assert l.find('\n') == -1
93
return read_weave_v4(f)
97
from weave import Weave, WeaveFormatError
100
wfe = WeaveFormatError
103
raise WeaveFormatError('invalid weave file header: %r' % l)
112
w._parents.append(frozenset(map(int, l[2:].split(' '))))
114
w._parents.append(frozenset())
116
l = f.readline()[:-1]
117
assert l.startswith('1 ')
118
w._sha1s.append(l[2:])
125
raise WeaveFormatError('unexpected line %r' % l)
131
elif l.startswith('. '):
132
w._weave.append(l[2:]) # include newline
133
elif l.startswith(', '):
134
w._weave.append(l[2:-1]) # exclude newline
136
w._weave.append(('}', None))
138
assert l[0] in '{[]', l
139
assert l[1] == ' ', l
140
w._weave.append((intern(l[0]), int(l[2:])))