Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. HgDriver.php

class HgDriver

@author Per Bernhardt <plb@webfactory.de>

Hierarchy

  • class \Composer\Repository\Vcs\VcsDriver implements \Composer\Repository\Vcs\VcsDriverInterface
    • class \Composer\Repository\Vcs\HgDriver extends \Composer\Repository\Vcs\VcsDriver

Expanded class hierarchy of HgDriver

1 file declares its use of HgDriver
VersionGuesser.php in vendor/composer/composer/src/Composer/Package/Version/VersionGuesser.php

File

vendor/composer/composer/src/Composer/Repository/Vcs/HgDriver.php, line 27

Namespace

Composer\Repository\Vcs
View source
class HgDriver extends VcsDriver {
    
    /** @var array<int|string, string> Map of tag name to identifier */
    protected $tags;
    
    /** @var array<int|string, string> Map of branch name to identifier */
    protected $branches;
    
    /** @var string */
    protected $rootIdentifier;
    
    /** @var string */
    protected $repoDir;
    
    /**
     * @inheritDoc
     */
    public function initialize() : void {
        if (Filesystem::isLocalPath($this->url)) {
            $this->repoDir = $this->url;
        }
        else {
            if (!Cache::isUsable($this->config
                ->get('cache-vcs-dir'))) {
                throw new \RuntimeException('HgDriver requires a usable cache directory, and it looks like you set it to be disabled');
            }
            $cacheDir = $this->config
                ->get('cache-vcs-dir');
            $this->repoDir = $cacheDir . '/' . Preg::replace('{[^a-z0-9]}i', '-', Url::sanitize($this->url)) . '/';
            $fs = new Filesystem();
            $fs->ensureDirectoryExists($cacheDir);
            if (!is_writable(dirname($this->repoDir))) {
                throw new \RuntimeException('Can not clone ' . $this->url . ' to access package information. The "' . $cacheDir . '" directory is not writable by the current user.');
            }
            // Ensure we are allowed to use this URL by config
            $this->config
                ->prohibitUrlByConfig($this->url, $this->io);
            $hgUtils = new HgUtils($this->io, $this->config, $this->process);
            // update the repo if it is a valid hg repository
            if (is_dir($this->repoDir) && 0 === $this->process
                ->execute([
                'hg',
                'summary',
            ], $output, $this->repoDir)) {
                if (0 !== $this->process
                    ->execute([
                    'hg',
                    'pull',
                ], $output, $this->repoDir)) {
                    $this->io
                        ->writeError('<error>Failed to update ' . $this->url . ', package information from this repository may be outdated (' . $this->process
                        ->getErrorOutput() . ')</error>');
                }
            }
            else {
                // clean up directory and do a fresh clone into it
                $fs->removeDirectory($this->repoDir);
                $repoDir = $this->repoDir;
                $command = static function ($url) use ($repoDir) : array {
                    return [
                        'hg',
                        'clone',
                        '--noupdate',
                        '--',
                        $url,
                        $repoDir,
                    ];
                };
                $hgUtils->runCommand($command, $this->url, null);
            }
        }
        $this->getTags();
        $this->getBranches();
    }
    
    /**
     * @inheritDoc
     */
    public function getRootIdentifier() : string {
        if (null === $this->rootIdentifier) {
            $this->process
                ->execute([
                'hg',
                'tip',
                '--template',
                '{node}',
            ], $output, $this->repoDir);
            $output = $this->process
                ->splitLines($output);
            $this->rootIdentifier = $output[0];
        }
        return $this->rootIdentifier;
    }
    
    /**
     * @inheritDoc
     */
    public function getUrl() : string {
        return $this->url;
    }
    
    /**
     * @inheritDoc
     */
    public function getSource(string $identifier) : array {
        return [
            'type' => 'hg',
            'url' => $this->getUrl(),
            'reference' => $identifier,
        ];
    }
    
    /**
     * @inheritDoc
     */
    public function getDist(string $identifier) : ?array {
        return null;
    }
    
    /**
     * @inheritDoc
     */
    public function getFileContent(string $file, string $identifier) : ?string {
        if (isset($identifier[0]) && $identifier[0] === '-') {
            throw new \RuntimeException('Invalid hg identifier detected. Identifier must not start with a -, given: ' . $identifier);
        }
        $resource = [
            'hg',
            'cat',
            '-r',
            $identifier,
            '--',
            $file,
        ];
        $this->process
            ->execute($resource, $content, $this->repoDir);
        if (!trim($content)) {
            return null;
        }
        return $content;
    }
    
    /**
     * @inheritDoc
     */
    public function getChangeDate(string $identifier) : ?\DateTimeImmutable {
        $this->process
            ->execute([
            'hg',
            'log',
            '--template',
            '{date|rfc3339date}',
            '-r',
            $identifier,
        ], $output, $this->repoDir);
        return new \DateTimeImmutable(trim($output), new \DateTimeZone('UTC'));
    }
    
