Forráskód Böngészése

diff_tree: Add key for sorting TreeChanges.

Change-Id: I40d5453d8fd1f61984961cd3fc4436175ffaafa9
Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
Dave Borowitz 14 éve
szülő
commit
f208c6932f
2 módosított fájl, 35 hozzáadás és 0 törlés
  1. 11 0
      dulwich/diff_tree.py
  2. 24 0
      dulwich/tests/test_diff_tree.py

+ 11 - 0
dulwich/diff_tree.py

@@ -241,3 +241,14 @@ def _similarity_score(obj1, obj2, block_cache=None):
     if not max_size:
         return _MAX_SCORE
     return int(float(common_bytes) * _MAX_SCORE / max_size)
+
+
+def _tree_change_key(entry):
+    # Sort by old path then new path. If only one exists, use it for both keys.
+    path1 = entry.old.path
+    path2 = entry.new.path
+    if path1 is None:
+        path1 = path2
+    if path2 is None:
+        path2 = path1
+    return (path1, path2)

+ 24 - 0
dulwich/tests/test_diff_tree.py

@@ -20,22 +20,29 @@
 
 from dulwich.diff_tree import (
     CHANGE_MODIFY,
+    CHANGE_RENAME,
+    CHANGE_COPY,
     CHANGE_UNCHANGED,
     TreeChange,
     _merge_entries,
     tree_changes,
     _count_blocks,
     _similarity_score,
+    _tree_change_key,
     )
 from dulwich.index import (
     commit_tree,
     )
+from dulwich.misc import (
+    permutations,
+    )
 from dulwich.object_store import (
     MemoryObjectStore,
     )
 from dulwich.objects import (
     ShaFile,
     Blob,
+    TreeEntry,
     )
 from dulwich.tests import (
     TestCase,
@@ -303,3 +310,20 @@ class RenameDetectionTest(TestCase):
         blob2.raw_length = lambda: 3
         self.assertEqual(
           50, _similarity_score(blob1, blob2, block_cache=block_cache))
+
+    def test_tree_entry_sort(self):
+        sha = 'abcd' * 10
+        expected_entries = [
+          TreeChange.add(TreeEntry('aaa', F, sha)),
+          TreeChange(CHANGE_COPY, TreeEntry('bbb', F, sha),
+                     TreeEntry('aab', F, sha)),
+          TreeChange(CHANGE_MODIFY, TreeEntry('bbb', F, sha),
+                     TreeEntry('bbb', F, 'dabc' * 10)),
+          TreeChange(CHANGE_RENAME, TreeEntry('bbc', F, sha),
+                     TreeEntry('ddd', F, sha)),
+          TreeChange.delete(TreeEntry('ccc', F, sha)),
+          ]
+
+        for perm in permutations(expected_entries):
+            self.assertEqual(expected_entries,
+                             sorted(perm, key=_tree_change_key))