@@ -127,15 +127,20 @@ def pack_object_at(data, offset, as_stream):
127
127
# END handle info
128
128
# END handle stream
129
129
130
- def write_stream_to_pack (read , write , zstream , want_crc = False ):
130
+ def write_stream_to_pack (read , write , zstream , base_crc = None ):
131
131
"""Copy a stream as read from read function, zip it, and write the result.
132
132
Count the number of written bytes and return it
133
- :param want_crc: if True, the crc will be generated over the compressed data.
134
- :return: tuple(no bytes read, no bytes written, crc32) crc might be 0 if want_crc
133
+ :param base_crc: if not None, the crc will be the base for all compressed data
134
+ we consecutively write and generate a crc32 from. If None, no crc will be generated
135
+ :return: tuple(no bytes read, no bytes written, crc32) crc might be 0 if base_crc
135
136
was false"""
136
137
br = 0 # bytes read
137
138
bw = 0 # bytes written
139
+ want_crc = base_crc is not None
138
140
crc = 0
141
+ if want_crc :
142
+ crc = base_crc
143
+ #END initialize crc
139
144
140
145
while True :
141
146
chunk = read (chunk_size )
@@ -651,6 +656,9 @@ def __init__(self, pack_or_index_path):
651
656
652
657
def _set_cache_ (self , attr ):
653
658
# currently this can only be _offset_map
659
+ # TODO: make this a simple sorted offset array which can be bisected
660
+ # to find the respective entry, from which we can take a +1 easily
661
+ # This might be slower, but should also be much lighter in memory !
654
662
offsets_sorted = sorted (self ._index .offsets ())
655
663
last_offset = len (self ._pack .data ()) - self ._pack .footer_size
656
664
assert offsets_sorted , "Cannot handle empty indices"
@@ -926,15 +934,21 @@ def write_pack(cls, object_iter, pack_write, index_write=None,
926
934
actual_count = 0
927
935
for obj in objs :
928
936
actual_count += 1
937
+ crc = 0
929
938
930
939
# object header
931
940
hdr = create_pack_object_header (obj .type_id , obj .size )
941
+ if index_write :
942
+ crc = crc32 (hdr )
943
+ else :
944
+ crc = None
945
+ #END handle crc
932
946
pwrite (hdr )
933
947
934
948
# data stream
935
949
zstream = zlib .compressobj (zlib_compression )
936
950
ostream = obj .stream
937
- br , bw , crc = write_stream_to_pack (ostream .read , pwrite , zstream , want_crc = index_write )
951
+ br , bw , crc = write_stream_to_pack (ostream .read , pwrite , zstream , base_crc = crc )
938
952
assert (br == obj .size )
939
953
if wants_index :
940
954
index .append (obj .binsha , crc , ofs )
0 commit comments