addPanel($class);
}
/**
* Renders HTML code for custom tab.
*
* @return string
*/
public function getTab(): string
{
$style = '';
if ($this->getBranchName() === 'master' || $this->getBranchName() === 'staging') {
$style = 'background:#dd4742;color:white;padding:3px 4px 4px';
}
$icon = '';
$label = ''.$this->getBranchName().'';
return $icon.$label;
}
/**
* Renders HTML code for custom panel.
*
* @return string
*/
public function getPanel(): string
{
if ($this->isUnderVersionControl()) {
$title = '
GIT
';
$warning = '';
$cntTable = '';
if ($this->getBranchName() === 'master' || $this->getBranchName() === 'staging') {
$warning = 'You are working in '.$this->getBranchName().' branch
';
}
// commit message
if ($this->getLastCommitMessage() !== null) {
$cntTable .= 'Last commit | '.$this->getLastCommitMessage().' |
';
}
// heads
if ($this->getHeads() !== null) {
$cntTable .= 'Branches | '.$this->getHeads().' |
';
}
// remotes
if ($this->getRemotes() !== null) {
$cntTable .= 'Remotes | '.$this->getRemotes().' |
';
}
// tags
if ($this->getTags() !== null && !empty($this->getTags())) {
$cntTable .= 'Tags | '.$this->getTags().' |
';
}
$content = '';
return $title.$warning.$content;
}
return "";
}
protected function getBranchName(): string
{
$dir = $this->getDirectory();
$head = $dir.'/.git/HEAD';
if ($dir && is_readable($head)) {
$branch = file_get_contents($head);
if (strpos($branch, 'ref:') === 0) {
$parts = explode('/', $branch);
return substr($parts[2], 0, -1);
}
return '('.substr($branch, 0, 7).'…)';
}
return 'not versioned';
}
protected function getLastCommitMessage()
{
$dir = $this->getDirectory();
$fileMessage = $dir.'/.git/COMMIT_EDITMSG';
if ($dir && is_readable($fileMessage)) {
$message = file_get_contents($fileMessage);
return $message;
}
return null;
}
protected function getHeads()
{
$dir = $this->getDirectory();
$files = scandir($dir.'/.git/refs/heads');
$message = '';
if ($dir && is_array($files)) {
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
if ($file === $this->getBranchName()) {
$message .= ''.$file.' ';
} else {
$message .= $file.'
';
}
}
}
return $message;
}
return null;
}
protected function getRemotes()
{
$dir = $this->getDirectory();
try {
$files = scandir($dir.'/.git/refs/remotes');
} catch (\ErrorException $e) {
return null;
}
$message = '';
if ($dir && is_array($files)) {
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$message .= $file.' ';
}
}
return $message;
}
return null;
}
protected function getTags()
{
$dir = $this->getDirectory();
$files = scandir($dir.'/.git/refs/tags');
$message = '';
if ($dir && is_array($files)) {
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$message .= $file.' ';
}
}
return $message;
}
return null;
}
private function getDirectory(): string
{
$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;
}
}
return $dir;
}
private function isUnderVersionControl(): bool
{
$dir = $this->getDirectory();
$head = $dir.'/.git/HEAD';
if ($dir && is_readable($head)) {
return true;
}
return false;
}
}