47 $this->basedir =
$dir . DIRECTORY_SEPARATOR .
'.git';
48 if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
49 $GITfile = file_get_contents( $this->basedir );
50 if ( strlen( $GITfile ) > 8 && substr( $GITfile, 0, 8 ) ===
'gitdir: ' ) {
51 $path = rtrim( substr( $GITfile, 8 ),
"\r\n" );
52 $isAbsolute =
$path[0] ===
'/' || substr(
$path, 1, 1 ) ===
':';
53 $this->basedir = $isAbsolute ?
$path :
$dir . DIRECTORY_SEPARATOR .
$path;
62 public static function repo() {
64 if ( is_null( self::$repo ) ) {
65 self::$repo =
new self(
$IP );
76 public static function isSHA1( $str ) {
77 return !!preg_match(
'/^[0-9A-F]{40}$/i', $str );
85 $headFile =
"{$this->basedir}/HEAD";
87 if ( !is_readable( $headFile ) ) {
91 $head = file_get_contents( $headFile );
93 if ( preg_match(
"/ref: (.*)/", $head, $m ) ) {
94 return rtrim( $m[1] );
96 return rtrim( $head );
108 if ( self::isSHA1( $head ) ) {
113 $refFile =
"{$this->basedir}/{$head}";
114 if ( !is_readable( $refFile ) ) {
118 $sha1 = rtrim( file_get_contents( $refFile ) );
132 if ( !is_file( $wgGitBin ) || !is_executable( $wgGitBin ) ) {
136 $environment =
array(
"GIT_DIR" => $this->basedir );
139 $commitDate =
wfShellExec( $cmd, $retc, $environment );
144 return (
int)$commitDate;
154 if ( $head && preg_match(
"#^refs/heads/(.*)$#", $head, $m ) ) {
167 $config =
"{$this->basedir}/config";
168 if ( !is_readable( $config ) ) {
173 $configArray = parse_ini_file( $config,
true );
178 if ( isset( $configArray[
'remote origin'] ) ) {
179 $remote = $configArray[
'remote origin'];
180 } elseif ( is_array( $configArray ) ) {
181 foreach ( $configArray
as $sectionName => $sectionConf ) {
182 if ( substr( $sectionName, 0, 6 ) ==
'remote' ) {
183 $remote = $sectionConf;
188 if ( $remote ===
false || !isset( $remote[
'url'] ) ) {
192 $url = $remote[
'url'];
193 if ( substr( $url, -4 ) !==
'.git' ) {
196 foreach ( self::getViewers()
as $repo => $viewer ) {
197 $pattern =
'#^' .
$repo .
'$#';
198 if ( preg_match( $pattern, $url,
$matches ) ) {
199 $viewerUrl = preg_replace( $pattern, $viewer, $url );
201 $replacements =
array(
202 '%h' => substr( $headSHA1, 0, 7 ),
206 return strtr( $viewerUrl, $replacements );
241 global $wgGitRepositoryViewers;
243 if ( self::$viewers ===
false ) {
244 self::$viewers = $wgGitRepositoryViewers;