diff --git a/src/IDF/Scm.php b/src/IDF/Scm.php index c104651..9e8de2d 100644 --- a/src/IDF/Scm.php +++ b/src/IDF/Scm.php @@ -40,5 +40,43 @@ class IDF_Scm return call_user_func(array($scms[$scm], 'factory'), $request->project); } + + /** + * Equivalent to exec but with caching. + * + * @param string Command + * @param &array Output + * @param &int Return value + * @return string Last line of the output + */ + public static function exec($command, &$output=array(), &$return=0) + { + $key = md5($command); + $cache = Pluf_Cache::factory(); + if (null === ($res=$cache->get($key))) { + $ll = exec($command, $output, $return); + $cache->set($key, array($ll, $return, $output)); + } else { + list($ll, $return, $output) = $res; + } + return $ll; + } + + /** + * Equivalent to shell_exec but with caching. + * + * @param string Command + * @return string Output of the command + */ + public static function shell_exec($command) + { + $key = md5($command); + $cache = Pluf_Cache::factory(); + if (null === ($res=$cache->get($key))) { + $res = shell_exec($command); + $cache->set($key, $res); + } + return $res; + } } diff --git a/src/IDF/Scm/Git.php b/src/IDF/Scm/Git.php index f072a71..23870fb 100644 --- a/src/IDF/Scm/Git.php +++ b/src/IDF/Scm/Git.php @@ -78,7 +78,7 @@ class IDF_Scm_Git escapeshellarg($this->repo), escapeshellarg($hash)); $ret = 0; $out = array(); - exec($cmd, &$out, &$ret); + IDF_Scm::exec($cmd, &$out, &$ret); if ($ret != 0) return false; return trim($out[0]); } @@ -121,7 +121,7 @@ class IDF_Scm_Git $rawlog = array(); $cmd = sprintf('GIT_DIR=%s git log --raw --abbrev=40 --pretty=oneline %s', escapeshellarg($this->repo), escapeshellarg($commit)); - exec($cmd, &$rawlog); + IDF_Scm::exec($cmd, &$rawlog); // We reverse the log to be able to use a fixed efficient // regex without back tracking. $rawlog = implode("\n", array_reverse($rawlog)); @@ -165,7 +165,7 @@ class IDF_Scm_Git escapeshellarg($tree)); $out = array(); $res = array(); - exec($cmd, &$out); + IDF_Scm::exec($cmd, &$out); foreach ($out as $line) { list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY); $res[] = (object) array('perm' => $perm, 'type' => $type, @@ -190,7 +190,7 @@ class IDF_Scm_Git escapeshellarg($this->repo), escapeshellarg($commit)); $out = array(); - exec($cmd, &$out); + IDF_Scm::exec($cmd, &$out); foreach ($out as $line) { list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY); if ($totest == $file) { @@ -211,9 +211,9 @@ class IDF_Scm_Git */ public function getBlob($request_file_info, $dummy=null) { - return shell_exec(sprintf('GIT_DIR=%s git-cat-file blob %s', - escapeshellarg($this->repo), - escapeshellarg($request_file_info->hash))); + return IDF_Scm::shell_exec(sprintf('GIT_DIR=%s git-cat-file blob %s', + escapeshellarg($this->repo), + escapeshellarg($request_file_info->hash))); } /** @@ -224,8 +224,8 @@ class IDF_Scm_Git public function getBranches() { $out = array(); - exec(sprintf('GIT_DIR=%s git branch', - escapeshellarg($this->repo)), &$out); + IDF_Scm::exec(sprintf('GIT_DIR=%s git branch', + escapeshellarg($this->repo)), &$out); $res = array(); foreach ($out as $b) { $res[] = substr($b, 2); @@ -246,7 +246,7 @@ class IDF_Scm_Git "'".$this->mediumtree_fmt."'", escapeshellarg($commit)); $out = array(); - exec($cmd, &$out); + IDF_Scm::exec($cmd, &$out); $log = array(); $change = array(); $inchange = false; @@ -281,7 +281,7 @@ class IDF_Scm_Git escapeshellarg($this->repo), $n, $this->mediumtree_fmt, escapeshellarg($commit)); $out = array(); - exec($cmd, &$out); + IDF_Scm::exec($cmd, &$out); return self::parseLog($out, 4); } diff --git a/src/IDF/Scm/Svn.php b/src/IDF/Scm/Svn.php index 0b5f561..3cd19fd 100644 --- a/src/IDF/Scm/Svn.php +++ b/src/IDF/Scm/Svn.php @@ -104,7 +104,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo.'/'.$path), escapeshellarg($rev)); - $xmlInfo = shell_exec($cmd); + $xmlInfo = IDF_Scm::shell_exec($cmd); // If exception is thrown, return false try { @@ -142,7 +142,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo.'/'.$folder), escapeshellarg($rev)); - $xmlLs = shell_exec($cmd); + $xmlLs = IDF_Scm::shell_exec($cmd); $xml = simplexml_load_string($xmlLs); $res = array(); foreach ($xml->list->entry as $entry) { @@ -185,7 +185,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($file), escapeshellarg($rev)); - $xmlLog = shell_exec($cmd); + $xmlLog = IDF_Scm::shell_exec($cmd); $xml = simplexml_load_string($xmlLog); return (string) $xml->logentry->msg; } @@ -205,7 +205,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo.'/'.$totest), escapeshellarg($rev)); - $xmlInfo = shell_exec($cmd); + $xmlInfo = IDF_Scm::shell_exec($cmd); $xml = simplexml_load_string($xmlInfo); $entry = $xml->entry; @@ -237,7 +237,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo.'/'.$request_file_info->fullpath), escapeshellarg($rev)); - return shell_exec($cmd); + return IDF_Scm::shell_exec($cmd); } @@ -267,7 +267,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo), escapeshellarg($rev)); - $xmlRes = shell_exec($cmd); + $xmlRes = IDF_Scm::shell_exec($cmd); $xml = simplexml_load_string($xmlRes); $res['author'] = (string) $xml->logentry->author; @@ -289,7 +289,7 @@ class IDF_Scm_Svn escapeshellarg($this->username), escapeshellarg($this->password), escapeshellarg($this->repo)); - return shell_exec($cmd); + return IDF_Scm::shell_exec($cmd); } @@ -310,7 +310,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo), escapeshellarg($rev)); - $xmlRes = shell_exec($cmd); + $xmlRes = IDF_Scm::shell_exec($cmd); $xml = simplexml_load_string($xmlRes); $res = array(); @@ -358,7 +358,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo.'/'.$path), escapeshellarg($rev)); - $xmlProps = shell_exec($cmd); + $xmlProps = IDF_Scm::shell_exec($cmd); $props = simplexml_load_string($xmlProps); // No properties, returns an empty array @@ -393,7 +393,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo.'/'.$path), escapeshellarg($rev)); - $xmlProp = shell_exec($cmd); + $xmlProp = IDF_Scm::shell_exec($cmd); $prop = simplexml_load_string($xmlProp); return (string) $prop->target->property; @@ -415,7 +415,7 @@ class IDF_Scm_Svn escapeshellarg($this->password), escapeshellarg($this->repo), escapeshellarg($rev)); - $xmlInfo = shell_exec($cmd); + $xmlInfo = IDF_Scm::shell_exec($cmd); $xml = simplexml_load_string($xmlInfo); return (string) $xml->entry->commit['revision'];