Skip to content

Commit

Permalink
Better look, detailed info (#2)
Browse files Browse the repository at this point in the history
Refactored for more details

- Extracted templates
- New layout
- Support for named branches (feature/name, hotfix/name...)
- New SVG icon
  • Loading branch information
Štěpán Škorpil authored and Jan Drábek committed Apr 24, 2017
1 parent 1cfc4ef commit 3617ba9
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 19 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"name": "Jan Drábek",
"homepage": "http://www.jandrabek.cz"
},
{
"name": "Štěpán Škorpil",
"homepage": "http://www.skorpils.cz"
}
],
"require": {
Expand Down
88 changes: 69 additions & 19 deletions src/GitVersionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,54 @@
*
* @author Jan Drábek
* @author Vojtěch Vondra
* @author Štěpán Škorpil
*/
class GitVersionPanel implements IBarPanel
{

private $read = false;

private $dir;

private $branch;
private $commit;

public function getPanel()
{
$this->parseHead();
return '<h1>Git Version</h1><p>Revision: ' . $this->getCurrentCommitHash() . '</p>';
ob_start(function () {
});
require __DIR__ . '/templates/GitVersionPanel.panel.phtml';
return ob_get_clean();
}

protected function getLogTail($rowCount = 5)
{
$dir = $this->findGitDir();
$logHead = $dir . '/logs/HEAD';
if (!$dir || !is_readable($logHead)) {
return [];
}
$fp = fopen($logHead, 'r');
fseek($fp, -1, SEEK_END);
$pos = ftell($fp);
$log = "";
$rowCounter = -1;
while ($rowCounter <= $rowCount && $pos >= 0) {
$char = fgetc($fp);
$log = $char . $log;
if ($char == "\n")
$rowCounter++;
fseek($fp, $pos--);
}
$result = [];
foreach (explode("\n", trim($log)) as $row) {
$input = [];
list($row, $input['action']) = explode("\t", $row, 2);
list($input['from'], $input['to'], $input['user']) = explode(" ", $row, 3);
$result[] = $input;
}
return $result;
}

protected function getCurrentBranchName()
Expand All @@ -45,35 +80,50 @@ protected function getCurrentCommitHash()

public function getTab()
{
return '<img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAedJREFUeNqMk79LXEEQx+e8FyKIGAKBNCKxiXiIP4r8EB9iEUFFSKFgDPkDrjpLi3QhfYSApSAoop34I4WNXAoVq9icheH8dXLxok9Ofd6+3Vtnxn2PxzuiGfgwszO735vZtxf7Mv8BIlZn/AU8YJ+HZ6FKKQUhPsVjlkNQHKlVQGYpKX3BAStuTb9rHeLFyvbMtFTyDMPl+7qokp4EpKus9FJPy3u4EXmGYspRzeypgAU8z4tLqdJ2oh/KugjO1R+GYspRjfYgEIVH8ISgYezVjbkk+lG77Q0X1rbmyc0ik4j61wiWKAnyPw2jN8Llgsl/DO19iSSN4G5wB9gBhHHFNRPNI8nOlt4Uzp7B+BXlWIB+KYxbchmznkJ+IxpJPXqsobtjEMXkJq7fkkBsZDwRHkm3tzdzcLzvQEfChvrnjVBX+5Rz679m4NmTBtCiGn6kF0DrsmX5rfhG7ZP19QzDUSED23tL2FExqBddB2JeDY+FAsElBuZfYnpnAXKHebg4u6RPybmm1hdQdK4gd3CKh7W9+D2rKjooGYFctkCHJzD8ipwi3/7mz1Mnh3z4Na637t6BeRCBgFdiX8ifkxsLlSb3944h+hkt/0n6JuRdR9G8OTRW8ZBCfyZWz2xmk34M/2G3AgwAYPB4kNQnLB4AAAAASUVORK5CYII=" />'
. $this->getCurrentBranchName();
ob_start(function () {
});
require __DIR__ . '/templates/GitVersionPanel.tab.phtml';
return ob_get_clean();
}

private function findGitDir()
{
if ($this->dir)
return $this->dir;

$scriptPath = $_SERVER['SCRIPT_FILENAME'];
$dir = realpath(dirname($scriptPath));
while ($dir !== false) {
flush();
$currentDir = $dir;
$dir .= '/..';
$dir = realpath($dir);
$gitDir = $dir . '/.git';
if (is_dir($gitDir)) {
$this->dir = $gitDir;
return $gitDir;
}
// Stop recursion to parent on root directory
if ($dir == $currentDir) {
break;
}
}
return NULL;
}

private function parseHead()
{
if (!$this->read) {
$scriptPath = $_SERVER['SCRIPT_FILENAME'];
$dir = realpath(dirname($scriptPath));
while ($dir !== false && !is_dir($dir . '/.git')) {
flush();
$currentDir = $dir;
$dir .= '/..';
$dir = realpath($dir);

// Stop recursion to parent on root directory
if ($dir == $currentDir) {
break;
}
}
$dir = $this->findGitDir();

$head = $dir . '/.git/HEAD';
$head = $dir . '/HEAD';
if ($dir && is_readable($head)) {
$branch = file_get_contents($head);
if (strpos($branch, 'ref:') === 0) {
$parts = explode('/', $branch);
$parts = explode('/', $branch, 3);
$this->branch = $parts[2];

$commitFile = $dir . '/.git/' . trim(substr($branch, 5, strlen($branch)));
$commitFile = $dir . '/' . trim(substr($branch, 5, strlen($branch)));
if (is_readable($commitFile)) {
$this->commit = file_get_contents($commitFile);
}
Expand Down
50 changes: 50 additions & 0 deletions src/templates/GitVersionPanel.panel.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace JanDrabek\Tracy;

?>

<style class="tracy-debug">

#tracy-debug .nette-ContainerPanel .tracy-inner {
width: 700px;
}

#tracy-debug .nette-ContainerPanel table {
width: 100%;
white-space: nowrap;
}

</style>

<div class="nette-ContainerPanel">
<h1>Git Version</h1>

<h2>Actual state</h2>
<table>
<tr>
<th>Revision</th>
<td><?php echo $this->getCurrentCommitHash(); ?></td>
</tr>
</table>

<h2>Last logs</h2>
<div class="tracy-inner">
<table>
<tr>
<th>From</th>
<th>To</th>
<th>User &amp; time</th>
<th>Action</th>
</tr>
<?php foreach ($this->getLogTail() as $row) { ?>
<tr>
<td><?php echo $row['from']; ?></td>
<td><?php echo $row['to']; ?></td>
<td><?php echo $row['user']; ?></td>
<td><?php echo $row['action']; ?></td>
</tr>
<?php } ?>
</table>
</div>
</div>
13 changes: 13 additions & 0 deletions src/templates/GitVersionPanel.tab.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace JanDrabek\Tracy;

?>
<span title="Git Version">
<svg viewBox="0 0 2048 2048">
<g transform="matrix(4.6701586,0,0,4.6701586,6.6953342e-6,0)">
<path style="fill:#00c200;fill-opacity:1;stroke:none" d="M 349.459,52.534 C 338.796,41.877 325.854,36.547 310.625,36.547 c -15.222,0 -28.165,5.327 -38.825,15.987 -10.656,10.657 -15.984,23.598 -15.984,38.828 0,9.897 2.467,19.081 7.416,27.55 4.948,8.47 11.604,15.086 19.985,19.842 0,9.897 -0.805,18.608 -2.42,26.125 -1.622,7.517 -4.284,14.128 -7.994,19.842 -3.72,5.711 -7.566,10.561 -11.566,14.56 -4.001,3.999 -9.616,7.755 -16.848,11.278 -7.231,3.521 -13.945,6.468 -20.129,8.851 -6.184,2.375 -14.514,5.182 -24.982,8.419 -19.036,5.903 -33.689,11.323 -43.968,16.275 V 102.206 c 8.375,-4.755 15.037,-11.37 19.985,-19.84 4.947,-8.47 7.421,-17.655 7.421,-27.552 0,-15.225 -5.327,-28.169 -15.987,-38.826 C 156.073,5.332 143.132,0 127.903,0 112.673,0 99.732,5.328 89.072,15.988 78.416,26.645 73.085,39.589 73.085,54.814 c 0,9.897 2.474,19.082 7.421,27.552 4.948,8.47 11.609,15.085 19.985,19.84 v 234.117 c -8.376,4.753 -15.037,11.375 -19.985,19.842 -4.947,8.473 -7.421,17.658 -7.421,27.552 0,15.225 5.327,28.168 15.987,38.824 10.66,10.656 23.604,15.988 38.831,15.988 15.226,0 28.17,-5.332 38.826,-15.988 10.657,-10.656 15.987,-23.6 15.987,-38.824 0,-9.894 -2.474,-19.079 -7.421,-27.552 -4.949,-8.467 -11.61,-15.089 -19.985,-19.842 V 328.9 c 0,-13.131 3.949,-22.645 11.847,-28.544 7.898,-5.907 24.029,-12.662 48.395,-20.273 25.699,-8.186 45.021,-15.899 57.963,-23.134 42.633,-24.167 64.142,-63.568 64.521,-118.196 8.381,-4.755 15.037,-11.372 19.985,-19.842 4.945,-8.47 7.423,-17.653 7.423,-27.55 0.003,-15.226 -5.328,-28.167 -15.985,-38.827 z M 147.321,403.138 c -5.332,5.331 -11.803,7.994 -19.414,7.994 -7.616,0 -14.087,-2.663 -19.417,-7.994 -5.327,-5.325 -7.994,-11.8 -7.994,-19.411 0,-7.617 2.664,-14.085 7.994,-19.417 5.33,-5.328 11.801,-7.994 19.417,-7.994 7.611,0 14.083,2.669 19.414,7.994 5.33,5.332 7.993,11.8 7.993,19.417 -0.001,7.611 -2.663,14.085 -7.993,19.411 z m 0,-328.906 c -5.332,5.33 -11.803,7.994 -19.414,7.994 -7.616,0 -14.087,-2.664 -19.417,-7.994 -5.327,-5.33 -7.994,-11.798 -7.994,-19.414 0,-7.614 2.664,-14.087 7.994,-19.412 5.33,-5.329 11.801,-7.994 19.417,-7.994 7.611,0 14.083,2.666 19.414,7.994 5.33,5.325 7.993,11.798 7.993,19.412 -0.001,7.616 -2.663,14.087 -7.993,19.414 z m 182.721,36.547 c -5.328,5.327 -11.796,7.993 -19.41,7.993 -7.618,0 -14.09,-2.666 -19.414,-7.993 -5.328,-5.327 -7.994,-11.799 -7.994,-19.414 0,-7.614 2.666,-14.083 7.994,-19.414 5.328,-5.331 11.796,-7.993 19.414,-7.993 7.614,0 14.082,2.663 19.41,7.993 5.328,5.326 7.994,11.799 7.994,19.414 0,7.614 -2.662,14.087 -7.994,19.414 z"/>
</g>
</svg>
<span class="tracy-label"><?php echo $this->getCurrentBranchName(); ?></span>
</span>

0 comments on commit 3617ba9

Please sign in to comment.