* @param string $dir The root directory of the repo where the .git dir can be found
*/
public function __construct( $dir ) {
- $this->basedir = "{$dir}/.git";
+ $this->basedir = $dir . DIRECTORY_SEPARATOR . '.git';
if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
$GITfile = file_get_contents( $this->basedir );
if ( strlen( $GITfile ) > 8 && substr( $GITfile, 0, 8 ) === 'gitdir: ' ) {
$path = rtrim( substr( $GITfile, 8 ), "\r\n" );
$isAbsolute = $path[0] === '/' || substr( $path, 1, 1 ) === ':';
- $this->basedir = $isAbsolute ? $path : "{$dir}/{$path}";
+ $this->basedir = $isAbsolute ? $path : $dir . DIRECTORY_SEPARATOR . $path;
}
}
}
* @return string The HEAD
*/
public function getHead() {
- $HEADfile = "{$this->basedir}/HEAD";
+ $headFile = "{$this->basedir}/HEAD";
- if ( !is_readable( $HEADfile ) ) {
+ if ( !is_readable( $headFile ) ) {
return false;
}
- $HEAD = file_get_contents( $HEADfile );
+ $head = file_get_contents( $headFile );
- if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
+ if ( preg_match( "/ref: (.*)/", $head, $m ) ) {
return rtrim( $m[1] );
} else {
- return rtrim( $HEAD );
+ return rtrim( $head );
}
}
* @return string A SHA1 or false
*/
public function getHeadSHA1() {
- $HEAD = $this->getHead();
+ $head = $this->getHead();
// If detached HEAD may be a SHA1
- if ( self::isSHA1( $HEAD ) ) {
- return $HEAD;
+ if ( self::isSHA1( $head ) ) {
+ return $head;
}
// If not a SHA1 it may be a ref:
- $REFfile = "{$this->basedir}/{$HEAD}";
- if ( !is_readable( $REFfile ) ) {
+ $refFile = "{$this->basedir}/{$head}";
+ if ( !is_readable( $refFile ) ) {
return false;
}
- $sha1 = rtrim( file_get_contents( $REFfile ) );
+ $sha1 = rtrim( file_get_contents( $refFile ) );
return $sha1;
}
} else {
return (int)$commitDate;
}
-
- }
+ }
/**
* Return the name of the current branch, or HEAD if not found
* @return string The branch name, HEAD, or false
*/
public function getCurrentBranch() {
- $HEAD = $this->getHead();
- if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
+ $head = $this->getHead();
+ if ( $head && preg_match( "#^refs/heads/(.*)$#", $head, $m ) ) {
return $m[1];
} else {
- return $HEAD;
+ return $head;
}
}
/**
* Get an URL to a web viewer link to the HEAD revision.
*
- * @return string|bool string if an URL is available or false otherwise.
+ * @return string|bool string if a URL is available or false otherwise.
*/
public function getHeadViewUrl() {
$config = "{$this->basedir}/config";
return false;
}
+ wfSuppressWarnings();
$configArray = parse_ini_file( $config, true );
+ wfRestoreWarnings();
$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 ) {
+ } elseif ( is_array( $configArray ) ) {
+ foreach ( $configArray as $sectionName => $sectionConf ) {
if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
$remote = $sectionConf;
}
if ( substr( $url, -4 ) !== '.git' ) {
$url .= '.git';
}
- foreach( self::getViewers() as $repo => $viewer ) {
+ foreach ( self::getViewers() as $repo => $viewer ) {
$pattern = '#^' . $repo . '$#';
- if ( preg_match( $pattern, $url ) ) {
+ if ( preg_match( $pattern, $url, $matches ) ) {
$viewerUrl = preg_replace( $pattern, $viewer, $url );
$headSHA1 = $this->getHeadSHA1();
$replacements = array(
'%h' => substr( $headSHA1, 0, 7 ),
- '%H' => $headSHA1
+ '%H' => $headSHA1,
+ '%r' => urlencode( $matches[1] ),
);
return strtr( $viewerUrl, $replacements );
}
protected static function getViewers() {
global $wgGitRepositoryViewers;
- if( self::$viewers === false ) {
+ if ( self::$viewers === false ) {
self::$viewers = $wgGitRepositoryViewers;
wfRunHooks( 'GitViewers', array( &self::$viewers ) );
}