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...
41
FORMAT_1 = '# bzr weave file v3\n'
44
def write_weave(weave, f, format=None):
45
if format == None or format == 1:
46
return write_weave_v1(weave, f)
48
raise ValueError("unknown weave format %r" % format)
51
def write_weave_v1(weave, f):
52
"""Write weave to file f."""
55
for version, included in enumerate(weave._v):
57
# mininc = weave.minimal_parents(version)
65
print >>f, '1', weave._sha1s[version]
71
if isinstance(l, tuple):
73
print >>f, '%s %d' % l
78
assert l.find('\n', 0, -1) == -1
81
assert l.find('\n') == -1
89
return read_weave_v1(f)
93
from weave import Weave, WeaveFormatError
96
wfe = WeaveFormatError
99
raise WeaveFormatError('invalid weave file header: %r' % l)
108
included = map(int, l[2:].split(' '))
109
w._addversion(included)
113
l = f.readline()[:-1]
114
assert l.startswith('1 ')
115
w._sha1s.append(l[2:])
122
raise WeaveFormatError('unexpected line %r' % l)
128
elif l.startswith('. '):
129
w._l.append(intern(l[2:])) # include newline
130
elif l.startswith(', '):
131
w._l.append(l[2:-1]) # exclude newline
133
assert l[0] in '{}[]', l
134
assert l[1] == ' ', l
135
w._l.append((intern(l[0]), int(l[2:])))