7namespace MediaWiki\Utils;
9use MediaWiki\Config\ServiceOptions;
10use MediaWiki\HookContainer\HookRunner;
11use MediaWiki\Json\FormatJson;
12use MediaWiki\Logger\LoggerFactory;
13use MediaWiki\MainConfigNames;
14use MediaWiki\MediaWikiServices;
15use MediaWiki\Shell\Shell;
16use Psr\Log\LoggerInterface;
53 private static $viewers =
false;
56 private const CONSTRUCTOR_OPTIONS = [
63 private LoggerInterface $logger;
77 self::CONSTRUCTOR_OPTIONS, $services->getMainConfig()
79 $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
81 $this->cacheFile = $this->getCacheFilePath(
$repoDir );
82 $this->logger = LoggerFactory::getInstance(
'gitinfo' );
84 "Candidate cacheFile={$this->cacheFile} for {$repoDir}"
86 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
87 if ( $usePrecomputed &&
88 $this->cacheFile !==
null &&
89 is_readable( $this->cacheFile )
91 $this->cache = FormatJson::decode(
92 file_get_contents( $this->cacheFile ),
95 $this->logger->debug(
"Loaded git data from cache for {$repoDir}" );
99 $this->logger->debug(
"Cache incomplete for {$repoDir}" );
100 $this->basedir =
$repoDir . DIRECTORY_SEPARATOR .
'.git';
101 if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
102 $GITfile = file_get_contents( $this->basedir );
103 if ( strlen( $GITfile ) > 8 &&
104 substr( $GITfile, 0, 8 ) ===
'gitdir: '
106 $path = rtrim( substr( $GITfile, 8 ),
"\r\n" );
107 if (
$path[0] ===
'/' || substr(
$path, 1, 1 ) ===
':' ) {
109 $this->basedir =
$path;
126 private function getCacheFilePath(
$repoDir ) {
128 if ( $gitInfoCacheDirectory ===
false ) {
131 if ( $gitInfoCacheDirectory ) {
134 if ( $repoName ===
false ) {
138 $realIP = realpath( MW_INSTALL_PATH );
139 if ( str_starts_with( $repoName, $realIP ) ) {
141 $repoName = substr( $repoName, strlen( $realIP ) );
145 $repoName = strtr( $repoName, [
'/' =>
'-' ] );
146 $repoName = strtr( $repoName, [ DIRECTORY_SEPARATOR =>
'-' ] );
147 $fileName =
'info' . $repoName .
'.json';
148 $cachePath =
"{$gitInfoCacheDirectory}/{$fileName}";
149 if ( is_readable( $cachePath ) ) {
154 return "$repoDir/gitinfo.json";
160 public static function repo(): self {
161 self::
$repo ??= new self( MW_INSTALL_PATH );
172 return (
bool)preg_match(
'/^[0-9A-F]{40}$/i', $str );
181 if ( !isset( $this->cache[
'head'] ) ) {
182 $headFile =
"{$this->basedir}/HEAD";
185 if ( is_readable( $headFile ) ) {
186 $head = file_get_contents( $headFile );
188 if ( preg_match(
"/ref: (.*)/", $head, $m ) ) {
189 $head = rtrim( $m[1] );
191 $head = rtrim( $head );
194 $this->cache[
'head'] = $head;
196 return $this->cache[
'head'];
205 if ( !isset( $this->cache[
'headSHA1'] ) ) {
210 if ( self::isSHA1( $head ) ) {
214 $refFile =
"{$this->basedir}/{$head}";
215 $packedRefs =
"{$this->basedir}/packed-refs";
216 $headRegex = preg_quote( $head,
'/' );
217 if ( is_readable( $refFile ) ) {
218 $sha1 = rtrim( file_get_contents( $refFile ) );
219 } elseif ( is_readable( $packedRefs ) &&
220 preg_match(
"/^([0-9A-Fa-f]{40}) $headRegex$/m", file_get_contents( $packedRefs ), $matches )
225 $this->cache[
'headSHA1'] = $sha1;
227 return $this->cache[
'headSHA1'];
239 if ( !isset( $this->cache[
'headCommitDate'] ) ) {
244 $isFile = @is_file( $gitBin );
246 is_executable( $gitBin ) &&
254 '--format=format:%ct',
257 $gitDir = realpath( $this->basedir );
258 $result = Shell::command( $cmd )
259 ->environment( [
'GIT_DIR' => $gitDir ] )
260 ->restrict( Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK )
261 ->allowPath( $gitDir, $this->repoDir )
264 if ( $result->getExitCode() === 0 ) {
265 $date = (int)$result->getStdout();
268 $this->cache[
'headCommitDate'] = $date;
270 return $this->cache[
'headCommitDate'];
279 if ( !isset( $this->cache[
'branch'] ) ) {
282 preg_match(
"#^refs/heads/(.*)$#", $branch, $m )
286 $this->cache[
'branch'] = $branch;
288 return $this->cache[
'branch'];
298 if (
$url ===
false ) {
301 foreach ( $this->getViewers() as
$repo => $viewer ) {
302 $pattern =
'#^' .
$repo .
'$#';
303 if ( preg_match( $pattern,
$url, $matches ) ) {
304 $viewerUrl = preg_replace( $pattern, $viewer,
$url );
307 '%h' => substr( $headSHA1, 0, 7 ),
309 '%r' => urlencode( $matches[1] ),
312 return strtr( $viewerUrl, $replacements );
323 if ( !isset( $this->cache[
'remoteURL'] ) ) {
324 $config =
"{$this->basedir}/config";
326 if ( is_readable( $config ) ) {
328 $configArray = @parse_ini_file( $config,
true );
332 if ( isset( $configArray[
'remote origin'] ) ) {
333 $remote = $configArray[
'remote origin'];
334 } elseif ( is_array( $configArray ) ) {
335 foreach ( $configArray as $sectionName => $sectionConf ) {
336 if ( str_starts_with( $sectionName,
'remote' ) ) {
337 $remote = $sectionConf;
342 if ( $remote !==
false && isset( $remote[
'url'] ) ) {
343 $url = $remote[
'url'];
346 $this->cache[
'remoteURL'] =
$url;
348 return $this->cache[
'remoteURL'];
361 return isset( $this->cache[
'head'] ) &&
362 isset( $this->cache[
'headSHA1'] ) &&
363 isset( $this->cache[
'headCommitDate'] ) &&
364 isset( $this->cache[
'branch'] ) &&
365 isset( $this->cache[
'remoteURL'] );
378 if ( $this->cacheFile !==
null ) {
387 $this->logger->debug(
388 "Failed to compute GitInfo for \"{$this->basedir}\""
393 $cacheDir = dirname( $this->cacheFile );
394 if ( !file_exists( $cacheDir ) &&
397 throw new RuntimeException(
"Unable to create GitInfo cache \"{$cacheDir}\"" );
400 file_put_contents( $this->cacheFile, FormatJson::encode( $this->cache ) );
409 return self::repo()->getHeadSHA1();
417 return self::repo()->getCurrentBranch();
425 return self::repo()->getHeadViewUrl();
432 private function getViewers() {
433 if ( self::$viewers ===
false ) {
435 $this->hookRunner->onGitViewers( self::$viewers );
438 return self::$viewers;
443class_alias( GitInfo::class,
'GitInfo' );
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
const CacheDirectory
Name constant for the CacheDirectory setting, for use with Config\get().
const GitRepositoryViewers
Name constant for the GitRepositoryViewers setting, for use with Config\get().
const GitBin
Name constant for the GitBin setting, for use with Config\get().
const GitInfoCacheDirectory
Name constant for the GitInfoCacheDirectory setting, for use with Config\get().