Jelmer Vernooij 3 anni fa
parent
commit
176e2bc3fb
2 ha cambiato i file con 42 aggiunte e 27 eliminazioni
  1. 22 16
      dulwich/diff_tree.py
  2. 20 11
      dulwich/merge.py

+ 22 - 16
dulwich/diff_tree.py

@@ -28,6 +28,7 @@ from collections import (
 from io import BytesIO
 from itertools import chain
 import stat
+from typing import Iterable, Tuple, List
 
 from dulwich.objects import (
     S_ISGITLINK,
@@ -114,14 +115,17 @@ def _merge_entries(path, tree1, tree2):
     return result
 
 
-def _is_tree(entry):
+def _is_tree(entry: TreeEntry) -> bool:
     mode = entry.mode
     if mode is None:
         return False
     return stat.S_ISDIR(mode)
 
 
-def walk_trees(store, tree1_id, tree2_id, prune_identical=False):
+def walk_trees(
+        store, tree1_id: bytes, tree2_id: bytes,
+        prune_identical: bool = False) -> Iterable[
+            Tuple[TreeEntry, TreeEntry]]:
     """Recursively walk all the entries of two trees.
 
     Iteration is depth-first pre-order, as in e.g. os.walk.
@@ -157,7 +161,7 @@ def walk_trees(store, tree1_id, tree2_id, prune_identical=False):
         yield entry1, entry2
 
 
-def _skip_tree(entry, include_trees):
+def _skip_tree(entry: TreeEntry, include_trees: bool):
     if entry.mode is None or (not include_trees and stat.S_ISDIR(entry.mode)):
         return _NULL_ENTRY
     return entry
@@ -165,12 +169,12 @@ def _skip_tree(entry, include_trees):
 
 def tree_changes(
     store,
-    tree1_id,
-    tree2_id,
-    want_unchanged=False,
+    tree1_id: bytes,
+    tree2_id: bytes,
+    want_unchanged: bool = False,
     rename_detector=None,
-    include_trees=False,
-    change_type_same=False,
+    include_trees: bool = False,
+    change_type_same: bool = False,
 ):
     """Find the differences between the contents of two trees.
 
@@ -243,7 +247,8 @@ def _all_same(seq, key):
     return _all_eq(seq[1:], key, key(seq[0]))
 
 
-def tree_changes_for_merge(store, parent_tree_ids, tree_id, rename_detector=None):
+def tree_changes_for_merge(
+        store, parent_tree_ids, tree_id: bytes, rename_detector=None):
     """Get the tree changes for a merge tree relative to all its parents.
 
     Args:
@@ -389,7 +394,7 @@ def _similarity_score(obj1, obj2, block_cache=None):
     return int(float(common_bytes) * _MAX_SCORE / max_size)
 
 
-def _tree_change_key(entry):
+def _tree_change_key(entry: TreeChange) -> Tuple[str, str]:
     # Sort by old path then new path. If only one exists, use it for both keys.
     path1 = entry.old.path
     path2 = entry.new.path
@@ -406,10 +411,10 @@ class RenameDetector(object):
     def __init__(
         self,
         store,
-        rename_threshold=RENAME_THRESHOLD,
-        max_files=MAX_FILES,
-        rewrite_threshold=REWRITE_THRESHOLD,
-        find_copies_harder=False,
+        rename_threshold: int = RENAME_THRESHOLD,
+        max_files: int = MAX_FILES,
+        rewrite_threshold: int = REWRITE_THRESHOLD,
+        find_copies_harder: bool = False,
     ):
         """Initialize the rename detector.
 
@@ -618,8 +623,9 @@ class RenameDetector(object):
         self._deletes = [d for d in self._deletes if d.type != CHANGE_UNCHANGED]
 
     def changes_with_renames(
-        self, tree1_id, tree2_id, want_unchanged=False, include_trees=False
-    ):
+        self, tree1_id: bytes, tree2_id: bytes, want_unchanged: bool = False,
+        include_trees: bool = False
+    ) -> List[TreeChange]:
         """Iterate TreeChanges between two tree SHAs, with rename detection."""
         self._reset()
         self._want_unchanged = want_unchanged

+ 20 - 11
dulwich/merge.py

@@ -21,6 +21,7 @@
 """Merge support."""
 
 from collections import namedtuple
+from typing import Union, Iterable, List
 
 
 from .diff_tree import (
@@ -34,7 +35,7 @@ from .diff_tree import (
     CHANGE_UNCHANGED,
     )
 from .merge_base import find_merge_base
-from .objects import Blob
+from .objects import Blob, Tree
 
 
 class MergeConflict(namedtuple(
@@ -43,13 +44,14 @@ class MergeConflict(namedtuple(
     """A merge conflict."""
 
 
-def _merge_entry(new_path, object_store, this_entry, other_entry, base_entry,
-                 file_merger):
+def _merge_entry(
+        new_path: str, object_store, this_entry: TreeEntry,
+        other_entry: TreeEntry, base_entry: TreeEntry,
+        file_merger):
     """Run a per entry-merge."""
     if file_merger is None:
         return MergeConflict(
-            this_entry, other_entry,
-            other_entry.old,
+            this_entry, other_entry, base_entry,
             'Conflict in %s but no file merger provided'
             % new_path)
     merged_text = file_merger(
@@ -69,8 +71,10 @@ def _merge_entry(new_path, object_store, this_entry, other_entry, base_entry,
     return TreeEntry(new_path, mode, merged_text_blob.id)
 
 
-def merge_tree(object_store, this_tree, other_tree, common_tree,
-               rename_detector=None, file_merger=None):
+def merge_tree(
+        object_store, this_tree: bytes, other_tree: bytes, common_tree: bytes,
+        rename_detector=None, file_merger=None) -> Iterable[
+            Union[TreeEntry, MergeConflict]]:
     """Merge two trees.
 
     Args:
@@ -84,12 +88,15 @@ def merge_tree(object_store, this_tree, other_tree, common_tree,
       iterator over objects, either TreeEntry (updating an entry)
         or MergeConflict (indicating a conflict)
     """
-    changes_this = tree_changes(object_store, common_tree, this_tree)
+    changes_this = tree_changes(
+        object_store, common_tree, this_tree, rename_detector=rename_detector)
     changes_this_by_common_path = {
         change.old.path: change for change in changes_this if change.old}
     changes_this_by_this_path = {
         change.new.path: change for change in changes_this if change.new}
-    for other_change in tree_changes(object_store, common_tree, other_tree):
+    for other_change in tree_changes(
+            object_store, common_tree, other_tree,
+            rename_detector=rename_detector):
         this_change = changes_this_by_common_path.get(other_change.old.path)
         if this_change == other_change:
             continue
@@ -172,7 +179,9 @@ class MergeResults(object):
         self.conflicts = conflicts
 
 
-def merge(repo, commit_ids, rename_detector=None, file_merger=None):
+def merge(
+        repo, commit_ids: List[bytes], rename_detector=None,
+        file_merger=None) -> MergeResults:
     """Perform a merge.
     """
     conflicts = []
@@ -193,4 +202,4 @@ def merge(repo, commit_ids, rename_detector=None, file_merger=None):
 
         # TODO(jelmer): apply the change to the tree
 
-    return conflicts
+    return MergeResults(conflicts=conflicts)