← Back to team overview

dulwich-users team mailing list archive

[PATCH 15/34] diff_tree: Add rename_detector to tree_changes.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

This just delegates to rename_detector.changes_with_renames(), but is
provided so the signature of tree_changes will match that of
tree_changes_for_merge.

Change-Id: I1cd62ad75f8b57ae56b1ef1e5bc44dddb909bf3e
---
 NEWS                            |    2 ++
 dulwich/diff_tree.py            |   11 ++++++++++-
 dulwich/tests/test_diff_tree.py |   29 +++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletions(-)

diff --git a/NEWS b/NEWS
index 3354272..4ada9bf 100644
--- a/NEWS
+++ b/NEWS
@@ -78,6 +78,8 @@
 
   * Optionally include unchanged entries in RenameDetectors. (Dave Borowitz)
 
+  * Optionally pass a RenameDetector to tree_changes. (Dave Borowitz)
+
  TEST CHANGES
 
   * If setuptools is installed, "python setup.py test" will now run the testsuite.
diff --git a/dulwich/diff_tree.py b/dulwich/diff_tree.py
index 1ef4779..e90afa2 100644
--- a/dulwich/diff_tree.py
+++ b/dulwich/diff_tree.py
@@ -152,7 +152,8 @@ def _skip_tree(entry):
     return entry
 
 
-def tree_changes(store, tree1_id, tree2_id, want_unchanged=False):
+def tree_changes(store, tree1_id, tree2_id, want_unchanged=False,
+                 rename_detector=None):
     """Find the differences between the contents of two trees.
 
     :param store: An ObjectStore for looking up objects.
@@ -160,9 +161,17 @@ def tree_changes(store, tree1_id, tree2_id, want_unchanged=False):
     :param tree2_id: The SHA of the target tree.
     :param want_unchanged: If True, include TreeChanges for unmodified entries
         as well.
+    :param rename_detector: RenameDetector object for detecting renames.
     :return: Iterator over TreeChange instances for each change between the
         source and target tree.
     """
+    if (rename_detector is not None and tree1_id is not None and
+        tree2_id is not None):
+        for change in rename_detector.changes_with_renames(
+          tree1_id, tree2_id, want_unchanged=want_unchanged):
+            yield change
+        return
+
     entries = walk_trees(store, tree1_id, tree2_id,
                          prune_identical=(not want_unchanged))
     for entry1, entry2 in entries:
diff --git a/dulwich/tests/test_diff_tree.py b/dulwich/tests/test_diff_tree.py
index 5de7a55..9c3ac70 100644
--- a/dulwich/tests/test_diff_tree.py
+++ b/dulwich/tests/test_diff_tree.py
@@ -289,6 +289,35 @@ class TreeChangesTest(DiffTestCase):
                       ('a', F, blob_a2.id))],
           tree1, tree2)
 
+    def test_tree_changes_rename_detector(self):
+        blob_a1 = make_object(Blob, data='a\nb\nc\nd\n')
+        blob_a2 = make_object(Blob, data='a\nb\nc\ne\n')
+        blob_b = make_object(Blob, data='b')
+        tree1 = self.commit_tree([('a', blob_a1), ('b', blob_b)])
+        tree2 = self.commit_tree([('c', blob_a2), ('b', blob_b)])
+        detector = RenameDetector(self.store)
+
+        self.assertChangesEqual(
+          [TreeChange.delete(('a', F, blob_a1.id)),
+           TreeChange.add(('c', F, blob_a2.id))],
+          tree1, tree2)
+        self.assertChangesEqual(
+          [TreeChange.delete(('a', F, blob_a1.id)),
+           TreeChange(CHANGE_UNCHANGED, ('b', F, blob_b.id),
+                      ('b', F, blob_b.id)),
+           TreeChange.add(('c', F, blob_a2.id))],
+          tree1, tree2, want_unchanged=True)
+        self.assertChangesEqual(
+          [TreeChange(CHANGE_RENAME, ('a', F, blob_a1.id),
+                      ('c', F, blob_a2.id))],
+          tree1, tree2, rename_detector=detector)
+        self.assertChangesEqual(
+          [TreeChange(CHANGE_RENAME, ('a', F, blob_a1.id),
+                      ('c', F, blob_a2.id)),
+           TreeChange(CHANGE_UNCHANGED, ('b', F, blob_b.id),
+                      ('b', F, blob_b.id))],
+          tree1, tree2, rename_detector=detector, want_unchanged=True)
+
     def assertChangesForMergeEqual(self, expected, parent_trees, merge_tree,
                                    **kwargs):
         parent_tree_ids = [t.id for t in parent_trees]
-- 
1.7.3.1



References