Rev 3788: (robertc) Allow recovery of the text when sha1 checks fail. (Robert in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Oct 21 07:01:43 BST 2008


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 3788
revision-id: pqm at pqm.ubuntu.com-20081021060139-fpwr4fxr2oww2x5o
parent: pqm at pqm.ubuntu.com-20081021012629-d2xnxqmr42xrtiy2
parent: robertc at robertcollins.net-20081021034713-dhjfb9ypmnyr6nga
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2008-10-21 07:01:39 +0100
message:
  (robertc) Allow recovery of the text when sha1 checks fail. (Robert
  	Collins)
modified:
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/knit.py                 knit.py-20051212171256-f056ac8f0fbe1bd9
  bzrlib/tests/test_knit.py      test_knit.py-20051212171302-95d4c00dd5f11f2b
    ------------------------------------------------------------
    revno: 3787.1.2
    revision-id: robertc at robertcollins.net-20081021034713-dhjfb9ypmnyr6nga
    parent: robertc at robertcollins.net-20081021033614-e29ktibkuh7e5zps
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: knits
    timestamp: Tue 2008-10-21 14:47:13 +1100
    message:
      Ensure SHA1KnitCorrupt formats ok.
    modified:
      bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
      bzrlib/tests/test_knit.py      test_knit.py-20051212171302-95d4c00dd5f11f2b
    ------------------------------------------------------------
    revno: 3787.1.1
    revision-id: robertc at robertcollins.net-20081021033614-e29ktibkuh7e5zps
    parent: pqm at pqm.ubuntu.com-20081021012629-d2xnxqmr42xrtiy2
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: knits
    timestamp: Tue 2008-10-21 14:36:14 +1100
    message:
      Embed the failed text in sha1 knit errors.
    modified:
      bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
      bzrlib/knit.py                 knit.py-20051212171256-f056ac8f0fbe1bd9
      bzrlib/tests/test_knit.py      test_knit.py-20051212171302-95d4c00dd5f11f2b
=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2008-10-01 05:40:45 +0000
+++ b/bzrlib/errors.py	2008-10-21 03:47:13 +0000
@@ -1424,6 +1424,21 @@
         self.how = how
 
 
+class SHA1KnitCorrupt(KnitCorrupt):
+
+    _fmt = ("Knit %(filename)s corrupt: sha-1 of reconstructed text does not "
+        "match expected sha-1. key %(key)s expected sha %(expected)s actual "
+        "sha %(actual)s")
+
+    def __init__(self, filename, actual, expected, key, content):
+        KnitError.__init__(self)
+        self.filename = filename
+        self.actual = actual
+        self.expected = expected
+        self.key = key
+        self.content = content
+
+
 class KnitDataStreamIncompatible(KnitError):
     # Not raised anymore, as we can convert data streams.  In future we may
     # need it again for more exotic cases, so we're keeping it around for now.

=== modified file 'bzrlib/knit.py'
--- a/bzrlib/knit.py	2008-10-17 16:14:20 +0000
+++ b/bzrlib/knit.py	2008-10-21 03:36:14 +0000
@@ -95,6 +95,7 @@
     KnitHeaderError,
     RevisionNotPresent,
     RevisionAlreadyPresent,
+    SHA1KnitCorrupt,
     )
 from bzrlib.osutils import (
     contains_whitespace,
@@ -1054,12 +1055,7 @@
             text = content.text()
             actual_sha = sha_strings(text)
             if actual_sha != digest:
-                raise KnitCorrupt(self,
-                    '\n  sha-1 %s'
-                    '\n  of reconstructed text does not match'
-                    '\n  expected %s'
-                    '\n  for version %s' %
-                    (actual_sha, digest, key))
+                raise SHA1KnitCorrupt(self, actual_sha, digest, key, text)
             text_map[key] = text
         return text_map, final_content
 
@@ -1671,7 +1667,6 @@
         return result
 
 
-
 class _KndxIndex(object):
     """Manages knit index files
 

=== modified file 'bzrlib/tests/test_knit.py'
--- a/bzrlib/tests/test_knit.py	2008-09-26 07:09:50 +0000
+++ b/bzrlib/tests/test_knit.py	2008-10-21 03:47:13 +0000
@@ -54,6 +54,7 @@
     TestCase,
     TestCaseWithMemoryTransport,
     TestCaseWithTransport,
+    TestNotApplicable,
     )
 from bzrlib.transport import get_transport
 from bzrlib.transport.memory import MemoryTransport
@@ -920,6 +921,32 @@
         return make_file_factory(annotate, mapper)(self.get_transport())
 
 
+class TestBadShaError(KnitTests):
+    """Tests for handling of sha errors."""
+
+    def test_exception_has_text(self):
+        # having the failed text included in the error allows for recovery.
+        source = self.make_test_knit()
+        target = self.make_test_knit(name="target")
+        if not source._max_delta_chain:
+            raise TestNotApplicable(
+                "cannot get delta-caused sha failures without deltas.")
+        # create a basis
+        basis = ('basis',)
+        broken = ('broken',)
+        source.add_lines(basis, (), ['foo\n'])
+        source.add_lines(broken, (basis,), ['foo\n', 'bar\n'])
+        # Seed target with a bad basis text
+        target.add_lines(basis, (), ['gam\n'])
+        target.insert_record_stream(
+            source.get_record_stream([broken], 'unordered', False))
+        err = self.assertRaises(errors.KnitCorrupt,
+            target.get_record_stream([broken], 'unordered', True).next)
+        self.assertEqual(['gam\n', 'bar\n'], err.content)
+        # Test for formatting with live data
+        self.assertStartsWith(str(err), "Knit ")
+
+
 class TestKnitIndex(KnitTests):
 
     def test_add_versions_dictionary_compresses(self):




More information about the bazaar-commits mailing list