    /**
     * @inheritDoc
     */
    public function getTags() : array {
        if (null === $this->tags) {
            $tags = [];
            $this->process
                ->execute([
                'hg',
                'tags',
            ], $output, $this->repoDir);
            foreach ($this->process
                ->splitLines($output) as $tag) {
                if ($tag && Preg::isMatchStrictGroups('(^([^\\s]+)\\s+\\d+:(.*)$)', $tag, $match)) {
                    $tags[$match[1]] = $match[2];
                }
            }
            unset($tags['tip']);
            $this->tags = $tags;
        }
        return $this->tags;
    }
    
    /**
     * @inheritDoc
     */
    public function getBranches() : array {
        if (null === $this->branches) {
            $branches = [];
            $bookmarks = [];
            $this->process
                ->execute([
                'hg',
                'branches',
            ], $output, $this->repoDir);
            foreach ($this->process
                ->splitLines($output) as $branch) {
                if ($branch && Preg::isMatchStrictGroups('(^([^\\s]+)\\s+\\d+:([a-f0-9]+))', $branch, $match) && $match[1][0] !== '-') {
                    $branches[$match[1]] = $match[2];
                }
            }
            $this->process
                ->execute([
                'hg',
                'bookmarks',
            ], $output, $this->repoDir);
            foreach ($this->process
                ->splitLines($output) as $branch) {
                if ($branch && Preg::isMatchStrictGroups('(^(?:[\\s*]*)([^\\s]+)\\s+\\d+:(.*)$)', $branch, $match) && $match[1][0] !== '-') {
                    $bookmarks[$match[1]] = $match[2];
                }
            }
            // Branches will have preference over bookmarks
            $this->branches = array_merge($bookmarks, $branches);
        }
        return $this->branches;
    }
    
    /**
     * @inheritDoc
     */
    public static function supports(IOInterface $io, Config $config, string $url, bool $deep = false) : bool {
        if (Preg::isMatch('#(^(?:https?|ssh)://(?:[^@]+@)?bitbucket.org|https://(?:.*?)\\.kilnhg.com)#i', $url)) {
            return true;
        }
        // local filesystem
        if (Filesystem::isLocalPath($url)) {
            $url = Filesystem::getPlatformPath($url);
            if (!is_dir($url)) {
                return false;
            }
            $process = new ProcessExecutor($io);
            // check whether there is a hg repo in that path
            if ($process->execute([
                'hg',
                'summary',
            ], $output, $url) === 0) {
                return true;
            }
        }
        if (!$deep) {
            return false;
        }
        $process = new ProcessExecutor($io);
        $exit = $process->execute([
            'hg',
            'identify',
            '--',
            $url,
        ], $ignored);
        return $exit === 0;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
HgDriver::$branches protected property @var array&lt;int|string, string&gt; Map of branch name to identifier
HgDriver::$repoDir protected property @var string
HgDriver::$rootIdentifier protected property @var string
HgDriver::$tags protected property @var array&lt;int|string, string&gt; Map of tag name to identifier
HgDriver::getBranches public function @inheritDoc Overrides VcsDriverInterface::getBranches
HgDriver::getChangeDate public function @inheritDoc Overrides VcsDriverInterface::getChangeDate
HgDriver::getDist public function @inheritDoc Overrides VcsDriverInterface::getDist
HgDriver::getFileContent public function @inheritDoc Overrides VcsDriverInterface::getFileContent
HgDriver::getRootIdentifier public function @inheritDoc Overrides VcsDriverInterface::getRootIdentifier
HgDriver::getSource public function @inheritDoc Overrides VcsDriverInterface::getSource
HgDriver::getTags public function @inheritDoc Overrides VcsDriverInterface::getTags
HgDriver::getUrl public function @inheritDoc Overrides VcsDriverInterface::getUrl
HgDriver::initialize public function @inheritDoc Overrides VcsDriverInterface::initialize
HgDriver::supports public static function @inheritDoc Overrides VcsDriverInterface::supports
VcsDriver::$cache protected property @var ?Cache
VcsDriver::$config protected property @var Config
VcsDriver::$httpDownloader protected property @var HttpDownloader
VcsDriver::$infoCache protected property @var array&lt;int|string, mixed&gt;
VcsDriver::$io protected property @var IOInterface
VcsDriver::$originUrl protected property @var string
VcsDriver::$process protected property @var ProcessExecutor
VcsDriver::$repoConfig protected property @var array&lt;string, mixed&gt;
VcsDriver::$url protected property @var string
VcsDriver::cleanup public function @inheritDoc Overrides VcsDriverInterface::cleanup 1
VcsDriver::getBaseComposerInformation protected function
VcsDriver::getComposerInformation public function @inheritDoc Overrides VcsDriverInterface::getComposerInformation 4
VcsDriver::getContents protected function Get the remote content. 3
VcsDriver::getScheme protected function Get the https or http protocol depending on SSL support.
VcsDriver::hasComposerFile public function @inheritDoc Overrides VcsDriverInterface::hasComposerFile 1
VcsDriver::shouldCache protected function Returns whether or not the given $identifier should be cached or not. 1
VcsDriver::__construct final public function Constructor.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal