dulwich-users team mailing list archive
-
dulwich-users team
-
Mailing list archive
-
Message #00335
[PATCH 10/24] diff_tree: Add key for sorting TreeChanges.
From: Dave Borowitz <dborowitz@xxxxxxxxxx>
Change-Id: I40d5453d8fd1f61984961cd3fc4436175ffaafa9
---
dulwich/diff_tree.py | 11 +++++++++++
dulwich/tests/test_diff_tree.py | 24 ++++++++++++++++++++++++
2 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/dulwich/diff_tree.py b/dulwich/diff_tree.py
index 1ce7d0c..88dad28 100644
--- a/dulwich/diff_tree.py
+++ b/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)
diff --git a/dulwich/tests/test_diff_tree.py b/dulwich/tests/test_diff_tree.py
index 9181f8c..089bbff 100644
--- a/dulwich/tests/test_diff_tree.py
+++ b/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,
@@ -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