Przeglądaj źródła

Support for including trees in tree_changes reports.

Jelmer Vernooij 7 lat temu
rodzic
commit
884e68c09f
2 zmienionych plików z 15 dodań i 8 usunięć
  1. 9 5
      dulwich/diff_tree.py
  2. 6 3
      dulwich/object_store.py

+ 9 - 5
dulwich/diff_tree.py

@@ -153,14 +153,14 @@ def walk_trees(store, tree1_id, tree2_id, prune_identical=False):
         yield entry1, entry2
 
 
-def _skip_tree(entry):
-    if entry.mode is None or stat.S_ISDIR(entry.mode):
+def _skip_tree(entry, include_trees):
+    if entry.mode is None or (not include_trees and stat.S_ISDIR(entry.mode)):
         return _NULL_ENTRY
     return entry
 
 
 def tree_changes(store, tree1_id, tree2_id, want_unchanged=False,
-                 rename_detector=None):
+                 rename_detector=None, include_trees=False):
     """Find the differences between the contents of two trees.
 
     :param store: An ObjectStore for looking up objects.
@@ -168,10 +168,14 @@ 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 include_trees: Whether to include trees
     :param rename_detector: RenameDetector object for detecting renames.
     :return: Iterator over TreeChange instances for each change between the
         source and target tree.
     """
+    if include_trees and rename_detector is not None:
+        raise NotImplementedError(
+            'rename_detector and include_trees are mutually exclusive')
     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(
@@ -186,8 +190,8 @@ def tree_changes(store, tree1_id, tree2_id, want_unchanged=False,
             continue
 
         # Treat entries for trees as missing.
-        entry1 = _skip_tree(entry1)
-        entry2 = _skip_tree(entry2)
+        entry1 = _skip_tree(entry1, include_trees)
+        entry2 = _skip_tree(entry2, include_trees)
 
         if entry1 != _NULL_ENTRY and entry2 != _NULL_ENTRY:
             if stat.S_IFMT(entry1.mode) != stat.S_IFMT(entry2.mode):

+ 6 - 3
dulwich/object_store.py

@@ -154,17 +154,20 @@ class BaseObjectStore(object):
         else:
             return commit()
 
-    def tree_changes(self, source, target, want_unchanged=False):
+    def tree_changes(self, source, target, want_unchanged=False,
+                     include_trees=False):
         """Find the differences between the contents of two trees
 
         :param source: SHA1 of the source tree
         :param target: SHA1 of the target tree
         :param want_unchanged: Whether unchanged files should be reported
+        :param include_trees: Whether to include trees
         :return: Iterator over tuples with
             (oldpath, newpath), (oldmode, newmode), (oldsha, newsha)
         """
         for change in tree_changes(self, source, target,
-                                   want_unchanged=want_unchanged):
+                                   want_unchanged=want_unchanged,
+                                   include_trees=include_trees):
             yield ((change.old.path, change.new.path),
                    (change.old.mode, change.new.mode),
                    (change.old.sha, change.new.sha))
@@ -180,7 +183,7 @@ class BaseObjectStore(object):
             tree.
         """
         for entry, _ in walk_trees(self, tree_id, None):
-            if not stat.S_ISDIR(entry.mode) or include_trees:
+            if (entry.mode is not None and not stat.S_ISDIR(entry.mode)) or include_trees:
                 yield entry
 
     def find_missing_objects(self, haves, wants, progress=None,