From 7ce27bb3d52c20801930f1f5dfdc44a327ffef82 Mon Sep 17 00:00:00 2001 From: Jo Michael Date: Sat, 14 Apr 2012 00:02:06 +0200 Subject: [PATCH 1/6] 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 */ From 06001a08e846a040183f5f84128f5cd2de0cc804 Mon Sep 17 00:00:00 2001 From: Jo Michael Date: Sat, 14 Apr 2012 16:12:05 +0200 Subject: [PATCH 2/6] Use separate library file for displaying Git revision, minor fixes --- libraries/Config.class.php | 5 +-- libraries/display_git_revision.lib.php | 56 ++++++++++++++++++++++++++ main.php | 46 +-------------------- 3 files changed, 59 insertions(+), 48 deletions(-) create mode 100644 libraries/display_git_revision.lib.php diff --git a/libraries/Config.class.php b/libraries/Config.class.php index aec951d22e..4460364564 100644 --- a/libraries/Config.class.php +++ b/libraries/Config.class.php @@ -479,7 +479,7 @@ class PMA_Config 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); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); $data = @curl_exec($ch); if ($data === false) { return null; @@ -488,8 +488,7 @@ class PMA_Config $notfound = 'HTTP/1.1 404 Not Found'; if (substr($data, 0, strlen($ok)) === $ok) { return true; - } - elseif (substr($data, 0, strlen($notfound)) === $notfound) { + } elseif (substr($data, 0, strlen($notfound)) === $notfound) { return false; } return null; diff --git a/libraries/display_git_revision.lib.php b/libraries/display_git_revision.lib.php new file mode 100644 index 0000000000..5b7da85fb6 --- /dev/null +++ b/libraries/display_git_revision.lib.php @@ -0,0 +1,56 @@ +get('PMA_VERSION_GIT_COMMITHASH'), 0, 7); + $commit_hash = '' + . $commit_hash . ''; + if ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_ISREMOTECOMMIT')) { + $commit_hash = + '' . $commit_hash . ''; + } + + $branch = $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_BRANCH'); + 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); +} diff --git a/main.php b/main.php index f5a2bddc42..bdcd2f8d72 100644 --- a/main.php +++ b/main.php @@ -235,6 +235,7 @@ PMA_printListItem(__('Version information') . ': ' . PMA_VERSION, 'li_pma_versio // if running in Git repo, show revision and branch $GLOBALS['PMA_Config']->checkGitRevision(); if ($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT')) { + include_once 'libraries/display_git_revision.lib.php'; PMA_printGitRevision(); } @@ -448,51 +449,6 @@ 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 */ From cd3b41fd25c4d991461b15c19a04583688f9bcdf Mon Sep 17 00:00:00 2001 From: Jo Michael Date: Sat, 14 Apr 2012 23:54:53 +0200 Subject: [PATCH 3/6] Suppress error messages when trying to read non-existant .git/refs/heads/master file --- libraries/Config.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Config.class.php b/libraries/Config.class.php index 4460364564..89e75f7e4c 100644 --- a/libraries/Config.class.php +++ b/libraries/Config.class.php @@ -366,12 +366,12 @@ class PMA_Config $ref_head = substr(trim($ref_head), 5); $branch = basename($ref_head); - if (! $hash = file_get_contents($git_folder . '/' . $ref_head)) { + if (! $hash = @file_get_contents($git_folder . '/' . $ref_head)) { return; } $hash = trim($hash); - if (! $commit = file_get_contents( + if (! $commit = @file_get_contents( $git_folder . '/objects/' . substr($hash, 0, 2) . '/' . substr($hash, 2))) { return; From 0a52239900828cb05eaa3aa307fa5948014517c8 Mon Sep 17 00:00:00 2001 From: Jo Michael Date: Sun, 15 Apr 2012 00:22:17 +0200 Subject: [PATCH 4/6] Add support for repos that are not on any branch --- libraries/Config.class.php | 24 ++++++++++++++---------- libraries/display_git_revision.lib.php | 8 ++++++-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/libraries/Config.class.php b/libraries/Config.class.php index 89e75f7e4c..715da087c0 100644 --- a/libraries/Config.class.php +++ b/libraries/Config.class.php @@ -363,13 +363,19 @@ class PMA_Config if (! $ref_head = @file_get_contents($git_folder . '/HEAD')) { return; } - $ref_head = substr(trim($ref_head), 5); - $branch = basename($ref_head); + $branch = false; + // are we on any branch? + if (strstr($ref_head, '/')) { + $ref_head = substr(trim($ref_head), 5); + $branch = basename($ref_head); - if (! $hash = @file_get_contents($git_folder . '/' . $ref_head)) { - return; + if (! $hash = @file_get_contents($git_folder . '/' . $ref_head)) { + return; + } + $hash = trim($hash); + } else { + $hash = trim($ref_head); } - $hash = trim($hash); if (! $commit = @file_get_contents( $git_folder . '/objects/' . substr($hash, 0, 2) @@ -407,8 +413,7 @@ class PMA_Config $is_remote_commit = false; if (isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])) { $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash]; - } - else { + } else { $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/commits/' . $hash; $is_found = $this->checkHTTP($link); switch($is_found) { @@ -428,12 +433,11 @@ class PMA_Config } $is_remote_branch = false; - if ($is_remote_commit) { + if ($is_remote_commit && $branch !== false) { // check if branch exists in Github if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) { $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash]; - } - else { + } else { $link = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/git/trees/' . $branch; $is_found = $this->checkHTTP($link); switch($is_found) { diff --git a/libraries/display_git_revision.lib.php b/libraries/display_git_revision.lib.php index 5b7da85fb6..51812e443e 100644 --- a/libraries/display_git_revision.lib.php +++ b/libraries/display_git_revision.lib.php @@ -35,10 +35,14 @@ function PMA_printGitRevision() . $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_BRANCH')) . '" target="_blank">' . $branch . ''; } + if ($branch !== false) { + $branch = sprintf(__('%s from %s branch'), $commit_hash, $branch); + } else { + $branch = $commit_hash . ' (' . __('no branch') . ')'; + } PMA_printListItem(__('Git revision') . ': ' - . sprintf( - __('%s from %s branch'), $commit_hash, $branch) . ',
' + . $branch . ',
' . sprintf( __('committed on %s by %s'), PMA_localisedDate(strtotime($GLOBALS['PMA_Config']->get('PMA_VERSION_GIT_COMMITTER')['date'])), From d6c28d3923b02e712d6c93c24e54392c66b94155 Mon Sep 17 00:00:00 2001 From: Jo Michael Date: Sun, 15 Apr 2012 01:15:30 +0200 Subject: [PATCH 5/6] Show Git revision asynchronously --- js/functions.js | 19 +++++++++++++++++++ libraries/Config.class.php | 17 +++++++++++++++++ libraries/display_git_revision.lib.php | 11 +++++++++++ main.php | 20 ++++++++++++-------- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/js/functions.js b/js/functions.js index 088cc457d8..4d63780301 100644 --- a/js/functions.js +++ b/js/functions.js @@ -149,6 +149,21 @@ function PMA_current_version() $('#li_pma_version').append(version_information_message); } +/** + * Loads Git revision data from ajax for main.php + */ +function PMA_display_git_revision() +{ + $.get("main.php?token=" + + $("input[type=hidden][name=token]").val() + + "&git_revision=1&ajax_request=true", function (data) { + if (data.error != "undefined" && data.error) { + return; + } + $(data.message).insertAfter('#li_pma_version'); + }); +} + /** * for libraries/display_change_password.lib.php * libraries/user_password.php @@ -3080,6 +3095,10 @@ $(document).ready(function() { $.getScript('http://www.phpmyadmin.net/home_page/version.js', PMA_current_version); } + if (typeof is_git_revision != "undefined") { + setTimeout(PMA_display_git_revision, 10); + } + /** * Slider effect. */ diff --git a/libraries/Config.class.php b/libraries/Config.class.php index 715da087c0..f181558d11 100644 --- a/libraries/Config.class.php +++ b/libraries/Config.class.php @@ -346,6 +346,23 @@ class PMA_Config $this->set('PMA_PHP_STR_VERSION', phpversion()); } + /** + * detects if Git revision + * + * @return boolean + */ + function isGitRevision() + { + // find out if there is a .git folder + $git_folder = '.git'; + if (! @file_exists($git_folder) + || ! @file_exists($git_folder . '/config')) { + return false; + } + $this->set('PMA_VERSION_GIT', 1); + return true; + } + /** * detects Git revision, if running inside repo * diff --git a/libraries/display_git_revision.lib.php b/libraries/display_git_revision.lib.php index 51812e443e..b31ce8a361 100644 --- a/libraries/display_git_revision.lib.php +++ b/libraries/display_git_revision.lib.php @@ -14,6 +14,13 @@ if (! defined('PHPMYADMIN')) { */ function PMA_printGitRevision() { + if (! $GLOBALS['PMA_Config']->get('PMA_VERSION_GIT')) { + PMA_ajaxResponse('', false); + } + + // load revision data from repo + $GLOBALS['PMA_Config']->checkGitRevision(); + // 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 = ' '', 'email' => '', 'date' => ''); $committer = array('name' => '', 'email' => '', 'date' => '');