1212
1313from fun import (
1414 pack_object_header_info ,
15+ is_equal_canonical_sha ,
1516 type_id_to_type_map ,
1617 write_object ,
1718 stream_copy ,
@@ -303,24 +304,26 @@ def sha_to_index(self, sha):
303304 # END bisect
304305 return None
305306
306- def partial_sha_to_index (self , partial_sha ):
307- """:return: index as in `sha_to_index` or None if the sha was not found in this
308- index file
309- :param partial_sha: an at least two bytes of a partial sha
307+ def partial_sha_to_index (self , partial_bin_sha , canonical_length ):
308+ """
309+ :return: index as in `sha_to_index` or None if the sha was not found in this
310+ index file
311+ :param partial_bin_sha: an at least two bytes of a partial binary sha
312+ :param canonical_length: lenght of the original hexadecimal representation of the
313+ given partial binary sha
310314 :raise AmbiguousObjectName:"""
311- if len (partial_sha ) < 2 :
315+ if len (partial_bin_sha ) < 2 :
312316 raise ValueError ("Require at least 2 bytes of partial sha" )
313317
314- first_byte = ord (partial_sha [0 ])
318+ first_byte = ord (partial_bin_sha [0 ])
315319 get_sha = self .sha
316320 lo = 0 # lower index, the left bound of the bisection
317321 if first_byte != 0 :
318322 lo = self ._fanout_table [first_byte - 1 ]
319323 hi = self ._fanout_table [first_byte ] # the upper, right bound of the bisection
320324
321- len_partial = len (partial_sha )
322325 # fill the partial to full 20 bytes
323- filled_sha = partial_sha + '\0 ' * (20 - len_partial )
326+ filled_sha = partial_bin_sha + '\0 ' * (20 - len ( partial_bin_sha ) )
324327
325328 # find lowest
326329 while lo < hi :
@@ -336,14 +339,15 @@ def partial_sha_to_index(self, partial_sha):
336339 lo = mid + 1
337340 # END handle midpoint
338341 # END bisect
339- if lo < self .size :
342+
343+ if lo < self .size ():
340344 cur_sha = get_sha (lo )
341- if cur_sha [: len_partial ] == partial_sha :
345+ if is_equal_canonical_sha ( canonical_length , partial_bin_sha , cur_sha ) :
342346 next_sha = None
343- if lo + 1 < self .size :
347+ if lo + 1 < self .size () :
344348 next_sha = get_sha (lo + 1 )
345349 if next_sha and next_sha == cur_sha :
346- raise AmbiguousObjectName (partial_sha )
350+ raise AmbiguousObjectName (partial_bin_sha )
347351 return lo
348352 # END if we have a match
349353 # END if we found something
0 commit comments