Merge improvements and extra tests, mainly to deal better with creating non-bare...
authorJelmer Vernooij <jelmer@samba.org>
Mon, 17 May 2010 21:50:35 +0000 (23:50 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Mon, 17 May 2010 21:50:35 +0000 (23:50 +0200)
dulwich/objects.py
dulwich/tests/test_objects.py

index 8a644e034b5a5fa220ebb77bcf2c963ac272c294..d78e6d7e00b0c95491eccc2cb3be42852fb82ed2 100644 (file)
@@ -140,6 +140,13 @@ def check_hexsha(hex, error_msg):
 
 
 def check_identity(identity, error_msg):
+    """Check if the specified identity is valid.
+
+    This will raise an exception if the identity is not valid.
+    
+    :param identity: Identity string
+    :param error_msg: Error message to use in exception
+    """
     email_start = identity.find("<")
     email_end = identity.find(">")
     if (email_start < 0 or email_end < 0 or email_end <= email_start
@@ -267,8 +274,8 @@ class ShaFile(object):
         size = os.path.getsize(f.name)
         map = mmap.mmap(f.fileno(), size, access=mmap.ACCESS_READ)
         try:
-            # skip type and size; type must have already been determined, and we
-            # trust zlib to fail if it's otherwise corrupted
+            # skip type and size; type must have already been determined, and
+            # we trust zlib to fail if it's otherwise corrupted
             byte = ord(map[0])
             used = 1
             while (byte & 0x80) != 0:
@@ -427,7 +434,7 @@ class ShaFile(object):
 
     def sha(self):
         """The SHA1 object that is the name of this object."""
-        if self._sha is None:
+        if self._sha is None or self._needs_serialization:
             # this is a local because as_raw_chunks() overwrites self._sha
             new_sha = make_sha()
             new_sha.update(self._header())
index c3b4c69544c9014eb98dcfe0eeaf91c18f6e8ca2..3a00d4345f1358cd9ba37762c2fd8c762b70b2b4 100644 (file)
@@ -187,7 +187,8 @@ class BlobReadTests(unittest.TestCase):
         sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
         c = self.commit(sha)
         self.assertEqual(c.tree, tree_sha)
-        self.assertEqual(c.parents, ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
+        self.assertEqual(c.parents,
+            ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
         self.assertEqual(c.author,
             'James Westby <jw+debian@jameswestby.net>')
         self.assertEqual(c.committer,
@@ -226,13 +227,6 @@ class BlobReadTests(unittest.TestCase):
         self.assertEqual(c.author_timezone, 0)
         self.assertEqual(c.message, 'Merge ../b\n')
 
-    def test_check_id(self):
-        wrong_sha = '1' * 40
-        b = self.get_blob(wrong_sha)
-        self.assertEqual(wrong_sha, b.id)
-        self.assertRaises(ChecksumMismatch, b.check)
-        self.assertEqual('742b386350576589175e374a5706505cbd17680c', b.id)
-
 
 class ShaFileCheckTests(unittest.TestCase):
 
@@ -419,6 +413,13 @@ class TreeTests(ShaFileCheckTests):
         self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
                 x.as_raw_string())
 
+    def test_tree_update_id(self):
+        x = Tree()
+        x["a.c"] = (0100755, "d80c186a03f423a81b39df39dc87fd269736ca86")
+        self.assertEquals("0c5c6bc2c081accfbc250331b19e43b904ab9cdd", x.id)
+        x["a.b"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
+        self.assertEquals("07bfcb5f3ada15bbebdfa3bbb8fd858a363925c8", x.id)
+
     def test_tree_dir_sort(self):
         x = Tree()
         x["a.c"] = (0100755, "d80c186a03f423a81b39df39dc87fd269736ca86")