From 7ce27bb3d52c20801930f1f5dfdc44a327ffef82 Mon Sep 17 00:00:00 2001 From: Jo Michael Date: Sat, 14 Apr 2012 00:02:06 +0200 Subject: [PATCH] Show Git revision details in right main.php column (hover SHA1 to read commit message) --- libraries/Config.class.php | 149 +++++++++++++++++++++++++++++++++++++ main.php | 52 +++++++++++++ 2 files changed, 201 insertions(+) diff --git a/libraries/Config.class.php b/libraries/Config.class.php index 8dd39d188c..aec951d22e 100644 --- a/libraries/Config.class.php +++ b/libraries/Config.class.php @@ -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 * diff --git a/main.php b/main.php index f4babfe139..f5a2bddc42 100644 --- a/main.php +++ b/main.php @@ -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 ''; } +/** +* 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 = '' + . $commit_hash . ''; + $branch = $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_BRANCH'); + if ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_ISREMOTECOMMIT')) { + $commit_hash = + '' . $commit_hash . ''; + } + if ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_ISREMOTEBRANCH')) { + $branch = + '' . $branch . ''; + } + + PMA_printListItem(__('Git revision') . ': ' + . sprintf( + __('%s from %s branch'), $commit_hash, $branch) . ',
' + . sprintf( + __('committed on %s by %s'), + PMA_localisedDate(strtotime($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER')['date'])), + '' + . htmlspecialchars($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER')['name']) . '') + . ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_AUTHOR') != $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER') + ? ',
' + . sprintf( + __('authored on %s by %s'), + PMA_localisedDate(strtotime($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_AUTHOR')['date'])), + '' + . htmlspecialchars($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_AUTHOR')['name']) . '') + : ''), + 'li_pma_version_git', null, null, null); +} + /** * Displays the footer */