(bug 34420) - Special:Version should use git
authorAntoine Musso <hashar@users.mediawiki.org>
Thu, 16 Feb 2012 15:40:32 +0000 (15:40 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Thu, 16 Feb 2012 15:40:32 +0000 (15:40 +0000)
You can test it using git init in our $IP and do a dummy commit with:
  git commit -a RELEASE-NOTES-1.19

Then head to Special:Version and look at the magic version number.

This need a backport in REL1_19 / 1.19wmf1 since WMF is going to use
git "soon" (tm).

RELEASE-NOTES-1.19
includes/specials/SpecialVersion.php

index d9b3cd4..b23bedc 100644 (file)
@@ -122,6 +122,7 @@ production.
 * sha1 xml tag added to XML dump file. 
 * (bug 33646) Badtitle error page now emits a 400 HTTP status.
 * Special:MovePage now has a dropdown menu for namespaces.
+* (bug 34420) Special:Version now shows git HEAD sha1 when available
 
 === Bug fixes in 1.19 ===
 * $wgUploadNavigationUrl should be used for file redlinks if.
index 3c6659e..c5943cd 100644 (file)
@@ -180,36 +180,67 @@ class SpecialVersion extends SpecialPage {
 
        /**
         * Return a wikitext-formatted string of the MediaWiki version with a link to
-        * the SVN revision if available.
+        * the SVN revision or the git SHA1 of head if available.
+        * Git is prefered over Svn
+        * The fallback is just $wgVersion
         *
         * @return mixed
         */
        public static function getVersionLinked() {
-               global $wgVersion, $IP;
+               global $wgVersion;
                wfProfileIn( __METHOD__ );
 
+               if( $gitVersion = self::getVersionLinkedGit() ) {
+                       $v = $gitVersion;
+               } elseif( $svnVersion = self::getVersionLinkedSvn() ) {
+                       $v = $svnVersion;
+               } else {
+                       $v = $wgVersion; // fallback
+               }
+
+               wfProfileOut( __METHOD__ );
+               return $v;
+       }
+
+       /**
+        * @return string wgVersion + a link to subversion revision of svn BASE 
+        */
+       private static function getVersionLinkedSvn() {
+               global $wgVersion, $IP;
+
                $info = self::getSvnInfo( $IP );
+               if( !isset( $info['checkout-rev'] ) ) {
+                       return false;
+               }
 
-               if ( isset( $info['checkout-rev'] ) ) {
-                       $linkText = wfMsg(
-                               'version-svn-revision',
-                               isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
-                               $info['checkout-rev']
-                       );
+               $linkText = wfMsg(
+                       'version-svn-revision',
+                       isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
+                       $info['checkout-rev']
+               );
 
-                       if ( isset( $info['viewvc-url'] ) ) {
-                               $version = "$wgVersion [{$info['viewvc-url']} $linkText]";
-                       } else {
-                               $version = "$wgVersion $linkText";
-                       }
+               if ( isset( $info['viewvc-url'] ) ) {
+                       $version = "$wgVersion [{$info['viewvc-url']} $linkText]";
                } else {
-                       $version = $wgVersion;
+                       $version = "$wgVersion $linkText";
                }
 
-               wfProfileOut( __METHOD__ );
                return $version;
        }
 
+       /**
+        * @return false|string wgVersion + HEAD sha1 stripped to the first 7 chars
+        */
+       private static function getVersionLinkedGit() {
+               global $wgVersion, $IP;
+               if( ! $sha1 = self::getGitHeadSha1( $IP) ) {
+                       return false;
+               }
+               $short_sha1 = substr( $sha1, 0, 7 );
+
+               return "$wgVersion ($short_sha1)";
+       }
+
        /**
         * Returns an array with the base extension types.
         * Type is stored as array key, the message as array value.
@@ -678,6 +709,33 @@ class SpecialVersion extends SpecialPage {
                }
        }
 
+       /**
+        * @param $dir String: directory of the git checkout
+        * @return false|String sha1 of commit HEAD points to
+        */
+       public static function getGitHeadSha1( $dir ) {
+               $BASEDIR  = "{$dir}/.git/";
+               $HEADfile = "{$BASEDIR}/HEAD";
+
+               if( !file_exists( $HEADfile ) ) {
+                       return false;
+               }
+
+               preg_match( "/ref: (.*)/",
+                       file_get_contents( $HEADfile), $m );
+
+               $REFfile = "{$BASEDIR}{$m[1]}";
+               if( !file_exists( $REFfile ) ) {
+                       print "$REFfile doesnot exit?";
+                       return false;
+               }
+
+               $sha1 = chop(file_get_contents( $REFfile ));
+
+               return $sha1;
+       }
+
+
        function showEasterEgg() {
                $rx = $rp = $xe = '';
                $alpha = array("", "kbQW", "\$\n()");