74,19 → 74,10 |
if ($pack_files) foreach ($pack_files as $basename) { |
$basename = substr(basename($basename),0,strlen(basename($basename))-5); |
try { |
if (class_exists('ViaThinkSoft\Glip\Git')) { |
// https://github.com/danielmarschall/glip |
// composer require danielmarschall/glip |
$git = new Git($git_dir); |
$obj = $git->getObject(hex2bin($commit_object)); |
echo $obj->detail; |
} else { |
// Own implementation (cannot read delta objects yet) |
return git_read_object($commit_object, |
$objects_dir.'/pack/'.$basename.'.idx', |
$objects_dir.'/pack/'.$basename.'.pack' |
); |
} |
return git_read_object($commit_object, |
$objects_dir.'/pack/'.$basename.'.idx', |
$objects_dir.'/pack/'.$basename.'.pack' |
); |
} catch (Exception $e) { |
$last_exception = $e; |
} |
150,9 → 141,10 |
|
if ($version == 2) { |
// Get CRC32 |
// TODO: is this correct? or do we need to read 'H8' ? |
fseek($fp, $fanout_offset + 4*256 + 20*$num_objects + 4*$object_no); |
$crc32 = unpack('H8', fread($fp,4))[1]; |
if ($debug) echo "CRC32 = ".$crc32."\n"; |
$crc32 = unpack('N', fread($fp,4))[1]; |
if ($debug) echo "CRC32 = ".sprintf('0x%08x',$crc32)."\n"; |
|
// Get offset (32 bit) |
fseek($fp, $fanout_offset + 4*256 + 20*$num_objects + 4*$num_objects + 4*$object_no); |
180,11 → 172,10 |
$fp = fopen($pack_file, 'rb'); |
if (!$fp) throw new Exception("Cannot open pack file $pack_file"); |
|
// Read type and first part of the size |
// Find out type |
fseek($fp, $pack_offset); |
$size_info = unpack('C', fread($fp,1))[1]; |
|
// Detect type |
$type = ($size_info & 0x70) >> 4; /*0b01110000*/ |
switch ($type) { |
case 1: |
210,7 → 201,7 |
break; |
} |
|
// Find out the expected unpacked size |
// Find out size |
$size = $size_info & 0xF /*0x00001111*/; |
$shift_info = 4; |
while ($size_info >= 0x80) { |
218,10 → 209,11 |
$size = (($size_info & 0x7F) << $shift_info) + $size; |
$shift_info += 7; |
} |
if ($debug) echo "Expected unpacked size = $size\n"; |
|
// TODO: Is this the packed or unpacked size? http://driusan.github.io/git-pack.html |
if ($debug) echo "Packed size = ".sprintf('0x%x',$size)."\n"; |
|
// Read delta base type |
// Example implementation: https://github.com/AlexFBP/glip/blob/master/lib/git.class.php#L240 |
if ($type == 6/*OBJ_OFS_DELTA*/) { |
// "a negative relative offset from the delta object's position in the pack if this is an OBJ_OFS_DELTA object" |
|
244,19 → 236,11 |
throw new Exception("OBJ_REF_DELTA is currently not implemented"); // TODO! Implement OBJ_REF_DELTA! |
} |
|
// Read and uncompress the compressed data |
$compressed = ''; |
$uncompressed = false; |
for ($compressed_size=1; $compressed_size<=32768*$size; $compressed_size++) { |
// Since we don't know the compressed size, we need to do trial and error |
// TODO: this is a super stupid algorithm! Is there a better way??? |
$compressed .= fread($fp,1); |
$uncompressed = @gzuncompress($compressed); |
if (strlen($uncompressed) === $size) { |
if ($debug) echo "Detected compressed size = $compressed_size\n"; |
break; |
} |
} |
// Read compressed data |
$compressed = fread($fp,$size); |
|
// Uncompress |
$uncompressed = @gzuncompress($compressed); |
if ($uncompressed === false) throw new Exception("Decompression failed"); |
if ($debug) echo "$uncompressed\n"; |
|
263,12 → 247,9 |
// Close pack file |
fclose($fp); |
|
if ($version == 2) { |
// Check CRC32 |
// TODO: Hash does not match. What are we doing wrong?! |
// if ($debug) echo "CRC32 found = ".hash('crc32',$compressed)." vs $crc32\n"; |
// if ($debug) echo "CRC32 found = ".hash('crc32b',$compressed)." vs $crc32\n"; |
} |
// Check CRC32 |
// TODO: Does not fit; neither crc32, nor crc32b... |
// if ($debug) echo "CRC32 found = 0x".hash('crc32',$compressed)." vs $crc32\n"; |
|
return $uncompressed; |
} |