Permalink
Browse files

Add missing CACHE_DIR config entry. Use no cache when CACHE_DIR is not

set. Implement Git command benchmark for debug mode
  • Loading branch information...
1 parent a8e2ab8 commit af87bae3f95674bd6b711dcfc0c5fb72314a38d3 @kayahr committed Jan 26, 2013
View
@@ -44,3 +44,7 @@
// The admin email address (Will be displayed in the footer)
define("ADMIN_EMAIL", "not@configured.tld");
+
+// The directory where igitigit caches various data to improve speed. If not
+// set then no cache will be used.
+//define("CACHE_DIR", "/tmp/igitigit.cache");
@@ -0,0 +1,8 @@
+<?if (DEBUG && isset($REPO)):?>
+ <dl class="benchmark">
+ <?foreach ($REPO->getBenchmark() as $entry):?>
+ <dt><?=$entry["cmd"]?></dt>
+ <dd><?=$entry["time"]?></dd>
+ <?endforeach?>
+ </dl>
+<?endif?>
@@ -10,5 +10,6 @@
</div>
</div>
</div>
+ <?include "parts/benchmark.html.php"?>
</body>
</html>
@@ -53,6 +53,21 @@ class GitRepository implements Repository, Directory, SystemObject
/** The selected branch. */
private $branch;
+ /** Benchmark of the executed git commands for debugging purposes. */
+ private $gitBenchmark = array();
+
+ /** The recorded start time of a Git command. */
+ private $gitStartTime;
+
+ /** The cached available branches. Access it with getBranches() */
+ private $branches;
+
+ /** The caches tags. Access it with getTags() */
+ private $tags;
+
+ /** The currently checked out branch. Access it with getCurrentBranch() */
+ private $currentBranch;
+
/**
* Constructs a new repository.
*
@@ -333,6 +348,7 @@ private function openGit($args___)
foreach (func_get_args() as $arg)
$args[] = escapeshellarg($arg);
$this->gitCmd = implode(" ", $args);
+ $this->gitStartTime = microtime(true);
$this->gitProc = proc_open($this->gitCmd, $descriptors,
$this->gitPipes);
if (!is_resource($this->gitProc))
@@ -345,6 +361,10 @@ private function openGit($args___)
private function closeGit()
{
$result = proc_close($this->gitProc);
+ $this->gitBenchmark[] = array(
+ "cmd" => $this->gitCmd,
+ "time" => microtime(true) - $this->gitStartTime
+ );
if ($result)
throw new GitException($this->gitCmd, $result,
$this->gitErrorFile);
@@ -407,31 +427,14 @@ public function gitForEachLine($callback, $args___)
}
/**
- * @see Repository#getBranches()
- *
- * @return string[]
- */
- public function getBranches()
- {
- $rows = $this->gitRows("\n", "branch");
- $branches = array();
- foreach ($rows as $row)
- {
- if (!$row) continue;
- $branch = substr($row, 2);
- if ($branch == "(no branch)") continue;
- $branches[] = $branch;
- }
- return $branches;
- }
-
- /**
* @see Repository#getTags()
*
* @return string[]
*/
public function getTags()
{
+ if (!is_null($this->tags)) return $this->tags;
+
$rows = $this->gitRows("\n", "for-each-ref", "--sort=taggerdate",
"--format=%(refname:short)", "refs/tags");
$tags = array();
@@ -440,10 +443,27 @@ public function getTags()
if (!$row) continue;
array_unshift($tags, $row);
}
+ $this->tags = $tags;
return $tags;
}
+
+ private function readTags()
+ {
+
+ }
/**
+ * @see Repository#getBranches()
+ *
+ * @return string[]
+ */
+ public function getBranches()
+ {
+ $this->readBranches();
+ return $this->branches;
+ }
+
+ /**
* @see Repository#hasBranch()
*
* @param string $branch
@@ -462,19 +482,31 @@ public function hasBranch($branch)
*/
public function getCurrentBranch()
{
- $rows = $this->gitRows("\n", "branch");
- $branches = array();
- foreach ($rows as $row)
- {
- if (!$row) continue;
- if ($row[0] == "*" && $row != "* (no branch)")
- return substr($row, 2);
- }
- $branches = $this->getBranches();
-
- if (!count($branches)) return NULL;
-
- return $branches[0];
+ $this->readBranches();
+ return $this->currentBranch;
+ }
+
+ /**
+ * Reads the available branches (and the current branch) once and caches
+ * them for this request.
+ */
+ private function readBranches()
+ {
+ // Do nothing if already read
+ if ($this->branches) return;
+
+ $this->currentBranch = NULL;
+ $rows = $this->gitRows("\n", "branch");
+ $this->branches = array();
+ foreach ($rows as $row)
+ {
+ if (!$row) continue;
+ $branch = substr($row, 2);
+ if ($branch == "(no branch)") continue;
+ $this->branches[] = $branch;
+ if (!$this->currentBranch || $row[0] == "*")
+ $this->currentBranch = $branch;
+ }
}
/**
@@ -803,6 +835,7 @@ private function getCacheFile($key)
private function getCachedValue($key)
{
+ if (!defined("CACHE_DIR")) return null;
$file = $this->getCacheFile($key);
if (file_exists($file))
return unserialize(file_get_contents($file));
@@ -811,9 +844,15 @@ private function getCachedValue($key)
private function cacheValue($key, $value)
{
- $file = $this->getCacheFile($key);
+ if (!defined("CACHE_DIR")) return null;
+ $file = $this->getCacheFile($key);
$dir = dirname($file);
if (!file_exists($dir)) mkdir($dir, 0755, true);
file_put_contents($file, serialize($value));
}
+
+ public function getBenchmark()
+ {
+ return $this->gitBenchmark;
+ }
}

0 comments on commit af87bae

Please sign in to comment.