Adding typehints and optional root .git directory check
Publish PHP Package / docker (push) Successful in 7s Details

This commit is contained in:
Pavel Shevaev 2024-08-13 10:57:08 +03:00
parent d57847f525
commit 3a6aed2d29
1 changed files with 13 additions and 11 deletions

View File

@ -12,7 +12,7 @@ task('git_info', function()
echo $info; echo $info;
}); });
function git_do($repo, $action, &$out = NULL, $verbose = true) function git_do(string $repo, string $action, &$out = NULL, bool $verbose = true)
{ {
$cli = "cd $repo && git $action"; $cli = "cd $repo && git $action";
if($verbose) if($verbose)
@ -24,14 +24,16 @@ function git_do($repo, $action, &$out = NULL, $verbose = true)
throw new Exception("Error executing command: $cli ($result)"); throw new Exception("Error executing command: $cli ($result)");
} }
function git_is_repo($directory) function git_is_repo(string $directory, bool $check_root = false) : bool
{ {
if($check_root && !is_dir("$directory/.git"))
return false;
$output = []; $output = [];
git_do($directory, "rev-parse --is-inside-work-tree", $output, false); git_do($directory, "rev-parse --is-inside-work-tree", $output, false);
return !empty($output) && $output[0] === 'true'; return !empty($output) && $output[0] === 'true';
} }
function git_rev_from_commit_message($message) function git_rev_from_commit_message(string $message) : string
{ {
// Regular expression pattern to extract the commit hash // Regular expression pattern to extract the commit hash
$pattern = '/\b[0-9a-f]{7,40}\b/'; $pattern = '/\b[0-9a-f]{7,40}\b/';
@ -42,7 +44,7 @@ function git_rev_from_commit_message($message)
return $commit_hash; return $commit_hash;
} }
function get_git_last_remote_tag($repo) function get_git_last_remote_tag(string $repo) : string|bool
{ {
$remote_tags = []; $remote_tags = [];
git_do($repo, 'ls-remote --tags origin', $output, false); git_do($repo, 'ls-remote --tags origin', $output, false);
@ -60,7 +62,7 @@ function get_git_last_remote_tag($repo)
return _get_last_version_tag($remote_tags); return _get_last_version_tag($remote_tags);
} }
function _get_last_version_tag(array $tags) function _get_last_version_tag(array $tags) : string|bool
{ {
if(!$tags) if(!$tags)
return false; return false;
@ -74,11 +76,11 @@ function _get_last_version_tag(array $tags)
class GitVersion class GitVersion
{ {
public $major; public int $major;
public $minor; public int $minor;
public $patch; public int $patch;
static function parse($version_str) static function parse($version_str) : GitVersion
{ {
$parts = explode('.', $version_str); $parts = explode('.', $version_str);
if(sizeof($parts) != 3) if(sizeof($parts) != 3)
@ -92,7 +94,7 @@ class GitVersion
return $v; return $v;
} }
function bump($up_mode) function bump(int $up_mode)
{ {
if($up_mode == 1) if($up_mode == 1)
++$this->patch; ++$this->patch;
@ -111,7 +113,7 @@ class GitVersion
throw new Exception("Unsupported up mode: $up_mode"); throw new Exception("Unsupported up mode: $up_mode");
} }
function encode() function encode() : string
{ {
return 'v'.$this->major.'.'.$this->minor.'.'.$this->patch; return 'v'.$this->major.'.'.$this->minor.'.'.$this->patch;
} }