1
0
mirror of https://github.com/phpmyadmin/phpmyadmin.git synced 2026-10-28 07:41:55 +03:00

Show Git revision details in right main.php column (hover SHA1 to read commit message)

This commit is contained in:
Jo Michael
2012-04-14 00:02:06 +02:00
parent e662600415
commit 7ce27bb3d5
2 changed files with 201 additions and 0 deletions

View File

@@ -346,6 +346,155 @@ class PMA_Config
$this->set('PMA_PHP_STR_VERSION', phpversion());
}
/**
* detects Git revision, if running inside repo
*
* @return void
*/
function checkGitRevision()
{
// find out if there is a .git folder
$git_folder = '.git';
if (! @file_exists($git_folder)
|| ! @file_exists($git_folder . '/config')) {
return;
}
if (! $ref_head = @file_get_contents($git_folder . '/HEAD')) {
return;
}
$ref_head = substr(trim($ref_head), 5);
$branch = basename($ref_head);
if (! $hash = file_get_contents($git_folder . '/' . $ref_head)) {
return;
}
$hash = trim($hash);
if (! $commit = file_get_contents(
$git_folder . '/objects/' . substr($hash, 0, 2)
. '/' . substr($hash, 2))) {
return;
}
$commit = explode("\0", gzuncompress($commit), 2);
$commit = explode("\n", $commit[1]);
$author = array('name' => '', 'email' => '', 'date' => '');
$committer = array('name' => '', 'email' => '', 'date' => '');
do {
$dataline = array_shift($commit);
$datalinearr = explode(' ', $dataline, 2);
$linetype = $datalinearr[0];
if (in_array($linetype, array('author', 'committer')))
{
$user = $datalinearr[1];
preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
$user2 = array(
'name' => trim($user[1]),
'email' => trim($user[2]),
'date' => date('Y-m-d H:i:s', $user[3]));
if (isset($user[4]))
{
$user2['date'] .= $user[4];
}
$$linetype = $user2;
}
}
while ($dataline != '');
$message = trim(implode(' ', $commit));
// check if commit exists in Github
$is_remote_commit = false;
if (isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])) {
$is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
}
else {
$link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/' . $hash;
$is_found = $this->checkHTTP($link);
switch($is_found) {
case true:
$is_remote_commit = true;
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
break;
case false:
$is_remote_commit = false;
$_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
break;
case null:
// no remote link for now, but don't cache this as Github is down
$is_remote_commit = false;
break;
}
}
$is_remote_branch = false;
if ($is_remote_commit) {
// check if branch exists in Github
if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
$is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
}
else {
$link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/trees/' . $branch;
$is_found = $this->checkHTTP($link);
switch($is_found) {
case true:
$is_remote_branch = true;
$_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
break;
case false:
$is_remote_branch = false;
$_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
break;
case null:
// no remote link for now, but don't cache this as Github is down
$is_remote_branch = false;
break;
}
}
}
$this->set('PMA_VERSION_GIT', 1);
$this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
$this->set('PMA_VERSION_GIT_BRANCH', $branch);
$this->set('PMA_VERSION_GIT_MESSAGE', $message);
$this->set('PMA_VERSION_GIT_AUTHOR', $author);
$this->set('PMA_VERSION_GIT_COMMITTER', $committer);
$this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
$this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
}
/**
* Checks if given URL is 200 or 404
*/
function checkHTTP($link)
{
if (! function_exists('curl_init')) {
return null;
}
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = @curl_exec($ch);
if ($data === false) {
return null;
}
$ok = 'HTTP/1.1 200 OK';
$notfound = 'HTTP/1.1 404 Not Found';
if (substr($data, 0, strlen($ok)) === $ok) {
return true;
}
elseif (substr($data, 0, strlen($notfound)) === $notfound) {
return false;
}
return null;
}
/**
* loads default values from default source
*

View File

@@ -231,6 +231,13 @@ if ($GLOBALS['cfg']['VersionCheck'] && ! $GLOBALS['PMA_Config']->get('is_https')
$class = 'jsversioncheck';
}
PMA_printListItem(__('Version information') . ': ' . PMA_VERSION, 'li_pma_version', null, null, null, null, $class);
// if running in Git repo, show revision and branch
$GLOBALS['PMA_Config']->checkGitRevision();
if ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT')) {
PMA_printGitRevision();
}
PMA_printListItem(__('Documentation'), 'li_pma_docs', 'Documentation.html', null, '_blank');
PMA_printListItem(__('Wiki'), 'li_pma_wiki', PMA_linkURL('http://wiki.phpmyadmin.net/'), null, '_blank');
@@ -441,6 +448,51 @@ function PMA_printListItem($name, $id = null, $url = null, $mysql_help_page = nu
echo '</li>';
}
/**
* Prints details about the current Git commit revision
*/
function PMA_printGitRevision()
{
// if using a remote commit fast-forwarded, link to Github
$commit_hash = substr($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITHASH'), 0, 7);
$commit_hash = '<strong title="'
. htmlspecialchars($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_MESSAGE')) . '">'
. $commit_hash . '</strong>';
$branch = $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_BRANCH');
if ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_ISREMOTECOMMIT')) {
$commit_hash =
'<a href="'
. PMA_linkURL('https://github.com/phpmyadmin/phpmyadmin/commit/'
. $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITHASH'))
. '" target="_blank">' . $commit_hash . '</a>';
}
if ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_ISREMOTEBRANCH')) {
$branch =
'<a href="'
. PMA_linkURL('https://github.com/phpmyadmin/phpmyadmin/tree/'
. $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_BRANCH'))
. '" target="_blank">' . $branch . '</a>';
}
PMA_printListItem(__('Git revision') . ': '
. sprintf(
__('%s from %s branch'), $commit_hash, $branch) . ',<br /> '
. sprintf(
__('committed on %s by %s'),
PMA_localisedDate(strtotime($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER')['date'])),
'<a href="' . PMA_linkURL('mailto:' . $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER')['email']) . '">'
. htmlspecialchars($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER')['name']) . '</a>')
. ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_AUTHOR') != $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER')
? ', <br />'
. sprintf(
__('authored on %s by %s'),
PMA_localisedDate(strtotime($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_AUTHOR')['date'])),
'<a href="' . PMA_linkURL('mailto:' . $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_AUTHOR')['email']) . '">'
. htmlspecialchars($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_AUTHOR')['name']) . '</a>')
: ''),
'li_pma_version_git', null, null, null);
}
/**
* Displays the footer
*/