Browse Source

index: Fix IndexEntry to not include name.

Jelmer Vernooij 11 năm trước cách đây
mục cha
commit
c34bd620e4
2 tập tin đã thay đổi với 42 bổ sung12 xóa
  1. 12 12
      dulwich/index.py
  2. 30 0
      dulwich/tests/test_index.py

+ 12 - 12
dulwich/index.py

@@ -41,8 +41,8 @@ from dulwich.pack import (
 
 IndexEntry = collections.namedtuple(
     'IndexEntry', [
-        'name', 'ctime', 'mtime', 'dev', 'ino',
-        'mode', 'uid', 'gid', 'size', 'sha', 'flags'])
+        'ctime', 'mtime', 'dev', 'ino', 'mode', 'uid', 'gid', 'size', 'sha',
+        'flags'])
 
 
 def pathsplit(path):
@@ -106,8 +106,8 @@ def read_cache_entry(f):
     # Padding:
     real_size = ((f.tell() - beginoffset + 8) & ~7)
     f.read((beginoffset + real_size) - f.tell())
-    return IndexEntry(name, ctime, mtime, dev, ino, mode, uid, gid, size,
-                      sha_to_hex(sha), flags & ~0x0fff)
+    return (name, ctime, mtime, dev, ino, mode, uid, gid, size,
+            sha_to_hex(sha), flags & ~0x0fff)
 
 
 def write_cache_entry(f, entry):
@@ -146,7 +146,7 @@ def read_index_dict(f):
     """
     ret = {}
     for x in read_index(f):
-        ret[x[0]] = tuple(x[1:])
+        ret[x[0]] = IndexEntry(*x[1:])
     return ret
 
 
@@ -222,7 +222,7 @@ class Index(object):
         try:
             f = SHA1Reader(f)
             for x in read_index(f):
-                self[x[0]] = tuple(x[1:])
+                self[x[0]] = IndexEntry(*x[1:])
             # FIXME: Additional data?
             f.read(os.path.getsize(self._filename)-f.tell()-20)
             f.check_sha()
@@ -246,17 +246,17 @@ class Index(object):
 
     def get_sha1(self, path):
         """Return the (git object) SHA1 for the object at a path."""
-        return self[path][-2]
+        return self[path].sha
 
     def get_mode(self, path):
         """Return the POSIX file mode for the object at a path."""
-        return self[path][-6]
+        return self[path].mode
 
     def iterblobs(self):
         """Iterate over path, sha, mode tuples for use with commit_tree."""
         for path in self:
             entry = self[path]
-            yield path, entry[-2], cleanup_mode(entry[-6])
+            yield path, entry.sha, cleanup_mode(entry.mode)
 
     def clear(self):
         """Remove all contents from this index."""
@@ -289,7 +289,7 @@ class Index(object):
         """
         def lookup_entry(path):
             entry = self[path]
-            return entry[-2], entry[-6]
+            return entry.sha, entry.mode
         for (name, mode, sha) in changes_from_tree(self._byname.keys(),
                 lookup_entry, object_store, tree,
                 want_unchanged=want_unchanged):
@@ -479,11 +479,11 @@ def get_unstaged_changes(index, path):
 
     :param index: index to check
     :param path: path in which to find files
-    :yields: paths with unstaged changes
+    :return: iterator over paths with unstaged changes
     """
     # For each entry in the index check the sha1 & ensure not staged
     for name, entry in index.iteritems():
         fp = os.path.join(path, name)
         blob = blob_from_path_and_stat(fp, os.lstat(fp))
-        if blob.id != entry[-2]:
+        if blob.id != entry.sha:
             yield name

+ 30 - 0
dulwich/tests/test_index.py

@@ -34,8 +34,10 @@ from dulwich.index import (
     get_unstaged_changes,
     index_entry_from_stat,
     read_index,
+    read_index_dict,
     write_cache_time,
     write_index,
+    write_index_dict,
     )
 from dulwich.object_store import (
     MemoryObjectStore,
@@ -83,6 +85,7 @@ class SimpleIndexTestCase(IndexTestCase):
         self.assertEqual('bla', newname)
         self.assertEqual('e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', newsha)
 
+
 class SimpleIndexWriterTestCase(IndexTestCase):
 
     def setUp(self):
@@ -110,6 +113,33 @@ class SimpleIndexWriterTestCase(IndexTestCase):
             x.close()
 
 
+class ReadIndexDictTests(IndexTestCase):
+
+    def setUp(self):
+        IndexTestCase.setUp(self)
+        self.tempdir = tempfile.mkdtemp()
+
+    def tearDown(self):
+        IndexTestCase.tearDown(self)
+        shutil.rmtree(self.tempdir)
+
+    def test_simple_write(self):
+        entries = {'barbla': ((1230680220, 0), (1230680220, 0), 2050, 3761020,
+                    33188, 1000, 1000, 0,
+                    'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)}
+        filename = os.path.join(self.tempdir, 'test-simple-write-index')
+        x = open(filename, 'w+')
+        try:
+            write_index_dict(x, entries)
+        finally:
+            x.close()
+        x = open(filename, 'r')
+        try:
+            self.assertEqual(entries, read_index_dict(x))
+        finally:
+            x.close()
+
+
 class CommitTreeTests(TestCase):
 
     def setUp(self):