67 | 67 |
|
68 | 68 |
|
69 | 69 |
def take_msb_bytes(map, offset):
|
|
70 |
"""Read bytes marked with most significant bit.
|
|
71 |
|
|
72 |
:param map: The buffer.
|
|
73 |
:param offset: Offset in the buffer at which to start reading.
|
|
74 |
"""
|
70 | 75 |
ret = []
|
71 | 76 |
while len(ret) == 0 or ret[-1] & 0x80:
|
72 | 77 |
ret.append(ord(map[offset]))
|
|
74 | 79 |
return ret
|
75 | 80 |
|
76 | 81 |
|
77 | |
def read_zlib_chunks(data, offset, dec_size):
|
|
82 |
def read_zlib_chunks(data, offset):
|
|
83 |
"""Read chunks of zlib data from a buffer.
|
|
84 |
|
|
85 |
:param data: Buffer to read from
|
|
86 |
:param offset: Offset at which to start reading
|
|
87 |
:return: Tuple with list of chunks and length of
|
|
88 |
compressed data length
|
|
89 |
"""
|
78 | 90 |
obj = zlib.decompressobj()
|
79 | 91 |
ret = []
|
80 | 92 |
fed = 0
|
|
90 | 102 |
|
91 | 103 |
|
92 | 104 |
def read_zlib(data, offset, dec_size):
|
93 | |
ret, comp_len = read_zlib_chunks(data, offset, dec_size)
|
|
105 |
"""Read zlib-compressed data from a buffer.
|
|
106 |
|
|
107 |
:param data: Buffer
|
|
108 |
:param offset: Offset in the buffer at which to read
|
|
109 |
:param dec_size: Size of the decompressed buffer
|
|
110 |
:return: Uncompressed buffer and compressed buffer length.
|
|
111 |
"""
|
|
112 |
ret, comp_len = read_zlib_chunks(data, offset)
|
94 | 113 |
x = "".join(ret)
|
95 | 114 |
assert len(x) == dec_size
|
96 | 115 |
return x, comp_len
|
|
122 | 141 |
|
123 | 142 |
|
124 | 143 |
def load_pack_index(filename):
|
|
144 |
"""Load an index file by path.
|
|
145 |
|
|
146 |
:param filename: Path to the index file
|
|
147 |
"""
|
125 | 148 |
f = open(filename, 'rb')
|
126 | 149 |
if f.read(4) == '\377tOc':
|
127 | 150 |
version = struct.unpack(">L", f.read(4))[0]
|
|
370 | 393 |
assert version in (2, 3), "Version was %d" % version
|
371 | 394 |
(num_objects,) = unpack_from(">L", header, 8)
|
372 | 395 |
return (version, num_objects)
|
373 | |
|
374 | |
|
375 | |
def read_pack_tail(f):
|
376 | |
return (f.read(20),)
|
377 | 396 |
|
378 | 397 |
|
379 | 398 |
def unpack_object(map, offset=0):
|
|
471 | 490 |
def _read_header(self):
|
472 | 491 |
(version, self._num_objects) = read_pack_header(self._file)
|
473 | 492 |
self._file.seek(self._size-20)
|
474 | |
(self._stored_checksum,) = read_pack_tail(self._file)
|
|
493 |
self._stored_checksum = self._file.read(20)
|
475 | 494 |
|
476 | 495 |
def __len__(self):
|
477 | 496 |
"""Returns the number of objects in this pack."""
|
|
635 | 654 |
return self._stored_checksum
|
636 | 655 |
|
637 | 656 |
def check(self):
|
|
657 |
"""Check the consistency of this pack."""
|
638 | 658 |
return (self.calculate_checksum() == self.get_stored_checksum())
|
639 | 659 |
|
640 | 660 |
def get_object_at(self, offset):
|
|
1096 | 1116 |
*self.data.resolve_object(offset, type, obj, get_raw))
|
1097 | 1117 |
|
1098 | 1118 |
|
1099 | |
def load_packs(path):
|
1100 | |
if not os.path.exists(path):
|
1101 | |
return
|
1102 | |
for name in os.listdir(path):
|
1103 | |
if name.startswith("pack-") and name.endswith(".pack"):
|
1104 | |
yield Pack(os.path.join(path, name[:-len(".pack")]))
|
1105 | |
|
1106 | |
|
1107 | 1119 |
try:
|
1108 | 1120 |
from dulwich._pack import apply_delta, bisect_find_sha
|
1109 | 1121 |
except ImportError:
|