Przeglądaj źródła

diff_tree: Add rename_detector to tree_changes.

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
Dave Borowitz 13 lat temu
rodzic
commit
ef2583af24
3 zmienionych plików z 41 dodań i 1 usunięć
  1. 2 0
      NEWS
  2. 10 1
      dulwich/diff_tree.py
  3. 29 0
      dulwich/tests/test_diff_tree.py

+ 2 - 0
NEWS

@@ -80,6 +80,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.

+ 10 - 1
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:

+ 29 - 0
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]