57
57
58
58
if TYPE_CHECKING :
59
59
from .base import IndexFile
60
+ from git .objects .fun import EntryTup
60
61
61
62
# ------------------------------------------------------------------------------------
62
63
@@ -188,7 +189,7 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i
188
189
189
190
def is_entry_tuple (entry : Tuple ) -> TypeGuard [Tuple [PathLike , int ]]:
190
191
return isinstance (entry , tuple ) and len (entry ) == 2
191
-
192
+
192
193
if len (entry ) == 1 :
193
194
entry_first = entry [0 ]
194
195
assert isinstance (entry_first , BaseIndexEntry )
@@ -259,8 +260,8 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
259
260
:param sl: slice indicating the range we should process on the entries list
260
261
:return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of
261
262
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
+
264
265
ci = sl .start
265
266
end = sl .stop
266
267
while ci < end :
@@ -272,7 +273,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
272
273
rbound = entry .path .find ('/' , si )
273
274
if rbound == - 1 :
274
275
# 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 :]))
276
277
else :
277
278
# find common base range
278
279
base = entry .path [si :rbound ]
@@ -289,7 +290,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
289
290
# enter recursion
290
291
# ci - 1 as we want to count our current item as well
291
292
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 ))
293
294
294
295
# skip ahead
295
296
ci = xi
@@ -306,7 +307,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
306
307
return (istream .binsha , tree_items_stringified )
307
308
308
309
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 :
310
311
return BaseIndexEntry ((tree_entry [1 ], tree_entry [0 ], stage << CE_STAGESHIFT , tree_entry [2 ]))
311
312
312
313
@@ -319,23 +320,30 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
319
320
:param tree_shas: 1, 2 or 3 trees as identified by their binary 20 byte shas
320
321
If 1 or two, the entries will effectively correspond to the last given tree
321
322
If 3 are given, a 3 way merge is performed"""
322
- out = [] # type: List[BaseIndexEntry]
323
- out_append = out .append
323
+ out : List [BaseIndexEntry ] = []
324
324
325
325
# one and two way is the same for us, as we don't have to handle an existing
326
326
# index, instrea
327
327
if len (tree_shas ) in (1 , 2 ):
328
328
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 ))
330
330
# END for each entry
331
331
return out
332
332
# END handle single tree
333
333
334
334
if len (tree_shas ) > 3 :
335
335
raise ValueError ("Cannot handle %i trees at once" % len (tree_shas ))
336
336
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
+
337
342
# 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
339
347
if base is not None :
340
348
# base version exists
341
349
if ours is not None :
@@ -347,23 +355,23 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
347
355
if (base [0 ] != ours [0 ] and base [0 ] != theirs [0 ] and ours [0 ] != theirs [0 ]) or \
348
356
(base [1 ] != ours [1 ] and base [1 ] != theirs [1 ] and ours [1 ] != theirs [1 ]):
349
357
# 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 ))
353
361
elif base [0 ] != ours [0 ] or base [1 ] != ours [1 ]:
354
362
# only we changed it
355
- out_append (_tree_entry_to_baseindexentry (ours , 0 ))
363
+ out . append (_tree_entry_to_baseindexentry (ours , 0 ))
356
364
else :
357
365
# either nobody changed it, or they did. In either
358
366
# case, use theirs
359
- out_append (_tree_entry_to_baseindexentry (theirs , 0 ))
367
+ out . append (_tree_entry_to_baseindexentry (theirs , 0 ))
360
368
# END handle modification
361
369
else :
362
370
363
371
if ours [0 ] != base [0 ] or ours [1 ] != base [1 ]:
364
372
# 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 ))
367
375
# else:
368
376
# we didn't change it, ignore
369
377
# pass
@@ -376,8 +384,8 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
376
384
else :
377
385
if theirs [0 ] != base [0 ] or theirs [1 ] != base [1 ]:
378
386
# 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 ))
381
389
# END theirs changed
382
390
# else:
383
391
# theirs didn't change
@@ -386,20 +394,20 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
386
394
# END handle ours
387
395
else :
388
396
# all three can't be None
389
- if ours is None :
397
+ if ours is None and theirs is not None :
390
398
# 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 :
393
401
# 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 :
396
404
# both have it, except for the base, see whether it changed
397
405
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 ))
400
408
else :
401
409
# 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 ))
403
411
# END handle two items
404
412
# END handle heads
405
413
# END handle base exists
0 commit comments