(bug 35728) Git revisions are now linked on Special:Version
authorAlexandre Emsenhuber <ialex.wiki@gmail.com>
Thu, 5 Apr 2012 16:09:59 +0000 (18:09 +0200)
committerAlexandre Emsenhuber <ialex.wiki@gmail.com>
Thu, 5 Apr 2012 16:22:48 +0000 (18:22 +0200)
Change-Id: I5b02aa914916f64492c85ce6dcc3272b6406551a

RELEASE-NOTES-1.20
includes/GitInfo.php
includes/specials/SpecialVersion.php

index 7dd6b88..3f6a79f 100644 (file)
@@ -33,6 +33,7 @@ production.
 * (bug 17615) nosummary option should be reassigned on preview/captcha.
 * (bug 34355) add a variable and parser function for the namespace number.
 * (bug 35649) Special:Version now shows hashes of extensions checked out from git.
+* (bug 35728) Git revisions are now linked on Special:Version.
 
 === Bug fixes in 1.20 ===
 * (bug 30245) Use the correct way to construct a log page title.
index 618ae94..82c01f7 100644 (file)
@@ -20,6 +20,19 @@ class GitInfo {
         */
        protected $basedir;
 
+       /**
+        * Map of repo URLs to viewer URLs.
+        * Key is a pattern passed to preg_match() and preg_replace(),
+        * without the delimiters (which are #) and must match the whole URL.
+        * The value is the replacement for the key (it can contain $1, etc.)
+        * %h will be replaced by the short SHA-1 (7 first chars) and %H by the
+        * full SHA-1 of the HEAD revision.
+        */
+       protected $viewers = array(
+               'https://gerrit.wikimedia.org/r/p/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%h',
+               'ssh://(?:[a-z0-9_]+@)?gerrit.wikimedia.org:29418/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%h',
+       );
+
        /**
         * @param $dir The root directory of the repo where the .git dir can be found
         */
@@ -105,6 +118,52 @@ class GitInfo {
                }
        }
 
+       /**
+        * Get an URL to a web viewer link to the HEAD revision.
+        *
+        * @return string|false string if an URL is available or false otherwise.
+        */
+       public function getHeadViewUrl() {
+               $config = "{$this->basedir}/config";
+               if ( !is_readable( $config ) ) {
+                       return false;
+               }
+
+               $configArray = parse_ini_file( $config, true );
+               $remote = false;
+
+               // Use the "origin" remote repo if available or any other repo if not.
+               if ( isset( $configArray['remote origin'] ) ) {
+                       $remote = $configArray['remote origin'];
+               } else {
+                       foreach( $configArray as $sectionName => $sectionConf ) {
+                               if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
+                                       $remote = $sectionConf;
+                               }
+                       }
+               }
+
+               if ( $remote === false || !isset( $remote['url'] ) ) {
+                       return false;
+               }
+
+               $url = $remote['url'];
+               foreach( $this->viewers as $repo => $viewer ) {
+                       $m = array();
+                       $pattern = '#^' . $repo . '$#';
+                       if ( preg_match( $pattern, $url ) ) {
+                               $viewerUrl = preg_replace( $pattern, $viewer, $url );
+                               $headSHA1 = $this->getHeadSHA1();
+                               $replacements = array(
+                                       '%h' => substr( $headSHA1, 0, 7 ),
+                                       '%H' => $headSHA1
+                               );
+                               return strtr( $viewerUrl, $replacements );
+                       }
+               }
+               return false;
+       }
+
        /**
         * @see self::getHeadSHA1
         */
index 76d60c7..5965714 100644 (file)
@@ -237,12 +237,19 @@ class SpecialVersion extends SpecialPage {
         */
        private static function getVersionLinkedGit() {
                global $wgVersion, $IP;
-               if( ! $sha1 = self::getGitHeadSha1( $IP) ) {
+
+               $gitInfo = new GitInfo( $IP );
+               $headSHA1 = $gitInfo->getHeadSHA1();
+               if( !$headSHA1 ) {
                        return false;
                }
-               $short_sha1 = substr( $sha1, 0, 7 );
 
-               return "$wgVersion ($short_sha1)";
+               $shortSHA1 = '(' . substr( $headSHA1, 0, 7 ) . ')';
+               $viewerUrl = $gitInfo->getHeadViewUrl();
+               if ( $viewerUrl !== false ) {
+                       $shortSHA1 = "[$viewerUrl $shortSHA1]";
+               }
+               return "$wgVersion $shortSHA1";
        }
 
        /**
@@ -422,6 +429,10 @@ class SpecialVersion extends SpecialPage {
                        $gitHeadSHA1 = $gitInfo->getHeadSHA1();
                        if ( $gitHeadSHA1 !== false ) {
                                $vcsText = substr( $gitHeadSHA1, 0, 7 );
+                               $gitViewerUrl = $gitInfo->getHeadViewUrl();
+                               if ( $gitViewerUrl !== false ) {
+                                       $vcsText = "[$gitViewerUrl $vcsText]";
+                               }
                        } else {
                                $svnInfo = self::getSvnInfo( dirname( $extension['path'] ) );
                                # Make subversion text/link.