<commit_before><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\DependencyResolver;

use Composer\Package\BasePackage;
use Composer\Package\AliasPackage;
use Composer\Package\Version\VersionParser;
use Composer\Package\Link;
use Composer\Package\LinkConstraint\LinkConstraintInterface;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Repository\StreamableRepositoryInterface;
use Composer\Repository\PlatformRepository;

/**
 * A package pool contains repositories that provide packages.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Pool
{
    const MATCH_NAME = -1;
    const MATCH_NONE = 0;
    const MATCH = 1;
    const MATCH_PROVIDE = 2;
    const MATCH_REPLACE = 3;

    protected $repositories = array();
    protected $packages = array();
    protected $packageByName = array();
    protected $acceptableStabilities;
    protected $stabilityFlags;
    protected $versionParser;

    public function __construct($minimumStability = 'stable', array $stabilityFlags = array())
    {
        $stabilities = BasePackage::$stabilities;
        $this->versionParser = new VersionParser;
        $this->acceptableStabilities = array();
        foreach (BasePackage::$stabilities as $stability => $value) {
            if ($value <= BasePackage::$stabilities[$minimumStability]) {
                $this->acceptableStabilities[$stability] = $value;
            }
        }
        $this->stabilityFlags = $stabilityFlags;
    }

    /**
     * Adds a repository and its packages to this package pool
     *
     * @param RepositoryInterface $repo        A package repository
     * @param array               $rootAliases
     */
    public function addRepository(RepositoryInterface $repo, $rootAliases = array())
    {
        if ($repo instanceof CompositeRepository) {
            $repos = $repo->getRepositories();
        } else {
            $repos = array($repo);
        }

        $id = count($this->packages) + 1;
        foreach ($repos as $repo) {
            $this->repositories[] = $repo;

            $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface;
            if ($repo instanceof StreamableRepositoryInterface) {
                foreach ($repo->getMinimalPackages() as $package) {
                    $name = $package['name'];
                    $version = $package['version'];
                    $stability = VersionParser::parseStability($version);
                    if ($exempt || $this->isPackageAcceptable($name, $stability)) {
                        $package['id'] = $id++;
                        $this->packages[] = $package;

                        // collect names
                        $names = array(
                            $name => true,
                        );
                        if (isset($package['provide'])) {
                            foreach ($package['provide'] as $target => $constraint) {
                                $names[$target] = true;
                            }
                        }
                        if (isset($package['replace'])) {
                            foreach ($package['replace'] as $target => $constraint) {
                                $names[$target] = true;
                            }
                        }

                        foreach (array_keys($names) as $name) {
                            $this->packageByName[$name][] =& $this->packages[$id-2];
                        }

                        // handle root package aliases
                        if (isset($rootAliases[$name][$version])) {
                            $alias = $package;
                            unset($alias['raw']);
                            $alias['version'] = $rootAliases[$name][$version]['alias_normalized'];
                            $alias['alias'] = $rootAliases[$name][$version]['alias'];
                            $alias['alias_of'] = $package['id'];
                            $alias['id'] = $id++;
                            $alias['root_alias'] = true;
                            $this->packages[] = $alias;

                            foreach (array_keys($names) as $name) {
                                $this->packageByName[$name][] =& $this->packages[$id-2];
                            }
                        }

                        // handle normal package aliases
                        if (isset($package['alias'])) {
                            $alias = $package;
                            unset($alias['raw']);
                            $alias['version'] = $package['alias_normalized'];
                            $alias['alias'] = $package['alias'];
                            $alias['alias_of'] = $package['id'];
                            $alias['id'] = $id++;
                            $this->packages[] = $alias;

                            foreach (array_keys($names) as $name) {
                                $this->packageByName[$name][] =& $this->packages[$id-2];
                            }
                        }
                    }<commit_msg>Fix root aliasing and allow aliasing using the branch-alias as source<commit_after><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\DependencyResolver;

use Composer\Package\BasePackage;
use Composer\Package\AliasPackage;
use Composer\Package\Version\VersionParser;
use Composer\Package\Link;
use Composer\Package\LinkConstraint\LinkConstraintInterface;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Repository\StreamableRepositoryInterface;
use Composer\Repository\PlatformRepository;

/**
 * A package pool contains repositories that provide packages.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Pool
{
    const MATCH_NAME = -1;
    const MATCH_NONE = 0;
    const MATCH = 1;
    const MATCH_PROVIDE = 2;
    const MATCH_REPLACE = 3;

    protected $repositories = array();
    protected $packages = array();
    protected $packageByName = array();
    protected $acceptableStabilities;
    protected $stabilityFlags;
    protected $versionParser;

    public function __construct($minimumStability = 'stable', array $stabilityFlags = array())
    {
        $stabilities = BasePackage::$stabilities;
        $this->versionParser = new VersionParser;
        $this->acceptableStabilities = array();
        foreach (BasePackage::$stabilities as $stability => $value) {
            if ($value <= BasePackage::$stabilities[$minimumStability]) {
                $this->acceptableStabilities[$stability] = $value;
            }
        }
        $this->stabilityFlags = $stabilityFlags;
    }

    /**
     * Adds a repository and its packages to this package pool
     *
     * @param RepositoryInterface $repo        A package repository
     * @param array               $rootAliases
     */
    public function addRepository(RepositoryInterface $repo, $rootAliases = array())
    {
        if ($repo instanceof CompositeRepository) {
            $repos = $repo->getRepositories();
        } else {
            $repos = array($repo);
        }

        $id = count($this->packages) + 1;
        foreach ($repos as $repo) {
            $this->repositories[] = $repo;

            $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface;
            if ($repo instanceof StreamableRepositoryInterface) {
                foreach ($repo->getMinimalPackages() as $package) {
                    $name = $package['name'];
                    $version = $package['version'];
                    $stability = VersionParser::parseStability($version);
                    if ($exempt || $this->isPackageAcceptable($name, $stability)) {
                        $package['id'] = $id++;
                        $this->packages[] = $package;

                        // collect names
                        $names = array(
                            $name => true,
                        );
                        if (isset($package['provide'])) {
                            foreach ($package['provide'] as $target => $constraint) {
                                $names[$target] = true;
                            }
                        }
                        if (isset($package['replace'])) {
                            foreach ($package['replace'] as $target => $constraint) {
                                $names[$target] = true;
                            }
                        }

                        foreach (array_keys($names) as $provided) {
                            $this->packageByName[$provided][] =& $this->packages[$id-2];
                        }

                        // handle root package aliases
                        unset($rootAliasData);
                        if (isset($rootAliases[$name][$version])) {
                            $rootAliasData = $rootAliases[$name][$version];
                        } elseif (isset($package['alias_normalized']) && isset($rootAliases[$name][$package['alias_normalized']])) {
                            $rootAliasData = $rootAliases[$name][$package['alias_normalized']];
                        }

                        if (isset($rootAliasData)) {
                            $alias = $package;
                            unset($alias['raw']);
                            $alias['version'] = $rootAliasData['alias_normalized'];
                            $alias['alias'] = $rootAliasData['alias'];
                            $alias['alias_of'] = $package['id'];
                            $alias['id'] = $id++;
                            $alias['root_alias'] = true;
                            $this->packages[] = $alias;

                            foreach (array_keys($names) as $name) {
                                $this->packageByName[$name][] =& $this->packages[$id-2];
                            }
                        }

                        // handle normal package aliases
                        if (isset($package['alias'])) {
                            $alias = $package;
                            unset($alias['raw']);
                            $alias['version'] = $package['alias_normalized'];
                            $alias['alias'] = $package['alias'];
                            $alias['alias_of'] = $package['id'];
                            $alias['id'] = $id++;
                            $this->packages[] = $alias;

                            foreach (array_keys($names) as $name) {
                                $this->packageByName[$name][] =& $this->packages[$id-2];
                            }
                        }
                    }
{'fraction_non_alphanumeric': 0.12192118226600986, 'fraction_numerical': 0.0015833919774806474, 'mean_word_length': 1.545678459471563, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6633569', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2643, 'n_words': 582}