MediaWiki  REL1_21
GitInfo.php
Go to the documentation of this file.
00001 <?php
00026 class GitInfo {
00027 
00031         protected static $repo = null;
00032 
00036         protected $basedir;
00037 
00041         private static $viewers = false;
00042 
00046         public function __construct( $dir ) {
00047                 $this->basedir = "{$dir}/.git";
00048                 if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
00049                         $GITfile = file_get_contents( $this->basedir );
00050                         if ( strlen( $GITfile ) > 8 && substr( $GITfile, 0, 8 ) === 'gitdir: ' ) {
00051                                 $path = rtrim( substr( $GITfile, 8 ), "\r\n" );
00052                                 $isAbsolute = $path[0] === '/' || substr( $path, 1, 1 ) === ':';
00053                                 $this->basedir = $isAbsolute ? $path : "{$dir}/{$path}";
00054                         }
00055                 }
00056         }
00057 
00062         public static function repo() {
00063                 global $IP;
00064                 if ( is_null( self::$repo ) ) {
00065                         self::$repo = new self( $IP );
00066                 }
00067                 return self::$repo;
00068         }
00069 
00076         public static function isSHA1( $str ) {
00077                 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
00078         }
00079 
00084         public function getHead() {
00085                 $HEADfile = "{$this->basedir}/HEAD";
00086 
00087                 if ( !is_readable( $HEADfile ) ) {
00088                         return false;
00089                 }
00090 
00091                 $HEAD = file_get_contents( $HEADfile );
00092 
00093                 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
00094                         return rtrim( $m[1] );
00095                 } else {
00096                         return rtrim( $HEAD );
00097                 }
00098         }
00099 
00104         public function getHeadSHA1() {
00105                 $HEAD = $this->getHead();
00106 
00107                 // If detached HEAD may be a SHA1
00108                 if ( self::isSHA1( $HEAD ) ) {
00109                         return $HEAD;
00110                 }
00111 
00112                 // If not a SHA1 it may be a ref:
00113                 $REFfile = "{$this->basedir}/{$HEAD}";
00114                 if ( !is_readable( $REFfile ) ) {
00115                         return false;
00116                 }
00117 
00118                 $sha1 = rtrim( file_get_contents( $REFfile ) );
00119 
00120                 return $sha1;
00121         }
00122 
00127         public function getCurrentBranch() {
00128                 $HEAD = $this->getHead();
00129                 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
00130                         return $m[1];
00131                 } else {
00132                         return $HEAD;
00133                 }
00134         }
00135 
00141         public function getHeadViewUrl() {
00142                 $config = "{$this->basedir}/config";
00143                 if ( !is_readable( $config ) ) {
00144                         return false;
00145                 }
00146 
00147                 $configArray = parse_ini_file( $config, true );
00148                 $remote = false;
00149 
00150                 // Use the "origin" remote repo if available or any other repo if not.
00151                 if ( isset( $configArray['remote origin'] ) ) {
00152                         $remote = $configArray['remote origin'];
00153                 } else {
00154                         foreach( $configArray as $sectionName => $sectionConf ) {
00155                                 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
00156                                         $remote = $sectionConf;
00157                                 }
00158                         }
00159                 }
00160 
00161                 if ( $remote === false || !isset( $remote['url'] ) ) {
00162                         return false;
00163                 }
00164 
00165                 $url = $remote['url'];
00166                 if ( substr( $url, -4 ) !== '.git' ) {
00167                         $url .= '.git';
00168                 }
00169                 foreach( self::getViewers() as $repo => $viewer ) {
00170                         $pattern = '#^' . $repo . '$#';
00171                         if ( preg_match( $pattern, $url ) ) {
00172                                 $viewerUrl = preg_replace( $pattern, $viewer, $url );
00173                                 $headSHA1 = $this->getHeadSHA1();
00174                                 $replacements = array(
00175                                         '%h' => substr( $headSHA1, 0, 7 ),
00176                                         '%H' => $headSHA1
00177                                 );
00178                                 return strtr( $viewerUrl, $replacements );
00179                         }
00180                 }
00181                 return false;
00182         }
00183 
00188         public static function headSHA1() {
00189                 return self::repo()->getHeadSHA1();
00190         }
00191 
00196         public static function currentBranch() {
00197                 return self::repo()->getCurrentBranch();
00198         }
00199 
00204         public static function headViewUrl() {
00205                 return self::repo()->getHeadViewUrl();
00206         }
00207 
00212         protected static function getViewers() {
00213                 global $wgGitRepositoryViewers;
00214 
00215                 if( self::$viewers === false ) {
00216                         self::$viewers = $wgGitRepositoryViewers;
00217                         wfRunHooks( 'GitViewers', array( &self::$viewers ) );
00218                 }
00219 
00220                 return self::$viewers;
00221         }
00222 }