MediaWiki
REL1_20
|
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 } 00049 00054 public static function repo() { 00055 global $IP; 00056 if ( is_null( self::$repo ) ) { 00057 self::$repo = new self( $IP ); 00058 } 00059 return self::$repo; 00060 } 00061 00068 public static function isSHA1( $str ) { 00069 return !!preg_match( '/^[0-9A-F]{40}$/i', $str ); 00070 } 00071 00076 public function getHead() { 00077 $HEADfile = "{$this->basedir}/HEAD"; 00078 00079 if ( !is_readable( $HEADfile ) ) { 00080 return false; 00081 } 00082 00083 $HEAD = file_get_contents( $HEADfile ); 00084 00085 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) { 00086 return rtrim( $m[1] ); 00087 } else { 00088 return rtrim( $HEAD ); 00089 } 00090 } 00091 00096 public function getHeadSHA1() { 00097 $HEAD = $this->getHead(); 00098 00099 // If detached HEAD may be a SHA1 00100 if ( self::isSHA1( $HEAD ) ) { 00101 return $HEAD; 00102 } 00103 00104 // If not a SHA1 it may be a ref: 00105 $REFfile = "{$this->basedir}{$HEAD}"; 00106 if ( !is_readable( $REFfile ) ) { 00107 return false; 00108 } 00109 00110 $sha1 = rtrim( file_get_contents( $REFfile ) ); 00111 00112 return $sha1; 00113 } 00114 00119 public function getCurrentBranch() { 00120 $HEAD = $this->getHead(); 00121 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) { 00122 return $m[1]; 00123 } else { 00124 return $HEAD; 00125 } 00126 } 00127 00133 public function getHeadViewUrl() { 00134 $config = "{$this->basedir}/config"; 00135 if ( !is_readable( $config ) ) { 00136 return false; 00137 } 00138 00139 $configArray = parse_ini_file( $config, true ); 00140 $remote = false; 00141 00142 // Use the "origin" remote repo if available or any other repo if not. 00143 if ( isset( $configArray['remote origin'] ) ) { 00144 $remote = $configArray['remote origin']; 00145 } else { 00146 foreach( $configArray as $sectionName => $sectionConf ) { 00147 if ( substr( $sectionName, 0, 6 ) == 'remote' ) { 00148 $remote = $sectionConf; 00149 } 00150 } 00151 } 00152 00153 if ( $remote === false || !isset( $remote['url'] ) ) { 00154 return false; 00155 } 00156 00157 $url = $remote['url']; 00158 if ( substr( $url, -4 ) !== '.git' ) { 00159 $url .= '.git'; 00160 } 00161 foreach( self::getViewers() as $repo => $viewer ) { 00162 $pattern = '#^' . $repo . '$#'; 00163 if ( preg_match( $pattern, $url ) ) { 00164 $viewerUrl = preg_replace( $pattern, $viewer, $url ); 00165 $headSHA1 = $this->getHeadSHA1(); 00166 $replacements = array( 00167 '%h' => substr( $headSHA1, 0, 7 ), 00168 '%H' => $headSHA1 00169 ); 00170 return strtr( $viewerUrl, $replacements ); 00171 } 00172 } 00173 return false; 00174 } 00175 00180 public static function headSHA1() { 00181 return self::repo()->getHeadSHA1(); 00182 } 00183 00188 public static function currentBranch() { 00189 return self::repo()->getCurrentBranch(); 00190 } 00191 00196 public static function headViewUrl() { 00197 return self::repo()->getHeadViewUrl(); 00198 } 00199 00204 protected static function getViewers() { 00205 global $wgGitRepositoryViewers; 00206 00207 if( self::$viewers === false ) { 00208 self::$viewers = $wgGitRepositoryViewers; 00209 wfRunHooks( 'GitViewers', array( &self::$viewers ) ); 00210 } 00211 00212 return self::$viewers; 00213 } 00214 }