Convert GitInfo to the new shell framework
[lhc/web/wiklou.git] / includes / GitInfo.php
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.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 use MediaWiki\Shell\Shell;
27
28 class GitInfo {
29
30 /**
31 * Singleton for the repo at $IP
32 */
33 protected static $repo = null;
34
35 /**
36 * Location of the .git directory
37 */
38 protected $basedir;
39
40 /**
41 * Path to JSON cache file for pre-computed git information.
42 */
43 protected $cacheFile;
44
45 /**
46 * Cached git information.
47 */
48 protected $cache = [];
49
50 /**
51 * @var array|false Map of repo URLs to viewer URLs. Access via static method getViewers().
52 */
53 private static $viewers = false;
54
55 /**
56 * @param string $repoDir The root directory of the repo where .git can be found
57 * @param bool $usePrecomputed Use precomputed information if available
58 * @see precomputeValues
59 */
60 public function __construct( $repoDir, $usePrecomputed = true ) {
61 $this->cacheFile = self::getCacheFilePath( $repoDir );
62 wfDebugLog( 'gitinfo',
63 "Computed cacheFile={$this->cacheFile} for {$repoDir}"
64 );
65 if ( $usePrecomputed &&
66 $this->cacheFile !== null &&
67 is_readable( $this->cacheFile )
68 ) {
69 $this->cache = FormatJson::decode(
70 file_get_contents( $this->cacheFile ),
71 true
72 );
73 wfDebugLog( 'gitinfo', "Loaded git data from cache for {$repoDir}" );
74 }
75
76 if ( !$this->cacheIsComplete() ) {
77 wfDebugLog( 'gitinfo', "Cache incomplete for {$repoDir}" );
78 $this->basedir = $repoDir . DIRECTORY_SEPARATOR . '.git';
79 if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
80 $GITfile = file_get_contents( $this->basedir );
81 if ( strlen( $GITfile ) > 8 &&
82 substr( $GITfile, 0, 8 ) === 'gitdir: '
83 ) {
84 $path = rtrim( substr( $GITfile, 8 ), "\r\n" );
85 if ( $path[0] === '/' || substr( $path, 1, 1 ) === ':' ) {
86 // Path from GITfile is absolute
87 $this->basedir = $path;
88 } else {
89 $this->basedir = $repoDir . DIRECTORY_SEPARATOR . $path;
90 }
91 }
92 }
93 }
94 }
95
96 /**
97 * Compute the path to the cache file for a given directory.
98 *
99 * @param string $repoDir The root directory of the repo where .git can be found
100 * @return string Path to GitInfo cache file in $wgGitInfoCacheDirectory or
101 * fallback in the extension directory itself
102 * @since 1.24
103 */
104 protected static function getCacheFilePath( $repoDir ) {
105 global $IP, $wgGitInfoCacheDirectory;
106
107 if ( $wgGitInfoCacheDirectory ) {
108 // Convert both $IP and $repoDir to canonical paths to protect against
109 // $IP having changed between the settings files and runtime.
110 $realIP = realpath( $IP );
111 $repoName = realpath( $repoDir );
112 if ( $repoName === false ) {
113 // Unit tests use fake path names
114 $repoName = $repoDir;
115 }
116 if ( strpos( $repoName, $realIP ) === 0 ) {
117 // Strip $IP from path
118 $repoName = substr( $repoName, strlen( $realIP ) );
119 }
120 // Transform path to git repo to something we can safely embed in
121 // a filename
122 $repoName = strtr( $repoName, DIRECTORY_SEPARATOR, '-' );
123 $fileName = 'info' . $repoName . '.json';
124 $cachePath = "{$wgGitInfoCacheDirectory}/{$fileName}";
125 if ( is_readable( $cachePath ) ) {
126 return $cachePath;
127 }
128 }
129
130 return "$repoDir/gitinfo.json";
131 }
132
133 /**
134 * Get the singleton for the repo at $IP
135 *
136 * @return GitInfo
137 */
138 public static function repo() {
139 if ( is_null( self::$repo ) ) {
140 global $IP;
141 self::$repo = new self( $IP );
142 }
143 return self::$repo;
144 }
145
146 /**
147 * Check if a string looks like a hex encoded SHA1 hash
148 *
149 * @param string $str The string to check
150 * @return bool Whether or not the string looks like a SHA1
151 */
152 public static function isSHA1( $str ) {
153 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
154 }
155
156 /**
157 * Get the HEAD of the repo (without any opening "ref: ")
158 *
159 * @return string|bool The HEAD (git reference or SHA1) or false
160 */
161 public function getHead() {
162 if ( !isset( $this->cache['head'] ) ) {
163 $headFile = "{$this->basedir}/HEAD";
164 $head = false;
165
166 if ( is_readable( $headFile ) ) {
167 $head = file_get_contents( $headFile );
168
169 if ( preg_match( "/ref: (.*)/", $head, $m ) ) {
170 $head = rtrim( $m[1] );
171 } else {
172 $head = rtrim( $head );
173 }
174 }
175 $this->cache['head'] = $head;
176 }
177 return $this->cache['head'];
178 }
179
180 /**
181 * Get the SHA1 for the current HEAD of the repo
182 *
183 * @return string|bool A SHA1 or false
184 */
185 public function getHeadSHA1() {
186 if ( !isset( $this->cache['headSHA1'] ) ) {
187 $head = $this->getHead();
188 $sha1 = false;
189
190 // If detached HEAD may be a SHA1
191 if ( self::isSHA1( $head ) ) {
192 $sha1 = $head;
193 } else {
194 // If not a SHA1 it may be a ref:
195 $refFile = "{$this->basedir}/{$head}";
196 $packedRefs = "{$this->basedir}/packed-refs";
197 $headRegex = preg_quote( $head, '/' );
198 if ( is_readable( $refFile ) ) {
199 $sha1 = rtrim( file_get_contents( $refFile ) );
200 } elseif ( is_readable( $packedRefs ) &&
201 preg_match( "/^([0-9A-Fa-f]{40}) $headRegex$/m", file_get_contents( $packedRefs ), $matches )
202 ) {
203 $sha1 = $matches[1];
204 }
205 }
206 $this->cache['headSHA1'] = $sha1;
207 }
208 return $this->cache['headSHA1'];
209 }
210
211 /**
212 * Get the commit date of HEAD entry of the git code repository
213 *
214 * @since 1.22
215 * @return int|bool Commit date (UNIX timestamp) or false
216 */
217 public function getHeadCommitDate() {
218 global $wgGitBin;
219
220 if ( !isset( $this->cache['headCommitDate'] ) ) {
221 $date = false;
222 if ( is_file( $wgGitBin ) &&
223 is_executable( $wgGitBin ) &&
224 $this->getHead() !== false
225 ) {
226 $cmd = [
227 $wgGitBin,
228 'show',
229 '-s',
230 '--format=format:%ct',
231 'HEAD',
232 ];
233 $result = Shell::command( $cmd )
234 ->environment( [ 'GIT_DIR' => $this->basedir ] )
235 ->execute();
236
237 if ( $result->getExitCode() === 0 ) {
238 $date = (int)$result->getStdout();
239 }
240 }
241 $this->cache['headCommitDate'] = $date;
242 }
243 return $this->cache['headCommitDate'];
244 }
245
246 /**
247 * Get the name of the current branch, or HEAD if not found
248 *
249 * @return string|bool The branch name, HEAD, or false
250 */
251 public function getCurrentBranch() {
252 if ( !isset( $this->cache['branch'] ) ) {
253 $branch = $this->getHead();
254 if ( $branch &&
255 preg_match( "#^refs/heads/(.*)$#", $branch, $m )
256 ) {
257 $branch = $m[1];
258 }
259 $this->cache['branch'] = $branch;
260 }
261 return $this->cache['branch'];
262 }
263
264 /**
265 * Get an URL to a web viewer link to the HEAD revision.
266 *
267 * @return string|bool String if a URL is available or false otherwise
268 */
269 public function getHeadViewUrl() {
270 $url = $this->getRemoteUrl();
271 if ( $url === false ) {
272 return false;
273 }
274 foreach ( self::getViewers() as $repo => $viewer ) {
275 $pattern = '#^' . $repo . '$#';
276 if ( preg_match( $pattern, $url, $matches ) ) {
277 $viewerUrl = preg_replace( $pattern, $viewer, $url );
278 $headSHA1 = $this->getHeadSHA1();
279 $replacements = [
280 '%h' => substr( $headSHA1, 0, 7 ),
281 '%H' => $headSHA1,
282 '%r' => urlencode( $matches[1] ),
283 '%R' => $matches[1],
284 ];
285 return strtr( $viewerUrl, $replacements );
286 }
287 }
288 return false;
289 }
290
291 /**
292 * Get the URL of the remote origin.
293 * @return string|bool String if a URL is available or false otherwise.
294 */
295 protected function getRemoteUrl() {
296 if ( !isset( $this->cache['remoteURL'] ) ) {
297 $config = "{$this->basedir}/config";
298 $url = false;
299 if ( is_readable( $config ) ) {
300 MediaWiki\suppressWarnings();
301 $configArray = parse_ini_file( $config, true );
302 MediaWiki\restoreWarnings();
303 $remote = false;
304
305 // Use the "origin" remote repo if available or any other repo if not.
306 if ( isset( $configArray['remote origin'] ) ) {
307 $remote = $configArray['remote origin'];
308 } elseif ( is_array( $configArray ) ) {
309 foreach ( $configArray as $sectionName => $sectionConf ) {
310 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
311 $remote = $sectionConf;
312 }
313 }
314 }
315
316 if ( $remote !== false && isset( $remote['url'] ) ) {
317 $url = $remote['url'];
318 }
319 }
320 $this->cache['remoteURL'] = $url;
321 }
322 return $this->cache['remoteURL'];
323 }
324
325 /**
326 * Check to see if the current cache is fully populated.
327 *
328 * Note: This method is public only to make unit testing easier. There's
329 * really no strong reason that anything other than a test should want to
330 * call this method.
331 *
332 * @return bool True if all expected cache keys exist, false otherwise
333 */
334 public function cacheIsComplete() {
335 return isset( $this->cache['head'] ) &&
336 isset( $this->cache['headSHA1'] ) &&
337 isset( $this->cache['headCommitDate'] ) &&
338 isset( $this->cache['branch'] ) &&
339 isset( $this->cache['remoteURL'] );
340 }
341
342 /**
343 * Precompute and cache git information.
344 *
345 * Creates a JSON file in the cache directory associated with this
346 * GitInfo instance. This cache file will be used by subsequent GitInfo objects referencing
347 * the same directory to avoid needing to examine the .git directory again.
348 *
349 * @since 1.24
350 */
351 public function precomputeValues() {
352 if ( $this->cacheFile !== null ) {
353 // Try to completely populate the cache
354 $this->getHead();
355 $this->getHeadSHA1();
356 $this->getHeadCommitDate();
357 $this->getCurrentBranch();
358 $this->getRemoteUrl();
359
360 if ( !$this->cacheIsComplete() ) {
361 wfDebugLog( 'gitinfo',
362 "Failed to compute GitInfo for \"{$this->basedir}\""
363 );
364 return;
365 }
366
367 $cacheDir = dirname( $this->cacheFile );
368 if ( !file_exists( $cacheDir ) &&
369 !wfMkdirParents( $cacheDir, null, __METHOD__ )
370 ) {
371 throw new MWException( "Unable to create GitInfo cache \"{$cacheDir}\"" );
372 }
373
374 file_put_contents( $this->cacheFile, FormatJson::encode( $this->cache ) );
375 }
376 }
377
378 /**
379 * @see self::getHeadSHA1
380 * @return string
381 */
382 public static function headSHA1() {
383 return self::repo()->getHeadSHA1();
384 }
385
386 /**
387 * @see self::getCurrentBranch
388 * @return string
389 */
390 public static function currentBranch() {
391 return self::repo()->getCurrentBranch();
392 }
393
394 /**
395 * @see self::getHeadViewUrl()
396 * @return bool|string
397 */
398 public static function headViewUrl() {
399 return self::repo()->getHeadViewUrl();
400 }
401
402 /**
403 * Gets the list of repository viewers
404 * @return array
405 */
406 protected static function getViewers() {
407 global $wgGitRepositoryViewers;
408
409 if ( self::$viewers === false ) {
410 self::$viewers = $wgGitRepositoryViewers;
411 Hooks::run( 'GitViewers', [ &self::$viewers ] );
412 }
413
414 return self::$viewers;
415 }
416 }