Skip to content
Snippets Groups Projects
Commit 8c95acd8 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

vcs interface + git: added variable types; short array syntax

parent 79eda527
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
...@@ -9,74 +9,85 @@ require_once __DIR__.'/../../vendor/axelhahn/ahcache/cache.class.php'; ...@@ -9,74 +9,85 @@ require_once __DIR__.'/../../vendor/axelhahn/ahcache/cache.class.php';
* *
* *
* @author hahn * @author hahn
*
* Axel: <axel.hahn@unibe.ch>
* (...)
* 2024-08-28 Axel php8 only; added variable types; short array syntax
*/ */
class vcs implements iVcs { class vcs implements iVcs
{
// class vcs { // class vcs {
/** /**
* configuration * configuration
* @var array * @var array
*/ */
private $_aCfg = array(); private array $_aCfg = [];
/** /**
* temp dir to fetch repo version and ommit message; its value will be * temp dir to fetch repo version and ommit message; its value will be
* generated in set_config() * generated in set_config()
* @var string * @var string
*/ */
private $_sTempDir = false; // private string $_sTempDir = ''; //
/** /**
* filename of ssh key file with complete path * filename of ssh key file with complete path
* @var string * @var string
*/ */
private $_sKeyfile = false; private string $_sKeyfile = '';
/** /**
* filename of ssh wrapper script with complete path * filename of ssh wrapper script with complete path
* @var string * @var string
*/ */
private $_sWrapper = false; private string $_sWrapper = '';
/** /**
* flat array with remote branch names * flat array with remote branch names
* @var array * @var array
*/ */
private $_aRemoteBranches = array(); private array $_aRemoteBranches = [];
/** /**
* name of the default remote branch to access * name of the default remote branch to access
* @var string * @var string
*/ */
private $_sCurrentBranch = ""; private string $_sCurrentBranch = '';
/** /**
* constructor * Constructor
* @param array $aRepoConfig * @param array $aRepoConfig
*/ */
public function __construct($aRepoConfig = array()) { public function __construct(array $aRepoConfig = [])
{
$this->setConfig($aRepoConfig); $this->setConfig($aRepoConfig);
$this->getRemoteBranches(); // to fill the cache $this->getRemoteBranches(); // to fill the cache
} }
/** /**
* add a log messsage * Add a log messsage
* @global object $oLog * @global object $oLog
*
* @param string $sMessage messeage text * @param string $sMessage messeage text
* @param string $sLevel warnlevel of the given message * @param string $sLevel warnlevel of the given message
* @return bool * @return bool
*/ */
private function log($sMessage,$sLevel="info"){ private function log(string $sMessage, string $sLevel = "info"): bool
{
global $oCLog; global $oCLog;
return $oCLog->add(basename(__FILE__) . " class " . __CLASS__ . " - " . $sMessage, $sLevel); return $oCLog->add(basename(__FILE__) . " class " . __CLASS__ . " - " . $sMessage, $sLevel);
} }
/** /**
* set a config and update internal (private) variables * Set a config and update internal (private) variables
*
* @param array $aRepoConfig * @param array $aRepoConfig
* @return boolean * @return boolean
*/ */
public function setConfig($aRepoConfig = array()) { public function setConfig(array $aRepoConfig = []): bool
{
// checks // checks
// foreach (array("type", "url") as $key) { // foreach (array("type", "url") as $key) {
foreach (array("type") as $key) { foreach (array("type") as $key) {
...@@ -99,15 +110,18 @@ class vcs implements iVcs { ...@@ -99,15 +110,18 @@ class vcs implements iVcs {
$this->_sWrapper = $this->_aCfg["appRootDir"] . "/shellscripts/gitsshwrapper.sh"; $this->_sWrapper = $this->_aCfg["appRootDir"] . "/shellscripts/gitsshwrapper.sh";
$this->_setTempdir(); $this->_setTempdir();
return $this->_aCfg = $aRepoConfig; $this->_aCfg = $aRepoConfig;
return true;
} }
/** /**
* set directory für current branch of a project below tempdir * Get directory für current branch of a project below tempdir
* In it the branch will be initialized * If it does not exist yet it will be created and the repository will be initialized.
* @return type *
* @return string
*/ */
private function _setTempdir() { private function _setTempdir()
{
$this->_sTempDir = $this->_aCfg["url"]; $this->_sTempDir = $this->_aCfg["url"];
$this->_sTempDir = preg_replace('/[\@\.\:\/]/', '_', $this->_sTempDir); $this->_sTempDir = preg_replace('/[\@\.\:\/]/', '_', $this->_sTempDir);
$this->_sTempDir = $this->_aCfg["tmpDir"] . '/checkout_vcsgit_' . $this->_sTempDir . '/'; $this->_sTempDir = $this->_aCfg["tmpDir"] . '/checkout_vcsgit_' . $this->_sTempDir . '/';
...@@ -129,20 +143,23 @@ class vcs implements iVcs { ...@@ -129,20 +143,23 @@ class vcs implements iVcs {
} }
/** /**
* set the current branch * Set the current branch
*
* @param string $sBranchname name of the branch * @param string $sBranchname name of the branch
* @return void
*/ */
public function setCurrentBranch($sBranchname) { public function setCurrentBranch($sBranchname): void
{
$this->_sCurrentBranch = $sBranchname; $this->_sCurrentBranch = $sBranchname;
$this->_setTempdir(); $this->_setTempdir();
return $this->_sCurrentBranch;
} }
/** /**
* helper: dump values * helper: dump values
* @return boolean * @return boolean
*/ */
public function dump() { public function dump(): bool
{
echo "<h3>Dump class " . __CLASS__ . "</h3>"; echo "<h3>Dump class " . __CLASS__ . "</h3>";
echo "config array: <pre>" . print_r($this->_aCfg, true) . "</pre>"; echo "config array: <pre>" . print_r($this->_aCfg, true) . "</pre>";
echo "temp dir to read revision: " . $this->_sTempDir . "<br>"; echo "temp dir to read revision: " . $this->_sTempDir . "<br>";
...@@ -151,10 +168,14 @@ class vcs implements iVcs { ...@@ -151,10 +168,14 @@ class vcs implements iVcs {
/** /**
* cleanup unneeded files and directories in a checked out directory * cleanup unneeded files and directories in a checked out directory
* and remove all vcs specific files and directories * and remove all vcs specific files and directories.
* This method works on linux only
*
* @param string $sWorkDir path of the build directory to cleanup the git meta data from
* @return bool * @return bool
*/ */
public function cleanupWorkdir($sWorkDir) { public function cleanupWorkdir(string $sWorkDir): bool
{
if (!is_dir($sWorkDir)) { if (!is_dir($sWorkDir)) {
return false; return false;
} }
...@@ -164,11 +185,13 @@ class vcs implements iVcs { ...@@ -164,11 +185,13 @@ class vcs implements iVcs {
} }
/** /**
* get the current branch * Get the current branch
*
* @param bool $bReturnMasterIfEmpty flag: if there is no current branch then detect a master branch * @param bool $bReturnMasterIfEmpty flag: if there is no current branch then detect a master branch
* @return string * @return string
*/ */
public function getCurrentBranch($bReturnMasterIfEmpty=false) { public function getCurrentBranch(bool $bReturnMasterIfEmpty = false): string
{
if (!$this->_sCurrentBranch) { if (!$this->_sCurrentBranch) {
if ($bReturnMasterIfEmpty) { if ($bReturnMasterIfEmpty) {
$this->_sCurrentBranch = $this->_getMasterbranchname(); $this->_sCurrentBranch = $this->_getMasterbranchname();
...@@ -178,10 +201,12 @@ class vcs implements iVcs { ...@@ -178,10 +201,12 @@ class vcs implements iVcs {
} }
/** /**
* detect an existing master branch ... and return one of 'origin/main' | 'origin/master' * Detect an existing master branch ... and return one of 'origin/main' | 'origin/master'
*
* @return string * @return string
*/ */
protected function _getMasterbranchname(){ protected function _getMasterbranchname(): string
{
$sMasterBranch = ''; $sMasterBranch = '';
$aMasternames = ['origin/main', 'origin/master']; $aMasternames = ['origin/main', 'origin/master'];
...@@ -198,26 +223,32 @@ class vcs implements iVcs { ...@@ -198,26 +223,32 @@ class vcs implements iVcs {
} }
/** /**
* return the build type, i.e. git|svn|cvs| * Get the build type, i.e. git|svn|cvs|
* @return string * @return string
*/ */
public function getBuildType() { public function getBuildType(): string
return $this->_aCfg["type"]; {
return $this->_aCfg["type"] ?? '';
} }
/** /**
* get a nice name for a cache module based on repo url * Get a nice name for a cache module based on repo url
* @return type *
* @return string
*/ */
private function _getNameOfCacheModule() { private function _getNameOfCacheModule(): string
{
return preg_replace('/([^0-9a-z])/i', "", $this->getUrl()); return preg_replace('/([^0-9a-z])/i', "", $this->getUrl());
} }
/** /**
* cleanup cache data for this project (revisions, list of branches+tags) * Cleanup cache data for this project (revisions, list of branches+tags)
*
* @param int $iAge max age in sec; older items will be deleted
* @return bool * @return bool
*/ */
public function cleanupCache($iAge) { public function cleanupCache(int $iAge): string
{
$this->log(__FUNCTION__ . " start"); $this->log(__FUNCTION__ . " start");
$oCache = new AhCache($this->_getNameOfCacheModule()); $oCache = new AhCache($this->_getNameOfCacheModule());
ob_start(); ob_start();
...@@ -228,11 +259,13 @@ class vcs implements iVcs { ...@@ -228,11 +259,13 @@ class vcs implements iVcs {
} }
/** /**
* helper: cache hash with all branches * helper: store hash with all branches in cache
* It saves 1.5 sec for reading 300 branches * It saves 1.5 sec for reading 300 branches
*
* @return boolean * @return boolean
*/ */
private function _cacheRemoteBranches() { private function _cacheRemoteBranches(): bool
{
$iTtl = 300; $iTtl = 300;
$oCache = new AhCache($this->_getNameOfCacheModule(), "RemoteBranches"); $oCache = new AhCache($this->_getNameOfCacheModule(), "RemoteBranches");
// $this->log(__FUNCTION__." <pre>".print_r($this->_aRemoteBranches, 1)."</pre>"); // $this->log(__FUNCTION__." <pre>".print_r($this->_aRemoteBranches, 1)."</pre>");
...@@ -241,15 +274,18 @@ class vcs implements iVcs { ...@@ -241,15 +274,18 @@ class vcs implements iVcs {
} }
/** /**
* read remote repository and get an array with names and revisions of * Read remote repository and get an array with names and revisions of
* all branches and tags * all branches and tags
* pre branch you get an array element with the keys revision, name, type * per branch you get an array element with the keys revision, name, type
* It returns false if there is no git url
*
* @param bool $bIgnoreCache flag to overrde cache * @param bool $bIgnoreCache flag to overrde cache
* @return array * @return array
*/ */
private function _fetchRemoteBranches($bIgnoreCache = false) { private function _fetchRemoteBranches($bIgnoreCache = false): bool|array
{
$this->log(__FUNCTION__ . "(bIgnoreCache = " . ($bIgnoreCache ? 'true' : 'false') . ") start"); $this->log(__FUNCTION__ . "(bIgnoreCache = " . ($bIgnoreCache ? 'true' : 'false') . ") start");
$aReturn = array(); $aReturn = [];
$sGitUrl = $this->getUrl(); $sGitUrl = $this->getUrl();
if (!$sGitUrl) { if (!$sGitUrl) {
...@@ -349,11 +385,12 @@ class vcs implements iVcs { ...@@ -349,11 +385,12 @@ class vcs implements iVcs {
} }
/** /**
* get a flat array with names of all remote branches * Get a flat array with names of all remote branches
* @param bool $bIgnoreCache flag: ignore caching; default: use cache * @param bool $bIgnoreCache flag: ignore caching; default: use cache
* @return array * @return array
*/ */
public function getRemoteBranches($bIgnoreCache=false) { public function getRemoteBranches(bool $bIgnoreCache = false): array
{
$this->log(__FUNCTION__ . "($bIgnoreCache) start"); $this->log(__FUNCTION__ . "($bIgnoreCache) start");
if (!$this->_aRemoteBranches || $bIgnoreCache) { if (!$this->_aRemoteBranches || $bIgnoreCache) {
$this->log(__FUNCTION__ . "($bIgnoreCache) --> fetching fresh data"); $this->log(__FUNCTION__ . "($bIgnoreCache) --> fetching fresh data");
...@@ -365,12 +402,15 @@ class vcs implements iVcs { ...@@ -365,12 +402,15 @@ class vcs implements iVcs {
} }
/** /**
* get current revision and commit message from remote repository * Get current revision and commit message from remote repository
* @see $this::getRevision * @see $this::getRevision
* It returns false if no branch is set
*
* @param boolean $bRefresh optional: refresh data; default: use cache * @param boolean $bRefresh optional: refresh data; default: use cache
* @return array * @return bool|array
*/ */
public function getRepoRevision($bRefresh=false) { public function getRepoRevision(bool $bRefresh = false): bool|array
{
$this->log(__FUNCTION__ . "($bRefresh) start"); $this->log(__FUNCTION__ . "($bRefresh) start");
if (!$this->_sCurrentBranch) { if (!$this->_sCurrentBranch) {
return false; return false;
...@@ -392,12 +432,15 @@ class vcs implements iVcs { ...@@ -392,12 +432,15 @@ class vcs implements iVcs {
} }
/** /**
* get a commit message of a given branch * Get a commit message of a given branch.
* It reurns if no branch was found in meta infos
*
* @param string $sBranch name of a branch * @param string $sBranch name of a branch
* @param string $sVerifyRevision optional: revision to verify if it is the newsest * @param string $sVerifyRevision optional: revision to verify if it is the newsest
* @return string * @return string
*/ */
public function getCommitmessageByBranch($sBranch = false, $sVerifyRevision = false) { public function getCommitmessageByBranch(string $sBranch = '', string $sVerifyRevision = ''): bool|string
{
$this->log(__FUNCTION__ . "($sBranch, $sVerifyRevision) start"); $this->log(__FUNCTION__ . "($sBranch, $sVerifyRevision) start");
if (!$sBranch) { if (!$sBranch) {
$sBranch = $this->_sCurrentBranch; $sBranch = $this->_sCurrentBranch;
...@@ -440,7 +483,7 @@ class vcs implements iVcs { ...@@ -440,7 +483,7 @@ class vcs implements iVcs {
} }
/** /**
* get current revision and commit message from an existing directory or a * Get current revision and commit message from an existing directory or a
* remote repository * remote repository
* the return will fill $this->_aData["phases"]["source"] in project class * the return will fill $this->_aData["phases"]["source"] in project class
* (array keys are revision, message or error) * (array keys are revision, message or error)
...@@ -455,13 +498,15 @@ class vcs implements iVcs { ...@@ -455,13 +498,15 @@ class vcs implements iVcs {
* array( * array(
* "error" => $sErrormessage, * "error" => $sErrormessage,
* ); * );
*
* @param string $sWorkDir optional: local directory with initialized git repo * @param string $sWorkDir optional: local directory with initialized git repo
* @return array * @return bool|array
*/ */
public function getRevision($sWorkDir = false) { public function getRevision(string $sWorkDir = ''): bool|array
{
$this->log(__FUNCTION__ . " start"); $this->log(__FUNCTION__ . " start");
$aReturn = array(); $aReturn = [];
$aOutput=array(); $aOutput = [];
$iRc = false; $iRc = false;
if (!$this->getUrl()) { if (!$this->getUrl()) {
return false; return false;
...@@ -521,7 +566,7 @@ class vcs implements iVcs { ...@@ -521,7 +566,7 @@ class vcs implements iVcs {
// parse revision // parse revision
$sRevision = false; $sRevision = false;
$aRev=array(); $aRev = [];
if (preg_match('#commit\ (.*)#', $sLoginfo, $aRev)) { if (preg_match('#commit\ (.*)#', $sLoginfo, $aRev)) {
$sRevision = $aRev[1]; $sRevision = $aRev[1];
} }
...@@ -555,10 +600,15 @@ class vcs implements iVcs { ...@@ -555,10 +600,15 @@ class vcs implements iVcs {
} }
/** /**
* get sources from vsc and check them out in given directory * Get sources from vsc and check them out in given directory
* @return bool * It returns false if the workdir was not found or the url for git repo is not set
* Otherwise it retunrs the output of git clone + git checkout
*
* @param string $sWorkDir working dir where to check out source url
* @return bool|string
*/ */
public function getSources($sWorkDir) { public function getSources(string $sWorkDir): bool|string
{
$this->log(__FUNCTION__ . " start"); $this->log(__FUNCTION__ . " start");
if (!$sWorkDir || !is_dir($sWorkDir)) { if (!$sWorkDir || !is_dir($sWorkDir)) {
return false; return false;
...@@ -586,19 +636,25 @@ class vcs implements iVcs { ...@@ -586,19 +636,25 @@ class vcs implements iVcs {
} }
/** /**
* return url to vcs sources * Get url to vcs sources
*
* @return string
*/ */
public function getUrl() { public function getUrl(): string
{
$this->log(__FUNCTION__ . " --> " . $this->_aCfg["url"]); $this->log(__FUNCTION__ . " --> " . $this->_aCfg["url"]);
return $this->_aCfg["url"]; return $this->_aCfg["url"] ?? '';
} }
/** /**
* return url to view sources in webrowser to generate an infolink * Get url to view sources in webrowser to generate an infolink
*
* @return string
*/ */
public function getWebGuiUrl() { public function getWebGuiUrl(): string
{
$this->log(__FUNCTION__ . " start"); $this->log(__FUNCTION__ . " start");
return $this->_aCfg["webaccess"]; return $this->_aCfg["webaccess"] ?? '';
} }
} }
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
* *
* interface for a version control system * interface for a version control system
* @author hahn * @author hahn
*
* Axel: <axel.hahn@unibe.ch>
* (...)
* 2024-08-28 Axel php8 only; added variable types; short array syntax
*/ */
interface iVcs { interface iVcs {
...@@ -14,46 +18,54 @@ interface iVcs { ...@@ -14,46 +18,54 @@ interface iVcs {
/** /**
* return url to vcs sources * return url to vcs sources
*/ */
public function getUrl(); public function getUrl(): string;
/** /**
* return url to view sources in webrowser to generate an infolink * return url to view sources in webrowser to generate an infolink
*/ */
public function getWebGuiUrl(); public function getWebGuiUrl(): string;
/** /**
* return the build type, i.e. git|svn|cvs| * return the build type, i.e. git|svn|cvs|
*/ */
public function getBuildType(); public function getBuildType(): string;
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// actions // actions
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* cleanup unneeded files and directories in a checked out directory * cleanup unneeded files and directories in a checked out directory
* and remove all vcs specific files and directories * and remove all vcs specific files and directories.
* This method works on linux only
*
* @param string $sWorkDir path of the build directory to cleanup the vcs meta data from
* @return bool * @return bool
*/ */
public function cleanupWorkdir($sWorkDir); public function cleanupWorkdir(string $sWorkDir): bool;
/** /**
* get current revision and commit message from remote repository * get current revision and commit message from remote repository
* @param boolean $bRefresh optional: refresh data; default: use cache * @param boolean $bRefresh optional: refresh data; default: use cache
* @return array * @return array
*/ */
public function getRepoRevision($bRefresh=false); public function getRepoRevision(bool $bRefresh=false);
/** /**
* get current revision and log message from given directory * get current revision and log message from given directory
* @return array * @return bool|array
*/ */
public function getRevision($sWorkDir); public function getRevision(string $sWorkDir = ''): bool|array;
/** /**
* get sources from vsc and check them out in given directory * Get sources from vsc and check them out in given directory
* @return bool * It returns false if the workdir was not found or the url for git repo is not set
* Otherwise it retunrs the output of the checkout commands
*
* @param string $sWorkDir working dir where to check out source url
* @return bool|string
*/ */
public function getSources($sWorkDir); public function getSources(string $sWorkDir): bool|string;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment