3 * A class to help return information about a git repo MediaWiki may be inside
4 * This is used by Special:Version and is also useful for the LocalSettings.php
5 * of anyone working on large branches in git to setup config that show up only
6 * when specific branches are currently checked out.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
29 * Singleton for the repo at $IP
31 protected static $repo = null;
34 * Location of the .git directory
39 * Map of repo URLs to viewer URLs. Access via static method getViewers().
41 private static $viewers = false;
44 * @param $dir string The root directory of the repo where the .git dir can be found
46 public function __construct( $dir ) {
47 $this->basedir
= "{$dir}/.git/";
51 * Return a singleton for the repo at $IP
54 public static function repo() {
56 if ( is_null( self
::$repo ) ) {
57 self
::$repo = new self( $IP );
63 * Check if a string looks like a hex encoded SHA1 hash
65 * @param $str string The string to check
66 * @return bool Whether or not the string looks like a SHA1
68 public static function isSHA1( $str ) {
69 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
73 * Return the HEAD of the repo (without any opening "ref: ")
74 * @return string The HEAD
76 public function getHead() {
77 $HEADfile = "{$this->basedir}/HEAD";
79 if ( !is_readable( $HEADfile ) ) {
83 $HEAD = file_get_contents( $HEADfile );
85 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
86 return rtrim( $m[1] );
88 return rtrim( $HEAD );
93 * Return the SHA1 for the current HEAD of the repo
94 * @return string A SHA1 or false
96 public function getHeadSHA1() {
97 $HEAD = $this->getHead();
99 // If detached HEAD may be a SHA1
100 if ( self
::isSHA1( $HEAD ) ) {
104 // If not a SHA1 it may be a ref:
105 $REFfile = "{$this->basedir}{$HEAD}";
106 if ( !is_readable( $REFfile ) ) {
110 $sha1 = rtrim( file_get_contents( $REFfile ) );
116 * Return the name of the current branch, or HEAD if not found
117 * @return string The branch name, HEAD, or false
119 public function getCurrentBranch() {
120 $HEAD = $this->getHead();
121 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
129 * Get an URL to a web viewer link to the HEAD revision.
131 * @return string|bool string if an URL is available or false otherwise.
133 public function getHeadViewUrl() {
134 $config = "{$this->basedir}/config";
135 if ( !is_readable( $config ) ) {
139 $configArray = parse_ini_file( $config, true );
142 // Use the "origin" remote repo if available or any other repo if not.
143 if ( isset( $configArray['remote origin'] ) ) {
144 $remote = $configArray['remote origin'];
146 foreach( $configArray as $sectionName => $sectionConf ) {
147 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
148 $remote = $sectionConf;
153 if ( $remote === false ||
!isset( $remote['url'] ) ) {
157 $url = $remote['url'];
158 if ( substr( $url, -4 ) !== '.git' ) {
161 foreach( self
::getViewers() as $repo => $viewer ) {
162 $pattern = '#^' . $repo . '$#';
163 if ( preg_match( $pattern, $url ) ) {
164 $viewerUrl = preg_replace( $pattern, $viewer, $url );
165 $headSHA1 = $this->getHeadSHA1();
166 $replacements = array(
167 '%h' => substr( $headSHA1, 0, 7 ),
170 return strtr( $viewerUrl, $replacements );
177 * @see self::getHeadSHA1
180 public static function headSHA1() {
181 return self
::repo()->getHeadSHA1();
185 * @see self::getCurrentBranch
188 public static function currentBranch() {
189 return self
::repo()->getCurrentBranch();
193 * @see self::getHeadViewUrl()
194 * @return bool|string
196 public static function headViewUrl() {
197 return self
::repo()->getHeadViewUrl();
201 * Gets the list of repository viewers
204 protected static function getViewers() {
205 global $wgGitRepositoryViewers;
207 if( self
::$viewers === false ) {
208 self
::$viewers = $wgGitRepositoryViewers;
209 wfRunHooks( 'GitViewers', array( &self
::$viewers ) );
212 return self
::$viewers;