~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Martin Pool
  • Date: 2005-09-13 08:21:32 UTC
  • Revision ID: mbp@sourcefrog.net-20050913082132-66279763a02e695c
- allow the same version to be repeatedly added to a weave

  (silently absorb them into a single version)

- tests for this

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
 
88
88
 
89
89
import sha
 
90
 
90
91
from cStringIO import StringIO
91
92
 
 
93
from bzrlib.osutils import sha_strings
 
94
 
92
95
 
93
96
class WeaveError(Exception):
94
97
    """Exception in processing weave"""
222
225
    def idx_to_name(self, version):
223
226
        return self._names[version]
224
227
 
 
228
 
 
229
    def _check_repeated_add(self, name, parents, text):
 
230
        """Check that a duplicated add is OK.
 
231
 
 
232
        If it is, return the (old) index; otherwise raise an exception.
 
233
        """
 
234
        idx = self.lookup(name)
 
235
        if sorted(self._parents[idx]) != sorted(parents):
 
236
            raise WeaveError("name \"%s\" already present in weave "
 
237
                             "with different parents" % name)
 
238
        new_sha1 = sha_strings(text)
 
239
        if new_sha1 != self._sha1s[idx]:
 
240
            raise WeaveError("name \"%s\" already present in weave "
 
241
                             "with different text" % name)            
 
242
        return idx
 
243
        
 
244
 
225
245
        
226
246
    def add(self, name, parents, text):
227
247
        """Add a single text on top of the weave.
240
260
 
241
261
        assert isinstance(name, basestring)
242
262
        if name in self._name_map:
243
 
            raise WeaveError("name %r already present in weave" % name)
 
263
            return self._check_repeated_add(name, parents, text)
244
264
        
245
265
        self._check_versions(parents)
246
266
        ## self._check_lines(text)
247
267
        new_version = len(self._parents)
248
268
 
249
 
        s = sha.new()
250
 
        map(s.update, text)
251
 
        sha1 = s.hexdigest()
252
 
        del s
 
269
        sha1 = sha_strings(text)
253
270
 
254
271
        # if we abort after here the (in-memory) weave will be corrupt because only
255
272
        # some fields are updated