taskman_git/git.inc.php

121 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2023-10-23 15:13:23 +03:00
<?php
namespace taskman;
use Exception;
task('git_info', function()
{
list($rev_hash, $branch, $revision_number) = git_get_info();
$info = "==============GIT INFO==============\n"
. "$rev_hash - hash\n"
. "$branch - branch\n"
. "$revision_number - revision\n";
echo $info;
});
function git_do(string $repo, string $action, &$out = NULL, bool $verbose = true)
2023-10-23 15:13:23 +03:00
{
$cli = "cd $repo && git $action";
if($verbose)
echo "Git CLI: $cli\n";
exec($cli, $out, $result);
if($verbose || $result !== 0)
echo implode("\n", $out)."\n";
if($result !== 0)
throw new Exception("Error executing command: $cli ($result)");
}
function git_is_repo(string $directory, bool $check_root = false) : bool
2023-10-23 15:13:23 +03:00
{
if($check_root && !is_dir("$directory/.git"))
return false;
2023-10-23 15:13:23 +03:00
$output = [];
git_do($directory, "rev-parse --is-inside-work-tree", $output, false);
return !empty($output) && $output[0] === 'true';
}
function git_rev_from_commit_message(string $message) : string
2023-10-23 15:13:23 +03:00
{
// Regular expression pattern to extract the commit hash
$pattern = '/\b[0-9a-f]{7,40}\b/';
// Extract the commit hash from the message
preg_match($pattern, $message, $matches);
$commit_hash = $matches[0] ?? '';
return $commit_hash;
}
function get_git_last_remote_tag(string $repo) : string|bool
2023-10-23 15:13:23 +03:00
{
$remote_tags = [];
git_do($repo, 'ls-remote --tags origin', $output, false);
foreach($output as $line)
{
$items = explode("\t", trim($line));
if(sizeof($items) < 2)
continue;
$tag_spec = $items[1];
if(str_ends_with($tag_spec, '^{}'))
continue;
$tag = str_replace('refs/tags/', '', $tag_spec);
$remote_tags[] = $tag;
}
return _get_last_version_tag($remote_tags);
}
function _get_last_version_tag(array $tags) : string|bool
2023-10-23 15:13:23 +03:00
{
if(!$tags)
return false;
usort($tags, function ($tag1, $tag2) {
$version1 = str_replace('v', '', $tag1);
$version2 = str_replace('v', '', $tag2);
return version_compare($version1, $version2);
});
return end($tags);
}
class GitVersion
{
public int $major;
public int $minor;
public int $patch;
2023-10-23 15:13:23 +03:00
static function parse($version_str) : GitVersion
2023-10-23 15:13:23 +03:00
{
$parts = explode('.', $version_str);
if(sizeof($parts) != 3)
throw new Exception("Invalid version string: $version_str");
$v = new GitVersion();
if($parts[0][0] === 'v')
$parts[0] = substr($parts[0], 1);
$v->major = (int)$parts[0];
$v->minor = (int)$parts[1];
$v->patch = (int)$parts[2];
return $v;
}
function bump(int $up_mode)
2023-10-23 15:13:23 +03:00
{
if($up_mode == 1)
++$this->patch;
else if($up_mode == 2)
{
++$this->minor;
$this->patch = 0;
}
else if($up_mode == 3)
{
++$this->major;
$this->minor = 0;
$this->patch = 0;
}
else
throw new Exception("Unsupported up mode: $up_mode");
}
function encode() : string
2023-10-23 15:13:23 +03:00
{
return 'v'.$this->major.'.'.$this->minor.'.'.$this->patch;
}
}