← Back to team overview

dulwich-users team mailing list archive

[PATCH 14/28] diff: Add key for sorting TreeChanges.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I40d5453d8fd1f61984961cd3fc4436175ffaafa9
---
 dulwich/diff.py            |   11 +++++++++++
 dulwich/tests/test_diff.py |   24 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/dulwich/diff.py b/dulwich/diff.py
index 98668ce..a38e8ef 100644
--- a/dulwich/diff.py
+++ b/dulwich/diff.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)
diff --git a/dulwich/tests/test_diff.py b/dulwich/tests/test_diff.py
index 08bfaa6..8e77d9c 100644
--- a/dulwich/tests/test_diff.py
+++ b/dulwich/tests/test_diff.py
@@ -20,22 +20,29 @@
 
 from dulwich.diff 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,
@@ -302,3 +309,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))
-- 
1.7.3.2.168.gd6b63




References