1515 LazyMixin ,
1616 unpack_from ,
1717 bin_to_hex ,
18+ byte_ord ,
1819)
1920
2021from gitdb .fun import (
@@ -421,7 +422,7 @@ def sha_to_index(self, sha):
421422 :return: index usable with the ``offset`` or ``entry`` method, or None
422423 if the sha was not found in this pack index
423424 :param sha: 20 byte sha to lookup"""
424- first_byte = ord (sha [0 ])
425+ first_byte = byte_ord (sha [0 ])
425426 get_sha = self .sha
426427 lo = 0 # lower index, the left bound of the bisection
427428 if first_byte != 0 :
@@ -430,11 +431,11 @@ def sha_to_index(self, sha):
430431
431432 # bisect until we have the sha
432433 while lo < hi :
433- mid = (lo + hi ) / 2
434- c = cmp ( sha , get_sha (mid ) )
435- if c < 0 :
434+ mid = (lo + hi ) // 2
435+ mid_sha = get_sha (mid )
436+ if sha < mid_sha :
436437 hi = mid
437- elif not c :
438+ elif sha == mid_sha :
438439 return mid
439440 else :
440441 lo = mid + 1
@@ -453,23 +454,24 @@ def partial_sha_to_index(self, partial_bin_sha, canonical_length):
453454 if len (partial_bin_sha ) < 2 :
454455 raise ValueError ("Require at least 2 bytes of partial sha" )
455456
456- first_byte = ord (partial_bin_sha [0 ])
457+ first_byte = byte_ord (partial_bin_sha [0 ])
458+
457459 get_sha = self .sha
458460 lo = 0 # lower index, the left bound of the bisection
459461 if first_byte != 0 :
460462 lo = self ._fanout_table [first_byte - 1 ]
461463 hi = self ._fanout_table [first_byte ] # the upper, right bound of the bisection
462464
463465 # fill the partial to full 20 bytes
464- filled_sha = partial_bin_sha + '\0 ' * (20 - len (partial_bin_sha ))
466+ filled_sha = partial_bin_sha + '\0 ' . encode ( "ascii" ) * (20 - len (partial_bin_sha ))
465467
466468 # find lowest
467469 while lo < hi :
468- mid = (lo + hi ) / 2
469- c = cmp ( filled_sha , get_sha (mid ) )
470- if c < 0 :
470+ mid = (lo + hi ) // 2
471+ mid_sha = get_sha (mid )
472+ if filled_sha < mid_sha :
471473 hi = mid
472- elif not c :
474+ elif filled_sha == mid_sha :
473475 # perfect match
474476 lo = mid
475477 break
@@ -482,7 +484,7 @@ def partial_sha_to_index(self, partial_bin_sha, canonical_length):
482484 cur_sha = get_sha (lo )
483485 if is_equal_canonical_sha (canonical_length , partial_bin_sha , cur_sha ):
484486 next_sha = None
485- if lo + 1 < self .size ():
487+ if lo + 1 < self .size ():
486488 next_sha = get_sha (lo + 1 )
487489 if next_sha and next_sha == cur_sha :
488490 raise AmbiguousObjectName (partial_bin_sha )
0 commit comments