+ /**
+ * Get the URL of the remote origin.
+ * @return string|bool string if a URL is available or false otherwise.
+ */
+ protected function getRemoteUrl() {
+ if ( !isset( $this->cache['remoteURL'] ) ) {
+ $config = "{$this->basedir}/config";
+ $url = false;
+ if ( is_readable( $config ) ) {
+ wfSuppressWarnings();
+ $configArray = parse_ini_file( $config, true );
+ wfRestoreWarnings();
+ $remote = false;
+
+ // Use the "origin" remote repo if available or any other repo if not.
+ if ( isset( $configArray['remote origin'] ) ) {
+ $remote = $configArray['remote origin'];
+ } elseif ( is_array( $configArray ) ) {
+ foreach ( $configArray as $sectionName => $sectionConf ) {
+ if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
+ $remote = $sectionConf;
+ }
+ }
+ }
+
+ if ( $remote !== false && isset( $remote['url'] ) ) {
+ $url = $remote['url'];
+ }
+ }
+ $this->cache['remoteURL'] = $url;
+ }
+ return $this->cache['remoteURL'];
+ }
+
+ /**
+ * Check to see if the current cache is fully populated.
+ *
+ * Note: This method is public only to make unit testing easier. There's
+ * really no strong reason that anything other than a test should want to
+ * call this method.
+ *
+ * @return bool True if all expected cache keys exist, false otherwise
+ */
+ public function cacheIsComplete() {
+ return isset( $this->cache['head'] ) &&
+ isset( $this->cache['headSHA1'] ) &&
+ isset( $this->cache['headCommitDate'] ) &&
+ isset( $this->cache['branch'] ) &&
+ isset( $this->cache['remoteURL'] );
+ }
+
+ /**
+ * Precompute and cache git information.
+ *
+ * Creates a JSON file in the cache directory associated with this
+ * GitInfo instance. This cache file will be used by subsequent GitInfo objects referencing
+ * the same directory to avoid needing to examine the .git directory again.
+ *
+ * @since 1.24
+ */
+ public function precomputeValues() {
+ if ( $this->cacheFile !== null ) {
+ // Try to completely populate the cache
+ $this->getHead();
+ $this->getHeadSHA1();
+ $this->getHeadCommitDate();
+ $this->getCurrentBranch();
+ $this->getRemoteUrl();
+
+ if ( !$this->cacheIsComplete() ) {
+ wfDebugLog( "Failed to compute GitInfo for \"{$this->basedir}\"" );
+ return;
+ }
+
+ $cacheDir = dirname( $this->cacheFile );
+ if ( !file_exists( $cacheDir ) &&
+ !wfMkdirParents( $cacheDir, null, __METHOD__ )
+ ) {
+ throw new MWException( "Unable to create GitInfo cache \"{$cacheDir}\"" );
+ }
+
+ file_put_contents( $this->cacheFile, FormatJson::encode( $this->cache ) );
+ }
+ }
+