18,84 → 18,111 |
* limitations under the License. |
*/ |
|
function git_get_latest_commit_message($git_dir) { |
// First try an official git client |
$cmd = "git --git-dir=".escapeshellarg("$git_dir")." log -1 2>&1"; |
$ec = -1; |
$out = array(); |
@exec($cmd, $out, $ec); |
$out = implode("\n",$out); |
if (($ec == 0) && ($out != '')) return $out; |
function git_get_latest_commit_id(string $git_dir): string { |
// Note: The method "getTip()" of GLIP only implements "refs/heads/master" and "packed-refs" (but for packed-refs without "refs/remotes/origin/...") |
|
// If that failed, try to decode the binary files outselves |
$cont = @file_get_contents($git_dir.'/HEAD'); |
if (preg_match('@ref: (.+)[\r\n]@', "$cont\n", $m) && file_exists($git_dir.'/'.$m[1])) { |
$cont = @file_get_contents($git_dir . '/HEAD'); |
if (preg_match('@ref: (.+)[\r\n]@', "$cont\n", $m) && file_exists($git_dir . '/' . $m[1])) { |
// Example content of a .git folder file: |
// 091a5fa6b157be035e88f5d24aa329ba44d20d63 |
// Not available |
$commit_object = trim(file_get_contents($git_dir.'/'.$m[1])); |
} else if (file_exists($git_dir.'/refs/heads/master')) { |
return trim(file_get_contents($git_dir . '/' . $m[1])); |
} |
|
if (file_exists($git_dir . '/refs/heads/master')) { |
// Missing at Plesk Git initial checkout, but available on update. |
$commit_object = trim(file_get_contents($git_dir.'/refs/heads/master')); |
} else if (file_exists($git_dir.'/FETCH_HEAD')) { |
return trim(file_get_contents($git_dir . '/refs/heads/master')); |
} |
|
if (file_exists($git_dir . '/packed-refs')) { |
// Example contents of the file: |
// # pack-refs with: peeled fully-peeled sorted |
// 5605bd539677494558470234266cb5885343e72b refs/remotes/origin/master |
// a3d910dd0cdca30827ae25b0f89045d8403b8843 refs/remotes/origin/patch-1 |
$subpaths = ['refs/heads/master', 'refs/remotes/origin/master']; |
foreach ($subpaths as $subpath) { |
$head = null; |
$f = fopen($git_dir . '/packed-refs', 'rb'); |
flock($f, LOCK_SH); |
while ($head === null && ($line = fgets($f)) !== false) { |
if ($line[0] == '#') |
continue; |
$parts = explode(' ', trim($line)); |
if (count($parts) == 2 && $parts[1] == $subpath) |
$head = $parts[0]; |
} |
fclose($f); |
if ($head !== null) |
return $head; |
} |
} |
|
if (file_exists($git_dir . '/FETCH_HEAD')) { |
// Example content of a Plesk Git folder (fresh): |
// 091a5fa6b157be035e88f5d24aa329ba44d20d63 not-for-merge branch 'master' of https://github.com/danielmarschall/oidplus |
// 091a5fa6b157be035e88f5d24aa329ba44d20d63 not-for-merge remote-tracking branch 'origin/trunk' of https://github.com/danielmarschall/oidplus |
$cont = file_get_contents($git_dir.'/FETCH_HEAD'); |
$commit_object = substr(trim($cont),0,40); |
} else { |
throw new Exception("Cannot detect last commit object"); |
$cont = file_get_contents($git_dir . '/FETCH_HEAD'); |
return substr(trim($cont), 0, 40); |
} |
|
throw new Exception("Cannot detect latest Commit ID"); |
} |
|
function git_get_latest_commit_message(string $git_dir): string { |
// First try an official git client |
$cmd = "git --git-dir=" . escapeshellarg("$git_dir") . " log -1 2>&1"; |
$ec = -1; |
$out = array(); |
@exec($cmd, $out, $ec); |
$out = implode("\n", $out); |
if (($ec == 0) && ($out != '')) return $out; |
|
// If that failed, try to decode the binary files ourselves |
$commit_object = git_get_latest_commit_id($git_dir); |
$objects_dir = $git_dir . '/objects'; |
|
|
// Sometimes, objects are uncompressed, sometimes compressed in a pack file |
// Plesk initial checkout is compressed, but pulls via web interface |
// save uncompressed files |
|
$uncompressed_file = $objects_dir . '/' . substr($commit_object,0,2) . '/' . substr($commit_object,2); |
if (file_exists($uncompressed_file)) { |
// Read compressed data |
$compressed = file_get_contents($uncompressed_file); |
if (class_exists('ViaThinkSoft\Glip\Git')) { |
// https://github.com/danielmarschall/glip |
// composer require danielmarschall/glip |
$git = new ViaThinkSoft\Glip\Git($git_dir); |
$obj = $git->getObject(hex2bin($commit_object)); |
return $obj->detail; |
} else { |
// Own implementation (the compressed read cannot handle delta objects yet) |
|
// Uncompress |
$uncompressed = @gzuncompress($compressed); |
if ($uncompressed === false) throw new Exception("Decompression failed"); |
$uncompressed_file = $objects_dir . '/' . substr($commit_object, 0, 2) . '/' . substr($commit_object, 2); |
if (file_exists($uncompressed_file)) { |
// Read compressed data |
$compressed = file_get_contents($uncompressed_file); |
|
// The format is "commit <nnn>\0<Message>" where <nnn> is only a 3 digit number?! |
$ary = explode(chr(0), $uncompressed); |
$uncompressed = array_pop($ary); |
// Uncompress |
$uncompressed = @gzuncompress($compressed); |
if ($uncompressed === false) throw new Exception("Decompression failed"); |
|
return $uncompressed; |
} else { |
$pack_files = @glob($objects_dir.'/pack/pack-*.pack'); |
$last_exception = 'No pack files found'; |
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 ViaThinkSoft\Glip\Git($git_dir); |
$obj = $git->getObject(hex2bin($commit_object)); |
return $obj->detail; |
} else { |
// Own implementation (cannot read delta objects yet) |
// The format is "<type> <size>\0<Message>" |
list($hdr, $object_data) = explode("\0", $uncompressed, 2); |
// sscanf($hdr, "%s %d", $type, $object_size); |
return $object_data; |
} else { |
$pack_files = @glob($objects_dir . '/pack/pack-*.pack'); |
if ($pack_files) { |
foreach ($pack_files as $basename) { |
$basename = substr(basename($basename), 0, strlen(basename($basename)) - 5); |
return git_read_object($commit_object, |
$objects_dir.'/pack/'.$basename.'.idx', |
$objects_dir.'/pack/'.$basename.'.pack' |
$objects_dir . '/pack/' . $basename . '.idx', |
$objects_dir . '/pack/' . $basename . '.pack' |
); |
} |
} catch (Exception $e) { |
$last_exception = $e; |
} |
throw new Exception("No pack files found"); |
} |
throw new Exception($last_exception); |
} |
} |
|
function git_read_object($object_wanted, $idx_file, $pack_file, $debug=false) { |
function git_read_object(string $object_wanted, string $idx_file, string $pack_file, bool $debug = false): string { |
// More info about the IDX and PACK format: https://git-scm.com/docs/pack-format |
|
// Do some checks |
123,17 → 150,17 |
// Read fanout table |
fseek($fp, $fanout_offset); |
$fanout_ary[0] = 0; |
$fanout_ary = unpack('N*', fread($fp, 4*256)); |
$fanout_ary = unpack('N*', fread($fp, 4 * 256)); |
$num_objects = $fanout_ary[256]; |
|
// Find out approximate object number (from fanout table) |
$fanout_index = hexdec(substr($object_wanted,0,2)); |
if ($debug) echo "Fanout index = ".($fanout_index-1)."\n"; |
$fanout_index = hexdec(substr($object_wanted, 0, 2)); |
if ($debug) echo "Fanout index = " . ($fanout_index - 1) . "\n"; |
$object_no = $fanout_ary[$fanout_index]; // approximate |
if ($debug) echo "Object no approx $object_no\n"; |
|
// Find the exact object number |
fseek($fp, $fanout_offset + 4*256 + 20*$object_no); |
fseek($fp, $fanout_offset + 4 * 256 + 20 * $object_no); |
$object_no--; |
$pack_offset = -1; // avoid that phpstan complains |
do { |
142,7 → 169,7 |
$pack_offset = fread($fp, 4); |
} |
$binary = fread($fp, 20); |
if (substr(bin2hex($binary),0,2) != substr(strtolower($object_wanted),0,2)) { |
if (substr(bin2hex($binary), 0, 2) != substr(strtolower($object_wanted), 0, 2)) { |
throw new Exception("Object $object_wanted not found"); |
} |
} while (bin2hex($binary) != strtolower($object_wanted)); |
150,19 → 177,19 |
|
if ($version == 2) { |
// Get CRC32 |
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"; |
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"; |
|
// Get offset (32 bit) |
fseek($fp, $fanout_offset + 4*256 + 20*$num_objects + 4*$num_objects + 4*$object_no); |
$offset_info = unpack('N', fread($fp,4))[1]; |
fseek($fp, $fanout_offset + 4 * 256 + 20 * $num_objects + 4 * $num_objects + 4 * $object_no); |
$offset_info = unpack('N', fread($fp, 4))[1]; |
if ($offset_info >= 0x80000000) { |
// MSB set, so the offset is 64 bit |
if ($debug) echo "64 bit pack offset\n"; |
$offset_info &= 0x7FFFFFFF; |
fseek($fp, $fanout_offset + 4*256 + 20*$num_objects + 4*$num_objects + 4*$num_objects + 8*$offset_info); |
$pack_offset = unpack('J', fread($fp,8))[1]; |
fseek($fp, $fanout_offset + 4 * 256 + 20 * $num_objects + 4 * $num_objects + 4 * $num_objects + 8 * $offset_info); |
$pack_offset = unpack('J', fread($fp, 8))[1]; |
} else { |
// MSB is not set, so the offset is 32 bit |
if ($debug) echo "32 bit pack offset\n"; |
171,7 → 198,7 |
} |
} |
|
if ($debug) echo "Pack file offset = ".sprintf('0x%x',$pack_offset)."\n"; |
if ($debug) echo "Pack file offset = " . sprintf('0x%x', $pack_offset) . "\n"; |
|
// Close index file |
fclose($fp); |
182,7 → 209,7 |
|
// Read type and first part of the size |
fseek($fp, $pack_offset); |
$size_info = unpack('C', fread($fp,1))[1]; |
$size_info = unpack('C', fread($fp, 1))[1]; |
|
// Detect type |
$type = ($size_info & 0x70) >> 4; /*0b01110000*/ |
211,10 → 238,11 |
} |
|
// Find out the expected unpacked size |
$size = $size_info & 0xF /*0x00001111*/; |
$size = $size_info & 0xF /*0x00001111*/ |
; |
$shift_info = 4; |
while ($size_info >= 0x80) { |
$size_info = unpack('C', fread($fp,1))[1]; |
$size_info = unpack('C', fread($fp, 1))[1]; |
$size = (($size_info & 0x7F) << $shift_info) + $size; |
$shift_info += 7; |
} |
229,7 → 257,7 |
$offset = 0; |
$shift_info = 0; |
do { |
$offset_info = unpack('C', fread($fp,1))[1]; |
$offset_info = unpack('C', fread($fp, 1))[1]; |
$offset = (($offset_info & 0x7F) << $shift_info) + $offset; |
$shift_info += 7; |
} while ($offset_info >= 0x80); |
239,7 → 267,7 |
} |
if ($type == 7/*OBJ_REF_DELTA*/) { |
// "base object name if OBJ_REF_DELTA" |
$delta_info = bin2hex(fread($fp,20)); |
$delta_info = bin2hex(fread($fp, 20)); |
if ($debug) echo "Delta base object name: $delta_info\n"; |
throw new Exception("OBJ_REF_DELTA is currently not implemented"); // TODO! Implement OBJ_REF_DELTA! |
} |
247,10 → 275,10 |
// Read and uncompress the compressed data |
$compressed = ''; |
$uncompressed = false; |
for ($compressed_size=1; $compressed_size<=32768*$size; $compressed_size++) { |
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); |
$compressed .= fread($fp, 1); |
$uncompressed = @gzuncompress($compressed); |
if (strlen($uncompressed) === $size) { |
if ($debug) echo "Detected compressed size = $compressed_size\n"; |