1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# Author: Martin Pool <mbp@canonical.com>
22
"""Store and retrieve weaves in files.
24
There is one format marker followed by a blank line, followed by a
25
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.
35
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
36
processing instructions. Lines of text are prefixed by '.' if the
37
line contains a newline, or ',' if not.
40
# 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'
47
def write_weave(weave, f, format=None):
48
if format == None or format == 1:
49
return write_weave_v5(weave, f)
51
raise ValueError("unknown weave format %r" % format)
54
def write_weave_v5(weave, f):
55
"""Write weave to file f."""
58
for version, included in enumerate(weave._parents):
60
# mininc = weave.minimal_parents(version)
68
print >>f, '1', weave._sha1s[version]
69
print >>f, 'n', weave._names[version]
74
for l in weave._weave:
75
if isinstance(l, tuple):
80
print >>f, '%s %d' % l
85
assert l.find('\n', 0, -1) == -1
88
assert l.find('\n') == -1
95
def read_weave(f, prelude=False):
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))
100
_read_weave_v5(f, w, prelude=prelude)
104
def _read_weave_v5(f, w, prelude=False):
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
from weave import WeaveFormatError
113
raise WeaveFormatError('invalid weave file header: %r' % l)
120
w._parents.append(map(int, l[2:].split(' ')))
122
w._parents.append([])
124
l = f.readline()[:-1]
125
assert l.startswith('1 ')
126
w._sha1s.append(l[2:])
129
assert l.startswith('n ')
131
assert name not in w._name_map
132
w._names.append(name)
133
w._name_map[name] = ver
142
raise WeaveFormatError('unexpected line %r' % l)
151
elif l.startswith('. '):
152
w._weave.append(l[2:]) # include newline
153
elif l.startswith(', '):
154
w._weave.append(l[2:-1]) # exclude newline
156
w._weave.append(('}', None))
158
assert l[0] in '{[]', l
159
assert l[1] == ' ', l
160
w._weave.append((intern(l[0]), int(l[2:])))