Sync up core repo with Parsoid
[mediawiki.git] / includes / GitInfo.php
blob0e9e5164a06c33191d98c90fd524a304f7927c2e
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 use MediaWiki\Config\ServiceOptions;
27 use MediaWiki\HookContainer\HookRunner;
28 use MediaWiki\Logger\LoggerFactory;
29 use MediaWiki\MainConfigNames;
30 use MediaWiki\MediaWikiServices;
31 use MediaWiki\Shell\Shell;
32 use Psr\Log\LoggerInterface;
33 use Wikimedia\AtEase\AtEase;
35 /**
36 * @newable
37 * @note marked as newable in 1.35 for lack of a better alternative,
38 * but should become a stateless service eventually.
40 class GitInfo {
42 /**
43 * Singleton for the repo at $IP
45 protected static $repo = null;
47 /**
48 * Location of the .git directory
50 protected $basedir;
52 /**
53 * Location of the repository
55 protected $repoDir;
57 /**
58 * Path to JSON cache file for pre-computed git information.
60 protected $cacheFile;
62 /**
63 * Cached git information.
65 protected $cache = [];
67 /**
68 * @var array|false Map of repo URLs to viewer URLs. Access via method getViewers().
70 private static $viewers = false;
72 /** Configuration options needed */
73 private const CONSTRUCTOR_OPTIONS = [
74 MainConfigNames::BaseDirectory,
75 MainConfigNames::CacheDirectory,
76 MainConfigNames::GitBin,
77 MainConfigNames::GitInfoCacheDirectory,
78 MainConfigNames::GitRepositoryViewers,
81 /** @var LoggerInterface */
82 private $logger;
84 /** @var ServiceOptions */
85 private $options;
87 /** @var HookRunner */
88 private $hookRunner;
90 /**
91 * @stable to call
92 * @param string $repoDir The root directory of the repo where .git can be found
93 * @param bool $usePrecomputed Use precomputed information if available
94 * @see precomputeValues
96 public function __construct( $repoDir, $usePrecomputed = true ) {
97 $this->repoDir = $repoDir;
98 $this->options = new ServiceOptions(
99 self::CONSTRUCTOR_OPTIONS,
100 MediaWikiServices::getInstance()->getMainConfig()
102 $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
103 // $this->options must be set before using getCacheFilePath()
104 $this->cacheFile = $this->getCacheFilePath( $repoDir );
105 $this->logger = LoggerFactory::getInstance( 'gitinfo' );
106 $this->logger->debug(
107 "Candidate cacheFile={$this->cacheFile} for {$repoDir}"
109 $this->hookRunner = Hooks::runner();
110 if ( $usePrecomputed &&
111 $this->cacheFile !== null &&
112 is_readable( $this->cacheFile )
114 $this->cache = FormatJson::decode(
115 file_get_contents( $this->cacheFile ),
116 true
118 $this->logger->debug( "Loaded git data from cache for {$repoDir}" );
121 if ( !$this->cacheIsComplete() ) {
122 $this->logger->debug( "Cache incomplete for {$repoDir}" );
123 $this->basedir = $repoDir . DIRECTORY_SEPARATOR . '.git';
124 if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
125 $GITfile = file_get_contents( $this->basedir );
126 if ( strlen( $GITfile ) > 8 &&
127 substr( $GITfile, 0, 8 ) === 'gitdir: '
129 $path = rtrim( substr( $GITfile, 8 ), "\r\n" );
130 if ( $path[0] === '/' || substr( $path, 1, 1 ) === ':' ) {
131 // Path from GITfile is absolute
132 $this->basedir = $path;
133 } else {
134 $this->basedir = $repoDir . DIRECTORY_SEPARATOR . $path;
142 * Compute the path to the cache file for a given directory.
144 * @param string $repoDir The root directory of the repo where .git can be found
145 * @return string Path to GitInfo cache file in $wgGitInfoCacheDirectory or
146 * fallback in the extension directory itself
147 * @since 1.24
149 private function getCacheFilePath( $repoDir ) {
150 $gitInfoCacheDirectory = $this->options->get( MainConfigNames::GitInfoCacheDirectory );
151 if ( $gitInfoCacheDirectory === false ) {
152 $gitInfoCacheDirectory = $this->options->get( MainConfigNames::CacheDirectory ) . '/gitinfo';
154 $baseDir = $this->options->get( MainConfigNames::BaseDirectory );
155 if ( $gitInfoCacheDirectory ) {
156 // Convert both $IP and $repoDir to canonical paths to protect against
157 // $IP having changed between the settings files and runtime.
158 $realIP = realpath( $baseDir );
159 $repoName = realpath( $repoDir );
160 if ( $repoName === false ) {
161 // Unit tests use fake path names
162 $repoName = $repoDir;
164 if ( strpos( $repoName, $realIP ) === 0 ) {
165 // Strip $IP from path
166 $repoName = substr( $repoName, strlen( $realIP ) );
168 // Transform path to git repo to something we can safely embed in
169 // a filename
170 $repoName = strtr( $repoName, DIRECTORY_SEPARATOR, '-' );
171 $fileName = 'info' . $repoName . '.json';
172 $cachePath = "{$gitInfoCacheDirectory}/{$fileName}";
173 if ( is_readable( $cachePath ) ) {
174 return $cachePath;
178 return "$repoDir/gitinfo.json";
182 * Get the singleton for the repo at MW_INSTALL_PATH
184 * @return GitInfo
186 public static function repo() {
187 if ( self::$repo === null ) {
188 self::$repo = new self( MW_INSTALL_PATH );
190 return self::$repo;
194 * Check if a string looks like a hex encoded SHA1 hash
196 * @param string $str The string to check
197 * @return bool Whether or not the string looks like a SHA1
199 public static function isSHA1( $str ) {
200 return (bool)preg_match( '/^[0-9A-F]{40}$/i', $str );
204 * Get the HEAD of the repo (without any opening "ref: ")
206 * @return string|false The HEAD (git reference or SHA1) or false
208 public function getHead() {
209 if ( !isset( $this->cache['head'] ) ) {
210 $headFile = "{$this->basedir}/HEAD";
211 $head = false;
213 if ( is_readable( $headFile ) ) {
214 $head = file_get_contents( $headFile );
216 if ( preg_match( "/ref: (.*)/", $head, $m ) ) {
217 $head = rtrim( $m[1] );
218 } else {
219 $head = rtrim( $head );
222 $this->cache['head'] = $head;
224 return $this->cache['head'];
228 * Get the SHA1 for the current HEAD of the repo
230 * @return string|false A SHA1 or false
232 public function getHeadSHA1() {
233 if ( !isset( $this->cache['headSHA1'] ) ) {
234 $head = $this->getHead();
235 $sha1 = false;
237 // If detached HEAD may be a SHA1
238 if ( self::isSHA1( $head ) ) {
239 $sha1 = $head;
240 } else {
241 // If not a SHA1 it may be a ref:
242 $refFile = "{$this->basedir}/{$head}";
243 $packedRefs = "{$this->basedir}/packed-refs";
244 $headRegex = preg_quote( $head, '/' );
245 if ( is_readable( $refFile ) ) {
246 $sha1 = rtrim( file_get_contents( $refFile ) );
247 } elseif ( is_readable( $packedRefs ) &&
248 preg_match( "/^([0-9A-Fa-f]{40}) $headRegex$/m", file_get_contents( $packedRefs ), $matches )
250 $sha1 = $matches[1];
253 $this->cache['headSHA1'] = $sha1;
255 return $this->cache['headSHA1'];
259 * Get the commit date of HEAD entry of the git code repository
261 * @since 1.22
262 * @return int|false Commit date (UNIX timestamp) or false
264 public function getHeadCommitDate() {
265 $gitBin = $this->options->get( MainConfigNames::GitBin );
267 if ( !isset( $this->cache['headCommitDate'] ) ) {
268 $date = false;
270 // Suppress warnings about any open_basedir restrictions affecting $wgGitBin (T74445).
271 $isFile = AtEase::quietCall( 'is_file', $gitBin );
272 if ( $isFile &&
273 is_executable( $gitBin ) &&
274 !Shell::isDisabled() &&
275 $this->getHead() !== false
277 $cmd = [
278 $gitBin,
279 'show',
280 '-s',
281 '--format=format:%ct',
282 'HEAD',
284 $gitDir = realpath( $this->basedir );
285 $result = Shell::command( $cmd )
286 ->environment( [ 'GIT_DIR' => $gitDir ] )
287 ->restrict( Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK )
288 ->allowPath( $gitDir, $this->repoDir )
289 ->execute();
291 if ( $result->getExitCode() === 0 ) {
292 $date = (int)$result->getStdout();
295 $this->cache['headCommitDate'] = $date;
297 return $this->cache['headCommitDate'];
301 * Get the name of the current branch, or HEAD if not found
303 * @return string|false The branch name, HEAD, or false
305 public function getCurrentBranch() {
306 if ( !isset( $this->cache['branch'] ) ) {
307 $branch = $this->getHead();
308 if ( $branch &&
309 preg_match( "#^refs/heads/(.*)$#", $branch, $m )
311 $branch = $m[1];
313 $this->cache['branch'] = $branch;
315 return $this->cache['branch'];
319 * Get an URL to a web viewer link to the HEAD revision.
321 * @return string|false String if a URL is available or false otherwise
323 public function getHeadViewUrl() {
324 $url = $this->getRemoteUrl();
325 if ( $url === false ) {
326 return false;
328 foreach ( $this->getViewers() as $repo => $viewer ) {
329 $pattern = '#^' . $repo . '$#';
330 if ( preg_match( $pattern, $url, $matches ) ) {
331 $viewerUrl = preg_replace( $pattern, $viewer, $url );
332 $headSHA1 = $this->getHeadSHA1();
333 $replacements = [
334 '%h' => substr( $headSHA1, 0, 7 ),
335 '%H' => $headSHA1,
336 '%r' => urlencode( $matches[1] ),
337 '%R' => $matches[1],
339 return strtr( $viewerUrl, $replacements );
342 return false;
346 * Get the URL of the remote origin.
347 * @return string|false String if a URL is available or false otherwise.
349 protected function getRemoteUrl() {
350 if ( !isset( $this->cache['remoteURL'] ) ) {
351 $config = "{$this->basedir}/config";
352 $url = false;
353 if ( is_readable( $config ) ) {
354 AtEase::suppressWarnings();
355 $configArray = parse_ini_file( $config, true );
356 AtEase::restoreWarnings();
357 $remote = false;
359 // Use the "origin" remote repo if available or any other repo if not.
360 if ( isset( $configArray['remote origin'] ) ) {
361 $remote = $configArray['remote origin'];
362 } elseif ( is_array( $configArray ) ) {
363 foreach ( $configArray as $sectionName => $sectionConf ) {
364 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
365 $remote = $sectionConf;
370 if ( $remote !== false && isset( $remote['url'] ) ) {
371 $url = $remote['url'];
374 $this->cache['remoteURL'] = $url;
376 return $this->cache['remoteURL'];
380 * Check to see if the current cache is fully populated.
382 * Note: This method is public only to make unit testing easier. There's
383 * really no strong reason that anything other than a test should want to
384 * call this method.
386 * @return bool True if all expected cache keys exist, false otherwise
388 public function cacheIsComplete() {
389 return isset( $this->cache['head'] ) &&
390 isset( $this->cache['headSHA1'] ) &&
391 isset( $this->cache['headCommitDate'] ) &&
392 isset( $this->cache['branch'] ) &&
393 isset( $this->cache['remoteURL'] );
397 * Precompute and cache git information.
399 * Creates a JSON file in the cache directory associated with this
400 * GitInfo instance. This cache file will be used by subsequent GitInfo objects referencing
401 * the same directory to avoid needing to examine the .git directory again.
403 * @since 1.24
405 public function precomputeValues() {
406 if ( $this->cacheFile !== null ) {
407 // Try to completely populate the cache
408 $this->getHead();
409 $this->getHeadSHA1();
410 $this->getHeadCommitDate();
411 $this->getCurrentBranch();
412 $this->getRemoteUrl();
414 if ( !$this->cacheIsComplete() ) {
415 $this->logger->debug(
416 "Failed to compute GitInfo for \"{$this->basedir}\""
418 return;
421 $cacheDir = dirname( $this->cacheFile );
422 if ( !file_exists( $cacheDir ) &&
423 !wfMkdirParents( $cacheDir, null, __METHOD__ )
425 throw new RuntimeException( "Unable to create GitInfo cache \"{$cacheDir}\"" );
428 file_put_contents( $this->cacheFile, FormatJson::encode( $this->cache ) );
433 * @see self::getHeadSHA1
434 * @return string
436 public static function headSHA1() {
437 return self::repo()->getHeadSHA1();
441 * @see self::getCurrentBranch
442 * @return string
444 public static function currentBranch() {
445 return self::repo()->getCurrentBranch();
449 * @see self::getHeadViewUrl()
450 * @return string|false
452 public static function headViewUrl() {
453 return self::repo()->getHeadViewUrl();
457 * Gets the list of repository viewers
458 * @return array
460 private function getViewers() {
461 if ( self::$viewers === false ) {
462 self::$viewers = $this->options->get( MainConfigNames::GitRepositoryViewers );
463 $this->hookRunner->onGitViewers( self::$viewers );
466 return self::$viewers;