18,42 → 18,81 |
* 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])) { |
// 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"); |
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 |
|
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) |
|
$uncompressed_file = $objects_dir . '/' . substr($commit_object,0,2) . '/' . substr($commit_object,2); |
if (file_exists($uncompressed_file)) { |
// Read compressed data |
63,39 → 102,27 |
$uncompressed = @gzuncompress($compressed); |
if ($uncompressed === false) throw new Exception("Decompression failed"); |
|
// The format is "commit <nnn>\0<Message>" where <nnn> is only a 3 digit number?! |
$ary = explode(chr(0), $uncompressed); |
$uncompressed = array_pop($ary); |
|
return $uncompressed; |
// 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'); |
$last_exception = 'No pack files found'; |
if ($pack_files) foreach ($pack_files as $basename) { |
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) |
return git_read_object($commit_object, |
$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 |
211,7 → 238,8 |
} |
|
// 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]; |