From e76afadae1845e3b914a778033cbe4b2714f70b1 Mon Sep 17 00:00:00 2001
From: hahn <axel.hahn@iml.unibe.ch>
Date: Fri, 23 Mar 2018 12:17:27 +0100
Subject: [PATCH] #1797 - non cached view of the current branch on start of
 build

---
 .../deployment/classes/project.class.php      |  2 +-
 .../deployment/classes/vcs.git.class.php      | 51 ++++++++++++++++---
 2 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/public_html/deployment/classes/project.class.php b/public_html/deployment/classes/project.class.php
index 9852f388..283b81fb 100644
--- a/public_html/deployment/classes/project.class.php
+++ b/public_html/deployment/classes/project.class.php
@@ -1255,7 +1255,7 @@ class project extends base {
      * @return array
      */
     public function getRepoRevision($bRefresh = false) {
-        $this->log(__FUNCTION__ . " start");
+        $this->log(__FUNCTION__ . "($bRefresh) start");
 
         if (!$this->_aPrjConfig["build"]["type"]) {
             $this->_aData["phases"]["source"] = array("error" => t("class-project-error-repo-type-not-set"),);
diff --git a/public_html/deployment/classes/vcs.git.class.php b/public_html/deployment/classes/vcs.git.class.php
index a06fa59d..bab4d552 100644
--- a/public_html/deployment/classes/vcs.git.class.php
+++ b/public_html/deployment/classes/vcs.git.class.php
@@ -1,6 +1,7 @@
 <?php
 
 require_once("vcs.interface.php");
+require_once 'cache.class.php';
 
 /**
  * version control system :: GIT
@@ -184,7 +185,6 @@ class vcs implements iVcs {
      */
     public function cleanupCache($iAge) {
         $this->log(__FUNCTION__." start");
-        require_once 'cache.class.php';
         $oCache = new AhCache($this->_getNameOfCacheModule());
         ob_start();
         $oCache->cleanup((int)$iAge);
@@ -193,6 +193,22 @@ class vcs implements iVcs {
         return $sOut;
     }
 
+    /**
+     * helper: cache hash with all branches
+     * @return boolean
+     */
+    private function _cacheRemoteBranches() {
+        $iTtl=300;
+        $oCache = new AhCache($this->_getNameOfCacheModule(), "RemoteBranches");
+        // $this->log(__FUNCTION__." <pre>".print_r($this->_aRemoteBranches, 1)."</pre>");
+        $oCache->write($this->_aRemoteBranches, $iTtl);
+        return true;
+    }
+    
+    private function _extend_brancharray($a){
+        
+    }
+    
     /**
      * read remote repository and get an array with names and revisions of 
      * all branches and tags
@@ -208,8 +224,6 @@ class vcs implements iVcs {
             return false;
         }
 
-        $iTtl = 300; // cache for 5 min
-        require_once 'cache.class.php';
         $oCache = new AhCache($this->_getNameOfCacheModule(), "RemoteBranches");
         $aOutput=false; 
         $iRc=false;
@@ -233,24 +247,36 @@ class vcs implements iVcs {
 
                 // use cache that getCommitmessageByBranch can access it
                 $this->_aRemoteBranches = $oCache->read();
+                $this->log(__FUNCTION__ . ' data from cache: <pre>'.print_r($this->_aRemoteBranches, 1).'</pre>');
+                /**
+                 * $aOutput = Array
+                 * (
+                 *     [0] => cd7b238a75fe3df3f53ca3c258078d6736cc5bec	refs/heads/foreman-integration
+                 *     [1] => 68cef2c74b58db8e13413c1c54104e060d8ffbb3	refs/heads/master
+                 *     [2] => cae3a1eaee180b05fff883d1cfb36d09778dec2c	refs/heads/task-1726-gitssh-extensions
+                 * )
+                 */
 
                 foreach ($aOutput as $sBranchLine) {
+                    $this->log(__FUNCTION__ . ' loop over output of git ls-remote <pre>'.print_r($sBranchLine, 1).'</pre>');
                     $aTmp = explode("\t", $sBranchLine);
 
                     $aBranch = explode("/", $aTmp[1]);
-                    $sBranch = array_pop($aBranch);
-                    $sRevision = $aTmp[0];
+                    $sBranch = array_pop($aBranch); // remove "refs"
 
                     // skip dereferences
                     // http://stackoverflow.com/questions/15472107/when-listing-git-ls-remote-why-theres-after-the-tag-name
                     if (!preg_match('/\^\{\}$/', $sBranch)) {
+                        $sRevision = $aTmp[0];
                         $sType = array_pop($aBranch);
                         $sName = ($sType == "heads") ? "origin/" . $sBranch : $sBranch;
+                        
                         $sBranchKey = $sName;
+                        $this->log(__FUNCTION__ . ' $sBranchKey = '.$sBranchKey);
 
                         $sMessage = $this->getCommitmessageByBranch($sName, $sRevision);
                         $aReturn[$sBranchKey] = array(
-                            // 'debug'=> $aTmp,
+                            'debug'=> $aTmp,
                             'revision' => $sRevision,
                             'name' => $sName,
                             'label' => $sType . ': ' . $sBranch,
@@ -260,7 +286,7 @@ class vcs implements iVcs {
                     }
                 }
                 $this->_aRemoteBranches = $aReturn;
-                $oCache->write($aReturn, $iTtl);
+                $this->_cacheRemoteBranches();
             }
         } else {
             $this->_aRemoteBranches = $oCache->read();
@@ -335,6 +361,14 @@ class vcs implements iVcs {
         } else {
             $a = $this->getRevision(false);
         }
+        // merge with cached info ... to add type and label
+        if(isset($this->_aRemoteBranches[$a['branch']])){
+            $this->_aRemoteBranches[$a['branch']]=array_merge($this->_aRemoteBranches[$a['branch']], $a);
+        } else {
+            $this->_aRemoteBranches[$a['branch']]=$a;
+        }
+        // store in cache
+        $this->_cacheRemoteBranches();
         return $a['message'];
     }
 
@@ -427,7 +461,7 @@ class vcs implements iVcs {
             $aReturn = array(
                 "branch" => $this->_sCurrentBranch,
                 "revision" => $sRevision,
-                "message" => $sCommitMsg
+                "message" => $sCommitMsg // ."\n". microtime(true),
             );
         } else {
             if (!$sLoginfo) {
@@ -438,6 +472,7 @@ class vcs implements iVcs {
                 "error" => '<pre>' . $sLoginfo . '<hr>' . $sGitCmd . '</pre>'
             );
         }
+        // $this->log(__FUNCTION__ . ' return is <pre>'.print_r($aReturn, 1).'</pre>');
         return $aReturn;
     }
 
-- 
GitLab