Browse Source

Support generating index files.

Jelmer Vernooij 16 years ago
parent
commit
854ad99760
3 changed files with 46 additions and 2 deletions
  1. 3 0
      dulwich/objects.py
  2. 27 1
      dulwich/pack.py
  3. 16 1
      dulwich/tests/test_pack.py

+ 3 - 0
dulwich/objects.py

@@ -154,6 +154,9 @@ class ShaFile(object):
     """The raw bytes of this object"""
     return self._contents
 
+  def crc32(self):
+    return zlib.crc32(self._text)
+
   def sha(self):
     """The SHA1 object that is the name of this object."""
     ressha = sha.new()

+ 27 - 1
dulwich/pack.py

@@ -151,6 +151,11 @@ class PackIndex(object):
         self._crc32_table_offset = self._name_table_offset + 20 * len(self)
         self._pack_offset_table_offset = self._crc32_table_offset + 4 * len(self)
 
+  def __eq__(self, other):
+    return (type(self) == type(other) and 
+            self._fan_out_table == other._fan_out_table and
+            list(self.iterentries()) == list(other.iterentries()))
+
   def close(self):
     self._file.close()
 
@@ -333,10 +338,31 @@ class PackData(object):
     for i in range(len(self)):
         map = simple_mmap(f, offset, self._size-offset)
         (type, obj, total_size) = self._unpack_object(map)
-        yield type, obj
+        yield offset, type, obj
         offset += total_size
     f.close()
 
+  def iterentries(self):
+    found = {}
+    for offset, type, obj in self.iterobjects():
+      if type == 6: # offset delta
+         pass # FIXME
+      elif type == 7:
+         pass # FIXME
+      else:
+        shafile = ShaFile.from_raw_string(type, obj)
+        sha = shafile.sha().digest()
+        found[sha] = (type, obj)
+        yield sha, offset, shafile.crc32()
+
+  def create_index_v1(self, filename):
+    entries = list(self.iterentries())
+    write_pack_index_v1(filename, entries, self.calculate_checksum())
+
+  def create_index_v2(self, filename):
+    entries = list(self.iterentries())
+    write_pack_index_v1(filename, entries, self.calculate_checksum())
+
   def check(self):
     return (self.calculate_checksum() == self._stored_checksum)
 

+ 16 - 1
dulwich/tests/test_pack.py

@@ -108,7 +108,22 @@ class TestPackData(PackTests):
 
   def test_iterobjects(self):
     p = self.get_pack_data(pack1_sha)
-    self.assertEquals([(1, 'tree b2a2766a2879c209ab1176e7e778b81ae422eeaa\nauthor James Westby <jw+debian@jameswestby.net> 1174945067 +0100\ncommitter James Westby <jw+debian@jameswestby.net> 1174945067 +0100\n\nTest commit\n'), (2, '100644 a\x00og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8'), (3, 'test 1\n')], list(p.iterobjects()))
+    self.assertEquals([(12, 1, 'tree b2a2766a2879c209ab1176e7e778b81ae422eeaa\nauthor James Westby <jw+debian@jameswestby.net> 1174945067 +0100\ncommitter James Westby <jw+debian@jameswestby.net> 1174945067 +0100\n\nTest commit\n'), (138, 2, '100644 a\x00og\x0c\x0f\xb5?\x94cv\x0br\x95\xfb\xb8\x14\xe9e\xfb \xc8'), (178, 3, 'test 1\n')], list(p.iterobjects()))
+
+  def test_create_index_v1(self):
+    p = self.get_pack_data(pack1_sha)
+    p.create_index_v1("v1test.idx")
+    idx1 = PackIndex("v1test.idx")
+    idx2 = self.get_pack_index(pack1_sha)
+    self.assertEquals(idx1, idx2)
+
+  def test_create_index_v2(self):
+    p = self.get_pack_data(pack1_sha)
+    p.create_index_v2("v2test.idx")
+    idx1 = PackIndex("v2test.idx")
+    idx2 = self.get_pack_index(pack1_sha)
+    self.assertEquals(idx1, idx2)
+
 
 
 class TestPack(PackTests):