63
62
# binaries, perhaps by naively splitting on \n or perhaps using
64
63
# something like a rolling checksum.
66
# TODO: Track version names as well as indexes.
68
65
# TODO: End marker for each version so we can stop reading?
70
67
# TODO: Check that no insertion occurs inside a deletion that was
84
81
# have slight specializations for different ways its used: annotate,
85
82
# basis for add, get, etc.
84
# TODO: Perhaps the API should work only in names to hide the integer
85
# indexes from the user?
173
174
each version; the parent's parents are implied.
176
List of hex SHA-1 of each version, or None if not recorded.
177
List of hex SHA-1 of each version.
180
List of symbolic names for each version. Each should be unique.
183
For each name, the version number.
179
__slots__ = ['_weave', '_parents', '_sha1s']
186
__slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map']
181
188
def __init__(self):
183
190
self._parents = []
187
196
def __eq__(self, other):
188
197
if not isinstance(other, Weave):
190
199
return self._parents == other._parents \
191
and self._weave == other._weave
200
and self._weave == other._weave \
201
and self._sha1s == other._sha1s
194
204
def __ne__(self, other):
195
205
return not self.__eq__(other)
208
def lookup(self, name):
210
return self._name_map[name]
212
raise WeaveError("name %s not present in weave" % name)
198
def add(self, parents, text):
215
def add(self, name, parents, text):
199
216
"""Add a single text on top of the weave.
201
218
Returns the index number of the newly added version.
221
Symbolic name for this version.
222
(Typically the revision-id of the revision that added it.)
204
225
List or set of direct parent version numbers.
207
228
Sequence of lines to be added in the new version."""
230
assert isinstance(name, basestring)
231
if name in self._name_map:
232
raise WeaveError("name %r already present in weave" % name)
209
234
self._check_versions(parents)
210
235
## self._check_lines(text)
211
236
new_version = len(self._parents)
215
240
sha1 = s.hexdigest()
218
# if we abort after here the weave will be corrupt
219
self._parents.append(frozenset(parents))
243
# if we abort after here the (in-memory) weave will be corrupt because only
244
# some fields are updated
245
self._parents.append(parents[:])
220
246
self._sha1s.append(sha1)
247
self._names.append(name)
248
self._name_map[name] = new_version
684
712
def weave_toc(w):
685
713
"""Show the weave's table-of-contents"""
686
print '%6s %40s %20s' % ('ver', 'sha1', 'parents')
687
for i in (6, 40, 20):
714
print '%6s %50s %10s %10s' % ('ver', 'name', 'sha1', 'parents')
715
for i in (6, 50, 10, 10):
690
718
for i in range(w.numversions()):
691
719
sha1 = w._sha1s[i]
692
print '%6d %40s %s' % (i, sha1, ' '.join(map(str, w._parents[i])))
721
parent_str = ' '.join(map(str, w._parents[i]))
722
print '%6d %-50.50s %10.10s %s' % (i, name, sha1, parent_str)