Merge branch 'feature/show-git-head-for-modules-11664'

resolves #11664
This commit is contained in:
Eric Lippmann 2016-09-09 14:23:39 +02:00
commit eafb689136
3 changed files with 40 additions and 17 deletions

View File

@ -4,6 +4,7 @@
namespace Icinga\Controllers;
use Exception;
use Icinga\Application\Version;
use InvalidArgumentException;
use Icinga\Application\Config;
use Icinga\Application\Icinga;
@ -122,6 +123,7 @@ class ConfigController extends Controller
$this->view->module = $module;
$this->view->tabs = $module->getConfigTabs()->activate('info');
$this->view->moduleGitCommitId = Version::getGitHead($module->getBaseDir());
} else {
$this->view->module = false;
$this->view->tabs = null;

View File

@ -37,7 +37,14 @@
</td>
<tr>
<th><?= $this->escape($this->translate('Version')) ?></th>
<td><?= $this->escape($module->getVersion()) ?></td></tr>
<td><?= $this->escape($module->getVersion()) ?></td>
</tr>
<?php if (isset($moduleGitCommitId) && $moduleGitCommitId !== false): ?>
<tr>
<th><?= $this->escape($this->translate('Git commit')) ?></th>
<td><?= $this->escape($moduleGitCommitId) ?></td>
</tr>
<?php endif ?>
<tr>
<th><?= $this->escape($this->translate('Description')) ?></th>
<td><?= nl2br($this->escape($module->getDescription())) ?></td>

View File

@ -25,24 +25,38 @@ class Version
}
}
$gitDir = Icinga::app()->getBaseDir('.git');
$gitHead = @file_get_contents($gitDir . DIRECTORY_SEPARATOR . 'HEAD');
if (false !== $gitHead) {
$matches = array();
if (@preg_match('/(?<!.)ref:\s+(.+?)$/ms', $gitHead, $matches)) {
$gitCommitID = @file_get_contents($gitDir . DIRECTORY_SEPARATOR . $matches[1]);
} else {
$gitCommitID = $gitHead;
}
if (false !== $gitCommitID) {
$matches = array();
if (@preg_match('/(?<!.)(?P<gitCommitID>[0-9a-f]+)$/ms', $gitCommitID, $matches)) {
return array_merge($version, $matches);
}
}
$gitCommitId = static::getGitHead(Icinga::app()->getBaseDir());
if ($gitCommitId !== false) {
$version['gitCommitID'] = $gitCommitId;
}
return $version;
}
/**
* Get the current commit of the Git repository in the given path
*
* @param string $repo Path to the Git repository
* @param bool $bare Whether the Git repository is bare
*
* @return string|bool False if not available
*/
public static function getGitHead($repo, $bare = false)
{
if (! $bare) {
$repo .= '/.git';
}
$head = @file_get_contents($repo . '/HEAD');
if ($head !== false) {
if (preg_match('/^ref: (.+)/', $head, $matches)) {
return @file_get_contents($repo . '/' . $matches[1]);
}
return $head;
}
return false;
}
}