Skip to content

Commit 3617ba9

Browse files
Štěpán ŠkorpilJan Drábek
authored andcommitted
Better look, detailed info (#2)
Refactored for more details - Extracted templates - New layout - Support for named branches (feature/name, hotfix/name...) - New SVG icon
1 parent 1cfc4ef commit 3617ba9

File tree

4 files changed

+136
-19
lines changed

4 files changed

+136
-19
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
{
1212
"name": "Jan Drábek",
1313
"homepage": "http://www.jandrabek.cz"
14+
},
15+
{
16+
"name": "Štěpán Škorpil",
17+
"homepage": "http://www.skorpils.cz"
1418
}
1519
],
1620
"require": {

src/GitVersionPanel.php

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,54 @@
88
*
99
* @author Jan Drábek
1010
* @author Vojtěch Vondra
11+
* @author Štěpán Škorpil
1112
*/
1213
class GitVersionPanel implements IBarPanel
1314
{
1415

1516
private $read = false;
1617

18+
private $dir;
19+
1720
private $branch;
1821
private $commit;
1922

2023
public function getPanel()
2124
{
2225
$this->parseHead();
23-
return '<h1>Git Version</h1><p>Revision: ' . $this->getCurrentCommitHash() . '</p>';
26+
ob_start(function () {
27+
});
28+
require __DIR__ . '/templates/GitVersionPanel.panel.phtml';
29+
return ob_get_clean();
30+
}
31+
32+
protected function getLogTail($rowCount = 5)
33+
{
34+
$dir = $this->findGitDir();
35+
$logHead = $dir . '/logs/HEAD';
36+
if (!$dir || !is_readable($logHead)) {
37+
return [];
38+
}
39+
$fp = fopen($logHead, 'r');
40+
fseek($fp, -1, SEEK_END);
41+
$pos = ftell($fp);
42+
$log = "";
43+
$rowCounter = -1;
44+
while ($rowCounter <= $rowCount && $pos >= 0) {
45+
$char = fgetc($fp);
46+
$log = $char . $log;
47+
if ($char == "\n")
48+
$rowCounter++;
49+
fseek($fp, $pos--);
50+
}
51+
$result = [];
52+
foreach (explode("\n", trim($log)) as $row) {
53+
$input = [];
54+
list($row, $input['action']) = explode("\t", $row, 2);
55+
list($input['from'], $input['to'], $input['user']) = explode(" ", $row, 3);
56+
$result[] = $input;
57+
}
58+
return $result;
2459
}
2560

2661
protected function getCurrentBranchName()
@@ -45,35 +80,50 @@ protected function getCurrentCommitHash()
4580

4681
public function getTab()
4782
{
48-
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=" />'
49-
. $this->getCurrentBranchName();
83+
ob_start(function () {
84+
});
85+
require __DIR__ . '/templates/GitVersionPanel.tab.phtml';
86+
return ob_get_clean();
87+
}
88+
89+
private function findGitDir()
90+
{
91+
if ($this->dir)
92+
return $this->dir;
93+
94+
$scriptPath = $_SERVER['SCRIPT_FILENAME'];
95+
$dir = realpath(dirname($scriptPath));
96+
while ($dir !== false) {
97+
flush();
98+
$currentDir = $dir;
99+
$dir .= '/..';
100+
$dir = realpath($dir);
101+
$gitDir = $dir . '/.git';
102+
if (is_dir($gitDir)) {
103+
$this->dir = $gitDir;
104+
return $gitDir;
105+
}
106+
// Stop recursion to parent on root directory
107+
if ($dir == $currentDir) {
108+
break;
109+
}
110+
}
111+
return NULL;
50112
}
51113

52114
private function parseHead()
53115
{
54116
if (!$this->read) {
55-
$scriptPath = $_SERVER['SCRIPT_FILENAME'];
56-
$dir = realpath(dirname($scriptPath));
57-
while ($dir !== false && !is_dir($dir . '/.git')) {
58-
flush();
59-
$currentDir = $dir;
60-
$dir .= '/..';
61-
$dir = realpath($dir);
62-
63-
// Stop recursion to parent on root directory
64-
if ($dir == $currentDir) {
65-
break;
66-
}
67-
}
117+
$dir = $this->findGitDir();
68118

69-
$head = $dir . '/.git/HEAD';
119+
$head = $dir . '/HEAD';
70120
if ($dir && is_readable($head)) {
71121
$branch = file_get_contents($head);
72122
if (strpos($branch, 'ref:') === 0) {
73-
$parts = explode('/', $branch);
123+
$parts = explode('/', $branch, 3);
74124
$this->branch = $parts[2];
75125

76-
$commitFile = $dir . '/.git/' . trim(substr($branch, 5, strlen($branch)));
126+
$commitFile = $dir . '/' . trim(substr($branch, 5, strlen($branch)));
77127
if (is_readable($commitFile)) {
78128
$this->commit = file_get_contents($commitFile);
79129
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace JanDrabek\Tracy;
4+
5+
?>
6+
7+
<style class="tracy-debug">
8+
9+
#tracy-debug .nette-ContainerPanel .tracy-inner {
10+
width: 700px;
11+
}
12+
13+
#tracy-debug .nette-ContainerPanel table {
14+
width: 100%;
15+
white-space: nowrap;
16+
}
17+
18+
</style>
19+
20+
<div class="nette-ContainerPanel">
21+
<h1>Git Version</h1>
22+
23+
<h2>Actual state</h2>
24+
<table>
25+
<tr>
26+
<th>Revision</th>
27+
<td><?php echo $this->getCurrentCommitHash(); ?></td>
28+
</tr>
29+
</table>
30+
31+
<h2>Last logs</h2>
32+
<div class="tracy-inner">
33+
<table>
34+
<tr>
35+
<th>From</th>
36+
<th>To</th>
37+
<th>User &amp; time</th>
38+
<th>Action</th>
39+
</tr>
40+
<?php foreach ($this->getLogTail() as $row) { ?>
41+
<tr>
42+
<td><?php echo $row['from']; ?></td>
43+
<td><?php echo $row['to']; ?></td>
44+
<td><?php echo $row['user']; ?></td>
45+
<td><?php echo $row['action']; ?></td>
46+
</tr>
47+
<?php } ?>
48+
</table>
49+
</div>
50+
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace JanDrabek\Tracy;
4+
5+
?>
6+
<span title="Git Version">
7+
<svg viewBox="0 0 2048 2048">
8+
<g transform="matrix(4.6701586,0,0,4.6701586,6.6953342e-6,0)">
9+
<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"/>
10+
</g>
11+
</svg>
12+
<span class="tracy-label"><?php echo $this->getCurrentBranchName(); ?></span>
13+
</span>

0 commit comments

Comments
 (0)