| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Utils; |
| 22 |
|
| 23 |
use MediaWiki\Config\ServiceOptions; |
| 24 |
use MediaWiki\HookContainer\HookRunner; |
| 25 |
use MediaWiki\Json\FormatJson; |
| 26 |
use MediaWiki\Logger\LoggerFactory; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
use MediaWiki\Shell\Shell; |
| 30 |
use Psr\Log\LoggerInterface; |
| 31 |
use RuntimeException; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class GitInfo { |
| 48 |
|
| 49 |
|
| 50 |
protected static $repo = null; |
| 51 |
|
| 52 |
|
| 53 |
protected $basedir; |
| 54 |
|
| 55 |
|
| 56 |
protected $repoDir; |
| 57 |
|
| 58 |
|
| 59 |
protected $cacheFile; |
| 60 |
|
| 61 |
|
| 62 |
protected $cache = []; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private static $viewers = false; |
| 68 |
|
| 69 |
|
| 70 |
private const CONSTRUCTOR_OPTIONS = [ |
| 71 |
MainConfigNames::CacheDirectory, |
| 72 |
MainConfigNames::GitBin, |
| 73 |
MainConfigNames::GitInfoCacheDirectory, |
| 74 |
MainConfigNames::GitRepositoryViewers, |
| 75 |
]; |
| 76 |
|
| 77 |
private LoggerInterface $logger; |
| 78 |
private ServiceOptions $options; |
| 79 |
private HookRunner $hookRunner; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function __construct( $repoDir, $usePrecomputed = true ) { |
| 88 |
$this->repoDir = $repoDir; |
| 89 |
$services = MediaWikiServices::getInstance(); |
| 90 |
$this->options = new ServiceOptions( |
| 91 |
self::CONSTRUCTOR_OPTIONS, $services->getMainConfig() |
| 92 |
); |
| 93 |
$this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 94 |
|
| 95 |
$this->cacheFile = $this->getCacheFilePath( $repoDir ); |
| 96 |
$this->logger = LoggerFactory::getInstance( 'gitinfo' ); |
| 97 |
$this->logger->debug( |
| 98 |
"Candidate cacheFile={$this->cacheFile} for {$repoDir}" |
| 99 |
); |
| 100 |
$this->hookRunner = new HookRunner( $services->getHookContainer() ); |
| 101 |
if ( $usePrecomputed && |
| 102 |
$this->cacheFile !== null && |
| 103 |
is_readable( $this->cacheFile ) |
| 104 |
) { |
| 105 |
$this->cache = FormatJson::decode( |
| 106 |
file_get_contents( $this->cacheFile ), |
| 107 |
true |
| 108 |
); |
| 109 |
$this->logger->debug( "Loaded git data from cache for {$repoDir}" ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( !$this->cacheIsComplete() ) { |
| 113 |
$this->logger->debug( "Cache incomplete for {$repoDir}" ); |
| 114 |
$this->basedir = $repoDir . DIRECTORY_SEPARATOR . '.git'; |
| 115 |
if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) { |
| 116 |
$GITfile = file_get_contents( $this->basedir ); |
| 117 |
if ( strlen( $GITfile ) > 8 && |
| 118 |
substr( $GITfile, 0, 8 ) === 'gitdir: ' |
| 119 |
) { |
| 120 |
$path = rtrim( substr( $GITfile, 8 ), "\r\n" ); |
| 121 |
if ( $path[0] === '/' || substr( $path, 1, 1 ) === ':' ) { |
| 122 |
|
| 123 |
$this->basedir = $path; |
| 124 |
} else { |
| 125 |
$this->basedir = $repoDir . DIRECTORY_SEPARATOR . $path; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private function getCacheFilePath( $repoDir ) { |
| 141 |
$gitInfoCacheDirectory = $this->options->get( MainConfigNames::GitInfoCacheDirectory ); |
| 142 |
if ( $gitInfoCacheDirectory === false ) { |
| 143 |
$gitInfoCacheDirectory = $this->options->get( MainConfigNames::CacheDirectory ) . '/gitinfo'; |
| 144 |
} |
| 145 |
if ( $gitInfoCacheDirectory ) { |
| 146 |
|
| 147 |
$repoName = realpath( $repoDir ); |
| 148 |
if ( $repoName === false ) { |
| 149 |
|
| 150 |
$repoName = $repoDir; |
| 151 |
} |
| 152 |
$realIP = realpath( MW_INSTALL_PATH ); |
| 153 |
if ( str_starts_with( $repoName, $realIP ) ) { |
| 154 |
|
| 155 |
$repoName = substr( $repoName, strlen( $realIP ) ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
$repoName = strtr( $repoName, [ '/' => '-' ] ); |
| 160 |
$repoName = strtr( $repoName, [ DIRECTORY_SEPARATOR => '-' ] ); |
| 161 |
$fileName = 'info' . $repoName . '.json'; |
| 162 |
$cachePath = "{$gitInfoCacheDirectory}/{$fileName}"; |
| 163 |
if ( is_readable( $cachePath ) ) { |
| 164 |
return $cachePath; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return "$repoDir/gitinfo.json"; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public static function repo() { |
| 177 |
if ( self::$repo === null ) { |
| 178 |
self::$repo = new self( MW_INSTALL_PATH ); |
| 179 |
} |
| 180 |
return self::$repo; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static function isSHA1( $str ) { |
| 190 |
return (bool)preg_match( '/^[0-9A-F]{40}$/i', $str ); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function getHead() { |
| 199 |
if ( !isset( $this->cache['head'] ) ) { |
| 200 |
$headFile = "{$this->basedir}/HEAD"; |
| 201 |
$head = false; |
| 202 |
|
| 203 |
if ( is_readable( $headFile ) ) { |
| 204 |
$head = file_get_contents( $headFile ); |
| 205 |
|
| 206 |
if ( preg_match( "/ref: (.*)/", $head, $m ) ) { |
| 207 |
$head = rtrim( $m[1] ); |
| 208 |
} else { |
| 209 |
$head = rtrim( $head ); |
| 210 |
} |
| 211 |
} |
| 212 |
$this->cache['head'] = $head; |
| 213 |
} |
| 214 |
return $this->cache['head']; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function getHeadSHA1() { |
| 223 |
if ( !isset( $this->cache['headSHA1'] ) ) { |
| 224 |
$head = $this->getHead(); |
| 225 |
$sha1 = false; |
| 226 |
|
| 227 |
|
| 228 |
if ( self::isSHA1( $head ) ) { |
| 229 |
$sha1 = $head; |
| 230 |
} else { |
| 231 |
|
| 232 |
$refFile = "{$this->basedir}/{$head}"; |
| 233 |
$packedRefs = "{$this->basedir}/packed-refs"; |
| 234 |
$headRegex = preg_quote( $head, '/' ); |
| 235 |
if ( is_readable( $refFile ) ) { |
| 236 |
$sha1 = rtrim( file_get_contents( $refFile ) ); |
| 237 |
} elseif ( is_readable( $packedRefs ) && |
| 238 |
preg_match( "/^([0-9A-Fa-f]{40}) $headRegex$/m", file_get_contents( $packedRefs ), $matches ) |
| 239 |
) { |
| 240 |
$sha1 = $matches[1]; |
| 241 |
} |
| 242 |
} |
| 243 |
$this->cache['headSHA1'] = $sha1; |
| 244 |
} |
| 245 |
return $this->cache['headSHA1']; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function getHeadCommitDate() { |
| 255 |
$gitBin = $this->options->get( MainConfigNames::GitBin ); |
| 256 |
|
| 257 |
if ( !isset( $this->cache['headCommitDate'] ) ) { |
| 258 |
$date = false; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
$isFile = @is_file( $gitBin ); |
| 263 |
if ( $isFile && |
| 264 |
is_executable( $gitBin ) && |
| 265 |
!Shell::isDisabled() && |
| 266 |
$this->getHead() !== false |
| 267 |
) { |
| 268 |
$cmd = [ |
| 269 |
$gitBin, |
| 270 |
'show', |
| 271 |
'-s', |
| 272 |
'--format=format:%ct', |
| 273 |
'HEAD', |
| 274 |
]; |
| 275 |
$gitDir = realpath( $this->basedir ); |
| 276 |
$result = Shell::command( $cmd ) |
| 277 |
->environment( [ 'GIT_DIR' => $gitDir ] ) |
| 278 |
->restrict( Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK ) |
| 279 |
->allowPath( $gitDir, $this->repoDir ) |
| 280 |
->execute(); |
| 281 |
|
| 282 |
if ( $result->getExitCode() === 0 ) { |
| 283 |
$date = (int)$result->getStdout(); |
| 284 |
} |
| 285 |
} |
| 286 |
$this->cache['headCommitDate'] = $date; |
| 287 |
} |
| 288 |
return $this->cache['headCommitDate']; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public function getCurrentBranch() { |
| 297 |
if ( !isset( $this->cache['branch'] ) ) { |
| 298 |
$branch = $this->getHead(); |
| 299 |
if ( $branch && |
| 300 |
preg_match( "#^refs/heads/(.*)$#", $branch, $m ) |
| 301 |
) { |
| 302 |
$branch = $m[1]; |
| 303 |
} |
| 304 |
$this->cache['branch'] = $branch; |
| 305 |
} |
| 306 |
return $this->cache['branch']; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function getHeadViewUrl() { |
| 315 |
$url = $this->getRemoteUrl(); |
| 316 |
if ( $url === false ) { |
| 317 |
return false; |
| 318 |
} |
| 319 |
foreach ( $this->getViewers() as $repo => $viewer ) { |
| 320 |
$pattern = '#^' . $repo . '$#'; |
| 321 |
if ( preg_match( $pattern, $url, $matches ) ) { |
| 322 |
$viewerUrl = preg_replace( $pattern, $viewer, $url ); |
| 323 |
$headSHA1 = $this->getHeadSHA1(); |
| 324 |
$replacements = [ |
| 325 |
'%h' => substr( $headSHA1, 0, 7 ), |
| 326 |
'%H' => $headSHA1, |
| 327 |
'%r' => urlencode( $matches[1] ), |
| 328 |
'%R' => $matches[1], |
| 329 |
]; |
| 330 |
return strtr( $viewerUrl, $replacements ); |
| 331 |
} |
| 332 |
} |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
protected function getRemoteUrl() { |
| 341 |
if ( !isset( $this->cache['remoteURL'] ) ) { |
| 342 |
$config = "{$this->basedir}/config"; |
| 343 |
$url = false; |
| 344 |
if ( is_readable( $config ) ) { |
| 345 |
|
| 346 |
$configArray = @parse_ini_file( $config, true ); |
| 347 |
$remote = false; |
| 348 |
|
| 349 |
|
| 350 |
if ( isset( $configArray['remote origin'] ) ) { |
| 351 |
$remote = $configArray['remote origin']; |
| 352 |
} elseif ( is_array( $configArray ) ) { |
| 353 |
foreach ( $configArray as $sectionName => $sectionConf ) { |
| 354 |
if ( str_starts_with( $sectionName, 'remote' ) ) { |
| 355 |
$remote = $sectionConf; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if ( $remote !== false && isset( $remote['url'] ) ) { |
| 361 |
$url = $remote['url']; |
| 362 |
} |
| 363 |
} |
| 364 |
$this->cache['remoteURL'] = $url; |
| 365 |
} |
| 366 |
return $this->cache['remoteURL']; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public function cacheIsComplete() { |
| 379 |
return isset( $this->cache['head'] ) && |
| 380 |
isset( $this->cache['headSHA1'] ) && |
| 381 |
isset( $this->cache['headCommitDate'] ) && |
| 382 |
isset( $this->cache['branch'] ) && |
| 383 |
isset( $this->cache['remoteURL'] ); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public function precomputeValues() { |
| 396 |
if ( $this->cacheFile !== null ) { |
| 397 |
|
| 398 |
$this->getHead(); |
| 399 |
$this->getHeadSHA1(); |
| 400 |
$this->getHeadCommitDate(); |
| 401 |
$this->getCurrentBranch(); |
| 402 |
$this->getRemoteUrl(); |
| 403 |
|
| 404 |
if ( !$this->cacheIsComplete() ) { |
| 405 |
$this->logger->debug( |
| 406 |
"Failed to compute GitInfo for \"{$this->basedir}\"" |
| 407 |
); |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
$cacheDir = dirname( $this->cacheFile ); |
| 412 |
if ( !file_exists( $cacheDir ) && |
| 413 |
!wfMkdirParents( $cacheDir, null, __METHOD__ ) |
| 414 |
) { |
| 415 |
throw new RuntimeException( "Unable to create GitInfo cache \"{$cacheDir}\"" ); |
| 416 |
} |
| 417 |
|
| 418 |
file_put_contents( $this->cacheFile, FormatJson::encode( $this->cache ) ); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public static function headSHA1() { |
| 427 |
return self::repo()->getHeadSHA1(); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public static function currentBranch() { |
| 435 |
return self::repo()->getCurrentBranch(); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public static function headViewUrl() { |
| 443 |
return self::repo()->getHeadViewUrl(); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
private function getViewers() { |
| 451 |
if ( self::$viewers === false ) { |
| 452 |
self::$viewers = $this->options->get( MainConfigNames::GitRepositoryViewers ); |
| 453 |
$this->hookRunner->onGitViewers( self::$viewers ); |
| 454 |
} |
| 455 |
|
| 456 |
return self::$viewers; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
class_alias( GitInfo::class, 'GitInfo' ); |