Subversion Repositories php_utils

Compare Revisions

Regard whitespace Rev 82 → Rev 81

/trunk/composer.json
Cannot display: file marked as a binary type.
svn:mime-type = application/json
/trunk/git_utils.inc.php
18,81 → 18,42
* limitations under the License.
*/
 
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/...")
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;
 
// 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
return trim(file_get_contents($git_dir . '/' . $m[1]));
}
 
if (file_exists($git_dir . '/refs/heads/master')) {
// Not available
$commit_object = trim(file_get_contents($git_dir.'/'.$m[1]));
} else if (file_exists($git_dir.'/refs/heads/master')) {
// Missing at Plesk Git initial checkout, but available on update.
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')) {
$commit_object = trim(file_get_contents($git_dir.'/refs/heads/master'));
} else 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');
return substr(trim($cont), 0, 40);
$commit_object = substr(trim($cont),0,40);
} else {
throw new Exception("Cannot detect last commit object");
}
 
throw new Exception("Cannot detect latest Commit ID");
}
$objects_dir = $git_dir . '/objects';
 
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
102,27 → 63,39
$uncompressed = @gzuncompress($compressed);
if ($uncompressed === false) throw new Exception("Decompression failed");
 
// 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;
// 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;
} else {
$pack_files = @glob($objects_dir . '/pack/pack-*.pack');
if ($pack_files) {
foreach ($pack_files as $basename) {
$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)
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(string $object_wanted, string $idx_file, string $pack_file, bool $debug = false): string {
function git_read_object($object_wanted, $idx_file, $pack_file, $debug=false) {
// More info about the IDX and PACK format: https://git-scm.com/docs/pack-format
 
// Do some checks
238,8 → 211,7
}
 
// 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];