Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / GitInfo.php
blob6f7f80200b03f82ad76dced9fb602f07a041bfc9
1 <?php
2 /**
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
23 * @file
26 class GitInfo {
28 /**
29 * Singleton for the repo at $IP
31 protected static $repo = null;
33 /**
34 * Location of the .git directory
36 protected $basedir;
38 /**
39 * Map of repo URLs to viewer URLs. Access via static method getViewers().
41 private static $viewers = false;
43 /**
44 * @param string $dir The root directory of the repo where the .git dir can be found
46 public function __construct( $dir ) {
47 $this->basedir = "{$dir}/.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}/{$path}";
58 /**
59 * Return a singleton for the repo at $IP
60 * @return GitInfo
62 public static function repo() {
63 global $IP;
64 if ( is_null( self::$repo ) ) {
65 self::$repo = new self( $IP );
67 return self::$repo;
70 /**
71 * Check if a string looks like a hex encoded SHA1 hash
73 * @param string $str The string to check
74 * @return bool Whether or not the string looks like a SHA1
76 public static function isSHA1( $str ) {
77 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
80 /**
81 * Return the HEAD of the repo (without any opening "ref: ")
82 * @return string The HEAD
84 public function getHead() {
85 $HEADfile = "{$this->basedir}/HEAD";
87 if ( !is_readable( $HEADfile ) ) {
88 return false;
91 $HEAD = file_get_contents( $HEADfile );
93 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
94 return rtrim( $m[1] );
95 } else {
96 return rtrim( $HEAD );
101 * Return the SHA1 for the current HEAD of the repo
102 * @return string A SHA1 or false
104 public function getHeadSHA1() {
105 $HEAD = $this->getHead();
107 // If detached HEAD may be a SHA1
108 if ( self::isSHA1( $HEAD ) ) {
109 return $HEAD;
112 // If not a SHA1 it may be a ref:
113 $REFfile = "{$this->basedir}/{$HEAD}";
114 if ( !is_readable( $REFfile ) ) {
115 return false;
118 $sha1 = rtrim( file_get_contents( $REFfile ) );
120 return $sha1;
124 * Return the name of the current branch, or HEAD if not found
125 * @return string The branch name, HEAD, or false
127 public function getCurrentBranch() {
128 $HEAD = $this->getHead();
129 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
130 return $m[1];
131 } else {
132 return $HEAD;
137 * Get an URL to a web viewer link to the HEAD revision.
139 * @return string|bool string if an URL is available or false otherwise.
141 public function getHeadViewUrl() {
142 $config = "{$this->basedir}/config";
143 if ( !is_readable( $config ) ) {
144 return false;
147 $configArray = parse_ini_file( $config, true );
148 $remote = false;
150 // Use the "origin" remote repo if available or any other repo if not.
151 if ( isset( $configArray['remote origin'] ) ) {
152 $remote = $configArray['remote origin'];
153 } else {
154 foreach( $configArray as $sectionName => $sectionConf ) {
155 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
156 $remote = $sectionConf;
161 if ( $remote === false || !isset( $remote['url'] ) ) {
162 return false;
165 $url = $remote['url'];
166 if ( substr( $url, -4 ) !== '.git' ) {
167 $url .= '.git';
169 foreach( self::getViewers() as $repo => $viewer ) {
170 $pattern = '#^' . $repo . '$#';
171 if ( preg_match( $pattern, $url ) ) {
172 $viewerUrl = preg_replace( $pattern, $viewer, $url );
173 $headSHA1 = $this->getHeadSHA1();
174 $replacements = array(
175 '%h' => substr( $headSHA1, 0, 7 ),
176 '%H' => $headSHA1
178 return strtr( $viewerUrl, $replacements );
181 return false;
185 * @see self::getHeadSHA1
186 * @return string
188 public static function headSHA1() {
189 return self::repo()->getHeadSHA1();
193 * @see self::getCurrentBranch
194 * @return string
196 public static function currentBranch() {
197 return self::repo()->getCurrentBranch();
201 * @see self::getHeadViewUrl()
202 * @return bool|string
204 public static function headViewUrl() {
205 return self::repo()->getHeadViewUrl();
209 * Gets the list of repository viewers
210 * @return array
212 protected static function getViewers() {
213 global $wgGitRepositoryViewers;
215 if( self::$viewers === false ) {
216 self::$viewers = $wgGitRepositoryViewers;
217 wfRunHooks( 'GitViewers', array( &self::$viewers ) );
220 return self::$viewers;