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

Breadcrumb

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

function VersionGuesser::guessGitVersion

Parameters

array<string, mixed> $packageConfig:

Return value

array{version: string|null, commit: string|null, pretty_version: string|null, feature_version?: string|null, feature_pretty_version?: string|null}

1 call to VersionGuesser::guessGitVersion()
VersionGuesser::guessVersion in vendor/composer/composer/src/Composer/Package/Version/VersionGuesser.php
@phpstan-return Version|null

File

vendor/composer/composer/src/Composer/Package/Version/VersionGuesser.php, line 135

Class

VersionGuesser
Try to guess the current version number based on different VCS configuration.

Namespace

Composer\Package\Version

Code

private function guessGitVersion(array $packageConfig, string $path) : array {
    GitUtil::cleanEnv();
    $commit = null;
    $version = null;
    $prettyVersion = null;
    $featureVersion = null;
    $featurePrettyVersion = null;
    $isDetached = false;
    // try to fetch current version from git branch
    if (0 === $this->process
        ->execute([
        'git',
        'branch',
        '-a',
        '--no-color',
        '--no-abbrev',
        '-v',
    ], $output, $path)) {
        $branches = [];
        $isFeatureBranch = false;
        // find current branch and collect all branch names
        foreach ($this->process
            ->splitLines($output) as $branch) {
            if ($branch && Preg::isMatchStrictGroups('{^(?:\\* ) *(\\(no branch\\)|\\(detached from \\S+\\)|\\(HEAD detached at \\S+\\)|\\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
                if ($match[1] === '(no branch)' || strpos($match[1], '(detached ') === 0 || strpos($match[1], '(HEAD detached at') === 0) {
                    $version = 'dev-' . $match[2];
                    $prettyVersion = $version;
                    $isFeatureBranch = true;
                    $isDetached = true;
                }
                else {
                    $version = $this->versionParser
                        ->normalizeBranch($match[1]);
                    $prettyVersion = 'dev-' . $match[1];
                    $isFeatureBranch = $this->isFeatureBranch($packageConfig, $match[1]);
                }
                $commit = $match[2];
            }
            if ($branch && !Preg::isMatchStrictGroups('{^ *.+/HEAD }', $branch)) {
                if (Preg::isMatchStrictGroups('{^(?:\\* )? *((?:remotes/(?:origin|upstream)/)?[^\\s/]+) *([a-f0-9]+) .*$}', $branch, $match)) {
                    $branches[] = $match[1];
                }
            }
        }
        if ($isFeatureBranch) {
            $featureVersion = $version;
            $featurePrettyVersion = $prettyVersion;
            // try to find the best (nearest) version branch to assume this feature's version
            $result = $this->guessFeatureVersion($packageConfig, $version, $branches, [
                'git',
                'rev-list',
                '%candidate%..%branch%',
            ], $path);
            $version = $result['version'];
            $prettyVersion = $result['pretty_version'];
        }
    }
    GitUtil::checkForRepoOwnershipError($this->process
        ->getErrorOutput(), $path, $this->io);
    if (!$version || $isDetached) {
        $result = $this->versionFromGitTags($path);
        if ($result) {
            $version = $result['version'];
            $prettyVersion = $result['pretty_version'];
            $featureVersion = null;
            $featurePrettyVersion = null;
        }
    }
    if (null === $commit) {
        $command = array_merge([
            'git',
            'log',
            '--pretty=%H',
            '-n1',
            'HEAD',
        ], GitUtil::getNoShowSignatureFlags($this->process));
        if (0 === $this->process
            ->execute($command, $output, $path)) {
            $commit = trim($output) ?: null;
        }
    }
    if ($featureVersion) {
        return [
            'version' => $version,
            'commit' => $commit,
            'pretty_version' => $prettyVersion,
            'feature_version' => $featureVersion,
            'feature_pretty_version' => $featurePrettyVersion,
        ];
    }
    return [
        'version' => $version,
        'commit' => $commit,
        'pretty_version' => $prettyVersion,
    ];
}
RSS feed
Powered by Drupal