5757
5858if TYPE_CHECKING :
5959 from .base import IndexFile
60+ from git .objects .fun import EntryTup
6061
6162# ------------------------------------------------------------------------------------
6263
@@ -188,7 +189,7 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i
188189
189190 def is_entry_tuple (entry : Tuple ) -> TypeGuard [Tuple [PathLike , int ]]:
190191 return isinstance (entry , tuple ) and len (entry ) == 2
191-
192+
192193 if len (entry ) == 1 :
193194 entry_first = entry [0 ]
194195 assert isinstance (entry_first , BaseIndexEntry )
@@ -259,8 +260,8 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
259260 :param sl: slice indicating the range we should process on the entries list
260261 :return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of
261262 tree entries being a tuple of hexsha, mode, name"""
262- tree_items = [] # type : List[Tuple[Union[ bytes, str], int, str]]
263- tree_items_append = tree_items . append
263+ tree_items : List [Tuple [bytes , int , str ]] = [ ]
264+
264265 ci = sl .start
265266 end = sl .stop
266267 while ci < end :
@@ -272,7 +273,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
272273 rbound = entry .path .find ('/' , si )
273274 if rbound == - 1 :
274275 # its not a tree
275- tree_items_append ((entry .binsha , entry .mode , entry .path [si :]))
276+ tree_items . append ((entry .binsha , entry .mode , entry .path [si :]))
276277 else :
277278 # find common base range
278279 base = entry .path [si :rbound ]
@@ -289,7 +290,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
289290 # enter recursion
290291 # ci - 1 as we want to count our current item as well
291292 sha , _tree_entry_list = write_tree_from_cache (entries , odb , slice (ci - 1 , xi ), rbound + 1 )
292- tree_items_append ((sha , S_IFDIR , base ))
293+ tree_items . append ((sha , S_IFDIR , base ))
293294
294295 # skip ahead
295296 ci = xi
@@ -306,7 +307,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
306307 return (istream .binsha , tree_items_stringified )
307308
308309
309- def _tree_entry_to_baseindexentry (tree_entry : Tuple [str , int , str ], stage : int ) -> BaseIndexEntry :
310+ def _tree_entry_to_baseindexentry (tree_entry : Tuple [bytes , int , str ], stage : int ) -> BaseIndexEntry :
310311 return BaseIndexEntry ((tree_entry [1 ], tree_entry [0 ], stage << CE_STAGESHIFT , tree_entry [2 ]))
311312
312313
@@ -319,23 +320,30 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
319320 :param tree_shas: 1, 2 or 3 trees as identified by their binary 20 byte shas
320321 If 1 or two, the entries will effectively correspond to the last given tree
321322 If 3 are given, a 3 way merge is performed"""
322- out = [] # type: List[BaseIndexEntry]
323- out_append = out .append
323+ out : List [BaseIndexEntry ] = []
324324
325325 # one and two way is the same for us, as we don't have to handle an existing
326326 # index, instrea
327327 if len (tree_shas ) in (1 , 2 ):
328328 for entry in traverse_tree_recursive (odb , tree_shas [- 1 ], '' ):
329- out_append (_tree_entry_to_baseindexentry (entry , 0 ))
329+ out . append (_tree_entry_to_baseindexentry (entry , 0 ))
330330 # END for each entry
331331 return out
332332 # END handle single tree
333333
334334 if len (tree_shas ) > 3 :
335335 raise ValueError ("Cannot handle %i trees at once" % len (tree_shas ))
336336
337+ EntryTupOrNone = Union [EntryTup , None ]
338+
339+ def is_three_entry_list (inp ) -> TypeGuard [List [EntryTupOrNone ]]:
340+ return isinstance (inp , list ) and len (inp ) == 3
341+
337342 # three trees
338- for base , ours , theirs in traverse_trees_recursive (odb , tree_shas , '' ):
343+ for three_entries in traverse_trees_recursive (odb , tree_shas , '' ):
344+
345+ assert is_three_entry_list (three_entries )
346+ base , ours , theirs = three_entries
339347 if base is not None :
340348 # base version exists
341349 if ours is not None :
@@ -347,23 +355,23 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
347355 if (base [0 ] != ours [0 ] and base [0 ] != theirs [0 ] and ours [0 ] != theirs [0 ]) or \
348356 (base [1 ] != ours [1 ] and base [1 ] != theirs [1 ] and ours [1 ] != theirs [1 ]):
349357 # changed by both
350- out_append (_tree_entry_to_baseindexentry (base , 1 ))
351- out_append (_tree_entry_to_baseindexentry (ours , 2 ))
352- out_append (_tree_entry_to_baseindexentry (theirs , 3 ))
358+ out . append (_tree_entry_to_baseindexentry (base , 1 ))
359+ out . append (_tree_entry_to_baseindexentry (ours , 2 ))
360+ out . append (_tree_entry_to_baseindexentry (theirs , 3 ))
353361 elif base [0 ] != ours [0 ] or base [1 ] != ours [1 ]:
354362 # only we changed it
355- out_append (_tree_entry_to_baseindexentry (ours , 0 ))
363+ out . append (_tree_entry_to_baseindexentry (ours , 0 ))
356364 else :
357365 # either nobody changed it, or they did. In either
358366 # case, use theirs
359- out_append (_tree_entry_to_baseindexentry (theirs , 0 ))
367+ out . append (_tree_entry_to_baseindexentry (theirs , 0 ))
360368 # END handle modification
361369 else :
362370
363371 if ours [0 ] != base [0 ] or ours [1 ] != base [1 ]:
364372 # they deleted it, we changed it, conflict
365- out_append (_tree_entry_to_baseindexentry (base , 1 ))
366- out_append (_tree_entry_to_baseindexentry (ours , 2 ))
373+ out . append (_tree_entry_to_baseindexentry (base , 1 ))
374+ out . append (_tree_entry_to_baseindexentry (ours , 2 ))
367375 # else:
368376 # we didn't change it, ignore
369377 # pass
@@ -376,8 +384,8 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
376384 else :
377385 if theirs [0 ] != base [0 ] or theirs [1 ] != base [1 ]:
378386 # deleted in ours, changed theirs, conflict
379- out_append (_tree_entry_to_baseindexentry (base , 1 ))
380- out_append (_tree_entry_to_baseindexentry (theirs , 3 ))
387+ out . append (_tree_entry_to_baseindexentry (base , 1 ))
388+ out . append (_tree_entry_to_baseindexentry (theirs , 3 ))
381389 # END theirs changed
382390 # else:
383391 # theirs didn't change
@@ -386,20 +394,20 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
386394 # END handle ours
387395 else :
388396 # all three can't be None
389- if ours is None :
397+ if ours is None and theirs is not None :
390398 # added in their branch
391- out_append (_tree_entry_to_baseindexentry (theirs , 0 ))
392- elif theirs is None :
399+ out . append (_tree_entry_to_baseindexentry (theirs , 0 ))
400+ elif theirs is None and ours is not None :
393401 # added in our branch
394- out_append (_tree_entry_to_baseindexentry (ours , 0 ))
395- else :
402+ out . append (_tree_entry_to_baseindexentry (ours , 0 ))
403+ elif ours is not None and theirs is not None :
396404 # both have it, except for the base, see whether it changed
397405 if ours [0 ] != theirs [0 ] or ours [1 ] != theirs [1 ]:
398- out_append (_tree_entry_to_baseindexentry (ours , 2 ))
399- out_append (_tree_entry_to_baseindexentry (theirs , 3 ))
406+ out . append (_tree_entry_to_baseindexentry (ours , 2 ))
407+ out . append (_tree_entry_to_baseindexentry (theirs , 3 ))
400408 else :
401409 # it was added the same in both
402- out_append (_tree_entry_to_baseindexentry (ours , 0 ))
410+ out . append (_tree_entry_to_baseindexentry (ours , 0 ))
403411 # END handle two items
404412 # END handle heads
405413 # END handle base exists
0 commit comments